blob: 28afec835ffdf243bc28b77ab83b6cd8ffc5af92 [file] [log] [blame]
George Becksteind82afbf2020-10-29 17:32:11 -04001/*
2 * Copyright (c) 2020 Embedded Planet
3 * Copyright (c) 2020 ARM Limited
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * 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, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License
17 */
18
19#include <assert.h>
20#include <cstring>
21#include "flash_map_backend/flash_map_backend.h"
22#include "flash_map_backend/secondary_bd.h"
23#include "sysflash/sysflash.h"
24
25#include "blockdevice/BlockDevice.h"
26#include "FlashIAP/FlashIAPBlockDevice.h"
27
28#include "mcuboot_config/mcuboot_logging.h"
29
George Beckstein28779652020-12-15 13:54:51 -050030#include "bootutil_priv.h"
31
George Becksteind82afbf2020-10-29 17:32:11 -040032#define FLASH_DEVICE_INTERNAL_FLASH 0
33#define FLASH_AREAS 3
34
35/** Application defined secondary block device */
36mbed::BlockDevice* mcuboot_secondary_bd = get_secondary_bd();
37
38/** Internal application block device */
39static FlashIAPBlockDevice mcuboot_primary_bd(MCUBOOT_PRIMARY_SLOT_START_ADDR, MCUBOOT_SLOT_SIZE);
40
George Beckstein28779652020-12-15 13:54:51 -050041#if MCUBOOT_SWAP_USING_SCRATCH
George Becksteind82afbf2020-10-29 17:32:11 -040042/** Scratch space is at the end of internal flash, after the main application */
43static FlashIAPBlockDevice mcuboot_scratch_bd(MCUBOOT_SCRATCH_START_ADDR, MCUBOOT_SCRATCH_SIZE);
44#endif
45
46static mbed::BlockDevice* flash_map_bd[FLASH_AREAS] = {
47 (mbed::BlockDevice*) &mcuboot_primary_bd, /** Primary (loadable) image area */
48 mcuboot_secondary_bd, /** Secondary (update candidate) image area */
George Beckstein28779652020-12-15 13:54:51 -050049#if MCUBOOT_SWAP_USING_SCRATCH
George Becksteind82afbf2020-10-29 17:32:11 -040050 (mbed::BlockDevice*) &mcuboot_scratch_bd /** Scratch space for swapping images */
51#else
52 nullptr
53#endif
54};
55
56static struct flash_area flash_areas[FLASH_AREAS];
57
George Becksteind1233e12020-12-02 01:57:30 -050058static unsigned int open_count[FLASH_AREAS] = {0};
59
George Becksteind82afbf2020-10-29 17:32:11 -040060int flash_area_open(uint8_t id, const struct flash_area** fapp) {
61
62 *fapp = &flash_areas[id];
63 struct flash_area* fap = (struct flash_area*)*fapp;
64
65 // The offset of the slot is from the beginning of the flash device.
66 switch (id) {
67 case PRIMARY_ID:
68 fap->fa_off = MCUBOOT_PRIMARY_SLOT_START_ADDR;
69 break;
70 case SECONDARY_ID:
George Beckstein28779652020-12-15 13:54:51 -050071#if MCUBOOT_DIRECT_XIP
72 fap->fa_off = MBED_CONF_MCUBOOT_XIP_SECONDARY_SLOT_ADDRESS;
73#else
George Becksteind82afbf2020-10-29 17:32:11 -040074 fap->fa_off = 0;
George Beckstein28779652020-12-15 13:54:51 -050075#endif
George Becksteind82afbf2020-10-29 17:32:11 -040076 break;
George Beckstein28779652020-12-15 13:54:51 -050077#if MCUBOOT_SWAP_USING_SCRATCH
George Becksteind82afbf2020-10-29 17:32:11 -040078 case SCRATCH_ID:
79 fap->fa_off = MCUBOOT_SCRATCH_START_ADDR;
80 break;
81#endif
82 default:
83 MCUBOOT_LOG_ERR("flash_area_open, unknown id %d", id);
84 return -1;
85 }
86
George Becksteind1233e12020-12-02 01:57:30 -050087 open_count[id]++;
88 MCUBOOT_LOG_DBG("flash area %d open count: %d (+)", id, open_count[id]);
89
George Becksteind82afbf2020-10-29 17:32:11 -040090 fap->fa_id = id;
91 fap->fa_device_id = 0; // not relevant
92
93 mbed::BlockDevice* bd = flash_map_bd[id];
94 fap->fa_size = (uint32_t) bd->size();
George Becksteind1233e12020-12-02 01:57:30 -050095
96 /* Only initialize if this isn't a nested call to open the flash area */
97 if (open_count[id] == 1) {
98 MCUBOOT_LOG_DBG("initializing flash area %d...", id);
99 return bd->init();
100 } else {
101 return 0;
102 }
George Becksteind82afbf2020-10-29 17:32:11 -0400103}
104
105void flash_area_close(const struct flash_area* fap) {
George Becksteind1233e12020-12-02 01:57:30 -0500106 uint8_t id = fap->fa_id;
107 /* No need to close an unopened flash area, avoid an overflow of the counter */
108 if (!open_count[id]) {
109 return;
110 }
111
112 open_count[id]--;
113 MCUBOOT_LOG_DBG("flash area %d open count: %d (-)", id, open_count[id]);
114 if (!open_count[id]) {
115 /* mcuboot is not currently consistent in opening/closing flash areas only once at a time
116 * so only deinitialize the BlockDevice if all callers have closed the flash area. */
117 MCUBOOT_LOG_DBG("deinitializing flash area block device %d...", id);
118 mbed::BlockDevice* bd = flash_map_bd[id];
119 bd->deinit();
120 }
George Becksteind82afbf2020-10-29 17:32:11 -0400121}
122
123/*
124 * Read/write/erase. Offset is relative from beginning of flash area.
125 */
126int flash_area_read(const struct flash_area* fap, uint32_t off, void* dst, uint32_t len) {
127 mbed::BlockDevice* bd = flash_map_bd[fap->fa_id];
128
George Becksteind1233e12020-12-02 01:57:30 -0500129 /* Note: The address must be aligned to bd->get_read_size(). If MCUBOOT_READ_GRANULARITY
130 is defined, the length does not need to be aligned. */
George Becksteind82afbf2020-10-29 17:32:11 -0400131#ifdef MCUBOOT_READ_GRANULARITY
132 uint32_t read_size = bd->get_read_size();
133 if (read_size == 0) {
134 MCUBOOT_LOG_ERR("Invalid read size: must be non-zero");
135 return -1;
136 }
137 if (MCUBOOT_READ_GRANULARITY < read_size) {
138 MCUBOOT_LOG_ERR("Please increase MCUBOOT_READ_GRANULARITY (currently %u) to be at least %u",
139 MCUBOOT_READ_GRANULARITY, read_size);
140 return -1;
141 }
142
143 uint32_t remainder = len % read_size;
144 len -= remainder;
145 if (len != 0) {
146#endif
147 if (!bd->is_valid_read(off, len)) {
George Beckstein28779652020-12-15 13:54:51 -0500148 MCUBOOT_LOG_ERR("Invalid read: fa_id %d offset 0x%x len 0x%x", fap->fa_id,
149 (unsigned int) off, (unsigned int) len);
George Becksteind82afbf2020-10-29 17:32:11 -0400150 return -1;
151 }
152 else {
153 int ret = bd->read(dst, off, len);
154 if (ret != 0) {
George Beckstein28779652020-12-15 13:54:51 -0500155 MCUBOOT_LOG_ERR("Read failed: fa_id %d offset 0x%x len 0x%x", fap->fa_id,
156 (unsigned int) off, (unsigned int) len);
George Becksteind82afbf2020-10-29 17:32:11 -0400157 return ret;
158 }
159 }
160#ifdef MCUBOOT_READ_GRANULARITY
161 }
162
163 if (remainder) {
164 if (!bd->is_valid_read(off + len, read_size)) {
George Beckstein28779652020-12-15 13:54:51 -0500165 MCUBOOT_LOG_ERR("Invalid read: fa_id %d offset 0x%x len 0x%x", fap->fa_id,
166 (unsigned int) (off + len), (unsigned int) read_size);
George Becksteind82afbf2020-10-29 17:32:11 -0400167 return -1;
168 }
169 else {
170 uint8_t buffer[MCUBOOT_READ_GRANULARITY];
171 int ret = bd->read(buffer, off + len, read_size);
172 if (ret != 0) {
173 MCUBOOT_LOG_ERR("Read failed: %d", ret);
174 return ret;
175 }
176 memcpy((uint8_t *)dst + len, buffer, remainder);
177 }
178 }
179#endif
180
181 return 0;
182}
183
184int flash_area_write(const struct flash_area* fap, uint32_t off, const void* src, uint32_t len) {
185 mbed::BlockDevice* bd = flash_map_bd[fap->fa_id];
186 return bd->program(src, off, len);
187}
188
189int flash_area_erase(const struct flash_area* fap, uint32_t off, uint32_t len) {
190 mbed::BlockDevice* bd = flash_map_bd[fap->fa_id];
191 return bd->erase(off, len);
192}
193
194uint8_t flash_area_align(const struct flash_area* fap) {
195 mbed::BlockDevice* bd = flash_map_bd[fap->fa_id];
196 return bd->get_program_size();
197}
198
199uint8_t flash_area_erased_val(const struct flash_area* fap) {
200 mbed::BlockDevice* bd = flash_map_bd[fap->fa_id];
201 return bd->get_erase_value();
202}
203
204int flash_area_get_sectors(int fa_id, uint32_t* count, struct flash_sector* sectors) {
205 mbed::BlockDevice* bd = flash_map_bd[fa_id];
206
George Becksteind1233e12020-12-02 01:57:30 -0500207 /* Loop through sectors and collect information on them */
George Becksteind82afbf2020-10-29 17:32:11 -0400208 bd_addr_t offset = 0;
209 *count = 0;
210 while (*count < MCUBOOT_MAX_IMG_SECTORS && bd->is_valid_read(offset, bd->get_read_size())) {
211
212 sectors[*count].fs_off = offset;
213 bd_size_t erase_size = bd->get_erase_size(offset);
214 sectors[*count].fs_size = erase_size;
215
216 offset += erase_size;
217 *count += 1;
218 }
219
220 return 0;
221}
222
223int flash_area_id_from_image_slot(int slot) {
224 return slot;
225}
226
227int flash_area_id_to_image_slot(int area_id) {
228 return area_id;
229}
230
231/**
232 * Multi images support not implemented yet
233 */
234int flash_area_id_from_multi_image_slot(int image_index, int slot)
235{
236 assert(image_index == 0);
237 return slot;
238}
239
240int flash_area_id_to_multi_image_slot(int image_index, int area_id)
241{
242 assert(image_index == 0);
243 return area_id;
244}