Improved empty save compatibility by filling initial save with 0xFF

This commit is contained in:
Mateusz Faderewski 2023-08-20 17:54:37 +02:00
parent 65a578c571
commit de97bedc00
3 changed files with 40 additions and 0 deletions

View File

@ -140,6 +140,9 @@ flashcart_error_t flashcart_load_save (char *save_path, flashcart_save_type_t sa
if (file_allocate(save_path, SAVE_SIZE[save_type])) {
return FLASHCART_ERROR_LOAD;
}
if (file_fill(save_path, 0xFF)) {
return FLASHCART_ERROR_LOAD;
}
}
if (file_get_size(save_path) != SAVE_SIZE[save_type]) {

View File

@ -70,6 +70,42 @@ bool file_allocate (char *path, size_t size) {
return error;
}
bool file_fill (char *path, uint8_t value) {
FIL fil;
bool error = false;
uint8_t buffer[FS_SECTOR_SIZE * 8];
FRESULT res;
UINT bytes_to_write;
UINT bytes_written;
for (int i = 0; i < sizeof(buffer); i++) {
buffer[i] = value;
}
if (f_open(&fil, strip_sd_prefix(path), FA_WRITE) != FR_OK) {
return true;
}
for (int i = 0; i < f_size(&fil); i += sizeof(buffer)) {
bytes_to_write = MIN(f_size(&fil) - f_tell(&fil), sizeof(buffer));
res = f_write(&fil, buffer, bytes_to_write, &bytes_written);
if ((res != FR_OK) || (bytes_to_write != bytes_written)) {
error = true;
break;
}
}
if (f_tell(&fil) != f_size(&fil)) {
error = true;
}
if (f_close(&fil) != FR_OK) {
error = true;
}
return error;
}
bool file_get_sectors (char *path, uint32_t *sectors, size_t entries) {
FATFS *fs;
FIL fil;

View File

@ -16,6 +16,7 @@ bool file_exists (char *path);
size_t file_get_size (char *path);
bool file_delete (char *path);
bool file_allocate (char *path, size_t size);
bool file_fill (char *path, uint8_t value);
bool file_get_sectors (char *path, uint32_t *sectors, size_t entries);
bool file_has_extensions (char *path, const char *extensions[]);