From 425020a849c98226c0bac04db0d63a8e55ae5bad Mon Sep 17 00:00:00 2001 From: Adeeb Shihadeh Date: Wed, 29 Sep 2021 11:08:19 -0700 Subject: [PATCH] agnos updater: support non-sparse images (#22371) * print progress for all partitions * noop generator * less spammy * cleanup --- selfdrive/hardware/tici/agnos.py | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/selfdrive/hardware/tici/agnos.py b/selfdrive/hardware/tici/agnos.py index d3b2ca96ec..526878aa4c 100755 --- a/selfdrive/hardware/tici/agnos.py +++ b/selfdrive/hardware/tici/agnos.py @@ -73,6 +73,11 @@ def unsparsify(f: StreamingDecompressor) -> Generator[bytes, None, None]: else: raise Exception("Unhandled sparse chunk type") +# noop wrapper with same API as unsparsify() for non sparse images +def noop(f: StreamingDecompressor) -> Generator[bytes, None, None]: + while not f.eof: + yield f.read(1024 * 1024) + def get_target_slot_number() -> int: current_slot = subprocess.check_output(["abctl", "--boot_slot"], encoding='utf-8').strip() @@ -141,22 +146,20 @@ def flash_partition(target_slot_number: int, partition: dict, cloudlog): path = get_partition_path(target_slot_number, partition) with open(path, 'wb+') as out: - partition_size = partition['size'] - # Flash partition - if partition['sparse']: - raw_hash = hashlib.sha256() - for chunk in unsparsify(downloader): - raw_hash.update(chunk) - out.write(chunk) - p = int(out.tell() / partition_size * 100) + last_p = 0 + raw_hash = hashlib.sha256() + f = unsparsify if partition['sparse'] else noop + for chunk in f(downloader): + raw_hash.update(chunk) + out.write(chunk) + p = int(out.tell() / partition['size'] * 100) + if p != last_p: + last_p = p print(f"Installing {partition['name']}: {p}") - if raw_hash.hexdigest().lower() != partition['hash_raw'].lower(): - raise Exception(f"Unsparse hash mismatch '{raw_hash.hexdigest().lower()}'") - else: - while not downloader.eof: - out.write(downloader.read(1024 * 1024)) + if raw_hash.hexdigest().lower() != partition['hash_raw'].lower(): + raise Exception(f"Raw hash mismatch '{raw_hash.hexdigest().lower()}'") if downloader.sha256.hexdigest().lower() != partition['hash'].lower(): raise Exception("Uncompressed hash mismatch")