Note this has very few guardrails
Code: Select all
uint32_t u8to32(const uint8_t d1, const uint8_t d2, const uint8_t d3, const uint8_t d4) {
return (d4<<24) + (d3<<16) + (d2<<8) + d1;
}
bool setVolName(const char* volname) {
if (strlen(volname) > 11) {
Serial.println("Volume name can only be 11 characters");
return false;
}
const esp_partition_t* Part = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_FAT, NULL);
uint32_t diskbuf[1024];
if (esp_partition_read(Part, 0, diskbuf, 4096) != ESP_OK) {
Serial.println("Unable to read ffat partition");
return false;
}
char set_name[13];
snprintf(set_name, 13, "%-11s%c", volname, 8);
// First grouping is old school partition name
diskbuf[10] = (diskbuf[10] & 0x00FFFFFF) | (set_name[0]<<24);
diskbuf[11] = u8to32(set_name[1],set_name[2],set_name[3],set_name[4]);
diskbuf[12] = u8to32(set_name[5],set_name[6],set_name[7],set_name[8]);
diskbuf[13] = (diskbuf[13] & 0xFFFF0000) | set_name[9] | (set_name[10]<<8);
if (esp_partition_erase_range(Part, 0, 4096)) return false;
if (esp_partition_write(Part, 0, diskbuf, 4096)) return false;
// Second grouping is the volume name that will likely be used
esp_partition_read(Part, 0x2000, diskbuf, 4096);
diskbuf[8] = u8to32(set_name[0],set_name[1],set_name[2],set_name[3]);
diskbuf[9] = u8to32(set_name[4],set_name[5],set_name[6],set_name[7]);
diskbuf[10] = u8to32(set_name[8],set_name[9],set_name[10],set_name[11]);
esp_partition_erase_range(Part, 0x2000, 4096);
if (esp_partition_write(Part, 0x2000, diskbuf, 4096)) return false;
return true;
}