diff --git a/src/partition_table.toit b/src/partition_table.toit index 7b28035..d46a217 100644 --- a/src/partition_table.toit +++ b/src/partition_table.toit @@ -213,6 +213,9 @@ class PartitionTable: // ota_1, app, ota_1, 0x1b0000, 0x1a0000, // nvs, data, nvs, 0x350000, 0x010000, // programs, 0x40, 0x00, 0x360000, 0x0a0000, encrypted + // + // Offsets may be missing, in which case they are calculated from the + // previous entry. table := PartitionTable @@ -221,6 +224,7 @@ class PartitionTable: trimmed := line.trim trimmed != "" and trimmed[0] != '#' + next-computed-offset/int? := null lines.do: | line/string | parts := line.split "," if parts.size < 5: throw "Malformed CSV line" @@ -244,14 +248,21 @@ class PartitionTable: offset-string := parts[3] offset/int := ? - if offset-string.starts-with "0x": offset = int.parse offset-string[2..] --radix=16 - else: offset = int.parse offset-string + if offset-string == "": + if not next-computed-offset: throw "Missing initial offset" + offset = next-computed-offset + else if offset-string.starts-with "0x": + offset = int.parse offset-string[2..] --radix=16 + else: + offset = int.parse offset-string size-string := parts[4] size/int := ? if size-string.starts-with "0x": size = int.parse size-string[2..] --radix=16 else: size = int.parse size-string + next-computed-offset = offset + size + flags/int := 0 flag-strings := parts.size > 5 ? parts[5].split "+" : [] flag-strings.filter --in-place: it != "" diff --git a/tests/cvs-test-partitions-no-offsets.cvs b/tests/cvs-test-partitions-no-offsets.cvs new file mode 100644 index 0000000..905dc4e --- /dev/null +++ b/tests/cvs-test-partitions-no-offsets.cvs @@ -0,0 +1,27 @@ +# Copyright (C) 2021 Toitware ApS. +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; version +# 2.1 only. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# The license can be found in the file `LICENSE` in the top level +# directory of this repository. + +# Partition Table for Toit. + +# Name, Type, SubType, Offset, Size +# bootloader,, , 0x001000, 0x007000 +# partitions,, , 0x008000, 0x000c00 +secure, 0x42, 0x00, 0x009000, 0x004000, +otadata, data, ota, , 0x002000, +phy_init, data, phy, , 0x001000, +ota_0, app, ota_0, , 0x1a0000, +ota_1, app, ota_1, , 0x1a0000, +nvs, data, nvs, , 0x010000, +programs, 0x40, 0x00, , 0x0a0000, encrypted diff --git a/tests/cvs-test.toit b/tests/cvs-test.toit index c2c7cc2..bc7ce6a 100644 --- a/tests/cvs-test.toit +++ b/tests/cvs-test.toit @@ -13,9 +13,13 @@ main: program-dir := fs.dirname program-path bin-contents := file.read-contents "$program-dir/cvs-test-partitions.bin" csv-contents := file.read-contents "$program-dir/cvs-test-partitions.cvs" + csv-no-offsets-contents := file.read-contents "$program-dir/cvs-test-partitions-no-offsets.cvs" partition-table-bin := PartitionTable.decode bin-contents partition-table-cvs := PartitionTable.decode csv-contents + partition-table-cvs-no-offsets := PartitionTable.decode csv-no-offsets-contents + + expect-equals-partition-tables partition-table-cvs partition-table-cvs-no-offsets partitions-bin := partition-table-bin.partitions partitions-cvs := partition-table-cvs.partitions