Skip to content

Commit af5bf78

Browse files
authored
Merge pull request #37 from kelnos/better-default-esp32-parttable
Write the ESP32 & ESP32-C3 partition table with nvs and phy_init partitions
2 parents b5282f2 + 2941b24 commit af5bf78

File tree

5 files changed

+87
-22
lines changed

5 files changed

+87
-22
lines changed

espflash/src/chip/esp32.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,14 @@ const DROM_MAP_END: u32 = 0x3F800000;
2424

2525
const BOOT_ADDR: u32 = 0x1000;
2626
const PARTION_ADDR: u32 = 0x8000;
27+
const NVS_ADDR: u32 = 0x9000;
28+
const PHY_INIT_DATA_ADDR: u32 = 0xf000;
2729
const APP_ADDR: u32 = 0x10000;
2830

31+
const NVS_SIZE: u32 = 0x6000;
32+
const PHY_INIT_DATA_SIZE: u32 = 0x1000;
33+
const APP_SIZE: u32 = 0x3f0000;
34+
2935
impl ChipType for Esp32 {
3036
const CHIP_DETECT_MAGIC_VALUE: u32 = 0x00f01d83;
3137

@@ -49,7 +55,15 @@ impl ChipType for Esp32 {
4955
) -> Box<dyn Iterator<Item = Result<RomSegment<'a>, Error>> + 'a> {
5056
let bootloader = include_bytes!("../../bootloader/esp32-bootloader.bin");
5157

52-
let partition_table = PartitionTable::basic(0x10000, 0x3f0000).to_bytes();
58+
let partition_table = PartitionTable::basic(
59+
NVS_ADDR,
60+
NVS_SIZE,
61+
PHY_INIT_DATA_ADDR,
62+
PHY_INIT_DATA_SIZE,
63+
APP_ADDR,
64+
APP_SIZE,
65+
)
66+
.to_bytes();
5367

5468
fn get_data<'a>(image: &'a FirmwareImage) -> Result<RomSegment<'a>, Error> {
5569
let mut data = Vec::new();

espflash/src/chip/esp32c3.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,14 @@ const DROM_MAP_END: u32 = 0x3c800000;
2424

2525
const BOOT_ADDR: u32 = 0x0;
2626
const PARTITION_ADDR: u32 = 0x8000;
27+
const NVS_ADDR: u32 = 0x9000;
28+
const PHY_INIT_DATA_ADDR: u32 = 0xf000;
2729
const APP_ADDR: u32 = 0x10000;
2830

31+
const NVS_SIZE: u32 = 0x6000;
32+
const PHY_INIT_DATA_SIZE: u32 = 0x1000;
33+
const APP_SIZE: u32 = 0x3f0000;
34+
2935
impl ChipType for Esp32c3 {
3036
const CHIP_DETECT_MAGIC_VALUE: u32 = 0x6921506f;
3137
const CHIP_DETECT_MAGIC_VALUE2: u32 = 0x1b31506f;
@@ -50,7 +56,15 @@ impl ChipType for Esp32c3 {
5056
) -> Box<dyn Iterator<Item = Result<RomSegment<'a>, Error>> + 'a> {
5157
let bootloader = include_bytes!("../../bootloader/esp32c3-bootloader.bin");
5258

53-
let partition_table = PartitionTable::basic(0x10000, 0x3f0000).to_bytes();
59+
let partition_table = PartitionTable::basic(
60+
NVS_ADDR,
61+
NVS_SIZE,
62+
PHY_INIT_DATA_ADDR,
63+
PHY_INIT_DATA_SIZE,
64+
APP_ADDR,
65+
APP_SIZE,
66+
)
67+
.to_bytes();
5468

5569
fn get_data<'a>(image: &'a FirmwareImage) -> Result<RomSegment<'a>, Error> {
5670
let mut data = Vec::new();

espflash/src/partition_table.rs

Lines changed: 52 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,39 @@ pub struct PartitionTable {
7272
}
7373

7474
impl PartitionTable {
75-
/// Create a basic partition table with a single app entry
76-
pub fn basic(app_offset: u32, app_size: u32) -> Self {
75+
/// Create a basic partition table with NVS, PHY init data, and the app partition
76+
pub fn basic(
77+
nvs_offset: u32,
78+
nvs_size: u32,
79+
phy_init_data_offset: u32,
80+
phy_init_data_size: u32,
81+
app_offset: u32,
82+
app_size: u32,
83+
) -> Self {
7784
PartitionTable {
78-
partitions: vec![Partition::new(
79-
String::from("factory"),
80-
Type::App,
81-
SubType::App(AppType::Factory),
82-
app_offset,
83-
app_size,
84-
0,
85-
)],
85+
partitions: vec![
86+
Partition::new(
87+
String::from("nvs"),
88+
SubType::Data(DataType::Nvs),
89+
nvs_offset,
90+
nvs_size,
91+
0,
92+
),
93+
Partition::new(
94+
String::from("phy_init"),
95+
SubType::Data(DataType::Phy),
96+
phy_init_data_offset,
97+
phy_init_data_size,
98+
0,
99+
),
100+
Partition::new(
101+
String::from("factory"),
102+
SubType::App(AppType::Factory),
103+
app_offset,
104+
app_size,
105+
0,
106+
),
107+
],
86108
}
87109
}
88110

@@ -127,17 +149,13 @@ struct Partition {
127149
}
128150

129151
impl Partition {
130-
pub fn new(
131-
name: String,
132-
ty: Type,
133-
sub_type: SubType,
134-
offset: u32,
135-
size: u32,
136-
flags: u32,
137-
) -> Self {
152+
pub fn new(name: String, sub_type: SubType, offset: u32, size: u32, flags: u32) -> Self {
138153
Partition {
139154
name,
140-
ty,
155+
ty: match sub_type {
156+
SubType::App(_) => Type::App,
157+
SubType::Data(_) => Type::Data,
158+
},
141159
sub_type,
142160
offset,
143161
size,
@@ -194,9 +212,23 @@ impl<W: Write> HashWriter<W> {
194212
#[test]
195213
fn test_basic() {
196214
use std::fs::read;
215+
const NVS_ADDR: u32 = 0x9000;
216+
const PHY_INIT_DATA_ADDR: u32 = 0xf000;
217+
const APP_ADDR: u32 = 0x10000;
218+
219+
const NVS_SIZE: u32 = 0x6000;
220+
const PHY_INIT_DATA_SIZE: u32 = 0x1000;
221+
const APP_SIZE: u32 = 0x3f0000;
197222

198223
let expected = read("./tests/data/partitions.bin").unwrap();
199-
let table = PartitionTable::basic(0x10000, 0x3f0000);
224+
let table = PartitionTable::basic(
225+
NVS_ADDR,
226+
NVS_SIZE,
227+
PHY_INIT_DATA_ADDR,
228+
PHY_INIT_DATA_SIZE,
229+
APP_ADDR,
230+
APP_SIZE,
231+
);
200232

201233
let result = table.to_bytes();
202234

espflash/tests/data/partitions.bin

0 Bytes
Binary file not shown.

espflash/tests/data/partitions.csv

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# ESP-IDF Partition Table
2+
# Name, Type, SubType, Offset, Size, Flags
3+
nvs, data, nvs, 0x9000, 0x6000,
4+
phy_init, data, phy, 0xf000, 0x1000,
5+
factory, app, factory, 0x10000, 0x3f0000,

0 commit comments

Comments
 (0)