Skip to content

Commit 934b646

Browse files
committed
Fix cases where one of the partitions might not be available
1 parent b5819d1 commit 934b646

File tree

1 file changed

+34
-21
lines changed

1 file changed

+34
-21
lines changed

cores/esp32/FirmwareMSC.cpp

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,15 @@ static size_t msc_update_get_required_disk_sectors(){
7373
if(msc_run_partition){
7474
fw_size = get_firmware_size(msc_run_partition);
7575
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");
7779
}
7880
if(msc_ota_partition){
7981
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");
8185
}
8286
msc_table_sectors = fat_sectors_per_alloc_table(data_sectors, false);
8387
total_sectors = data_sectors + msc_table_sectors + 2;
@@ -91,16 +95,21 @@ static size_t msc_update_get_required_disk_sectors(){
9195
log_d("USING FAT12");
9296
mcs_is_fat16 = false;
9397
}
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);
96101
return total_sectors;
97102
}
98103

99104
//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){
101106
msc_total_sectors = msc_update_get_required_disk_sectors();
102107
uint8_t ram_sectors = msc_table_sectors + 2;
103108
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+
}
104113
fw_start_sector = ram_sectors;
105114
fw_end_sector = fw_start_sector;
106115
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
110119
fw_entry = fat_add_root_file(msc_ram_disk, 0, "FIRMWARE", "BIN", fw_size, 2, mcs_is_fat16);
111120
fw_end_sector = FAT_SIZE_TO_SECTORS(fw_size) + fw_start_sector;
112121
}
122+
return true;
113123
}
114124

115125
//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
169179
esp_err_t err = ESP_OK;
170180
if((offset & (SPI_FLASH_SEC_SIZE-1)) == 0){
171181
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");
173183
if(err != ESP_OK){
174184
return err;
175185
}
@@ -179,7 +189,7 @@ static esp_err_t msc_update_write(const esp_partition_t *partition, uint32_t off
179189

180190
//called when error was encountered while updating
181191
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);
183193
arduino_firmware_msc_event_data_t p = {0};
184194
p.error.size = msc_update_bytes_written;
185195
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(){
191201

192202
//called when all firmware bytes have been received
193203
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);
195205
msc_update_state = MSC_UPDATE_END;
196206
size_t ota_size = get_firmware_size(msc_ota_partition);
197207
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_
214224
if(lba < fw_start_sector){
215225
//write to sectors that are in RAM
216226
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)){
218228
//monitor the root folder table
219229
if(msc_update_state <= MSC_UPDATE_RUNNING){
220230
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_
240250
}
241251
}
242252
}
243-
} else if(lba >= msc_update_start_sector){
253+
} else if(msc_ota_partition && lba >= msc_update_start_sector){
244254
//handle writes to the region where the new firmware will be uploaded
245255
arduino_firmware_msc_event_data_t p = {0};
246256
if(msc_update_state <= MSC_UPDATE_STARTING && buffer[0] == 0xE9){
247257
msc_update_state = MSC_UPDATE_RUNNING;
248258
msc_update_start_sector = lba;
249259
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);
251261
arduino_usb_event_post(ARDUINO_FIRMWARE_MSC_EVENTS, ARDUINO_FIRMWARE_MSC_START_EVENT, &p, sizeof(arduino_firmware_msc_event_data_t), portMAX_DELAY);
252262
if(msc_update_write(msc_ota_partition, ((lba - msc_update_start_sector) * DISK_SECTOR_SIZE) + offset, buffer, bufsize) == ESP_OK){
253263
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
285295
//log_d("lba: %u, offset: %u, bufsize: %u", lba, offset, bufsize);
286296
if(lba < fw_start_sector){
287297
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){
289299
//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){
291301
return 0;
292302
}
293303
} else {
@@ -324,7 +334,17 @@ FirmwareMSC::FirmwareMSC():msc(){}
324334
FirmwareMSC::~FirmwareMSC(){}
325335

326336
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+
328348
msc.vendorID("ESP32S2");//max 8 chars
329349
msc.productID("Firmware MSC");//max 16 chars
330350
msc.productRevision("1.0");//max 4 chars
@@ -333,13 +353,6 @@ bool FirmwareMSC::begin(){
333353
msc.onWrite(msc_write);
334354
msc.mediaPresent(true);
335355
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-
}
343356
return true;
344357
}
345358

0 commit comments

Comments
 (0)