Skip to content

Commit 113195e

Browse files
authored
Merge branch 'master' into patch-2
2 parents a258c6d + 0eeff3e commit 113195e

File tree

2 files changed

+22
-19
lines changed

2 files changed

+22
-19
lines changed

src/usb/msc_uf2.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ int write_block(uint32_t block_no, uint8_t *data, bool quiet, WriteState *state)
6363
int32_t tud_msc_scsi_cb (uint8_t lun, uint8_t const scsi_cmd[16], void* buffer, uint16_t bufsize)
6464
{
6565
void const* response = NULL;
66-
uint16_t resplen = 0;
66+
int32_t resplen = 0;
6767
memset(buffer, 0, bufsize);
6868

6969
switch ( scsi_cmd[0] )
@@ -99,7 +99,7 @@ int32_t tud_msc_scsi_cb (uint8_t lun, uint8_t const scsi_cmd[16], void* buffer,
9999
}
100100

101101
// return len must not larger than bufsize
102-
if ( resplen > bufsize ) resplen = bufsize;
102+
if ( resplen > (int32_t)bufsize ) resplen = bufsize;
103103

104104
// copy response to stack's buffer if any
105105
if ( response && resplen )

src/usb/uf2/ghostfat.c

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ static FAT_BootBlock const BootBlock = {
110110
.SectorsPerFAT = SECTORS_PER_FAT,
111111
.SectorsPerTrack = 1,
112112
.Heads = 1,
113+
.PhysicalDriveNum = 0x80, // to match MediaDescriptor of 0xF8
113114
.ExtendedBootSig = 0x29,
114115
.VolumeSerialNumber = 0x00420042,
115116
.VolumeLabel = VOLUME_LABEL,
@@ -123,35 +124,37 @@ static FAT_BootBlock const BootBlock = {
123124
static uint32_t current_flash_size(void)
124125
{
125126
static uint32_t flash_sz = 0;
127+
uint32_t result = flash_sz; // presumes atomic 32-bit read/write and static result
126128

127129
// only need to compute once
128-
if ( flash_sz == 0 )
130+
if ( result == 0 )
129131
{
130132
// return 1 block of 256 bytes
131133
if ( !bootloader_app_is_valid(DFU_BANK_0_REGION_START) )
132134
{
133-
flash_sz = 256;
135+
result = 256;
134136
}else
135137
{
136138
bootloader_settings_t const * boot_setting;
137139
bootloader_util_settings_get(&boot_setting);
138140

139-
flash_sz = boot_setting->bank_0_size;
141+
result = boot_setting->bank_0_size;
140142

141143
// Copy size must be multiple of 256 bytes
142144
// else we will got an issue copying current.uf2
143-
if (flash_sz & 0xff)
145+
if (result & 0xff)
144146
{
145-
flash_sz = (flash_sz & ~0xff) + 256;
147+
result = (result & ~0xff) + 256;
146148
}
147149

148150
// if bank0 size is not valid, happens when flashed with jlink
149151
// use maximum application size
150-
if ( (flash_sz == 0) || (flash_sz == 0xFFFFFFFFUL) )
152+
if ( (result == 0) || (result == 0xFFFFFFFFUL) )
151153
{
152-
flash_sz = FLASH_SIZE;
154+
result = FLASH_SIZE;
153155
}
154156
}
157+
flash_sz = result; // presumes atomic 32-bit read/write and static result
155158
}
156159

157160
return flash_sz;
@@ -176,30 +179,30 @@ void read_block(uint32_t block_no, uint8_t *data) {
176179
memset(data, 0, 512);
177180
uint32_t sectionIdx = block_no;
178181

179-
if (block_no == 0) {
182+
if (block_no == 0) { // Requested boot block
180183
memcpy(data, &BootBlock, sizeof(BootBlock));
181184
data[510] = 0x55;
182185
data[511] = 0xaa;
183186
// logval("data[0]", data[0]);
184-
} else if (block_no < START_ROOTDIR) {
187+
} else if (block_no < START_ROOTDIR) { // Requested FAT table sector
185188
sectionIdx -= START_FAT0;
186189
// logval("sidx", sectionIdx);
187190
if (sectionIdx >= SECTORS_PER_FAT)
188-
sectionIdx -= SECTORS_PER_FAT;
191+
sectionIdx -= SECTORS_PER_FAT; // second FAT is same as the first...
189192
if (sectionIdx == 0) {
190-
data[0] = 0xf0;
193+
data[0] = 0xf8; // first FAT entry must match BPB MediaDescriptor
191194
for (int i = 1; i < NUM_INFO * 2 + 4; ++i) {
192195
data[i] = 0xff;
193196
}
194197
}
195-
for (int i = 0; i < 256; ++i) {
198+
for (int i = 0; i < 256; ++i) { // Generate the FAT chain for the firmware "file"
196199
uint32_t v = sectionIdx * 256 + i;
197200
if (UF2_FIRST_SECTOR <= v && v <= UF2_LAST_SECTOR)
198201
((uint16_t *)(void *)data)[i] = v == UF2_LAST_SECTOR ? 0xffff : v + 1;
199202
}
200-
} else if (block_no < START_CLUSTERS) {
203+
} else if (block_no < START_CLUSTERS) { // Requested root directory sector
201204
sectionIdx -= START_ROOTDIR;
202-
if (sectionIdx == 0) {
205+
if (sectionIdx == 0) { // only one sector of directory entries generated
203206
DirEntry *d = (void *)data;
204207
padded_memcpy(d->name, (char const *) BootBlock.VolumeLabel, 11);
205208
d->attrs = 0x28;
@@ -209,9 +212,9 @@ void read_block(uint32_t block_no, uint8_t *data) {
209212
d->size = inf->content ? strlen(inf->content) : UF2_SIZE;
210213
d->startCluster = i + 2;
211214
padded_memcpy(d->name, inf->name, 11);
212-
}
213-
}
214-
} else {
215+
}
216+
}
217+
} else { // else Generate the UF2 file data on-the-fly
215218
sectionIdx -= START_CLUSTERS;
216219
if (sectionIdx < NUM_INFO - 1) {
217220
memcpy(data, info[sectionIdx].content, strlen(info[sectionIdx].content));

0 commit comments

Comments
 (0)