# Conflicts:
#	src/usb/uf2/ghostfat.c
This commit is contained in:
Henry Gabryjelski 2019-03-13 22:39:12 -07:00
commit 6d58633db0
2 changed files with 18 additions and 13 deletions

View File

@ -63,7 +63,8 @@ int write_block(uint32_t block_no, uint8_t *data, bool quiet, WriteState *state)
int32_t tud_msc_scsi_cb (uint8_t lun, uint8_t const scsi_cmd[16], void* buffer, uint16_t bufsize) int32_t tud_msc_scsi_cb (uint8_t lun, uint8_t const scsi_cmd[16], void* buffer, uint16_t bufsize)
{ {
void const* response = NULL; void const* response = NULL;
uint16_t resplen = 0; int32_t resplen = 0;
memset(buffer, 0, bufsize);
switch ( scsi_cmd[0] ) switch ( scsi_cmd[0] )
{ {
@ -98,7 +99,7 @@ int32_t tud_msc_scsi_cb (uint8_t lun, uint8_t const scsi_cmd[16], void* buffer,
} }
// return len must not larger than bufsize // return len must not larger than bufsize
if ( resplen > bufsize ) resplen = bufsize; if ( resplen > (int32_t)bufsize ) resplen = bufsize;
// copy response to stack's buffer if any // copy response to stack's buffer if any
if ( response && resplen ) if ( response && resplen )
@ -114,6 +115,7 @@ int32_t tud_msc_scsi_cb (uint8_t lun, uint8_t const scsi_cmd[16], void* buffer,
int32_t tud_msc_read10_cb (uint8_t lun, uint32_t lba, uint32_t offset, void* buffer, uint32_t bufsize) int32_t tud_msc_read10_cb (uint8_t lun, uint32_t lba, uint32_t offset, void* buffer, uint32_t bufsize)
{ {
(void) lun; (void) lun;
memset(buffer, 0, bufsize);
// since we return block size each, offset should always be zero // since we return block size each, offset should always be zero
TU_ASSERT(offset == 0, -1); TU_ASSERT(offset == 0, -1);

View File

@ -177,6 +177,7 @@ static FAT_BootBlock const BootBlock = {
.SectorsPerFAT = SECTORS_PER_FAT, .SectorsPerFAT = SECTORS_PER_FAT,
.SectorsPerTrack = 1, .SectorsPerTrack = 1,
.Heads = 1, .Heads = 1,
.PhysicalDriveNum = 0x80, // to match MediaDescriptor of 0xF8
.ExtendedBootSig = 0x29, .ExtendedBootSig = 0x29,
.VolumeSerialNumber = 0x00420042, .VolumeSerialNumber = 0x00420042,
.VolumeLabel = VOLUME_LABEL, .VolumeLabel = VOLUME_LABEL,
@ -190,35 +191,37 @@ static FAT_BootBlock const BootBlock = {
static uint32_t current_flash_size(void) static uint32_t current_flash_size(void)
{ {
static uint32_t flash_sz = 0; static uint32_t flash_sz = 0;
uint32_t result = flash_sz; // presumes atomic 32-bit read/write and static result
// only need to compute once // only need to compute once
if ( flash_sz == 0 ) if ( result == 0 )
{ {
// return 1 block of 256 bytes // return 1 block of 256 bytes
if ( !bootloader_app_is_valid(DFU_BANK_0_REGION_START) ) if ( !bootloader_app_is_valid(DFU_BANK_0_REGION_START) )
{ {
flash_sz = 256; result = 256;
}else }else
{ {
bootloader_settings_t const * boot_setting; bootloader_settings_t const * boot_setting;
bootloader_util_settings_get(&boot_setting); bootloader_util_settings_get(&boot_setting);
flash_sz = boot_setting->bank_0_size; result = boot_setting->bank_0_size;
// Copy size must be multiple of 256 bytes // Copy size must be multiple of 256 bytes
// else we will got an issue copying current.uf2 // else we will got an issue copying current.uf2
if (flash_sz & 0xff) if (result & 0xff)
{ {
flash_sz = (flash_sz & ~0xff) + 256; result = (result & ~0xff) + 256;
} }
// if bank0 size is not valid, happens when flashed with jlink // if bank0 size is not valid, happens when flashed with jlink
// use maximum application size // use maximum application size
if ( (flash_sz == 0) || (flash_sz == 0xFFFFFFFFUL) ) if ( (result == 0) || (result == 0xFFFFFFFFUL) )
{ {
flash_sz = FLASH_SIZE; result = FLASH_SIZE;
} }
} }
flash_sz = result; // presumes atomic 32-bit read/write and static result
} }
return flash_sz; return flash_sz;
@ -243,16 +246,16 @@ void read_block(uint32_t block_no, uint8_t *data) {
memset(data, 0, 512); memset(data, 0, 512);
uint32_t sectionIdx = block_no; uint32_t sectionIdx = block_no;
if (block_no == 0) { if (block_no == 0) { // Requested boot block
memcpy(data, &BootBlock, sizeof(BootBlock)); memcpy(data, &BootBlock, sizeof(BootBlock));
data[510] = 0x55; data[510] = 0x55;
data[511] = 0xaa; data[511] = 0xaa;
// logval("data[0]", data[0]); // logval("data[0]", data[0]);
} else if (block_no < START_ROOTDIR) { } else if (block_no < START_ROOTDIR) { // Requested FAT table sector
sectionIdx -= START_FAT0; sectionIdx -= START_FAT0;
// logval("sidx", sectionIdx); // logval("sidx", sectionIdx);
if (sectionIdx >= SECTORS_PER_FAT) if (sectionIdx >= SECTORS_PER_FAT)
sectionIdx -= SECTORS_PER_FAT; sectionIdx -= SECTORS_PER_FAT; // second FAT is same as the first...
if (sectionIdx == 0) { if (sectionIdx == 0) {
data[0] = 0xf0; data[0] = 0xf0;
// WARNING -- code presumes only one NULL .content for .UF2 file // WARNING -- code presumes only one NULL .content for .UF2 file
@ -262,7 +265,7 @@ void read_block(uint32_t block_no, uint8_t *data) {
data[i] = 0xff; data[i] = 0xff;
} }
} }
for (int i = 0; i < 256; ++i) { for (int i = 0; i < 256; ++i) { // Generate the FAT chain for the firmware "file"
uint32_t v = sectionIdx * 256 + i; uint32_t v = sectionIdx * 256 + i;
if (UF2_FIRST_SECTOR <= v && v <= UF2_LAST_SECTOR) if (UF2_FIRST_SECTOR <= v && v <= UF2_LAST_SECTOR)
((uint16_t *)(void *)data)[i] = v == UF2_LAST_SECTOR ? 0xffff : v + 1; ((uint16_t *)(void *)data)[i] = v == UF2_LAST_SECTOR ? 0xffff : v + 1;