summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorAlejandro Sirgo Rica <asirgo@soleta.eu>2024-04-22 13:55:21 +0200
committerAlejandro Sirgo Rica <asirgo@soleta.eu>2024-07-29 15:07:22 +0200
commit41b5f830c6d74dca69cfea1c259f7e21d4ad9bb9 (patch)
treecbd445bd832867d16e44c48afad5243472739ed5 /src
parente20cda122b87cc4ad62eaccd412d918aa38768cb (diff)
utils: consolidate code to find efi loader
Add find_windows_efi_loader and find_linux_efi_loader to reduce code duplication and to centralize efi loader path modifications.
Diffstat (limited to 'src')
-rw-r--r--src/utils/boot.py26
-rw-r--r--src/utils/uefi.py33
2 files changed, 29 insertions, 30 deletions
diff --git a/src/utils/boot.py b/src/utils/boot.py
index 399f4ba..244f994 100644
--- a/src/utils/boot.py
+++ b/src/utils/boot.py
@@ -62,19 +62,12 @@ def _boot_uefi_windows(disk, part, mountpoint):
if not mount_mkdir(esp, esp_mountpoint):
raise OgError(f'Unable to mount detected EFI System Partition at {esp} into {esp_mountpoint}')
- loader_paths = [f'{esp_mountpoint}/EFI/{bootlabel}/Boot/bootmgfw.efi',
- f'{esp_mountpoint}/EFI/Microsoft/Boot/bootmgfw.efi']
try:
- for efi_app in loader_paths:
- if os.path.exists(efi_app):
- loader = efi_app[len(esp_mountpoint):]
- logging.info(f'Found bootloader at ESP partition: {loader}')
- break
- else:
- raise OgError(f'Unable to locate Windows EFI bootloader bootmgfw.efi')
+ loader_path = find_windows_efi_loader(esp_mountpoint, bootlabel)
+ loader_path = loader_path[len(esp_mountpoint):]
efibootmgr_delete_bootentry(bootlabel)
- efibootmgr_create_bootentry(esp_disk, esp_part_number, loader, bootlabel)
+ efibootmgr_create_bootentry(esp_disk, esp_part_number, loader_path, bootlabel)
efibootmgr_bootnext(bootlabel)
finally:
umount(esp_mountpoint)
@@ -87,19 +80,12 @@ def _boot_uefi_linux(disk, part, mountpoint):
if not mount_mkdir(esp, esp_mountpoint):
raise OgError(f'Unable to mount detected EFI System Partition at {esp} into {esp_mountpoint}')
- loader_paths = [f'{esp_mountpoint}/EFI/{bootlabel}/Boot/shimx64.efi',
- f'{esp_mountpoint}/EFI/ubuntu/shimx64.efi']
try:
- for efi_app in loader_paths:
- if os.path.exists(efi_app):
- loader = efi_app[len(esp_mountpoint):]
- logging.info(f'Found bootloader at ESP partition: {loader}')
- break
- else:
- raise OgError(f'Unable to locate Linux EFI bootloader shimx64.efi')
+ loader_path = find_linux_efi_loader(esp_mountpoint, bootlabel)
+ loader_path = loader_path[len(esp_mountpoint):]
efibootmgr_delete_bootentry(bootlabel)
- efibootmgr_create_bootentry(esp_disk, esp_part_number, loader, bootlabel)
+ efibootmgr_create_bootentry(esp_disk, esp_part_number, loader_path, bootlabel)
efibootmgr_bootnext(bootlabel)
finally:
umount(esp_mountpoint)
diff --git a/src/utils/uefi.py b/src/utils/uefi.py
index 0906ae9..48fb6c3 100644
--- a/src/utils/uefi.py
+++ b/src/utils/uefi.py
@@ -103,6 +103,28 @@ def efibootmgr_create_bootentry(disk, part, loader, label, add_to_bootorder=True
raise OgError(f'Unexpected error adding boot entry to nvram. UEFI firmware might be buggy') from e
+def _find_efi_loader(loader_paths):
+ for efi_app in loader_paths:
+ logging.info(f'Searching for {efi_app}')
+ if os.path.exists(efi_app):
+ logging.info(f'Found bootloader at ESP partition: {efi_app}')
+ return efi_app
+ else:
+ raise OgError(f'Unable to locate Windows EFI bootloader bootmgfw.efi')
+
+
+def find_windows_efi_loader(esp_mountpoint, bootlabel):
+ loader_paths = [f'{esp_mountpoint}/EFI/{bootlabel}/Boot/bootmgfw.efi',
+ f'{esp_mountpoint}/EFI/Microsoft/Boot/bootmgfw.efi']
+ return _find_efi_loader(loader_paths)
+
+
+def find_linux_efi_loader(esp_mountpoint, bootlabel):
+ loader_paths = [f'{esp_mountpoint}/EFI/{bootlabel}/Boot/shimx64.efi',
+ f'{esp_mountpoint}/EFI/ubuntu/shimx64.efi']
+ return _find_efi_loader(loader_paths)
+
+
def copy_windows_efi_bootloader(disk, partition):
device = get_partition_device(disk, partition)
mountpoint = device.replace('dev', 'mnt')
@@ -122,17 +144,8 @@ def copy_windows_efi_bootloader(disk, partition):
umount(mountpoint)
raise OgError(f'Unable to mount detected EFI System Partition at {esp} into {esp_mountpoint}')
- loader_paths = [f'{esp_mountpoint}/EFI/{bootlabel}/Boot/bootmgfw.efi',
- f'{esp_mountpoint}/EFI/Microsoft/Boot/bootmgfw.efi']
try:
- for efi_app in loader_paths:
- logging.info(f'Searching for {efi_app}')
- if os.path.exists(efi_app):
- loader = efi_app
- logging.info(f'Found bootloader at ESP partition: {loader}')
- break
- else:
- raise OgError(f'Unable to locate Windows EFI bootloader bootmgfw.efi')
+ loader = find_windows_efi_loader(esp_mountpoint, bootlabel)
loader_dir = os.path.dirname(loader)
destination_dir = f'{mountpoint}/ogBoot'