Skip to content

Commit

Permalink
Support CVS partitions without offsets. (#31)
Browse files Browse the repository at this point in the history
Co-authored-by: Kasper Lund <kasper@toit.io>
  • Loading branch information
floitsch and kasperl authored Feb 3, 2025
1 parent 4a64a4a commit 9ee4032
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/partition_table.toit
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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"
Expand All @@ -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 != ""
Expand Down
27 changes: 27 additions & 0 deletions tests/cvs-test-partitions-no-offsets.cvs
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions tests/cvs-test.toit
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9ee4032

Please sign in to comment.