blob: ab79b8ba850c00e8a682e039da848d5c42dc68fe [file] [log] [blame]
Christopher Collins92ea77f2016-12-12 15:59:26 -08001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
David Vinczee2453472019-06-17 12:31:59 +020020/*
21 * Modifications are Copyright (c) 2019 Arm Limited.
22 */
23
Christopher Collins92ea77f2016-12-12 15:59:26 -080024#include <assert.h>
25#include <string.h>
26#include <inttypes.h>
Fabio Utziga0bc9b52017-06-28 09:19:55 -030027#include <stddef.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080028
Christopher Collins92ea77f2016-12-12 15:59:26 -080029#include "sysflash/sysflash.h"
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020030#include "flash_map_backend/flash_map_backend.h"
31
Christopher Collins92ea77f2016-12-12 15:59:26 -080032#include "bootutil/image.h"
33#include "bootutil/bootutil.h"
34#include "bootutil_priv.h"
Fabio Utzig7ebb7c22017-04-26 10:59:31 -030035#include "bootutil/bootutil_log.h"
Fabio Utzigba829042018-09-18 08:29:34 -030036#ifdef MCUBOOT_ENC_IMAGES
37#include "bootutil/enc_key.h"
38#endif
Fabio Utzig7ebb7c22017-04-26 10:59:31 -030039
Emanuele Di Santo9f1933d2018-11-20 10:59:59 +010040MCUBOOT_LOG_MODULE_DECLARE(mcuboot);
41
Fabio Utzig10ee6482019-08-01 12:04:52 -030042/* Currently only used by imgmgr */
Christopher Collins92ea77f2016-12-12 15:59:26 -080043int boot_current_slot;
44
Fabio Utzig24a273d2017-04-20 08:21:31 -030045const uint32_t boot_img_magic[] = {
Christopher Collins92ea77f2016-12-12 15:59:26 -080046 0xf395c277,
47 0x7fefd260,
48 0x0f505235,
49 0x8079b62c,
50};
51
Hovland, Sigvart1d96f362018-09-25 13:23:42 +020052#define BOOT_MAGIC_ARR_SZ \
53 (sizeof boot_img_magic / sizeof boot_img_magic[0])
54
Christopher Collins92ea77f2016-12-12 15:59:26 -080055struct boot_swap_table {
David Vincze2d736ad2019-02-18 11:50:22 +010056 uint8_t magic_primary_slot;
57 uint8_t magic_secondary_slot;
58 uint8_t image_ok_primary_slot;
59 uint8_t image_ok_secondary_slot;
60 uint8_t copy_done_primary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -080061
Fabio Utzigb5b2f552017-06-30 10:03:47 -030062 uint8_t swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -080063};
64
65/**
66 * This set of tables maps image trailer contents to swap operation type.
67 * When searching for a match, these tables must be iterated sequentially.
Fabio Utzigb5b2f552017-06-30 10:03:47 -030068 *
David Vincze2d736ad2019-02-18 11:50:22 +010069 * NOTE: the table order is very important. The settings in the secondary
70 * slot always are priority to the primary slot and should be located
71 * earlier in the table.
Fabio Utzigb5b2f552017-06-30 10:03:47 -030072 *
73 * The table lists only states where there is action needs to be taken by
74 * the bootloader, as in starting/finishing a swap operation.
Christopher Collins92ea77f2016-12-12 15:59:26 -080075 */
76static const struct boot_swap_table boot_swap_tables[] = {
77 {
David Vincze2d736ad2019-02-18 11:50:22 +010078 .magic_primary_slot = BOOT_MAGIC_ANY,
79 .magic_secondary_slot = BOOT_MAGIC_GOOD,
80 .image_ok_primary_slot = BOOT_FLAG_ANY,
81 .image_ok_secondary_slot = BOOT_FLAG_UNSET,
82 .copy_done_primary_slot = BOOT_FLAG_ANY,
83 .swap_type = BOOT_SWAP_TYPE_TEST,
Christopher Collins92ea77f2016-12-12 15:59:26 -080084 },
Christopher Collins92ea77f2016-12-12 15:59:26 -080085 {
David Vincze2d736ad2019-02-18 11:50:22 +010086 .magic_primary_slot = BOOT_MAGIC_ANY,
87 .magic_secondary_slot = BOOT_MAGIC_GOOD,
88 .image_ok_primary_slot = BOOT_FLAG_ANY,
89 .image_ok_secondary_slot = BOOT_FLAG_SET,
90 .copy_done_primary_slot = BOOT_FLAG_ANY,
91 .swap_type = BOOT_SWAP_TYPE_PERM,
Christopher Collins92ea77f2016-12-12 15:59:26 -080092 },
Christopher Collins92ea77f2016-12-12 15:59:26 -080093 {
David Vincze2d736ad2019-02-18 11:50:22 +010094 .magic_primary_slot = BOOT_MAGIC_GOOD,
95 .magic_secondary_slot = BOOT_MAGIC_UNSET,
96 .image_ok_primary_slot = BOOT_FLAG_UNSET,
97 .image_ok_secondary_slot = BOOT_FLAG_ANY,
98 .copy_done_primary_slot = BOOT_FLAG_SET,
99 .swap_type = BOOT_SWAP_TYPE_REVERT,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800100 },
101};
102
103#define BOOT_SWAP_TABLES_COUNT \
104 (sizeof boot_swap_tables / sizeof boot_swap_tables[0])
105
Fabio Utzig39000012018-07-30 12:40:20 -0300106static int
Fabio Utzig178be542018-09-19 08:12:56 -0300107boot_magic_decode(const uint32_t *magic)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800108{
Fabio Utzig24a273d2017-04-20 08:21:31 -0300109 if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800110 return BOOT_MAGIC_GOOD;
111 }
Fabio Utzig178be542018-09-19 08:12:56 -0300112 return BOOT_MAGIC_BAD;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800113}
114
Fabio Utzig39000012018-07-30 12:40:20 -0300115static int
Fabio Utzig178be542018-09-19 08:12:56 -0300116boot_flag_decode(uint8_t flag)
Fabio Utzig39000012018-07-30 12:40:20 -0300117{
Fabio Utzig39000012018-07-30 12:40:20 -0300118 if (flag != BOOT_FLAG_SET) {
119 return BOOT_FLAG_BAD;
120 }
121 return BOOT_FLAG_SET;
122}
123
Christopher Collinsa1c12042019-05-23 14:00:28 -0700124/**
125 * Determines if a status source table is satisfied by the specified magic
126 * code.
127 *
128 * @param tbl_val A magic field from a status source table.
129 * @param val The magic value in a trailer, encoded as a
130 * BOOT_MAGIC_[...].
131 *
132 * @return 1 if the two values are compatible;
133 * 0 otherwise.
134 */
135int
136boot_magic_compatible_check(uint8_t tbl_val, uint8_t val)
137{
138 switch (tbl_val) {
139 case BOOT_MAGIC_ANY:
140 return 1;
141
142 case BOOT_MAGIC_NOTGOOD:
143 return val != BOOT_MAGIC_GOOD;
144
145 default:
146 return tbl_val == val;
147 }
148}
149
Christopher Collins92ea77f2016-12-12 15:59:26 -0800150uint32_t
Fabio Utzig3fbbdac2019-12-19 15:18:23 -0300151boot_status_sz(uint32_t min_write_sz)
152{
153 return /* state for all sectors */
154 BOOT_STATUS_MAX_ENTRIES * BOOT_STATUS_STATE_COUNT * min_write_sz;
155}
156
157uint32_t
David Brownab449182019-11-15 09:32:52 -0700158boot_trailer_sz(uint32_t min_write_sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800159{
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300160 return /* state for all sectors */
Fabio Utzig3fbbdac2019-12-19 15:18:23 -0300161 boot_status_sz(min_write_sz) +
Fabio Utzigba829042018-09-18 08:29:34 -0300162#ifdef MCUBOOT_ENC_IMAGES
163 /* encryption keys */
Fabio Utzig4741c452019-12-19 15:32:41 -0300164# if MCUBOOT_SWAP_SAVE_ENCTLV
165 BOOT_ENC_TLV_ALIGN_SIZE * 2 +
166# else
Fabio Utzigba829042018-09-18 08:29:34 -0300167 BOOT_ENC_KEY_SIZE * 2 +
Fabio Utzig4741c452019-12-19 15:32:41 -0300168# endif
Fabio Utzigba829042018-09-18 08:29:34 -0300169#endif
Christopher Collins2adef702019-05-22 14:37:31 -0700170 /* swap_type + copy_done + image_ok + swap_size */
171 BOOT_MAX_ALIGN * 4 +
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300172 BOOT_MAGIC_SZ;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800173}
174
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400175int
Fabio Utzigb0f04732019-07-31 09:49:19 -0300176boot_status_entries(int image_index, const struct flash_area *fap)
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400177{
Fabio Utzig12d59162019-11-28 10:01:59 -0300178#if MCUBOOT_SWAP_USING_SCRATCH
David Vinczeb75c12a2019-03-22 14:58:33 +0100179 if (fap->fa_id == FLASH_AREA_IMAGE_SCRATCH) {
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400180 return BOOT_STATUS_STATE_COUNT;
Fabio Utzig12d59162019-11-28 10:01:59 -0300181 } else
182#endif
183 if (fap->fa_id == FLASH_AREA_IMAGE_PRIMARY(image_index) ||
Fabio Utzigb0f04732019-07-31 09:49:19 -0300184 fap->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
David Vinczeb75c12a2019-03-22 14:58:33 +0100185 return BOOT_STATUS_STATE_COUNT * BOOT_STATUS_MAX_ENTRIES;
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400186 }
Fabio Utzig9d160092019-08-09 07:46:34 -0300187 return -1;
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400188}
189
Christopher Collins92ea77f2016-12-12 15:59:26 -0800190uint32_t
191boot_status_off(const struct flash_area *fap)
192{
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300193 uint32_t off_from_end;
194 uint8_t elem_sz;
195
196 elem_sz = flash_area_align(fap);
197
Christopher Collins2adef702019-05-22 14:37:31 -0700198 off_from_end = boot_trailer_sz(elem_sz);
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300199
200 assert(off_from_end <= fap->fa_size);
201 return fap->fa_size - off_from_end;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800202}
203
Fabio Utzig4e8113b2019-08-09 09:11:18 -0300204static inline uint32_t
205boot_magic_off(const struct flash_area *fap)
206{
207 return fap->fa_size - BOOT_MAGIC_SZ;
208}
209
210static inline uint32_t
211boot_image_ok_off(const struct flash_area *fap)
212{
213 return boot_magic_off(fap) - BOOT_MAX_ALIGN;
214}
215
216static inline uint32_t
217boot_copy_done_off(const struct flash_area *fap)
218{
219 return boot_image_ok_off(fap) - BOOT_MAX_ALIGN;
220}
221
Christopher Collinsa1c12042019-05-23 14:00:28 -0700222uint32_t
David Vinczee2453472019-06-17 12:31:59 +0200223boot_swap_info_off(const struct flash_area *fap)
Christopher Collinsa1c12042019-05-23 14:00:28 -0700224{
Fabio Utzig4e8113b2019-08-09 09:11:18 -0300225 return boot_copy_done_off(fap) - BOOT_MAX_ALIGN;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700226}
227
Fabio Utzig4e8113b2019-08-09 09:11:18 -0300228static inline uint32_t
Fabio Utzig46490722017-09-04 15:34:32 -0300229boot_swap_size_off(const struct flash_area *fap)
230{
Fabio Utzig4e8113b2019-08-09 09:11:18 -0300231 return boot_swap_info_off(fap) - BOOT_MAX_ALIGN;
Fabio Utzig46490722017-09-04 15:34:32 -0300232}
233
Fabio Utzigba829042018-09-18 08:29:34 -0300234#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig4e8113b2019-08-09 09:11:18 -0300235static inline uint32_t
Fabio Utzigba829042018-09-18 08:29:34 -0300236boot_enc_key_off(const struct flash_area *fap, uint8_t slot)
237{
Fabio Utzig4741c452019-12-19 15:32:41 -0300238#if MCUBOOT_SWAP_SAVE_ENCTLV
239 return boot_swap_size_off(fap) - ((slot + 1) *
240 ((((BOOT_ENC_TLV_SIZE - 1) / BOOT_MAX_ALIGN) + 1) * BOOT_MAX_ALIGN));
241#else
Fabio Utzig4e8113b2019-08-09 09:11:18 -0300242 return boot_swap_size_off(fap) - ((slot + 1) * BOOT_ENC_KEY_SIZE);
Fabio Utzig4741c452019-12-19 15:32:41 -0300243#endif
Fabio Utzigba829042018-09-18 08:29:34 -0300244}
245#endif
246
Christopher Collins92ea77f2016-12-12 15:59:26 -0800247int
248boot_read_swap_state(const struct flash_area *fap,
249 struct boot_swap_state *state)
250{
Hovland, Sigvart1d96f362018-09-25 13:23:42 +0200251 uint32_t magic[BOOT_MAGIC_ARR_SZ];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800252 uint32_t off;
David Vinczee2453472019-06-17 12:31:59 +0200253 uint8_t swap_info;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800254 int rc;
255
256 off = boot_magic_off(fap);
Fabio Utzig178be542018-09-19 08:12:56 -0300257 rc = flash_area_read_is_empty(fap, off, magic, BOOT_MAGIC_SZ);
258 if (rc < 0) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800259 return BOOT_EFLASH;
260 }
Fabio Utzig178be542018-09-19 08:12:56 -0300261 if (rc == 1) {
262 state->magic = BOOT_MAGIC_UNSET;
263 } else {
264 state->magic = boot_magic_decode(magic);
265 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800266
David Vinczee2453472019-06-17 12:31:59 +0200267 off = boot_swap_info_off(fap);
268 rc = flash_area_read_is_empty(fap, off, &swap_info, sizeof swap_info);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700269 if (rc < 0) {
270 return BOOT_EFLASH;
271 }
David Vinczee2453472019-06-17 12:31:59 +0200272
273 /* Extract the swap type and image number */
274 state->swap_type = BOOT_GET_SWAP_TYPE(swap_info);
275 state->image_num = BOOT_GET_IMAGE_NUM(swap_info);
276
Christopher Collinsa1c12042019-05-23 14:00:28 -0700277 if (rc == 1 || state->swap_type > BOOT_SWAP_TYPE_REVERT) {
278 state->swap_type = BOOT_SWAP_TYPE_NONE;
David Vinczee2453472019-06-17 12:31:59 +0200279 state->image_num = 0;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700280 }
281
Christopher Collins2adef702019-05-22 14:37:31 -0700282 off = boot_copy_done_off(fap);
283 rc = flash_area_read_is_empty(fap, off, &state->copy_done,
284 sizeof state->copy_done);
285 if (rc < 0) {
286 return BOOT_EFLASH;
287 }
288 if (rc == 1) {
289 state->copy_done = BOOT_FLAG_UNSET;
290 } else {
291 state->copy_done = boot_flag_decode(state->copy_done);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800292 }
293
294 off = boot_image_ok_off(fap);
David Vincze2d736ad2019-02-18 11:50:22 +0100295 rc = flash_area_read_is_empty(fap, off, &state->image_ok,
296 sizeof state->image_ok);
Fabio Utzig178be542018-09-19 08:12:56 -0300297 if (rc < 0) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800298 return BOOT_EFLASH;
299 }
Fabio Utzig178be542018-09-19 08:12:56 -0300300 if (rc == 1) {
301 state->image_ok = BOOT_FLAG_UNSET;
302 } else {
303 state->image_ok = boot_flag_decode(state->image_ok);
304 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800305
306 return 0;
307}
308
309/**
310 * Reads the image trailer from the scratch area.
311 */
312int
Fabio Utzig2473ac02017-05-02 12:45:02 -0300313boot_read_swap_state_by_id(int flash_area_id, struct boot_swap_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800314{
315 const struct flash_area *fap;
316 int rc;
317
Fabio Utzigb0f04732019-07-31 09:49:19 -0300318 rc = flash_area_open(flash_area_id, &fap);
319 if (rc != 0) {
320 return BOOT_EFLASH;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300321 }
322
323 rc = boot_read_swap_state(fap, state);
Fabio Utzigacfba2e2017-05-22 11:06:29 -0400324 flash_area_close(fap);
325 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800326}
327
Fabio Utzigdf0cc502019-08-09 09:10:41 -0300328/**
329 * This functions tries to locate the status area after an aborted swap,
330 * by looking for the magic in the possible locations.
331 *
Sam Bristowd0ca0ff2019-10-30 20:51:35 +1300332 * If the magic is successfully found, a flash_area * is returned and it
Fabio Utzigdf0cc502019-08-09 09:10:41 -0300333 * is the responsibility of the called to close it.
334 *
335 * @returns 0 on success, -1 on errors
336 */
337static int
338boot_find_status(int image_index, const struct flash_area **fap)
339{
340 uint32_t magic[BOOT_MAGIC_ARR_SZ];
341 uint32_t off;
342 uint8_t areas[2] = {
Fabio Utzig12d59162019-11-28 10:01:59 -0300343#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzigdf0cc502019-08-09 09:10:41 -0300344 FLASH_AREA_IMAGE_SCRATCH,
Fabio Utzig12d59162019-11-28 10:01:59 -0300345#endif
Fabio Utzig3c446072019-11-22 12:48:26 -0300346 FLASH_AREA_IMAGE_PRIMARY(image_index),
Fabio Utzigdf0cc502019-08-09 09:10:41 -0300347 };
348 unsigned int i;
349 int rc;
350
351 /*
352 * In the middle a swap, tries to locate the area that is currently
353 * storing a valid magic, first on the primary slot, then on scratch.
354 * Both "slots" can end up being temporary storage for a swap and it
355 * is assumed that if magic is valid then other metadata is too,
356 * because magic is always written in the last step.
357 */
358
359 for (i = 0; i < sizeof(areas) / sizeof(areas[0]); i++) {
360 rc = flash_area_open(areas[i], fap);
361 if (rc != 0) {
362 return rc;
363 }
364
365 off = boot_magic_off(*fap);
366 rc = flash_area_read(*fap, off, magic, BOOT_MAGIC_SZ);
367 if (rc != 0) {
368 flash_area_close(*fap);
369 return rc;
370 }
371
372 if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0) {
373 return 0;
374 }
375
376 flash_area_close(*fap);
377 }
378
379 /* If we got here, no magic was found */
380 return -1;
381}
382
Christopher Collins92ea77f2016-12-12 15:59:26 -0800383int
Fabio Utzigb0f04732019-07-31 09:49:19 -0300384boot_read_swap_size(int image_index, uint32_t *swap_size)
Fabio Utzig46490722017-09-04 15:34:32 -0300385{
Fabio Utzig46490722017-09-04 15:34:32 -0300386 uint32_t off;
387 const struct flash_area *fap;
388 int rc;
389
Fabio Utzigdf0cc502019-08-09 09:10:41 -0300390 rc = boot_find_status(image_index, &fap);
391 if (rc == 0) {
392 off = boot_swap_size_off(fap);
393 rc = flash_area_read(fap, off, swap_size, sizeof *swap_size);
Fabio Utzig46490722017-09-04 15:34:32 -0300394 flash_area_close(fap);
Fabio Utzig46490722017-09-04 15:34:32 -0300395 }
396
Fabio Utzig46490722017-09-04 15:34:32 -0300397 return rc;
398}
399
Fabio Utzigba829042018-09-18 08:29:34 -0300400#ifdef MCUBOOT_ENC_IMAGES
401int
Fabio Utzig4741c452019-12-19 15:32:41 -0300402boot_read_enc_key(int image_index, uint8_t slot, struct boot_status *bs)
Fabio Utzigba829042018-09-18 08:29:34 -0300403{
Fabio Utzigba829042018-09-18 08:29:34 -0300404 uint32_t off;
405 const struct flash_area *fap;
Fabio Utzig4741c452019-12-19 15:32:41 -0300406#if MCUBOOT_SWAP_SAVE_ENCTLV
407 int i;
408#endif
Fabio Utzigba829042018-09-18 08:29:34 -0300409 int rc;
410
Fabio Utzigdf0cc502019-08-09 09:10:41 -0300411 rc = boot_find_status(image_index, &fap);
412 if (rc == 0) {
413 off = boot_enc_key_off(fap, slot);
Fabio Utzig4741c452019-12-19 15:32:41 -0300414#if MCUBOOT_SWAP_SAVE_ENCTLV
415 rc = flash_area_read(fap, off, bs->enctlv[slot], BOOT_ENC_TLV_ALIGN_SIZE);
416 if (rc == 0) {
417 for (i = 0; i < BOOT_ENC_TLV_ALIGN_SIZE; i++) {
418 if (bs->enctlv[slot][i] != 0xff) {
419 break;
420 }
421 }
422 /* Only try to decrypt non-erased TLV metadata */
423 if (i != BOOT_ENC_TLV_ALIGN_SIZE) {
424 rc = boot_enc_decrypt(bs->enctlv[slot], bs->enckey[slot]);
425 }
426 }
427#else
428 rc = flash_area_read(fap, off, bs->enckey[slot], BOOT_ENC_KEY_SIZE);
429#endif
Fabio Utzigba829042018-09-18 08:29:34 -0300430 flash_area_close(fap);
Fabio Utzigba829042018-09-18 08:29:34 -0300431 }
432
Fabio Utzigba829042018-09-18 08:29:34 -0300433 return rc;
434}
435#endif
Fabio Utzig46490722017-09-04 15:34:32 -0300436
437int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800438boot_write_magic(const struct flash_area *fap)
439{
440 uint32_t off;
441 int rc;
442
443 off = boot_magic_off(fap);
444
Ben McCrea4c0ee952019-08-28 09:13:24 -0700445 BOOT_LOG_DBG("writing magic; fa_id=%d off=0x%lx (0x%lx)",
Marti Bolivar99ec3832019-09-12 21:21:26 -0600446 fap->fa_id, (unsigned long)off,
447 (unsigned long)(fap->fa_off + off));
Fabio Utzig24a273d2017-04-20 08:21:31 -0300448 rc = flash_area_write(fap, off, boot_img_magic, BOOT_MAGIC_SZ);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800449 if (rc != 0) {
450 return BOOT_EFLASH;
451 }
452
453 return 0;
454}
455
Fabio Utzig1a1ec172019-08-09 10:21:43 -0300456/**
457 * Write trailer data; status bytes, swap_size, etc
458 *
459 * @returns 0 on success, != 0 on error.
460 */
Fabio Utzig2473ac02017-05-02 12:45:02 -0300461static int
Fabio Utzig1a1ec172019-08-09 10:21:43 -0300462boot_write_trailer(const struct flash_area *fap, uint32_t off,
463 const uint8_t *inbuf, uint8_t inlen)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800464{
Fabio Utzig644b8d42017-04-20 07:56:05 -0300465 uint8_t buf[BOOT_MAX_ALIGN];
David Brown9d725462017-01-23 15:50:58 -0700466 uint8_t align;
Fabio Utzig39000012018-07-30 12:40:20 -0300467 uint8_t erased_val;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700468 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800469
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200470 align = flash_area_align(fap);
Fabio Utzig1a1ec172019-08-09 10:21:43 -0300471 if (inlen > BOOT_MAX_ALIGN || align > BOOT_MAX_ALIGN) {
472 return -1;
473 }
Fabio Utzig39000012018-07-30 12:40:20 -0300474 erased_val = flash_area_erased_val(fap);
Fabio Utzig1a1ec172019-08-09 10:21:43 -0300475 if (align < inlen) {
476 align = inlen;
477 }
478 memcpy(buf, inbuf, inlen);
479 memset(&buf[inlen], erased_val, align - inlen);
David Brown9d725462017-01-23 15:50:58 -0700480
481 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800482 if (rc != 0) {
483 return BOOT_EFLASH;
484 }
485
486 return 0;
487}
488
Fabio Utzig1a1ec172019-08-09 10:21:43 -0300489static int
490boot_write_trailer_flag(const struct flash_area *fap, uint32_t off,
491 uint8_t flag_val)
492{
493 const uint8_t buf[1] = { flag_val };
494 return boot_write_trailer(fap, off, buf, 1);
495}
496
Christopher Collins92ea77f2016-12-12 15:59:26 -0800497int
Fabio Utzig2473ac02017-05-02 12:45:02 -0300498boot_write_copy_done(const struct flash_area *fap)
499{
Christopher Collinsa1c12042019-05-23 14:00:28 -0700500 uint32_t off;
501
502 off = boot_copy_done_off(fap);
Ben McCrea4c0ee952019-08-28 09:13:24 -0700503 BOOT_LOG_DBG("writing copy_done; fa_id=%d off=0x%lx (0x%lx)",
Marti Bolivar99ec3832019-09-12 21:21:26 -0600504 fap->fa_id, (unsigned long)off,
505 (unsigned long)(fap->fa_off + off));
Fabio Utzig1a1ec172019-08-09 10:21:43 -0300506 return boot_write_trailer_flag(fap, off, BOOT_FLAG_SET);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300507}
508
509int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800510boot_write_image_ok(const struct flash_area *fap)
511{
Christopher Collinsa1c12042019-05-23 14:00:28 -0700512 uint32_t off;
513
514 off = boot_image_ok_off(fap);
Ben McCrea4c0ee952019-08-28 09:13:24 -0700515 BOOT_LOG_DBG("writing image_ok; fa_id=%d off=0x%lx (0x%lx)",
Marti Bolivar99ec3832019-09-12 21:21:26 -0600516 fap->fa_id, (unsigned long)off,
517 (unsigned long)(fap->fa_off + off));
Fabio Utzig1a1ec172019-08-09 10:21:43 -0300518 return boot_write_trailer_flag(fap, off, BOOT_FLAG_SET);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700519}
520
521/**
522 * Writes the specified value to the `swap-type` field of an image trailer.
523 * This value is persisted so that the boot loader knows what swap operation to
524 * resume in case of an unexpected reset.
525 */
526int
David Vinczee2453472019-06-17 12:31:59 +0200527boot_write_swap_info(const struct flash_area *fap, uint8_t swap_type,
528 uint8_t image_num)
Christopher Collinsa1c12042019-05-23 14:00:28 -0700529{
530 uint32_t off;
David Vinczee2453472019-06-17 12:31:59 +0200531 uint8_t swap_info;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700532
David Vinczee2453472019-06-17 12:31:59 +0200533 BOOT_SET_SWAP_INFO(swap_info, image_num, swap_type);
534 off = boot_swap_info_off(fap);
Ben McCrea4c0ee952019-08-28 09:13:24 -0700535 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 +0200536 " image_num=0x%x",
Marti Bolivar99ec3832019-09-12 21:21:26 -0600537 fap->fa_id, (unsigned long)off,
538 (unsigned long)(fap->fa_off + off), swap_type, image_num);
Fabio Utzig1a1ec172019-08-09 10:21:43 -0300539 return boot_write_trailer(fap, off, (const uint8_t *) &swap_info, 1);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800540}
541
542int
Fabio Utzig46490722017-09-04 15:34:32 -0300543boot_write_swap_size(const struct flash_area *fap, uint32_t swap_size)
544{
545 uint32_t off;
Fabio Utzig46490722017-09-04 15:34:32 -0300546
547 off = boot_swap_size_off(fap);
Ben McCrea4c0ee952019-08-28 09:13:24 -0700548 BOOT_LOG_DBG("writing swap_size; fa_id=%d off=0x%lx (0x%lx)",
Marti Bolivar99ec3832019-09-12 21:21:26 -0600549 fap->fa_id, (unsigned long)off,
550 (unsigned long)fap->fa_off + off);
Fabio Utzig1a1ec172019-08-09 10:21:43 -0300551 return boot_write_trailer(fap, off, (const uint8_t *) &swap_size, 4);
Fabio Utzig46490722017-09-04 15:34:32 -0300552}
553
Fabio Utzigba829042018-09-18 08:29:34 -0300554#ifdef MCUBOOT_ENC_IMAGES
555int
Fabio Utzig4741c452019-12-19 15:32:41 -0300556boot_write_enc_key(const struct flash_area *fap, uint8_t slot,
557 const struct boot_status *bs)
Fabio Utzigba829042018-09-18 08:29:34 -0300558{
559 uint32_t off;
560 int rc;
561
562 off = boot_enc_key_off(fap, slot);
Fabio Utzig5e6ea222019-12-10 09:49:59 -0300563 BOOT_LOG_DBG("writing enc_key; fa_id=%d off=0x%lx (0x%lx)",
564 fap->fa_id, (unsigned long)off,
565 (unsigned long)fap->fa_off + off);
Fabio Utzig4741c452019-12-19 15:32:41 -0300566#if MCUBOOT_SWAP_SAVE_ENCTLV
567 rc = flash_area_write(fap, off, bs->enctlv[slot], BOOT_ENC_TLV_ALIGN_SIZE);
568#else
569 rc = flash_area_write(fap, off, bs->enckey[slot], BOOT_ENC_KEY_SIZE);
570#endif
Fabio Utzigba829042018-09-18 08:29:34 -0300571 if (rc != 0) {
572 return BOOT_EFLASH;
573 }
574
575 return 0;
576}
577#endif
578
Fabio Utzig46490722017-09-04 15:34:32 -0300579int
Fabio Utzigb0f04732019-07-31 09:49:19 -0300580boot_swap_type_multi(int image_index)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800581{
582 const struct boot_swap_table *table;
David Vincze2d736ad2019-02-18 11:50:22 +0100583 struct boot_swap_state primary_slot;
584 struct boot_swap_state secondary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800585 int rc;
Fabio Utzigcd5774b2017-11-29 10:18:26 -0200586 size_t i;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800587
Fabio Utzigb0f04732019-07-31 09:49:19 -0300588 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(image_index),
589 &primary_slot);
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300590 if (rc) {
591 return BOOT_SWAP_TYPE_PANIC;
592 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800593
Fabio Utzigb0f04732019-07-31 09:49:19 -0300594 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(image_index),
David Vincze2d736ad2019-02-18 11:50:22 +0100595 &secondary_slot);
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300596 if (rc) {
597 return BOOT_SWAP_TYPE_PANIC;
598 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800599
600 for (i = 0; i < BOOT_SWAP_TABLES_COUNT; i++) {
601 table = boot_swap_tables + i;
602
Christopher Collinsa1c12042019-05-23 14:00:28 -0700603 if (boot_magic_compatible_check(table->magic_primary_slot,
604 primary_slot.magic) &&
605 boot_magic_compatible_check(table->magic_secondary_slot,
606 secondary_slot.magic) &&
David Vincze2d736ad2019-02-18 11:50:22 +0100607 (table->image_ok_primary_slot == BOOT_FLAG_ANY ||
608 table->image_ok_primary_slot == primary_slot.image_ok) &&
609 (table->image_ok_secondary_slot == BOOT_FLAG_ANY ||
610 table->image_ok_secondary_slot == secondary_slot.image_ok) &&
611 (table->copy_done_primary_slot == BOOT_FLAG_ANY ||
612 table->copy_done_primary_slot == primary_slot.copy_done)) {
Fabio Utzig34e393e2017-05-22 11:07:07 -0400613 BOOT_LOG_INF("Swap type: %s",
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300614 table->swap_type == BOOT_SWAP_TYPE_TEST ? "test" :
615 table->swap_type == BOOT_SWAP_TYPE_PERM ? "perm" :
616 table->swap_type == BOOT_SWAP_TYPE_REVERT ? "revert" :
617 "BUG; can't happen");
Fabio Utzigf0dbd422019-08-09 10:22:05 -0300618 if (table->swap_type != BOOT_SWAP_TYPE_TEST &&
619 table->swap_type != BOOT_SWAP_TYPE_PERM &&
620 table->swap_type != BOOT_SWAP_TYPE_REVERT) {
621 return BOOT_SWAP_TYPE_PANIC;
622 }
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300623 return table->swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800624 }
625 }
626
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300627 BOOT_LOG_INF("Swap type: none");
Christopher Collins92ea77f2016-12-12 15:59:26 -0800628 return BOOT_SWAP_TYPE_NONE;
629}
630
Fabio Utzig75e9a592019-08-27 09:10:36 -0300631/*
632 * This function is not used by the bootloader itself, but its required API
633 * by external tooling like mcumgr.
634 */
Fabio Utzigb0f04732019-07-31 09:49:19 -0300635int
636boot_swap_type(void)
637{
638 return boot_swap_type_multi(0);
639}
640
Christopher Collins92ea77f2016-12-12 15:59:26 -0800641/**
David Vincze2d736ad2019-02-18 11:50:22 +0100642 * Marks the image in the secondary slot as pending. On the next reboot,
643 * the system will perform a one-time boot of the the secondary slot image.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800644 *
Christopher Collins7835c1e2016-12-21 10:10:51 -0800645 * @param permanent Whether the image should be used permanently or
646 * only tested once:
647 * 0=run image once, then confirm or revert.
648 * 1=run image forever.
649 *
Christopher Collins92ea77f2016-12-12 15:59:26 -0800650 * @return 0 on success; nonzero on failure.
651 */
652int
Christopher Collins7835c1e2016-12-21 10:10:51 -0800653boot_set_pending(int permanent)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800654{
655 const struct flash_area *fap;
David Vincze2d736ad2019-02-18 11:50:22 +0100656 struct boot_swap_state state_secondary_slot;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700657 uint8_t swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800658 int rc;
659
Fabio Utzigb0f04732019-07-31 09:49:19 -0300660 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(0),
David Vincze2d736ad2019-02-18 11:50:22 +0100661 &state_secondary_slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800662 if (rc != 0) {
663 return rc;
664 }
665
David Vincze2d736ad2019-02-18 11:50:22 +0100666 switch (state_secondary_slot.magic) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800667 case BOOT_MAGIC_GOOD:
668 /* Swap already scheduled. */
669 return 0;
670
671 case BOOT_MAGIC_UNSET:
Fabio Utzigb0f04732019-07-31 09:49:19 -0300672 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(0), &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800673 if (rc != 0) {
674 rc = BOOT_EFLASH;
675 } else {
676 rc = boot_write_magic(fap);
677 }
678
Christopher Collins7835c1e2016-12-21 10:10:51 -0800679 if (rc == 0 && permanent) {
680 rc = boot_write_image_ok(fap);
681 }
682
Christopher Collinsa1c12042019-05-23 14:00:28 -0700683 if (rc == 0) {
684 if (permanent) {
685 swap_type = BOOT_SWAP_TYPE_PERM;
686 } else {
687 swap_type = BOOT_SWAP_TYPE_TEST;
688 }
David Vinczee2453472019-06-17 12:31:59 +0200689 rc = boot_write_swap_info(fap, swap_type, 0);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700690 }
691
Christopher Collins92ea77f2016-12-12 15:59:26 -0800692 flash_area_close(fap);
693 return rc;
694
Christopher Collinsae01f152019-01-30 09:56:37 -0800695 case BOOT_MAGIC_BAD:
696 /* The image slot is corrupt. There is no way to recover, so erase the
697 * slot to allow future upgrades.
698 */
Fabio Utzigb0f04732019-07-31 09:49:19 -0300699 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(0), &fap);
Christopher Collinsae01f152019-01-30 09:56:37 -0800700 if (rc != 0) {
701 return BOOT_EFLASH;
702 }
703
704 flash_area_erase(fap, 0, fap->fa_size);
705 flash_area_close(fap);
706 return BOOT_EBADIMAGE;
707
Christopher Collins92ea77f2016-12-12 15:59:26 -0800708 default:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800709 assert(0);
Christopher Collinsae01f152019-01-30 09:56:37 -0800710 return BOOT_EBADIMAGE;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800711 }
712}
713
714/**
David Vincze2d736ad2019-02-18 11:50:22 +0100715 * Marks the image in the primary slot as confirmed. The system will continue
716 * booting into the image in the primary slot until told to boot from a
717 * different slot.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800718 *
719 * @return 0 on success; nonzero on failure.
720 */
721int
722boot_set_confirmed(void)
723{
724 const struct flash_area *fap;
David Vincze2d736ad2019-02-18 11:50:22 +0100725 struct boot_swap_state state_primary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800726 int rc;
727
Fabio Utzigb0f04732019-07-31 09:49:19 -0300728 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(0),
David Vincze2d736ad2019-02-18 11:50:22 +0100729 &state_primary_slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800730 if (rc != 0) {
731 return rc;
732 }
733
David Vincze2d736ad2019-02-18 11:50:22 +0100734 switch (state_primary_slot.magic) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800735 case BOOT_MAGIC_GOOD:
736 /* Confirm needed; proceed. */
737 break;
738
739 case BOOT_MAGIC_UNSET:
740 /* Already confirmed. */
741 return 0;
742
743 case BOOT_MAGIC_BAD:
744 /* Unexpected state. */
745 return BOOT_EBADVECT;
746 }
747
Fabio Utzigb0f04732019-07-31 09:49:19 -0300748 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(0), &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800749 if (rc) {
750 rc = BOOT_EFLASH;
751 goto done;
752 }
753
David Vincze2d736ad2019-02-18 11:50:22 +0100754 if (state_primary_slot.copy_done == BOOT_FLAG_UNSET) {
Fabio Utzig39000012018-07-30 12:40:20 -0300755 /* Swap never completed. This is unexpected. */
756 rc = BOOT_EBADVECT;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800757 goto done;
758 }
759
David Vincze2d736ad2019-02-18 11:50:22 +0100760 if (state_primary_slot.image_ok != BOOT_FLAG_UNSET) {
Fabio Utzig39000012018-07-30 12:40:20 -0300761 /* Already confirmed. */
762 goto done;
763 }
764
765 rc = boot_write_image_ok(fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800766
767done:
768 flash_area_close(fap);
769 return rc;
770}