forked from mcu-tools/mcuboot
-
Notifications
You must be signed in to change notification settings - Fork 2
/
flash_map.c
360 lines (312 loc) · 9.65 KB
/
flash_map.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#include <zephyr.h>
#include <flash.h>
#include "target.h"
#include <flash_map/flash_map.h>
#include <hal/hal_flash.h>
#include <sysflash/sysflash.h>
#define BOOT_LOG_LEVEL BOOT_LOG_LEVEL_INFO
#include "bootutil/bootutil_log.h"
extern struct device *boot_flash_device;
/*
* For now, we only support one flash device.
*
* Pick a random device ID for it that's unlikely to collide with
* anything "real".
*/
#define FLASH_DEVICE_ID 100
#define FLASH_DEVICE_BASE CONFIG_FLASH_BASE_ADDRESS
#define FLASH_MAP_ENTRY_MAGIC 0xd00dbeef
struct flash_map_entry {
const uint32_t magic;
const struct flash_area area;
unsigned int ref_count;
};
/*
* The flash area describes essentially the partition table of the
* flash. In this case, it starts with FLASH_AREA_IMAGE_0.
*/
static struct flash_map_entry part_map[] = {
{
.magic = FLASH_MAP_ENTRY_MAGIC,
.area = {
.fa_id = FLASH_AREA_IMAGE_0,
.fa_device_id = FLASH_DEVICE_ID,
.fa_off = FLASH_AREA_IMAGE_0_OFFSET,
.fa_size = FLASH_AREA_IMAGE_0_SIZE,
},
},
{
.magic = FLASH_MAP_ENTRY_MAGIC,
.area = {
.fa_id = FLASH_AREA_IMAGE_1,
.fa_device_id = FLASH_DEVICE_ID,
.fa_off = FLASH_AREA_IMAGE_1_OFFSET,
.fa_size = FLASH_AREA_IMAGE_1_SIZE,
},
},
{
.magic = FLASH_MAP_ENTRY_MAGIC,
.area = {
.fa_id = FLASH_AREA_IMAGE_SCRATCH,
.fa_device_id = FLASH_DEVICE_ID,
.fa_off = FLASH_AREA_IMAGE_SCRATCH_OFFSET,
.fa_size = FLASH_AREA_IMAGE_SCRATCH_SIZE,
},
}
};
int flash_device_base(uint8_t fd_id, uintptr_t *ret)
{
if (fd_id != FLASH_DEVICE_ID) {
BOOT_LOG_ERR("invalid flash ID %d; expected %d",
fd_id, FLASH_DEVICE_ID);
return -EINVAL;
}
*ret = FLASH_DEVICE_BASE;
return 0;
}
/*
* `open` a flash area. The `area` in this case is not the individual
* sectors, but describes the particular flash area in question.
*/
int flash_area_open(uint8_t id, const struct flash_area **area)
{
int i;
BOOT_LOG_DBG("area %d", id);
for (i = 0; i < ARRAY_SIZE(part_map); i++) {
if (id == part_map[i].area.fa_id) {
break;
}
}
if (i == ARRAY_SIZE(part_map)) {
return -1;
}
*area = &part_map[i].area;
part_map[i].ref_count++;
return 0;
}
/*
* Nothing to do on close.
*/
void flash_area_close(const struct flash_area *area)
{
struct flash_map_entry *entry;
if (!area) {
return;
}
entry = CONTAINER_OF(area, struct flash_map_entry, area);
if (entry->magic != FLASH_MAP_ENTRY_MAGIC) {
BOOT_LOG_ERR("invalid area %p (id %u)", area, area->fa_id);
return;
}
if (entry->ref_count == 0) {
BOOT_LOG_ERR("area %u use count underflow", area->fa_id);
return;
}
entry->ref_count--;
}
void zephyr_flash_area_warn_on_open(void)
{
int i;
for (i = 0; i < ARRAY_SIZE(part_map); i++) {
struct flash_map_entry *entry = &part_map[i];
if (entry->ref_count) {
BOOT_LOG_WRN("area %u has %u users",
entry->area.fa_id, entry->ref_count);
}
}
}
int flash_area_read(const struct flash_area *area, uint32_t off, void *dst,
uint32_t len)
{
BOOT_LOG_DBG("area=%d, off=%x, len=%x", area->fa_id, off, len);
return flash_read(boot_flash_device, area->fa_off + off, dst, len);
}
int flash_area_write(const struct flash_area *area, uint32_t off, const void *src,
uint32_t len)
{
int rc = 0;
BOOT_LOG_DBG("area=%d, off=%x, len=%x", area->fa_id, off, len);
flash_write_protection_set(boot_flash_device, false);
rc = flash_write(boot_flash_device, area->fa_off + off, src, len);
flash_write_protection_set(boot_flash_device, true);
return rc;
}
int flash_area_erase(const struct flash_area *area, uint32_t off, uint32_t len)
{
int rc;
BOOT_LOG_DBG("area=%d, off=%x, len=%x", area->fa_id, off, len);
flash_write_protection_set(boot_flash_device, false);
rc = flash_erase(boot_flash_device, area->fa_off + off, len);
flash_write_protection_set(boot_flash_device, true);
return rc;
}
uint8_t flash_area_align(const struct flash_area *area)
{
return hal_flash_align(area->fa_id);
}
/*
* This depends on the mappings defined in sysflash.h, and assumes
* that slot 0, slot 1, and the scratch area area contiguous.
*/
int flash_area_id_from_image_slot(int slot)
{
return slot + FLASH_AREA_IMAGE_0;
}
/*
* This is used by the legacy file as well; don't mark it static until
* that file is removed.
*/
int flash_area_get_bounds(int idx, uint32_t *off, uint32_t *len)
{
/*
* This simple layout has uniform slots, so just fill in the
* right one.
*/
if (idx < FLASH_AREA_IMAGE_0 || idx > FLASH_AREA_IMAGE_SCRATCH) {
return -1;
}
switch (idx) {
case FLASH_AREA_IMAGE_0:
*off = FLASH_AREA_IMAGE_0_OFFSET;
*len = FLASH_AREA_IMAGE_0_SIZE;
break;
case FLASH_AREA_IMAGE_1:
*off = FLASH_AREA_IMAGE_1_OFFSET;
*len = FLASH_AREA_IMAGE_1_SIZE;
break;
case FLASH_AREA_IMAGE_SCRATCH:
*off = FLASH_AREA_IMAGE_SCRATCH_OFFSET;
*len = FLASH_AREA_IMAGE_SCRATCH_SIZE;
break;
default:
BOOT_LOG_ERR("unknown flash area %d", idx);
return -1;
}
BOOT_LOG_DBG("area %d: offset=0x%x, length=0x%x", idx, *off, *len);
return 0;
}
/*
* The legacy fallbacks are used instead if the flash driver doesn't
* provide page layout support.
*/
#if defined(CONFIG_FLASH_PAGE_LAYOUT)
struct layout_data {
uint32_t area_idx;
uint32_t area_off;
uint32_t area_len;
void *ret; /* struct flash_area* or struct flash_sector* */
uint32_t ret_idx;
uint32_t ret_len;
int status;
};
/*
* Generic page layout discovery routine. This is kept separate to
* support both the deprecated flash_area_to_sectors() and the current
* flash_area_get_sectors(). A lot of this can be inlined once
* flash_area_to_sectors() is removed.
*/
static int flash_area_layout(int idx, int *cnt, void *ret,
flash_page_cb cb, struct layout_data *cb_data)
{
cb_data->area_idx = idx;
if (flash_area_get_bounds(idx, &cb_data->area_off, &cb_data->area_len)) {
return -1;
}
cb_data->ret = ret;
cb_data->ret_idx = 0;
cb_data->ret_len = *cnt;
cb_data->status = 0;
flash_page_foreach(boot_flash_device, cb, cb_data);
if (cb_data->status == 0) {
*cnt = cb_data->ret_idx;
}
return cb_data->status;
}
/*
* Check if a flash_page_foreach() callback should exit early, due to
* one of the following conditions:
*
* - The flash page described by "info" is before the area of interest
* described in "data"
* - The flash page is after the end of the area
* - There are too many flash pages on the device to fit in the array
* held in data->ret. In this case, data->status is set to -ENOMEM.
*
* The value to return to flash_page_foreach() is stored in
* "bail_value" if the callback should exit early.
*/
static bool should_bail(const struct flash_pages_info *info,
struct layout_data *data,
bool *bail_value)
{
if (info->start_offset < data->area_off) {
*bail_value = true;
return true;
} else if (info->start_offset >= data->area_off + data->area_len) {
*bail_value = false;
return true;
} else if (data->ret_idx >= data->ret_len) {
data->status = -ENOMEM;
*bail_value = false;
return true;
}
return false;
}
static bool to_sectors_cb(const struct flash_pages_info *info, void *datav)
{
struct layout_data *data = datav;
struct flash_area *ret = data->ret;
bool bail;
if (should_bail(info, data, &bail)) {
return bail;
}
ret[data->ret_idx].fa_id = data->area_idx;
ret[data->ret_idx].fa_device_id = 0;
ret[data->ret_idx].pad16 = 0;
ret[data->ret_idx].fa_off = info->start_offset;
ret[data->ret_idx].fa_size = info->size;
data->ret_idx++;
return true;
}
int flash_area_to_sectors(int idx, int *cnt, struct flash_area *ret)
{
struct layout_data data;
return flash_area_layout(idx, cnt, ret, to_sectors_cb, &data);
}
static bool get_sectors_cb(const struct flash_pages_info *info, void *datav)
{
struct layout_data *data = datav;
struct flash_sector *ret = data->ret;
bool bail;
if (should_bail(info, data, &bail)) {
return bail;
}
ret[data->ret_idx].fs_off = info->start_offset - data->area_off;
ret[data->ret_idx].fs_size = info->size;
data->ret_idx++;
return true;
}
int flash_area_get_sectors(int idx, uint32_t *cnt, struct flash_sector *ret)
{
struct layout_data data;
return flash_area_layout(idx, cnt, ret, get_sectors_cb, &data);
}
#endif /* defined(CONFIG_FLASH_PAGE_LAYOUT) */