@@ -36,6 +36,11 @@ static inline void set_bootloader(uint32_t address){
36
36
* BOOTLOADER_JUMP_ADDRESS = (uint32_t * )address ;
37
37
}
38
38
39
+ /*Light sanity checks on what a good stack-pointer and program counter look like */
40
+ static inline bool is_valid (uint32_t pc , uint32_t sp ){
41
+ return ((sp >> 24 ) == 0x20 ) && ((pc >> 24 ) == 0x08 );
42
+ }
43
+
39
44
/**
40
45
* Executed on boot; will jump to a non-default program if:
41
46
* 1. the value at `BOOTLOADER_MAGIC_ADDRESS` is `BOOTLOADER_MAGIC`
@@ -48,6 +53,7 @@ void bootloader(){
48
53
* BOOTLOADER_MAGIC_ADDRESS = 0 ;
49
54
uint32_t sp = (* BOOTLOADER_JUMP_ADDRESS )[0 ];
50
55
uint32_t pc = (* BOOTLOADER_JUMP_ADDRESS )[1 ];
56
+ if (!is_valid (pc , sp )) goto start_ofw ;
51
57
start_app ((void (* const )(void )) pc , (uint32_t ) sp );
52
58
}
53
59
@@ -79,6 +85,7 @@ void bootloader(){
79
85
* BOOTLOADER_MAGIC_ADDRESS = 0 ;
80
86
uint32_t sp = (* BOOTLOADER_JUMP_ADDRESS )[0 ];
81
87
uint32_t pc = (* BOOTLOADER_JUMP_ADDRESS )[1 ];
88
+ if (!is_valid (pc , sp )) goto start_ofw ;
82
89
start_app ((void (* const )(void )) pc , (uint32_t ) sp );
83
90
}
84
91
@@ -92,9 +99,11 @@ void bootloader(){
92
99
uint32_t pc = * ((uint32_t * )BANK_2_ADDRESS + 1 );
93
100
#endif
94
101
102
+ if (!is_valid (pc , sp )) goto start_ofw ;
95
103
start_app ((void (* const )(void )) pc , (uint32_t ) sp );
96
104
}
97
105
106
+ start_ofw :
98
107
start_app (stock_Reset_Handler , * (uint32_t * ) MSP_ADDRESS );
99
108
while (1 );
100
109
}
@@ -157,14 +166,19 @@ gamepad_t read_buttons() {
157
166
#else
158
167
if ((gamepad & GAMEPAD_LEFT ) && (gamepad & GAMEPAD_GAME )){
159
168
#endif
160
-
169
+ uint32_t * target_address ;
161
170
#if SD_BOOTLOADER
162
- set_bootloader ( SD_BOOTLOADER_ADDRESS ) ;
171
+ target_address = SD_BOOTLOADER_ADDRESS ;
163
172
#else
164
- set_bootloader ( BANK_2_ADDRESS ) ;
173
+ target_address = BANK_2_ADDRESS ;
165
174
#endif
175
+ uint32_t sp = * target_address ;
176
+ uint32_t pc = * (target_address + 1 );
166
177
167
- NVIC_SystemReset ();
178
+ if (is_valid (pc , sp )){
179
+ set_bootloader (target_address );
180
+ NVIC_SystemReset ();
181
+ }
168
182
}
169
183
170
184
#if ENABLE_SMB1_GRAPHIC_MODS
0 commit comments