Skip to content

Commit 7d8ff20

Browse files
committed
add initial storage extension support
1 parent 1fd09cb commit 7d8ff20

File tree

1 file changed

+43
-16
lines changed

1 file changed

+43
-16
lines changed

ports/espressif/supervisor/internal_flash.c

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,36 +42,52 @@
4242
#include "supervisor/flash.h"
4343
#include "supervisor/usb.h"
4444

45-
STATIC const esp_partition_t *_partition;
45+
STATIC const esp_partition_t *_partition[2];
4646

4747
// TODO: Split the caching out of supervisor/shared/external_flash so we can use it.
4848
#define SECTOR_SIZE 4096
4949
STATIC uint8_t _cache[SECTOR_SIZE];
5050
STATIC uint32_t _cache_lba = 0xffffffff;
5151

5252
void supervisor_flash_init(void) {
53-
_partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA,
53+
_partition[0] = esp_partition_find_first(ESP_PARTITION_TYPE_DATA,
5454
ESP_PARTITION_SUBTYPE_DATA_FAT,
5555
NULL);
56+
_partition[1] = esp_partition_find_first(ESP_PARTITION_TYPE_APP,
57+
ESP_PARTITION_SUBTYPE_APP_OTA_1,
58+
NULL);
5659
}
5760

5861
uint32_t supervisor_flash_get_block_size(void) {
5962
return FILESYSTEM_BLOCK_SIZE;
6063
}
6164

6265
uint32_t supervisor_flash_get_block_count(void) {
63-
return _partition->size / FILESYSTEM_BLOCK_SIZE;
66+
return (_partition[0]->size + _partition[1]->size) / FILESYSTEM_BLOCK_SIZE;
6467
}
6568

6669
void port_internal_flash_flush(void) {
6770

6871
}
6972

7073
mp_uint_t supervisor_flash_read_blocks(uint8_t *dest, uint32_t block, uint32_t num_blocks) {
71-
esp_partition_read(_partition,
72-
block * FILESYSTEM_BLOCK_SIZE,
73-
dest,
74-
num_blocks * FILESYSTEM_BLOCK_SIZE);
74+
uint32_t offset = block * FILESYSTEM_BLOCK_SIZE;
75+
uint32_t read_total = num_blocks * FILESYSTEM_BLOCK_SIZE;
76+
77+
if (offset > _partition[0]->size) {
78+
// only read from partition 1
79+
esp_partition_read(_partition[1], (offset - _partition[0]->size), dest, read_total);
80+
} else if ((offset + read_total) > _partition[0]->size) {
81+
// first read from partition 0, then partition 1
82+
uint32_t read_0 = _partition[0]->size - offset;
83+
uint32_t read_1 = read_total - read_0;
84+
esp_partition_read(_partition[0], offset, dest, read_0);
85+
esp_partition_read(_partition[1], 0, (dest + read_0), read_1);
86+
} else {
87+
// only read from partition 0
88+
esp_partition_read(_partition[0], offset, dest, read_total);
89+
}
90+
7591
return 0;
7692
}
7793

@@ -84,12 +100,10 @@ mp_uint_t supervisor_flash_write_blocks(const uint8_t *src, uint32_t lba, uint32
84100
uint8_t block_offset = block_address % blocks_per_sector;
85101

86102
if (_cache_lba != block_address) {
87-
esp_partition_read(_partition,
88-
sector_offset,
89-
_cache,
90-
SECTOR_SIZE);
103+
supervisor_flash_read_blocks(_cache, sector_offset / FILESYSTEM_BLOCK_SIZE, blocks_per_sector);
91104
_cache_lba = sector_offset;
92105
}
106+
93107
for (uint8_t b = block_offset; b < blocks_per_sector; b++) {
94108
// Stop copying after the last block.
95109
if (block >= num_blocks) {
@@ -100,11 +114,24 @@ mp_uint_t supervisor_flash_write_blocks(const uint8_t *src, uint32_t lba, uint32
100114
FILESYSTEM_BLOCK_SIZE);
101115
block++;
102116
}
103-
esp_partition_erase_range(_partition, sector_offset, SECTOR_SIZE);
104-
esp_partition_write(_partition,
105-
sector_offset,
106-
_cache,
107-
SECTOR_SIZE);
117+
118+
if (sector_offset > _partition[0]->size) {
119+
// only write to partition 1
120+
esp_partition_erase_range(_partition[1], sector_offset - _partition[0]->size, SECTOR_SIZE);
121+
esp_partition_write(_partition[1], sector_offset - _partition[0]->size, _cache, SECTOR_SIZE);
122+
} else if ((sector_offset + SECTOR_SIZE) > _partition[0]->size) {
123+
// first write to partition 0, then partition 1
124+
uint32_t write_0 = _partition[0]->size - sector_offset;
125+
uint32_t write_1 = SECTOR_SIZE - write_0;
126+
esp_partition_erase_range(_partition[0], sector_offset, write_0);
127+
esp_partition_write(_partition[0], sector_offset, _cache, write_0);
128+
esp_partition_erase_range(_partition[1], 0, write_1);
129+
esp_partition_write(_partition[1], 0, _cache + write_0, write_1);
130+
} else {
131+
// only write to partition 0
132+
esp_partition_erase_range(_partition[0], sector_offset, SECTOR_SIZE);
133+
esp_partition_write(_partition[0], sector_offset, _cache, SECTOR_SIZE);
134+
}
108135
}
109136

110137
return 0; // success

0 commit comments

Comments
 (0)