Skip to content

Commit fbb255c

Browse files
authored
Merge pull request #74 from BrianPugh/sanity-check-launch
Add a sanity-check to PC and SP before launching other firmware.
2 parents 1bd588c + 974c49b commit fbb255c

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

Core/Src/main.c

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ static inline void set_bootloader(uint32_t address){
3636
*BOOTLOADER_JUMP_ADDRESS = (uint32_t *)address;
3737
}
3838

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+
3944
/**
4045
* Executed on boot; will jump to a non-default program if:
4146
* 1. the value at `BOOTLOADER_MAGIC_ADDRESS` is `BOOTLOADER_MAGIC`
@@ -48,6 +53,7 @@ void bootloader(){
4853
*BOOTLOADER_MAGIC_ADDRESS = 0;
4954
uint32_t sp = (*BOOTLOADER_JUMP_ADDRESS)[0];
5055
uint32_t pc = (*BOOTLOADER_JUMP_ADDRESS)[1];
56+
if (!is_valid(pc, sp)) goto start_ofw;
5157
start_app((void (* const)(void)) pc, (uint32_t) sp);
5258
}
5359

@@ -79,6 +85,7 @@ void bootloader(){
7985
*BOOTLOADER_MAGIC_ADDRESS = 0;
8086
uint32_t sp = (*BOOTLOADER_JUMP_ADDRESS)[0];
8187
uint32_t pc = (*BOOTLOADER_JUMP_ADDRESS)[1];
88+
if (!is_valid(pc, sp)) goto start_ofw;
8289
start_app((void (* const)(void)) pc, (uint32_t) sp);
8390
}
8491

@@ -92,9 +99,11 @@ void bootloader(){
9299
uint32_t pc = *((uint32_t*)BANK_2_ADDRESS + 1);
93100
#endif
94101

102+
if (!is_valid(pc, sp)) goto start_ofw;
95103
start_app((void (* const)(void)) pc, (uint32_t) sp);
96104
}
97105

106+
start_ofw:
98107
start_app(stock_Reset_Handler, *(uint32_t *) MSP_ADDRESS);
99108
while(1);
100109
}
@@ -157,14 +166,19 @@ gamepad_t read_buttons() {
157166
#else
158167
if((gamepad & GAMEPAD_LEFT) && (gamepad & GAMEPAD_GAME)){
159168
#endif
160-
169+
uint32_t *target_address;
161170
#if SD_BOOTLOADER
162-
set_bootloader(SD_BOOTLOADER_ADDRESS);
171+
target_address = SD_BOOTLOADER_ADDRESS;
163172
#else
164-
set_bootloader(BANK_2_ADDRESS);
173+
target_address = BANK_2_ADDRESS;
165174
#endif
175+
uint32_t sp = *target_address;
176+
uint32_t pc = *(target_address + 1);
166177

167-
NVIC_SystemReset();
178+
if(is_valid(pc, sp)){
179+
set_bootloader(target_address);
180+
NVIC_SystemReset();
181+
}
168182
}
169183

170184
#if ENABLE_SMB1_GRAPHIC_MODS

scripts/device_from_patch_params.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
if __name__ == "__main__":
99
parser = argparse.ArgumentParser()
10-
parser.add_argument("--device", default="mario")
10+
parser.add_argument("--device", default="mario", choices=["mario", "zelda"])
1111
args, _ = parser.parse_known_args()
1212
device = args.device.upper()
1313
print(device)

0 commit comments

Comments
 (0)