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 | |
| 30 | #define FLASH_DEVICE_INTERNAL_FLASH 0 |
| 31 | #define FLASH_AREAS 3 |
| 32 | |
| 33 | /** Application defined secondary block device */ |
| 34 | mbed::BlockDevice* mcuboot_secondary_bd = get_secondary_bd(); |
| 35 | |
| 36 | /** Internal application block device */ |
| 37 | static FlashIAPBlockDevice mcuboot_primary_bd(MCUBOOT_PRIMARY_SLOT_START_ADDR, MCUBOOT_SLOT_SIZE); |
| 38 | |
| 39 | #ifndef MCUBOOT_OVERWRITE_ONLY |
| 40 | /** Scratch space is at the end of internal flash, after the main application */ |
| 41 | static FlashIAPBlockDevice mcuboot_scratch_bd(MCUBOOT_SCRATCH_START_ADDR, MCUBOOT_SCRATCH_SIZE); |
| 42 | #endif |
| 43 | |
| 44 | static mbed::BlockDevice* flash_map_bd[FLASH_AREAS] = { |
| 45 | (mbed::BlockDevice*) &mcuboot_primary_bd, /** Primary (loadable) image area */ |
| 46 | mcuboot_secondary_bd, /** Secondary (update candidate) image area */ |
| 47 | #ifndef MCUBOOT_OVERWRITE_ONLY |
| 48 | (mbed::BlockDevice*) &mcuboot_scratch_bd /** Scratch space for swapping images */ |
| 49 | #else |
| 50 | nullptr |
| 51 | #endif |
| 52 | }; |
| 53 | |
| 54 | static struct flash_area flash_areas[FLASH_AREAS]; |
| 55 | |
George Beckstein | d1233e1 | 2020-12-02 01:57:30 -0500 | [diff] [blame] | 56 | static unsigned int open_count[FLASH_AREAS] = {0}; |
| 57 | |
George Beckstein | d82afbf | 2020-10-29 17:32:11 -0400 | [diff] [blame] | 58 | int flash_area_open(uint8_t id, const struct flash_area** fapp) { |
| 59 | |
| 60 | *fapp = &flash_areas[id]; |
| 61 | struct flash_area* fap = (struct flash_area*)*fapp; |
| 62 | |
| 63 | // The offset of the slot is from the beginning of the flash device. |
| 64 | switch (id) { |
| 65 | case PRIMARY_ID: |
| 66 | fap->fa_off = MCUBOOT_PRIMARY_SLOT_START_ADDR; |
| 67 | break; |
| 68 | case SECONDARY_ID: |
| 69 | // The offset of the secondary slot is not currently used. |
| 70 | fap->fa_off = 0; |
| 71 | break; |
| 72 | #ifndef MCUBOOT_OVERWRITE_ONLY |
| 73 | case SCRATCH_ID: |
| 74 | fap->fa_off = MCUBOOT_SCRATCH_START_ADDR; |
| 75 | break; |
| 76 | #endif |
| 77 | default: |
| 78 | MCUBOOT_LOG_ERR("flash_area_open, unknown id %d", id); |
| 79 | return -1; |
| 80 | } |
| 81 | |
George Beckstein | d1233e1 | 2020-12-02 01:57:30 -0500 | [diff] [blame] | 82 | open_count[id]++; |
| 83 | MCUBOOT_LOG_DBG("flash area %d open count: %d (+)", id, open_count[id]); |
| 84 | |
George Beckstein | d82afbf | 2020-10-29 17:32:11 -0400 | [diff] [blame] | 85 | fap->fa_id = id; |
| 86 | fap->fa_device_id = 0; // not relevant |
| 87 | |
| 88 | mbed::BlockDevice* bd = flash_map_bd[id]; |
| 89 | fap->fa_size = (uint32_t) bd->size(); |
George Beckstein | d1233e1 | 2020-12-02 01:57:30 -0500 | [diff] [blame] | 90 | |
| 91 | /* Only initialize if this isn't a nested call to open the flash area */ |
| 92 | if (open_count[id] == 1) { |
| 93 | MCUBOOT_LOG_DBG("initializing flash area %d...", id); |
| 94 | return bd->init(); |
| 95 | } else { |
| 96 | return 0; |
| 97 | } |
George Beckstein | d82afbf | 2020-10-29 17:32:11 -0400 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | void flash_area_close(const struct flash_area* fap) { |
George Beckstein | d1233e1 | 2020-12-02 01:57:30 -0500 | [diff] [blame] | 101 | uint8_t id = fap->fa_id; |
| 102 | /* No need to close an unopened flash area, avoid an overflow of the counter */ |
| 103 | if (!open_count[id]) { |
| 104 | return; |
| 105 | } |
| 106 | |
| 107 | open_count[id]--; |
| 108 | MCUBOOT_LOG_DBG("flash area %d open count: %d (-)", id, open_count[id]); |
| 109 | if (!open_count[id]) { |
| 110 | /* mcuboot is not currently consistent in opening/closing flash areas only once at a time |
| 111 | * so only deinitialize the BlockDevice if all callers have closed the flash area. */ |
| 112 | MCUBOOT_LOG_DBG("deinitializing flash area block device %d...", id); |
| 113 | mbed::BlockDevice* bd = flash_map_bd[id]; |
| 114 | bd->deinit(); |
| 115 | } |
George Beckstein | d82afbf | 2020-10-29 17:32:11 -0400 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | /* |
| 119 | * Read/write/erase. Offset is relative from beginning of flash area. |
| 120 | */ |
| 121 | int flash_area_read(const struct flash_area* fap, uint32_t off, void* dst, uint32_t len) { |
| 122 | mbed::BlockDevice* bd = flash_map_bd[fap->fa_id]; |
| 123 | |
George Beckstein | d1233e1 | 2020-12-02 01:57:30 -0500 | [diff] [blame] | 124 | /* Note: The address must be aligned to bd->get_read_size(). If MCUBOOT_READ_GRANULARITY |
| 125 | is defined, the length does not need to be aligned. */ |
George Beckstein | d82afbf | 2020-10-29 17:32:11 -0400 | [diff] [blame] | 126 | #ifdef MCUBOOT_READ_GRANULARITY |
| 127 | uint32_t read_size = bd->get_read_size(); |
| 128 | if (read_size == 0) { |
| 129 | MCUBOOT_LOG_ERR("Invalid read size: must be non-zero"); |
| 130 | return -1; |
| 131 | } |
| 132 | if (MCUBOOT_READ_GRANULARITY < read_size) { |
| 133 | MCUBOOT_LOG_ERR("Please increase MCUBOOT_READ_GRANULARITY (currently %u) to be at least %u", |
| 134 | MCUBOOT_READ_GRANULARITY, read_size); |
| 135 | return -1; |
| 136 | } |
| 137 | |
| 138 | uint32_t remainder = len % read_size; |
| 139 | len -= remainder; |
| 140 | if (len != 0) { |
| 141 | #endif |
| 142 | if (!bd->is_valid_read(off, len)) { |
| 143 | MCUBOOT_LOG_ERR("Invalid read: fa_id %d offset 0x%x len 0x%x", fap->fa_id, off, len); |
| 144 | return -1; |
| 145 | } |
| 146 | else { |
| 147 | int ret = bd->read(dst, off, len); |
| 148 | if (ret != 0) { |
George Beckstein | d1233e1 | 2020-12-02 01:57:30 -0500 | [diff] [blame] | 149 | MCUBOOT_LOG_ERR("Read failed: fa_id %d offset 0x%x len 0x%x (%d)", fap->fa_id, off, len, ret); |
George Beckstein | d82afbf | 2020-10-29 17:32:11 -0400 | [diff] [blame] | 150 | return ret; |
| 151 | } |
| 152 | } |
| 153 | #ifdef MCUBOOT_READ_GRANULARITY |
| 154 | } |
| 155 | |
| 156 | if (remainder) { |
| 157 | if (!bd->is_valid_read(off + len, read_size)) { |
| 158 | MCUBOOT_LOG_ERR("Invalid read: fa_id %d offset 0x%x len 0x%x", fap->fa_id, off + len, read_size); |
| 159 | return -1; |
| 160 | } |
| 161 | else { |
| 162 | uint8_t buffer[MCUBOOT_READ_GRANULARITY]; |
| 163 | int ret = bd->read(buffer, off + len, read_size); |
| 164 | if (ret != 0) { |
| 165 | MCUBOOT_LOG_ERR("Read failed: %d", ret); |
| 166 | return ret; |
| 167 | } |
| 168 | memcpy((uint8_t *)dst + len, buffer, remainder); |
| 169 | } |
| 170 | } |
| 171 | #endif |
| 172 | |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | int flash_area_write(const struct flash_area* fap, uint32_t off, const void* src, uint32_t len) { |
| 177 | mbed::BlockDevice* bd = flash_map_bd[fap->fa_id]; |
| 178 | return bd->program(src, off, len); |
| 179 | } |
| 180 | |
| 181 | int flash_area_erase(const struct flash_area* fap, uint32_t off, uint32_t len) { |
| 182 | mbed::BlockDevice* bd = flash_map_bd[fap->fa_id]; |
| 183 | return bd->erase(off, len); |
| 184 | } |
| 185 | |
| 186 | uint8_t flash_area_align(const struct flash_area* fap) { |
| 187 | mbed::BlockDevice* bd = flash_map_bd[fap->fa_id]; |
| 188 | return bd->get_program_size(); |
| 189 | } |
| 190 | |
| 191 | uint8_t flash_area_erased_val(const struct flash_area* fap) { |
| 192 | mbed::BlockDevice* bd = flash_map_bd[fap->fa_id]; |
| 193 | return bd->get_erase_value(); |
| 194 | } |
| 195 | |
| 196 | int flash_area_get_sectors(int fa_id, uint32_t* count, struct flash_sector* sectors) { |
| 197 | mbed::BlockDevice* bd = flash_map_bd[fa_id]; |
| 198 | |
George Beckstein | d1233e1 | 2020-12-02 01:57:30 -0500 | [diff] [blame] | 199 | /* Loop through sectors and collect information on them */ |
George Beckstein | d82afbf | 2020-10-29 17:32:11 -0400 | [diff] [blame] | 200 | bd_addr_t offset = 0; |
| 201 | *count = 0; |
| 202 | while (*count < MCUBOOT_MAX_IMG_SECTORS && bd->is_valid_read(offset, bd->get_read_size())) { |
| 203 | |
| 204 | sectors[*count].fs_off = offset; |
| 205 | bd_size_t erase_size = bd->get_erase_size(offset); |
| 206 | sectors[*count].fs_size = erase_size; |
| 207 | |
| 208 | offset += erase_size; |
| 209 | *count += 1; |
| 210 | } |
| 211 | |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | int flash_area_id_from_image_slot(int slot) { |
| 216 | return slot; |
| 217 | } |
| 218 | |
| 219 | int flash_area_id_to_image_slot(int area_id) { |
| 220 | return area_id; |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Multi images support not implemented yet |
| 225 | */ |
| 226 | int flash_area_id_from_multi_image_slot(int image_index, int slot) |
| 227 | { |
| 228 | assert(image_index == 0); |
| 229 | return slot; |
| 230 | } |
| 231 | |
| 232 | int flash_area_id_to_multi_image_slot(int image_index, int area_id) |
| 233 | { |
| 234 | assert(image_index == 0); |
| 235 | return area_id; |
| 236 | } |