Skip to content

Commit 6d58633

Browse files
committed
Merge branch 'master' of https://github.com/adafruit/Adafruit_nRF52_Bootloader into patch-6
# Conflicts: # src/usb/uf2/ghostfat.c
2 parents 174e6e9 + 73456ac commit 6d58633

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

src/usb/msc_uf2.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ 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;
67+
memset(buffer, 0, bufsize);
6768

6869
switch ( scsi_cmd[0] )
6970
{
@@ -98,7 +99,7 @@ int32_t tud_msc_scsi_cb (uint8_t lun, uint8_t const scsi_cmd[16], void* buffer,
9899
}
99100

100101
// return len must not larger than bufsize
101-
if ( resplen > bufsize ) resplen = bufsize;
102+
if ( resplen > (int32_t)bufsize ) resplen = bufsize;
102103

103104
// copy response to stack's buffer if any
104105
if ( response && resplen )
@@ -114,6 +115,7 @@ int32_t tud_msc_scsi_cb (uint8_t lun, uint8_t const scsi_cmd[16], void* buffer,
114115
int32_t tud_msc_read10_cb (uint8_t lun, uint32_t lba, uint32_t offset, void* buffer, uint32_t bufsize)
115116
{
116117
(void) lun;
118+
memset(buffer, 0, bufsize);
117119

118120
// since we return block size each, offset should always be zero
119121
TU_ASSERT(offset == 0, -1);

src/usb/uf2/ghostfat.c

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ static FAT_BootBlock const BootBlock = {
177177
.SectorsPerFAT = SECTORS_PER_FAT,
178178
.SectorsPerTrack = 1,
179179
.Heads = 1,
180+
.PhysicalDriveNum = 0x80, // to match MediaDescriptor of 0xF8
180181
.ExtendedBootSig = 0x29,
181182
.VolumeSerialNumber = 0x00420042,
182183
.VolumeLabel = VOLUME_LABEL,
@@ -190,35 +191,37 @@ static FAT_BootBlock const BootBlock = {
190191
static uint32_t current_flash_size(void)
191192
{
192193
static uint32_t flash_sz = 0;
194+
uint32_t result = flash_sz; // presumes atomic 32-bit read/write and static result
193195

194196
// only need to compute once
195-
if ( flash_sz == 0 )
197+
if ( result == 0 )
196198
{
197199
// return 1 block of 256 bytes
198200
if ( !bootloader_app_is_valid(DFU_BANK_0_REGION_START) )
199201
{
200-
flash_sz = 256;
202+
result = 256;
201203
}else
202204
{
203205
bootloader_settings_t const * boot_setting;
204206
bootloader_util_settings_get(&boot_setting);
205207

206-
flash_sz = boot_setting->bank_0_size;
208+
result = boot_setting->bank_0_size;
207209

208210
// Copy size must be multiple of 256 bytes
209211
// else we will got an issue copying current.uf2
210-
if (flash_sz & 0xff)
212+
if (result & 0xff)
211213
{
212-
flash_sz = (flash_sz & ~0xff) + 256;
214+
result = (result & ~0xff) + 256;
213215
}
214216

215217
// if bank0 size is not valid, happens when flashed with jlink
216218
// use maximum application size
217-
if ( (flash_sz == 0) || (flash_sz == 0xFFFFFFFFUL) )
219+
if ( (result == 0) || (result == 0xFFFFFFFFUL) )
218220
{
219-
flash_sz = FLASH_SIZE;
221+
result = FLASH_SIZE;
220222
}
221223
}
224+
flash_sz = result; // presumes atomic 32-bit read/write and static result
222225
}
223226

224227
return flash_sz;
@@ -243,16 +246,16 @@ void read_block(uint32_t block_no, uint8_t *data) {
243246
memset(data, 0, 512);
244247
uint32_t sectionIdx = block_no;
245248

246-
if (block_no == 0) {
249+
if (block_no == 0) { // Requested boot block
247250
memcpy(data, &BootBlock, sizeof(BootBlock));
248251
data[510] = 0x55;
249252
data[511] = 0xaa;
250253
// logval("data[0]", data[0]);
251-
} else if (block_no < START_ROOTDIR) {
254+
} else if (block_no < START_ROOTDIR) { // Requested FAT table sector
252255
sectionIdx -= START_FAT0;
253256
// logval("sidx", sectionIdx);
254257
if (sectionIdx >= SECTORS_PER_FAT)
255-
sectionIdx -= SECTORS_PER_FAT;
258+
sectionIdx -= SECTORS_PER_FAT; // second FAT is same as the first...
256259
if (sectionIdx == 0) {
257260
data[0] = 0xf0;
258261
// WARNING -- code presumes only one NULL .content for .UF2 file
@@ -262,7 +265,7 @@ void read_block(uint32_t block_no, uint8_t *data) {
262265
data[i] = 0xff;
263266
}
264267
}
265-
for (int i = 0; i < 256; ++i) {
268+
for (int i = 0; i < 256; ++i) { // Generate the FAT chain for the firmware "file"
266269
uint32_t v = sectionIdx * 256 + i;
267270
if (UF2_FIRST_SECTOR <= v && v <= UF2_LAST_SECTOR)
268271
((uint16_t *)(void *)data)[i] = v == UF2_LAST_SECTOR ? 0xffff : v + 1;

0 commit comments

Comments
 (0)