Skip to content

Commit 1618e9a

Browse files
committed
WIP: fix issues based on review comments
Signed-off-by: Sherry Li <xiaoruli@tenstorrent.com>
1 parent 25a1a37 commit 1618e9a

File tree

1 file changed

+34
-29
lines changed

1 file changed

+34
-29
lines changed

scripts/tt_boot_fs.py

100755100644
Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -613,28 +613,35 @@ def from_binary(data: bytes, alignment: int = 0x1000) -> BootFs:
613613
num_tables=2,
614614
)
615615
default_table_addrs = [FD_HEAD_ADDR, FAILOVER_HEAD_ADDR]
616+
bootfs_header = default_bootfs_header
617+
table_addrs = default_table_addrs
616618

617619
# Pull file descriptor header
618-
if len(data) < TT_BOOT_FS_HEADER_ADDR + ctypes.sizeof(tt_boot_fs_header):
619-
# Use default table set
620-
bootfs_header = default_bootfs_header
621-
table_addrs = default_table_addrs
622-
else:
623-
bootfs_header = tt_boot_fs_header.from_buffer_copy(
624-
data, TT_BOOT_FS_HEADER_ADDR
625-
)
626-
if bootfs_header.magic != BOOTFS_HEADER_MAGIC:
627-
# This is an older bootfs that lacks a header. Use the default table set,
628-
# which included a ROM table and Fallback table
629-
table_addrs = default_table_addrs
630-
bootfs_header = default_bootfs_header
631-
else:
632-
table_addrs = []
633-
data_offs = TT_BOOT_FS_HEADER_ADDR + ctypes.sizeof(tt_boot_fs_header)
634-
for _ in range(bootfs_header.num_tables):
635-
table_addr = struct.unpack_from("<I", data, data_offs)[0]
636-
table_addrs.append(table_addr)
620+
try:
621+
header = tt_boot_fs_header.from_buffer_copy(data, TT_BOOT_FS_HEADER_ADDR)
622+
if header.magic == BOOTFS_HEADER_MAGIC:
623+
# Verify table address array fits in data
624+
table_array_start = TT_BOOT_FS_HEADER_ADDR + ctypes.sizeof(
625+
tt_boot_fs_header
626+
)
627+
table_array_size = header.num_tables * ctypes.sizeof(ctypes.c_uint32)
628+
if len(data) < table_array_start + table_array_size:
629+
raise ValueError(
630+
f"data length {len(data)} does not include table address array at 0x{table_array_start:x}"
631+
)
632+
633+
# Valid header -- parse table addresses
634+
addrs = []
635+
data_offs = table_array_start
636+
for _ in range(header.num_tables):
637+
addrs.append(
638+
ctypes.c_uint32.from_buffer_copy(data, data_offs).value
639+
)
637640
data_offs += 4
641+
bootfs_header = header
642+
table_addrs = addrs
643+
except ValueError:
644+
pass
638645

639646
if len(data) < FAILOVER_HEAD_ADDR + FD_SIZE:
640647
raise ValueError(
@@ -659,11 +666,11 @@ def from_binary(data: bytes, alignment: int = 0x1000) -> BootFs:
659666
fds: dict[str, tt_boot_fs_fd] = {}
660667
entries: dict[str, FsEntry] = {}
661668
# Scan FDs in this table
662-
for value in iter_fd(
669+
for _, fd in iter_fd(
663670
lambda addr, size: data[addr + offset : addr + offset + size]
664671
):
665-
tag = value[1].image_tag_str()
666-
fds[tag] = value[1]
672+
tag = fd.image_tag_str()
673+
fds[tag] = fd[1]
667674
order.append(tag)
668675

669676
for tag in order:
@@ -726,16 +733,14 @@ def load(path: str, env: dict):
726733
alignment = FileAlignment.loads(data["alignment"])
727734
tables = []
728735
for t in data["tables"]:
729-
table_ent = {"header_addr": t["header_addr"], "images": {}}
736+
images = {}
730737

731738
for ent in t["images"]:
732739
ent_name = ent["name"]
733-
if ent_name in table_ent["images"]:
740+
if ent_name in images:
734741
raise ValueError(f"Found duplicate image name '{ent_name}'")
735-
table_ent["images"][ent_name] = BootImage.loads(
736-
ent_name, ent, alignment, env
737-
)
738-
tables.append(table_ent)
742+
images[ent_name] = BootImage.loads(ent_name, ent, alignment, env)
743+
tables.append({"header_addr": t["header_addr"], "images": {}})
739744

740745
return FileImage(
741746
name=data["name"],
@@ -792,7 +797,7 @@ def to_boot_fs(self):
792797
if image.spi_addr % self.alignment.block_size != 0:
793798
raise ValueError(
794799
f"The spi_addr of {image.tag} at {image.spi_addr:x} "
795-
"is not aligned to the spi block size of {self.alignment.block_size}"
800+
f"is not aligned to the spi block size of {self.alignment.block_size}"
796801
)
797802
tracker.add(
798803
image.spi_addr, image.spi_addr + len(image.binary), image

0 commit comments

Comments
 (0)