Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit75baff8

Browse files
Loader: improve header struct definition
1 parent85b9c14 commit75baff8

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-
#defineHEADER_LEN 16
28-
29-
structsketch_header_v1 {
30-
uint8_tver;// @ 0x07
31-
uint32_tlen;// @ 0x08
32-
uint16_tmagic;// @ 0x0c
33-
uint8_tflags;// @ 0x0e
34-
} __attribute__((packed));
27+
typedefunion {
28+
struct {
29+
uint8_t_padding[7];
30+
uint8_tver;// @ 0x07
31+
uint32_tlen;// @ 0x08
32+
uint16_tmagic;// @ 0x0c
33+
uint8_tflags;// @ 0x0e
34+
// uint8_t _padding; last unused byte in header
35+
} __attribute__((packed));
36+
uint8_tbytes[16];
37+
}sketch_header_v1;
38+
39+
#defineHEADER_LEN sizeof(sketch_header_v1)
3540

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

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

116121
boolsketch_valid= true;
117122

118-
structsketch_header_v1*sketch_hdr= (structsketch_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
conststructgpio_dt_specspec=
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= (structsketch_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_tsketch_buf_len=sketch_hdr->len;
210+
size_tsketch_buf_len=sketch_hdr.len;
208211

209212
#ifTARGET_HAS_USB_CDC_SHELL
210-
intdebug= (!sketch_valid)|| (sketch_hdr->flags&SKETCH_FLAG_DEBUG);
213+
intdebug= (!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
#ifdefCONFIG_BOARD_ARDUINO_PORTENTA_C33
229232
#ifCONFIG_MPU
230233
barrier_dmem_fence_full();

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp