forked from trezor/trezor-firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flash_area.c
309 lines (270 loc) · 9.09 KB
/
flash_area.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
* This file is part of the Trezor project, https://trezor.io/
*
* Copyright (c) SatoshiLabs
*
* 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/>.
*/
#include "flash_area.h"
#include <stdbool.h>
#include <stddef.h>
#include <string.h>
uint32_t flash_area_get_size(const flash_area_t *area) {
uint32_t size = 0;
for (int i = 0; i < area->num_subareas; i++) {
size += flash_sector_size(area->subarea[i].first_sector,
area->subarea[i].num_sectors);
}
return size;
}
uint16_t flash_area_total_sectors(const flash_area_t *area) {
uint16_t total = 0;
for (int i = 0; i < area->num_subareas; i++) {
total += area->subarea[i].num_sectors;
}
return total;
}
static secbool get_sector_and_offset(const flash_area_t *area, uint32_t offset,
uint16_t *sector_out,
uint32_t *offset_out) {
for (int i = 0; i < area->num_subareas; i++) {
// Get the sub-area parameters
uint16_t first_sector = area->subarea[i].first_sector;
uint16_t num_sectors = area->subarea[i].num_sectors;
uint32_t subarea_size = flash_sector_size(first_sector, num_sectors);
// Does the requested offset start in the sub-area?
if (offset < subarea_size) {
uint16_t found_sector = flash_sector_find(first_sector, offset);
*sector_out = found_sector;
*offset_out =
offset - flash_sector_size(first_sector, found_sector - first_sector);
return sectrue;
}
offset -= subarea_size;
}
return secfalse;
}
const void *flash_area_get_address(const flash_area_t *area, uint32_t offset,
uint32_t size) {
for (int i = 0; i < area->num_subareas; i++) {
// Get sub-area parameters
uint16_t first_sector = area->subarea[i].first_sector;
uint16_t num_sectors = area->subarea[i].num_sectors;
uint32_t subarea_size = flash_sector_size(first_sector, num_sectors);
// Does the requested block start in the sub-area?
if (offset < subarea_size) {
// Does the requested block fit in the sub-area?
if (offset + size <= subarea_size) {
const uint8_t *ptr =
(const uint8_t *)flash_get_address(first_sector, 0, 0);
// We expect that all sectors/pages in the sub-area make
// a continuous block of adresses with the same security atributes
return ptr + offset;
} else {
return NULL;
}
}
offset -= subarea_size;
}
return NULL;
}
#if defined FLASH_BIT_ACCESS
secbool flash_area_write_byte(const flash_area_t *area, uint32_t offset,
uint8_t data) {
uint16_t sector;
uint32_t sector_offset;
if (get_sector_and_offset(area, offset, §or, §or_offset) != sectrue) {
return secfalse;
}
return flash_write_byte(sector, sector_offset, data);
}
secbool flash_area_write_word(const flash_area_t *area, uint32_t offset,
uint32_t data) {
uint16_t sector;
uint32_t sector_offset;
if (get_sector_and_offset(area, offset, §or, §or_offset) != sectrue) {
return secfalse;
}
return flash_write_word(sector, sector_offset, data);
}
#else // not defined FLASH_BIT_ACCESS
secbool flash_area_write_quadword(const flash_area_t *area, uint32_t offset,
const uint32_t *data) {
uint16_t sector;
uint32_t sector_offset;
if (get_sector_and_offset(area, offset, §or, §or_offset) != sectrue) {
return secfalse;
}
return flash_write_quadword(sector, sector_offset, data);
}
#endif // not defined FLASH_BIT_ACCESS
#ifdef FLASH_BURST_SIZE
secbool flash_area_write_burst(const flash_area_t *area, uint32_t offset,
const uint32_t *data) {
uint16_t sector;
uint32_t sector_offset;
if (get_sector_and_offset(area, offset, §or, §or_offset) != sectrue) {
return secfalse;
}
return flash_write_burst(sector, sector_offset, data);
}
#endif
secbool flash_area_write_block(const flash_area_t *area, uint32_t offset,
const flash_block_t block) {
if (!FLASH_IS_ALIGNED(offset)) {
return secfalse;
}
uint16_t sector;
uint32_t sector_offset;
if (sectrue != get_sector_and_offset(area, offset, §or, §or_offset)) {
return secfalse;
}
return flash_write_block(sector, sector_offset, block);
}
secbool __wur flash_area_write_data(const flash_area_t *area, uint32_t offset,
const void *data, uint32_t size) {
return flash_area_write_data_padded(area, offset, data, size, 0, size);
}
secbool __wur flash_area_write_data_padded(const flash_area_t *area,
uint32_t offset, const void *data,
uint32_t data_size, uint8_t padding,
uint32_t total_size) {
if (offset % FLASH_BLOCK_SIZE) {
return secfalse;
}
if (total_size % FLASH_BLOCK_SIZE) {
return secfalse;
}
if (data_size > total_size) {
return secfalse;
}
if (offset + total_size > flash_area_get_size(area)) {
return secfalse;
}
const uint32_t *data32 = (const uint32_t *)data;
while (total_size > 0) {
#ifdef FLASH_BURST_SIZE
if ((offset % FLASH_BURST_SIZE) == 0 &&
(offset + FLASH_BURST_SIZE) <= total_size) {
if (data_size >= FLASH_BURST_SIZE) {
if (flash_area_write_burst(area, offset, data32) != sectrue) {
return secfalse;
}
data_size -= FLASH_BURST_SIZE;
data32 += FLASH_BURST_WORDS;
} else {
uint32_t burst[FLASH_BURST_WORDS];
memset(burst, padding, sizeof(burst));
if (data_size > 0) {
memcpy(burst, data32, data_size);
data_size = 0;
}
if (flash_area_write_burst(area, offset, burst) != sectrue) {
return secfalse;
}
}
offset += FLASH_BURST_SIZE;
total_size -= FLASH_BURST_SIZE;
} else
#endif
{
if (data_size >= FLASH_BLOCK_SIZE) {
if (flash_area_write_block(area, offset, data32) != sectrue) {
return secfalse;
}
data_size -= FLASH_BLOCK_SIZE;
data32 += FLASH_BLOCK_WORDS;
} else {
uint32_t block[FLASH_BLOCK_WORDS];
memset(block, padding, sizeof(block));
if (data_size > 0) {
memcpy(block, data32, data_size);
data_size = 0;
}
if (flash_area_write_block(area, offset, block) != sectrue) {
return secfalse;
}
}
offset += FLASH_BLOCK_SIZE;
total_size -= FLASH_BLOCK_SIZE;
}
}
return sectrue;
}
secbool flash_area_erase(const flash_area_t *area,
void (*progress)(int pos, int len)) {
return flash_area_erase_bulk(area, 1, progress);
}
static secbool erase_sector(uint16_t sector) {
secbool result = secfalse;
if (sectrue != flash_unlock_write()) {
return secfalse;
}
result = flash_sector_erase(sector);
if (sectrue != flash_lock_write()) {
return secfalse;
}
return result;
}
secbool flash_area_erase_bulk(const flash_area_t *area, int count,
void (*progress)(int pos, int len)) {
int total_sectors = 0;
int done_sectors = 0;
for (int a = 0; a < count; a++) {
for (int i = 0; i < area[a].num_subareas; i++) {
total_sectors += area[a].subarea[i].num_sectors;
}
}
if (progress) {
progress(0, total_sectors);
}
for (int a = 0; a < count; a++) {
for (int s = 0; s < area[a].num_subareas; s++) {
for (int i = 0; i < area[a].subarea[s].num_sectors; i++) {
int sector = area[a].subarea[s].first_sector + i;
if (sectrue != erase_sector(sector)) {
return secfalse;
}
done_sectors++;
if (progress) {
progress(done_sectors, total_sectors);
}
}
}
}
return sectrue;
}
secbool flash_area_erase_partial(const flash_area_t *area, uint32_t offset,
uint32_t *bytes_erased) {
uint32_t sector_offset = 0;
*bytes_erased = 0;
for (int s = 0; s < area->num_subareas; s++) {
for (int i = 0; i < area->subarea[s].num_sectors; i++) {
uint32_t sector = area->subarea[s].first_sector + i;
uint32_t sector_size = flash_sector_size(sector, 1);
if (offset == sector_offset) {
if (sectrue != erase_sector(sector)) {
return secfalse;
}
*bytes_erased = sector_size;
return sectrue;
}
sector_offset += sector_size;
}
}
if (offset == sector_offset) {
return sectrue;
}
return secfalse;
}