summaryrefslogtreecommitdiffstats
path: root/src/utils/uefi.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils/uefi.py')
-rw-r--r--src/utils/uefi.py31
1 files changed, 27 insertions, 4 deletions
diff --git a/src/utils/uefi.py b/src/utils/uefi.py
index 5ca8270..c0bc958 100644
--- a/src/utils/uefi.py
+++ b/src/utils/uefi.py
@@ -54,21 +54,44 @@ def _check_efibootmgr_json():
def is_uefi_supported():
return os.path.exists("/sys/firmware/efi")
+def _validate_efibootmgr_json(dict_json):
+ missing_fields = []
+ if not 'BootOrder' in dict_json:
+ missing_fields.append('BootOrder')
-def run_efibootmgr_json():
+ if not 'vars' in dict_json:
+ missing_fields.append('vars')
+
+ for i, entry in enumerate(dict_json.get('vars', [])):
+ if not 'name' in entry:
+ missing_fields.append(f'vars[{i}].name')
+
+ if not 'active' in entry:
+ missing_fields.append('vars[{i}].active')
+
+ if not 'description' in entry:
+ missing_fields.append('vars[{i}].description')
+
+ if missing_fields:
+ raise OgError('Missing required efibootmgr fields: ' + ', '.join(missing_fields))
+
+def run_efibootmgr_json(validate):
if _check_efibootmgr_json() is False:
raise OgError(f'{EFIBOOTMGR_BIN} not available')
proc = subprocess.run([EFIBOOTMGR_BIN, '--json'], capture_output=True, text=True)
dict_json = json.loads(proc.stdout)
+ if validate:
+ _validate_efibootmgr_json(dict_json)
+
return dict_json
def efibootmgr_bootnext(description):
logging.info(f'Setting BootNext to value from boot entry with label {description}')
bootnext_cmd = '{efibootmgr} -n {bootnum}'
- boot_entries = run_efibootmgr_json().get('vars', [])
+ boot_entries = run_efibootmgr_json(validate=False).get('vars', [])
entry = _find_bootentry(boot_entries, description)
num = _strip_boot_prefix(entry) # efibootmgr output uses BootXXXX for each entry, remove the "Boot" prefix.
@@ -78,7 +101,7 @@ def efibootmgr_bootnext(description):
def efibootmgr_delete_bootentry(label):
- dict_json = run_efibootmgr_json()
+ dict_json = run_efibootmgr_json(validate=False)
efibootmgr_cmd = '{efibootmgr} -q -b {bootnum} -B'
for entry in dict_json.get('vars', []):
@@ -92,7 +115,7 @@ def efibootmgr_delete_bootentry(label):
def efibootmgr_create_bootentry(disk, part, loader, label, add_to_bootorder=True):
- entries = run_efibootmgr_json().get('vars', [])
+ entries = run_efibootmgr_json(validate=False).get('vars', [])
create_opt = '-c' if add_to_bootorder else '-C'
index_opt = f'-I {len(entries)}' # Requires efibootmgr version 18
efibootmgr_cmd = f'{EFIBOOTMGR_BIN} {create_opt} {index_opt} -d {disk} -p {part} -L {label} -l {loader}'