@@ -73,11 +73,15 @@ static size_t msc_update_get_required_disk_sectors(){
73
73
if (msc_run_partition){
74
74
fw_size = get_firmware_size (msc_run_partition);
75
75
data_sectors += FAT_SIZE_TO_SECTORS (fw_size);
76
- log_d (" running size: %u (%u:%u)" , fw_size, fw_size / DISK_SECTOR_SIZE, fw_size % DISK_SECTOR_SIZE);
76
+ log_d (" APP size: %u (%u sectors)" , fw_size, FAT_SIZE_TO_SECTORS (fw_size));
77
+ } else {
78
+ log_w (" APP partition not found. Reading disabled" );
77
79
}
78
80
if (msc_ota_partition){
79
81
data_sectors += FAT_SIZE_TO_SECTORS (msc_ota_partition->size );
80
- log_d (" ota size: %u (%u:%u)" , msc_ota_partition->size , msc_ota_partition->size / DISK_SECTOR_SIZE, msc_ota_partition->size % DISK_SECTOR_SIZE);
82
+ log_d (" OTA size: %u (%u sectors)" , msc_ota_partition->size , FAT_SIZE_TO_SECTORS (msc_ota_partition->size ));
83
+ } else {
84
+ log_w (" OTA partition not found. Writing disabled" );
81
85
}
82
86
msc_table_sectors = fat_sectors_per_alloc_table (data_sectors, false );
83
87
total_sectors = data_sectors + msc_table_sectors + 2 ;
@@ -91,16 +95,21 @@ static size_t msc_update_get_required_disk_sectors(){
91
95
log_d (" USING FAT12" );
92
96
mcs_is_fat16 = false ;
93
97
}
94
- log_d (" data sectors: %u" , data_sectors);
95
- log_d (" table sectors: %u" , msc_table_sectors);
98
+ log_d (" FAT data sectors: %u" , data_sectors);
99
+ log_d (" FAT table sectors: %u" , msc_table_sectors);
100
+ log_d (" FAT total sectors: %u (%uKB)" , total_sectors, (total_sectors * DISK_SECTOR_SIZE) / 1024 );
96
101
return total_sectors;
97
102
}
98
103
99
104
// setup the ramdisk and add the firmware download file
100
- static void msc_update_setup_disk (const char * volume_label, uint32_t serial_number){
105
+ static bool msc_update_setup_disk (const char * volume_label, uint32_t serial_number){
101
106
msc_total_sectors = msc_update_get_required_disk_sectors ();
102
107
uint8_t ram_sectors = msc_table_sectors + 2 ;
103
108
msc_ram_disk = (uint8_t *)calloc (ram_sectors, DISK_SECTOR_SIZE);
109
+ if (!msc_ram_disk){
110
+ log_e (" Failed to allocate RAM Disk: %u bytes" , ram_sectors * DISK_SECTOR_SIZE);
111
+ return false ;
112
+ }
104
113
fw_start_sector = ram_sectors;
105
114
fw_end_sector = fw_start_sector;
106
115
msc_boot = fat_add_boot_sector (msc_ram_disk, msc_total_sectors, msc_table_sectors, fat_file_system_type (mcs_is_fat16), volume_label, serial_number);
@@ -110,6 +119,7 @@ static void msc_update_setup_disk(const char * volume_label, uint32_t serial_num
110
119
fw_entry = fat_add_root_file (msc_ram_disk, 0 , " FIRMWARE" , " BIN" , fw_size, 2 , mcs_is_fat16);
111
120
fw_end_sector = FAT_SIZE_TO_SECTORS (fw_size) + fw_start_sector;
112
121
}
122
+ return true ;
113
123
}
114
124
115
125
// filter out entries to only include BINs in the root folder
@@ -169,7 +179,7 @@ static esp_err_t msc_update_write(const esp_partition_t *partition, uint32_t off
169
179
esp_err_t err = ESP_OK;
170
180
if ((offset & (SPI_FLASH_SEC_SIZE-1 )) == 0 ){
171
181
err = esp_partition_erase_range (partition, offset, SPI_FLASH_SEC_SIZE);
172
- // log_i ("ERASE[0x%08X]: %s", offset, (err != ESP_OK)?"FAIL":"OK");
182
+ log_v (" ERASE[0x%08X]: %s" , offset, (err != ESP_OK)?" FAIL" :" OK" );
173
183
if (err != ESP_OK){
174
184
return err;
175
185
}
@@ -179,7 +189,7 @@ static esp_err_t msc_update_write(const esp_partition_t *partition, uint32_t off
179
189
180
190
// called when error was encountered while updating
181
191
static void msc_update_error (){
182
- // log_e("UPDATE_ERROR: %u", msc_update_bytes_written);
192
+ log_e (" UPDATE_ERROR: %u" , msc_update_bytes_written);
183
193
arduino_firmware_msc_event_data_t p = {0 };
184
194
p.error .size = msc_update_bytes_written;
185
195
arduino_usb_event_post (ARDUINO_FIRMWARE_MSC_EVENTS, ARDUINO_FIRMWARE_MSC_ERROR_EVENT, &p, sizeof (arduino_firmware_msc_event_data_t ), portMAX_DELAY);
@@ -191,7 +201,7 @@ static void msc_update_error(){
191
201
192
202
// called when all firmware bytes have been received
193
203
static void msc_update_end (){
194
- log_v (" UPDATE_END: %u (%u) " , msc_update_entry-> file_size , msc_update_bytes_written - msc_update_entry->file_size );
204
+ log_d (" UPDATE_END: %u" , msc_update_entry->file_size );
195
205
msc_update_state = MSC_UPDATE_END;
196
206
size_t ota_size = get_firmware_size (msc_ota_partition);
197
207
if (ota_size != msc_update_entry->file_size ){
@@ -214,7 +224,7 @@ static int32_t msc_write(uint32_t lba, uint32_t offset, uint8_t* buffer, uint32_
214
224
if (lba < fw_start_sector){
215
225
// write to sectors that are in RAM
216
226
memcpy (msc_ram_disk + (lba * DISK_SECTOR_SIZE) + offset, buffer, bufsize);
217
- if (lba == (fw_start_sector - 1 )){
227
+ if (msc_ota_partition && lba == (fw_start_sector - 1 )){
218
228
// monitor the root folder table
219
229
if (msc_update_state <= MSC_UPDATE_RUNNING){
220
230
fat_dir_entry_t * update_entry = msc_update_find_new_bin ();
@@ -240,14 +250,14 @@ static int32_t msc_write(uint32_t lba, uint32_t offset, uint8_t* buffer, uint32_
240
250
}
241
251
}
242
252
}
243
- } else if (lba >= msc_update_start_sector){
253
+ } else if (msc_ota_partition && lba >= msc_update_start_sector){
244
254
// handle writes to the region where the new firmware will be uploaded
245
255
arduino_firmware_msc_event_data_t p = {0 };
246
256
if (msc_update_state <= MSC_UPDATE_STARTING && buffer[0 ] == 0xE9 ){
247
257
msc_update_state = MSC_UPDATE_RUNNING;
248
258
msc_update_start_sector = lba;
249
259
msc_update_bytes_written = 0 ;
250
- log_v (" UPDATE_START: %u (0x%02X)" , lba, lba - msc_boot->sectors_per_alloc_table );
260
+ log_d (" UPDATE_START: %u (0x%02X)" , lba, lba - msc_boot->sectors_per_alloc_table );
251
261
arduino_usb_event_post (ARDUINO_FIRMWARE_MSC_EVENTS, ARDUINO_FIRMWARE_MSC_START_EVENT, &p, sizeof (arduino_firmware_msc_event_data_t ), portMAX_DELAY);
252
262
if (msc_update_write (msc_ota_partition, ((lba - msc_update_start_sector) * DISK_SECTOR_SIZE) + offset, buffer, bufsize) == ESP_OK){
253
263
log_v (" UPDATE_WRITE: %u %u" , ((lba - msc_update_start_sector) * DISK_SECTOR_SIZE) + offset, bufsize);
@@ -285,9 +295,9 @@ static int32_t msc_read(uint32_t lba, uint32_t offset, void* buffer, uint32_t bu
285
295
// log_d("lba: %u, offset: %u, bufsize: %u", lba, offset, bufsize);
286
296
if (lba < fw_start_sector){
287
297
memcpy (buffer, msc_ram_disk + (lba * DISK_SECTOR_SIZE) + offset, bufsize);
288
- } else if (lba < fw_end_sector){
298
+ } else if (msc_run_partition && lba < fw_end_sector){
289
299
// read the currently running firmware
290
- if (!msc_run_partition || esp_partition_read (msc_run_partition, ((lba - fw_start_sector) * DISK_SECTOR_SIZE) + offset, buffer, bufsize) != ESP_OK){
300
+ if (esp_partition_read (msc_run_partition, ((lba - fw_start_sector) * DISK_SECTOR_SIZE) + offset, buffer, bufsize) != ESP_OK){
291
301
return 0 ;
292
302
}
293
303
} else {
@@ -324,7 +334,17 @@ FirmwareMSC::FirmwareMSC():msc(){}
324
334
FirmwareMSC::~FirmwareMSC (){}
325
335
326
336
bool FirmwareMSC::begin (){
327
- msc_update_setup_disk (" ESP32-FWMSC" , 0x0 );
337
+ if (!msc_update_setup_disk (" ESP32-FWMSC" , 0x0 )){
338
+ return false ;
339
+ }
340
+
341
+ if (!msc_task_handle){
342
+ xTaskCreateUniversal (msc_task, " msc_disk" , 1024 , NULL , 2 , (TaskHandle_t*)&msc_task_handle, 0 );
343
+ if (!msc_task_handle){
344
+ return false ;
345
+ }
346
+ }
347
+
328
348
msc.vendorID (" ESP32S2" );// max 8 chars
329
349
msc.productID (" Firmware MSC" );// max 16 chars
330
350
msc.productRevision (" 1.0" );// max 4 chars
@@ -333,13 +353,6 @@ bool FirmwareMSC::begin(){
333
353
msc.onWrite (msc_write);
334
354
msc.mediaPresent (true );
335
355
msc.begin (msc_boot->fat12_sector_num , DISK_SECTOR_SIZE);
336
-
337
- if (!msc_task_handle){
338
- xTaskCreateUniversal (msc_task, " msc_disk" , 1024 , NULL , 2 , (TaskHandle_t*)&msc_task_handle, 0 );
339
- if (!msc_task_handle){
340
- return false ;
341
- }
342
- }
343
356
return true ;
344
357
}
345
358
0 commit comments