blob: e894699c536b5ff24d58944cde5b0876b2ae9af3 [file] [log] [blame]
David Brown5153bd62017-01-06 11:16:53 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20#include <zephyr.h>
David Brown5153bd62017-01-06 11:16:53 -070021#include <flash.h>
22
Marti Bolivar51181cf2017-03-20 11:03:41 -040023#include "target.h"
Ricardo Salveti3a2c1242017-01-19 10:22:35 -020024
David Brown5153bd62017-01-06 11:16:53 -070025#include <flash_map/flash_map.h>
26#include <hal/hal_flash.h>
27#include <sysflash/sysflash.h>
28
Marti Bolivar4a97b4c2017-01-31 18:20:02 -050029#define BOOT_LOG_LEVEL BOOT_LOG_LEVEL_INFO
30#include "bootutil/bootutil_log.h"
Ricardo Salveti7cf3d9e2017-01-18 16:38:22 -020031
David Brown5153bd62017-01-06 11:16:53 -070032extern struct device *boot_flash_device;
33
34/*
Marti Bolivared2eaf12017-06-14 09:46:53 -040035 * For now, we only support one flash device.
36 *
37 * Pick a random device ID for it that's unlikely to collide with
38 * anything "real".
39 */
40#define FLASH_DEVICE_ID 100
41#define FLASH_DEVICE_BASE CONFIG_FLASH_BASE_ADDRESS
42
Marti Bolivar83a3cef2017-05-05 11:59:49 -040043#define FLASH_MAP_ENTRY_MAGIC 0xd00dbeef
44
45struct flash_map_entry {
46 const uint32_t magic;
47 const struct flash_area area;
48 unsigned int ref_count;
49};
50
Marti Bolivared2eaf12017-06-14 09:46:53 -040051/*
David Brown5153bd62017-01-06 11:16:53 -070052 * The flash area describes essentially the partition table of the
53 * flash. In this case, it starts with FLASH_AREA_IMAGE_0.
54 */
Marti Bolivar83a3cef2017-05-05 11:59:49 -040055static struct flash_map_entry part_map[] = {
David Brown0d0652a2017-04-11 17:33:30 -060056 {
Marti Bolivar83a3cef2017-05-05 11:59:49 -040057 .magic = FLASH_MAP_ENTRY_MAGIC,
58 .area = {
59 .fa_id = FLASH_AREA_IMAGE_0,
60 .fa_device_id = FLASH_DEVICE_ID,
61 .fa_off = FLASH_AREA_IMAGE_0_OFFSET,
62 .fa_size = FLASH_AREA_IMAGE_0_SIZE,
63 },
David Brown0d0652a2017-04-11 17:33:30 -060064 },
65 {
Marti Bolivar83a3cef2017-05-05 11:59:49 -040066 .magic = FLASH_MAP_ENTRY_MAGIC,
67 .area = {
68 .fa_id = FLASH_AREA_IMAGE_1,
69 .fa_device_id = FLASH_DEVICE_ID,
70 .fa_off = FLASH_AREA_IMAGE_1_OFFSET,
71 .fa_size = FLASH_AREA_IMAGE_1_SIZE,
72 },
David Brown0d0652a2017-04-11 17:33:30 -060073 },
74 {
Marti Bolivar83a3cef2017-05-05 11:59:49 -040075 .magic = FLASH_MAP_ENTRY_MAGIC,
76 .area = {
77 .fa_id = FLASH_AREA_IMAGE_SCRATCH,
78 .fa_device_id = FLASH_DEVICE_ID,
79 .fa_off = FLASH_AREA_IMAGE_SCRATCH_OFFSET,
80 .fa_size = FLASH_AREA_IMAGE_SCRATCH_SIZE,
81 },
82 }
David Brown5153bd62017-01-06 11:16:53 -070083};
84
Marti Bolivared2eaf12017-06-14 09:46:53 -040085int flash_device_base(uint8_t fd_id, uintptr_t *ret)
86{
87 if (fd_id != FLASH_DEVICE_ID) {
88 BOOT_LOG_ERR("invalid flash ID %d; expected %d",
89 fd_id, FLASH_DEVICE_ID);
90 return -EINVAL;
91 }
92 *ret = FLASH_DEVICE_BASE;
93 return 0;
94}
95
David Brown5153bd62017-01-06 11:16:53 -070096/*
David Brown5153bd62017-01-06 11:16:53 -070097 * `open` a flash area. The `area` in this case is not the individual
98 * sectors, but describes the particular flash area in question.
99 */
100int flash_area_open(uint8_t id, const struct flash_area **area)
101{
David Brown0d0652a2017-04-11 17:33:30 -0600102 int i;
Ricardo Salveti7cf3d9e2017-01-18 16:38:22 -0200103
David Brown0d0652a2017-04-11 17:33:30 -0600104 BOOT_LOG_DBG("area %d", id);
David Brown5153bd62017-01-06 11:16:53 -0700105
David Brown0d0652a2017-04-11 17:33:30 -0600106 for (i = 0; i < ARRAY_SIZE(part_map); i++) {
Marti Bolivar83a3cef2017-05-05 11:59:49 -0400107 if (id == part_map[i].area.fa_id) {
David Brown0d0652a2017-04-11 17:33:30 -0600108 break;
David Brown74b3c582017-04-11 17:39:25 -0600109 }
David Brown0d0652a2017-04-11 17:33:30 -0600110 }
David Brown74b3c582017-04-11 17:39:25 -0600111 if (i == ARRAY_SIZE(part_map)) {
David Brown0d0652a2017-04-11 17:33:30 -0600112 return -1;
David Brown74b3c582017-04-11 17:39:25 -0600113 }
David Brown5153bd62017-01-06 11:16:53 -0700114
Marti Bolivar83a3cef2017-05-05 11:59:49 -0400115 *area = &part_map[i].area;
116 part_map[i].ref_count++;
David Brown0d0652a2017-04-11 17:33:30 -0600117 return 0;
David Brown5153bd62017-01-06 11:16:53 -0700118}
119
120/*
121 * Nothing to do on close.
122 */
123void flash_area_close(const struct flash_area *area)
124{
Fabio Utzige7686262017-06-28 09:26:54 -0300125 struct flash_map_entry *entry;
126
127 if (!area) {
128 return;
129 }
130
131 entry = CONTAINER_OF(area, struct flash_map_entry, area);
Marti Bolivar83a3cef2017-05-05 11:59:49 -0400132 if (entry->magic != FLASH_MAP_ENTRY_MAGIC) {
133 BOOT_LOG_ERR("invalid area %p (id %u)", area, area->fa_id);
134 return;
135 }
136 if (entry->ref_count == 0) {
137 BOOT_LOG_ERR("area %u use count underflow", area->fa_id);
138 return;
139 }
140 entry->ref_count--;
141}
142
143void zephyr_flash_area_warn_on_open(void)
144{
145 int i;
146
147 for (i = 0; i < ARRAY_SIZE(part_map); i++) {
148 struct flash_map_entry *entry = &part_map[i];
149 if (entry->ref_count) {
150 BOOT_LOG_WRN("area %u has %u users",
151 entry->area.fa_id, entry->ref_count);
152 }
153 }
David Brown5153bd62017-01-06 11:16:53 -0700154}
155
156int flash_area_read(const struct flash_area *area, uint32_t off, void *dst,
David Brown0d0652a2017-04-11 17:33:30 -0600157 uint32_t len)
David Brown5153bd62017-01-06 11:16:53 -0700158{
David Brown0d0652a2017-04-11 17:33:30 -0600159 BOOT_LOG_DBG("area=%d, off=%x, len=%x", area->fa_id, off, len);
160 return flash_read(boot_flash_device, area->fa_off + off, dst, len);
David Brown5153bd62017-01-06 11:16:53 -0700161}
162
163int flash_area_write(const struct flash_area *area, uint32_t off, const void *src,
David Brown0d0652a2017-04-11 17:33:30 -0600164 uint32_t len)
David Brown5153bd62017-01-06 11:16:53 -0700165{
David Brown0d0652a2017-04-11 17:33:30 -0600166 int rc = 0;
Michael Scotte12746c2017-01-31 16:07:08 -0800167
David Brown0d0652a2017-04-11 17:33:30 -0600168 BOOT_LOG_DBG("area=%d, off=%x, len=%x", area->fa_id, off, len);
169 flash_write_protection_set(boot_flash_device, false);
170 rc = flash_write(boot_flash_device, area->fa_off + off, src, len);
171 flash_write_protection_set(boot_flash_device, true);
172 return rc;
David Brown5153bd62017-01-06 11:16:53 -0700173}
174
175int flash_area_erase(const struct flash_area *area, uint32_t off, uint32_t len)
176{
David Brown0d0652a2017-04-11 17:33:30 -0600177 int rc;
Marti Bolivar53cfdb92017-02-10 15:05:22 -0500178
David Brown0d0652a2017-04-11 17:33:30 -0600179 BOOT_LOG_DBG("area=%d, off=%x, len=%x", area->fa_id, off, len);
180 flash_write_protection_set(boot_flash_device, false);
181 rc = flash_erase(boot_flash_device, area->fa_off + off, len);
182 flash_write_protection_set(boot_flash_device, true);
183 return rc;
David Brown5153bd62017-01-06 11:16:53 -0700184}
185
186uint8_t flash_area_align(const struct flash_area *area)
187{
David Brown0d0652a2017-04-11 17:33:30 -0600188 return hal_flash_align(area->fa_id);
David Brown5153bd62017-01-06 11:16:53 -0700189}
190
191/*
192 * This depends on the mappings defined in sysflash.h, and assumes
193 * that slot 0, slot 1, and the scratch area area contiguous.
194 */
195int flash_area_id_from_image_slot(int slot)
196{
David Brown0d0652a2017-04-11 17:33:30 -0600197 return slot + FLASH_AREA_IMAGE_0;
David Brown5153bd62017-01-06 11:16:53 -0700198}
199
Marti Bolivar29d3a772017-03-20 10:44:25 -0400200#ifndef FLASH_AREA_IMAGE_SECTOR_SIZE
201#warning "Missing FLASH_AREA_IMAGE_SECTOR_SIZE; assuming scratch size instead"
202#define FLASH_AREA_IMAGE_SECTOR_SIZE FLASH_AREA_IMAGE_SCRATCH_SIZE
203#endif
204
Marti Bolivar1c0ddca2017-06-14 09:51:43 -0400205static int validate_idx(int idx, uint32_t *off, uint32_t *len)
David Brown5153bd62017-01-06 11:16:53 -0700206{
David Brown0d0652a2017-04-11 17:33:30 -0600207 /*
208 * This simple layout has uniform slots, so just fill in the
209 * right one.
210 */
David Brown74b3c582017-04-11 17:39:25 -0600211 if (idx < FLASH_AREA_IMAGE_0 || idx > FLASH_AREA_IMAGE_SCRATCH) {
David Brown0d0652a2017-04-11 17:33:30 -0600212 return -1;
David Brown74b3c582017-04-11 17:39:25 -0600213 }
David Brown5153bd62017-01-06 11:16:53 -0700214
David Brown0d0652a2017-04-11 17:33:30 -0600215 switch (idx) {
216 case FLASH_AREA_IMAGE_0:
Marti Bolivar1c0ddca2017-06-14 09:51:43 -0400217 *off = FLASH_AREA_IMAGE_0_OFFSET;
218 *len = FLASH_AREA_IMAGE_0_SIZE;
David Brown1d9f1852017-05-23 10:32:22 -0600219 break;
David Brown0d0652a2017-04-11 17:33:30 -0600220 case FLASH_AREA_IMAGE_1:
Marti Bolivar1c0ddca2017-06-14 09:51:43 -0400221 *off = FLASH_AREA_IMAGE_1_OFFSET;
222 *len = FLASH_AREA_IMAGE_1_SIZE;
David Brown1d9f1852017-05-23 10:32:22 -0600223 break;
David Brown0d0652a2017-04-11 17:33:30 -0600224 case FLASH_AREA_IMAGE_SCRATCH:
Marti Bolivar1c0ddca2017-06-14 09:51:43 -0400225 *off = FLASH_AREA_IMAGE_SCRATCH_OFFSET;
226 *len = FLASH_AREA_IMAGE_SCRATCH_SIZE;
David Brown1d9f1852017-05-23 10:32:22 -0600227 break;
David Brown0d0652a2017-04-11 17:33:30 -0600228 default:
229 BOOT_LOG_ERR("unknown flash area %d", idx);
230 return -1;
231 }
David Brown5153bd62017-01-06 11:16:53 -0700232
David Brown0d0652a2017-04-11 17:33:30 -0600233 BOOT_LOG_DBG("area %d: offset=0x%x, length=0x%x, sector size=0x%x",
Marti Bolivar1c0ddca2017-06-14 09:51:43 -0400234 idx, *off, *len, FLASH_AREA_IMAGE_SECTOR_SIZE);
235 return 0;
236}
237
238int flash_area_to_sectors(int idx, int *cnt, struct flash_area *ret)
239{
240 uint32_t off;
241 uint32_t len;
242 uint32_t max_cnt = *cnt;
243 uint32_t rem_len;
244
245 if (validate_idx(idx, &off, &len)) {
246 return -1;
247 }
248
249 if (*cnt < 1) {
250 return -1;
251 }
Marti Bolivar29d3a772017-03-20 10:44:25 -0400252
David Brown0d0652a2017-04-11 17:33:30 -0600253 rem_len = len;
254 *cnt = 0;
255 while (rem_len > 0 && *cnt < max_cnt) {
256 if (rem_len < FLASH_AREA_IMAGE_SECTOR_SIZE) {
257 BOOT_LOG_ERR("area %d size 0x%x not divisible by sector size 0x%x",
258 idx, len, FLASH_AREA_IMAGE_SECTOR_SIZE);
259 return -1;
260 }
Marti Bolivar29d3a772017-03-20 10:44:25 -0400261
David Brown0d0652a2017-04-11 17:33:30 -0600262 ret[*cnt].fa_id = idx;
263 ret[*cnt].fa_device_id = 0;
264 ret[*cnt].pad16 = 0;
265 ret[*cnt].fa_off = off + (FLASH_AREA_IMAGE_SECTOR_SIZE * (*cnt));
266 ret[*cnt].fa_size = FLASH_AREA_IMAGE_SECTOR_SIZE;
267 *cnt = *cnt + 1;
268 rem_len -= FLASH_AREA_IMAGE_SECTOR_SIZE;
269 }
Marti Bolivar29d3a772017-03-20 10:44:25 -0400270
David Brown0d0652a2017-04-11 17:33:30 -0600271 if (*cnt >= max_cnt) {
272 BOOT_LOG_ERR("flash area %d sector count overflow", idx);
273 return -1;
274 }
David Brown5153bd62017-01-06 11:16:53 -0700275
David Brown0d0652a2017-04-11 17:33:30 -0600276 return 0;
David Brown5153bd62017-01-06 11:16:53 -0700277}
Marti Bolivar1c0ddca2017-06-14 09:51:43 -0400278
279/*
280 * Lookup the sector map for a given flash area. This should fill in
281 * `ret` with all of the sectors in the area. `*cnt` will be set to
282 * the storage at `ret` and should be set to the final number of
283 * sectors in this area.
284 */
285int flash_area_get_sectors(int idx, uint32_t *cnt, struct flash_sector *ret)
286{
287 uint32_t off;
288 uint32_t len;
289 uint32_t max_cnt = *cnt;
290 uint32_t rem_len;
291
292 if (validate_idx(idx, &off, &len)) {
293 return -1;
294 }
295
296 if (*cnt < 1) {
297 return -1;
298 }
299
300 rem_len = len;
301 *cnt = 0;
302 while (rem_len > 0 && *cnt < max_cnt) {
303 if (rem_len < FLASH_AREA_IMAGE_SECTOR_SIZE) {
304 BOOT_LOG_ERR("area %d size 0x%x not divisible by sector size 0x%x",
305 idx, len, FLASH_AREA_IMAGE_SECTOR_SIZE);
306 return -1;
307 }
308
309 ret[*cnt].fs_off = FLASH_AREA_IMAGE_SECTOR_SIZE * (*cnt);
310 ret[*cnt].fs_size = FLASH_AREA_IMAGE_SECTOR_SIZE;
311 *cnt = *cnt + 1;
312 rem_len -= FLASH_AREA_IMAGE_SECTOR_SIZE;
313 }
314
315 if (*cnt >= max_cnt) {
316 BOOT_LOG_ERR("flash area %d sector count overflow", idx);
317 return -1;
318 }
319
320 return 0;
321}