Skip to content

Commit 7929dc3

Browse files
committed
add check for boatloader id in cf2 config
1 parent 7c8e1e5 commit 7929dc3

File tree

4 files changed

+63
-1
lines changed

4 files changed

+63
-1
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include "boards.h"
2+
#include "uf2/configkeys.h"
3+
4+
__attribute__((used, section(".bootloaderConfig")))
5+
const uint32_t bootloaderConfig[] =
6+
{
7+
/* CF2 START */
8+
CFG_MAGIC0, CFG_MAGIC1, // magic
9+
32, 100, // used entries, total entries
10+
11+
#ifdef LED_NEOPIXEL
12+
20, LED_NEOPIXEL, // PIN_NEOPIXEL
13+
200, NEOPIXELS_NUMBER, // NUM_NEOPIXELS
14+
#endif
15+
16+
204, 0x100000, // FLASH_BYTES = 0x100000
17+
205, 0x40000, // RAM_BYTES = 0x40000
18+
CFG_BOOTLOADER_BOARD_ID, (USB_DESC_VID << 16) | USB_DESC_UF2_PID,
19+
209, 0xada52840, // UF2_FAMILY = 0xada52840
20+
210, 0x20, // PINS_PORT_SIZE = PA_32
21+
22+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
23+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
24+
/* CF2 END */
25+
};

src/flash_nrf5x.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ void flash_nrf5x_flush (bool need_erase)
4747
// Note: MSC uf2 does not erase page in advance like dfu serial
4848
if ( need_erase )
4949
{
50-
PRINTF("Erase 0x%08lX\r\n", _fl_addr);
50+
PRINTF("Erase and ");
5151
nrfx_nvmc_page_erase(_fl_addr);
5252
}
5353

src/usb/uf2/ghostfat.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "compile_date.h"
22

33
#include "uf2.h"
4+
#include "configkeys.h"
45
#include "flash_nrf5x.h"
56
#include <string.h>
67

@@ -140,6 +141,10 @@ static FAT_BootBlock const BootBlock = {
140141
.FilesystemIdentifier = "FAT16 ",
141142
};
142143

144+
// Use bootloaderConfig to detect BOOTLOADER ID when updating bootloader
145+
// This helps to prevent incorrect uf2 from other boards.
146+
extern const uint32_t bootloaderConfig[];
147+
143148
//--------------------------------------------------------------------+
144149
//
145150
//--------------------------------------------------------------------+
@@ -406,6 +411,37 @@ int write_block (uint32_t block_no, uint8_t *data, WriteState *state)
406411
}
407412
else if ( in_bootloader_space(bl->targetAddr) )
408413
{
414+
// Bootloader CF2 config
415+
if ( !state->boot_id_matches && (bl->targetAddr >= ((uint32_t) bootloaderConfig)) )
416+
{
417+
// check if bootloader ID matches current VID/PID
418+
for (uint32_t i=0; i < bl->payloadSize; i += 8)
419+
{
420+
uint32_t key;
421+
memcpy(&key, bl->data+i, 4);
422+
423+
if ( key == CFG_BOOTLOADER_BOARD_ID )
424+
{
425+
uint32_t value;
426+
memcpy(&value, bl->data+i+4, 4);
427+
428+
PRINTF("Bootloader ID = 0x%08lX and ", value);
429+
if ( value == ((USB_DESC_VID << 16) | USB_DESC_UF2_PID) )
430+
{
431+
PRINTF("matches our VID/PID\r\n");
432+
state->boot_id_matches = true;
433+
break;
434+
}
435+
else
436+
{
437+
PRINTF("DOES NOT mismatches our VID/PID\r\n");
438+
state->aborted = true;
439+
return -1;
440+
}
441+
}
442+
}
443+
}
444+
409445
// Offset to write the new bootloader address (skipping the App Data)
410446
uint32_t const offset_addr = BOOTLOADER_ADDR_END-USER_FLASH_END;
411447
flash_nrf5x_write(bl->targetAddr-offset_addr, bl->data, bl->payloadSize, true);

src/usb/uf2/uf2.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ typedef struct {
5656
bool aborted; // aborting update and reset
5757
bool update_bootloader; // if updating bootloader (else app)
5858
bool has_uicr; // if containing uicr data
59+
bool boot_id_matches; // if bootloader id in cf2 config matches our VID/PID
5960

6061
uint8_t writtenMask[MAX_BLOCKS / 8 + 1];
6162
} WriteState;

0 commit comments

Comments
 (0)