Skip to content

Commit 6094737

Browse files
committed
mark app as invalid when updating Softdevice only as application in uf2 mode
able to flash combine SD + cpy as single uf2. still has issue writing back CURRENT.UF2
1 parent 54a0ad7 commit 6094737

File tree

5 files changed

+24
-7
lines changed

5 files changed

+24
-7
lines changed

lib/sdk11/components/libraries/bootloader_dfu/bootloader.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ void bootloader_dfu_update_process(dfu_update_status_t update_status)
196196
{
197197
settings.bank_0_crc = update_status.app_crc;
198198
settings.bank_0_size = update_status.app_size;
199-
settings.bank_0 = BANK_VALID_APP;
199+
settings.bank_0 = (update_status.app_size ? BANK_VALID_APP : BANK_INVALID_APP);
200200
settings.bank_1 = BANK_INVALID_APP;
201201

202202
m_update_status = BOOTLOADER_SETTINGS_SAVING;

lib/sdk11/components/libraries/bootloader_dfu/dfu_types.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,8 @@ typedef enum
138138
DFU_UPDATE_BOOT_COMPLETE, /**< Status update complete.*/
139139
DFU_BANK_0_ERASED, /**< Status bank 0 erased.*/
140140
DFU_TIMEOUT, /**< Status timeout.*/
141-
DFU_RESET /**< Status Reset to indicate current update procedure has been aborted and system should reset. */
141+
DFU_RESET, /**< Status Reset to indicate current update procedure has been aborted and system should reset. */
142+
DFU_UF2_BOOTLOADER_COMPLETE
142143
} dfu_update_status_code_t;
143144

144145
/**@brief Structure holding DFU complete event.

src/usb/msc_uf2.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ void tud_msc_write10_complete_cb(uint8_t lun)
183183
}else
184184
{
185185
// update App
186-
update_status.status_code = DFU_UF2_APP_COMPLETE;
186+
update_status.status_code = DFU_UPDATE_APP_COMPLETE;
187187
update_status.app_crc = 0; // skip CRC checking with uf2 upgrade
188188
update_status.app_size = _wr_state.numBlocks*256;
189189

@@ -193,8 +193,16 @@ void tud_msc_write10_complete_cb(uint8_t lun)
193193
* to stay compatible with DFU CDC interface. We re-calculate it based on written
194194
* address and its contents ( SD_MAGIC matches )
195195
*/
196-
update_status.app_size -= SD_SIZE_GET(MBR_SIZE);
196+
update_status.app_size -= (SD_SIZE_GET(MBR_SIZE) - MBR_SIZE);
197197
}
198+
199+
if ( _wr_state.has_mbr )
200+
{
201+
// MBR write isn't counted app size as well
202+
update_status.app_size -= MBR_SIZE;
203+
}
204+
205+
PRINT_INT(update_status.app_size);
198206
}
199207

200208
bootloader_dfu_update_process(update_status);

src/usb/uf2/ghostfat.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ static uint32_t current_flash_size(void)
212212
void uf2_init(void)
213213
{
214214
uf2current_flash_sz = current_flash_size();
215+
PRINT_INT(uf2current_flash_sz);
215216
}
216217

217218
/*------------------------------------------------------------------*/
@@ -348,7 +349,7 @@ int write_block (uint32_t block_no, uint8_t *data, WriteState *state)
348349
/* Upgrading Application
349350
*
350351
* Although SoftDevice is considered as part of application and the flashing is the same with/without it.
351-
* There are still 3 cases with slight differences in finishing procedure:
352+
* There are still 4 cases with slight differences in finishing procedure:
352353
* 1. Application with SoftDevice:
353354
* - starting address 0x0000
354355
* - since MBR is included in SD Hex file (then uf2 file), we must skip it
@@ -357,6 +358,8 @@ int write_block (uint32_t block_no, uint8_t *data, WriteState *state)
357358
* - starting address is right after SD e.g 0x26000
358359
* b. For running without SoftDevice e.g using other stack such as nimble or zephyr.
359360
* - starting address is right after MBR 0x1000
361+
* 3. SoftDevice only, should user somehow only flash with SD only. Bootloader mark app as invalid and wiil
362+
* back on bootloader mode after reset.
360363
*
361364
* ------------- -------------
362365
* | | | |
@@ -377,13 +380,17 @@ int write_block (uint32_t block_no, uint8_t *data, WriteState *state)
377380
PRINTF("Write addr = 0x%08lX, block = %ld (%ld of %ld)\r\n", bl->targetAddr, bl->blockNo, state->numWritten, bl->numBlocks);
378381

379382
// writing to SD Info struct is used as SD detector
380-
if (bl->targetAddr == (SOFTDEVICE_INFO_STRUCT_ADDRESS & 0xff) ) state->has_sd = true;
383+
if (bl->targetAddr == (SOFTDEVICE_INFO_STRUCT_ADDRESS & 0xFFFFFF00) )
384+
{
385+
state->has_sd = true;
386+
}
381387

382388
flash_nrf5x_write(bl->targetAddr, bl->data, bl->payloadSize, true);
383389
}else if ( bl->targetAddr < USER_FLASH_START )
384390
{
385391
// do nothing if writing to MBR
386392
// keep going as successful write
393+
state->has_mbr = true;
387394
}else
388395
{
389396
return -1;

src/usb/uf2/uf2.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ SOFTWARE.
5252
typedef struct {
5353
uint32_t numBlocks;
5454
uint32_t numWritten;
55-
bool has_sd; // if SD is included as part of uf2 file
55+
bool has_sd; // if uf2 includes SD
56+
bool has_mbr; // if uf2 includes MBR
5657
bool update_bootloader; // if updating bootloader
5758
uint8_t writtenMask[MAX_BLOCKS / 8 + 1];
5859
} WriteState;

0 commit comments

Comments
 (0)