-
Notifications
You must be signed in to change notification settings - Fork 0
/
reset.c
262 lines (215 loc) · 7.1 KB
/
reset.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
* This file is part of the stfuboot project.
*
* Copyright (C) 2012 Innovative Converged Devices (ICD)
*
* Author(s):
* Andrey Smirnov <andrey.smirnov@convergeddevices.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* ---- 0x08000000 ----
* permanent vector table
* permanent vector handlers' code
* ---- 0x08001000 ----
* reset vector handler
* main vector table
* bootloader code
* ---- 0x08004800 ----
* fw information block
* ---- 0x08004a00 ----
* application code
*/
#include <libopencm3/cm3/vector.h>
#include <libopencm3/cm3/scb.h>
#include <libopencm3/stm32/usart.h>
#include <libopencm3/stm32/crc.h>
#include <libopencm3/stm32/f1/rcc.h>
#include <libstfub/scratch.h>
#include <libstfub/info_block.h>
#include "uart.h"
extern unsigned _data_loadaddr, _data, _edata, _ebss, _stack;
extern unsigned _text_loadaddr, _text, _etext;
extern unsigned _ram_start;
extern int main(void);
extern unsigned _if_rom_start;
extern unsigned _ap_rom_start;
extern unsigned _sy_rom_start;
static bool stfub_firmware_is_valid(void)
{
u32 crc;
struct stfub_firmware_info *info_block;
info_block = (struct stfub_firmware_info *)&_if_rom_start;
#if 1
crc = crc_calculate_block((u32 *) info_block,
(sizeof(*info_block) / 4) - 4);
if (crc != info_block->crc.info_block)
return false;
#endif
crc = crc_calculate_block((u32 *)&_ap_rom_start,
info_block->size / 4);
return crc == info_block->crc.firmware;
/* force to dfu mode if previous line commented */
return false;
}
__attribute__ ((noreturn, interrupt))
void stfub_ram_reset_handler(void)
{
volatile unsigned *src, *dest;
asm volatile("msr msp, %0" : : "r"(&_stack));
/* Copy .data section to system RAM */
for (src = &_data_loadaddr, dest = &_data; dest < &_edata; src++, dest++)
*dest = *src;
while (dest < &_ebss)
*dest++ = 0;
/* Call the application's entry point. */
main();
for(;;)
asm volatile("nop");
}
void null_handler(void);
volatile u32 cfg = 0;
static void stfub_reset_start_clocks(void)
{
cfg = RCC_CFGR;
if ( RCC_CFGR != 6865920 ) {
rcc_clock_setup_in_hsi_out_48mhz();
} else {
rcc_osc_on(PLL);
rcc_wait_for_osc_ready(PLL);
}
rcc_peripheral_enable_clock(&RCC_AHBENR, RCC_AHBENR_CRCEN);
}
static void stfub_reset_stop_clocks(void)
{
rcc_peripheral_disable_clock(&RCC_AHBENR, RCC_AHBENR_CRCEN);
rcc_set_sysclk_source(RCC_CFGR_SW_SYSCLKSEL_HSICLK);
rcc_osc_off(PLL);
}
__attribute__ ((section(".exception_handlers"), naked))
static void stfub_start_with_vector_table_at_offset(void *table)
{
vector_table_t *vtable = table;
SCB_VTOR = (u32)vtable;
vtable->reset();
}
volatile bool scratchpad_is_valid, firmware_is_valid;
__attribute__ ((section(".reset_code"), naked, noreturn, interrupt))
void stfub_rom_reset_handler(void)
{
volatile unsigned *src, *dest;
vector_table_t *vtable;
/* Copy the interrupt vector table to the the system RAM */
for (src = &_text_loadaddr, dest = &_text; dest < &_etext; src++, dest++)
*dest = *src;
/* Copy the .text section to the the system RAM */
for (src = &_text_loadaddr, dest = &_text; dest < &_etext; src++, dest++)
*dest = *src;
stfub_reset_start_clocks();
scratchpad_is_valid = stfub_scratchpad_is_valid();
firmware_is_valid = stfub_firmware_is_valid();
stfub_reset_stop_clocks();
if ((scratchpad_is_valid && stfub_scratchpad_dfu_switch_requested())
|| !firmware_is_valid) {
/* Patch vector table so it would point to correct handlers
* located in RAM */
vtable = (vector_table_t *)&_ram_start;
vtable->reset = stfub_ram_reset_handler;
stfub_start_with_vector_table_at_offset(&_ram_start);
} else {
stfub_start_with_vector_table_at_offset(&_ap_rom_start);
}
}
__attribute__ ((section(".exception_handlers")))
bool stfub_exception_handlers_pages_are_protected(void)
{
extern unsigned _op_rom_start;
struct option_bytes *obytes = (struct option_bytes *) &_op_rom_start;
/* FIXME: checking for 0 instead of 1 */
#if 0
return (obytes->wrp0 & (FLASH_WRP_PAGE(0) | FLASH_WRP_PAGE(1))) ==
(FLASH_WRP_PAGE(0) | FLASH_WRP_PAGE(1));
#else
return true;
#endif
}
__attribute__ ((section(".exception_handlers")))
bool stfub_bootloader_update_failed(void)
{
extern unsigned _op_rom_start;
struct option_bytes *obytes = (struct option_bytes *) &_op_rom_start;
#if 0
return (obytes->data0 == ~obytes->ndata0) &&
(obytes->data1 == ~obytes->ndata1) &&
obytes->data0 == 0x42 &&
obytes->data1 == 0x24;
#else
return false;
#endif
}
__attribute__ ((section(".exception_handlers"), naked, interrupt))
void stfub_rom_early_reset(void)
{
if (!stfub_exception_handlers_pages_are_protected() ||
stfub_bootloader_update_failed())
stfub_start_with_vector_table_at_offset(&_sy_rom_start);
else
stfub_start_with_vector_table_at_offset(&_text_loadaddr);
}
__attribute__ ((section(".exception_handlers"), naked, interrupt))
void stfub_rom_retreat_to_factory_dfu(void)
{
register vector_table_t *vtable;
register struct scb_exception_stack_frame *frame;
SCB_GET_EXCEPTION_STACK_FRAME(frame);
vtable = (vector_table_t *)&_sy_rom_start;
SCB_VTOR = (u32)vtable;
frame->pc = (u32)vtable->reset;
asm volatile ("bx lr");
}
__attribute__ ((section(".exception_handlers"), naked, interrupt))
void stfub_rom_null_handler(void)
{
/* Do nothing. */
asm volatile ("bx lr");
}
__attribute__ ((section(".vectors")))
vector_table_t pvector_table = {
.initial_sp_value = &_stack,
.reset = stfub_rom_early_reset,
.nmi = stfub_rom_null_handler,
.hard_fault = stfub_rom_retreat_to_factory_dfu,
.memory_manage_fault = stfub_rom_retreat_to_factory_dfu,
.bus_fault = stfub_rom_retreat_to_factory_dfu,
.usage_fault = stfub_rom_retreat_to_factory_dfu,
.debug_monitor = stfub_rom_null_handler,
.sv_call = stfub_rom_null_handler,
.pend_sv = stfub_rom_null_handler,
.systick = stfub_rom_null_handler,
.irq = {
[0 ... NVIC_IRQ_COUNT - 1] = stfub_rom_null_handler
}
};
#define ALIAS(name) __attribute__((alias (#name)))
void reset_handler(void) ALIAS(stfub_rom_reset_handler);
void nmi_handler(void) ALIAS(stfub_rom_null_handler);
void hard_fault_handler(void) ALIAS(stfub_rom_retreat_to_factory_dfu);
void mem_manage_handler(void) ALIAS(stfub_rom_retreat_to_factory_dfu);
void bus_fault_handler(void) ALIAS(stfub_rom_retreat_to_factory_dfu);
void usage_fault_handler(void) ALIAS(stfub_rom_retreat_to_factory_dfu);
void sv_call_handler(void) ALIAS(stfub_rom_null_handler);
void debug_monitor_handler(void) ALIAS(stfub_rom_null_handler);
void pend_sv_handler(void) ALIAS(stfub_rom_null_handler);
void sys_tick_handler(void) ALIAS(stfub_rom_null_handler);