Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame^] | 20 | /* |
| 21 | * Original code taken from mcuboot project at: |
| 22 | * https://github.com/JuulLabs-OSS/mcuboot |
| 23 | * Git SHA of the original version: b69841820462fa0227d7fb407620405f6426bb4b |
| 24 | * Modifications are Copyright (c) 2018-2019 Arm Limited. |
| 25 | */ |
| 26 | |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 27 | #include <errno.h> |
| 28 | #include <stdbool.h> |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 29 | |
| 30 | #include "target.h" |
Tamas Ban | c382885 | 2018-02-01 12:24:16 +0000 | [diff] [blame] | 31 | #include "bl2_util.h" |
| 32 | #include "Driver_Flash.h" |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 33 | |
| 34 | #include <flash_map/flash_map.h> |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 35 | |
| 36 | #define BOOT_LOG_LEVEL BOOT_LOG_LEVEL_INFO |
| 37 | #include "bootutil/bootutil_log.h" |
| 38 | |
Tamas Ban | c382885 | 2018-02-01 12:24:16 +0000 | [diff] [blame] | 39 | /* Flash device name must be specified by target */ |
| 40 | extern ARM_DRIVER_FLASH FLASH_DEV_NAME; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 41 | |
| 42 | /* |
| 43 | * For now, we only support one flash device. |
| 44 | * |
| 45 | * Pick a random device ID for it that's unlikely to collide with |
| 46 | * anything "real". |
| 47 | */ |
| 48 | #define FLASH_DEVICE_ID 100 |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 49 | #define FLASH_DEVICE_BASE FLASH_BASE_ADDRESS |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 50 | |
| 51 | #define FLASH_MAP_ENTRY_MAGIC 0xd00dbeef |
| 52 | |
| 53 | struct flash_map_entry { |
| 54 | const uint32_t magic; |
| 55 | const struct flash_area area; |
| 56 | unsigned int ref_count; |
| 57 | }; |
| 58 | |
| 59 | /* |
| 60 | * The flash area describes essentially the partition table of the |
| 61 | * flash. In this case, it starts with FLASH_AREA_IMAGE_0. |
| 62 | */ |
| 63 | static struct flash_map_entry part_map[] = { |
| 64 | { |
| 65 | .magic = FLASH_MAP_ENTRY_MAGIC, |
| 66 | .area = { |
| 67 | .fa_id = FLASH_AREA_IMAGE_0, |
| 68 | .fa_device_id = FLASH_DEVICE_ID, |
| 69 | .fa_off = FLASH_AREA_IMAGE_0_OFFSET, |
| 70 | .fa_size = FLASH_AREA_IMAGE_0_SIZE, |
| 71 | }, |
| 72 | }, |
| 73 | { |
| 74 | .magic = FLASH_MAP_ENTRY_MAGIC, |
| 75 | .area = { |
| 76 | .fa_id = FLASH_AREA_IMAGE_1, |
| 77 | .fa_device_id = FLASH_DEVICE_ID, |
| 78 | .fa_off = FLASH_AREA_IMAGE_1_OFFSET, |
| 79 | .fa_size = FLASH_AREA_IMAGE_1_SIZE, |
| 80 | }, |
| 81 | }, |
| 82 | { |
| 83 | .magic = FLASH_MAP_ENTRY_MAGIC, |
| 84 | .area = { |
| 85 | .fa_id = FLASH_AREA_IMAGE_SCRATCH, |
| 86 | .fa_device_id = FLASH_DEVICE_ID, |
| 87 | .fa_off = FLASH_AREA_IMAGE_SCRATCH_OFFSET, |
| 88 | .fa_size = FLASH_AREA_IMAGE_SCRATCH_SIZE, |
| 89 | }, |
| 90 | } |
| 91 | }; |
| 92 | |
| 93 | int flash_device_base(uint8_t fd_id, uintptr_t *ret) |
| 94 | { |
| 95 | if (fd_id != FLASH_DEVICE_ID) { |
| 96 | BOOT_LOG_ERR("invalid flash ID %d; expected %d", |
| 97 | fd_id, FLASH_DEVICE_ID); |
| 98 | return -EINVAL; |
| 99 | } |
| 100 | *ret = FLASH_DEVICE_BASE; |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | /* |
| 105 | * `open` a flash area. The `area` in this case is not the individual |
| 106 | * sectors, but describes the particular flash area in question. |
| 107 | */ |
| 108 | int flash_area_open(uint8_t id, const struct flash_area **area) |
| 109 | { |
| 110 | int i; |
| 111 | |
| 112 | BOOT_LOG_DBG("area %d", id); |
| 113 | |
| 114 | for (i = 0; i < ARRAY_SIZE(part_map); i++) { |
| 115 | if (id == part_map[i].area.fa_id) { |
| 116 | break; |
| 117 | } |
| 118 | } |
| 119 | if (i == ARRAY_SIZE(part_map)) { |
| 120 | return -1; |
| 121 | } |
| 122 | |
| 123 | *area = &part_map[i].area; |
| 124 | part_map[i].ref_count++; |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | /* |
| 129 | * Nothing to do on close. |
| 130 | */ |
| 131 | void flash_area_close(const struct flash_area *area) |
| 132 | { |
| 133 | struct flash_map_entry *entry; |
| 134 | |
| 135 | if (!area) { |
| 136 | return; |
| 137 | } |
| 138 | |
| 139 | entry = CONTAINER_OF(area, struct flash_map_entry, area); |
| 140 | if (entry->magic != FLASH_MAP_ENTRY_MAGIC) { |
| 141 | BOOT_LOG_ERR("invalid area %p (id %u)", area, area->fa_id); |
| 142 | return; |
| 143 | } |
| 144 | if (entry->ref_count == 0) { |
| 145 | BOOT_LOG_ERR("area %u use count underflow", area->fa_id); |
| 146 | return; |
| 147 | } |
| 148 | entry->ref_count--; |
| 149 | } |
| 150 | |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 151 | void flash_area_warn_on_open(void) |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 152 | { |
| 153 | int i; |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 154 | struct flash_map_entry *entry; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 155 | |
| 156 | for (i = 0; i < ARRAY_SIZE(part_map); i++) { |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 157 | entry = &part_map[i]; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 158 | if (entry->ref_count) { |
| 159 | BOOT_LOG_WRN("area %u has %u users", |
| 160 | entry->area.fa_id, entry->ref_count); |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame^] | 165 | uint8_t flash_area_erased_val(const struct flash_area *area) |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 166 | { |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame^] | 167 | (void)area; |
| 168 | |
| 169 | return FLASH_DEV_NAME.GetInfo()->erased_value; |
| 170 | } |
| 171 | |
| 172 | int flash_area_read(const struct flash_area *area, uint32_t off, void *dst, |
| 173 | uint32_t len) |
| 174 | { |
| 175 | BOOT_LOG_DBG("read area=%d, off=%#x, len=%#x", area->fa_id, off, len); |
Tamas Ban | c382885 | 2018-02-01 12:24:16 +0000 | [diff] [blame] | 176 | return FLASH_DEV_NAME.ReadData(area->fa_off + off, dst, len); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 177 | } |
| 178 | |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame^] | 179 | int flash_area_read_is_empty(const struct flash_area *area, uint32_t off, |
| 180 | void *dst, uint32_t len) |
| 181 | { |
| 182 | uint32_t i; |
| 183 | uint8_t *u8dst; |
| 184 | int rc; |
| 185 | |
| 186 | BOOT_LOG_DBG("read_is_empty area=%d, off=%#x, len=%#x", |
| 187 | area->fa_id, off, len); |
| 188 | |
| 189 | rc = FLASH_DEV_NAME.ReadData(area->fa_off + off, dst, len); |
| 190 | if(rc != 0) { |
| 191 | return -1; |
| 192 | } |
| 193 | |
| 194 | u8dst = (uint8_t*)dst; |
| 195 | |
| 196 | for (i = 0; i < len; i++) { |
| 197 | if (u8dst[i] != flash_area_erased_val(area)) { |
| 198 | return 0; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | return 1; |
| 203 | } |
| 204 | |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 205 | int flash_area_write(const struct flash_area *area, uint32_t off, |
| 206 | const void *src, uint32_t len) |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 207 | { |
Tamas Ban | c382885 | 2018-02-01 12:24:16 +0000 | [diff] [blame] | 208 | BOOT_LOG_DBG("write area=%d, off=%#x, len=%#x", area->fa_id, off, len); |
| 209 | return FLASH_DEV_NAME.ProgramData(area->fa_off + off, src, len); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | int flash_area_erase(const struct flash_area *area, uint32_t off, uint32_t len) |
| 213 | { |
Tamas Ban | c382885 | 2018-02-01 12:24:16 +0000 | [diff] [blame] | 214 | ARM_FLASH_INFO *flash_info; |
| 215 | uint32_t deleted_len = 0; |
| 216 | int32_t rc = 0; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 217 | |
Tamas Ban | c382885 | 2018-02-01 12:24:16 +0000 | [diff] [blame] | 218 | BOOT_LOG_DBG("erase area=%d, off=%#x, len=%#x", area->fa_id, off, len); |
| 219 | flash_info = FLASH_DEV_NAME.GetInfo(); |
| 220 | |
| 221 | if (flash_info->sector_info == NULL) { |
| 222 | /* Uniform sector layout */ |
| 223 | while (deleted_len < len) { |
| 224 | rc = FLASH_DEV_NAME.EraseSector(area->fa_off + off); |
| 225 | if (rc != 0) { |
| 226 | break; |
| 227 | } |
| 228 | deleted_len += flash_info->sector_size; |
| 229 | off += flash_info->sector_size; |
| 230 | } |
| 231 | } else { |
| 232 | /* Inhomogeneous sector layout, explicitly defined |
| 233 | * Currently not supported. |
| 234 | */ |
| 235 | } |
| 236 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 237 | return rc; |
| 238 | } |
| 239 | |
| 240 | uint8_t flash_area_align(const struct flash_area *area) |
| 241 | { |
Tamas Ban | c382885 | 2018-02-01 12:24:16 +0000 | [diff] [blame] | 242 | ARM_FLASH_INFO *flash_info; |
| 243 | |
| 244 | flash_info = FLASH_DEV_NAME.GetInfo(); |
| 245 | return flash_info->program_unit; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | /* |
| 249 | * This depends on the mappings defined in sysflash.h, and assumes |
| 250 | * that slot 0, slot 1, and the scratch area area contiguous. |
| 251 | */ |
| 252 | int flash_area_id_from_image_slot(int slot) |
| 253 | { |
| 254 | return slot + FLASH_AREA_IMAGE_0; |
| 255 | } |
| 256 | |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 257 | static int validate_idx(int idx, uint32_t *off, uint32_t *len) |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 258 | { |
| 259 | /* |
| 260 | * This simple layout has uniform slots, so just fill in the |
| 261 | * right one. |
| 262 | */ |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 263 | |
| 264 | switch (idx) { |
| 265 | case FLASH_AREA_IMAGE_0: |
| 266 | *off = FLASH_AREA_IMAGE_0_OFFSET; |
| 267 | *len = FLASH_AREA_IMAGE_0_SIZE; |
| 268 | break; |
| 269 | case FLASH_AREA_IMAGE_1: |
| 270 | *off = FLASH_AREA_IMAGE_1_OFFSET; |
| 271 | *len = FLASH_AREA_IMAGE_1_SIZE; |
| 272 | break; |
| 273 | case FLASH_AREA_IMAGE_SCRATCH: |
| 274 | *off = FLASH_AREA_IMAGE_SCRATCH_OFFSET; |
| 275 | *len = FLASH_AREA_IMAGE_SCRATCH_SIZE; |
| 276 | break; |
| 277 | default: |
| 278 | BOOT_LOG_ERR("unknown flash area %d", idx); |
| 279 | return -1; |
| 280 | } |
| 281 | |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 282 | BOOT_LOG_DBG("area %d: offset=0x%x, length=0x%x, sector size=0x%x", |
| 283 | idx, *off, *len, FLASH_AREA_IMAGE_SECTOR_SIZE); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 284 | return 0; |
| 285 | } |
| 286 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 287 | int flash_area_to_sectors(int idx, int *cnt, struct flash_area *ret) |
| 288 | { |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 289 | uint32_t off; |
| 290 | uint32_t len; |
| 291 | uint32_t max_cnt = *cnt; |
| 292 | uint32_t rem_len; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 293 | |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 294 | if (validate_idx(idx, &off, &len)) { |
| 295 | return -1; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 298 | if (*cnt < 1) { |
| 299 | return -1; |
| 300 | } |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 301 | |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 302 | rem_len = len; |
| 303 | *cnt = 0; |
| 304 | while (rem_len > 0 && *cnt < max_cnt) { |
| 305 | if (rem_len < FLASH_AREA_IMAGE_SECTOR_SIZE) { |
| 306 | BOOT_LOG_ERR("area %d size 0x%x not divisible by sector size 0x%x", |
| 307 | idx, len, FLASH_AREA_IMAGE_SECTOR_SIZE); |
| 308 | return -1; |
| 309 | } |
| 310 | |
| 311 | ret[*cnt].fa_id = idx; |
| 312 | ret[*cnt].fa_device_id = 0; |
| 313 | ret[*cnt].pad16 = 0; |
| 314 | ret[*cnt].fa_off = off + (FLASH_AREA_IMAGE_SECTOR_SIZE * (*cnt)); |
| 315 | ret[*cnt].fa_size = FLASH_AREA_IMAGE_SECTOR_SIZE; |
| 316 | *cnt = *cnt + 1; |
| 317 | rem_len -= FLASH_AREA_IMAGE_SECTOR_SIZE; |
| 318 | } |
| 319 | |
Tamas Ban | c382885 | 2018-02-01 12:24:16 +0000 | [diff] [blame] | 320 | if (*cnt > max_cnt) { |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 321 | BOOT_LOG_ERR("flash area %d sector count overflow", idx); |
| 322 | return -1; |
| 323 | } |
| 324 | |
| 325 | return 0; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 326 | } |
| 327 | |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 328 | /* |
| 329 | * Lookup the sector map for a given flash area. This should fill in |
| 330 | * `ret` with all of the sectors in the area. `*cnt` will be set to |
| 331 | * the storage at `ret` and should be set to the final number of |
| 332 | * sectors in this area. |
| 333 | */ |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 334 | int flash_area_get_sectors(int idx, uint32_t *cnt, struct flash_sector *ret) |
| 335 | { |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 336 | uint32_t off; |
| 337 | uint32_t len; |
| 338 | uint32_t max_cnt = *cnt; |
| 339 | uint32_t rem_len; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 340 | |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 341 | if (validate_idx(idx, &off, &len)) { |
| 342 | return -1; |
| 343 | } |
| 344 | |
| 345 | if (*cnt < 1) { |
| 346 | return -1; |
| 347 | } |
| 348 | |
| 349 | rem_len = len; |
| 350 | *cnt = 0; |
| 351 | while (rem_len > 0 && *cnt < max_cnt) { |
| 352 | if (rem_len < FLASH_AREA_IMAGE_SECTOR_SIZE) { |
| 353 | BOOT_LOG_ERR("area %d size 0x%x not divisible by sector size 0x%x", |
| 354 | idx, len, FLASH_AREA_IMAGE_SECTOR_SIZE); |
| 355 | return -1; |
| 356 | } |
| 357 | |
| 358 | ret[*cnt].fs_off = FLASH_AREA_IMAGE_SECTOR_SIZE * (*cnt); |
| 359 | ret[*cnt].fs_size = FLASH_AREA_IMAGE_SECTOR_SIZE; |
| 360 | *cnt = *cnt + 1; |
| 361 | rem_len -= FLASH_AREA_IMAGE_SECTOR_SIZE; |
| 362 | } |
| 363 | |
Tamas Ban | c382885 | 2018-02-01 12:24:16 +0000 | [diff] [blame] | 364 | if (*cnt > max_cnt) { |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 365 | BOOT_LOG_ERR("flash area %d sector count overflow", idx); |
| 366 | return -1; |
| 367 | } |
| 368 | |
| 369 | return 0; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 370 | } |