Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 1 | /* |
David Brown | aac7111 | 2020-02-03 16:13:42 -0700 | [diff] [blame] | 2 | * SPDX-License-Identifier: Apache-2.0 |
| 3 | * |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 4 | * Copyright (c) 2019 JUUL Labs |
| 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 | |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 19 | #include <stddef.h> |
| 20 | #include <stdbool.h> |
| 21 | #include <inttypes.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include "bootutil/bootutil.h" |
| 25 | #include "bootutil_priv.h" |
| 26 | #include "swap_priv.h" |
| 27 | #include "bootutil/bootutil_log.h" |
| 28 | |
| 29 | #include "mcuboot_config/mcuboot_config.h" |
| 30 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 31 | BOOT_LOG_MODULE_DECLARE(mcuboot); |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 32 | |
| 33 | #if defined(MCUBOOT_SWAP_USING_SCRATCH) || defined(MCUBOOT_SWAP_USING_MOVE) |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 34 | |
| 35 | #ifndef MCUBOOT_SWAP_USING_STATUS |
| 36 | |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 37 | int |
| 38 | swap_erase_trailer_sectors(const struct boot_loader_state *state, |
| 39 | const struct flash_area *fap) |
| 40 | { |
| 41 | uint8_t slot; |
| 42 | uint32_t sector; |
| 43 | uint32_t trailer_sz; |
| 44 | uint32_t total_sz; |
| 45 | uint32_t off; |
| 46 | uint32_t sz; |
| 47 | int fa_id_primary; |
| 48 | int fa_id_secondary; |
| 49 | uint8_t image_index; |
| 50 | int rc; |
| 51 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 52 | BOOT_LOG_DBG("erasing trailer; fa_id=%u", (unsigned)flash_area_get_id(fap)); |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 53 | |
| 54 | image_index = BOOT_CURR_IMG(state); |
| 55 | fa_id_primary = flash_area_id_from_multi_image_slot(image_index, |
| 56 | BOOT_PRIMARY_SLOT); |
| 57 | fa_id_secondary = flash_area_id_from_multi_image_slot(image_index, |
| 58 | BOOT_SECONDARY_SLOT); |
| 59 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 60 | if (flash_area_get_id(fap) == fa_id_primary) { |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 61 | slot = BOOT_PRIMARY_SLOT; |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 62 | } else if (flash_area_get_id(fap) == fa_id_secondary) { |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 63 | slot = BOOT_SECONDARY_SLOT; |
| 64 | } else { |
| 65 | return BOOT_EFLASH; |
| 66 | } |
| 67 | |
| 68 | /* delete starting from last sector and moving to beginning */ |
| 69 | sector = boot_img_num_sectors(state, slot) - 1; |
| 70 | trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state)); |
| 71 | total_sz = 0; |
| 72 | do { |
| 73 | sz = boot_img_sector_size(state, slot, sector); |
| 74 | off = boot_img_sector_off(state, slot, sector); |
| 75 | rc = boot_erase_region(fap, off, sz); |
| 76 | assert(rc == 0); |
| 77 | |
| 78 | sector--; |
| 79 | total_sz += sz; |
| 80 | } while (total_sz < trailer_sz); |
| 81 | |
| 82 | return rc; |
| 83 | } |
| 84 | |
| 85 | int |
| 86 | swap_status_init(const struct boot_loader_state *state, |
| 87 | const struct flash_area *fap, |
| 88 | const struct boot_status *bs) |
| 89 | { |
| 90 | struct boot_swap_state swap_state; |
| 91 | uint8_t image_index; |
| 92 | int rc; |
| 93 | |
| 94 | #if (BOOT_IMAGE_NUMBER == 1) |
| 95 | (void)state; |
| 96 | #endif |
| 97 | |
| 98 | image_index = BOOT_CURR_IMG(state); |
| 99 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 100 | BOOT_LOG_DBG("initializing status; fa_id=%u", (unsigned)flash_area_get_id(fap)); |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 101 | |
| 102 | rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(image_index), |
| 103 | &swap_state); |
| 104 | assert(rc == 0); |
| 105 | |
| 106 | if (bs->swap_type != BOOT_SWAP_TYPE_NONE) { |
| 107 | rc = boot_write_swap_info(fap, bs->swap_type, image_index); |
| 108 | assert(rc == 0); |
| 109 | } |
| 110 | |
| 111 | if (swap_state.image_ok == BOOT_FLAG_SET) { |
| 112 | rc = boot_write_image_ok(fap); |
| 113 | assert(rc == 0); |
| 114 | } |
| 115 | |
| 116 | rc = boot_write_swap_size(fap, bs->swap_size); |
| 117 | assert(rc == 0); |
| 118 | |
| 119 | #ifdef MCUBOOT_ENC_IMAGES |
Fabio Utzig | 4741c45 | 2019-12-19 15:32:41 -0300 | [diff] [blame] | 120 | rc = boot_write_enc_key(fap, 0, bs); |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 121 | assert(rc == 0); |
| 122 | |
Fabio Utzig | 4741c45 | 2019-12-19 15:32:41 -0300 | [diff] [blame] | 123 | rc = boot_write_enc_key(fap, 1, bs); |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 124 | assert(rc == 0); |
| 125 | #endif |
| 126 | |
| 127 | rc = boot_write_magic(fap); |
| 128 | assert(rc == 0); |
| 129 | |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | int |
| 134 | swap_read_status(struct boot_loader_state *state, struct boot_status *bs) |
| 135 | { |
| 136 | const struct flash_area *fap; |
| 137 | uint32_t off; |
| 138 | uint8_t swap_info; |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 139 | int area_id; |
| 140 | int rc; |
| 141 | |
Fabio Utzig | b86e688 | 2019-12-10 09:51:17 -0300 | [diff] [blame] | 142 | bs->source = swap_status_source(state); |
| 143 | switch (bs->source) { |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 144 | case BOOT_STATUS_SOURCE_NONE: |
| 145 | return 0; |
| 146 | |
| 147 | #if MCUBOOT_SWAP_USING_SCRATCH |
| 148 | case BOOT_STATUS_SOURCE_SCRATCH: |
| 149 | area_id = FLASH_AREA_IMAGE_SCRATCH; |
| 150 | break; |
| 151 | #endif |
| 152 | |
| 153 | case BOOT_STATUS_SOURCE_PRIMARY_SLOT: |
| 154 | area_id = FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state)); |
| 155 | break; |
| 156 | |
| 157 | default: |
| 158 | assert(0); |
| 159 | return BOOT_EBADARGS; |
| 160 | } |
| 161 | |
| 162 | rc = flash_area_open(area_id, &fap); |
| 163 | if (rc != 0) { |
| 164 | return BOOT_EFLASH; |
| 165 | } |
| 166 | |
| 167 | rc = swap_read_status_bytes(fap, state, bs); |
| 168 | if (rc == 0) { |
| 169 | off = boot_swap_info_off(fap); |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 170 | rc = flash_area_read(fap, off, &swap_info, sizeof swap_info); |
Fabio Utzig | 4b2e55f | 2020-09-24 11:49:20 -0300 | [diff] [blame] | 171 | if (rc != 0) { |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 172 | rc = BOOT_EFLASH; |
| 173 | goto done; |
Fabio Utzig | 4b2e55f | 2020-09-24 11:49:20 -0300 | [diff] [blame] | 174 | } |
| 175 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 176 | if (swap_info == flash_area_erased_val(fap)) { |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 177 | BOOT_SET_SWAP_INFO(swap_info, 0, BOOT_SWAP_TYPE_NONE); |
| 178 | rc = 0; |
| 179 | } |
| 180 | |
| 181 | /* Extract the swap type info */ |
| 182 | bs->swap_type = BOOT_GET_SWAP_TYPE(swap_info); |
| 183 | } |
| 184 | |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 185 | done: |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 186 | flash_area_close(fap); |
| 187 | |
| 188 | return rc; |
| 189 | } |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 190 | #endif /* !MCUBOOT_SWAP_USING_STATUS */ |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 191 | |
| 192 | int |
| 193 | swap_set_copy_done(uint8_t image_index) |
| 194 | { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 195 | const struct flash_area *fap = NULL; |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 196 | int rc; |
| 197 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 198 | rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index), &fap); |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 199 | if (rc != 0) { |
| 200 | return BOOT_EFLASH; |
| 201 | } |
| 202 | |
| 203 | rc = boot_write_copy_done(fap); |
| 204 | flash_area_close(fap); |
| 205 | return rc; |
| 206 | } |
| 207 | |
| 208 | int |
| 209 | swap_set_image_ok(uint8_t image_index) |
| 210 | { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 211 | const struct flash_area *fap = NULL; |
| 212 | struct boot_swap_state state = {0}; |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 213 | int rc; |
| 214 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 215 | rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index), &fap); |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 216 | if (rc != 0) { |
| 217 | return BOOT_EFLASH; |
| 218 | } |
| 219 | |
| 220 | rc = boot_read_swap_state(fap, &state); |
| 221 | if (rc != 0) { |
| 222 | rc = BOOT_EFLASH; |
| 223 | goto out; |
| 224 | } |
| 225 | |
| 226 | if (state.image_ok == BOOT_FLAG_UNSET) { |
| 227 | rc = boot_write_image_ok(fap); |
| 228 | } |
| 229 | |
| 230 | out: |
| 231 | flash_area_close(fap); |
| 232 | return rc; |
| 233 | } |
| 234 | |
Fabio Utzig | 12d5916 | 2019-11-28 10:01:59 -0300 | [diff] [blame] | 235 | #endif /* defined(MCUBOOT_SWAP_USING_SCRATCH) || defined(MCUBOOT_SWAP_USING_MOVE) */ |