-
Notifications
You must be signed in to change notification settings - Fork 103
/
supervise.c
225 lines (187 loc) · 5.74 KB
/
supervise.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
/*
* Copyright (C) 2018 Keepkey
*
* This library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This library 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef EMULATOR
#include <libopencm3/stm32/flash.h>
#else
#include <stdint.h>
#include <stdbool.h>
#endif
#include <stdint.h>
#include <string.h>
#include "keepkey/board/supervise.h"
#include "keepkey/board/memory.h"
#include "keepkey/board/keepkey_flash.h"
#ifndef EMULATOR
/// Return context from user isr processing
void svc_busr_return(void) {
__asm__ __volatile__("svc %0" ::"i"(SVC_BUSR_RET) : "memory");
}
/// Return context from user isr processing
void svc_tusr_return(void) {
__asm__ __volatile__("svc %0" ::"i"(SVC_TUSR_RET) : "memory");
}
/// Enable interrupts
void svc_enable_interrupts(void) {
__asm__ __volatile__("svc %0" ::"i"(SVC_ENA_INTR) : "memory");
}
/// Return context from user isr processing
void svc_disable_interrupts(void) {
__asm__ __volatile__("svc %0" ::"i"(SVC_DIS_INTR) : "memory");
}
/// \brief Erase a flash sector.
/// @param sector sector number 0..11
void svc_flash_erase_sector(uint32_t sector) {
_param_1 = sector;
_param_2 = 0;
_param_3 = 0;
__asm__ __volatile__("svc %0" ::"i"(SVC_FLASH_ERASE) : "memory");
}
bool svc_flash_pgm_blk(uint32_t beginAddr, uint32_t data, uint32_t align) {
_param_1 = beginAddr;
_param_2 = data;
_param_3 = align;
__asm__ __volatile__("svc %0" ::"i"(SVC_FLASH_PGM_BLK) : "memory");
return !!_param_1;
}
bool svc_flash_pgm_word(uint32_t beginAddr, uint32_t data) {
_param_1 = beginAddr;
_param_2 = data;
_param_3 = 0;
__asm__ __volatile__("svc %0" ::"i"(SVC_FLASH_PGM_WORD) : "memory");
return !!_param_1;
}
void svhandler_flash_erase_sector(void) {
uint32_t sector = _param_1;
// Do not allow firmware to erase bootstrap or bootloader sectors.
if ((sector == FLASH_BOOTSTRAP_SECTOR) ||
(sector >= FLASH_BOOT_SECTOR_FIRST && sector <= FLASH_BOOT_SECTOR_LAST)) {
return;
}
// Unlock flash.
flash_clear_status_flags();
flash_unlock();
// Erase the sector.
flash_erase_sector(sector, FLASH_CR_PROGRAM_X32);
// Return flash status.
_param_1 = !!flash_chk_status();
_param_2 = 0;
_param_3 = 0;
// Wait for any write operation to complete.
flash_wait_for_last_operation();
// Disable writes to flash.
FLASH_CR &= ~FLASH_CR_PG;
// lock flash register
FLASH_CR |= FLASH_CR_LOCK;
}
void svhandler_flash_pgm_blk(void) {
uint32_t beginAddr = _param_1;
uint32_t data = _param_2;
uint32_t length = _param_3;
// Protect from overflow.
if (beginAddr + length < beginAddr) return;
// Do not allow firmware to erase bootstrap or bootloader sectors.
if (((beginAddr >= BSTRP_FLASH_SECT_START) &&
(beginAddr <= (BSTRP_FLASH_SECT_START + BSTRP_FLASH_SECT_LEN - 1))) ||
(((beginAddr + length) >= BSTRP_FLASH_SECT_START) &&
((beginAddr + length) <=
(BSTRP_FLASH_SECT_START + BSTRP_FLASH_SECT_LEN - 1)))) {
return;
}
if (((beginAddr >= BLDR_FLASH_SECT_START) &&
(beginAddr <= (BLDR_FLASH_SECT_START + 2 * BLDR_FLASH_SECT_LEN - 1))) ||
(((beginAddr + length) >= BLDR_FLASH_SECT_START) &&
((beginAddr + length) <=
(BLDR_FLASH_SECT_START + 2 * BLDR_FLASH_SECT_LEN - 1)))) {
return;
}
// Unlock flash.
flash_clear_status_flags();
flash_unlock();
// Flash write.
flash_program(beginAddr, (uint8_t *)data, length);
// Return flash status.
_param_1 = !!flash_chk_status();
_param_2 = 0;
_param_3 = 0;
// Wait for any write operation to complete.
flash_wait_for_last_operation();
// Disable writes to flash.
FLASH_CR &= ~FLASH_CR_PG;
// Lock flash register
FLASH_CR |= FLASH_CR_LOCK;
}
void svhandler_flash_pgm_word(void) {
uint32_t dst = _param_1;
uint32_t src = _param_2;
// Do not allow firmware to erase bootstrap or bootloader sectors.
if ((dst >= BSTRP_FLASH_SECT_START) &&
(dst <= (BSTRP_FLASH_SECT_START + BSTRP_FLASH_SECT_LEN))) {
return;
}
if ((dst >= BLDR_FLASH_SECT_START) &&
(dst <= (BLDR_FLASH_SECT_START + 2 * BLDR_FLASH_SECT_LEN))) {
return;
}
// Unlock flash.
flash_clear_status_flags();
flash_unlock();
// Flash write.
flash_program_word(dst, src);
_param_1 = !!flash_chk_status();
_param_2 = 0;
_param_3 = 0;
// Wait for any write operation to complete.
flash_wait_for_last_operation();
// Disable writes to flash.
FLASH_CR &= ~FLASH_CR_PG;
// Lock flash register
FLASH_CR |= FLASH_CR_LOCK;
}
void svc_handler_main(uint32_t *stack) {
uint8_t svc_number = ((uint8_t *)stack[6])[-2];
switch (svc_number) {
case SVC_BUSR_RET:
svhandler_button_usr_return();
break;
case SVC_TUSR_RET:
svhandler_timer_usr_return();
break;
case SVC_ENA_INTR:
svhandler_enable_interrupts();
break;
case SVC_DIS_INTR:
svhandler_disable_interrupts();
break;
case SVC_FLASH_ERASE:
svhandler_flash_erase_sector();
break;
case SVC_FLASH_PGM_BLK:
svhandler_flash_pgm_blk();
break;
case SVC_FLASH_PGM_WORD:
svhandler_flash_pgm_word();
break;
case SVC_FIRMWARE_PRIV:
case SVC_FIRMWARE_UNPRIV:
svhandler_start_firmware(svc_number);
break;
default:
stack[0] = 0xffffffff;
break;
}
}
#endif