George Beckstein | d82afbf | 2020-10-29 17:32:11 -0400 | [diff] [blame] | 1 | /* |
| 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 Beckstein | 2877965 | 2020-12-15 13:54:51 -0500 | [diff] [blame^] | 30 | #include "bootutil_priv.h" |
| 31 | |
George Beckstein | d82afbf | 2020-10-29 17:32:11 -0400 | [diff] [blame] | 32 | #define FLASH_DEVICE_INTERNAL_FLASH 0 |
| 33 | #define FLASH_AREAS 3 |
| 34 | |
| 35 | /** Application defined secondary block device */ |
| 36 | mbed::BlockDevice* mcuboot_secondary_bd = get_secondary_bd(); |
| 37 | |
| 38 | /** Internal application block device */ |
| 39 | static FlashIAPBlockDevice mcuboot_primary_bd(MCUBOOT_PRIMARY_SLOT_START_ADDR, MCUBOOT_SLOT_SIZE); |
| 40 | |
George Beckstein | 2877965 | 2020-12-15 13:54:51 -0500 | [diff] [blame^] | 41 | #if MCUBOOT_SWAP_USING_SCRATCH |
George Beckstein | d82afbf | 2020-10-29 17:32:11 -0400 | [diff] [blame] | 42 | /** Scratch space is at the end of internal flash, after the main application */ |
| 43 | static FlashIAPBlockDevice mcuboot_scratch_bd(MCUBOOT_SCRATCH_START_ADDR, MCUBOOT_SCRATCH_SIZE); |
| 44 | #endif |
| 45 | |
| 46 | static 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 Beckstein | 2877965 | 2020-12-15 13:54:51 -0500 | [diff] [blame^] | 49 | #if MCUBOOT_SWAP_USING_SCRATCH |
George Beckstein | d82afbf | 2020-10-29 17:32:11 -0400 | [diff] [blame] | 50 | (mbed::BlockDevice*) &mcuboot_scratch_bd /** Scratch space for swapping images */ |
| 51 | #else |
| 52 | nullptr |
| 53 | #endif |
| 54 | }; |
| 55 | |
| 56 | static struct flash_area flash_areas[FLASH_AREAS]; |
| 57 | |
George Beckstein | d1233e1 | 2020-12-02 01:57:30 -0500 | [diff] [blame] | 58 | static unsigned int open_count[FLASH_AREAS] = {0}; |
| 59 | |
George Beckstein | d82afbf | 2020-10-29 17:32:11 -0400 | [diff] [blame] | 60 | int 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 Beckstein | 2877965 | 2020-12-15 13:54:51 -0500 | [diff] [blame^] | 71 | #if MCUBOOT_DIRECT_XIP |
| 72 | fap->fa_off = MBED_CONF_MCUBOOT_XIP_SECONDARY_SLOT_ADDRESS; |
| 73 | #else |
George Beckstein | d82afbf | 2020-10-29 17:32:11 -0400 | [diff] [blame] | 74 | fap->fa_off = 0; |
George Beckstein | 2877965 | 2020-12-15 13:54:51 -0500 | [diff] [blame^] | 75 | #endif |
George Beckstein | d82afbf | 2020-10-29 17:32:11 -0400 | [diff] [blame] | 76 | break; |
George Beckstein | 2877965 | 2020-12-15 13:54:51 -0500 | [diff] [blame^] | 77 | #if MCUBOOT_SWAP_USING_SCRATCH |
George Beckstein | d82afbf | 2020-10-29 17:32:11 -0400 | [diff] [blame] | 78 | 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 Beckstein | d1233e1 | 2020-12-02 01:57:30 -0500 | [diff] [blame] | 87 | open_count[id]++; |
| 88 | MCUBOOT_LOG_DBG("flash area %d open count: %d (+)", id, open_count[id]); |
| 89 | |
George Beckstein | d82afbf | 2020-10-29 17:32:11 -0400 | [diff] [blame] | 90 | 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 Beckstein | d1233e1 | 2020-12-02 01:57:30 -0500 | [diff] [blame] | 95 | |
| 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 Beckstein | d82afbf | 2020-10-29 17:32:11 -0400 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | void flash_area_close(const struct flash_area* fap) { |
George Beckstein | d1233e1 | 2020-12-02 01:57:30 -0500 | [diff] [blame] | 106 | 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 Beckstein | d82afbf | 2020-10-29 17:32:11 -0400 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | /* |
| 124 | * Read/write/erase. Offset is relative from beginning of flash area. |
| 125 | */ |
| 126 | int 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 Beckstein | d1233e1 | 2020-12-02 01:57:30 -0500 | [diff] [blame] | 129 | /* 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 Beckstein | d82afbf | 2020-10-29 17:32:11 -0400 | [diff] [blame] | 131 | #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 Beckstein | 2877965 | 2020-12-15 13:54:51 -0500 | [diff] [blame^] | 148 | 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 Beckstein | d82afbf | 2020-10-29 17:32:11 -0400 | [diff] [blame] | 150 | return -1; |
| 151 | } |
| 152 | else { |
| 153 | int ret = bd->read(dst, off, len); |
| 154 | if (ret != 0) { |
George Beckstein | 2877965 | 2020-12-15 13:54:51 -0500 | [diff] [blame^] | 155 | 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 Beckstein | d82afbf | 2020-10-29 17:32:11 -0400 | [diff] [blame] | 157 | 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 Beckstein | 2877965 | 2020-12-15 13:54:51 -0500 | [diff] [blame^] | 165 | 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 Beckstein | d82afbf | 2020-10-29 17:32:11 -0400 | [diff] [blame] | 167 | 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 | |
| 184 | int 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 | |
| 189 | int 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 | |
| 194 | uint8_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 | |
| 199 | uint8_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 | |
| 204 | int 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 Beckstein | d1233e1 | 2020-12-02 01:57:30 -0500 | [diff] [blame] | 207 | /* Loop through sectors and collect information on them */ |
George Beckstein | d82afbf | 2020-10-29 17:32:11 -0400 | [diff] [blame] | 208 | 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 | |
| 223 | int flash_area_id_from_image_slot(int slot) { |
| 224 | return slot; |
| 225 | } |
| 226 | |
| 227 | int flash_area_id_to_image_slot(int area_id) { |
| 228 | return area_id; |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Multi images support not implemented yet |
| 233 | */ |
| 234 | int flash_area_id_from_multi_image_slot(int image_index, int slot) |
| 235 | { |
| 236 | assert(image_index == 0); |
| 237 | return slot; |
| 238 | } |
| 239 | |
| 240 | int flash_area_id_to_multi_image_slot(int image_index, int area_id) |
| 241 | { |
| 242 | assert(image_index == 0); |
| 243 | return area_id; |
| 244 | } |