@@ -160,19 +160,7 @@ static inline bool in_app_space (uint32_t addr)
160
160
// used when upgrading bootloader
161
161
static inline bool in_bootloader_space (uint32_t addr )
162
162
{
163
- return BOOTLOADER_LOWEST_ADDR <= addr && addr < BOOTLOADER_ADDR_END ;
164
- }
165
-
166
- static inline uint32_t get_new_bootloader_size (uint32_t numblocks )
167
- {
168
- // -1 for UCIR block
169
- uint32_t new_size = (numblocks - 1 )* 256 ;
170
-
171
- // round up to 4K
172
- uint32_t mod4k = new_size & 0xFFFUL ;
173
- if ( mod4k ) new_size += 4096 - mod4k ;
174
-
175
- return new_size ;
163
+ return BOOTLOADER_ADDR_START <= addr && addr < BOOTLOADER_ADDR_END ;
176
164
}
177
165
178
166
// used when upgrading bootloader
@@ -324,13 +312,13 @@ int write_block (uint32_t block_no, uint8_t *data, WriteState *state)
324
312
*
325
313
* ------------- -------------
326
314
* | | | |
327
- * | Bootloader | | Bootloader |
315
+ * | Bootloader | | Bootloader |
328
316
* BOOTLOADER_ADDR_START--|-------------| |-------------|
317
+ * | App Data | | App Data |
318
+ * USER_FLASH_END ---|-------------| |-------------|
329
319
* | | | |
330
- * | | | |
331
- * | Application | ---> | New |
332
- * | | | Application |
333
- * | | | |
320
+ * | | | New |
321
+ * | Application | ----> | Application |
334
322
* | | | |
335
323
* USER_FLASH_START--|-------------| |-------------|
336
324
* | MBR | | MBR |
@@ -354,18 +342,16 @@ int write_block (uint32_t block_no, uint8_t *data, WriteState *state)
354
342
case CFG_UF2_BOOTLOADER_ID :
355
343
/* Upgrading Bootloader
356
344
*
357
- * Along with bootloader code, UCIR (at 0x1000100) is also included containing
358
- * 0x10001014 (bootloader address), and 0x10001018 (MBR Params address).
345
+ * - For simplicity, the Bootloader Start Address is fixed for now.
346
+ *
347
+ * - Since SoftDevice is not part of Bootloader, it MUST NOT be included as part of uf2 file.
359
348
*
360
- * Since SoftDevice is not part of Bootloader, it must not be included as part of uf2 file.
361
- * To prevent generating uf2 from incorrect bin/hex. Bootloader imposes a hard code limit size
362
- * of 64 KB (end address is fixed at MBR Params). If uf2 contains any address lower than this
363
- * (except UCIR) the update is aborted.
349
+ * - To prevent corruption/disconnection while transferring we don't directly write over Bootloader.
350
+ * Instead it is written to highest possible address in Application region. Once everything is received
351
+ * and verified, it is safely activated using MBR COPY BL command.
364
352
*
365
- * To prevent corruption/disconnection while transferring we don't directly write over
366
- * Bootloader. Instead it is written to highest possible address in Application
367
- * region. Once everything is received and verified, it is safely activated using
368
- * MBR COPY BL command.
353
+ * - Along with bootloader code, UCIR (at 0x1000100) is also included containing
354
+ * 0x10001014 (bootloader address), and 0x10001018 (MBR Params address).
369
355
*
370
356
* Note: part of the existing application can be affected when updating bootloader.
371
357
* TODO May be worth to have some kind crc/application integrity checking
@@ -374,11 +360,11 @@ int write_block (uint32_t block_no, uint8_t *data, WriteState *state)
374
360
* | | | | + | New |
375
361
* | Bootloader | | Bootloader | + | Bootloader |
376
362
* BOOTLOADER_ADDR_START--|-------------| |-------------| + |-------------|
377
- * | | | New | + | |
378
- * | | | Bootloader | + | |
379
- * | | | ++++++++ | | |
380
- * | | ---> | | | |
381
- * | | | | | |
363
+ * | App Data | | App Data | + | App Data |
364
+ * USER_FLASH_END --|------------- | | ---------- | + |------------ |
365
+ * | | | New |+ | |
366
+ * | | ---> | Bootloader | | |
367
+ * | | | ++++++ | | |
382
368
* | Application | | Application | | Application |
383
369
* | | | | | |
384
370
* | | | | | |
@@ -388,54 +374,53 @@ int write_block (uint32_t block_no, uint8_t *data, WriteState *state)
388
374
*/
389
375
PRINTF ("addr = 0x%08lX, block = %ld (%ld of %ld)\r\n" , bl -> targetAddr , bl -> blockNo , state -> numWritten , bl -> numBlocks );
390
376
391
- uint32_t const new_boot_size = get_new_bootloader_size (bl -> numBlocks );
392
-
393
- // Offset write the new bootloader address.
394
- // The offset is the current bootloader size or the new size whichever is larger
395
- uint32_t const offset_addr = max32 (new_boot_size , BOOTLOADER_ADDR_END - BOOTLOADER_ADDR_START );
396
-
377
+ state -> update_bootloader = true;
397
378
if ( in_uicr_space (bl -> targetAddr ) )
398
379
{
399
380
/* UCIR contains bootloader & MBR address as follow:
400
- * - 0x10001014 bootloader address: only change if bootloader size changes
381
+ * - 0x10001014 bootloader address
401
382
* - 0x10001018 MBR Params: mostly fixed
402
383
*
403
- * WARNING: incorrect value of these UCIR will brick device
384
+ * Since the bootloader start address is fixed, we only use this for verification
404
385
*/
405
- uint32_t ucir_boot_addr ;
406
- uint32_t ucir_mbr_param ;
386
+ uint32_t uicr_boot_addr ;
387
+ uint32_t uicr_mbr_param ;
407
388
408
- memcpy (& ucir_boot_addr , bl -> data + 0x14 , 4 );
409
- memcpy (& ucir_mbr_param , bl -> data + 0x18 , 4 );
410
-
411
- PRINT_HEX (ucir_boot_addr );
412
- PRINT_HEX (ucir_mbr_param );
413
-
414
- PRINT_HEX (new_boot_size );
389
+ memcpy (& uicr_boot_addr , bl -> data + 0x14 , 4 );
390
+ memcpy (& uicr_mbr_param , bl -> data + 0x18 , 4 );
415
391
416
392
// Check MBR params is fixed and prohibited to change and
417
393
// Bootloader address against its new size
418
- if ( (ucir_mbr_param != BOOTLOADER_MBR_PARAMS_PAGE_ADDRESS ) ||
419
- (ucir_boot_addr != BOOTLOADER_ADDR_END - new_boot_size ) )
394
+ if ( (uicr_boot_addr != BOOTLOADER_ADDR_START ) ||
395
+ (uicr_mbr_param != BOOTLOADER_MBR_PARAMS_PAGE_ADDRESS ) )
420
396
{
421
- PRINTF ("Incorrect UCIR value" );
397
+ PRINTF ("Incorrect UICR value" );
398
+ PRINT_HEX (uicr_boot_addr );
399
+ PRINT_HEX (uicr_mbr_param );
400
+
422
401
state -> aborted = true;
423
402
return -1 ;
424
- }else
425
- {
426
- // Good to go, save the boot address
427
- state -> boot_addr_ucir = ucir_boot_addr ;
428
- state -> boot_size = new_boot_size ;
429
- state -> boot_stored_addr = BOOTLOADER_ADDR_END - (new_boot_size + offset_addr );
430
-
431
- PRINT_HEX (state -> boot_stored_addr );
432
403
}
404
+
405
+ state -> has_uicr = true;
433
406
}
434
- else if ( in_bootloader_space (bl -> targetAddr ) && ( bl -> targetAddr >= BOOTLOADER_LOWEST_ADDR ) )
407
+ else if ( in_bootloader_space (bl -> targetAddr ) )
435
408
{
436
- // PRINT_HEX(offset_addr);
409
+ // Offset to write the new bootloader address (skipping the App Data)
410
+ uint32_t const offset_addr = BOOTLOADER_ADDR_END - USER_FLASH_END ;
437
411
flash_nrf5x_write (bl -> targetAddr - offset_addr , bl -> data , bl -> payloadSize , true);
438
412
}
413
+ #if 0 // don't allow bundle SoftDevice to prevent confusion
414
+ else if ( in_app_space (bl -> targetAddr ) )
415
+ {
416
+ // Should be Softdevice
417
+ flash_nrf5x_write (bl -> targetAddr , bl -> data , bl -> payloadSize , true);
418
+ }
419
+ else if ( bl -> targetAddr < USER_FLASH_START )
420
+ {
421
+ PRINTF ("skip writing to MBR\r\n" );
422
+ }
423
+ #endif
439
424
else
440
425
{
441
426
state -> aborted = true;
@@ -478,7 +463,7 @@ int write_block (uint32_t block_no, uint8_t *data, WriteState *state)
478
463
flash_nrf5x_flush (true);
479
464
480
465
// Failed if update bootloader without UCIR value
481
- if ( ( bl -> familyID == CFG_UF2_BOOTLOADER_ID ) && !state -> boot_addr_ucir )
466
+ if ( state -> update_bootloader && !state -> has_uicr )
482
467
{
483
468
state -> aborted = true;
484
469
}
0 commit comments