Skip to content

Commit 683465f

Browse files
committed
fixme: add bootloader reflasher for c33
This is needed to expose the second dfu endpoint for sketches
1 parent 009e7b5 commit 683465f

File tree

4 files changed

+60
-0
lines changed

4 files changed

+60
-0
lines changed

loader/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,9 @@ find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
88

99
project(app LANGUAGES C CXX)
1010

11+
include(${CMAKE_CURRENT_SOURCE_DIR}/blobs/CMakeLists.txt)
12+
1113
FILE(GLOB app_sources *.c)
1214
target_sources(app PRIVATE ${app_sources})
15+
16+
target_link_libraries(app PUBLIC blobs)

loader/blobs/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
zephyr_interface_library_named(blobs)
2+
3+
if (CONFIG_BOARD_ARDUINO_PORTENTA_C33)
4+
set(gen_inc_dir ${ZEPHYR_BINARY_DIR}/misc/generated)
5+
zephyr_include_directories(${gen_inc_dir})
6+
set(gen_dir ${gen_inc_dir}/c33_bl_patch)
7+
generate_inc_file_for_target(
8+
${ZEPHYR_CURRENT_LIBRARY}
9+
${CMAKE_CURRENT_SOURCE_DIR}/blobs/c33_bl.bin
10+
${gen_dir}/c33_bl.bin.inc
11+
)
12+
endif()

loader/blobs/c33_bl.bin

17.3 KB
Binary file not shown.

loader/fixups.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,48 @@ int smh_init(void) {
100100
}
101101

102102
SYS_INIT(smh_init, POST_KERNEL, CONFIG_CLOCK_CONTROL_PWM_INIT_PRIORITY);
103+
#endif
104+
105+
#if defined(CONFIG_BOARD_ARDUINO_PORTENTA_C33)
106+
#include <zephyr/kernel.h>
107+
#include <zephyr/storage/flash_map.h>
108+
109+
int maybe_flash_bootloader(void)
110+
{
111+
// memcmp the first 256bytes of "embedded bootloader" and address 0x0
112+
// if they are different, flash the bootloader
113+
const uint8_t embedded_bootloader[] = {
114+
#include "c33_bl_patch/c33_bl.bin.inc"
115+
};
116+
117+
const struct flash_area *fa;
118+
int rc;
119+
120+
rc = flash_area_open(FIXED_PARTITION_ID(mcuboot), &fa);
121+
if (rc) {
122+
printk("Failed to open flash area, rc %d\n", rc);
123+
return rc;
124+
}
125+
126+
uint8_t flash_bootloader[256];
127+
flash_area_read(fa, 0, flash_bootloader, 256);
128+
129+
if (memcmp(embedded_bootloader, flash_bootloader, 256) != 0) {
130+
// flash the bootloader
131+
rc = flash_area_erase(fa, 0, fa->fa_size);
132+
if (rc) {
133+
printk("Failed to erase flash area, rc %d\n", rc);
134+
return rc;
135+
}
136+
flash_area_write(fa, 0, embedded_bootloader, sizeof(embedded_bootloader));
137+
if (rc) {
138+
printk("Failed to write flash area, rc %d\n", rc);
139+
return rc;
140+
}
141+
}
142+
return 0;
143+
}
144+
145+
SYS_INIT(maybe_flash_bootloader, POST_KERNEL, CONFIG_FILE_SYSTEM_INIT_PRIORITY);
146+
103147
#endif

0 commit comments

Comments
 (0)