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