Skip to content

Commit 12277c4

Browse files
committed
Allow multiple spiffs/fat partitions
1 parent 99e2b3f commit 12277c4

File tree

1 file changed

+39
-8
lines changed

1 file changed

+39
-8
lines changed

espflash/src/partition_table.rs

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,12 @@ pub enum DataType {
115115
Spiffs = 0x82,
116116
}
117117

118+
impl DataType {
119+
fn is_multiple_allowed(self) -> bool {
120+
matches!(self, Self::Fat | Self::Spiffs)
121+
}
122+
}
123+
118124
#[derive(Debug, Deserialize, PartialEq, Copy, Clone)]
119125
#[allow(dead_code)]
120126
#[serde(untagged)]
@@ -141,6 +147,13 @@ impl SubType {
141147
SubType::Data(ty) => *ty as u8,
142148
}
143149
}
150+
151+
fn is_multiple_allowed(self) -> bool {
152+
match self {
153+
SubType::App(_) => false,
154+
SubType::Data(ty) => ty.is_multiple_allowed(),
155+
}
156+
}
144157
}
145158

146159
#[derive(Debug)]
@@ -198,7 +211,7 @@ impl PartitionTable {
198211
.from_reader(data.trim().as_bytes());
199212

200213
// Default offset is 0x8000 in esp-idf, partition table size is 0x1000
201-
let mut offset = 0x9000;
214+
let mut offset = 0x9000;
202215
let mut partitions = Vec::with_capacity(data.lines().count());
203216
for record in reader.records() {
204217
let record = record.map_err(|e| CSVError::new(e, data.clone()))?;
@@ -292,7 +305,9 @@ impl PartitionTable {
292305
.into());
293306
}
294307

295-
if partition1.sub_type == partition2.sub_type {
308+
if partition1.sub_type == partition2.sub_type
309+
&& !SubType::is_multiple_allowed(partition1.sub_type)
310+
{
296311
return Err(DuplicatePartitionsError::new(
297312
source, *line1, *line2, "sub-type",
298313
)
@@ -550,20 +565,31 @@ ota_0, app, ota_0, 0x110000, 1M,
550565
ota_1, app, ota_1, 0x210000, 1M,
551566
";
552567

553-
const PTABLE_2: &str = "
568+
const PTABLE_2: &str = "
554569
# ESP-IDF Partition Table
555570
# Name, Type, SubType, Offset, Size, Flags
556571
nvs, data, nvs, , 0x4000,
557572
phy_init, data, phy, , 0x1000,
558573
factory, app, factory, , 1M,
559574
";
560575

561-
const PTABLE_3: &str = "
576+
const PTABLE_3: &str = "
562577
# ESP-IDF Partition Table
563578
# Name, Type, SubType, Offset, Size, Flags
564579
nvs, data, nvs, 0x10000, 0x4000,
565580
phy_init, data, phy, , 0x1000,
566581
factory, app, factory, , 1M,
582+
";
583+
584+
const PTABLE_SPIFFS: &str = "
585+
# ESP-IDF Partition Table
586+
# Name, Type, SubType, Offset, Size, Flags
587+
nvs, data, nvs, 0x9000, 0x4000,
588+
otadata, data, ota, 0xd000, 0x2000,
589+
phy_init, data, phy, 0xf000, 0x1000,
590+
factory, app, factory, 0x10000, 1M,
591+
a, data, spiffs, 0x110000, 1M,
592+
b, data, spiffs, 0x210000, 1M,
567593
";
568594

569595
#[test]
@@ -600,12 +626,16 @@ factory, app, factory, , 1M,
600626

601627
let pt1 = PartitionTable::try_from_str(PTABLE_1);
602628
assert!(pt1.is_ok());
629+
630+
let pt_spiffs = PartitionTable::try_from_str(PTABLE_SPIFFS);
631+
assert!(pt_spiffs.is_ok());
603632
}
604633

605634
#[test]
606635
fn blank_offsets_are_filled_in() {
607-
let pt2 = PartitionTable::try_from_str(PTABLE_2).expect("Failed to parse partition table with blank offsets");
608-
636+
let pt2 = PartitionTable::try_from_str(PTABLE_2)
637+
.expect("Failed to parse partition table with blank offsets");
638+
609639
assert_eq!(3, pt2.partitions.len());
610640
assert_eq!(0x4000, pt2.partitions[0].size);
611641
assert_eq!(0x1000, pt2.partitions[1].size);
@@ -618,8 +648,9 @@ factory, app, factory, , 1M,
618648

619649
#[test]
620650
fn first_offsets_are_respected() {
621-
let pt3 = PartitionTable::try_from_str(PTABLE_3).expect("Failed to parse partition table with blank offsets");
622-
651+
let pt3 = PartitionTable::try_from_str(PTABLE_3)
652+
.expect("Failed to parse partition table with blank offsets");
653+
623654
assert_eq!(3, pt3.partitions.len());
624655
assert_eq!(0x4000, pt3.partitions[0].size);
625656
assert_eq!(0x1000, pt3.partitions[1].size);

0 commit comments

Comments
 (0)