Skip to content

Commit 80411d2

Browse files
committed
boot-qemu.py + utils.py: Refactor finding EFI images
This will be useful for follow up changes and helps reduce the amount of duplicate code. Signed-off-by: Nathan Chancellor <nathan@kernel.org>
1 parent 6bbfed7 commit 80411d2

2 files changed

Lines changed: 31 additions & 17 deletions

File tree

boot-qemu.py

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -395,13 +395,11 @@ def get_efi_args(guest_arch):
395395
Path("edk2/aarch64/QEMU_EFI.fd"), # Arch Linux (current)
396396
Path("edk2-armvirt/aarch64/QEMU_EFI.fd"), # Arch Linux (old)
397397
Path("qemu-efi-aarch64/QEMU_EFI.fd"), # Debian and Ubuntu
398-
None, # Terminator
399398
],
400399
"x86_64": [
401400
Path("edk2/x64/OVMF_CODE.fd"), # Arch Linux (current), Fedora
402401
Path("edk2-ovmf/x64/OVMF_CODE.fd"), # Arch Linux (old)
403402
Path("OVMF/OVMF_CODE.fd"), # Debian and Ubuntu
404-
None, # Terminator
405403
],
406404
} # yapf: disable
407405

@@ -411,13 +409,8 @@ def get_efi_args(guest_arch):
411409
)
412410
return []
413411

414-
for efi_img_location in efi_img_locations[guest_arch]:
415-
if efi_img_location is None:
416-
raise FileNotFoundError(
417-
f"edk2 could not be found for {guest_arch}!")
418-
efi_img = Path("/usr/share", efi_img_location)
419-
if efi_img.exists():
420-
break
412+
usr_share = Path('/usr/share')
413+
efi_img = utils.find_first_file(usr_share, efi_img_locations[guest_arch])
421414

422415
if guest_arch == "arm64":
423416
# Sizing the images to 64M is recommended by "Prepare the firmware" section at
@@ -442,15 +435,8 @@ def get_efi_args(guest_arch):
442435
efi_vars_locations = [
443436
Path("edk2/x64/OVMF_VARS.fd"), # Arch Linux and Fedora
444437
Path("OVMF/OVMF_VARS.fd"), # Debian and Ubuntu
445-
None, # Terminator
446438
]
447-
for efi_vars_location in efi_vars_locations:
448-
if efi_vars_location is None:
449-
raise FileNotFoundError("OVMF_VARS.fd could not be found!")
450-
efi_vars = Path('/usr/share', efi_vars_location)
451-
if efi_vars.exists():
452-
break
453-
439+
efi_vars = utils.find_first_file(usr_share, efi_vars_locations)
454440
efi_vars_qemu = base_folder.joinpath("images", guest_arch,
455441
efi_vars.name)
456442
shutil.copyfile(efi_vars, efi_vars_qemu)

utils.py

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

3232

33+
def find_first_file(relative_root, possible_files, required=True):
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+
required (bool): Whether or not the file is required, which determines
45+
if not finding the file is an error.
46+
Returns:
47+
The full path to the first file found in the list. If none could be
48+
found, an Exception is raised.
49+
"""
50+
for possible_file in possible_files:
51+
if (full_path := relative_root.joinpath(possible_file)).exists():
52+
return full_path
53+
if required:
54+
files_str = "', '".join([str(elem) for elem in possible_files])
55+
raise FileNotFoundError(
56+
f"No files from list ('{files_str}') could be found within '{relative_root}'!",
57+
)
58+
return None
59+
60+
3361
def get_full_kernel_path(kernel_location, image, arch=None):
3462
"""
3563
Get the full path to a kernel image based on the architecture and image

0 commit comments

Comments
 (0)