Skip to content

Commit 7b28086

Browse files
committed
[ot] scripts/opentitan: flashgen.py: Change otdesc to use any binaries
Currently the `otdesc` option accepts an address for a binary and then attempts to guess whether the binary is a ROM_EXT or a bootloader file, loading it at the hard-coded offsets. With the result of `opentitantool image assemble` it would instead be more useful to place binaries arbitrarily, wherever we wish, even across flash banks to support mirrored flash images. This commit changes the `otdesc` option to just load the given data at the given offset provided it does not exceed the maximum flash size. This option is basically converted to mean "place this data at this location, without care for what it is". For now, use of ELFs for debug symbol support is dropped from the `otdesc` option, because binaries can be loaded that are not ROM_EXTs or bl0s as defined in the flash format. In the future, the flash format should be updated to support an arbitrary number of convenience debugging ELFs, and then this option can be added back, but support for this is dropped for now when using this option (debug ELFs still remain if loading ROM_EXT and bl0 files the normal way). Signed-off-by: Alex Jones <[email protected]>
1 parent 58f17bc commit 7b28086

File tree

2 files changed

+22
-22
lines changed

2 files changed

+22
-22
lines changed

docs/opentitan/flashgen.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,13 @@ matching signed binary files to help with debugging.
7777
may also be hardcoded in the ROM_EXT application. Changing the default offset may cause the
7878
ROM_EXT to fail to load the BL0 application.
7979

80-
* `-t binfile@address[:elfile]` specify a binary file to store into the data partition of the flash.
80+
* `-t binfile@address` specify a binary file to store into the data partition of the flash.
8181
This is a compatibility option introduced to support the original `opentitantool image assemble`
82-
syntax. In this mode, the file kind and the destination flash bank are guessed from the specified
83-
address. The address should be specified in hexadecimal format. It is possible to specify an
84-
matching ELF file by appending its path after a column separator following the address value. This
85-
option may be repeated to specify multiple files such as the ROM EXT and a bootloader for example.
86-
This option is mutually exclusive with `-b`, `-B`, `-x`, `-X` and `-a`.
82+
syntax. The provided address should be specified in hexadecimal format. Currently, it is not
83+
possible to specify a matching ELF file as the flash debug trailer format is hardcoded to only
84+
support ROM EXT and bootloader binaries in both banks, and does not support arbitrary ELFs.
85+
This option may be repeated to specify multiple files such as the ROM EXT and a bootloader for
86+
example. This option is mutually exclusive with `-b`, `-B`, `-x`, `-X` and `-a`.
8787

8888
* `-X elf` specify an alternative path to the ROM_EXT ELF file. If not specified, the ELF path file
8989
is reconstructed from the specified binary file (from the same directory). The ELF file is only

python/qemu/ot/eflash/gen.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -359,13 +359,11 @@ def store_bootloader(self, bank: int, dfp: BinaryIO,
359359
self._store_debug_info(ename, elfpath)
360360

361361
def store_ot_files(self, otdescs: list[str]) -> None:
362-
for dpos, otdesc in enumerate(otdescs, start=1):
363-
parts = otdesc.rsplit(':', 1)
364-
if len(parts) > 1:
365-
otdesc = parts[0]
366-
elf_filename = parts[1]
367-
else:
368-
elf_filename = None
362+
for otdesc in otdescs:
363+
# @TODO: add back support for ELFs for convenience debug symbols
364+
# when the flash debug trailer format is changed to support
365+
# arbitrary numbers (and locations) of ELFs, which is also
366+
# needed due to mutable flash.
369367
parts = otdesc.split('@', 1)
370368
if len(parts) < 2:
371369
raise ValueError('Missing address in OT descriptor')
@@ -376,16 +374,18 @@ def store_ot_files(self, otdescs: list[str]) -> None:
376374
address = int(parts[1], 16)
377375
except ValueError as exc:
378376
raise ValueError('Invalid address in OT descriptor') from exc
379-
bank = address // self.BYTES_PER_BANK
380-
address %= self.BYTES_PER_BANK
381-
kind = 'rom_ext' if address < self.CHIP_ROM_EXT_SIZE_MAX else \
382-
'bootloader'
383-
self._log.info(
384-
'Handling file #%d as %s @ 0x%x in bank %d with%s ELF',
385-
dpos, kind, address, bank, '' if elf_filename else 'out')
377+
max_offset = self.NUM_BANKS * self.BYTES_PER_BANK
378+
if not 0 <= address < max_offset:
379+
raise ValueError(f'Invalid address 0x{address:x} for bin '
380+
f'{basename(bin_filename)}')
386381
with open(bin_filename, 'rb') as bfp:
387-
# func decode should never fail, so no error handling here
388-
getattr(self, f'store_{kind}')(bank, bfp, elf_filename)
382+
data = bfp.read()
383+
address_end = address + len(data)
384+
if not 0 <= address_end <= max_offset:
385+
raise ValueError(f'Binary {basename(bin_filename)} at '
386+
f'0x{address:x}:0x{address_end:x} does not '
387+
f'fit in flash size 0x{max_offset:x}')
388+
self._write(self._header_size + address, data)
389389

390390
def _compare_bin_elf(self, bindesc: RuntimeDescriptor, elfpath: str) \
391391
-> Optional[bool]:

0 commit comments

Comments
 (0)