Skip to content

Commit 4bf4cb0

Browse files
committed
Support overwriting of the firmware file
Overwriting a file is done totally differently on MacOS, Windows and Linux. This change supports it on all of them.
1 parent fabbda6 commit 4bf4cb0

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

libraries/USB/src/FirmwareMSC.cpp

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ static void msc_update_setup_disk(const char * volume_label, uint32_t serial_num
107107
msc_table = fat_add_table(msc_ram_disk, msc_boot, mcs_is_fat16);
108108
//fat_dir_entry_t * label = fat_add_label(msc_ram_disk, volume_label);
109109
if(msc_run_partition){
110-
fw_entry = fat_add_root_file(msc_ram_disk, 0, "ACTIVEFW", "BIN", fw_size, 2, mcs_is_fat16);
110+
fw_entry = fat_add_root_file(msc_ram_disk, 0, "FIRMWARE", "BIN", fw_size, 2, mcs_is_fat16);
111111
fw_end_sector = FAT_SIZE_TO_SECTORS(fw_size) + fw_start_sector;
112112
}
113113
}
@@ -117,10 +117,6 @@ static fat_dir_entry_t * msc_update_get_root_bin_entry(uint8_t index){
117117
fat_dir_entry_t * entry = (fat_dir_entry_t *)(msc_ram_disk + ((msc_boot->sectors_per_alloc_table+1) * DISK_SECTOR_SIZE) + (index * sizeof(fat_dir_entry_t)));
118118
fat_lfn_entry_t * lfn = (fat_lfn_entry_t*)entry;
119119

120-
//fw read entry
121-
if(entry == fw_entry){
122-
return NULL;
123-
}
124120
//empty entry
125121
if(entry->file_magic == 0){
126122
return NULL;
@@ -146,7 +142,8 @@ static fat_dir_entry_t * msc_update_get_root_bin_entry(uint8_t index){
146142

147143
//get an empty bin (the host will add an entry for file about to be written with size of zero)
148144
static fat_dir_entry_t * msc_update_find_new_bin(){
149-
for(uint8_t i=15; i; i--){
145+
for(uint8_t i=16; i;){
146+
i--;
150147
fat_dir_entry_t * entry = msc_update_get_root_bin_entry(i);
151148
if(entry && entry->file_size == 0){
152149
return entry;
@@ -155,6 +152,18 @@ static fat_dir_entry_t * msc_update_find_new_bin(){
155152
return NULL;
156153
}
157154

155+
//get a bin starting from particular sector
156+
static fat_dir_entry_t * msc_update_find_bin(uint16_t sector){
157+
for(uint8_t i=16; i; ){
158+
i--;
159+
fat_dir_entry_t * entry = msc_update_get_root_bin_entry(i);
160+
if(entry && entry->data_start_sector == (sector - msc_boot->sectors_per_alloc_table)){
161+
return entry;
162+
}
163+
}
164+
return NULL;
165+
}
166+
158167
//write the new data and erase the flash blocks when necessary
159168
static esp_err_t msc_update_write(const esp_partition_t *partition, uint32_t offset, void *data, size_t size){
160169
esp_err_t err = ESP_OK;
@@ -222,13 +231,16 @@ static int32_t msc_write(uint32_t lba, uint32_t offset, uint8_t* buffer, uint32_
222231
}
223232
msc_update_entry = update_entry;
224233
} else if(msc_update_state == MSC_UPDATE_RUNNING){
234+
if(!msc_update_entry && msc_update_start_sector){
235+
msc_update_entry = msc_update_find_bin(msc_update_start_sector);
236+
}
225237
if(msc_update_entry && msc_update_bytes_written >= msc_update_entry->file_size){
226238
msc_update_end();
227239
}
228240
}
229241
}
230242
}
231-
} else if(lba >= fw_end_sector && lba >= msc_update_start_sector){
243+
} else if(lba >= msc_update_start_sector){
232244
//handle writes to the region where the new firmware will be uploaded
233245
arduino_firmware_msc_event_data_t p = {0};
234246
if(msc_update_state <= MSC_UPDATE_STARTING && buffer[0] == 0xE9){
@@ -312,7 +324,7 @@ FirmwareMSC::FirmwareMSC():msc(){}
312324
FirmwareMSC::~FirmwareMSC(){}
313325

314326
bool FirmwareMSC::begin(){
315-
msc_update_setup_disk("ESP32S2-FW", 0x0);
327+
msc_update_setup_disk("ESP32-FWMSC", 0x0);
316328
msc.vendorID("ESP32S2");//max 8 chars
317329
msc.productID("Firmware MSC");//max 16 chars
318330
msc.productRevision("1.0");//max 4 chars

0 commit comments

Comments
 (0)