Dominik Ermel | 8101c0c | 2020-05-19 13:01:16 +0000 | [diff] [blame] | 1 | /* |
| 2 | * SPDX-License-Identifier: Apache-2.0 |
| 3 | * |
| 4 | * Copyright (c) 2020 Nordic Semiconductor ASA |
Tamas Ban | ee6615d | 2020-09-30 07:58:48 +0100 | [diff] [blame] | 5 | * Copyright (c) 2020 Arm Limited |
Dominik Ermel | 8101c0c | 2020-05-19 13:01:16 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <assert.h> |
| 9 | #include "bootutil/image.h" |
| 10 | #include "bootutil_priv.h" |
Dane Wagner | f2a6146 | 2024-12-04 10:46:40 -0600 | [diff] [blame] | 11 | #include "bootutil/boot_record.h" |
| 12 | #include "bootutil/bootutil.h" |
Dominik Ermel | 8101c0c | 2020-05-19 13:01:16 +0000 | [diff] [blame] | 13 | #include "bootutil/bootutil_log.h" |
Jamie McCrae | c9fa608 | 2023-07-21 10:23:17 +0100 | [diff] [blame] | 14 | #include "bootutil/bootutil_public.h" |
Tamas Ban | ee6615d | 2020-09-30 07:58:48 +0100 | [diff] [blame] | 15 | #include "bootutil/fault_injection_hardening.h" |
Dominik Ermel | 8101c0c | 2020-05-19 13:01:16 +0000 | [diff] [blame] | 16 | |
| 17 | #include "mcuboot_config/mcuboot_config.h" |
| 18 | |
Carlos Falgueras GarcĂa | a4b4b0f | 2021-06-22 10:00:22 +0200 | [diff] [blame] | 19 | BOOT_LOG_MODULE_DECLARE(mcuboot); |
Dominik Ermel | 8101c0c | 2020-05-19 13:01:16 +0000 | [diff] [blame] | 20 | |
| 21 | /* Variables passed outside of unit via poiters. */ |
| 22 | static const struct flash_area *_fa_p; |
| 23 | static struct image_header _hdr = { 0 }; |
Ederson de Souza | 98a6681 | 2024-12-20 12:34:19 -0800 | [diff] [blame] | 24 | static struct boot_loader_state boot_data; |
| 25 | |
| 26 | struct boot_loader_state *boot_get_loader_state(void) |
| 27 | { |
| 28 | return &boot_data; |
| 29 | } |
Dominik Ermel | 8101c0c | 2020-05-19 13:01:16 +0000 | [diff] [blame] | 30 | |
Wouter Cappelle | 953a761 | 2021-05-03 16:53:05 +0200 | [diff] [blame] | 31 | #if defined(MCUBOOT_VALIDATE_PRIMARY_SLOT) || defined(MCUBOOT_VALIDATE_PRIMARY_SLOT_ONCE) |
Dominik Ermel | 8101c0c | 2020-05-19 13:01:16 +0000 | [diff] [blame] | 32 | /** |
| 33 | * Validate hash of a primary boot image. |
| 34 | * |
| 35 | * @param[in] fa_p flash area pointer |
| 36 | * @param[in] hdr boot image header pointer |
| 37 | * |
Tamas Ban | ee6615d | 2020-09-30 07:58:48 +0100 | [diff] [blame] | 38 | * @return FIH_SUCCESS on success, error code otherwise |
Dominik Ermel | 8101c0c | 2020-05-19 13:01:16 +0000 | [diff] [blame] | 39 | */ |
Michael Grand | 5047f03 | 2022-11-24 16:49:56 +0100 | [diff] [blame] | 40 | fih_ret |
Dominik Ermel | 8101c0c | 2020-05-19 13:01:16 +0000 | [diff] [blame] | 41 | boot_image_validate(const struct flash_area *fa_p, |
| 42 | struct image_header *hdr) |
| 43 | { |
| 44 | static uint8_t tmpbuf[BOOT_TMPBUF_SZ]; |
Michael Grand | 5047f03 | 2022-11-24 16:49:56 +0100 | [diff] [blame] | 45 | FIH_DECLARE(fih_rc, FIH_FAILURE); |
Dominik Ermel | 8101c0c | 2020-05-19 13:01:16 +0000 | [diff] [blame] | 46 | |
Dominik Ermel | d8db025 | 2020-10-07 11:22:45 +0000 | [diff] [blame] | 47 | /* NOTE: The first argument to boot_image_validate, for enc_state pointer, |
| 48 | * is allowed to be NULL only because the single image loader compiles |
| 49 | * with BOOT_IMAGE_NUMBER == 1, which excludes the code that uses |
| 50 | * the pointer from compilation. |
Dominik Ermel | 8101c0c | 2020-05-19 13:01:16 +0000 | [diff] [blame] | 51 | */ |
| 52 | /* Validate hash */ |
Wouter Cappelle | 7679215 | 2022-01-19 17:28:55 +0100 | [diff] [blame] | 53 | if (IS_ENCRYPTED(hdr)) |
Wouter Cappelle | 953a761 | 2021-05-03 16:53:05 +0200 | [diff] [blame] | 54 | { |
| 55 | /* Clear the encrypted flag we didn't supply a key |
| 56 | * This flag could be set if there was a decryption in place |
| 57 | * was performed. We will try to validate the image, and if still |
| 58 | * encrypted the validation will fail, and go in panic mode |
| 59 | */ |
Wouter Cappelle | 7679215 | 2022-01-19 17:28:55 +0100 | [diff] [blame] | 60 | hdr->ih_flags &= ~(ENCRYPTIONFLAGS); |
Wouter Cappelle | 953a761 | 2021-05-03 16:53:05 +0200 | [diff] [blame] | 61 | } |
Jamie McCrae | 2fe9cd4 | 2025-01-14 16:42:00 +0000 | [diff] [blame] | 62 | FIH_CALL(bootutil_img_validate, fih_rc, NULL, hdr, fa_p, tmpbuf, |
Tamas Ban | ee6615d | 2020-09-30 07:58:48 +0100 | [diff] [blame] | 63 | BOOT_TMPBUF_SZ, NULL, 0, NULL); |
Dominik Ermel | 8101c0c | 2020-05-19 13:01:16 +0000 | [diff] [blame] | 64 | |
Tamas Ban | ee6615d | 2020-09-30 07:58:48 +0100 | [diff] [blame] | 65 | FIH_RET(fih_rc); |
Dominik Ermel | 8101c0c | 2020-05-19 13:01:16 +0000 | [diff] [blame] | 66 | } |
Wouter Cappelle | 953a761 | 2021-05-03 16:53:05 +0200 | [diff] [blame] | 67 | #endif /* MCUBOOT_VALIDATE_PRIMARY_SLOT || MCUBOOT_VALIDATE_PRIMARY_SLOT_ONCE*/ |
Dominik Ermel | 8101c0c | 2020-05-19 13:01:16 +0000 | [diff] [blame] | 68 | |
Jamie McCrae | 2129973 | 2023-12-07 09:41:55 +0000 | [diff] [blame] | 69 | #if defined(MCUBOOT_VALIDATE_PRIMARY_SLOT_ONCE) |
Michael Grand | 5047f03 | 2022-11-24 16:49:56 +0100 | [diff] [blame] | 70 | inline static fih_ret |
Wouter Cappelle | bb7a39d | 2021-05-03 16:44:44 +0200 | [diff] [blame] | 71 | boot_image_validate_once(const struct flash_area *fa_p, |
| 72 | struct image_header *hdr) |
| 73 | { |
| 74 | static struct boot_swap_state state; |
| 75 | int rc; |
Michael Grand | 5047f03 | 2022-11-24 16:49:56 +0100 | [diff] [blame] | 76 | FIH_DECLARE(fih_rc, FIH_FAILURE); |
Wouter Cappelle | bb7a39d | 2021-05-03 16:44:44 +0200 | [diff] [blame] | 77 | |
| 78 | memset(&state, 0, sizeof(struct boot_swap_state)); |
| 79 | rc = boot_read_swap_state(fa_p, &state); |
| 80 | if (rc != 0) |
| 81 | FIH_RET(FIH_FAILURE); |
| 82 | if (state.magic != BOOT_MAGIC_GOOD |
| 83 | || state.image_ok != BOOT_FLAG_SET) { |
| 84 | /* At least validate the image once */ |
| 85 | FIH_CALL(boot_image_validate, fih_rc, fa_p, hdr); |
Michael Grand | 5047f03 | 2022-11-24 16:49:56 +0100 | [diff] [blame] | 86 | if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) { |
Wouter Cappelle | bb7a39d | 2021-05-03 16:44:44 +0200 | [diff] [blame] | 87 | FIH_RET(FIH_FAILURE); |
| 88 | } |
| 89 | if (state.magic != BOOT_MAGIC_GOOD) { |
| 90 | rc = boot_write_magic(fa_p); |
| 91 | if (rc != 0) |
| 92 | FIH_RET(FIH_FAILURE); |
| 93 | } |
| 94 | rc = boot_write_image_ok(fa_p); |
| 95 | if (rc != 0) |
| 96 | FIH_RET(FIH_FAILURE); |
| 97 | } |
| 98 | FIH_RET(FIH_SUCCESS); |
| 99 | } |
Jamie McCrae | 2129973 | 2023-12-07 09:41:55 +0000 | [diff] [blame] | 100 | #endif |
Wouter Cappelle | bb7a39d | 2021-05-03 16:44:44 +0200 | [diff] [blame] | 101 | |
Dominik Ermel | 8101c0c | 2020-05-19 13:01:16 +0000 | [diff] [blame] | 102 | /** |
Dominik Ermel | 8101c0c | 2020-05-19 13:01:16 +0000 | [diff] [blame] | 103 | * Gather information on image and prepare for booting. |
| 104 | * |
| 105 | * @parami[out] rsp Parameters for booting image, on success |
| 106 | * |
Tamas Ban | ee6615d | 2020-09-30 07:58:48 +0100 | [diff] [blame] | 107 | * @return FIH_SUCCESS on success; nonzero on failure. |
Dominik Ermel | 8101c0c | 2020-05-19 13:01:16 +0000 | [diff] [blame] | 108 | */ |
Michael Grand | 5047f03 | 2022-11-24 16:49:56 +0100 | [diff] [blame] | 109 | fih_ret |
Dominik Ermel | 8101c0c | 2020-05-19 13:01:16 +0000 | [diff] [blame] | 110 | boot_go(struct boot_rsp *rsp) |
| 111 | { |
| 112 | int rc = -1; |
Michael Grand | 5047f03 | 2022-11-24 16:49:56 +0100 | [diff] [blame] | 113 | FIH_DECLARE(fih_rc, FIH_FAILURE); |
Dominik Ermel | 8101c0c | 2020-05-19 13:01:16 +0000 | [diff] [blame] | 114 | |
| 115 | rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(0), &_fa_p); |
| 116 | assert(rc == 0); |
| 117 | |
| 118 | rc = boot_image_load_header(_fa_p, &_hdr); |
| 119 | if (rc != 0) |
| 120 | goto out; |
| 121 | |
Ederson de Souza | 040fc42 | 2024-08-15 16:35:07 -0700 | [diff] [blame] | 122 | #ifdef MCUBOOT_RAM_LOAD |
| 123 | static struct boot_loader_state state; |
Ederson de Souza | 993c2ff | 2025-03-24 20:36:21 -0700 | [diff] [blame] | 124 | BOOT_IMG_AREA(&state, 0) = _fa_p; |
Ederson de Souza | 040fc42 | 2024-08-15 16:35:07 -0700 | [diff] [blame] | 125 | state.imgs[0][0].hdr = _hdr; |
| 126 | |
| 127 | rc = boot_load_image_to_sram(&state); |
| 128 | if (rc != 0) |
| 129 | goto out; |
| 130 | #endif |
| 131 | |
Dominik Ermel | 8101c0c | 2020-05-19 13:01:16 +0000 | [diff] [blame] | 132 | #ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT |
Tamas Ban | ee6615d | 2020-09-30 07:58:48 +0100 | [diff] [blame] | 133 | FIH_CALL(boot_image_validate, fih_rc, _fa_p, &_hdr); |
Michael Grand | 5047f03 | 2022-11-24 16:49:56 +0100 | [diff] [blame] | 134 | if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) { |
Ederson de Souza | 040fc42 | 2024-08-15 16:35:07 -0700 | [diff] [blame] | 135 | #ifdef MCUBOOT_RAM_LOAD |
| 136 | boot_remove_image_from_sram(&state); |
| 137 | #endif |
Dominik Ermel | 8101c0c | 2020-05-19 13:01:16 +0000 | [diff] [blame] | 138 | goto out; |
| 139 | } |
Wouter Cappelle | bb7a39d | 2021-05-03 16:44:44 +0200 | [diff] [blame] | 140 | #elif defined(MCUBOOT_VALIDATE_PRIMARY_SLOT_ONCE) |
| 141 | FIH_CALL(boot_image_validate_once, fih_rc, _fa_p, &_hdr); |
Michael Grand | 5047f03 | 2022-11-24 16:49:56 +0100 | [diff] [blame] | 142 | if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) { |
Ederson de Souza | 040fc42 | 2024-08-15 16:35:07 -0700 | [diff] [blame] | 143 | #ifdef MCUBOOT_RAM_LOAD |
| 144 | boot_remove_image_from_sram(&state); |
| 145 | #endif |
Wouter Cappelle | bb7a39d | 2021-05-03 16:44:44 +0200 | [diff] [blame] | 146 | goto out; |
| 147 | } |
Tamas Ban | ee6615d | 2020-09-30 07:58:48 +0100 | [diff] [blame] | 148 | #else |
| 149 | fih_rc = FIH_SUCCESS; |
Dominik Ermel | 8101c0c | 2020-05-19 13:01:16 +0000 | [diff] [blame] | 150 | #endif /* MCUBOOT_VALIDATE_PRIMARY_SLOT */ |
| 151 | |
Dane Wagner | f2a6146 | 2024-12-04 10:46:40 -0600 | [diff] [blame] | 152 | #ifdef MCUBOOT_MEASURED_BOOT |
| 153 | rc = boot_save_boot_status(0, &_hdr, _fa_p); |
| 154 | if (rc != 0) { |
| 155 | BOOT_LOG_ERR("Failed to add image data to shared area"); |
| 156 | return rc; |
| 157 | } |
| 158 | #endif /* MCUBOOT_MEASURED_BOOT */ |
| 159 | |
| 160 | #ifdef MCUBOOT_DATA_SHARING |
| 161 | rc = boot_save_shared_data(&_hdr, _fa_p, 0, NULL); |
| 162 | if (rc != 0) { |
| 163 | BOOT_LOG_ERR("Failed to add data to shared memory area."); |
| 164 | return rc; |
| 165 | } |
| 166 | #endif /* MCUBOOT_DATA_SHARING */ |
| 167 | |
Dominik Ermel | 036d521 | 2021-07-01 11:07:41 +0000 | [diff] [blame] | 168 | rsp->br_flash_dev_id = flash_area_get_device_id(_fa_p); |
| 169 | rsp->br_image_off = flash_area_get_off(_fa_p); |
Dominik Ermel | 8101c0c | 2020-05-19 13:01:16 +0000 | [diff] [blame] | 170 | rsp->br_hdr = &_hdr; |
| 171 | |
| 172 | out: |
| 173 | flash_area_close(_fa_p); |
Tamas Ban | ee6615d | 2020-09-30 07:58:48 +0100 | [diff] [blame] | 174 | |
| 175 | FIH_RET(fih_rc); |
Dominik Ermel | 8101c0c | 2020-05-19 13:01:16 +0000 | [diff] [blame] | 176 | } |