flash: fix encoding handling of files/programs
Make sure we use bytes when reading binary image data, and UTF-8
encoding when parsing simple command output as strings.
BUG=chromium:997354
TEST=`./run_tests` passes
Change-Id: I679d06dbabef76962c2d175aa64aeecb8e24da80
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/chromite/+/1932720
Reviewed-by: Jack Rosenthal <jrosenth@chromium.org>
Tested-by: Mike Frysinger <vapier@chromium.org>
diff --git a/cli/flash.py b/cli/flash.py
index c61d21a..f653d99 100644
--- a/cli/flash.py
+++ b/cli/flash.py
@@ -46,7 +46,7 @@
"""Get the Pid of dd."""
try:
pids = cros_build_lib.run(['pgrep', 'dd'], capture_output=True,
- print_cmd=False).output
+ print_cmd=False, encoding='utf-8').stdout
for pid in pids.splitlines():
if osutils.IsChildProcess(int(pid), name='dd'):
return int(pid)
@@ -92,11 +92,11 @@
require_pmbr: Whether to require a PMBR in LBA0.
"""
if os.path.isfile(file_path):
- with open(file_path) as image_file:
+ with open(file_path, 'rb') as image_file:
if require_pmbr:
# Seek to the end of LBA0 and look for the PMBR boot signature.
image_file.seek(0x1fe)
- if image_file.read(2) != '\x55\xaa':
+ if image_file.read(2) != b'\x55\xaa':
return False
# Current file position is start of LBA1 now.
else:
@@ -104,7 +104,7 @@
image_file.seek(0x200)
# See if there's a GPT here.
- if image_file.read(8) == 'EFI PART':
+ if image_file.read(8) == b'EFI PART':
return True
return False
@@ -231,7 +231,7 @@
if logging.getLogger().getEffectiveLevel() <= logging.NOTICE:
op = UsbImagerOperation(image)
op.Run(cros_build_lib.sudo_run, cmd, debug_level=logging.NOTICE,
- update_period=0.5)
+ encoding='utf-8', update_period=0.5)
else:
cros_build_lib.sudo_run(
cmd, debug_level=logging.NOTICE,