From 292c3d47cb767b4bc6f1d784644e087c935125f7 Mon Sep 17 00:00:00 2001 From: Emmanuel Blot Date: Fri, 24 Oct 2025 09:39:38 +0200 Subject: [PATCH 1/2] [ot] python/qemu: ot.rom.image: fix VMEM handling input stream is a binary string, not a text string. RE should match bytes, not string. Signed-off-by: Emmanuel Blot --- python/qemu/ot/rom/image.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/qemu/ot/rom/image.py b/python/qemu/ot/rom/image.py index e481485fc78f7..8e3d3524f156a 100644 --- a/python/qemu/ot/rom/image.py +++ b/python/qemu/ot/rom/image.py @@ -395,7 +395,7 @@ def _load_vmem(self, rfp: BinaryIO, size: Optional[int]) -> None: line = line.strip() if not line.startswith(b'@'): continue - parts = re.split(r'\s+', line[1:]) + parts = re.split(rb'\s+', line[1:]) address_str = parts[0] data_str = parts[1:] scrambled_data = all(len(d) in (9, 10) for d in data_str) From 9c465f41febce79a24000d2c828fe3254b8d38c2 Mon Sep 17 00:00:00 2001 From: Emmanuel Blot Date: Thu, 23 Oct 2025 18:52:12 +0200 Subject: [PATCH 2/2] [ot] scripts/opentitan: romtool.py: add a new option to report the ROM digest Signed-off-by: Emmanuel Blot --- docs/opentitan/romtool.md | 11 +++++++---- python/qemu/ot/rom/image.py | 10 ++++++++++ scripts/opentitan/romtool.py | 15 ++++++++++----- 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/docs/opentitan/romtool.md b/docs/opentitan/romtool.md index 3efdc6584fec7..1439d64d637f0 100644 --- a/docs/opentitan/romtool.md +++ b/docs/opentitan/romtool.md @@ -12,8 +12,8 @@ Supported input formats: ## Usage ````text -usage: romtool.py [-h] -c CFG [-o OUTPUT] [-i ROM_ID] [-z SIZE] - [-f {HEX,BIN,SVMEM,VMEM}] [-s] [-v] [-d] +usage: romtool.py [-h] -c CFG [-o OUTPUT] [-D] [-f {HEX,BIN,SVMEM,VMEM}] + [-i ROM_ID] [-s] [-z SIZE] [-v] [-d] rom QEMU OT tool to generate a scrambled ROM image. @@ -27,11 +27,12 @@ Files: -o, --output OUTPUT output ROM image file Parameters: - -i, --rom-id ROM_ID ROM image identifier - -z, --rom-size SIZE ROM image size in bytes (accepts Ki suffix) + -D, --digest Show the ROM digest -f, --output-format {HEX,BIN,SVMEM,VMEM} Output file format (default: HEX) + -i, --rom-id ROM_ID ROM image identifier -s, --subst-perm Enable legacy mode with S&P mode + -z, --rom-size SIZE ROM image size in bytes (accepts Ki suffix) Extras: -v, --verbose increase verbosity @@ -43,6 +44,8 @@ Extras: * `-c` specify a QEMU [configuration file](otcfg.md) from which to read all the cryptographic constants. See [`cfggen.py`](cfggen.md) tool to generate such a file. +* `-D` show the loaded or computed ROM digest as an hexadecimal string + * `-d` only useful to debug the script, reports any Python traceback to the standard error stream. * `-f` output file format. `HEX` format always output scrambled/SEC-DED data, `SVMEM` specifies a diff --git a/python/qemu/ot/rom/image.py b/python/qemu/ot/rom/image.py index 8e3d3524f156a..ade0faf8b25bc 100644 --- a/python/qemu/ot/rom/image.py +++ b/python/qemu/ot/rom/image.py @@ -185,6 +185,16 @@ def digest(self) -> bytes: self._make_digest() return self._digest + @property + def hexdigest(self) -> str: + """Return the current digest of the ROM image. + + Digest is computed on-the-fly if not already known. + + :return: the digest as a hexa string. + """ + return hexlify(self.digest).decode() + @property def key(self) -> Optional[int]: """Key observer.""" diff --git a/scripts/opentitan/romtool.py b/scripts/opentitan/romtool.py index 069ee7ae9d90f..02e5d45d5e116 100755 --- a/scripts/opentitan/romtool.py +++ b/scripts/opentitan/romtool.py @@ -47,17 +47,19 @@ def main(): help='output ROM image file') params = argparser.add_argument_group(title='Parameters') - params.add_argument('-i', '--rom-id', type=int, - help='ROM image identifier') - params.add_argument('-z', '--rom-size', metavar='SIZE', - type=HexInt.xparse, - help='ROM image size in bytes (accepts Ki suffix)') + params.add_argument('-D', '--digest', action='store_true', + help='Show the ROM digest') params.add_argument('-f', '--output-format', choices=out_formats, default=out_formats[0], help=f'Output file format ' f'(default: {out_formats[0]})') + params.add_argument('-i', '--rom-id', type=int, + help='ROM image identifier') params.add_argument('-s', '--subst-perm', action='store_true', help='Enable legacy mode with S&P mode') + params.add_argument('-z', '--rom-size', metavar='SIZE', + type=HexInt.xparse, + help='ROM image size in bytes (accepts Ki suffix)') extra = argparser.add_argument_group(title='Extras') extra.add_argument('-v', '--verbose', action='count', @@ -74,6 +76,9 @@ def main(): rom_img.load_config(args.config, args.rom_id) rom_img.load(args.rom[0], args.rom_size) + if args.digest: + print('Digest:', rom_img.hexdigest) + with open(args.output, 'wb') if args.output else \ sys.stdout.buffer as wfp: rom_img.save(wfp, args.output_format)