@@ -83,7 +83,7 @@ static struct TextFile const info[] = {
8383};
8484#define NUM_INFO (sizeof(info) / sizeof(info[0]))
8585
86- #define UF2_SIZE (get_flash_size () * 2)
86+ #define UF2_SIZE (current_flash_size () * 2)
8787#define UF2_SECTORS (UF2_SIZE / 512)
8888#define UF2_FIRST_SECTOR (NUM_INFO + 1)
8989#define UF2_LAST_SECTOR (UF2_FIRST_SECTOR + UF2_SECTORS - 1)
@@ -120,7 +120,9 @@ static FAT_BootBlock const BootBlock = {
120120#define NRF_LOG_WARNING (...)
121121
122122static WriteState _wr_state = { 0 };
123- static uint32_t get_flash_size (void )
123+
124+ // get current.uf2 flash size in bytes, round up to 256 bytes
125+ static uint32_t current_flash_size (void )
124126{
125127 static uint32_t flash_sz = 0 ;
126128
@@ -136,10 +138,17 @@ static uint32_t get_flash_size(void)
136138 bootloader_settings_t const * boot_setting ;
137139 bootloader_util_settings_get (& boot_setting );
138140
141+ flash_sz = boot_setting -> bank_0_size ;
142+
143+ // Copy size must be multiple of 256 bytes
144+ // else we will got an issue copying current.uf2
145+ if (flash_sz & 0xff )
146+ {
147+ flash_sz = (flash_sz & ~0xff ) + 256 ;
148+ }
149+
139150 // if bank0 size is not valid, happens when flashed with jlink
140151 // use maximum application size
141-
142- flash_sz = boot_setting -> bank_0_size ;
143152 if ( (flash_sz == 0 ) || (flash_sz == 0xFFFFFFFFUL ) )
144153 {
145154 flash_sz = FLASH_SIZE ;
@@ -217,7 +226,7 @@ void read_block(uint32_t block_no, uint8_t *data) {
217226 bl -> magicStart1 = UF2_MAGIC_START1 ;
218227 bl -> magicEnd = UF2_MAGIC_END ;
219228 bl -> blockNo = sectionIdx ;
220- bl -> numBlocks = FLASH_SIZE / 256 ;
229+ bl -> numBlocks = current_flash_size () / 256 ;
221230 bl -> targetAddr = addr ;
222231 bl -> payloadSize = 256 ;
223232 bl -> flags = UF2_FLAG_FAMILYID ;
0 commit comments