42
42
#include "supervisor/flash.h"
43
43
#include "supervisor/usb.h"
44
44
45
- STATIC const esp_partition_t * _partition ;
45
+ STATIC const esp_partition_t * _partition [ 2 ] ;
46
46
47
47
// TODO: Split the caching out of supervisor/shared/external_flash so we can use it.
48
48
#define SECTOR_SIZE 4096
49
49
STATIC uint8_t _cache [SECTOR_SIZE ];
50
50
STATIC uint32_t _cache_lba = 0xffffffff ;
51
51
52
52
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 ,
54
54
ESP_PARTITION_SUBTYPE_DATA_FAT ,
55
55
NULL );
56
+ _partition [1 ] = esp_partition_find_first (ESP_PARTITION_TYPE_APP ,
57
+ ESP_PARTITION_SUBTYPE_APP_OTA_1 ,
58
+ NULL );
56
59
}
57
60
58
61
uint32_t supervisor_flash_get_block_size (void ) {
59
62
return FILESYSTEM_BLOCK_SIZE ;
60
63
}
61
64
62
65
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 ;
64
67
}
65
68
66
69
void port_internal_flash_flush (void ) {
67
70
68
71
}
69
72
70
73
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
+
75
91
return 0 ;
76
92
}
77
93
@@ -84,12 +100,10 @@ mp_uint_t supervisor_flash_write_blocks(const uint8_t *src, uint32_t lba, uint32
84
100
uint8_t block_offset = block_address % blocks_per_sector ;
85
101
86
102
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 );
91
104
_cache_lba = sector_offset ;
92
105
}
106
+
93
107
for (uint8_t b = block_offset ; b < blocks_per_sector ; b ++ ) {
94
108
// Stop copying after the last block.
95
109
if (block >= num_blocks ) {
@@ -100,11 +114,24 @@ mp_uint_t supervisor_flash_write_blocks(const uint8_t *src, uint32_t lba, uint32
100
114
FILESYSTEM_BLOCK_SIZE );
101
115
block ++ ;
102
116
}
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
+ }
108
135
}
109
136
110
137
return 0 ; // success
0 commit comments