Skip to content

Commit f2fd5f1

Browse files
committed
feature: Add --rename option to rename partitions.
Includes a simple test in `test_firmware.py`. Closes: #5
1 parent b7aa945 commit f2fd5f1

File tree

4 files changed

+37
-0
lines changed

4 files changed

+37
-0
lines changed

src/mp_image_tool_esp32/main.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ class TypedNamespace(argparse.Namespace):
8181
table: PartList
8282
delete: ArgList
8383
add: PartList
84+
rename: ArgList
8485
resize: PartList
8586
erase: ArgList
8687
erase_fs: ArgList
@@ -131,6 +132,8 @@ class TypedNamespace(argparse.Namespace):
131132
| resize partitions \
132133
eg. --resize factory=2M,nvs=5B,vfs=0. \
133134
If SIZE is 0, expand partition to available space
135+
--rename NAME1=NEW1[,NAME2=NEW2,...] \
136+
| rename partitions, eg. --rename ffat=vfs,factory=app
134137
--erase NAME1[,NAME2,...] | erase the named partitions
135138
--erase-fs NAME1[,NAME2,...] \
136139
| erase first 4 blocks of a partition on flash storage.\
@@ -295,6 +298,13 @@ def run_commands(argv: Sequence[str] | None = None) -> None:
295298
new_table.check()
296299
extension += f"-resize={args.resize}"
297300

301+
if args.rename: # --rename NAME1=NEW1[,NAME2=NEW2,...] : Rename partitions
302+
for name, new_name in args.rename:
303+
log.action(f"Renaming partition '{name}' to '{new_name}'...")
304+
new_table.rename_part(name, new_name)
305+
new_table.check()
306+
extension += f"-rename={args.rename}"
307+
298308
if args.add: # --add NAME1=SUBTYPE:OFFSET:SIZE[,..] : Add new partitions
299309
for name, subtype, offset, size in args.add:
300310
subtype = layouts.get_subtype(name, subtype)

src/mp_image_tool_esp32/partition_table.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,21 @@ def resize_part(self, name: str, new_size: int) -> int:
301301
self[j] = self[j]._replace(size=self.max_size - self[j].offset)
302302
return new_size
303303

304+
def rename_part(self, name: str, new_name: str) -> None:
305+
"""Rename a partition in the partition table.
306+
Raises:
307+
PartitionError: If the partition with `old_name` does not exist or
308+
if `new_name` already exists in the table.
309+
"""
310+
i = self.index(self.by_name(name))
311+
if any(p for p in self if p.name == new_name):
312+
raise PartitionError(
313+
f"Partition '{new_name}' already exists in table.", self
314+
)
315+
self[i] = self[i]._replace(
316+
label=new_name.encode().ljust(PART_NAME_LEN, b"\x00")
317+
)
318+
304319
def check(self, app_size: int = 0) -> None:
305320
"""Check the partition table for consistency.
306321
Raises `PartError` if any inconsistencies found."""

tests/test_firmware.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ def test_resize(firmware: Path) -> None:
5050
mpi_check_output(firmware, "--resize vfs=1M")
5151

5252

53+
def test_rename(firmware: Path) -> None:
54+
mpi_check_output(firmware, "--rename vfs=vfs2,factory=app")
55+
56+
5357
def test_add(firmware: Path) -> None:
5458
mpi_check_output(firmware, "--resize vfs=1M --add data=fat:50B")
5559

tests/test_outputs.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@
7070
factory app factory 0x10000 0x1f0000 0x200000 0x0 (1.9 MB)
7171
vfs data fat 0x200000 0x100000 0x300000 0x0 (1.0 MB)
7272
73+
--rename vfs=vfs2,factory=app: |
74+
Partition table (flash size: 4MB):
75+
# Name Type SubType Offset Size End Flags
76+
nvs data nvs 0x9000 0x6000 0xf000 0x0 (24.0 kB)
77+
phy_init data phy 0xf000 0x1000 0x10000 0x0 (4.0 kB)
78+
app app factory 0x10000 0x1f0000 0x200000 0x0 (1.9 MB)
79+
vfs2 data fat 0x200000 0x200000 0x400000 0x0 (2.0 MB)
80+
7381
--resize vfs=1M --add data=fat:50B: |
7482
Partition table (flash size: 4MB):
7583
# Name Type SubType Offset Size End Flags

0 commit comments

Comments
 (0)