Sfoglia il codice sorgente

Merge pull request #15 from thingsconnected/pullreq4

Replace constant value for IMAGE_HEADER_OFFSET by linker supplied value
Brian Starkey 1 anno fa
parent
commit
a9d04021b0
Nessun account collegato all'indirizzo email del committer
4 ha cambiato i file con 9 aggiunte e 515 eliminazioni
  1. 0
    258
      bootloader.ld
  2. 1
    1
      bootloader_shell.ld
  3. 0
    248
      combined.ld
  4. 8
    8
      main.c

+ 0
- 258
bootloader.ld Vedi File

@@ -1,258 +0,0 @@
1
-/* Based on GCC ARM embedded samples.
2
-   Defines the following symbols for use by code:
3
-    __exidx_start
4
-    __exidx_end
5
-    __etext
6
-    __data_start__
7
-    __preinit_array_start
8
-    __preinit_array_end
9
-    __init_array_start
10
-    __init_array_end
11
-    __fini_array_start
12
-    __fini_array_end
13
-    __data_end__
14
-    __bss_start__
15
-    __bss_end__
16
-    __end__
17
-    end
18
-    __HeapLimit
19
-    __StackLimit
20
-    __StackTop
21
-    __stack (== StackTop)
22
-*/
23
-
24
-/*
25
- * TODO: Hard-code 360k, which is the size I see on my machine with
26
- * 'sta' mode and debug printf.
27
- * There must be a better way to determine the size dynamically somehow.
28
- */
29
-MEMORY
30
-{
31
-    FLASH(rx) : ORIGIN = 0x10000000, LENGTH = 360k
32
-    RAM(rwx) : ORIGIN =  0x20000000, LENGTH = 256k
33
-    SCRATCH_X(rwx) : ORIGIN = 0x20040000, LENGTH = 4k
34
-    SCRATCH_Y(rwx) : ORIGIN = 0x20041000, LENGTH = 4k
35
-}
36
-
37
-ENTRY(_entry_point)
38
-
39
-SECTIONS
40
-{
41
-    /* Second stage bootloader is prepended to the image. It must be 256 bytes big
42
-       and checksummed. It is usually built by the boot_stage2 target
43
-       in the Raspberry Pi Pico SDK
44
-    */
45
-
46
-    .flash_begin : {
47
-        __flash_binary_start = .;
48
-    } > FLASH
49
-
50
-    .boot2 : {
51
-        __boot2_start__ = .;
52
-        KEEP (*(.boot2))
53
-        __boot2_end__ = .;
54
-    } > FLASH
55
-
56
-    ASSERT(__boot2_end__ - __boot2_start__ == 256,
57
-        "ERROR: Pico second stage bootloader must be 256 bytes in size")
58
-
59
-    /* The second stage will always enter the image at the start of .text.
60
-       The debugger will use the ELF entry point, which is the _entry_point
61
-       symbol if present, otherwise defaults to start of .text.
62
-       This can be used to transfer control back to the bootrom on debugger
63
-       launches only, to perform proper flash setup.
64
-    */
65
-
66
-    .flashtext : {
67
-        __logical_binary_start = .;
68
-        KEEP (*(.vectors))
69
-        KEEP (*(.binary_info_header))
70
-        __binary_info_header_end = .;
71
-        KEEP (*(.reset))
72
-    }
73
-
74
-    .rodata : {
75
-        /* segments not marked as .flashdata are instead pulled into .data (in RAM) to avoid accidental flash accesses */
76
-        *(SORT_BY_ALIGNMENT(SORT_BY_NAME(.flashdata*)))
77
-        . = ALIGN(4);
78
-    } > FLASH
79
-
80
-    .ARM.extab :
81
-    {
82
-        *(.ARM.extab* .gnu.linkonce.armextab.*)
83
-    } > FLASH
84
-
85
-    __exidx_start = .;
86
-    .ARM.exidx :
87
-    {
88
-        *(.ARM.exidx* .gnu.linkonce.armexidx.*)
89
-    } > FLASH
90
-    __exidx_end = .;
91
-
92
-    /* Machine inspectable binary information */
93
-    . = ALIGN(4);
94
-    __binary_info_start = .;
95
-    .binary_info :
96
-    {
97
-        KEEP(*(.binary_info.keep.*))
98
-        *(.binary_info.*)
99
-    } > FLASH
100
-    __binary_info_end = .;
101
-    . = ALIGN(4);
102
-
103
-    /* Vector table goes first in RAM, to avoid large alignment hole */
104
-   .ram_vector_table (COPY): {
105
-        *(.ram_vector_table)
106
-    } > RAM
107
-
108
-    .text : {
109
-        __ram_text_start__ = .;
110
-        *(.init)
111
-        *(.text*)
112
-        *(.fini)
113
-        /* Pull all c'tors into .text */
114
-        *crtbegin.o(.ctors)
115
-        *crtbegin?.o(.ctors)
116
-        *(EXCLUDE_FILE(*crtend?.o *crtend.o) .ctors)
117
-        *(SORT(.ctors.*))
118
-        *(.ctors)
119
-        /* Followed by destructors */
120
-        *crtbegin.o(.dtors)
121
-        *crtbegin?.o(.dtors)
122
-        *(EXCLUDE_FILE(*crtend?.o *crtend.o) .dtors)
123
-        *(SORT(.dtors.*))
124
-        *(.dtors)
125
-
126
-        *(.eh_frame*)
127
-        . = ALIGN(4);
128
-        __ram_text_end__ = .;
129
-    } > RAM AT> FLASH
130
-    __ram_text_source__ = LOADADDR(.text);
131
-
132
-
133
-    .data : {
134
-        __data_start__ = .;
135
-        *(vtable)
136
-
137
-        *(.time_critical*)
138
-
139
-        . = ALIGN(4);
140
-        *(.rodata*)
141
-        . = ALIGN(4);
142
-
143
-        *(.data*)
144
-
145
-        . = ALIGN(4);
146
-        *(.after_data.*)
147
-        . = ALIGN(4);
148
-        /* preinit data */
149
-        PROVIDE_HIDDEN (__mutex_array_start = .);
150
-        KEEP(*(SORT(.mutex_array.*)))
151
-        KEEP(*(.mutex_array))
152
-        PROVIDE_HIDDEN (__mutex_array_end = .);
153
-
154
-        . = ALIGN(4);
155
-        /* preinit data */
156
-        PROVIDE_HIDDEN (__preinit_array_start = .);
157
-        KEEP(*(SORT(.preinit_array.*)))
158
-        KEEP(*(.preinit_array))
159
-        PROVIDE_HIDDEN (__preinit_array_end = .);
160
-
161
-        . = ALIGN(4);
162
-        /* init data */
163
-        PROVIDE_HIDDEN (__init_array_start = .);
164
-        KEEP(*(SORT(.init_array.*)))
165
-        KEEP(*(.init_array))
166
-        PROVIDE_HIDDEN (__init_array_end = .);
167
-
168
-        . = ALIGN(4);
169
-        /* finit data */
170
-        PROVIDE_HIDDEN (__fini_array_start = .);
171
-        *(SORT(.fini_array.*))
172
-        *(.fini_array)
173
-        PROVIDE_HIDDEN (__fini_array_end = .);
174
-
175
-        *(.jcr)
176
-        . = ALIGN(4);
177
-        /* All data end */
178
-        __data_end__ = .;
179
-    } > RAM AT> FLASH
180
-    /* __etext is the name of the .data init source pointer (...) */
181
-    __etext = LOADADDR(.data);
182
-
183
-    .uninitialized_data (COPY): {
184
-        . = ALIGN(4);
185
-        *(.uninitialized_data*)
186
-    } > RAM
187
-
188
-    /* Start and end symbols must be word-aligned */
189
-    .scratch_x : {
190
-        __scratch_x_start__ = .;
191
-        *(.scratch_x.*)
192
-        . = ALIGN(4);
193
-        __scratch_x_end__ = .;
194
-    } > SCRATCH_X AT > FLASH
195
-    __scratch_x_source__ = LOADADDR(.scratch_x);
196
-
197
-    .scratch_y : {
198
-        __scratch_y_start__ = .;
199
-        *(.scratch_y.*)
200
-        . = ALIGN(4);
201
-        __scratch_y_end__ = .;
202
-    } > SCRATCH_Y AT > FLASH
203
-    __scratch_y_source__ = LOADADDR(.scratch_y);
204
-
205
-    .bss  : {
206
-        . = ALIGN(4);
207
-        __bss_start__ = .;
208
-        *(SORT_BY_ALIGNMENT(SORT_BY_NAME(.bss*)))
209
-        *(COMMON)
210
-        . = ALIGN(4);
211
-        __bss_end__ = .;
212
-    } > RAM
213
-
214
-    .heap (COPY):
215
-    {
216
-        __end__ = .;
217
-        end = __end__;
218
-        *(.heap*)
219
-        __HeapLimit = .;
220
-    } > RAM
221
-
222
-    /* .stack*_dummy section doesn't contains any symbols. It is only
223
-     * used for linker to calculate size of stack sections, and assign
224
-     * values to stack symbols later
225
-     *
226
-     * stack1 section may be empty/missing if platform_launch_core1 is not used */
227
-
228
-    /* by default we put core 0 stack at the end of scratch Y, so that if core 1
229
-     * stack is not used then all of SCRATCH_X is free.
230
-     */
231
-    .stack1_dummy (COPY):
232
-    {
233
-        *(.stack1*)
234
-    } > SCRATCH_X
235
-    .stack_dummy (COPY):
236
-    {
237
-        *(.stack*)
238
-    } > SCRATCH_Y
239
-
240
-    .flash_end : {
241
-        __flash_binary_end = .;
242
-    } > FLASH
243
-
244
-    /* stack limit is poorly named, but historically is maximum heap ptr */
245
-    __StackLimit = ORIGIN(RAM) + LENGTH(RAM);
246
-    __StackOneTop = ORIGIN(SCRATCH_X) + LENGTH(SCRATCH_X);
247
-    __StackTop = ORIGIN(SCRATCH_Y) + LENGTH(SCRATCH_Y);
248
-    __StackOneBottom = __StackOneTop - SIZEOF(.stack1_dummy);
249
-    __StackBottom = __StackTop - SIZEOF(.stack_dummy);
250
-    PROVIDE(__stack = __StackTop);
251
-
252
-    /* Check if data + heap + stack exceeds RAM limit */
253
-    ASSERT(__StackLimit >= __HeapLimit, "region RAM overflowed")
254
-
255
-    ASSERT( __binary_info_header_end - __logical_binary_start <= 256, "Binary info must be in first 256 bytes of the binary")
256
-    /* todo assert on extra code */
257
-}
258
-

