blob: 75374d2db6ea39ab3f1501ce7c2e1fa6f26f4260 [file] [log] [blame]
Dominik Ermel8101c0c2020-05-19 13:01:16 +00001/*
2 * SPDX-License-Identifier: Apache-2.0
3 *
4 * Copyright (c) 2020 Nordic Semiconductor ASA
Tamas Banee6615d2020-09-30 07:58:48 +01005 * Copyright (c) 2020 Arm Limited
Dominik Ermel8101c0c2020-05-19 13:01:16 +00006 */
7
8#include <assert.h>
9#include "bootutil/image.h"
10#include "bootutil_priv.h"
11#include "bootutil/bootutil_log.h"
Jamie McCraec9fa6082023-07-21 10:23:17 +010012#include "bootutil/bootutil_public.h"
Tamas Banee6615d2020-09-30 07:58:48 +010013#include "bootutil/fault_injection_hardening.h"
Dominik Ermel8101c0c2020-05-19 13:01:16 +000014
15#include "mcuboot_config/mcuboot_config.h"
16
Carlos Falgueras GarcĂ­aa4b4b0f2021-06-22 10:00:22 +020017BOOT_LOG_MODULE_DECLARE(mcuboot);
Dominik Ermel8101c0c2020-05-19 13:01:16 +000018
19/* Variables passed outside of unit via poiters. */
20static const struct flash_area *_fa_p;
21static struct image_header _hdr = { 0 };
22
Wouter Cappelle953a7612021-05-03 16:53:05 +020023#if defined(MCUBOOT_VALIDATE_PRIMARY_SLOT) || defined(MCUBOOT_VALIDATE_PRIMARY_SLOT_ONCE)
Dominik Ermel8101c0c2020-05-19 13:01:16 +000024/**
25 * Validate hash of a primary boot image.
26 *
27 * @param[in] fa_p flash area pointer
28 * @param[in] hdr boot image header pointer
29 *
Tamas Banee6615d2020-09-30 07:58:48 +010030 * @return FIH_SUCCESS on success, error code otherwise
Dominik Ermel8101c0c2020-05-19 13:01:16 +000031 */
Michael Grand5047f032022-11-24 16:49:56 +010032fih_ret
Dominik Ermel8101c0c2020-05-19 13:01:16 +000033boot_image_validate(const struct flash_area *fa_p,
34 struct image_header *hdr)
35{
36 static uint8_t tmpbuf[BOOT_TMPBUF_SZ];
Michael Grand5047f032022-11-24 16:49:56 +010037 FIH_DECLARE(fih_rc, FIH_FAILURE);
Dominik Ermel8101c0c2020-05-19 13:01:16 +000038
Dominik Ermeld8db0252020-10-07 11:22:45 +000039 /* NOTE: The first argument to boot_image_validate, for enc_state pointer,
40 * is allowed to be NULL only because the single image loader compiles
41 * with BOOT_IMAGE_NUMBER == 1, which excludes the code that uses
42 * the pointer from compilation.
Dominik Ermel8101c0c2020-05-19 13:01:16 +000043 */
44 /* Validate hash */
Wouter Cappelle76792152022-01-19 17:28:55 +010045 if (IS_ENCRYPTED(hdr))
Wouter Cappelle953a7612021-05-03 16:53:05 +020046 {
47 /* Clear the encrypted flag we didn't supply a key
48 * This flag could be set if there was a decryption in place
49 * was performed. We will try to validate the image, and if still
50 * encrypted the validation will fail, and go in panic mode
51 */
Wouter Cappelle76792152022-01-19 17:28:55 +010052 hdr->ih_flags &= ~(ENCRYPTIONFLAGS);
Wouter Cappelle953a7612021-05-03 16:53:05 +020053 }
Tamas Banee6615d2020-09-30 07:58:48 +010054 FIH_CALL(bootutil_img_validate, fih_rc, NULL, 0, hdr, fa_p, tmpbuf,
55 BOOT_TMPBUF_SZ, NULL, 0, NULL);
Dominik Ermel8101c0c2020-05-19 13:01:16 +000056
Tamas Banee6615d2020-09-30 07:58:48 +010057 FIH_RET(fih_rc);
Dominik Ermel8101c0c2020-05-19 13:01:16 +000058}
Wouter Cappelle953a7612021-05-03 16:53:05 +020059#endif /* MCUBOOT_VALIDATE_PRIMARY_SLOT || MCUBOOT_VALIDATE_PRIMARY_SLOT_ONCE*/
Dominik Ermel8101c0c2020-05-19 13:01:16 +000060
Jamie McCrae21299732023-12-07 09:41:55 +000061#if defined(MCUBOOT_VALIDATE_PRIMARY_SLOT_ONCE)
Michael Grand5047f032022-11-24 16:49:56 +010062inline static fih_ret
Wouter Cappellebb7a39d2021-05-03 16:44:44 +020063boot_image_validate_once(const struct flash_area *fa_p,
64 struct image_header *hdr)
65{
66 static struct boot_swap_state state;
67 int rc;
Michael Grand5047f032022-11-24 16:49:56 +010068 FIH_DECLARE(fih_rc, FIH_FAILURE);
Wouter Cappellebb7a39d2021-05-03 16:44:44 +020069
70 memset(&state, 0, sizeof(struct boot_swap_state));
71 rc = boot_read_swap_state(fa_p, &state);
72 if (rc != 0)
73 FIH_RET(FIH_FAILURE);
74 if (state.magic != BOOT_MAGIC_GOOD
75 || state.image_ok != BOOT_FLAG_SET) {
76 /* At least validate the image once */
77 FIH_CALL(boot_image_validate, fih_rc, fa_p, hdr);
Michael Grand5047f032022-11-24 16:49:56 +010078 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
Wouter Cappellebb7a39d2021-05-03 16:44:44 +020079 FIH_RET(FIH_FAILURE);
80 }
81 if (state.magic != BOOT_MAGIC_GOOD) {
82 rc = boot_write_magic(fa_p);
83 if (rc != 0)
84 FIH_RET(FIH_FAILURE);
85 }
86 rc = boot_write_image_ok(fa_p);
87 if (rc != 0)
88 FIH_RET(FIH_FAILURE);
89 }
90 FIH_RET(FIH_SUCCESS);
91}
Jamie McCrae21299732023-12-07 09:41:55 +000092#endif
Wouter Cappellebb7a39d2021-05-03 16:44:44 +020093
Dominik Ermel8101c0c2020-05-19 13:01:16 +000094/**
Dominik Ermel8101c0c2020-05-19 13:01:16 +000095 * Gather information on image and prepare for booting.
96 *
97 * @parami[out] rsp Parameters for booting image, on success
98 *
Tamas Banee6615d2020-09-30 07:58:48 +010099 * @return FIH_SUCCESS on success; nonzero on failure.
Dominik Ermel8101c0c2020-05-19 13:01:16 +0000100 */
Michael Grand5047f032022-11-24 16:49:56 +0100101fih_ret
Dominik Ermel8101c0c2020-05-19 13:01:16 +0000102boot_go(struct boot_rsp *rsp)
103{
104 int rc = -1;
Michael Grand5047f032022-11-24 16:49:56 +0100105 FIH_DECLARE(fih_rc, FIH_FAILURE);
Dominik Ermel8101c0c2020-05-19 13:01:16 +0000106
107 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(0), &_fa_p);
108 assert(rc == 0);
109
110 rc = boot_image_load_header(_fa_p, &_hdr);
111 if (rc != 0)
112 goto out;
113
114#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Tamas Banee6615d2020-09-30 07:58:48 +0100115 FIH_CALL(boot_image_validate, fih_rc, _fa_p, &_hdr);
Michael Grand5047f032022-11-24 16:49:56 +0100116 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
Dominik Ermel8101c0c2020-05-19 13:01:16 +0000117 goto out;
118 }
Wouter Cappellebb7a39d2021-05-03 16:44:44 +0200119#elif defined(MCUBOOT_VALIDATE_PRIMARY_SLOT_ONCE)
120 FIH_CALL(boot_image_validate_once, fih_rc, _fa_p, &_hdr);
Michael Grand5047f032022-11-24 16:49:56 +0100121 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
Wouter Cappellebb7a39d2021-05-03 16:44:44 +0200122 goto out;
123 }
Tamas Banee6615d2020-09-30 07:58:48 +0100124#else
125 fih_rc = FIH_SUCCESS;
Dominik Ermel8101c0c2020-05-19 13:01:16 +0000126#endif /* MCUBOOT_VALIDATE_PRIMARY_SLOT */
127
Dominik Ermel036d5212021-07-01 11:07:41 +0000128 rsp->br_flash_dev_id = flash_area_get_device_id(_fa_p);
129 rsp->br_image_off = flash_area_get_off(_fa_p);
Dominik Ermel8101c0c2020-05-19 13:01:16 +0000130 rsp->br_hdr = &_hdr;
131
132out:
133 flash_area_close(_fa_p);
Tamas Banee6615d2020-09-30 07:58:48 +0100134
135 FIH_RET(fih_rc);
Dominik Ermel8101c0c2020-05-19 13:01:16 +0000136}