Skip to content

Commit ba96d6e

Browse files
committed
fix: size CURRENT.UF2 from the app start, not from every block written
bank_0_size is measured from DFU_BANK_0_REGION_START by the CRC check and by serial/OTA DFU, but msc_uf2.c recorded every UF2 block written from 0x1000 (so a CURRENT.UF2 restore, which carries the SoftDevice blocks, overcounted by the SD span) and ghostfat.c used the value as a window from USER_FLASH_START (so after a serial DFU the dump was short by the same span - 1.2M instead of the real 1.5M+, and a restore of that dump only works while the tail is still in flash). Now ghostfat adds the SoftDevice span to bank_0_size, and msc_uf2 records the highest app address written minus the app start. Verified on RAK4631 with 2.8.0.abd3348 (729,528 B app): after a serial DFU the dump is 1,762,304 B (SD span + app rounded to 256), and after restoring that dump it is byte-identical and the same size.
1 parent b7b7cf4 commit ba96d6e

3 files changed

Lines changed: 17 additions & 5 deletions

File tree

src/usb/msc_uf2.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -223,12 +223,15 @@ void tud_msc_write10_complete_cb(uint8_t lun)
223223
// update App
224224
update_status.status_code = DFU_UPDATE_APP_COMPLETE;
225225

226-
// Record the real written size so bootloader_settings.bank_0_size
226+
// Record the real app span so bootloader_settings.bank_0_size
227227
// reflects the actual app, not 0 -- ghostfat.c's CURRENT.UF2 uses
228-
// this to size its dump to the real app instead of the max region.
229-
// 256 mirrors ghostfat.c's UF2_FIRMWARE_BYTES_PER_SECTOR (the UF2
230-
// format's fixed payload-per-block size, not board-specific).
231-
update_status.app_size = _wr_state.numWritten * 256;
228+
// this to size its dump. Measured from DFU_BANK_0_REGION_START like
229+
// serial/OTA DFU do, so a CURRENT.UF2 restore (which also carries
230+
// the SoftDevice blocks) does not overcount.
231+
if ( _wr_state.appEnd > DFU_BANK_0_REGION_START )
232+
{
233+
update_status.app_size = _wr_state.appEnd - DFU_BANK_0_REGION_START;
234+
}
232235

233236
PRINTF("Application update complete\r\n");
234237
}

src/usb/uf2/ghostfat.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,14 @@ static uint32_t current_flash_size(void)
199199
bootloader_settings_t const * boot_setting;
200200
bootloader_util_settings_get(&boot_setting);
201201

202+
// bank_0_size counts from DFU_BANK_0_REGION_START (the CRC check and
203+
// serial/OTA DFU both define it that way); the dump starts at
204+
// USER_FLASH_START, so add the SoftDevice span in between.
202205
flash_sz = boot_setting->bank_0_size;
206+
if ( flash_sz && (flash_sz != 0xFFFFFFFFUL) )
207+
{
208+
flash_sz += DFU_BANK_0_REGION_START - USER_FLASH_START;
209+
}
203210

204211
// Round up to a whole UF2 payload chunk, else the last real chunk of
205212
// the app would get truncated out of CURRENT.UF2.
@@ -484,6 +491,7 @@ int write_block (uint32_t block_no, uint8_t *data, WriteState *state)
484491
{
485492
PRINTF("Write addr = 0x%08lX, block = %ld (%ld of %ld)\r\n", bl->targetAddr, bl->blockNo, state->numWritten, bl->numBlocks);
486493
flash_nrf5x_write(bl->targetAddr, bl->data, bl->payloadSize, true);
494+
if ( bl->targetAddr + bl->payloadSize > state->appEnd ) state->appEnd = bl->targetAddr + bl->payloadSize;
487495
}else if ( bl->targetAddr < USER_FLASH_START )
488496
{
489497
// do nothing if writing to MBR, occurs when SD hex is included

src/usb/uf2/uf2.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ SOFTWARE.
5252
typedef struct {
5353
uint32_t numBlocks;
5454
uint32_t numWritten;
55+
uint32_t appEnd; // highest end address written in app space (0 if none)
5556

5657
bool aborted; // aborting update and reset
5758
bool update_bootloader; // if updating bootloader (else app)

0 commit comments

Comments
 (0)