Skip to content

Commit f3173cb

Browse files
committed
Refactor finding EFI images
This will be useful for a follow up commit, which will attempt to look for a binary relative to QEMU's prefix. It happens to simplify the code as well. Signed-off-by: Nathan Chancellor <nathan@kernel.org>
1 parent 0466026 commit f3173cb

2 files changed

Lines changed: 26 additions & 16 deletions

File tree

boot-qemu.py

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -378,13 +378,11 @@ def get_efi_args(guest_arch):
378378
Path("edk2/aarch64/QEMU_EFI.fd"), # Arch Linux (current)
379379
Path("edk2-armvirt/aarch64/QEMU_EFI.fd"), # Arch Linux (old)
380380
Path("qemu-efi-aarch64/QEMU_EFI.fd"), # Debian and Ubuntu
381-
None # Terminator
382381
],
383382
"x86_64": [
384383
Path("edk2/x64/OVMF_CODE.fd"), # Arch Linux (current), Fedora
385384
Path("edk2-ovmf/x64/OVMF_CODE.fd"), # Arch Linux (old)
386385
Path("OVMF/OVMF_CODE.fd"), # Debian and Ubuntu
387-
None # Terminator
388386
]
389387
} # yapf: disable
390388

@@ -394,12 +392,8 @@ def get_efi_args(guest_arch):
394392
)
395393
return []
396394

397-
for efi_img_location in efi_img_locations[guest_arch]:
398-
if efi_img_location is None:
399-
raise Exception(f"edk2 could not be found for {guest_arch}!")
400-
efi_img = Path("/usr/share", efi_img_location)
401-
if efi_img.exists():
402-
break
395+
usr_share = Path('/usr/share')
396+
efi_img = utils.find_first_file(usr_share, efi_img_locations[guest_arch])
403397

404398
if guest_arch == "arm64":
405399
# Sizing the images to 64M is recommended by "Prepare the firmware" section at
@@ -422,15 +416,8 @@ def get_efi_args(guest_arch):
422416
efi_vars_locations = [
423417
Path("edk2/x64/OVMF_VARS.fd"), # Arch Linux and Fedora
424418
Path("OVMF/OVMF_VARS.fd"), # Debian and Ubuntu
425-
None # Terminator
426419
]
427-
for efi_vars_location in efi_vars_locations:
428-
if efi_vars_location is None:
429-
raise Exception("OVMF_VARS.fd could not be found!")
430-
efi_vars = Path('/usr/share', efi_vars_location)
431-
if efi_vars.exists():
432-
break
433-
420+
efi_vars = utils.find_first_file(usr_share, efi_vars_locations)
434421
efi_vars_qemu = base_folder.joinpath("images", guest_arch,
435422
efi_vars.name)
436423
shutil.copyfile(efi_vars, efi_vars_qemu)

utils.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,29 @@ def die(string):
3030
sys.exit(1)
3131

3232

33+
def find_first_file(relative_root, possible_files):
34+
"""
35+
Attempts to find the first option available in the list of files relative
36+
to a specified root folder.
37+
38+
Parameters:
39+
relative_root (Path): A Path object containing the folder to search for
40+
files within.
41+
possible_files (list): A list of Paths that may be within the relative
42+
root folder. They will be automatically appended
43+
to relative_root.
44+
Returns:
45+
The full path to the first file found in the list. If none could be
46+
found, an Exception is raised.
47+
"""
48+
for possible_file in possible_files:
49+
if (full_path := relative_root.joinpath(possible_file)).exists():
50+
return full_path
51+
raise Exception(
52+
f"No files from list ('{', '.join(possible_files)}') could be found within '{relative_root}'!"
53+
)
54+
55+
3356
def get_full_kernel_path(kernel_location, image, arch=None):
3457
"""
3558
Get the full path to a kernel image based on the architecture and image

0 commit comments

Comments
 (0)