Skip to content

Commit 3d1157a

Browse files
Loader: improve header struct definition
1 parent e82f2e3 commit 3d1157a

File tree

1 file changed

+23
-20
lines changed

1 file changed

+23
-20
lines changed

loader/main.c

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,19 @@ LOG_MODULE_REGISTER(sketch);
2424
#include <zephyr/drivers/uart.h>
2525
#include <zephyr/usb/usb_device.h>
2626

27-
#define HEADER_LEN 16
28-
29-
struct sketch_header_v1 {
30-
uint8_t ver; // @ 0x07
31-
uint32_t len; // @ 0x08
32-
uint16_t magic; // @ 0x0c
33-
uint8_t flags; // @ 0x0e
34-
} __attribute__((packed));
27+
typedef union {
28+
struct {
29+
uint8_t _padding[7];
30+
uint8_t ver; // @ 0x07
31+
uint32_t len; // @ 0x08
32+
uint16_t magic; // @ 0x0c
33+
uint8_t flags; // @ 0x0e
34+
// uint8_t _padding; last unused byte in header
35+
}__attribute__((packed));
36+
uint8_t buf[16];
37+
} sketch_header_v1;
38+
39+
#define HEADER_LEN sizeof(sketch_header_v1)
3540

3641
#define SKETCH_FLAG_DEBUG 0x01
3742
#define SKETCH_FLAG_LINKED 0x02
@@ -106,22 +111,20 @@ static int loader(const struct shell *sh) {
106111
uintptr_t base_addr =
107112
DT_REG_ADDR(DT_GPARENT(DT_NODELABEL(user_sketch))) + DT_REG_ADDR(DT_NODELABEL(user_sketch));
108113

109-
char header[HEADER_LEN];
110-
rc = flash_area_read(fa, 0, header, sizeof(header));
114+
sketch_header_v1 sketch_hdr;
115+
rc = flash_area_read(fa, 0, sketch_hdr.buf, sizeof(sketch_hdr.buf));
111116
if (rc) {
112117
printk("Failed to read header, rc %d\n", rc);
113118
return rc;
114119
}
115120

116121
bool sketch_valid = true;
117122

118-
struct sketch_header_v1 *sketch_hdr = (struct sketch_header_v1 *)(header + 7);
119-
120123
// Ensure endianness is preserved in fields of header with more than 1 byte
121-
sketch_hdr->len = sys_le32_to_cpu(sketch_hdr.len);
122-
sketch_hdr->magic = sys_le16_to_cpu(sketch_hdr.magic);
124+
sketch_hdr.len = sys_le32_to_cpu(sketch_hdr.len);
125+
sketch_hdr.magic = sys_le16_to_cpu(sketch_hdr.magic);
123126

124-
if (sketch_hdr->ver != 0x1 || sketch_hdr->magic != 0x2341) {
127+
if (sketch_hdr.ver != 0x1 || sketch_hdr.magic != 0x2341) {
125128
printk("Invalid sketch header\n");
126129
sketch_valid = false;
127130
// This is not a valid sketch, but try to start a shell anyway
@@ -160,7 +163,7 @@ static int loader(const struct shell *sh) {
160163
_bootanimation_end = _bootanimation + user_bootanimation->len_loop;
161164
}
162165

163-
if ((!sketch_valid) || !(sketch_hdr->flags & SKETCH_FLAG_IMMEDIATE)) {
166+
if ((!sketch_valid) || !(sketch_hdr.flags & SKETCH_FLAG_IMMEDIATE)) {
164167
// Start the bootanimation while waiting for the MPU to boot
165168
const struct gpio_dt_spec spec =
166169
GPIO_DT_SPEC_GET_BY_IDX(DT_PATH(zephyr_user), control_gpios, 0);
@@ -194,7 +197,7 @@ static int loader(const struct shell *sh) {
194197
__asm__("bkpt");
195198
// poll the first bytes, if filled try to use them for booting
196199
sketch_hdr = (struct sketch_header_v1 *)(ram_firmware + 7);
197-
if (sketch_hdr->ver == 0x1 && sketch_hdr->magic == 0x2341) {
200+
if (sketch_hdr.ver == 0x1 && sketch_hdr.magic == 0x2341) {
198201
// Found valid data, use it for booting
199202
base_addr = (uintptr_t)ram_firmware;
200203
*ram_start = 0;
@@ -204,10 +207,10 @@ static int loader(const struct shell *sh) {
204207
}
205208
#endif
206209

207-
size_t sketch_buf_len = sketch_hdr->len;
210+
size_t sketch_buf_len = sketch_hdr.len;
208211

209212
#if TARGET_HAS_USB_CDC_SHELL
210-
int debug = (!sketch_valid) || (sketch_hdr->flags & SKETCH_FLAG_DEBUG);
213+
int debug = (!sketch_valid) || (sketch_hdr.flags & SKETCH_FLAG_DEBUG);
211214
if (debug && strcmp(k_thread_name_get(k_current_get()), "main") == 0) {
212215
// disables default shell on UART
213216
shell_uninit(shell_backend_uart_get_ptr(), NULL);
@@ -224,7 +227,7 @@ static int loader(const struct shell *sh) {
224227
}
225228
#endif
226229

227-
if (sketch_hdr->flags & SKETCH_FLAG_LINKED) {
230+
if (sketch_hdr.flags & SKETCH_FLAG_LINKED) {
228231
#ifdef CONFIG_BOARD_ARDUINO_PORTENTA_C33
229232
#if CONFIG_MPU
230233
barrier_dmem_fence_full();

0 commit comments

Comments
 (0)