summaryrefslogtreecommitdiff
path: root/ironic_python_agent/efi_utils.py
diff options
context:
space:
mode:
authorJulia Kreger <juliaashleykreger@gmail.com>2023-04-07 07:32:44 -0700
committerJulia Kreger <juliaashleykreger@gmail.com>2023-04-27 15:57:21 +0000
commit09c3c412193187c8b71f8fd3326b012d8f0953bf (patch)
tree9f36227318d4d404c2bdd52a735cd4ddea84d329 /ironic_python_agent/efi_utils.py
parentfa7e33b0b48480f0836f9ded8561cd35bf40ebab (diff)
downloadironic-python-agent-stable/zed.tar.gz
Fix UTF-16 result handling for efibootmgrstable/zed
The tl;dr is that UEFI NVRAM is in encoded in UTF-16, and when we run the efibootmgr command, we can get unicode characters back. Except we previously were forcing everything to be treated as UTF-8 due to the way oslo.concurrency's processutils module works. This could be observed with UTF character 0x00FF which raises up a nice exception when we try to decode it. Anyhow! while fixing handling of this, we discovered we could get basically the cruft out of the NVRAM, by getting what was most likey a truncated string out of our own test VMs. As such, we need to also permit decoding to be tollerant of failures. This could be binary data or as simple as flipped bits which get interpretted invalid characters. As such, we have introduced such data into one of our tests involving UEFI record de-duplication. Closes-Bug: 2015602 Change-Id: I006535bf124379ed65443c7b283bc99ecc95568b (cherry picked from commit 76accfb880474445a5dcb07825889123b3dd0237)
Diffstat (limited to 'ironic_python_agent/efi_utils.py')
-rw-r--r--ironic_python_agent/efi_utils.py15
1 files changed, 11 insertions, 4 deletions
diff --git a/ironic_python_agent/efi_utils.py b/ironic_python_agent/efi_utils.py
index 48e643d3..6fc8dff1 100644
--- a/ironic_python_agent/efi_utils.py
+++ b/ironic_python_agent/efi_utils.py
@@ -275,8 +275,15 @@ def get_boot_records():
:return: an iterator yielding pairs (boot number, boot record).
"""
- efi_output = utils.execute('efibootmgr', '-v')
- for line in efi_output[0].split('\n'):
+ # Invokes binary=True so we get a bytestream back.
+ efi_output = utils.execute('efibootmgr', '-v', binary=True)
+ # Bytes must be decoded before regex can be run and
+ # matching to work as intended.
+ # Also ignore errors on decoding, as we can basically get
+ # garbage out of the nvram record, this way we don't fail
+ # hard on unrelated records.
+ cmd_output = efi_output[0].decode('utf-16', errors='ignore')
+ for line in cmd_output.split('\n'):
match = _ENTRY_LABEL.match(line)
if match is not None:
yield (match[1], match[2])
@@ -293,7 +300,7 @@ def add_boot_record(device, efi_partition, loader, label):
# https://linux.die.net/man/8/efibootmgr
utils.execute('efibootmgr', '-v', '-c', '-d', device,
'-p', str(efi_partition), '-w', '-L', label,
- '-l', loader)
+ '-l', loader, binary=True)
def remove_boot_record(boot_num):
@@ -301,7 +308,7 @@ def remove_boot_record(boot_num):
:param boot_num: the number of the boot record
"""
- utils.execute('efibootmgr', '-b', boot_num, '-B')
+ utils.execute('efibootmgr', '-b', boot_num, '-B', binary=True)
def _run_efibootmgr(valid_efi_bootloaders, device, efi_partition,