1
0
mirror of https://github.com/marqs85/ossc.git synced 2024-12-29 09:32:45 +00:00

Userdata export: Zero out whole FAT area and handle SD write errors.

This commit is contained in:
Ari Sundholm 2020-11-17 02:31:22 +02:00
parent 8068542da1
commit 67a64693c1
4 changed files with 5566 additions and 5535 deletions

File diff suppressed because it is too large Load Diff

View File

@ -107,8 +107,8 @@ alt_u16 generate_fat16(alt_u8 *const fat, const alt_u16 written) {
const alt_u16 start_cluster = 3U + written; const alt_u16 start_cluster = 3U + written;
const alt_u16 clusters_to_write = !written const alt_u16 clusters_to_write = !written
? ((FAT16_SECTOR_SIZE - sizeof(fat16_preamble)) >> FAT16_ENTRY_SHIFT) ? ((FAT16_SECTOR_SIZE - sizeof(fat16_preamble)) >> FAT16_ENTRY_SHIFT)
: (((514U - written) > (512U >> FAT16_ENTRY_SHIFT)) : (((514U - written) > (FAT16_SECTOR_SIZE >> FAT16_ENTRY_SHIFT))
? (512U >> FAT16_ENTRY_SHIFT) ? (FAT16_SECTOR_SIZE >> FAT16_ENTRY_SHIFT)
: (514U - written)); : (514U - written));
const alt_u16 end_cluster = start_cluster + clusters_to_write; const alt_u16 end_cluster = start_cluster + clusters_to_write;

View File

@ -35,20 +35,19 @@
#define FAT16_1_OFS 0x10000UL #define FAT16_1_OFS 0x10000UL
#define FAT16_2_OFS 0x14000UL #define FAT16_2_OFS 0x14000UL
/* Each FAT16 entry is a 16-bit little-endian integer. */ /* Each FAT16 entry is a 16-bit little-endian integer. */
#define FAT16_ENTRY_SIZE 2U #define FAT16_ENTRY_SIZE 2U
#define FAT16_ENTRY_SHIFT 1 #define FAT16_ENTRY_SHIFT 1U
/* On this volume, each FAT will be 16 kiB in size. */ /* On this volume, each FAT will be 16 kiB in size. */
#define FAT16_SIZE 0x4000U #define FAT16_SIZE 0x04000UL
/* /*
* Define the properties and contents of the directory entry for the * Define the properties and contents of the directory entry for the
* settings file. * settings file.
*/ */
#define PROF_DIRENT_16_OFS 0x18000UL #define PROF_DIRENT_16_OFS 0x18000UL
#define PROF_DIRENT_16_SIZE 32UL #define PROF_DIRENT_16_SIZE 32U
extern const alt_u8 prof_dirent_16[PROF_DIRENT_16_SIZE]; extern const alt_u8 prof_dirent_16[PROF_DIRENT_16_SIZE];

View File

@ -379,6 +379,8 @@ int export_userdata()
"Press 1 or 2"; // [48..60] "Press 1 or 2"; // [48..60]
alt_u32 btn_vec; alt_u32 btn_vec;
_Static_assert(SD_BLK_SIZE == FAT16_SECTOR_SIZE, "Sector size mismatch");
retval = check_sdcard(databuf); retval = check_sdcard(databuf);
SPI_CS_High(); SPI_CS_High();
if (retval != 0) { if (retval != 0) {
@ -414,14 +416,23 @@ eval_button:
strncpy(menu_row2, "Exporting...", LCD_ROW_LEN+1); strncpy(menu_row2, "Exporting...", LCD_ROW_LEN+1);
ui_disp_menu(2); ui_disp_menu(2);
/* TODO: write FAT */
_Static_assert(SD_BLK_SIZE == FAT16_SECTOR_SIZE, "Sector size mismatch");
/* Generate and write the boot sector. */ /* Generate and write the boot sector. */
memset(databuf, 0, SD_BLK_SIZE); /* Must be at least 512 bytes! */ memset(databuf, 0, SD_BLK_SIZE); /* Must be at least 512 bytes! */
generate_boot_sector_16(databuf); generate_boot_sector_16(databuf);
SD_Write(&sdcard_dev, databuf, 0); retval = SD_Write(&sdcard_dev, databuf, 0);
/* ERROR HANDLING */ if (retval)
goto out;
/* Zero out the FAT area */
/* TODO: a proper erase would be more ideal... */
memset(databuf, 0, SD_BLK_SIZE);
for (alt_u32 fat_sector = FAT16_1_OFS/SD_BLK_SIZE;
fat_sector < (FAT16_2_OFS + FAT16_SIZE)/SD_BLK_SIZE; ++fat_sector)
{
retval = SD_Write(&sdcard_dev, databuf, fat_sector);
if (retval)
goto out;
}
/* Generate and write the file allocation tables. */ /* Generate and write the file allocation tables. */
for (alt_u16 clusters_written = 0, sd_blk_idx = 0; for (alt_u16 clusters_written = 0, sd_blk_idx = 0;
@ -430,13 +441,15 @@ eval_button:
alt_u16 count; alt_u16 count;
memset(databuf, 0, SD_BLK_SIZE); /* Must be at least 512 bytes! */ memset(databuf, 0, SD_BLK_SIZE); /* Must be at least 512 bytes! */
count = generate_fat16(databuf, clusters_written); count = generate_fat16(databuf, clusters_written);
SD_Write(&sdcard_dev, databuf, retval = SD_Write(&sdcard_dev, databuf,
(FAT16_1_OFS/SD_BLK_SIZE) + sd_blk_idx); (FAT16_1_OFS/SD_BLK_SIZE) + sd_blk_idx);
/* ERROR HANDLING */ if (retval)
goto out;
SD_Write(&sdcard_dev, databuf, retval = SD_Write(&sdcard_dev, databuf,
(FAT16_2_OFS/SD_BLK_SIZE) + sd_blk_idx); (FAT16_2_OFS/SD_BLK_SIZE) + sd_blk_idx);
/* ERROR HANDLING */ if (retval)
goto out;
++sd_blk_idx; ++sd_blk_idx;
clusters_written += count; clusters_written += count;
@ -445,8 +458,9 @@ eval_button:
/* Write the directory entry of the settings file. */ /* Write the directory entry of the settings file. */
memset(databuf, 0, SD_BLK_SIZE); memset(databuf, 0, SD_BLK_SIZE);
memcpy(databuf, prof_dirent_16, PROF_DIRENT_16_SIZE); memcpy(databuf, prof_dirent_16, PROF_DIRENT_16_SIZE);
SD_Write(&sdcard_dev, databuf, PROF_DIRENT_16_OFS/SD_BLK_SIZE); retval = SD_Write(&sdcard_dev, databuf, PROF_DIRENT_16_OFS/SD_BLK_SIZE);
/* ERROR HANDLING */ if (retval)
goto out;
/* This may wear the SD card a bit more than necessary... */ /* This may wear the SD card a bit more than necessary... */
retval = copy_flash_to_sd(USERDATA_OFFSET/PAGESIZE, retval = copy_flash_to_sd(USERDATA_OFFSET/PAGESIZE,