blob: d9dd352259549728a6c6c423148764adec06dd96 [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
6 * Copyright (c) 2019 Arm Limited
7 *
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"
Fabio Utzigba829042018-09-18 08:29:34 -030039#ifdef MCUBOOT_ENC_IMAGES
40#include "bootutil/enc_key.h"
41#endif
Fabio Utzig7ebb7c22017-04-26 10:59:31 -030042
Emanuele Di Santo9f1933d2018-11-20 10:59:59 +010043MCUBOOT_LOG_MODULE_DECLARE(mcuboot);
44
Fabio Utzig10ee6482019-08-01 12:04:52 -030045/* Currently only used by imgmgr */
Christopher Collins92ea77f2016-12-12 15:59:26 -080046int boot_current_slot;
47
Fabio Utzig24a273d2017-04-20 08:21:31 -030048const uint32_t boot_img_magic[] = {
Christopher Collins92ea77f2016-12-12 15:59:26 -080049 0xf395c277,
50 0x7fefd260,
51 0x0f505235,
52 0x8079b62c,
53};
54
Hovland, Sigvart1d96f362018-09-25 13:23:42 +020055#define BOOT_MAGIC_ARR_SZ \
56 (sizeof boot_img_magic / sizeof boot_img_magic[0])
57
Christopher Collins92ea77f2016-12-12 15:59:26 -080058struct boot_swap_table {
David Vincze2d736ad2019-02-18 11:50:22 +010059 uint8_t magic_primary_slot;
60 uint8_t magic_secondary_slot;
61 uint8_t image_ok_primary_slot;
62 uint8_t image_ok_secondary_slot;
63 uint8_t copy_done_primary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -080064
Fabio Utzigb5b2f552017-06-30 10:03:47 -030065 uint8_t swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -080066};
67
68/**
69 * This set of tables maps image trailer contents to swap operation type.
70 * When searching for a match, these tables must be iterated sequentially.
Fabio Utzigb5b2f552017-06-30 10:03:47 -030071 *
David Vincze2d736ad2019-02-18 11:50:22 +010072 * NOTE: the table order is very important. The settings in the secondary
73 * slot always are priority to the primary slot and should be located
74 * earlier in the table.
Fabio Utzigb5b2f552017-06-30 10:03:47 -030075 *
76 * The table lists only states where there is action needs to be taken by
77 * the bootloader, as in starting/finishing a swap operation.
Christopher Collins92ea77f2016-12-12 15:59:26 -080078 */
79static const struct boot_swap_table boot_swap_tables[] = {
80 {
David Vincze2d736ad2019-02-18 11:50:22 +010081 .magic_primary_slot = BOOT_MAGIC_ANY,
82 .magic_secondary_slot = BOOT_MAGIC_GOOD,
83 .image_ok_primary_slot = BOOT_FLAG_ANY,
84 .image_ok_secondary_slot = BOOT_FLAG_UNSET,
85 .copy_done_primary_slot = BOOT_FLAG_ANY,
86 .swap_type = BOOT_SWAP_TYPE_TEST,
Christopher Collins92ea77f2016-12-12 15:59:26 -080087 },
Christopher Collins92ea77f2016-12-12 15:59:26 -080088 {
David Vincze2d736ad2019-02-18 11:50:22 +010089 .magic_primary_slot = BOOT_MAGIC_ANY,
90 .magic_secondary_slot = BOOT_MAGIC_GOOD,
91 .image_ok_primary_slot = BOOT_FLAG_ANY,
92 .image_ok_secondary_slot = BOOT_FLAG_SET,
93 .copy_done_primary_slot = BOOT_FLAG_ANY,
94 .swap_type = BOOT_SWAP_TYPE_PERM,
Christopher Collins92ea77f2016-12-12 15:59:26 -080095 },
Christopher Collins92ea77f2016-12-12 15:59:26 -080096 {
David Vincze2d736ad2019-02-18 11:50:22 +010097 .magic_primary_slot = BOOT_MAGIC_GOOD,
98 .magic_secondary_slot = BOOT_MAGIC_UNSET,
99 .image_ok_primary_slot = BOOT_FLAG_UNSET,
100 .image_ok_secondary_slot = BOOT_FLAG_ANY,
101 .copy_done_primary_slot = BOOT_FLAG_SET,
102 .swap_type = BOOT_SWAP_TYPE_REVERT,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800103 },
104};
105
106#define BOOT_SWAP_TABLES_COUNT \
107 (sizeof boot_swap_tables / sizeof boot_swap_tables[0])
108
Fabio Utzig39000012018-07-30 12:40:20 -0300109static int
Fabio Utzig178be542018-09-19 08:12:56 -0300110boot_magic_decode(const uint32_t *magic)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800111{
Fabio Utzig24a273d2017-04-20 08:21:31 -0300112 if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800113 return BOOT_MAGIC_GOOD;
114 }
Fabio Utzig178be542018-09-19 08:12:56 -0300115 return BOOT_MAGIC_BAD;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800116}
117
Fabio Utzig39000012018-07-30 12:40:20 -0300118static int
Fabio Utzig178be542018-09-19 08:12:56 -0300119boot_flag_decode(uint8_t flag)
Fabio Utzig39000012018-07-30 12:40:20 -0300120{
Fabio Utzig39000012018-07-30 12:40:20 -0300121 if (flag != BOOT_FLAG_SET) {
122 return BOOT_FLAG_BAD;
123 }
124 return BOOT_FLAG_SET;
125}
126
Christopher Collinsa1c12042019-05-23 14:00:28 -0700127/**
128 * Determines if a status source table is satisfied by the specified magic
129 * code.
130 *
131 * @param tbl_val A magic field from a status source table.
132 * @param val The magic value in a trailer, encoded as a
133 * BOOT_MAGIC_[...].
134 *
135 * @return 1 if the two values are compatible;
136 * 0 otherwise.
137 */
138int
139boot_magic_compatible_check(uint8_t tbl_val, uint8_t val)
140{
141 switch (tbl_val) {
142 case BOOT_MAGIC_ANY:
143 return 1;
144
145 case BOOT_MAGIC_NOTGOOD:
146 return val != BOOT_MAGIC_GOOD;
147
148 default:
149 return tbl_val == val;
150 }
151}
152
Christopher Collins92ea77f2016-12-12 15:59:26 -0800153uint32_t
Fabio Utzig3fbbdac2019-12-19 15:18:23 -0300154boot_status_sz(uint32_t min_write_sz)
155{
156 return /* state for all sectors */
157 BOOT_STATUS_MAX_ENTRIES * BOOT_STATUS_STATE_COUNT * min_write_sz;
158}
159
160uint32_t
David Brownab449182019-11-15 09:32:52 -0700161boot_trailer_sz(uint32_t min_write_sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800162{
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300163 return /* state for all sectors */
Fabio Utzig3fbbdac2019-12-19 15:18:23 -0300164 boot_status_sz(min_write_sz) +
Fabio Utzigba829042018-09-18 08:29:34 -0300165#ifdef MCUBOOT_ENC_IMAGES
166 /* encryption keys */
Fabio Utzig4741c452019-12-19 15:32:41 -0300167# if MCUBOOT_SWAP_SAVE_ENCTLV
168 BOOT_ENC_TLV_ALIGN_SIZE * 2 +
169# else
Fabio Utzigba829042018-09-18 08:29:34 -0300170 BOOT_ENC_KEY_SIZE * 2 +
Fabio Utzig4741c452019-12-19 15:32:41 -0300171# endif
Fabio Utzigba829042018-09-18 08:29:34 -0300172#endif
Christopher Collins2adef702019-05-22 14:37:31 -0700173 /* swap_type + copy_done + image_ok + swap_size */
174 BOOT_MAX_ALIGN * 4 +
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300175 BOOT_MAGIC_SZ;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800176}
177
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400178int
Fabio Utzigb0f04732019-07-31 09:49:19 -0300179boot_status_entries(int image_index, const struct flash_area *fap)
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400180{
Fabio Utzig12d59162019-11-28 10:01:59 -0300181#if MCUBOOT_SWAP_USING_SCRATCH
David Vinczeb75c12a2019-03-22 14:58:33 +0100182 if (fap->fa_id == FLASH_AREA_IMAGE_SCRATCH) {
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400183 return BOOT_STATUS_STATE_COUNT;
Fabio Utzig12d59162019-11-28 10:01:59 -0300184 } else
185#endif
186 if (fap->fa_id == FLASH_AREA_IMAGE_PRIMARY(image_index) ||
Fabio Utzigb0f04732019-07-31 09:49:19 -0300187 fap->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
David Vinczeb75c12a2019-03-22 14:58:33 +0100188 return BOOT_STATUS_STATE_COUNT * BOOT_STATUS_MAX_ENTRIES;
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400189 }
Fabio Utzig9d160092019-08-09 07:46:34 -0300190 return -1;
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400191}
192
Christopher Collins92ea77f2016-12-12 15:59:26 -0800193uint32_t
194boot_status_off(const struct flash_area *fap)
195{
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300196 uint32_t off_from_end;
197 uint8_t elem_sz;
198
199 elem_sz = flash_area_align(fap);
200
Christopher Collins2adef702019-05-22 14:37:31 -0700201 off_from_end = boot_trailer_sz(elem_sz);
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300202
203 assert(off_from_end <= fap->fa_size);
204 return fap->fa_size - off_from_end;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800205}
206
Fabio Utzig4e8113b2019-08-09 09:11:18 -0300207static inline uint32_t
208boot_magic_off(const struct flash_area *fap)
209{
210 return fap->fa_size - BOOT_MAGIC_SZ;
211}
212
213static inline uint32_t
214boot_image_ok_off(const struct flash_area *fap)
215{
216 return boot_magic_off(fap) - BOOT_MAX_ALIGN;
217}
218
219static inline uint32_t
220boot_copy_done_off(const struct flash_area *fap)
221{
222 return boot_image_ok_off(fap) - BOOT_MAX_ALIGN;
223}
224
Christopher Collinsa1c12042019-05-23 14:00:28 -0700225uint32_t
David Vinczee2453472019-06-17 12:31:59 +0200226boot_swap_info_off(const struct flash_area *fap)
Christopher Collinsa1c12042019-05-23 14:00:28 -0700227{
Fabio Utzig4e8113b2019-08-09 09:11:18 -0300228 return boot_copy_done_off(fap) - BOOT_MAX_ALIGN;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700229}
230
Fabio Utzig4e8113b2019-08-09 09:11:18 -0300231static inline uint32_t
Fabio Utzig46490722017-09-04 15:34:32 -0300232boot_swap_size_off(const struct flash_area *fap)
233{
Fabio Utzig4e8113b2019-08-09 09:11:18 -0300234 return boot_swap_info_off(fap) - BOOT_MAX_ALIGN;
Fabio Utzig46490722017-09-04 15:34:32 -0300235}
236
Fabio Utzigba829042018-09-18 08:29:34 -0300237#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig4e8113b2019-08-09 09:11:18 -0300238static inline uint32_t
Fabio Utzigba829042018-09-18 08:29:34 -0300239boot_enc_key_off(const struct flash_area *fap, uint8_t slot)
240{
Fabio Utzig4741c452019-12-19 15:32:41 -0300241#if MCUBOOT_SWAP_SAVE_ENCTLV
242 return boot_swap_size_off(fap) - ((slot + 1) *
243 ((((BOOT_ENC_TLV_SIZE - 1) / BOOT_MAX_ALIGN) + 1) * BOOT_MAX_ALIGN));
244#else
Fabio Utzig4e8113b2019-08-09 09:11:18 -0300245 return boot_swap_size_off(fap) - ((slot + 1) * BOOT_ENC_KEY_SIZE);
Fabio Utzig4741c452019-12-19 15:32:41 -0300246#endif
Fabio Utzigba829042018-09-18 08:29:34 -0300247}
248#endif
249
Christopher Collins92ea77f2016-12-12 15:59:26 -0800250int
251boot_read_swap_state(const struct flash_area *fap,
252 struct boot_swap_state *state)
253{
Hovland, Sigvart1d96f362018-09-25 13:23:42 +0200254 uint32_t magic[BOOT_MAGIC_ARR_SZ];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800255 uint32_t off;
David Vinczee2453472019-06-17 12:31:59 +0200256 uint8_t swap_info;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800257 int rc;
258
259 off = boot_magic_off(fap);
Fabio Utzig178be542018-09-19 08:12:56 -0300260 rc = flash_area_read_is_empty(fap, off, magic, BOOT_MAGIC_SZ);
261 if (rc < 0) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800262 return BOOT_EFLASH;
263 }
Fabio Utzig178be542018-09-19 08:12:56 -0300264 if (rc == 1) {
265 state->magic = BOOT_MAGIC_UNSET;
266 } else {
267 state->magic = boot_magic_decode(magic);
268 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800269
David Vinczee2453472019-06-17 12:31:59 +0200270 off = boot_swap_info_off(fap);
271 rc = flash_area_read_is_empty(fap, off, &swap_info, sizeof swap_info);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700272 if (rc < 0) {
273 return BOOT_EFLASH;
274 }
David Vinczee2453472019-06-17 12:31:59 +0200275
276 /* Extract the swap type and image number */
277 state->swap_type = BOOT_GET_SWAP_TYPE(swap_info);
278 state->image_num = BOOT_GET_IMAGE_NUM(swap_info);
279
Christopher Collinsa1c12042019-05-23 14:00:28 -0700280 if (rc == 1 || state->swap_type > BOOT_SWAP_TYPE_REVERT) {
281 state->swap_type = BOOT_SWAP_TYPE_NONE;
David Vinczee2453472019-06-17 12:31:59 +0200282 state->image_num = 0;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700283 }
284
Christopher Collins2adef702019-05-22 14:37:31 -0700285 off = boot_copy_done_off(fap);
286 rc = flash_area_read_is_empty(fap, off, &state->copy_done,
287 sizeof state->copy_done);
288 if (rc < 0) {
289 return BOOT_EFLASH;
290 }
291 if (rc == 1) {
292 state->copy_done = BOOT_FLAG_UNSET;
293 } else {
294 state->copy_done = boot_flag_decode(state->copy_done);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800295 }
296
297 off = boot_image_ok_off(fap);
David Vincze2d736ad2019-02-18 11:50:22 +0100298 rc = flash_area_read_is_empty(fap, off, &state->image_ok,
299 sizeof state->image_ok);
Fabio Utzig178be542018-09-19 08:12:56 -0300300 if (rc < 0) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800301 return BOOT_EFLASH;
302 }
Fabio Utzig178be542018-09-19 08:12:56 -0300303 if (rc == 1) {
304 state->image_ok = BOOT_FLAG_UNSET;
305 } else {
306 state->image_ok = boot_flag_decode(state->image_ok);
307 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800308
309 return 0;
310}
311
312/**
313 * Reads the image trailer from the scratch area.
314 */
315int
Fabio Utzig2473ac02017-05-02 12:45:02 -0300316boot_read_swap_state_by_id(int flash_area_id, struct boot_swap_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800317{
318 const struct flash_area *fap;
319 int rc;
320
Fabio Utzigb0f04732019-07-31 09:49:19 -0300321 rc = flash_area_open(flash_area_id, &fap);
322 if (rc != 0) {
323 return BOOT_EFLASH;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300324 }
325
326 rc = boot_read_swap_state(fap, state);
Fabio Utzigacfba2e2017-05-22 11:06:29 -0400327 flash_area_close(fap);
328 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800329}
330
Fabio Utzigdf0cc502019-08-09 09:10:41 -0300331/**
332 * This functions tries to locate the status area after an aborted swap,
333 * by looking for the magic in the possible locations.
334 *
Sam Bristowd0ca0ff2019-10-30 20:51:35 +1300335 * If the magic is successfully found, a flash_area * is returned and it
Fabio Utzigdf0cc502019-08-09 09:10:41 -0300336 * is the responsibility of the called to close it.
337 *
338 * @returns 0 on success, -1 on errors
339 */
340static int
341boot_find_status(int image_index, const struct flash_area **fap)
342{
343 uint32_t magic[BOOT_MAGIC_ARR_SZ];
344 uint32_t off;
345 uint8_t areas[2] = {
Fabio Utzig12d59162019-11-28 10:01:59 -0300346#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzigdf0cc502019-08-09 09:10:41 -0300347 FLASH_AREA_IMAGE_SCRATCH,
Fabio Utzig12d59162019-11-28 10:01:59 -0300348#endif
Fabio Utzig3c446072019-11-22 12:48:26 -0300349 FLASH_AREA_IMAGE_PRIMARY(image_index),
Fabio Utzigdf0cc502019-08-09 09:10:41 -0300350 };
351 unsigned int i;
352 int rc;
353
354 /*
355 * In the middle a swap, tries to locate the area that is currently
356 * storing a valid magic, first on the primary slot, then on scratch.
357 * Both "slots" can end up being temporary storage for a swap and it
358 * is assumed that if magic is valid then other metadata is too,
359 * because magic is always written in the last step.
360 */
361
362 for (i = 0; i < sizeof(areas) / sizeof(areas[0]); i++) {
363 rc = flash_area_open(areas[i], fap);
364 if (rc != 0) {
365 return rc;
366 }
367
368 off = boot_magic_off(*fap);
369 rc = flash_area_read(*fap, off, magic, BOOT_MAGIC_SZ);
370 if (rc != 0) {
371 flash_area_close(*fap);
372 return rc;
373 }
374
375 if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0) {
376 return 0;
377 }
378
379 flash_area_close(*fap);
380 }
381
382 /* If we got here, no magic was found */
383 return -1;
384}
385
Christopher Collins92ea77f2016-12-12 15:59:26 -0800386int
Fabio Utzigb0f04732019-07-31 09:49:19 -0300387boot_read_swap_size(int image_index, uint32_t *swap_size)
Fabio Utzig46490722017-09-04 15:34:32 -0300388{
Fabio Utzig46490722017-09-04 15:34:32 -0300389 uint32_t off;
390 const struct flash_area *fap;
391 int rc;
392
Fabio Utzigdf0cc502019-08-09 09:10:41 -0300393 rc = boot_find_status(image_index, &fap);
394 if (rc == 0) {
395 off = boot_swap_size_off(fap);
396 rc = flash_area_read(fap, off, swap_size, sizeof *swap_size);
Fabio Utzig46490722017-09-04 15:34:32 -0300397 flash_area_close(fap);
Fabio Utzig46490722017-09-04 15:34:32 -0300398 }
399
Fabio Utzig46490722017-09-04 15:34:32 -0300400 return rc;
401}
402
Fabio Utzigba829042018-09-18 08:29:34 -0300403#ifdef MCUBOOT_ENC_IMAGES
404int
Fabio Utzig4741c452019-12-19 15:32:41 -0300405boot_read_enc_key(int image_index, uint8_t slot, struct boot_status *bs)
Fabio Utzigba829042018-09-18 08:29:34 -0300406{
Fabio Utzigba829042018-09-18 08:29:34 -0300407 uint32_t off;
408 const struct flash_area *fap;
Fabio Utzig4741c452019-12-19 15:32:41 -0300409#if MCUBOOT_SWAP_SAVE_ENCTLV
410 int i;
411#endif
Fabio Utzigba829042018-09-18 08:29:34 -0300412 int rc;
413
Fabio Utzigdf0cc502019-08-09 09:10:41 -0300414 rc = boot_find_status(image_index, &fap);
415 if (rc == 0) {
416 off = boot_enc_key_off(fap, slot);
Fabio Utzig4741c452019-12-19 15:32:41 -0300417#if MCUBOOT_SWAP_SAVE_ENCTLV
418 rc = flash_area_read(fap, off, bs->enctlv[slot], BOOT_ENC_TLV_ALIGN_SIZE);
419 if (rc == 0) {
420 for (i = 0; i < BOOT_ENC_TLV_ALIGN_SIZE; i++) {
421 if (bs->enctlv[slot][i] != 0xff) {
422 break;
423 }
424 }
425 /* Only try to decrypt non-erased TLV metadata */
426 if (i != BOOT_ENC_TLV_ALIGN_SIZE) {
427 rc = boot_enc_decrypt(bs->enctlv[slot], bs->enckey[slot]);
428 }
429 }
430#else
431 rc = flash_area_read(fap, off, bs->enckey[slot], BOOT_ENC_KEY_SIZE);
432#endif
Fabio Utzigba829042018-09-18 08:29:34 -0300433 flash_area_close(fap);
Fabio Utzigba829042018-09-18 08:29:34 -0300434 }
435
Fabio Utzigba829042018-09-18 08:29:34 -0300436 return rc;
437}
438#endif
Fabio Utzig46490722017-09-04 15:34:32 -0300439
440int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800441boot_write_magic(const struct flash_area *fap)
442{
443 uint32_t off;
444 int rc;
445
446 off = boot_magic_off(fap);
447
Ben McCrea4c0ee952019-08-28 09:13:24 -0700448 BOOT_LOG_DBG("writing magic; fa_id=%d off=0x%lx (0x%lx)",
Marti Bolivar99ec3832019-09-12 21:21:26 -0600449 fap->fa_id, (unsigned long)off,
450 (unsigned long)(fap->fa_off + off));
Fabio Utzig24a273d2017-04-20 08:21:31 -0300451 rc = flash_area_write(fap, off, boot_img_magic, BOOT_MAGIC_SZ);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800452 if (rc != 0) {
453 return BOOT_EFLASH;
454 }
455
456 return 0;
457}
458
Fabio Utzig1a1ec172019-08-09 10:21:43 -0300459/**
460 * Write trailer data; status bytes, swap_size, etc
461 *
462 * @returns 0 on success, != 0 on error.
463 */
Fabio Utzig2473ac02017-05-02 12:45:02 -0300464static int
Fabio Utzig1a1ec172019-08-09 10:21:43 -0300465boot_write_trailer(const struct flash_area *fap, uint32_t off,
466 const uint8_t *inbuf, uint8_t inlen)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800467{
Fabio Utzig644b8d42017-04-20 07:56:05 -0300468 uint8_t buf[BOOT_MAX_ALIGN];
David Brown9d725462017-01-23 15:50:58 -0700469 uint8_t align;
Fabio Utzig39000012018-07-30 12:40:20 -0300470 uint8_t erased_val;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700471 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800472
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200473 align = flash_area_align(fap);
Fabio Utzig1a1ec172019-08-09 10:21:43 -0300474 if (inlen > BOOT_MAX_ALIGN || align > BOOT_MAX_ALIGN) {
475 return -1;
476 }
Fabio Utzig39000012018-07-30 12:40:20 -0300477 erased_val = flash_area_erased_val(fap);
Fabio Utzig1a1ec172019-08-09 10:21:43 -0300478 if (align < inlen) {
479 align = inlen;
480 }
481 memcpy(buf, inbuf, inlen);
482 memset(&buf[inlen], erased_val, align - inlen);
David Brown9d725462017-01-23 15:50:58 -0700483
484 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800485 if (rc != 0) {
486 return BOOT_EFLASH;
487 }
488
489 return 0;
490}
491
Fabio Utzig1a1ec172019-08-09 10:21:43 -0300492static int
493boot_write_trailer_flag(const struct flash_area *fap, uint32_t off,
494 uint8_t flag_val)
495{
496 const uint8_t buf[1] = { flag_val };
497 return boot_write_trailer(fap, off, buf, 1);
498}
499
Christopher Collins92ea77f2016-12-12 15:59:26 -0800500int
Fabio Utzig2473ac02017-05-02 12:45:02 -0300501boot_write_copy_done(const struct flash_area *fap)
502{
Christopher Collinsa1c12042019-05-23 14:00:28 -0700503 uint32_t off;
504
505 off = boot_copy_done_off(fap);
Ben McCrea4c0ee952019-08-28 09:13:24 -0700506 BOOT_LOG_DBG("writing copy_done; fa_id=%d off=0x%lx (0x%lx)",
Marti Bolivar99ec3832019-09-12 21:21:26 -0600507 fap->fa_id, (unsigned long)off,
508 (unsigned long)(fap->fa_off + off));
Fabio Utzig1a1ec172019-08-09 10:21:43 -0300509 return boot_write_trailer_flag(fap, off, BOOT_FLAG_SET);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300510}
511
512int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800513boot_write_image_ok(const struct flash_area *fap)
514{
Christopher Collinsa1c12042019-05-23 14:00:28 -0700515 uint32_t off;
516
517 off = boot_image_ok_off(fap);
Ben McCrea4c0ee952019-08-28 09:13:24 -0700518 BOOT_LOG_DBG("writing image_ok; fa_id=%d off=0x%lx (0x%lx)",
Marti Bolivar99ec3832019-09-12 21:21:26 -0600519 fap->fa_id, (unsigned long)off,
520 (unsigned long)(fap->fa_off + off));
Fabio Utzig1a1ec172019-08-09 10:21:43 -0300521 return boot_write_trailer_flag(fap, off, BOOT_FLAG_SET);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700522}
523
524/**
525 * Writes the specified value to the `swap-type` field of an image trailer.
526 * This value is persisted so that the boot loader knows what swap operation to
527 * resume in case of an unexpected reset.
528 */
529int
David Vinczee2453472019-06-17 12:31:59 +0200530boot_write_swap_info(const struct flash_area *fap, uint8_t swap_type,
531 uint8_t image_num)
Christopher Collinsa1c12042019-05-23 14:00:28 -0700532{
533 uint32_t off;
David Vinczee2453472019-06-17 12:31:59 +0200534 uint8_t swap_info;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700535
David Vinczee2453472019-06-17 12:31:59 +0200536 BOOT_SET_SWAP_INFO(swap_info, image_num, swap_type);
537 off = boot_swap_info_off(fap);
Ben McCrea4c0ee952019-08-28 09:13:24 -0700538 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 +0200539 " image_num=0x%x",
Marti Bolivar99ec3832019-09-12 21:21:26 -0600540 fap->fa_id, (unsigned long)off,
541 (unsigned long)(fap->fa_off + off), swap_type, image_num);
Fabio Utzig1a1ec172019-08-09 10:21:43 -0300542 return boot_write_trailer(fap, off, (const uint8_t *) &swap_info, 1);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800543}
544
545int
Fabio Utzig46490722017-09-04 15:34:32 -0300546boot_write_swap_size(const struct flash_area *fap, uint32_t swap_size)
547{
548 uint32_t off;
Fabio Utzig46490722017-09-04 15:34:32 -0300549
550 off = boot_swap_size_off(fap);
Ben McCrea4c0ee952019-08-28 09:13:24 -0700551 BOOT_LOG_DBG("writing swap_size; fa_id=%d off=0x%lx (0x%lx)",
Marti Bolivar99ec3832019-09-12 21:21:26 -0600552 fap->fa_id, (unsigned long)off,
553 (unsigned long)fap->fa_off + off);
Fabio Utzig1a1ec172019-08-09 10:21:43 -0300554 return boot_write_trailer(fap, off, (const uint8_t *) &swap_size, 4);
Fabio Utzig46490722017-09-04 15:34:32 -0300555}
556
Fabio Utzigba829042018-09-18 08:29:34 -0300557#ifdef MCUBOOT_ENC_IMAGES
558int
Fabio Utzig4741c452019-12-19 15:32:41 -0300559boot_write_enc_key(const struct flash_area *fap, uint8_t slot,
560 const struct boot_status *bs)
Fabio Utzigba829042018-09-18 08:29:34 -0300561{
562 uint32_t off;
563 int rc;
564
565 off = boot_enc_key_off(fap, slot);
Fabio Utzig5e6ea222019-12-10 09:49:59 -0300566 BOOT_LOG_DBG("writing enc_key; fa_id=%d off=0x%lx (0x%lx)",
567 fap->fa_id, (unsigned long)off,
568 (unsigned long)fap->fa_off + off);
Fabio Utzig4741c452019-12-19 15:32:41 -0300569#if MCUBOOT_SWAP_SAVE_ENCTLV
570 rc = flash_area_write(fap, off, bs->enctlv[slot], BOOT_ENC_TLV_ALIGN_SIZE);
571#else
572 rc = flash_area_write(fap, off, bs->enckey[slot], BOOT_ENC_KEY_SIZE);
573#endif
Fabio Utzigba829042018-09-18 08:29:34 -0300574 if (rc != 0) {
575 return BOOT_EFLASH;
576 }
577
578 return 0;
579}
580#endif
581
Fabio Utzig46490722017-09-04 15:34:32 -0300582int
Fabio Utzigb0f04732019-07-31 09:49:19 -0300583boot_swap_type_multi(int image_index)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800584{
585 const struct boot_swap_table *table;
David Vincze2d736ad2019-02-18 11:50:22 +0100586 struct boot_swap_state primary_slot;
587 struct boot_swap_state secondary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800588 int rc;
Fabio Utzigcd5774b2017-11-29 10:18:26 -0200589 size_t i;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800590
Fabio Utzigb0f04732019-07-31 09:49:19 -0300591 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(image_index),
592 &primary_slot);
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300593 if (rc) {
594 return BOOT_SWAP_TYPE_PANIC;
595 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800596
Fabio Utzigb0f04732019-07-31 09:49:19 -0300597 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(image_index),
David Vincze2d736ad2019-02-18 11:50:22 +0100598 &secondary_slot);
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300599 if (rc) {
600 return BOOT_SWAP_TYPE_PANIC;
601 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800602
603 for (i = 0; i < BOOT_SWAP_TABLES_COUNT; i++) {
604 table = boot_swap_tables + i;
605
Christopher Collinsa1c12042019-05-23 14:00:28 -0700606 if (boot_magic_compatible_check(table->magic_primary_slot,
607 primary_slot.magic) &&
608 boot_magic_compatible_check(table->magic_secondary_slot,
609 secondary_slot.magic) &&
David Vincze2d736ad2019-02-18 11:50:22 +0100610 (table->image_ok_primary_slot == BOOT_FLAG_ANY ||
611 table->image_ok_primary_slot == primary_slot.image_ok) &&
612 (table->image_ok_secondary_slot == BOOT_FLAG_ANY ||
613 table->image_ok_secondary_slot == secondary_slot.image_ok) &&
614 (table->copy_done_primary_slot == BOOT_FLAG_ANY ||
615 table->copy_done_primary_slot == primary_slot.copy_done)) {
Fabio Utzig34e393e2017-05-22 11:07:07 -0400616 BOOT_LOG_INF("Swap type: %s",
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300617 table->swap_type == BOOT_SWAP_TYPE_TEST ? "test" :
618 table->swap_type == BOOT_SWAP_TYPE_PERM ? "perm" :
619 table->swap_type == BOOT_SWAP_TYPE_REVERT ? "revert" :
620 "BUG; can't happen");
Fabio Utzigf0dbd422019-08-09 10:22:05 -0300621 if (table->swap_type != BOOT_SWAP_TYPE_TEST &&
622 table->swap_type != BOOT_SWAP_TYPE_PERM &&
623 table->swap_type != BOOT_SWAP_TYPE_REVERT) {
624 return BOOT_SWAP_TYPE_PANIC;
625 }
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300626 return table->swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800627 }
628 }
629
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300630 BOOT_LOG_INF("Swap type: none");
Christopher Collins92ea77f2016-12-12 15:59:26 -0800631 return BOOT_SWAP_TYPE_NONE;
632}
633
Fabio Utzig75e9a592019-08-27 09:10:36 -0300634/*
635 * This function is not used by the bootloader itself, but its required API
636 * by external tooling like mcumgr.
637 */
Fabio Utzigb0f04732019-07-31 09:49:19 -0300638int
639boot_swap_type(void)
640{
641 return boot_swap_type_multi(0);
642}
643
Christopher Collins92ea77f2016-12-12 15:59:26 -0800644/**
David Vincze2d736ad2019-02-18 11:50:22 +0100645 * Marks the image in the secondary slot as pending. On the next reboot,
646 * the system will perform a one-time boot of the the secondary slot image.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800647 *
Christopher Collins7835c1e2016-12-21 10:10:51 -0800648 * @param permanent Whether the image should be used permanently or
649 * only tested once:
650 * 0=run image once, then confirm or revert.
651 * 1=run image forever.
652 *
Christopher Collins92ea77f2016-12-12 15:59:26 -0800653 * @return 0 on success; nonzero on failure.
654 */
655int
Christopher Collins7835c1e2016-12-21 10:10:51 -0800656boot_set_pending(int permanent)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800657{
658 const struct flash_area *fap;
David Vincze2d736ad2019-02-18 11:50:22 +0100659 struct boot_swap_state state_secondary_slot;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700660 uint8_t swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800661 int rc;
662
Fabio Utzigb0f04732019-07-31 09:49:19 -0300663 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(0),
David Vincze2d736ad2019-02-18 11:50:22 +0100664 &state_secondary_slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800665 if (rc != 0) {
666 return rc;
667 }
668
David Vincze2d736ad2019-02-18 11:50:22 +0100669 switch (state_secondary_slot.magic) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800670 case BOOT_MAGIC_GOOD:
671 /* Swap already scheduled. */
672 return 0;
673
674 case BOOT_MAGIC_UNSET:
Fabio Utzigb0f04732019-07-31 09:49:19 -0300675 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(0), &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800676 if (rc != 0) {
677 rc = BOOT_EFLASH;
678 } else {
679 rc = boot_write_magic(fap);
680 }
681
Christopher Collins7835c1e2016-12-21 10:10:51 -0800682 if (rc == 0 && permanent) {
683 rc = boot_write_image_ok(fap);
684 }
685
Christopher Collinsa1c12042019-05-23 14:00:28 -0700686 if (rc == 0) {
687 if (permanent) {
688 swap_type = BOOT_SWAP_TYPE_PERM;
689 } else {
690 swap_type = BOOT_SWAP_TYPE_TEST;
691 }
David Vinczee2453472019-06-17 12:31:59 +0200692 rc = boot_write_swap_info(fap, swap_type, 0);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700693 }
694
Christopher Collins92ea77f2016-12-12 15:59:26 -0800695 flash_area_close(fap);
696 return rc;
697
Christopher Collinsae01f152019-01-30 09:56:37 -0800698 case BOOT_MAGIC_BAD:
699 /* The image slot is corrupt. There is no way to recover, so erase the
700 * slot to allow future upgrades.
701 */
Fabio Utzigb0f04732019-07-31 09:49:19 -0300702 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(0), &fap);
Christopher Collinsae01f152019-01-30 09:56:37 -0800703 if (rc != 0) {
704 return BOOT_EFLASH;
705 }
706
707 flash_area_erase(fap, 0, fap->fa_size);
708 flash_area_close(fap);
709 return BOOT_EBADIMAGE;
710
Christopher Collins92ea77f2016-12-12 15:59:26 -0800711 default:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800712 assert(0);
Christopher Collinsae01f152019-01-30 09:56:37 -0800713 return BOOT_EBADIMAGE;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800714 }
715}
716
717/**
David Vincze2d736ad2019-02-18 11:50:22 +0100718 * Marks the image in the primary slot as confirmed. The system will continue
719 * booting into the image in the primary slot until told to boot from a
720 * different slot.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800721 *
722 * @return 0 on success; nonzero on failure.
723 */
724int
725boot_set_confirmed(void)
726{
727 const struct flash_area *fap;
David Vincze2d736ad2019-02-18 11:50:22 +0100728 struct boot_swap_state state_primary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800729 int rc;
730
Fabio Utzigb0f04732019-07-31 09:49:19 -0300731 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(0),
David Vincze2d736ad2019-02-18 11:50:22 +0100732 &state_primary_slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800733 if (rc != 0) {
734 return rc;
735 }
736
David Vincze2d736ad2019-02-18 11:50:22 +0100737 switch (state_primary_slot.magic) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800738 case BOOT_MAGIC_GOOD:
739 /* Confirm needed; proceed. */
740 break;
741
742 case BOOT_MAGIC_UNSET:
743 /* Already confirmed. */
744 return 0;
745
746 case BOOT_MAGIC_BAD:
747 /* Unexpected state. */
748 return BOOT_EBADVECT;
749 }
750
Fabio Utzigb0f04732019-07-31 09:49:19 -0300751 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(0), &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800752 if (rc) {
753 rc = BOOT_EFLASH;
754 goto done;
755 }
756
David Vincze2d736ad2019-02-18 11:50:22 +0100757 if (state_primary_slot.copy_done == BOOT_FLAG_UNSET) {
Fabio Utzig39000012018-07-30 12:40:20 -0300758 /* Swap never completed. This is unexpected. */
759 rc = BOOT_EBADVECT;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800760 goto done;
761 }
762
David Vincze2d736ad2019-02-18 11:50:22 +0100763 if (state_primary_slot.image_ok != BOOT_FLAG_UNSET) {
Fabio Utzig39000012018-07-30 12:40:20 -0300764 /* Already confirmed. */
765 goto done;
766 }
767
768 rc = boot_write_image_ok(fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800769
770done:
771 flash_area_close(fap);
772 return rc;
773}