Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 1 | /* |
| 2 | * SPDX-License-Identifier: Apache-2.0 |
| 3 | * |
| 4 | * Copyright (c) 2017-2019 Linaro LTD |
| 5 | * Copyright (c) 2016-2019 JUUL Labs |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 6 | * Copyright (c) 2019-2021 Arm Limited |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 7 | * Copyright (c) 2020 Nordic Semiconductor ASA |
| 8 | * |
| 9 | * Original license: |
| 10 | * |
| 11 | * Licensed to the Apache Software Foundation (ASF) under one |
| 12 | * or more contributor license agreements. See the NOTICE file |
| 13 | * distributed with this work for additional information |
| 14 | * regarding copyright ownership. The ASF licenses this file |
| 15 | * to you under the Apache License, Version 2.0 (the |
| 16 | * "License"); you may not use this file except in compliance |
| 17 | * with the License. You may obtain a copy of the License at |
| 18 | * |
| 19 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 20 | * |
| 21 | * Unless required by applicable law or agreed to in writing, |
| 22 | * software distributed under the License is distributed on an |
| 23 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 24 | * KIND, either express or implied. See the License for the |
| 25 | * specific language governing permissions and limitations |
| 26 | * under the License. |
| 27 | */ |
| 28 | |
Andrzej Puzdrowski | 14ef576 | 2020-12-11 17:17:59 +0100 | [diff] [blame] | 29 | /** |
| 30 | * @file |
| 31 | * @brief Public MCUBoot interface API implementation |
| 32 | * |
| 33 | * This file contains API implementation which can be combined with |
| 34 | * the application in order to interact with the MCUBoot bootloader. |
| 35 | * This file contains shared code-base betwen MCUBoot and the application |
| 36 | * which controls DFU process. |
| 37 | */ |
| 38 | |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 39 | #include <string.h> |
| 40 | #include <inttypes.h> |
| 41 | #include <stddef.h> |
| 42 | |
| 43 | #include "sysflash/sysflash.h" |
| 44 | #include "flash_map_backend/flash_map_backend.h" |
| 45 | |
| 46 | #include "bootutil/image.h" |
| 47 | #include "bootutil/bootutil_public.h" |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 48 | #include "bootutil/bootutil_log.h" |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 49 | #include "swap_status.h" |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 50 | #ifdef MCUBOOT_ENC_IMAGES |
Andrzej Puzdrowski | 360763d | 2021-02-04 18:01:01 +0100 | [diff] [blame] | 51 | #include "bootutil/enc_key_public.h" |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 52 | #endif |
| 53 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 54 | #include "bootutil/boot_public_hooks.h" |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 55 | #include "bootutil_priv.h" |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 56 | |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 57 | #ifdef CONFIG_MCUBOOT |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 58 | BOOT_LOG_MODULE_DECLARE(mcuboot); |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 59 | #else |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 60 | BOOT_LOG_MODULE_REGISTER(mcuboot_util); |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 61 | #endif |
| 62 | |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 63 | #if BOOT_MAX_ALIGN == 8 |
| 64 | const union boot_img_magic_t boot_img_magic = { |
| 65 | .val = { |
| 66 | 0x77, 0xc2, 0x95, 0xf3, |
| 67 | 0x60, 0xd2, 0xef, 0x7f, |
| 68 | 0x35, 0x52, 0x50, 0x0f, |
| 69 | 0x2c, 0xb6, 0x79, 0x80 |
| 70 | } |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 71 | }; |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 72 | #else |
| 73 | const union boot_img_magic_t boot_img_magic = { |
| 74 | .align = BOOT_MAX_ALIGN, |
| 75 | .magic = { |
| 76 | 0x2d, 0xe1, |
| 77 | 0x5d, 0x29, 0x41, 0x0b, |
| 78 | 0x8d, 0x77, 0x67, 0x9c, |
| 79 | 0x11, 0x0f, 0x1f, 0x8a |
| 80 | } |
| 81 | }; |
| 82 | #endif |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 83 | |
| 84 | struct boot_swap_table { |
| 85 | uint8_t magic_primary_slot; |
| 86 | uint8_t magic_secondary_slot; |
| 87 | uint8_t image_ok_primary_slot; |
| 88 | uint8_t image_ok_secondary_slot; |
| 89 | uint8_t copy_done_primary_slot; |
| 90 | |
| 91 | uint8_t swap_type; |
| 92 | }; |
| 93 | |
| 94 | /** |
| 95 | * This set of tables maps image trailer contents to swap operation type. |
| 96 | * When searching for a match, these tables must be iterated sequentially. |
| 97 | * |
| 98 | * NOTE: the table order is very important. The settings in the secondary |
| 99 | * slot always are priority to the primary slot and should be located |
| 100 | * earlier in the table. |
| 101 | * |
| 102 | * The table lists only states where there is action needs to be taken by |
| 103 | * the bootloader, as in starting/finishing a swap operation. |
| 104 | */ |
| 105 | static const struct boot_swap_table boot_swap_tables[] = { |
| 106 | { |
| 107 | .magic_primary_slot = BOOT_MAGIC_ANY, |
| 108 | .magic_secondary_slot = BOOT_MAGIC_GOOD, |
| 109 | .image_ok_primary_slot = BOOT_FLAG_ANY, |
| 110 | .image_ok_secondary_slot = BOOT_FLAG_UNSET, |
| 111 | .copy_done_primary_slot = BOOT_FLAG_ANY, |
| 112 | .swap_type = BOOT_SWAP_TYPE_TEST, |
| 113 | }, |
| 114 | { |
| 115 | .magic_primary_slot = BOOT_MAGIC_ANY, |
| 116 | .magic_secondary_slot = BOOT_MAGIC_GOOD, |
| 117 | .image_ok_primary_slot = BOOT_FLAG_ANY, |
| 118 | .image_ok_secondary_slot = BOOT_FLAG_SET, |
| 119 | .copy_done_primary_slot = BOOT_FLAG_ANY, |
| 120 | .swap_type = BOOT_SWAP_TYPE_PERM, |
| 121 | }, |
| 122 | { |
| 123 | .magic_primary_slot = BOOT_MAGIC_GOOD, |
| 124 | .magic_secondary_slot = BOOT_MAGIC_UNSET, |
| 125 | .image_ok_primary_slot = BOOT_FLAG_UNSET, |
| 126 | .image_ok_secondary_slot = BOOT_FLAG_ANY, |
| 127 | .copy_done_primary_slot = BOOT_FLAG_SET, |
| 128 | .swap_type = BOOT_SWAP_TYPE_REVERT, |
| 129 | }, |
| 130 | }; |
| 131 | |
| 132 | #define BOOT_SWAP_TABLES_COUNT \ |
| 133 | (sizeof boot_swap_tables / sizeof boot_swap_tables[0]) |
| 134 | |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 135 | #ifndef MCUBOOT_SWAP_USING_STATUS |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 136 | static int |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 137 | boot_magic_decode(const uint8_t *magic) |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 138 | { |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 139 | if (memcmp(magic, BOOT_IMG_MAGIC, BOOT_MAGIC_SZ) == 0) { |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 140 | return BOOT_MAGIC_GOOD; |
| 141 | } |
| 142 | return BOOT_MAGIC_BAD; |
| 143 | } |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 144 | #endif /* !MCUBOOT_SWAP_USING_STATUS */ |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 145 | |
| 146 | static int |
| 147 | boot_flag_decode(uint8_t flag) |
| 148 | { |
| 149 | if (flag != BOOT_FLAG_SET) { |
| 150 | return BOOT_FLAG_BAD; |
| 151 | } |
| 152 | return BOOT_FLAG_SET; |
| 153 | } |
| 154 | |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 155 | #ifndef MCUBOOT_SWAP_USING_STATUS |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 156 | static inline uint32_t |
| 157 | boot_magic_off(const struct flash_area *fap) |
| 158 | { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 159 | return flash_area_get_size(fap) - BOOT_MAGIC_SZ; |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | static inline uint32_t |
| 163 | boot_image_ok_off(const struct flash_area *fap) |
| 164 | { |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 165 | return ALIGN_DOWN(boot_magic_off(fap) - BOOT_MAX_ALIGN, BOOT_MAX_ALIGN); |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | static inline uint32_t |
| 169 | boot_copy_done_off(const struct flash_area *fap) |
| 170 | { |
| 171 | return boot_image_ok_off(fap) - BOOT_MAX_ALIGN; |
| 172 | } |
| 173 | |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 174 | uint32_t |
| 175 | boot_swap_info_off(const struct flash_area *fap) |
| 176 | { |
| 177 | return boot_copy_done_off(fap) - BOOT_MAX_ALIGN; |
| 178 | } |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 179 | #endif /* !MCUBOOT_SWAP_USING_STATUS */ |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 180 | |
| 181 | /** |
| 182 | * Determines if a status source table is satisfied by the specified magic |
| 183 | * code. |
| 184 | * |
| 185 | * @param tbl_val A magic field from a status source table. |
| 186 | * @param val The magic value in a trailer, encoded as a |
| 187 | * BOOT_MAGIC_[...]. |
| 188 | * |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 189 | * @return true - if the two values are compatible; |
| 190 | * false - otherwise. |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 191 | */ |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 192 | bool boot_magic_compatible_check(uint8_t tbl_val, uint8_t val) |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 193 | { |
| 194 | switch (tbl_val) { |
| 195 | case BOOT_MAGIC_ANY: |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 196 | return true; |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 197 | |
| 198 | case BOOT_MAGIC_NOTGOOD: |
| 199 | return val != BOOT_MAGIC_GOOD; |
| 200 | |
| 201 | default: |
| 202 | return tbl_val == val; |
| 203 | } |
| 204 | } |
| 205 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 206 | bool bootutil_buffer_is_filled(const void *buffer, uint8_t fill, size_t len) |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 207 | { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 208 | uint8_t *p; |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 209 | |
| 210 | if (buffer == NULL || len == 0) { |
| 211 | return false; |
| 212 | } |
| 213 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 214 | for (p = (uint8_t *)buffer; len-- > 0; p++) { |
| 215 | if (*p != fill) { |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 216 | return false; |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | return true; |
| 221 | } |
| 222 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 223 | bool bootutil_buffer_is_erased(const struct flash_area *area, |
| 224 | const void *buffer, size_t len) |
| 225 | { |
| 226 | uint8_t erased_val; |
| 227 | |
| 228 | if (area == NULL) { |
| 229 | return false; |
| 230 | } |
| 231 | |
| 232 | erased_val = flash_area_erased_val(area); |
| 233 | |
| 234 | return bootutil_buffer_is_filled(buffer, erased_val, len); |
| 235 | } |
| 236 | |
Andrzej Puzdrowski | 4700b80 | 2020-12-09 14:55:36 +0100 | [diff] [blame] | 237 | static int |
| 238 | boot_read_flag(const struct flash_area *fap, uint8_t *flag, uint32_t off) |
| 239 | { |
| 240 | int rc; |
| 241 | |
| 242 | rc = flash_area_read(fap, off, flag, sizeof *flag); |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 243 | if (rc != 0) { |
Andrzej Puzdrowski | 4700b80 | 2020-12-09 14:55:36 +0100 | [diff] [blame] | 244 | return BOOT_EFLASH; |
| 245 | } |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 246 | if (*flag == flash_area_erased_val(fap)) { |
Andrzej Puzdrowski | 4700b80 | 2020-12-09 14:55:36 +0100 | [diff] [blame] | 247 | *flag = BOOT_FLAG_UNSET; |
| 248 | } else { |
| 249 | *flag = boot_flag_decode(*flag); |
| 250 | } |
| 251 | |
| 252 | return 0; |
| 253 | } |
| 254 | |
Dovhal Artem (CSUKR CSS ICW SW FW 1) | f7a3d1b | 2022-04-01 15:07:37 +0000 | [diff] [blame] | 255 | #ifndef MCUBOOT_SWAP_USING_STATUS |
| 256 | |
Andrzej Puzdrowski | 4700b80 | 2020-12-09 14:55:36 +0100 | [diff] [blame] | 257 | static inline int |
| 258 | boot_read_copy_done(const struct flash_area *fap, uint8_t *copy_done) |
| 259 | { |
| 260 | return boot_read_flag(fap, copy_done, boot_copy_done_off(fap)); |
| 261 | } |
| 262 | |
| 263 | |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 264 | int |
| 265 | boot_read_swap_state(const struct flash_area *fap, |
| 266 | struct boot_swap_state *state) |
| 267 | { |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 268 | uint8_t magic[BOOT_MAGIC_SZ]; |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 269 | uint32_t off; |
| 270 | uint8_t swap_info; |
| 271 | int rc; |
| 272 | |
| 273 | off = boot_magic_off(fap); |
| 274 | rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ); |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 275 | if (rc != 0) { |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 276 | return BOOT_EFLASH; |
| 277 | } |
| 278 | if (bootutil_buffer_is_erased(fap, magic, BOOT_MAGIC_SZ)) { |
| 279 | state->magic = BOOT_MAGIC_UNSET; |
| 280 | } else { |
| 281 | state->magic = boot_magic_decode(magic); |
| 282 | } |
| 283 | |
| 284 | off = boot_swap_info_off(fap); |
| 285 | rc = flash_area_read(fap, off, &swap_info, sizeof swap_info); |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 286 | if (rc != 0) { |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 287 | return BOOT_EFLASH; |
| 288 | } |
| 289 | |
| 290 | /* Extract the swap type and image number */ |
| 291 | state->swap_type = BOOT_GET_SWAP_TYPE(swap_info); |
| 292 | state->image_num = BOOT_GET_IMAGE_NUM(swap_info); |
| 293 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 294 | if (swap_info == flash_area_erased_val(fap) || |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 295 | state->swap_type > BOOT_SWAP_TYPE_REVERT) { |
| 296 | state->swap_type = BOOT_SWAP_TYPE_NONE; |
| 297 | state->image_num = 0; |
| 298 | } |
| 299 | |
Andrzej Puzdrowski | 4700b80 | 2020-12-09 14:55:36 +0100 | [diff] [blame] | 300 | rc = boot_read_copy_done(fap, &state->copy_done); |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 301 | if (rc != 0) { |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 302 | return BOOT_EFLASH; |
| 303 | } |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 304 | |
Andrzej Puzdrowski | 4700b80 | 2020-12-09 14:55:36 +0100 | [diff] [blame] | 305 | return boot_read_image_ok(fap, &state->image_ok); |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 306 | } |
| 307 | |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 308 | #endif /* !MCUBOOT_SWAP_USING_STATUS */ |
| 309 | |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 310 | int |
| 311 | boot_read_swap_state_by_id(int flash_area_id, struct boot_swap_state *state) |
| 312 | { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 313 | const struct flash_area *fap = NULL; |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 314 | int rc; |
| 315 | |
| 316 | rc = flash_area_open(flash_area_id, &fap); |
| 317 | if (rc != 0) { |
| 318 | return BOOT_EFLASH; |
| 319 | } |
| 320 | |
| 321 | rc = boot_read_swap_state(fap, state); |
| 322 | flash_area_close(fap); |
| 323 | return rc; |
| 324 | } |
| 325 | |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 326 | #ifndef MCUBOOT_SWAP_USING_STATUS |
| 327 | |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 328 | int |
| 329 | boot_write_magic(const struct flash_area *fap) |
| 330 | { |
| 331 | uint32_t off; |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 332 | uint32_t pad_off; |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 333 | int rc; |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 334 | uint8_t magic[BOOT_MAGIC_ALIGN_SIZE]; |
| 335 | uint8_t erased_val; |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 336 | |
| 337 | off = boot_magic_off(fap); |
| 338 | |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 339 | /* image_trailer structure was modified with additional padding such that |
| 340 | * the pad+magic ends up in a flash minimum write region. The address |
| 341 | * returned by boot_magic_off() is the start of magic which is not the |
| 342 | * start of the flash write boundary and thus writes to the magic will fail. |
| 343 | * To account for this change, write to magic is first padded with 0xFF |
| 344 | * before writing to the trailer. |
| 345 | */ |
| 346 | pad_off = ALIGN_DOWN(off, BOOT_MAX_ALIGN); |
| 347 | |
| 348 | erased_val = flash_area_erased_val(fap); |
| 349 | |
| 350 | (void)memset(&magic[0], erased_val, sizeof(magic)); |
| 351 | (void)memcpy(&magic[BOOT_MAGIC_ALIGN_SIZE - BOOT_MAGIC_SZ], BOOT_IMG_MAGIC, BOOT_MAGIC_SZ); |
| 352 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 353 | BOOT_LOG_DBG("writing magic; fa_id=%u off=0x%" PRIx32 |
| 354 | " (0x%" PRIx32 ")", (unsigned)flash_area_get_id(fap), |
| 355 | off, flash_area_get_off(fap) + off); |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 356 | rc = flash_area_write(fap, pad_off, &magic[0], BOOT_MAGIC_ALIGN_SIZE); |
| 357 | |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 358 | if (rc != 0) { |
| 359 | return BOOT_EFLASH; |
| 360 | } |
| 361 | |
| 362 | return 0; |
| 363 | } |
| 364 | |
| 365 | /** |
| 366 | * Write trailer data; status bytes, swap_size, etc |
| 367 | * |
| 368 | * @returns 0 on success, != 0 on error. |
| 369 | */ |
Dominik Ermel | a7f9e9f | 2021-03-24 17:31:57 +0000 | [diff] [blame] | 370 | int |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 371 | boot_write_trailer(const struct flash_area *fap, uint32_t off, |
| 372 | const uint8_t *inbuf, uint8_t inlen) |
| 373 | { |
| 374 | uint8_t buf[BOOT_MAX_ALIGN]; |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 375 | uint8_t erased_val; |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 376 | uint32_t align; |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 377 | int rc; |
| 378 | |
| 379 | align = flash_area_align(fap); |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 380 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 381 | if (align == 0u) { |
| 382 | return BOOT_EFLASH; |
| 383 | } |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 384 | |
| 385 | align = ALIGN_UP(inlen, align); |
Dominik Ermel | 4828162 | 2021-03-24 18:04:54 +0000 | [diff] [blame] | 386 | if (align > BOOT_MAX_ALIGN) { |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 387 | return -1; |
| 388 | } |
| 389 | erased_val = flash_area_erased_val(fap); |
Dominik Ermel | 4828162 | 2021-03-24 18:04:54 +0000 | [diff] [blame] | 390 | |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 391 | (void)memcpy(buf, inbuf, inlen); |
| 392 | (void)memset(&buf[inlen], erased_val, align - inlen); |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 393 | |
| 394 | rc = flash_area_write(fap, off, buf, align); |
| 395 | if (rc != 0) { |
| 396 | return BOOT_EFLASH; |
| 397 | } |
| 398 | |
| 399 | return 0; |
| 400 | } |
| 401 | |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 402 | #endif /* !MCUBOOT_SWAP_USING_STATUS */ |
| 403 | |
Dominik Ermel | a7f9e9f | 2021-03-24 17:31:57 +0000 | [diff] [blame] | 404 | int |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 405 | boot_write_trailer_flag(const struct flash_area *fap, uint32_t off, |
| 406 | uint8_t flag_val) |
| 407 | { |
| 408 | const uint8_t buf[1] = { flag_val }; |
| 409 | return boot_write_trailer(fap, off, buf, 1); |
| 410 | } |
| 411 | |
| 412 | int |
| 413 | boot_write_image_ok(const struct flash_area *fap) |
| 414 | { |
| 415 | uint32_t off; |
| 416 | |
| 417 | off = boot_image_ok_off(fap); |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 418 | BOOT_LOG_DBG("writing image_ok; fa_id=%u off=0x%" PRIx32 |
| 419 | " (0x%" PRIx32 ")", (unsigned)flash_area_get_id(fap), |
| 420 | off, flash_area_get_off(fap) + off); |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 421 | return boot_write_trailer_flag(fap, off, BOOT_FLAG_SET); |
| 422 | } |
| 423 | |
Andrzej Puzdrowski | 4700b80 | 2020-12-09 14:55:36 +0100 | [diff] [blame] | 424 | int |
| 425 | boot_read_image_ok(const struct flash_area *fap, uint8_t *image_ok) |
| 426 | { |
| 427 | return boot_read_flag(fap, image_ok, boot_image_ok_off(fap)); |
| 428 | } |
| 429 | |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 430 | /** |
| 431 | * Writes the specified value to the `swap-type` field of an image trailer. |
| 432 | * This value is persisted so that the boot loader knows what swap operation to |
| 433 | * resume in case of an unexpected reset. |
| 434 | */ |
| 435 | int |
| 436 | boot_write_swap_info(const struct flash_area *fap, uint8_t swap_type, |
| 437 | uint8_t image_num) |
| 438 | { |
| 439 | uint32_t off; |
| 440 | uint8_t swap_info; |
| 441 | |
| 442 | BOOT_SET_SWAP_INFO(swap_info, image_num, swap_type); |
| 443 | off = boot_swap_info_off(fap); |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 444 | BOOT_LOG_DBG("writing swap_info; fa_id=%u off=0x%" PRIx32 |
| 445 | " (0x%" PRIx32 "), swap_type=0x%x image_num=0x%x", |
| 446 | (unsigned)flash_area_get_id(fap), off, |
| 447 | flash_area_get_off(fap) + off, |
| 448 | (unsigned)swap_type, (unsigned)image_num); |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 449 | return boot_write_trailer(fap, off, (const uint8_t *) &swap_info, 1); |
| 450 | } |
| 451 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 452 | #define BOOT_LOG_SWAP_STATE(area, state) \ |
| 453 | BOOT_LOG_INF("%s: magic=%s, swap_type=0x%x, copy_done=0x%x, image_ok=0x%x", \ |
| 454 | (area), \ |
| 455 | ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \ |
| 456 | (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \ |
| 457 | "bad"), \ |
| 458 | (unsigned)(state)->swap_type, \ |
| 459 | (unsigned)(state)->copy_done, \ |
| 460 | (unsigned)(state)->image_ok) |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 461 | |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 462 | int |
| 463 | boot_swap_type_multi(int image_index) |
| 464 | { |
Dovhal Artem (CSUKR CSS ICW SW FW 1) | f7a3d1b | 2022-04-01 15:07:37 +0000 | [diff] [blame] | 465 | const struct boot_swap_table *table = NULL; |
| 466 | struct boot_swap_state primary_slot = {0}; |
| 467 | struct boot_swap_state secondary_slot = {0}; |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 468 | int rc; |
| 469 | size_t i; |
| 470 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 471 | rc = BOOT_HOOK_CALL(boot_read_swap_state_primary_slot_hook, |
| 472 | BOOT_HOOK_REGULAR, image_index, &primary_slot); |
| 473 | if (rc == BOOT_HOOK_REGULAR) |
| 474 | { |
| 475 | rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(image_index), |
| 476 | &primary_slot); |
| 477 | } |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 478 | if (rc) { |
| 479 | return BOOT_SWAP_TYPE_PANIC; |
| 480 | } |
| 481 | |
| 482 | rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(image_index), |
| 483 | &secondary_slot); |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 484 | if (rc == BOOT_EFLASH) { |
| 485 | BOOT_LOG_INF("Secondary image of image pair (%d.) " |
| 486 | "is unreachable. Treat it as empty", image_index); |
| 487 | secondary_slot.magic = BOOT_MAGIC_UNSET; |
| 488 | secondary_slot.swap_type = BOOT_SWAP_TYPE_NONE; |
| 489 | secondary_slot.copy_done = BOOT_FLAG_UNSET; |
| 490 | secondary_slot.image_ok = BOOT_FLAG_UNSET; |
| 491 | secondary_slot.image_num = 0; |
| 492 | } else if (rc) { |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 493 | return BOOT_SWAP_TYPE_PANIC; |
| 494 | } |
| 495 | |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 496 | BOOT_LOG_SWAP_STATE("boot_swap_type_multi: Primary image", &primary_slot); |
| 497 | BOOT_LOG_SWAP_STATE("boot_swap_type_multi: Secondary image", &secondary_slot); |
| 498 | |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 499 | for (i = 0; i < BOOT_SWAP_TABLES_COUNT; i++) { |
| 500 | table = boot_swap_tables + i; |
| 501 | |
| 502 | if (boot_magic_compatible_check(table->magic_primary_slot, |
| 503 | primary_slot.magic) && |
| 504 | boot_magic_compatible_check(table->magic_secondary_slot, |
| 505 | secondary_slot.magic) && |
| 506 | (table->image_ok_primary_slot == BOOT_FLAG_ANY || |
| 507 | table->image_ok_primary_slot == primary_slot.image_ok) && |
| 508 | (table->image_ok_secondary_slot == BOOT_FLAG_ANY || |
| 509 | table->image_ok_secondary_slot == secondary_slot.image_ok) && |
| 510 | (table->copy_done_primary_slot == BOOT_FLAG_ANY || |
| 511 | table->copy_done_primary_slot == primary_slot.copy_done)) { |
| 512 | BOOT_LOG_INF("Swap type: %s", |
| 513 | table->swap_type == BOOT_SWAP_TYPE_TEST ? "test" : |
| 514 | table->swap_type == BOOT_SWAP_TYPE_PERM ? "perm" : |
| 515 | table->swap_type == BOOT_SWAP_TYPE_REVERT ? "revert" : |
| 516 | "BUG; can't happen"); |
| 517 | if (table->swap_type != BOOT_SWAP_TYPE_TEST && |
| 518 | table->swap_type != BOOT_SWAP_TYPE_PERM && |
| 519 | table->swap_type != BOOT_SWAP_TYPE_REVERT) { |
| 520 | return BOOT_SWAP_TYPE_PANIC; |
| 521 | } |
| 522 | return table->swap_type; |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | BOOT_LOG_INF("Swap type: none"); |
| 527 | return BOOT_SWAP_TYPE_NONE; |
| 528 | } |
| 529 | |
| 530 | /* |
| 531 | * This function is not used by the bootloader itself, but its required API |
| 532 | * by external tooling like mcumgr. |
| 533 | */ |
| 534 | int |
| 535 | boot_swap_type(void) |
| 536 | { |
| 537 | return boot_swap_type_multi(0); |
| 538 | } |
| 539 | |
| 540 | /** |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 541 | * Marks the image with the given index in the secondary slot as pending. On the |
| 542 | * next reboot, the system will perform a one-time boot of the the secondary |
| 543 | * slot image. |
| 544 | * |
| 545 | * @param image_index Image pair index. |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 546 | * |
| 547 | * @param permanent Whether the image should be used permanently or |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 548 | * only tested once: |
| 549 | * 0=run image once, then confirm or revert. |
| 550 | * 1=run image forever. |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 551 | * |
| 552 | * @return 0 on success; nonzero on failure. |
| 553 | */ |
| 554 | int |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 555 | boot_set_pending_multi(int image_index, int permanent) |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 556 | { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 557 | const struct flash_area *fap = NULL; |
| 558 | struct boot_swap_state state_secondary_slot = {0}; |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 559 | uint8_t swap_type; |
| 560 | int rc; |
| 561 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 562 | rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index), &fap); |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 563 | if (rc != 0) { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 564 | return BOOT_EFLASH; |
| 565 | } |
| 566 | |
| 567 | rc = boot_read_swap_state(fap, &state_secondary_slot); |
| 568 | if (rc != 0) { |
| 569 | goto done; |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | switch (state_secondary_slot.magic) { |
| 573 | case BOOT_MAGIC_GOOD: |
| 574 | /* Swap already scheduled. */ |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 575 | break; |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 576 | |
| 577 | case BOOT_MAGIC_UNSET: |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 578 | rc = boot_write_magic(fap); |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 579 | |
| 580 | if (rc == 0 && permanent) { |
| 581 | rc = boot_write_image_ok(fap); |
| 582 | } |
| 583 | |
| 584 | if (rc == 0) { |
| 585 | if (permanent) { |
| 586 | swap_type = BOOT_SWAP_TYPE_PERM; |
| 587 | } else { |
| 588 | swap_type = BOOT_SWAP_TYPE_TEST; |
| 589 | } |
| 590 | rc = boot_write_swap_info(fap, swap_type, 0); |
| 591 | } |
| 592 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 593 | break; |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 594 | |
| 595 | case BOOT_MAGIC_BAD: |
| 596 | /* The image slot is corrupt. There is no way to recover, so erase the |
| 597 | * slot to allow future upgrades. |
| 598 | */ |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 599 | flash_area_erase(fap, 0, flash_area_get_size(fap)); |
| 600 | rc = BOOT_EBADIMAGE; |
| 601 | break; |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 602 | |
| 603 | default: |
| 604 | assert(0); |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 605 | rc = BOOT_EBADIMAGE; |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 606 | } |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 607 | |
| 608 | done: |
| 609 | flash_area_close(fap); |
| 610 | return rc; |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | /** |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 614 | * Marks the image with index 0 in the secondary slot as pending. On the next |
| 615 | * reboot, the system will perform a one-time boot of the the secondary slot |
| 616 | * image. Note that this API is kept for compatibility. The |
| 617 | * boot_set_pending_multi() API is recommended. |
| 618 | * |
| 619 | * @param permanent Whether the image should be used permanently or |
| 620 | * only tested once: |
| 621 | * 0=run image once, then confirm or revert. |
| 622 | * 1=run image forever. |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 623 | * |
| 624 | * @return 0 on success; nonzero on failure. |
| 625 | */ |
| 626 | int |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 627 | boot_set_pending(int permanent) |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 628 | { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 629 | return boot_set_pending_multi(0, permanent); |
| 630 | } |
| 631 | |
| 632 | /** |
| 633 | * Marks the image with the given index in the primary slot as confirmed. The |
| 634 | * system will continue booting into the image in the primary slot until told to |
| 635 | * boot from a different slot. |
| 636 | * |
| 637 | * @param image_index Image pair index. |
| 638 | * |
| 639 | * @return 0 on success; nonzero on failure. |
| 640 | */ |
| 641 | int |
| 642 | boot_set_confirmed_multi(int image_index) |
| 643 | { |
| 644 | const struct flash_area *fap = NULL; |
| 645 | struct boot_swap_state state_primary_slot = {0}; |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 646 | int rc; |
| 647 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 648 | rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index), &fap); |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 649 | if (rc != 0) { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 650 | return BOOT_EFLASH; |
| 651 | } |
| 652 | |
| 653 | rc = boot_read_swap_state(fap, &state_primary_slot); |
| 654 | if (rc != 0) { |
| 655 | goto done; |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | switch (state_primary_slot.magic) { |
| 659 | case BOOT_MAGIC_GOOD: |
| 660 | /* Confirm needed; proceed. */ |
| 661 | break; |
| 662 | |
| 663 | case BOOT_MAGIC_UNSET: |
| 664 | /* Already confirmed. */ |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 665 | goto done; |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 666 | |
| 667 | case BOOT_MAGIC_BAD: |
| 668 | /* Unexpected state. */ |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 669 | rc = BOOT_EBADVECT; |
| 670 | goto done; |
| 671 | } |
| 672 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 673 | /* Intentionally do not check copy_done flag |
| 674 | * so can confirm a padded image which was programed using a programing |
| 675 | * interface. |
| 676 | */ |
| 677 | |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 678 | if (state_primary_slot.image_ok != BOOT_FLAG_UNSET) { |
| 679 | /* Already confirmed. */ |
| 680 | goto done; |
| 681 | } |
| 682 | |
| 683 | rc = boot_write_image_ok(fap); |
| 684 | |
| 685 | done: |
| 686 | flash_area_close(fap); |
| 687 | return rc; |
| 688 | } |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 689 | |
| 690 | /** |
| 691 | * Marks the image with index 0 in the primary slot as confirmed. The system |
| 692 | * will continue booting into the image in the primary slot until told to boot |
| 693 | * from a different slot. Note that this API is kept for compatibility. The |
| 694 | * boot_set_confirmed_multi() API is recommended. |
| 695 | * |
| 696 | * @return 0 on success; nonzero on failure. |
| 697 | */ |
| 698 | int |
| 699 | boot_set_confirmed(void) |
| 700 | { |
| 701 | return boot_set_confirmed_multi(0); |
| 702 | } |