blob: a8b6b4778a3c4daac9d70067511cc77a14f5938a [file] [log] [blame]
Christopher Collins92ea77f2016-12-12 15:59:26 -08001/*
David Brownaac71112020-02-03 16:13:42 -07002 * SPDX-License-Identifier: Apache-2.0
3 *
4 * Copyright (c) 2017-2019 Linaro LTD
5 * Copyright (c) 2016-2019 JUUL Labs
Raef Colese8fe6cf2020-05-26 13:07:40 +01006 * Copyright (c) 2019-2020 Arm Limited
David Brownaac71112020-02-03 16:13:42 -07007 *
8 * Original license:
9 *
Christopher Collins92ea77f2016-12-12 15:59:26 -080010 * Licensed to the Apache Software Foundation (ASF) under one
11 * or more contributor license agreements. See the NOTICE file
12 * distributed with this work for additional information
13 * regarding copyright ownership. The ASF licenses this file
14 * to you under the Apache License, Version 2.0 (the
15 * "License"); you may not use this file except in compliance
16 * with the License. You may obtain a copy of the License at
17 *
18 * http://www.apache.org/licenses/LICENSE-2.0
19 *
20 * Unless required by applicable law or agreed to in writing,
21 * software distributed under the License is distributed on an
22 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
23 * KIND, either express or implied. See the License for the
24 * specific language governing permissions and limitations
25 * under the License.
26 */
27
Christopher Collins92ea77f2016-12-12 15:59:26 -080028#include <string.h>
29#include <inttypes.h>
Fabio Utziga0bc9b52017-06-28 09:19:55 -030030#include <stddef.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080031
Christopher Collins92ea77f2016-12-12 15:59:26 -080032#include "sysflash/sysflash.h"
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020033#include "flash_map_backend/flash_map_backend.h"
34
Christopher Collins92ea77f2016-12-12 15:59:26 -080035#include "bootutil/image.h"
36#include "bootutil/bootutil.h"
37#include "bootutil_priv.h"
Fabio Utzig7ebb7c22017-04-26 10:59:31 -030038#include "bootutil/bootutil_log.h"
Raef Colese8fe6cf2020-05-26 13:07:40 +010039#include "bootutil/fault_injection_hardening.h"
Fabio Utzigba829042018-09-18 08:29:34 -030040#ifdef MCUBOOT_ENC_IMAGES
41#include "bootutil/enc_key.h"
42#endif
Fabio Utzig7ebb7c22017-04-26 10:59:31 -030043
Emanuele Di Santo9f1933d2018-11-20 10:59:59 +010044MCUBOOT_LOG_MODULE_DECLARE(mcuboot);
45
Fabio Utzig10ee6482019-08-01 12:04:52 -030046/* Currently only used by imgmgr */
Christopher Collins92ea77f2016-12-12 15:59:26 -080047int boot_current_slot;
48
Fabio Utzig24a273d2017-04-20 08:21:31 -030049const uint32_t boot_img_magic[] = {
Christopher Collins92ea77f2016-12-12 15:59:26 -080050 0xf395c277,
51 0x7fefd260,
52 0x0f505235,
53 0x8079b62c,
54};
55
Hovland, Sigvart1d96f362018-09-25 13:23:42 +020056#define BOOT_MAGIC_ARR_SZ \
57 (sizeof boot_img_magic / sizeof boot_img_magic[0])
58
Christopher Collins92ea77f2016-12-12 15:59:26 -080059struct boot_swap_table {
David Vincze2d736ad2019-02-18 11:50:22 +010060 uint8_t magic_primary_slot;
61 uint8_t magic_secondary_slot;
62 uint8_t image_ok_primary_slot;
63 uint8_t image_ok_secondary_slot;
64 uint8_t copy_done_primary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -080065
Fabio Utzigb5b2f552017-06-30 10:03:47 -030066 uint8_t swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -080067};
68
69/**
70 * This set of tables maps image trailer contents to swap operation type.
71 * When searching for a match, these tables must be iterated sequentially.
Fabio Utzigb5b2f552017-06-30 10:03:47 -030072 *
David Vincze2d736ad2019-02-18 11:50:22 +010073 * NOTE: the table order is very important. The settings in the secondary
74 * slot always are priority to the primary slot and should be located
75 * earlier in the table.
Fabio Utzigb5b2f552017-06-30 10:03:47 -030076 *
77 * The table lists only states where there is action needs to be taken by
78 * the bootloader, as in starting/finishing a swap operation.
Christopher Collins92ea77f2016-12-12 15:59:26 -080079 */
80static const struct boot_swap_table boot_swap_tables[] = {
81 {
David Vincze2d736ad2019-02-18 11:50:22 +010082 .magic_primary_slot = BOOT_MAGIC_ANY,
83 .magic_secondary_slot = BOOT_MAGIC_GOOD,
84 .image_ok_primary_slot = BOOT_FLAG_ANY,
85 .image_ok_secondary_slot = BOOT_FLAG_UNSET,
86 .copy_done_primary_slot = BOOT_FLAG_ANY,
87 .swap_type = BOOT_SWAP_TYPE_TEST,
Christopher Collins92ea77f2016-12-12 15:59:26 -080088 },
Christopher Collins92ea77f2016-12-12 15:59:26 -080089 {
David Vincze2d736ad2019-02-18 11:50:22 +010090 .magic_primary_slot = BOOT_MAGIC_ANY,
91 .magic_secondary_slot = BOOT_MAGIC_GOOD,
92 .image_ok_primary_slot = BOOT_FLAG_ANY,
93 .image_ok_secondary_slot = BOOT_FLAG_SET,
94 .copy_done_primary_slot = BOOT_FLAG_ANY,
95 .swap_type = BOOT_SWAP_TYPE_PERM,
Christopher Collins92ea77f2016-12-12 15:59:26 -080096 },
Christopher Collins92ea77f2016-12-12 15:59:26 -080097 {
David Vincze2d736ad2019-02-18 11:50:22 +010098 .magic_primary_slot = BOOT_MAGIC_GOOD,
99 .magic_secondary_slot = BOOT_MAGIC_UNSET,
100 .image_ok_primary_slot = BOOT_FLAG_UNSET,
101 .image_ok_secondary_slot = BOOT_FLAG_ANY,
102 .copy_done_primary_slot = BOOT_FLAG_SET,
103 .swap_type = BOOT_SWAP_TYPE_REVERT,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800104 },
105};
106
107#define BOOT_SWAP_TABLES_COUNT \
108 (sizeof boot_swap_tables / sizeof boot_swap_tables[0])
109
Raef Colese8fe6cf2020-05-26 13:07:40 +0100110/**
111 * @brief Determine if the data at two memory addresses is equal
112 *
113 * @param s1 The first memory region to compare.
114 * @param s2 The second memory region to compare.
115 * @param n The amount of bytes to compare.
116 *
117 * @note This function does not comply with the specification of memcmp,
118 * so should not be considered a drop-in replacement. It has no
119 * constant time execution. The point is to make sure that all the
120 * bytes are compared and detect if loop was abused and some cycles
121 * was skipped due to fault injection.
122 *
123 * @return FIH_SUCCESS if memory regions are equal, otherwise FIH_FAILURE
124 */
125#ifdef MCUBOOT_FIH_PROFILE_OFF
126inline
127fih_int boot_fih_memequal(const void *s1, const void *s2, size_t n)
128{
129 return memcmp(s1, s2, n);
130}
131#else
132fih_int boot_fih_memequal(const void *s1, const void *s2, size_t n)
133{
134 size_t i;
135 uint8_t *s1_p = (uint8_t*) s1;
136 uint8_t *s2_p = (uint8_t*) s2;
137 fih_int ret = FIH_FAILURE;
138
139 for (i = 0; i < n; i++) {
140 if (s1_p[i] != s2_p[i]) {
141 goto out;
142 }
143 }
144 if (i == n) {
145 ret = FIH_SUCCESS;
146 }
147
148out:
149 FIH_RET(ret);
150}
151#endif
152
Fabio Utzig39000012018-07-30 12:40:20 -0300153static int
Fabio Utzig178be542018-09-19 08:12:56 -0300154boot_magic_decode(const uint32_t *magic)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800155{
Fabio Utzig24a273d2017-04-20 08:21:31 -0300156 if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800157 return BOOT_MAGIC_GOOD;
158 }
Fabio Utzig178be542018-09-19 08:12:56 -0300159 return BOOT_MAGIC_BAD;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800160}
161
Fabio Utzig39000012018-07-30 12:40:20 -0300162static int
Fabio Utzig178be542018-09-19 08:12:56 -0300163boot_flag_decode(uint8_t flag)
Fabio Utzig39000012018-07-30 12:40:20 -0300164{
Fabio Utzig39000012018-07-30 12:40:20 -0300165 if (flag != BOOT_FLAG_SET) {
166 return BOOT_FLAG_BAD;
167 }
168 return BOOT_FLAG_SET;
169}
170
Christopher Collinsa1c12042019-05-23 14:00:28 -0700171/**
172 * Determines if a status source table is satisfied by the specified magic
173 * code.
174 *
175 * @param tbl_val A magic field from a status source table.
176 * @param val The magic value in a trailer, encoded as a
177 * BOOT_MAGIC_[...].
178 *
179 * @return 1 if the two values are compatible;
180 * 0 otherwise.
181 */
182int
183boot_magic_compatible_check(uint8_t tbl_val, uint8_t val)
184{
185 switch (tbl_val) {
186 case BOOT_MAGIC_ANY:
187 return 1;
188
189 case BOOT_MAGIC_NOTGOOD:
190 return val != BOOT_MAGIC_GOOD;
191
192 default:
193 return tbl_val == val;
194 }
195}
196
Christopher Collins92ea77f2016-12-12 15:59:26 -0800197uint32_t
Fabio Utzig3fbbdac2019-12-19 15:18:23 -0300198boot_status_sz(uint32_t min_write_sz)
199{
200 return /* state for all sectors */
201 BOOT_STATUS_MAX_ENTRIES * BOOT_STATUS_STATE_COUNT * min_write_sz;
202}
203
204uint32_t
David Brownab449182019-11-15 09:32:52 -0700205boot_trailer_sz(uint32_t min_write_sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800206{
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300207 return /* state for all sectors */
Fabio Utzig3fbbdac2019-12-19 15:18:23 -0300208 boot_status_sz(min_write_sz) +
Fabio Utzigba829042018-09-18 08:29:34 -0300209#ifdef MCUBOOT_ENC_IMAGES
210 /* encryption keys */
Fabio Utzig4741c452019-12-19 15:32:41 -0300211# if MCUBOOT_SWAP_SAVE_ENCTLV
212 BOOT_ENC_TLV_ALIGN_SIZE * 2 +
213# else
Fabio Utzigba829042018-09-18 08:29:34 -0300214 BOOT_ENC_KEY_SIZE * 2 +
Fabio Utzig4741c452019-12-19 15:32:41 -0300215# endif
Fabio Utzigba829042018-09-18 08:29:34 -0300216#endif
Christopher Collins2adef702019-05-22 14:37:31 -0700217 /* swap_type + copy_done + image_ok + swap_size */
218 BOOT_MAX_ALIGN * 4 +
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300219 BOOT_MAGIC_SZ;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800220}
221
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400222int
Fabio Utzigb0f04732019-07-31 09:49:19 -0300223boot_status_entries(int image_index, const struct flash_area *fap)
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400224{
Fabio Utzig12d59162019-11-28 10:01:59 -0300225#if MCUBOOT_SWAP_USING_SCRATCH
David Vinczeb75c12a2019-03-22 14:58:33 +0100226 if (fap->fa_id == FLASH_AREA_IMAGE_SCRATCH) {
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400227 return BOOT_STATUS_STATE_COUNT;
Fabio Utzig12d59162019-11-28 10:01:59 -0300228 } else
229#endif
230 if (fap->fa_id == FLASH_AREA_IMAGE_PRIMARY(image_index) ||
Fabio Utzigb0f04732019-07-31 09:49:19 -0300231 fap->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
David Vinczeb75c12a2019-03-22 14:58:33 +0100232 return BOOT_STATUS_STATE_COUNT * BOOT_STATUS_MAX_ENTRIES;
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400233 }
Fabio Utzig9d160092019-08-09 07:46:34 -0300234 return -1;
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400235}
236
Christopher Collins92ea77f2016-12-12 15:59:26 -0800237uint32_t
238boot_status_off(const struct flash_area *fap)
239{
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300240 uint32_t off_from_end;
241 uint8_t elem_sz;
242
243 elem_sz = flash_area_align(fap);
244
Christopher Collins2adef702019-05-22 14:37:31 -0700245 off_from_end = boot_trailer_sz(elem_sz);
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300246
247 assert(off_from_end <= fap->fa_size);
248 return fap->fa_size - off_from_end;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800249}
250
Fabio Utzig4e8113b2019-08-09 09:11:18 -0300251static inline uint32_t
252boot_magic_off(const struct flash_area *fap)
253{
254 return fap->fa_size - BOOT_MAGIC_SZ;
255}
256
257static inline uint32_t
258boot_image_ok_off(const struct flash_area *fap)
259{
260 return boot_magic_off(fap) - BOOT_MAX_ALIGN;
261}
262
263static inline uint32_t
264boot_copy_done_off(const struct flash_area *fap)
265{
266 return boot_image_ok_off(fap) - BOOT_MAX_ALIGN;
267}
268
Christopher Collinsa1c12042019-05-23 14:00:28 -0700269uint32_t
David Vinczee2453472019-06-17 12:31:59 +0200270boot_swap_info_off(const struct flash_area *fap)
Christopher Collinsa1c12042019-05-23 14:00:28 -0700271{
Fabio Utzig4e8113b2019-08-09 09:11:18 -0300272 return boot_copy_done_off(fap) - BOOT_MAX_ALIGN;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700273}
274
Fabio Utzig4e8113b2019-08-09 09:11:18 -0300275static inline uint32_t
Fabio Utzig46490722017-09-04 15:34:32 -0300276boot_swap_size_off(const struct flash_area *fap)
277{
Fabio Utzig4e8113b2019-08-09 09:11:18 -0300278 return boot_swap_info_off(fap) - BOOT_MAX_ALIGN;
Fabio Utzig46490722017-09-04 15:34:32 -0300279}
280
Fabio Utzigba829042018-09-18 08:29:34 -0300281#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig4e8113b2019-08-09 09:11:18 -0300282static inline uint32_t
Fabio Utzigba829042018-09-18 08:29:34 -0300283boot_enc_key_off(const struct flash_area *fap, uint8_t slot)
284{
Fabio Utzig4741c452019-12-19 15:32:41 -0300285#if MCUBOOT_SWAP_SAVE_ENCTLV
286 return boot_swap_size_off(fap) - ((slot + 1) *
287 ((((BOOT_ENC_TLV_SIZE - 1) / BOOT_MAX_ALIGN) + 1) * BOOT_MAX_ALIGN));
288#else
Fabio Utzig4e8113b2019-08-09 09:11:18 -0300289 return boot_swap_size_off(fap) - ((slot + 1) * BOOT_ENC_KEY_SIZE);
Fabio Utzig4741c452019-12-19 15:32:41 -0300290#endif
Fabio Utzigba829042018-09-18 08:29:34 -0300291}
292#endif
293
Christopher Collins92ea77f2016-12-12 15:59:26 -0800294int
295boot_read_swap_state(const struct flash_area *fap,
296 struct boot_swap_state *state)
297{
Hovland, Sigvart1d96f362018-09-25 13:23:42 +0200298 uint32_t magic[BOOT_MAGIC_ARR_SZ];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800299 uint32_t off;
David Vinczee2453472019-06-17 12:31:59 +0200300 uint8_t swap_info;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800301 int rc;
302
303 off = boot_magic_off(fap);
Fabio Utzig178be542018-09-19 08:12:56 -0300304 rc = flash_area_read_is_empty(fap, off, magic, BOOT_MAGIC_SZ);
305 if (rc < 0) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800306 return BOOT_EFLASH;
307 }
Fabio Utzig178be542018-09-19 08:12:56 -0300308 if (rc == 1) {
309 state->magic = BOOT_MAGIC_UNSET;
310 } else {
311 state->magic = boot_magic_decode(magic);
312 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800313
David Vinczee2453472019-06-17 12:31:59 +0200314 off = boot_swap_info_off(fap);
315 rc = flash_area_read_is_empty(fap, off, &swap_info, sizeof swap_info);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700316 if (rc < 0) {
317 return BOOT_EFLASH;
318 }
David Vinczee2453472019-06-17 12:31:59 +0200319
320 /* Extract the swap type and image number */
321 state->swap_type = BOOT_GET_SWAP_TYPE(swap_info);
322 state->image_num = BOOT_GET_IMAGE_NUM(swap_info);
323
Christopher Collinsa1c12042019-05-23 14:00:28 -0700324 if (rc == 1 || state->swap_type > BOOT_SWAP_TYPE_REVERT) {
325 state->swap_type = BOOT_SWAP_TYPE_NONE;
David Vinczee2453472019-06-17 12:31:59 +0200326 state->image_num = 0;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700327 }
328
Christopher Collins2adef702019-05-22 14:37:31 -0700329 off = boot_copy_done_off(fap);
330 rc = flash_area_read_is_empty(fap, off, &state->copy_done,
331 sizeof state->copy_done);
332 if (rc < 0) {
333 return BOOT_EFLASH;
334 }
335 if (rc == 1) {
336 state->copy_done = BOOT_FLAG_UNSET;
337 } else {
338 state->copy_done = boot_flag_decode(state->copy_done);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800339 }
340
341 off = boot_image_ok_off(fap);
David Vincze2d736ad2019-02-18 11:50:22 +0100342 rc = flash_area_read_is_empty(fap, off, &state->image_ok,
343 sizeof state->image_ok);
Fabio Utzig178be542018-09-19 08:12:56 -0300344 if (rc < 0) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800345 return BOOT_EFLASH;
346 }
Fabio Utzig178be542018-09-19 08:12:56 -0300347 if (rc == 1) {
348 state->image_ok = BOOT_FLAG_UNSET;
349 } else {
350 state->image_ok = boot_flag_decode(state->image_ok);
351 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800352
353 return 0;
354}
355
356/**
357 * Reads the image trailer from the scratch area.
358 */
359int
Fabio Utzig2473ac02017-05-02 12:45:02 -0300360boot_read_swap_state_by_id(int flash_area_id, struct boot_swap_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800361{
362 const struct flash_area *fap;
363 int rc;
364
Fabio Utzigb0f04732019-07-31 09:49:19 -0300365 rc = flash_area_open(flash_area_id, &fap);
366 if (rc != 0) {
367 return BOOT_EFLASH;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300368 }
369
370 rc = boot_read_swap_state(fap, state);
Fabio Utzigacfba2e2017-05-22 11:06:29 -0400371 flash_area_close(fap);
372 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800373}
374
Fabio Utzigdf0cc502019-08-09 09:10:41 -0300375/**
376 * This functions tries to locate the status area after an aborted swap,
377 * by looking for the magic in the possible locations.
378 *
Sam Bristowd0ca0ff2019-10-30 20:51:35 +1300379 * If the magic is successfully found, a flash_area * is returned and it
Fabio Utzigdf0cc502019-08-09 09:10:41 -0300380 * is the responsibility of the called to close it.
381 *
382 * @returns 0 on success, -1 on errors
383 */
384static int
385boot_find_status(int image_index, const struct flash_area **fap)
386{
387 uint32_t magic[BOOT_MAGIC_ARR_SZ];
388 uint32_t off;
389 uint8_t areas[2] = {
Fabio Utzig12d59162019-11-28 10:01:59 -0300390#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzigdf0cc502019-08-09 09:10:41 -0300391 FLASH_AREA_IMAGE_SCRATCH,
Fabio Utzig12d59162019-11-28 10:01:59 -0300392#endif
Fabio Utzig3c446072019-11-22 12:48:26 -0300393 FLASH_AREA_IMAGE_PRIMARY(image_index),
Fabio Utzigdf0cc502019-08-09 09:10:41 -0300394 };
395 unsigned int i;
396 int rc;
397
398 /*
399 * In the middle a swap, tries to locate the area that is currently
400 * storing a valid magic, first on the primary slot, then on scratch.
401 * Both "slots" can end up being temporary storage for a swap and it
402 * is assumed that if magic is valid then other metadata is too,
403 * because magic is always written in the last step.
404 */
405
406 for (i = 0; i < sizeof(areas) / sizeof(areas[0]); i++) {
407 rc = flash_area_open(areas[i], fap);
408 if (rc != 0) {
409 return rc;
410 }
411
412 off = boot_magic_off(*fap);
413 rc = flash_area_read(*fap, off, magic, BOOT_MAGIC_SZ);
414 if (rc != 0) {
415 flash_area_close(*fap);
416 return rc;
417 }
418
419 if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0) {
420 return 0;
421 }
422
423 flash_area_close(*fap);
424 }
425
426 /* If we got here, no magic was found */
427 return -1;
428}
429
Christopher Collins92ea77f2016-12-12 15:59:26 -0800430int
Fabio Utzigb0f04732019-07-31 09:49:19 -0300431boot_read_swap_size(int image_index, uint32_t *swap_size)
Fabio Utzig46490722017-09-04 15:34:32 -0300432{
Fabio Utzig46490722017-09-04 15:34:32 -0300433 uint32_t off;
434 const struct flash_area *fap;
435 int rc;
436
Fabio Utzigdf0cc502019-08-09 09:10:41 -0300437 rc = boot_find_status(image_index, &fap);
438 if (rc == 0) {
439 off = boot_swap_size_off(fap);
440 rc = flash_area_read(fap, off, swap_size, sizeof *swap_size);
Fabio Utzig46490722017-09-04 15:34:32 -0300441 flash_area_close(fap);
Fabio Utzig46490722017-09-04 15:34:32 -0300442 }
443
Fabio Utzig46490722017-09-04 15:34:32 -0300444 return rc;
445}
446
Fabio Utzigba829042018-09-18 08:29:34 -0300447#ifdef MCUBOOT_ENC_IMAGES
448int
Fabio Utzig4741c452019-12-19 15:32:41 -0300449boot_read_enc_key(int image_index, uint8_t slot, struct boot_status *bs)
Fabio Utzigba829042018-09-18 08:29:34 -0300450{
Fabio Utzigba829042018-09-18 08:29:34 -0300451 uint32_t off;
452 const struct flash_area *fap;
Fabio Utzig4741c452019-12-19 15:32:41 -0300453#if MCUBOOT_SWAP_SAVE_ENCTLV
454 int i;
455#endif
Fabio Utzigba829042018-09-18 08:29:34 -0300456 int rc;
457
Fabio Utzigdf0cc502019-08-09 09:10:41 -0300458 rc = boot_find_status(image_index, &fap);
459 if (rc == 0) {
460 off = boot_enc_key_off(fap, slot);
Fabio Utzig4741c452019-12-19 15:32:41 -0300461#if MCUBOOT_SWAP_SAVE_ENCTLV
462 rc = flash_area_read(fap, off, bs->enctlv[slot], BOOT_ENC_TLV_ALIGN_SIZE);
463 if (rc == 0) {
464 for (i = 0; i < BOOT_ENC_TLV_ALIGN_SIZE; i++) {
465 if (bs->enctlv[slot][i] != 0xff) {
466 break;
467 }
468 }
469 /* Only try to decrypt non-erased TLV metadata */
470 if (i != BOOT_ENC_TLV_ALIGN_SIZE) {
471 rc = boot_enc_decrypt(bs->enctlv[slot], bs->enckey[slot]);
472 }
473 }
474#else
475 rc = flash_area_read(fap, off, bs->enckey[slot], BOOT_ENC_KEY_SIZE);
476#endif
Fabio Utzigba829042018-09-18 08:29:34 -0300477 flash_area_close(fap);
Fabio Utzigba829042018-09-18 08:29:34 -0300478 }
479
Fabio Utzigba829042018-09-18 08:29:34 -0300480 return rc;
481}
482#endif
Fabio Utzig46490722017-09-04 15:34:32 -0300483
484int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800485boot_write_magic(const struct flash_area *fap)
486{
487 uint32_t off;
488 int rc;
489
490 off = boot_magic_off(fap);
491
Ben McCrea4c0ee952019-08-28 09:13:24 -0700492 BOOT_LOG_DBG("writing magic; fa_id=%d off=0x%lx (0x%lx)",
Marti Bolivar99ec3832019-09-12 21:21:26 -0600493 fap->fa_id, (unsigned long)off,
494 (unsigned long)(fap->fa_off + off));
Fabio Utzig24a273d2017-04-20 08:21:31 -0300495 rc = flash_area_write(fap, off, boot_img_magic, BOOT_MAGIC_SZ);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800496 if (rc != 0) {
497 return BOOT_EFLASH;
498 }
499
500 return 0;
501}
502
Fabio Utzig1a1ec172019-08-09 10:21:43 -0300503/**
504 * Write trailer data; status bytes, swap_size, etc
505 *
506 * @returns 0 on success, != 0 on error.
507 */
Fabio Utzig2473ac02017-05-02 12:45:02 -0300508static int
Fabio Utzig1a1ec172019-08-09 10:21:43 -0300509boot_write_trailer(const struct flash_area *fap, uint32_t off,
510 const uint8_t *inbuf, uint8_t inlen)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800511{
Fabio Utzig644b8d42017-04-20 07:56:05 -0300512 uint8_t buf[BOOT_MAX_ALIGN];
David Brown9d725462017-01-23 15:50:58 -0700513 uint8_t align;
Fabio Utzig39000012018-07-30 12:40:20 -0300514 uint8_t erased_val;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700515 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800516
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200517 align = flash_area_align(fap);
Fabio Utzig1a1ec172019-08-09 10:21:43 -0300518 if (inlen > BOOT_MAX_ALIGN || align > BOOT_MAX_ALIGN) {
519 return -1;
520 }
Fabio Utzig39000012018-07-30 12:40:20 -0300521 erased_val = flash_area_erased_val(fap);
Fabio Utzig1a1ec172019-08-09 10:21:43 -0300522 if (align < inlen) {
523 align = inlen;
524 }
525 memcpy(buf, inbuf, inlen);
526 memset(&buf[inlen], erased_val, align - inlen);
David Brown9d725462017-01-23 15:50:58 -0700527
528 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800529 if (rc != 0) {
530 return BOOT_EFLASH;
531 }
532
533 return 0;
534}
535
Fabio Utzig1a1ec172019-08-09 10:21:43 -0300536static int
537boot_write_trailer_flag(const struct flash_area *fap, uint32_t off,
538 uint8_t flag_val)
539{
540 const uint8_t buf[1] = { flag_val };
541 return boot_write_trailer(fap, off, buf, 1);
542}
543
Christopher Collins92ea77f2016-12-12 15:59:26 -0800544int
Fabio Utzig2473ac02017-05-02 12:45:02 -0300545boot_write_copy_done(const struct flash_area *fap)
546{
Christopher Collinsa1c12042019-05-23 14:00:28 -0700547 uint32_t off;
548
549 off = boot_copy_done_off(fap);
Ben McCrea4c0ee952019-08-28 09:13:24 -0700550 BOOT_LOG_DBG("writing copy_done; fa_id=%d off=0x%lx (0x%lx)",
Marti Bolivar99ec3832019-09-12 21:21:26 -0600551 fap->fa_id, (unsigned long)off,
552 (unsigned long)(fap->fa_off + off));
Fabio Utzig1a1ec172019-08-09 10:21:43 -0300553 return boot_write_trailer_flag(fap, off, BOOT_FLAG_SET);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300554}
555
556int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800557boot_write_image_ok(const struct flash_area *fap)
558{
Christopher Collinsa1c12042019-05-23 14:00:28 -0700559 uint32_t off;
560
561 off = boot_image_ok_off(fap);
Ben McCrea4c0ee952019-08-28 09:13:24 -0700562 BOOT_LOG_DBG("writing image_ok; fa_id=%d off=0x%lx (0x%lx)",
Marti Bolivar99ec3832019-09-12 21:21:26 -0600563 fap->fa_id, (unsigned long)off,
564 (unsigned long)(fap->fa_off + off));
Fabio Utzig1a1ec172019-08-09 10:21:43 -0300565 return boot_write_trailer_flag(fap, off, BOOT_FLAG_SET);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700566}
567
568/**
569 * Writes the specified value to the `swap-type` field of an image trailer.
570 * This value is persisted so that the boot loader knows what swap operation to
571 * resume in case of an unexpected reset.
572 */
573int
David Vinczee2453472019-06-17 12:31:59 +0200574boot_write_swap_info(const struct flash_area *fap, uint8_t swap_type,
575 uint8_t image_num)
Christopher Collinsa1c12042019-05-23 14:00:28 -0700576{
577 uint32_t off;
David Vinczee2453472019-06-17 12:31:59 +0200578 uint8_t swap_info;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700579
David Vinczee2453472019-06-17 12:31:59 +0200580 BOOT_SET_SWAP_INFO(swap_info, image_num, swap_type);
581 off = boot_swap_info_off(fap);
Ben McCrea4c0ee952019-08-28 09:13:24 -0700582 BOOT_LOG_DBG("writing swap_info; fa_id=%d off=0x%lx (0x%lx), swap_type=0x%x"
David Vinczee2453472019-06-17 12:31:59 +0200583 " image_num=0x%x",
Marti Bolivar99ec3832019-09-12 21:21:26 -0600584 fap->fa_id, (unsigned long)off,
585 (unsigned long)(fap->fa_off + off), swap_type, image_num);
Fabio Utzig1a1ec172019-08-09 10:21:43 -0300586 return boot_write_trailer(fap, off, (const uint8_t *) &swap_info, 1);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800587}
588
589int
Fabio Utzig46490722017-09-04 15:34:32 -0300590boot_write_swap_size(const struct flash_area *fap, uint32_t swap_size)
591{
592 uint32_t off;
Fabio Utzig46490722017-09-04 15:34:32 -0300593
594 off = boot_swap_size_off(fap);
Ben McCrea4c0ee952019-08-28 09:13:24 -0700595 BOOT_LOG_DBG("writing swap_size; fa_id=%d off=0x%lx (0x%lx)",
Marti Bolivar99ec3832019-09-12 21:21:26 -0600596 fap->fa_id, (unsigned long)off,
597 (unsigned long)fap->fa_off + off);
Fabio Utzig1a1ec172019-08-09 10:21:43 -0300598 return boot_write_trailer(fap, off, (const uint8_t *) &swap_size, 4);
Fabio Utzig46490722017-09-04 15:34:32 -0300599}
600
Fabio Utzigba829042018-09-18 08:29:34 -0300601#ifdef MCUBOOT_ENC_IMAGES
602int
Fabio Utzig4741c452019-12-19 15:32:41 -0300603boot_write_enc_key(const struct flash_area *fap, uint8_t slot,
604 const struct boot_status *bs)
Fabio Utzigba829042018-09-18 08:29:34 -0300605{
606 uint32_t off;
607 int rc;
608
609 off = boot_enc_key_off(fap, slot);
Fabio Utzig5e6ea222019-12-10 09:49:59 -0300610 BOOT_LOG_DBG("writing enc_key; fa_id=%d off=0x%lx (0x%lx)",
611 fap->fa_id, (unsigned long)off,
612 (unsigned long)fap->fa_off + off);
Fabio Utzig4741c452019-12-19 15:32:41 -0300613#if MCUBOOT_SWAP_SAVE_ENCTLV
614 rc = flash_area_write(fap, off, bs->enctlv[slot], BOOT_ENC_TLV_ALIGN_SIZE);
615#else
616 rc = flash_area_write(fap, off, bs->enckey[slot], BOOT_ENC_KEY_SIZE);
617#endif
Fabio Utzigba829042018-09-18 08:29:34 -0300618 if (rc != 0) {
619 return BOOT_EFLASH;
620 }
621
622 return 0;
623}
624#endif
625
Fabio Utzig46490722017-09-04 15:34:32 -0300626int
Fabio Utzigb0f04732019-07-31 09:49:19 -0300627boot_swap_type_multi(int image_index)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800628{
629 const struct boot_swap_table *table;
David Vincze2d736ad2019-02-18 11:50:22 +0100630 struct boot_swap_state primary_slot;
631 struct boot_swap_state secondary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800632 int rc;
Fabio Utzigcd5774b2017-11-29 10:18:26 -0200633 size_t i;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800634
Fabio Utzigb0f04732019-07-31 09:49:19 -0300635 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(image_index),
636 &primary_slot);
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300637 if (rc) {
638 return BOOT_SWAP_TYPE_PANIC;
639 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800640
Fabio Utzigb0f04732019-07-31 09:49:19 -0300641 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(image_index),
David Vincze2d736ad2019-02-18 11:50:22 +0100642 &secondary_slot);
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300643 if (rc) {
644 return BOOT_SWAP_TYPE_PANIC;
645 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800646
647 for (i = 0; i < BOOT_SWAP_TABLES_COUNT; i++) {
648 table = boot_swap_tables + i;
649
Christopher Collinsa1c12042019-05-23 14:00:28 -0700650 if (boot_magic_compatible_check(table->magic_primary_slot,
651 primary_slot.magic) &&
652 boot_magic_compatible_check(table->magic_secondary_slot,
653 secondary_slot.magic) &&
David Vincze2d736ad2019-02-18 11:50:22 +0100654 (table->image_ok_primary_slot == BOOT_FLAG_ANY ||
655 table->image_ok_primary_slot == primary_slot.image_ok) &&
656 (table->image_ok_secondary_slot == BOOT_FLAG_ANY ||
657 table->image_ok_secondary_slot == secondary_slot.image_ok) &&
658 (table->copy_done_primary_slot == BOOT_FLAG_ANY ||
659 table->copy_done_primary_slot == primary_slot.copy_done)) {
Fabio Utzig34e393e2017-05-22 11:07:07 -0400660 BOOT_LOG_INF("Swap type: %s",
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300661 table->swap_type == BOOT_SWAP_TYPE_TEST ? "test" :
662 table->swap_type == BOOT_SWAP_TYPE_PERM ? "perm" :
663 table->swap_type == BOOT_SWAP_TYPE_REVERT ? "revert" :
664 "BUG; can't happen");
Fabio Utzigf0dbd422019-08-09 10:22:05 -0300665 if (table->swap_type != BOOT_SWAP_TYPE_TEST &&
666 table->swap_type != BOOT_SWAP_TYPE_PERM &&
667 table->swap_type != BOOT_SWAP_TYPE_REVERT) {
668 return BOOT_SWAP_TYPE_PANIC;
669 }
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300670 return table->swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800671 }
672 }
673
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300674 BOOT_LOG_INF("Swap type: none");
Christopher Collins92ea77f2016-12-12 15:59:26 -0800675 return BOOT_SWAP_TYPE_NONE;
676}
677
Fabio Utzig75e9a592019-08-27 09:10:36 -0300678/*
679 * This function is not used by the bootloader itself, but its required API
680 * by external tooling like mcumgr.
681 */
Fabio Utzigb0f04732019-07-31 09:49:19 -0300682int
683boot_swap_type(void)
684{
685 return boot_swap_type_multi(0);
686}
687
Christopher Collins92ea77f2016-12-12 15:59:26 -0800688/**
David Vincze2d736ad2019-02-18 11:50:22 +0100689 * Marks the image in the secondary slot as pending. On the next reboot,
690 * the system will perform a one-time boot of the the secondary slot image.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800691 *
Christopher Collins7835c1e2016-12-21 10:10:51 -0800692 * @param permanent Whether the image should be used permanently or
693 * only tested once:
694 * 0=run image once, then confirm or revert.
695 * 1=run image forever.
696 *
Christopher Collins92ea77f2016-12-12 15:59:26 -0800697 * @return 0 on success; nonzero on failure.
698 */
699int
Christopher Collins7835c1e2016-12-21 10:10:51 -0800700boot_set_pending(int permanent)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800701{
702 const struct flash_area *fap;
David Vincze2d736ad2019-02-18 11:50:22 +0100703 struct boot_swap_state state_secondary_slot;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700704 uint8_t swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800705 int rc;
706
Fabio Utzigb0f04732019-07-31 09:49:19 -0300707 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(0),
David Vincze2d736ad2019-02-18 11:50:22 +0100708 &state_secondary_slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800709 if (rc != 0) {
710 return rc;
711 }
712
David Vincze2d736ad2019-02-18 11:50:22 +0100713 switch (state_secondary_slot.magic) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800714 case BOOT_MAGIC_GOOD:
715 /* Swap already scheduled. */
716 return 0;
717
718 case BOOT_MAGIC_UNSET:
Fabio Utzigb0f04732019-07-31 09:49:19 -0300719 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(0), &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800720 if (rc != 0) {
721 rc = BOOT_EFLASH;
722 } else {
723 rc = boot_write_magic(fap);
724 }
725
Christopher Collins7835c1e2016-12-21 10:10:51 -0800726 if (rc == 0 && permanent) {
727 rc = boot_write_image_ok(fap);
728 }
729
Christopher Collinsa1c12042019-05-23 14:00:28 -0700730 if (rc == 0) {
731 if (permanent) {
732 swap_type = BOOT_SWAP_TYPE_PERM;
733 } else {
734 swap_type = BOOT_SWAP_TYPE_TEST;
735 }
David Vinczee2453472019-06-17 12:31:59 +0200736 rc = boot_write_swap_info(fap, swap_type, 0);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700737 }
738
Christopher Collins92ea77f2016-12-12 15:59:26 -0800739 flash_area_close(fap);
740 return rc;
741
Christopher Collinsae01f152019-01-30 09:56:37 -0800742 case BOOT_MAGIC_BAD:
743 /* The image slot is corrupt. There is no way to recover, so erase the
744 * slot to allow future upgrades.
745 */
Fabio Utzigb0f04732019-07-31 09:49:19 -0300746 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(0), &fap);
Christopher Collinsae01f152019-01-30 09:56:37 -0800747 if (rc != 0) {
748 return BOOT_EFLASH;
749 }
750
751 flash_area_erase(fap, 0, fap->fa_size);
752 flash_area_close(fap);
753 return BOOT_EBADIMAGE;
754
Christopher Collins92ea77f2016-12-12 15:59:26 -0800755 default:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800756 assert(0);
Christopher Collinsae01f152019-01-30 09:56:37 -0800757 return BOOT_EBADIMAGE;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800758 }
759}
760
761/**
David Vincze2d736ad2019-02-18 11:50:22 +0100762 * Marks the image in the primary slot as confirmed. The system will continue
763 * booting into the image in the primary slot until told to boot from a
764 * different slot.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800765 *
766 * @return 0 on success; nonzero on failure.
767 */
768int
769boot_set_confirmed(void)
770{
771 const struct flash_area *fap;
David Vincze2d736ad2019-02-18 11:50:22 +0100772 struct boot_swap_state state_primary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800773 int rc;
774
Fabio Utzigb0f04732019-07-31 09:49:19 -0300775 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(0),
David Vincze2d736ad2019-02-18 11:50:22 +0100776 &state_primary_slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800777 if (rc != 0) {
778 return rc;
779 }
780
David Vincze2d736ad2019-02-18 11:50:22 +0100781 switch (state_primary_slot.magic) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800782 case BOOT_MAGIC_GOOD:
783 /* Confirm needed; proceed. */
784 break;
785
786 case BOOT_MAGIC_UNSET:
787 /* Already confirmed. */
788 return 0;
789
790 case BOOT_MAGIC_BAD:
791 /* Unexpected state. */
792 return BOOT_EBADVECT;
793 }
794
Fabio Utzigb0f04732019-07-31 09:49:19 -0300795 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(0), &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800796 if (rc) {
797 rc = BOOT_EFLASH;
798 goto done;
799 }
800
David Vincze2d736ad2019-02-18 11:50:22 +0100801 if (state_primary_slot.copy_done == BOOT_FLAG_UNSET) {
Fabio Utzig39000012018-07-30 12:40:20 -0300802 /* Swap never completed. This is unexpected. */
803 rc = BOOT_EBADVECT;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800804 goto done;
805 }
806
David Vincze2d736ad2019-02-18 11:50:22 +0100807 if (state_primary_slot.image_ok != BOOT_FLAG_UNSET) {
Fabio Utzig39000012018-07-30 12:40:20 -0300808 /* Already confirmed. */
809 goto done;
810 }
811
812 rc = boot_write_image_ok(fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800813
814done:
815 flash_area_close(fap);
816 return rc;
817}