Skip to content

Commit d1a97a7

Browse files
committed
[ot] scripts/opentitan: flashgen.py: add an option to ignore time mismatch
Signed-off-by: Emmanuel Blot <[email protected]>
1 parent 55bf323 commit d1a97a7

File tree

2 files changed

+32
-24
lines changed

2 files changed

+32
-24
lines changed

docs/opentitan/flashgen.md

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ flash controller virtual device.
77

88
````text
99
usage: flashgen.py [-h] [-D] [-a {0,1}] [-s OFFSET] [-x file] [-X elf]
10-
[-b file] [-B elf] [-t OTDESC] [-U] [-A] [-v] [-d] flash
10+
[-b file] [-B elf] [-t OTDESC] [-T] [-U] [-A] [-v] [-d]
11+
flash
1112
1213
Create/update an OpenTitan backend flash file.
1314
@@ -17,20 +18,18 @@ options:
1718
Image:
1819
flash virtual flash file
1920
-D, --discard Discard any previous flash file content
20-
-a {0,1}, --bank {0,1}
21-
flash bank for data (default: 0)
22-
-s OFFSET, --offset OFFSET
23-
offset of the BL0 file (default: 0x10000)
21+
-a, --bank {0,1} flash bank for data (default: 0)
22+
-s, --offset OFFSET offset of the BL0 file (default: 0x10000)
2423
2524
Files:
26-
-x file, --exec file rom extension or application
27-
-X elf, --exec-elf elf
28-
ELF file for rom extension or application, for symbol tracking (default: auto)
29-
-b file, --boot file bootloader 0 file
30-
-B elf, --boot-elf elf
31-
ELF file for bootloader, for symbol tracking (default: auto)
32-
-t OTDESC, --otdesc OTDESC
33-
OpenTitan style file descriptor, may be repeated
25+
-x, --exec file rom extension or application
26+
-X, --exec-elf elf ELF file for rom extension or application, for symbol
27+
tracking (default: auto)
28+
-b, --boot file bootloader 0 file
29+
-B, --boot-elf elf ELF file for bootloader, for symbol tracking (default:
30+
auto)
31+
-t, --otdesc OTDESC OpenTitan style file descriptor, may be repeated
32+
-T, --ignore-time Discard time checking on ELF files
3433
-U, --unsafe-elf Discard sanity checking on ELF files
3534
-A, --accept-invalid Blindy accept invalid input files
3635
@@ -96,10 +95,13 @@ matching signed binary files to help with debugging.
9695
file should be smaller than the selected offset for the bootloader location, mutually exclusive
9796
with `-t`.
9897

98+
* `-T` tell the script to ignore any time discrepancy between a binary and an ELF file. A binary
99+
file being generated from an ELF file, the former is not expected to be older than the latter.
100+
99101
* `-U` tell the script to ignore any discrepancies found between a binary file and an ELF file. If
100102
the path to an ELF file is stored in the flash image file, the ELF content is validated against
101103
the binary file. Its code position and size, overall size and entry point should be coherent.
102-
Moreover, the modification time of the binary file should not be younger than the ELF origin file.
104+
This option implies `-T` option, _i.e._ also discards time comparison between BIN and ELF files.
103105
Note that contents of both files are not compared, as the binary file may be amended after the ELF
104106
file creation.
105107

scripts/opentitan/flashgen.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env python3
22

3-
# Copyright (c) 2023-2024 Rivos, Inc.
3+
# Copyright (c) 2023-2025 Rivos, Inc.
44
# SPDX-License-Identifier: Apache2
55

66
"""Create/update an OpenTitan backend flash file.
@@ -57,8 +57,10 @@ class FlashGen:
5757
:param bl_offset: offset of the BL0 storage within the data partition.
5858
if forced to 0, do not reserve any space for BL0, i.e.
5959
dedicated all storage space to ROM_EXT section.
60-
:discard_elf_check: whether to ignore mismatching binary/elf files.
60+
:discard_elf_check: whether to ignore mismatching binary/ELF files.
6161
:accept_invalid: accept invalid input files (fully ignore content)
62+
:discard_time_check: whether to ignore mismatching time between binary
63+
and ELF files.
6264
"""
6365

6466
NUM_BANKS = 2
@@ -155,14 +157,16 @@ class FlashGen:
155157
BOOT_PARTS = 2
156158

157159
def __init__(self, bl_offset: Optional[int] = None,
158-
discard_elf_check: Optional[bool] = None,
159-
accept_invalid: Optional[bool] = None):
160+
discard_elf_check: bool = False,
161+
accept_invalid: bool = False,
162+
discard_time_check: bool = False):
160163
self._log = getLogger('flashgen')
161164
self._check_manifest_size()
162165
self._bl_offset = bl_offset if bl_offset is not None \
163166
else self.CHIP_ROM_EXT_SIZE_MAX
164-
self._accept_invalid = bool(accept_invalid)
165-
self._check_elf = not (bool(discard_elf_check) or self._accept_invalid)
167+
self._accept_invalid = accept_invalid
168+
self._check_elf = not (discard_elf_check or self._accept_invalid)
169+
self._check_time = not discard_time_check and self._check_elf
166170
hfmt = ''.join(self.HEADER_FORMAT.values())
167171
header_size = scalc(hfmt)
168172
assert header_size == 32
@@ -314,7 +318,7 @@ def store_rom_ext(self, bank: int, dfp: BinaryIO,
314318
bintime = stat(dfp.name).st_mtime
315319
if bintime < elftime:
316320
msg = 'Application binary file is older than ELF file'
317-
if self._check_elf:
321+
if self._check_time:
318322
raise RuntimeError(msg)
319323
self._log.warning(msg)
320324
be_match = self._compare_bin_elf(bindesc, elfpath)
@@ -351,7 +355,7 @@ def store_bootloader(self, bank: int, dfp: BinaryIO,
351355
bintime = stat(dfp.name).st_mtime
352356
if bintime < elftime:
353357
msg = 'Boot binary file is older than ELF file'
354-
if self._check_elf:
358+
if self._check_time:
355359
raise RuntimeError(msg)
356360
self._log.warning(msg)
357361
be_match = self._compare_bin_elf(bindesc, elfpath)
@@ -634,6 +638,8 @@ def main():
634638
files.add_argument('-t', '--otdesc', action='append', default=[],
635639
help='OpenTitan style file descriptor, '
636640
'may be repeated')
641+
files.add_argument('-T', '--ignore-time', action='store_true',
642+
help='Discard time checking on ELF files')
637643
files.add_argument('-U', '--unsafe-elf', action='store_true',
638644
help='Discard sanity checking on ELF files')
639645
files.add_argument('-A', '--accept-invalid', action='store_true',
@@ -649,8 +655,8 @@ def main():
649655
configure_loggers(args.verbose, 'flashgen', 'elf')
650656

651657
use_bl0 = bool(args.boot) or len(args.otdesc) > 1
652-
gen = FlashGen(args.offset if use_bl0 else 0, bool(args.unsafe_elf),
653-
bool(args.accept_invalid))
658+
gen = FlashGen(args.offset if use_bl0 else 0, args.unsafe_elf,
659+
args.accept_invalid, args.ignore_time)
654660
flash_pathname = args.flash[0]
655661
backup_filename = None
656662
if args.otdesc and any(filter(None, (args.bank,

0 commit comments

Comments
 (0)