blob: e15579e934b149aa3511a536e389b4c60a62da64 [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
Fabio Utzig24a273d2017-04-20 08:21:31 -030055const uint32_t BOOT_MAGIC_SZ = sizeof boot_img_magic;
Fabio Utziga0bc9b52017-06-28 09:19:55 -030056const uint32_t BOOT_MAX_ALIGN = MAX_FLASH_ALIGN;
Fabio Utzig24a273d2017-04-20 08:21:31 -030057
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
Christopher Collins2adef702019-05-22 14:37:31 -0700154boot_trailer_sz(uint8_t min_write_sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800155{
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300156 return /* state for all sectors */
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300157 BOOT_STATUS_MAX_ENTRIES * BOOT_STATUS_STATE_COUNT * min_write_sz +
Fabio Utzigba829042018-09-18 08:29:34 -0300158#ifdef MCUBOOT_ENC_IMAGES
159 /* encryption keys */
160 BOOT_ENC_KEY_SIZE * 2 +
161#endif
Christopher Collins2adef702019-05-22 14:37:31 -0700162 /* swap_type + copy_done + image_ok + swap_size */
163 BOOT_MAX_ALIGN * 4 +
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300164 BOOT_MAGIC_SZ;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800165}
166
167static uint32_t
168boot_magic_off(const struct flash_area *fap)
169{
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300170 return fap->fa_size - BOOT_MAGIC_SZ;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800171}
172
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400173int
Fabio Utzigb0f04732019-07-31 09:49:19 -0300174boot_status_entries(int image_index, const struct flash_area *fap)
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400175{
David Vinczeb75c12a2019-03-22 14:58:33 +0100176 if (fap->fa_id == FLASH_AREA_IMAGE_SCRATCH) {
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400177 return BOOT_STATUS_STATE_COUNT;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300178 } else if (fap->fa_id == FLASH_AREA_IMAGE_PRIMARY(image_index) ||
179 fap->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
David Vinczeb75c12a2019-03-22 14:58:33 +0100180 return BOOT_STATUS_STATE_COUNT * BOOT_STATUS_MAX_ENTRIES;
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400181 }
Fabio Utzig9d160092019-08-09 07:46:34 -0300182 return -1;
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400183}
184
Christopher Collins92ea77f2016-12-12 15:59:26 -0800185uint32_t
186boot_status_off(const struct flash_area *fap)
187{
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300188 uint32_t off_from_end;
189 uint8_t elem_sz;
190
191 elem_sz = flash_area_align(fap);
192
Christopher Collins2adef702019-05-22 14:37:31 -0700193 off_from_end = boot_trailer_sz(elem_sz);
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300194
195 assert(off_from_end <= fap->fa_size);
196 return fap->fa_size - off_from_end;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800197}
198
Christopher Collinsa1c12042019-05-23 14:00:28 -0700199uint32_t
David Vinczee2453472019-06-17 12:31:59 +0200200boot_swap_info_off(const struct flash_area *fap)
Christopher Collinsa1c12042019-05-23 14:00:28 -0700201{
202 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 3;
203}
204
Christopher Collins92ea77f2016-12-12 15:59:26 -0800205static uint32_t
206boot_copy_done_off(const struct flash_area *fap)
207{
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300208 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 2;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800209}
210
211static uint32_t
212boot_image_ok_off(const struct flash_area *fap)
213{
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300214 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800215}
216
Fabio Utzig46490722017-09-04 15:34:32 -0300217static uint32_t
218boot_swap_size_off(const struct flash_area *fap)
219{
Christopher Collins2adef702019-05-22 14:37:31 -0700220 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 4;
Fabio Utzig46490722017-09-04 15:34:32 -0300221}
222
Fabio Utzigba829042018-09-18 08:29:34 -0300223#ifdef MCUBOOT_ENC_IMAGES
224static uint32_t
225boot_enc_key_off(const struct flash_area *fap, uint8_t slot)
226{
Christopher Collins2adef702019-05-22 14:37:31 -0700227 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 4 -
Fabio Utzigba829042018-09-18 08:29:34 -0300228 ((slot + 1) * BOOT_ENC_KEY_SIZE);
229}
230#endif
231
Christopher Collins92ea77f2016-12-12 15:59:26 -0800232int
233boot_read_swap_state(const struct flash_area *fap,
234 struct boot_swap_state *state)
235{
Hovland, Sigvart1d96f362018-09-25 13:23:42 +0200236 uint32_t magic[BOOT_MAGIC_ARR_SZ];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800237 uint32_t off;
David Vinczee2453472019-06-17 12:31:59 +0200238 uint8_t swap_info;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800239 int rc;
240
241 off = boot_magic_off(fap);
Fabio Utzig178be542018-09-19 08:12:56 -0300242 rc = flash_area_read_is_empty(fap, off, magic, BOOT_MAGIC_SZ);
243 if (rc < 0) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800244 return BOOT_EFLASH;
245 }
Fabio Utzig178be542018-09-19 08:12:56 -0300246 if (rc == 1) {
247 state->magic = BOOT_MAGIC_UNSET;
248 } else {
249 state->magic = boot_magic_decode(magic);
250 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800251
David Vinczee2453472019-06-17 12:31:59 +0200252 off = boot_swap_info_off(fap);
253 rc = flash_area_read_is_empty(fap, off, &swap_info, sizeof swap_info);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700254 if (rc < 0) {
255 return BOOT_EFLASH;
256 }
David Vinczee2453472019-06-17 12:31:59 +0200257
258 /* Extract the swap type and image number */
259 state->swap_type = BOOT_GET_SWAP_TYPE(swap_info);
260 state->image_num = BOOT_GET_IMAGE_NUM(swap_info);
261
Christopher Collinsa1c12042019-05-23 14:00:28 -0700262 if (rc == 1 || state->swap_type > BOOT_SWAP_TYPE_REVERT) {
263 state->swap_type = BOOT_SWAP_TYPE_NONE;
David Vinczee2453472019-06-17 12:31:59 +0200264 state->image_num = 0;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700265 }
266
Christopher Collins2adef702019-05-22 14:37:31 -0700267 off = boot_copy_done_off(fap);
268 rc = flash_area_read_is_empty(fap, off, &state->copy_done,
269 sizeof state->copy_done);
270 if (rc < 0) {
271 return BOOT_EFLASH;
272 }
273 if (rc == 1) {
274 state->copy_done = BOOT_FLAG_UNSET;
275 } else {
276 state->copy_done = boot_flag_decode(state->copy_done);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800277 }
278
279 off = boot_image_ok_off(fap);
David Vincze2d736ad2019-02-18 11:50:22 +0100280 rc = flash_area_read_is_empty(fap, off, &state->image_ok,
281 sizeof state->image_ok);
Fabio Utzig178be542018-09-19 08:12:56 -0300282 if (rc < 0) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800283 return BOOT_EFLASH;
284 }
Fabio Utzig178be542018-09-19 08:12:56 -0300285 if (rc == 1) {
286 state->image_ok = BOOT_FLAG_UNSET;
287 } else {
288 state->image_ok = boot_flag_decode(state->image_ok);
289 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800290
291 return 0;
292}
293
294/**
295 * Reads the image trailer from the scratch area.
296 */
297int
Fabio Utzig2473ac02017-05-02 12:45:02 -0300298boot_read_swap_state_by_id(int flash_area_id, struct boot_swap_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800299{
300 const struct flash_area *fap;
301 int rc;
302
Fabio Utzigb0f04732019-07-31 09:49:19 -0300303 rc = flash_area_open(flash_area_id, &fap);
304 if (rc != 0) {
305 return BOOT_EFLASH;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300306 }
307
308 rc = boot_read_swap_state(fap, state);
Fabio Utzigacfba2e2017-05-22 11:06:29 -0400309 flash_area_close(fap);
310 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800311}
312
Fabio Utzigdf0cc502019-08-09 09:10:41 -0300313/**
314 * This functions tries to locate the status area after an aborted swap,
315 * by looking for the magic in the possible locations.
316 *
317 * If the magic is sucessfully found, a flash_area * is returned and it
318 * is the responsibility of the called to close it.
319 *
320 * @returns 0 on success, -1 on errors
321 */
322static int
323boot_find_status(int image_index, const struct flash_area **fap)
324{
325 uint32_t magic[BOOT_MAGIC_ARR_SZ];
326 uint32_t off;
327 uint8_t areas[2] = {
328 FLASH_AREA_IMAGE_PRIMARY(image_index),
329 FLASH_AREA_IMAGE_SCRATCH,
330 };
331 unsigned int i;
332 int rc;
333
334 /*
335 * In the middle a swap, tries to locate the area that is currently
336 * storing a valid magic, first on the primary slot, then on scratch.
337 * Both "slots" can end up being temporary storage for a swap and it
338 * is assumed that if magic is valid then other metadata is too,
339 * because magic is always written in the last step.
340 */
341
342 for (i = 0; i < sizeof(areas) / sizeof(areas[0]); i++) {
343 rc = flash_area_open(areas[i], fap);
344 if (rc != 0) {
345 return rc;
346 }
347
348 off = boot_magic_off(*fap);
349 rc = flash_area_read(*fap, off, magic, BOOT_MAGIC_SZ);
350 if (rc != 0) {
351 flash_area_close(*fap);
352 return rc;
353 }
354
355 if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0) {
356 return 0;
357 }
358
359 flash_area_close(*fap);
360 }
361
362 /* If we got here, no magic was found */
363 return -1;
364}
365
Christopher Collins92ea77f2016-12-12 15:59:26 -0800366int
Fabio Utzigb0f04732019-07-31 09:49:19 -0300367boot_read_swap_size(int image_index, uint32_t *swap_size)
Fabio Utzig46490722017-09-04 15:34:32 -0300368{
Fabio Utzig46490722017-09-04 15:34:32 -0300369 uint32_t off;
370 const struct flash_area *fap;
371 int rc;
372
Fabio Utzigdf0cc502019-08-09 09:10:41 -0300373 rc = boot_find_status(image_index, &fap);
374 if (rc == 0) {
375 off = boot_swap_size_off(fap);
376 rc = flash_area_read(fap, off, swap_size, sizeof *swap_size);
Fabio Utzig46490722017-09-04 15:34:32 -0300377 flash_area_close(fap);
Fabio Utzig46490722017-09-04 15:34:32 -0300378 }
379
Fabio Utzig46490722017-09-04 15:34:32 -0300380 return rc;
381}
382
Fabio Utzigdf0cc502019-08-09 09:10:41 -0300383
Fabio Utzigba829042018-09-18 08:29:34 -0300384#ifdef MCUBOOT_ENC_IMAGES
385int
Fabio Utzigb0f04732019-07-31 09:49:19 -0300386boot_read_enc_key(int image_index, uint8_t slot, uint8_t *enckey)
Fabio Utzigba829042018-09-18 08:29:34 -0300387{
Fabio Utzigba829042018-09-18 08:29:34 -0300388 uint32_t off;
389 const struct flash_area *fap;
390 int rc;
391
Fabio Utzigdf0cc502019-08-09 09:10:41 -0300392 rc = boot_find_status(image_index, &fap);
393 if (rc == 0) {
394 off = boot_enc_key_off(fap, slot);
395 rc = flash_area_read(fap, off, enckey, BOOT_ENC_KEY_SIZE);
Fabio Utzigba829042018-09-18 08:29:34 -0300396 flash_area_close(fap);
Fabio Utzigba829042018-09-18 08:29:34 -0300397 }
398
Fabio Utzigba829042018-09-18 08:29:34 -0300399 return rc;
400}
401#endif
Fabio Utzig46490722017-09-04 15:34:32 -0300402
403int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800404boot_write_magic(const struct flash_area *fap)
405{
406 uint32_t off;
407 int rc;
408
409 off = boot_magic_off(fap);
410
Christopher Collins2c88e692019-05-22 15:10:14 -0700411 BOOT_LOG_DBG("writing magic; fa_id=%d off=0x%x (0x%x)",
412 fap->fa_id, off, fap->fa_off + off);
Fabio Utzig24a273d2017-04-20 08:21:31 -0300413 rc = flash_area_write(fap, off, boot_img_magic, BOOT_MAGIC_SZ);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800414 if (rc != 0) {
415 return BOOT_EFLASH;
416 }
417
418 return 0;
419}
420
Fabio Utzig2473ac02017-05-02 12:45:02 -0300421static int
Christopher Collinsa1c12042019-05-23 14:00:28 -0700422boot_write_trailer_byte(const struct flash_area *fap, uint32_t off,
423 uint8_t val)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800424{
Fabio Utzig644b8d42017-04-20 07:56:05 -0300425 uint8_t buf[BOOT_MAX_ALIGN];
David Brown9d725462017-01-23 15:50:58 -0700426 uint8_t align;
Fabio Utzig39000012018-07-30 12:40:20 -0300427 uint8_t erased_val;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700428 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800429
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200430 align = flash_area_align(fap);
Fabio Utzig644b8d42017-04-20 07:56:05 -0300431 assert(align <= BOOT_MAX_ALIGN);
Fabio Utzig39000012018-07-30 12:40:20 -0300432 erased_val = flash_area_erased_val(fap);
433 memset(buf, erased_val, BOOT_MAX_ALIGN);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700434 buf[0] = val;
David Brown9d725462017-01-23 15:50:58 -0700435
436 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800437 if (rc != 0) {
438 return BOOT_EFLASH;
439 }
440
441 return 0;
442}
443
444int
Fabio Utzig2473ac02017-05-02 12:45:02 -0300445boot_write_copy_done(const struct flash_area *fap)
446{
Christopher Collinsa1c12042019-05-23 14:00:28 -0700447 uint32_t off;
448
449 off = boot_copy_done_off(fap);
450 BOOT_LOG_DBG("writing copy_done; fa_id=%d off=0x%x (0x%x)",
451 fap->fa_id, off, fap->fa_off + off);
452 return boot_write_trailer_byte(fap, off, BOOT_FLAG_SET);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300453}
454
455int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800456boot_write_image_ok(const struct flash_area *fap)
457{
Christopher Collinsa1c12042019-05-23 14:00:28 -0700458 uint32_t off;
459
460 off = boot_image_ok_off(fap);
461 BOOT_LOG_DBG("writing image_ok; fa_id=%d off=0x%x (0x%x)",
462 fap->fa_id, off, fap->fa_off + off);
463 return boot_write_trailer_byte(fap, off, BOOT_FLAG_SET);
464}
465
466/**
467 * Writes the specified value to the `swap-type` field of an image trailer.
468 * This value is persisted so that the boot loader knows what swap operation to
469 * resume in case of an unexpected reset.
470 */
471int
David Vinczee2453472019-06-17 12:31:59 +0200472boot_write_swap_info(const struct flash_area *fap, uint8_t swap_type,
473 uint8_t image_num)
Christopher Collinsa1c12042019-05-23 14:00:28 -0700474{
475 uint32_t off;
David Vinczee2453472019-06-17 12:31:59 +0200476 uint8_t swap_info;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700477
David Vinczee2453472019-06-17 12:31:59 +0200478 BOOT_SET_SWAP_INFO(swap_info, image_num, swap_type);
479 off = boot_swap_info_off(fap);
480 BOOT_LOG_DBG("writing swap_info; fa_id=%d off=0x%x (0x%x), swap_type=0x%x"
481 " image_num=0x%x",
482 fap->fa_id, off, fap->fa_off + off, swap_type, image_num);
483 return boot_write_trailer_byte(fap, off, swap_info);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800484}
485
486int
Fabio Utzig46490722017-09-04 15:34:32 -0300487boot_write_swap_size(const struct flash_area *fap, uint32_t swap_size)
488{
489 uint32_t off;
490 int rc;
491 uint8_t buf[BOOT_MAX_ALIGN];
492 uint8_t align;
Fabio Utzig39000012018-07-30 12:40:20 -0300493 uint8_t erased_val;
Fabio Utzig46490722017-09-04 15:34:32 -0300494
495 off = boot_swap_size_off(fap);
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200496 align = flash_area_align(fap);
Fabio Utzig46490722017-09-04 15:34:32 -0300497 assert(align <= BOOT_MAX_ALIGN);
498 if (align < sizeof swap_size) {
499 align = sizeof swap_size;
500 }
Fabio Utzig39000012018-07-30 12:40:20 -0300501 erased_val = flash_area_erased_val(fap);
502 memset(buf, erased_val, BOOT_MAX_ALIGN);
Fabio Utzig46490722017-09-04 15:34:32 -0300503 memcpy(buf, (uint8_t *)&swap_size, sizeof swap_size);
504
Christopher Collins2c88e692019-05-22 15:10:14 -0700505 BOOT_LOG_DBG("writing swap_size; fa_id=%d off=0x%x (0x%x)",
506 fap->fa_id, off, fap->fa_off + off);
507
Fabio Utzig46490722017-09-04 15:34:32 -0300508 rc = flash_area_write(fap, off, buf, align);
509 if (rc != 0) {
510 return BOOT_EFLASH;
511 }
512
513 return 0;
514}
515
Fabio Utzigba829042018-09-18 08:29:34 -0300516#ifdef MCUBOOT_ENC_IMAGES
517int
518boot_write_enc_key(const struct flash_area *fap, uint8_t slot, const uint8_t *enckey)
519{
520 uint32_t off;
521 int rc;
522
523 off = boot_enc_key_off(fap, slot);
524 rc = flash_area_write(fap, off, enckey, BOOT_ENC_KEY_SIZE);
525 if (rc != 0) {
526 return BOOT_EFLASH;
527 }
528
529 return 0;
530}
531#endif
532
Fabio Utzig46490722017-09-04 15:34:32 -0300533int
Fabio Utzigb0f04732019-07-31 09:49:19 -0300534boot_swap_type_multi(int image_index)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800535{
536 const struct boot_swap_table *table;
David Vincze2d736ad2019-02-18 11:50:22 +0100537 struct boot_swap_state primary_slot;
538 struct boot_swap_state secondary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800539 int rc;
Fabio Utzigcd5774b2017-11-29 10:18:26 -0200540 size_t i;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800541
Fabio Utzigb0f04732019-07-31 09:49:19 -0300542 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(image_index),
543 &primary_slot);
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300544 if (rc) {
545 return BOOT_SWAP_TYPE_PANIC;
546 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800547
Fabio Utzigb0f04732019-07-31 09:49:19 -0300548 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(image_index),
David Vincze2d736ad2019-02-18 11:50:22 +0100549 &secondary_slot);
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300550 if (rc) {
551 return BOOT_SWAP_TYPE_PANIC;
552 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800553
554 for (i = 0; i < BOOT_SWAP_TABLES_COUNT; i++) {
555 table = boot_swap_tables + i;
556
Christopher Collinsa1c12042019-05-23 14:00:28 -0700557 if (boot_magic_compatible_check(table->magic_primary_slot,
558 primary_slot.magic) &&
559 boot_magic_compatible_check(table->magic_secondary_slot,
560 secondary_slot.magic) &&
David Vincze2d736ad2019-02-18 11:50:22 +0100561 (table->image_ok_primary_slot == BOOT_FLAG_ANY ||
562 table->image_ok_primary_slot == primary_slot.image_ok) &&
563 (table->image_ok_secondary_slot == BOOT_FLAG_ANY ||
564 table->image_ok_secondary_slot == secondary_slot.image_ok) &&
565 (table->copy_done_primary_slot == BOOT_FLAG_ANY ||
566 table->copy_done_primary_slot == primary_slot.copy_done)) {
Fabio Utzig34e393e2017-05-22 11:07:07 -0400567 BOOT_LOG_INF("Swap type: %s",
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300568 table->swap_type == BOOT_SWAP_TYPE_TEST ? "test" :
569 table->swap_type == BOOT_SWAP_TYPE_PERM ? "perm" :
570 table->swap_type == BOOT_SWAP_TYPE_REVERT ? "revert" :
571 "BUG; can't happen");
572 assert(table->swap_type == BOOT_SWAP_TYPE_TEST ||
573 table->swap_type == BOOT_SWAP_TYPE_PERM ||
574 table->swap_type == BOOT_SWAP_TYPE_REVERT);
575 return table->swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800576 }
577 }
578
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300579 BOOT_LOG_INF("Swap type: none");
Christopher Collins92ea77f2016-12-12 15:59:26 -0800580 return BOOT_SWAP_TYPE_NONE;
581}
582
Fabio Utzigb0f04732019-07-31 09:49:19 -0300583int
584boot_swap_type(void)
585{
586 return boot_swap_type_multi(0);
587}
588
589
Christopher Collins92ea77f2016-12-12 15:59:26 -0800590/**
David Vincze2d736ad2019-02-18 11:50:22 +0100591 * Marks the image in the secondary slot as pending. On the next reboot,
592 * the system will perform a one-time boot of the the secondary slot image.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800593 *
Christopher Collins7835c1e2016-12-21 10:10:51 -0800594 * @param permanent Whether the image should be used permanently or
595 * only tested once:
596 * 0=run image once, then confirm or revert.
597 * 1=run image forever.
598 *
Christopher Collins92ea77f2016-12-12 15:59:26 -0800599 * @return 0 on success; nonzero on failure.
600 */
601int
Christopher Collins7835c1e2016-12-21 10:10:51 -0800602boot_set_pending(int permanent)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800603{
604 const struct flash_area *fap;
David Vincze2d736ad2019-02-18 11:50:22 +0100605 struct boot_swap_state state_secondary_slot;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700606 uint8_t swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800607 int rc;
608
Fabio Utzigb0f04732019-07-31 09:49:19 -0300609 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(0),
David Vincze2d736ad2019-02-18 11:50:22 +0100610 &state_secondary_slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800611 if (rc != 0) {
612 return rc;
613 }
614
David Vincze2d736ad2019-02-18 11:50:22 +0100615 switch (state_secondary_slot.magic) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800616 case BOOT_MAGIC_GOOD:
617 /* Swap already scheduled. */
618 return 0;
619
620 case BOOT_MAGIC_UNSET:
Fabio Utzigb0f04732019-07-31 09:49:19 -0300621 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(0), &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800622 if (rc != 0) {
623 rc = BOOT_EFLASH;
624 } else {
625 rc = boot_write_magic(fap);
626 }
627
Christopher Collins7835c1e2016-12-21 10:10:51 -0800628 if (rc == 0 && permanent) {
629 rc = boot_write_image_ok(fap);
630 }
631
Christopher Collinsa1c12042019-05-23 14:00:28 -0700632 if (rc == 0) {
633 if (permanent) {
634 swap_type = BOOT_SWAP_TYPE_PERM;
635 } else {
636 swap_type = BOOT_SWAP_TYPE_TEST;
637 }
David Vinczee2453472019-06-17 12:31:59 +0200638 rc = boot_write_swap_info(fap, swap_type, 0);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700639 }
640
Christopher Collins92ea77f2016-12-12 15:59:26 -0800641 flash_area_close(fap);
642 return rc;
643
Christopher Collinsae01f152019-01-30 09:56:37 -0800644 case BOOT_MAGIC_BAD:
645 /* The image slot is corrupt. There is no way to recover, so erase the
646 * slot to allow future upgrades.
647 */
Fabio Utzigb0f04732019-07-31 09:49:19 -0300648 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(0), &fap);
Christopher Collinsae01f152019-01-30 09:56:37 -0800649 if (rc != 0) {
650 return BOOT_EFLASH;
651 }
652
653 flash_area_erase(fap, 0, fap->fa_size);
654 flash_area_close(fap);
655 return BOOT_EBADIMAGE;
656
Christopher Collins92ea77f2016-12-12 15:59:26 -0800657 default:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800658 assert(0);
Christopher Collinsae01f152019-01-30 09:56:37 -0800659 return BOOT_EBADIMAGE;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800660 }
661}
662
663/**
David Vincze2d736ad2019-02-18 11:50:22 +0100664 * Marks the image in the primary slot as confirmed. The system will continue
665 * booting into the image in the primary slot until told to boot from a
666 * different slot.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800667 *
668 * @return 0 on success; nonzero on failure.
669 */
670int
671boot_set_confirmed(void)
672{
673 const struct flash_area *fap;
David Vincze2d736ad2019-02-18 11:50:22 +0100674 struct boot_swap_state state_primary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800675 int rc;
676
Fabio Utzigb0f04732019-07-31 09:49:19 -0300677 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(0),
David Vincze2d736ad2019-02-18 11:50:22 +0100678 &state_primary_slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800679 if (rc != 0) {
680 return rc;
681 }
682
David Vincze2d736ad2019-02-18 11:50:22 +0100683 switch (state_primary_slot.magic) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800684 case BOOT_MAGIC_GOOD:
685 /* Confirm needed; proceed. */
686 break;
687
688 case BOOT_MAGIC_UNSET:
689 /* Already confirmed. */
690 return 0;
691
692 case BOOT_MAGIC_BAD:
693 /* Unexpected state. */
694 return BOOT_EBADVECT;
695 }
696
Fabio Utzigb0f04732019-07-31 09:49:19 -0300697 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(0), &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800698 if (rc) {
699 rc = BOOT_EFLASH;
700 goto done;
701 }
702
David Vincze2d736ad2019-02-18 11:50:22 +0100703 if (state_primary_slot.copy_done == BOOT_FLAG_UNSET) {
Fabio Utzig39000012018-07-30 12:40:20 -0300704 /* Swap never completed. This is unexpected. */
705 rc = BOOT_EBADVECT;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800706 goto done;
707 }
708
David Vincze2d736ad2019-02-18 11:50:22 +0100709 if (state_primary_slot.image_ok != BOOT_FLAG_UNSET) {
Fabio Utzig39000012018-07-30 12:40:20 -0300710 /* Already confirmed. */
711 goto done;
712 }
713
714 rc = boot_write_image_ok(fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800715
716done:
717 flash_area_close(fap);
718 return rc;
719}
David Vinczee32483f2019-06-13 10:46:24 +0200720
721#if (BOOT_IMAGE_NUMBER > 1)
722/**
723 * Check if the version of the image is not older than required.
724 *
725 * @param req Required minimal image version.
726 * @param ver Version of the image to be checked.
727 *
728 * @return 0 if the version is sufficient, nonzero otherwise.
729 */
730int
731boot_is_version_sufficient(struct image_version *req,
732 struct image_version *ver)
733{
734 if (ver->iv_major > req->iv_major) {
735 return 0;
736 }
737 if (ver->iv_major < req->iv_major) {
738 return BOOT_EBADVERSION;
739 }
740 /* The major version numbers are equal. */
741 if (ver->iv_minor > req->iv_minor) {
742 return 0;
743 }
744 if (ver->iv_minor < req->iv_minor) {
745 return BOOT_EBADVERSION;
746 }
747 /* The minor version numbers are equal. */
748 if (ver->iv_revision < req->iv_revision) {
749 return BOOT_EBADVERSION;
750 }
751
752 return 0;
753}
754#endif /* BOOT_IMAGE_NUMBER > 1 */