+ 1
- 1
bootloader_shell.ld Vedi File

@@ -64,7 +64,7 @@ SECTIONS
64 64
      */
65 65
     .app_hdr : {
66 66
         . = ALIGN(4k);
67
-	__app_hdr = .;
67
+        app_image_header = .;
68 68
 	LONG(0xdeaddead)
69 69
 	LONG(0)
70 70
 	LONG(0xdeaddead)

+ 0
- 248
combined.ld Vedi File

@@ -1,248 +0,0 @@
1
-/* Based on GCC ARM embedded samples.
2
-   Defines the following symbols for use by code:
3
-    __exidx_start
4
-    __exidx_end
5
-    __etext
6
-    __data_start__
7
-    __preinit_array_start
8
-    __preinit_array_end
9
-    __init_array_start
10
-    __init_array_end
11
-    __fini_array_start
12
-    __fini_array_end
13
-    __data_end__
14
-    __bss_start__
15
-    __bss_end__
16
-    __end__
17
-    end
18
-    __HeapLimit
19
-    __StackLimit
20
-    __StackTop
21
-    __stack (== StackTop)
22
-*/
23
-
24
-MEMORY
25
-{
26
-    FLASH_BL(rx) : ORIGIN = 0x10000000, LENGTH = 360k
27
-    FLASH_IMGHDR(rx) : ORIGIN = 0x10000000 + 360k, LENGTH = 4k
28
-    FLASH_APP(rx) : ORIGIN = 0x10000000 + 364k, LENGTH = 2048k - 364k
29
-    RAM(rwx) : ORIGIN =  0x20000000, LENGTH = 256k
30
-    SCRATCH_X(rwx) : ORIGIN = 0x20040000, LENGTH = 4k
31
-    SCRATCH_Y(rwx) : ORIGIN = 0x20041000, LENGTH = 4k
32
-}
33
-
34
-ENTRY(_entry_point)
35
-
36
-SECTIONS
37
-{
38
-    .flash_begin : {
39
-        __flash_binary_start = .;
40
-    } > FLASH_APP
41
-
42
-    /* Insert boot3, which is the combined boot2 + boot3 */
43
-    .boot3 : {
44
-        KEEP (*(.boot3))
45
-    } > FLASH_BL
46
-
47
-    /*
48
-     * Name a section for the image header.
49
-     * The contents will get replaced post-build
50
-     */
51
-    .app_hdr : {
52
-	LONG(0xdeaddead)
53
-	LONG(0)
54
-	LONG(0xdeaddead)
55
-    } > FLASH_IMGHDR
56
-
57
-    .text : {
58
-        __logical_binary_start = .;
59
-        KEEP (*(.vectors))
60
-        KEEP (*(.binary_info_header))
61
-        __binary_info_header_end = .;
62
-        KEEP (*(.reset))
63
-        /* TODO revisit this now memset/memcpy/float in ROM */
64
-        /* bit of a hack right now to exclude all floating point and time critical (e.g. memset, memcpy) code from
65
-         * FLASH ... we will include any thing excluded here in .data below by default */
66
-        *(.init)
67
-        *(EXCLUDE_FILE(*libgcc.a: *libc.a:*lib_a-mem*.o *libm.a:) .text*)
68
-        *(.fini)
69
-        /* Pull all c'tors into .text */
70
-        *crtbegin.o(.ctors)
71
-        *crtbegin?.o(.ctors)
72
-        *(EXCLUDE_FILE(*crtend?.o *crtend.o) .ctors)
73
-        *(SORT(.ctors.*))
74
-        *(.ctors)
75
-        /* Followed by destructors */
76
-        *crtbegin.o(.dtors)
77
-        *crtbegin?.o(.dtors)
78
-        *(EXCLUDE_FILE(*crtend?.o *crtend.o) .dtors)
79
-        *(SORT(.dtors.*))
80
-        *(.dtors)
81
-
82
-        *(.eh_frame*)
83
-        . = ALIGN(4);
84
-    } > FLASH_APP
85
-
86
-    .rodata : {
87
-        *(EXCLUDE_FILE(*libgcc.a: *libc.a:*lib_a-mem*.o *libm.a:) .rodata*)
88
-        . = ALIGN(4);
89
-        *(SORT_BY_ALIGNMENT(SORT_BY_NAME(.flashdata*)))
90
-        . = ALIGN(4);
91
-    } > FLASH_APP
92
-
93
-    .ARM.extab :
94
-    {
95
-        *(.ARM.extab* .gnu.linkonce.armextab.*)
96
-    } > FLASH_APP
97
-
98
-    __exidx_start = .;
99
-    .ARM.exidx :
100
-    {
101
-        *(.ARM.exidx* .gnu.linkonce.armexidx.*)
102
-    } > FLASH_APP
103
-    __exidx_end = .;
104
-
105
-    /* Machine inspectable binary information */
106
-    . = ALIGN(4);
107
-    __binary_info_start = .;
108
-    .binary_info :
109
-    {
110
-        KEEP(*(.binary_info.keep.*))
111
-        *(.binary_info.*)
112
-    } > FLASH_APP
113
-    __binary_info_end = .;
114
-    . = ALIGN(4);
115
-
116
-    /* End of .text-like segments */
117
-    __etext = .;
118
-
119
-   .ram_vector_table (COPY): {
120
-        *(.ram_vector_table)
121
-    } > RAM
122
-
123
-    .data : {
124
-        __data_start__ = .;
125
-        *(vtable)
126
-
127
-        *(.time_critical*)
128
-
129
-        /* remaining .text and .rodata; i.e. stuff we exclude above because we want it in RAM */
130
-        *(.text*)
131
-        . = ALIGN(4);
132
-        *(.rodata*)
133
-        . = ALIGN(4);
134
-
135
-        *(.data*)
136
-
137
-        . = ALIGN(4);
138
-        *(.after_data.*)
139
-        . = ALIGN(4);
140
-        /* preinit data */
141
-        PROVIDE_HIDDEN (__mutex_array_start = .);
142
-        KEEP(*(SORT(.mutex_array.*)))
143
-        KEEP(*(.mutex_array))
144
-        PROVIDE_HIDDEN (__mutex_array_end = .);
145
-
146
-        . = ALIGN(4);
147
-        /* preinit data */
148
-        PROVIDE_HIDDEN (__preinit_array_start = .);
149
-        KEEP(*(SORT(.preinit_array.*)))
150
-        KEEP(*(.preinit_array))
151
-        PROVIDE_HIDDEN (__preinit_array_end = .);
152
-
153
-        . = ALIGN(4);
154
-        /* init data */
155
-        PROVIDE_HIDDEN (__init_array_start = .);
156
-        KEEP(*(SORT(.init_array.*)))
157
-        KEEP(*(.init_array))
158
-        PROVIDE_HIDDEN (__init_array_end = .);
159
-
160
-        . = ALIGN(4);
161
-        /* finit data */
162
-        PROVIDE_HIDDEN (__fini_array_start = .);
163
-        *(SORT(.fini_array.*))
164
-        *(.fini_array)
165
-        PROVIDE_HIDDEN (__fini_array_end = .);
166
-
167
-        *(.jcr)
168
-        . = ALIGN(4);
169
-        /* All data end */
170
-        __data_end__ = .;
171
-    } > RAM AT> FLASH_APP
172
-
173
-    .uninitialized_data (COPY): {
174
-        . = ALIGN(4);
175
-        *(.uninitialized_data*)
176
-    } > RAM
177
-
178
-    /* Start and end symbols must be word-aligned */
179
-    .scratch_x : {
180
-        __scratch_x_start__ = .;
181
-        *(.scratch_x.*)
182
-        . = ALIGN(4);
183
-        __scratch_x_end__ = .;
184
-    } > SCRATCH_X AT > FLASH_APP
185
-    __scratch_x_source__ = LOADADDR(.scratch_x);
186
-
187
-    .scratch_y : {
188
-        __scratch_y_start__ = .;
189
-        *(.scratch_y.*)
190
-        . = ALIGN(4);
191
-        __scratch_y_end__ = .;
192
-    } > SCRATCH_Y AT > FLASH_APP
193
-    __scratch_y_source__ = LOADADDR(.scratch_y);
194
-
195
-    .bss  : {
196
-        . = ALIGN(4);
197
-        __bss_start__ = .;
198
-        *(SORT_BY_ALIGNMENT(SORT_BY_NAME(.bss*)))
199
-        *(COMMON)
200
-        . = ALIGN(4);
201
-        __bss_end__ = .;
202
-    } > RAM
203
-
204
-    .heap (COPY):
205
-    {
206
-        __end__ = .;
207
-        end = __end__;
208
-        *(.heap*)
209
-        __HeapLimit = .;
210
-    } > RAM
211
-
212
-    /* .stack*_dummy section doesn't contains any symbols. It is only
213
-     * used for linker to calculate size of stack sections, and assign
214
-     * values to stack symbols later
215
-     *
216
-     * stack1 section may be empty/missing if platform_launch_core1 is not used */
217
-
218
-    /* by default we put core 0 stack at the end of scratch Y, so that if core 1
219
-     * stack is not used then all of SCRATCH_X is free.
220
-     */
221
-    .stack1_dummy (COPY):
222
-    {
223
-        *(.stack1*)
224
-    } > SCRATCH_X
225
-    .stack_dummy (COPY):
226
-    {
227
-        *(.stack*)
228
-    } > SCRATCH_Y
229
-
230
-    .flash_end : {
231
-        __flash_binary_end = .;
232
-    } > FLASH_APP
233
-
234
-    /* stack limit is poorly named, but historically is maximum heap ptr */
235
-    __StackLimit = ORIGIN(RAM) + LENGTH(RAM);
236
-    __StackOneTop = ORIGIN(SCRATCH_X) + LENGTH(SCRATCH_X);
237
-    __StackTop = ORIGIN(SCRATCH_Y) + LENGTH(SCRATCH_Y);
238
-    __StackOneBottom = __StackOneTop - SIZEOF(.stack1_dummy);
239
-    __StackBottom = __StackTop - SIZEOF(.stack_dummy);
240
-    PROVIDE(__stack = __StackTop);
241
-
242
-    /* Check if data + heap + stack exceeds RAM limit */
243
-    ASSERT(__StackLimit >= __HeapLimit, "region RAM overflowed")
244
-
245
-    ASSERT( __binary_info_header_end - __logical_binary_start <= 256, "Binary info must be in first 256 bytes of the binary")
246
-    /* todo assert on extra code */
247
-}
248
-

+ 8
- 8
main.c Vedi File

@@ -87,10 +87,12 @@ struct event {
87 87
 
88 88
 #define TCP_PORT 4242
89 89
 
90
-#define IMAGE_HEADER_OFFSET (360 * 1024)
90
+struct image_header app_image_header;
91
+#define IMAGE_HEADER_ADDR   ((uint32_t)&app_image_header)
92
+#define IMAGE_HEADER_OFFSET (IMAGE_HEADER_ADDR - XIP_BASE)
91 93
 
92
-#define WRITE_ADDR_MIN (XIP_BASE + IMAGE_HEADER_OFFSET + FLASH_SECTOR_SIZE)
93
-#define ERASE_ADDR_MIN (XIP_BASE + IMAGE_HEADER_OFFSET)
94
+#define WRITE_ADDR_MIN (IMAGE_HEADER_ADDR + FLASH_SECTOR_SIZE)
95
+#define ERASE_ADDR_MIN (IMAGE_HEADER_ADDR)
94 96
 #define FLASH_ADDR_MAX (XIP_BASE + PICO_FLASH_SIZE_BYTES)
95 97
 
96 98
 #define CMD_SYNC          (('S' << 0) | ('Y' << 8) | ('N' << 16) | ('C' << 24))
@@ -421,7 +423,7 @@ static uint32_t handle_seal(uint32_t *args_in, uint8_t *data_in, uint32_t *resp_
421 423
 	flash_range_program(IMAGE_HEADER_OFFSET, (const uint8_t *)&hdr, sizeof(hdr));
422 424
 	critical_section_exit(&critical_section);
423 425
 
424
-	struct image_header *check = (struct image_header *)(XIP_BASE + IMAGE_HEADER_OFFSET);
426
+	struct image_header *check = &app_image_header;
425 427
 	if (memcmp(&hdr, check, sizeof(hdr))) {
426 428
 		return TCP_COMM_RSP_ERR;
427 429
 	}
@@ -580,10 +582,8 @@ int main()
580 582
 
581 583
 	sleep_ms(10);
582 584
 
583
-	struct image_header *hdr = (struct image_header *)(XIP_BASE + IMAGE_HEADER_OFFSET);
584
-
585
-	if (!should_stay_in_bootloader() && image_header_ok(hdr)) {
586
-		uint32_t vtor = *((uint32_t *)(XIP_BASE + IMAGE_HEADER_OFFSET));
585
+	if (!should_stay_in_bootloader() && image_header_ok(&app_image_header)) {
586
+		uint32_t vtor = *(uint32_t *)IMAGE_HEADER_ADDR;
587 587
 		disable_interrupts();
588 588
 		reset_peripherals();
589 589
 		jump_to_vtor(vtor);

Loading…
Annulla
Salva