-
Notifications
You must be signed in to change notification settings - Fork 62
/
flashwarning.c
111 lines (86 loc) · 2.64 KB
/
flashwarning.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
#include "bl.h"
#include <libopencm3/stm32/flash.h>
#include <libopencm3/cm3/nvic.h>
#include <libopencm3/cm3/cortex.h>
// https://makecode.com/_0arRi9Ys0h1F
static int menuActive = 0;
#define MENU_SIZE 4
static const char *menu_options[] = {
"Cancel",
"Cancel",
"Cancel",
"Update",
};
static void draw_menu() {
int y = 72;
drawBar(y - 2, 12 * MENU_SIZE, 0);
for (int i = 0; i < MENU_SIZE; ++i) {
if (i == menuActive) {
drawBar(y - 2, 11, 4);
print(10, y, 9, ">");
}
print(26, y, 9, menu_options[i]);
y += 12;
}
}
#define WARNING_RTC_SIG 0x01312e8d
void warning_screen(uint32_t bootSig) {
start_systick();
if (bootSig == WARNING_RTC_SIG) {
flash_program_option_bytes(FLASH_OPTCR & ~0x80030000);
return;
}
board_set_rtc_signature(WARNING_RTC_SIG, 0);
screen_init();
drawBar(0, 12, 2);
print(10, 2, 1, "WARNING! DANGER!");
print(10, 20, 2,
" This bootloader\n"
"update may DAMAGE\n"
" your device!");
draw_menu();
draw_screen();
setup_input_pin(CFG_PIN_BTN_A);
setup_input_pin(CFG_PIN_BTN_B);
setup_input_pin(CFG_PIN_BTN_UP);
setup_input_pin(CFG_PIN_BTN_DOWN);
DMESG("OPTCR: %p", FLASH_OPTCR);
while (1) {
delay(100);
int d = 0;
if (pin_get(CFG_PIN_BTN_UP) == 0)
d = -1;
if (pin_get(CFG_PIN_BTN_DOWN) == 0)
d = 1;
if (d) {
if (0 <= menuActive + d && menuActive + d < MENU_SIZE) {
menuActive += d;
draw_menu();
draw_screen();
}
} else {
if (pin_get(CFG_PIN_BTN_A) == 0 || pin_get(CFG_PIN_BTN_B) == 0) {
drawBar(0, 120, 2);
draw_screen();
if (menuActive == MENU_SIZE - 1) {
//DMESG("flashing...");
jump_to_app();
}
// they chose a different option, or jump to app failed
// enable write protection
flash_program_option_bytes(FLASH_OPTCR & ~0x80030000);
//DMESG("OPTCR2: %p", FLASH_OPTCR);
//delay(300);
flash_unlock();
// disable the update software
flash_program_word(APP_LOAD_ADDRESS, 0);
flash_program_word(APP_LOAD_ADDRESS + 4, 0);
//DMESG("cleared: %p", *(int*)APP_LOAD_ADDRESS);
//delay(300);
//DMESG("done");
// exit to bootloader
return;
}
}
}
}