blob: 38b121cd47178e24d77cd98a135ac21badd1e08e [file] [log] [blame]
Jamie McCrae215345f2023-08-16 07:37:18 +01001/*
2 * SPDX-License-Identifier: Apache-2.0
3 *
4 * Copyright (c) 2020 Arm Limited
5 * Copyright (c) 2020-2023 Nordic Semiconductor ASA
6 */
7
8#include <assert.h>
9#include <zephyr/kernel.h>
10#include <zephyr/devicetree.h>
11#include <zephyr/drivers/gpio.h>
12#include "bootutil/image.h"
13#include "bootutil_priv.h"
14#include "bootutil/bootutil_log.h"
15#include "bootutil/bootutil_public.h"
16#include "bootutil/fault_injection_hardening.h"
17
18#include "io/io.h"
19#include "mcuboot_config/mcuboot_config.h"
20
21BOOT_LOG_MODULE_DECLARE(mcuboot);
22
23/* Variables passed outside of unit via poiters. */
24static const struct flash_area *_fa_p;
25static struct image_header _hdr = { 0 };
26
27#if defined(MCUBOOT_VALIDATE_PRIMARY_SLOT) || defined(MCUBOOT_VALIDATE_PRIMARY_SLOT_ONCE)
28/**
29 * Validate hash of a primary boot image.
30 *
31 * @param[in] fa_p flash area pointer
32 * @param[in] hdr boot image header pointer
33 *
34 * @return FIH_SUCCESS on success, error code otherwise
35 */
36fih_ret
37boot_image_validate(const struct flash_area *fa_p,
38 struct image_header *hdr)
39{
40 static uint8_t tmpbuf[BOOT_TMPBUF_SZ];
41 FIH_DECLARE(fih_rc, FIH_FAILURE);
42
43 /* NOTE: The first argument to boot_image_validate, for enc_state pointer,
44 * is allowed to be NULL only because the single image loader compiles
45 * with BOOT_IMAGE_NUMBER == 1, which excludes the code that uses
46 * the pointer from compilation.
47 */
48 /* Validate hash */
49 if (IS_ENCRYPTED(hdr))
50 {
51 /* Clear the encrypted flag we didn't supply a key
52 * This flag could be set if there was a decryption in place
53 * was performed. We will try to validate the image, and if still
54 * encrypted the validation will fail, and go in panic mode
55 */
56 hdr->ih_flags &= ~(ENCRYPTIONFLAGS);
57 }
58 FIH_CALL(bootutil_img_validate, fih_rc, NULL, 0, hdr, fa_p, tmpbuf,
59 BOOT_TMPBUF_SZ, NULL, 0, NULL);
60
61 FIH_RET(fih_rc);
62}
63#endif /* MCUBOOT_VALIDATE_PRIMARY_SLOT || MCUBOOT_VALIDATE_PRIMARY_SLOT_ONCE*/
64
65inline static fih_ret
66boot_image_validate_once(const struct flash_area *fa_p,
67 struct image_header *hdr)
68{
69 static struct boot_swap_state state;
70 int rc;
71 FIH_DECLARE(fih_rc, FIH_FAILURE);
72
73 memset(&state, 0, sizeof(struct boot_swap_state));
74 rc = boot_read_swap_state(fa_p, &state);
75 if (rc != 0)
76 FIH_RET(FIH_FAILURE);
77 if (state.magic != BOOT_MAGIC_GOOD
78 || state.image_ok != BOOT_FLAG_SET) {
79 /* At least validate the image once */
80 FIH_CALL(boot_image_validate, fih_rc, fa_p, hdr);
81 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
82 FIH_RET(FIH_FAILURE);
83 }
84 if (state.magic != BOOT_MAGIC_GOOD) {
85 rc = boot_write_magic(fa_p);
86 if (rc != 0)
87 FIH_RET(FIH_FAILURE);
88 }
89 rc = boot_write_image_ok(fa_p);
90 if (rc != 0)
91 FIH_RET(FIH_FAILURE);
92 }
93 FIH_RET(FIH_SUCCESS);
94}
95
96/**
97 * Validates that an image in a slot is OK to boot.
98 *
99 * @param[in] slot Slot number to check
100 * @param[out] rsp Parameters for booting image, on success
101 *
102 * @return FIH_SUCCESS on success; non-zero on failure.
103 */
104static fih_ret validate_image_slot(int slot, struct boot_rsp *rsp)
105{
106 int rc = -1;
107 FIH_DECLARE(fih_rc, FIH_FAILURE);
108
109 rc = flash_area_open(slot, &_fa_p);
110 assert(rc == 0);
111
112 rc = boot_image_load_header(_fa_p, &_hdr);
113 if (rc != 0) {
114 goto other;
115 }
116
117#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
118 FIH_CALL(boot_image_validate, fih_rc, _fa_p, &_hdr);
119 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
120 goto other;
121 }
122#elif defined(MCUBOOT_VALIDATE_PRIMARY_SLOT_ONCE)
123 FIH_CALL(boot_image_validate_once, fih_rc, _fa_p, &_hdr);
124 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
125 goto other;
126 }
127#else
128 fih_rc = FIH_SUCCESS;
129#endif /* MCUBOOT_VALIDATE_PRIMARY_SLOT */
130
131 rsp->br_flash_dev_id = flash_area_get_device_id(_fa_p);
132 rsp->br_image_off = flash_area_get_off(_fa_p);
133 rsp->br_hdr = &_hdr;
134
135other:
136 flash_area_close(_fa_p);
137
138 FIH_RET(fih_rc);
139}
140
141/**
142 * Gather information on image and prepare for booting. Will boot from main
143 * image if none of the enabled entrance modes for the firmware loader are set,
144 * otherwise will boot the firmware loader. Note: firmware loader must be a
145 * valid signed image with the same signing key as the application image.
146 *
147 * @param[out] rsp Parameters for booting image, on success
148 *
149 * @return FIH_SUCCESS on success; non-zero on failure.
150 */
151fih_ret
152boot_go(struct boot_rsp *rsp)
153{
154 bool boot_firmware_loader = false;
155 FIH_DECLARE(fih_rc, FIH_FAILURE);
156
157#ifdef CONFIG_BOOT_FIRMWARE_LOADER_ENTRANCE_GPIO
158 if (io_detect_pin() &&
159 !io_boot_skip_serial_recovery()) {
160 boot_firmware_loader = true;
161 }
162#endif
163
164#ifdef CONFIG_BOOT_FIRMWARE_LOADER_PIN_RESET
165 if (io_detect_pin_reset()) {
166 boot_firmware_loader = true;
167 }
168#endif
169
170#ifdef CONFIG_BOOT_FIRMWARE_LOADER_BOOT_MODE
171 if (io_detect_boot_mode()) {
172 boot_firmware_loader = true;
173 }
174#endif
175
176 /* Check if firmware loader button is pressed. TODO: check all entrance methods */
177 if (boot_firmware_loader == true) {
178 FIH_CALL(validate_image_slot, fih_rc, FLASH_AREA_IMAGE_SECONDARY(0), rsp);
179
180 if (FIH_EQ(fih_rc, FIH_SUCCESS)) {
181 FIH_RET(fih_rc);
182 }
183 }
184
185 FIH_CALL(validate_image_slot, fih_rc, FLASH_AREA_IMAGE_PRIMARY(0), rsp);
186
187#ifdef CONFIG_BOOT_FIRMWARE_LOADER_NO_APPLICATION
188 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
189 FIH_CALL(validate_image_slot, fih_rc, FLASH_AREA_IMAGE_SECONDARY(0), rsp);
190 }
191#endif
192
193 FIH_RET(fih_rc);
194}