Userdata export: Fix remaining regressions in FAT generation.

This commit is contained in:
Ari Sundholm 2020-11-17 14:51:29 +02:00
parent c5c3d28b48
commit 12436a3d3f
4 changed files with 5498 additions and 5502 deletions

File diff suppressed because it is too large Load Diff

View File

@ -83,15 +83,6 @@ static const alt_u8 fat16_preamble[4] = {
0xf8, 0xff, 0xff, 0xff,
};
#if 0
/*
* To generate each FAT, we need 1040 bytes of memory.
* The buffer is assumed to be zeroed out.
* XXX: This is a problem!
*/
#define FAT16_ALLOC_SIZE 0x420U
#endif
/*
* Generate a FAT.
* The buffer is assumed to be zeroed out and have a size of at least
@ -105,12 +96,22 @@ static const alt_u8 fat16_preamble[4] = {
alt_u16 generate_fat16(alt_u8 *const fat, const alt_u16 written) {
alt_u16 cur_ofs = 0;
const alt_u16 start_cluster = 3U + written;
const alt_u16 clusters_to_write = !written
? ((FAT16_SECTOR_SIZE - sizeof(fat16_preamble)) >> FAT16_ENTRY_SHIFT)
: (((514U - written) > (FAT16_SECTOR_SIZE >> FAT16_ENTRY_SHIFT))
? (FAT16_SECTOR_SIZE >> FAT16_ENTRY_SHIFT)
: (514U - written));
/*
* The total number of FAT entries to write consists of:
* 1. The FAT "preamble" (2 entries),
* 2. The cluster chain of the file (512 entries).
*
* The latter needs to contain the chain terminator.
*/
const alt_u16 clusters_remaining = PROF_16_CLUSTER_COUNT - written;
const alt_u16 preamble_compensation = written ? 0 : 2U;
const alt_u16 clusters_to_write =
((clusters_remaining > FAT16_ENTRIES_PER_SECTOR)
? FAT16_ENTRIES_PER_SECTOR
: clusters_remaining) - preamble_compensation;
const alt_u16 end_cluster = start_cluster + clusters_to_write;
static const alt_u16 last_fat_cluster = PROF_16_CLUSTER_COUNT + 2U;
if (!written) {
memcpy(fat, fat16_preamble, sizeof(fat16_preamble));
@ -119,15 +120,17 @@ alt_u16 generate_fat16(alt_u8 *const fat, const alt_u16 written) {
for (alt_u16 cluster = start_cluster; cluster < end_cluster; ++cluster) {
/* FAT16 entries are 16-bit little-endian. */
fat[cur_ofs++] = cluster & 0xffU;
fat[cur_ofs++] = (cluster >> 8U) & 0xffU;
if (cluster == last_fat_cluster) {
/* At the last cluster, write the chain terminator. */
fat[cur_ofs++] = 0xff;
fat[cur_ofs++] = 0xff;
}
else {
fat[cur_ofs++] = cluster & 0xffU;
fat[cur_ofs++] = (cluster >> 8U) & 0xffU;
}
}
/* After the last cluster, write the chain terminator. */
if (end_cluster == 514U) {
fat[cur_ofs++] = 0xff;
fat[cur_ofs++] = 0xff;
}
return end_cluster - 3U;
}

View File

@ -26,10 +26,10 @@
/* Use a sector size of 512 bytes. */
#define FAT16_SECTOR_SIZE 512U
//#define FAT16_BOOT_SECTOR_SIZE FAT16_SECTOR_SIZE
/* This volume has 2048-byte clusters. */
#define FAT16_CLUSTER_SIZE 2048U
#define FAT16_SECTORS_PER_CLUSTER (FAT16_CLUSTER_SIZE/FAT16_SECTOR_SIZE)
/* Offsets of the two File Allocation Tables. */
#define FAT16_1_OFS 0x10000UL
@ -38,6 +38,7 @@
/* Each FAT16 entry is a 16-bit little-endian integer. */
#define FAT16_ENTRY_SIZE 2U
#define FAT16_ENTRY_SHIFT 1U
#define FAT16_ENTRIES_PER_SECTOR (FAT16_SECTOR_SIZE >> FAT16_ENTRY_SHIFT)
/* On this volume, each FAT will be 16 kiB in size. */
#define FAT16_SIZE 0x04000UL
@ -53,6 +54,7 @@ extern const alt_u8 prof_dirent_16[PROF_DIRENT_16_SIZE];
#define PROF_16_DATA_OFS 0x028000UL
#define PROF_16_DATA_SIZE 0x100000UL
#define PROF_16_CLUSTER_COUNT (PROF_16_DATA_SIZE/FAT16_CLUSTER_SIZE)
/* Profile file data starts at offset 0x00028000 */
/* Profile file data ends at offset 0x00128000 */
/* Profile file data is exactly 1 MiB long. */

View File

@ -417,7 +417,7 @@ eval_button:
ui_disp_menu(2);
/* Generate and write the boot sector. */
memset(databuf, 0, SD_BLK_SIZE); /* Must be at least 512 bytes! */
memset(databuf, 0, SD_BLK_SIZE);
generate_boot_sector_16(databuf);
retval = SD_Write(&sdcard_dev, databuf, 0);
if (retval)
@ -439,7 +439,7 @@ eval_button:
clusters_written < (PROF_16_DATA_SIZE/FAT16_CLUSTER_SIZE);)
{
alt_u16 count;
memset(databuf, 0, SD_BLK_SIZE); /* Must be at least 512 bytes! */
memset(databuf, 0, SD_BLK_SIZE);
count = generate_fat16(databuf, clusters_written);
retval = SD_Write(&sdcard_dev, databuf,
(FAT16_1_OFS/SD_BLK_SIZE) + sd_blk_idx);
@ -452,7 +452,7 @@ eval_button:
goto out;
++sd_blk_idx;
clusters_written += count;
clusters_written = count;
}
/* Write the directory entry of the settings file. */