blob: 5afe3057e1d875a0ff3596d64f960fc5c78a55ce [file] [log] [blame]
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +01001/*
2 * 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-2020 Arm Limited
7 * Copyright (c) 2020 Nordic Semiconductor ASA
8 *
9 * Original license:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one
12 * or more contributor license agreements. See the NOTICE file
13 * distributed with this work for additional information
14 * regarding copyright ownership. The ASF licenses this file
15 * to you under the Apache License, Version 2.0 (the
16 * "License"); you may not use this file except in compliance
17 * with the License. You may obtain a copy of the License at
18 *
19 * http://www.apache.org/licenses/LICENSE-2.0
20 *
21 * Unless required by applicable law or agreed to in writing,
22 * software distributed under the License is distributed on an
23 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
24 * KIND, either express or implied. See the License for the
25 * specific language governing permissions and limitations
26 * under the License.
27 */
28
Andrzej Puzdrowski14ef5762020-12-11 17:17:59 +010029/**
30 * @file
31 * @brief Public MCUBoot interface API implementation
32 *
33 * This file contains API implementation which can be combined with
34 * the application in order to interact with the MCUBoot bootloader.
35 * This file contains shared code-base betwen MCUBoot and the application
36 * which controls DFU process.
37 */
38
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +010039#include <string.h>
40#include <inttypes.h>
41#include <stddef.h>
42
43#include "sysflash/sysflash.h"
44#include "flash_map_backend/flash_map_backend.h"
45
46#include "bootutil/image.h"
47#include "bootutil/bootutil_public.h"
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +010048#include "bootutil/bootutil_log.h"
49#ifdef MCUBOOT_ENC_IMAGES
Andrzej Puzdrowski360763d2021-02-04 18:01:01 +010050#include "bootutil/enc_key_public.h"
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +010051#endif
52
53#ifdef CONFIG_MCUBOOT
54MCUBOOT_LOG_MODULE_DECLARE(mcuboot);
55#else
56MCUBOOT_LOG_MODULE_REGISTER(mcuboot_util);
57#endif
58
59const uint32_t boot_img_magic[] = {
60 0xf395c277,
61 0x7fefd260,
62 0x0f505235,
63 0x8079b62c,
64};
65
66#define BOOT_MAGIC_ARR_SZ \
67 (sizeof boot_img_magic / sizeof boot_img_magic[0])
68
69struct boot_swap_table {
70 uint8_t magic_primary_slot;
71 uint8_t magic_secondary_slot;
72 uint8_t image_ok_primary_slot;
73 uint8_t image_ok_secondary_slot;
74 uint8_t copy_done_primary_slot;
75
76 uint8_t swap_type;
77};
78
79/**
80 * This set of tables maps image trailer contents to swap operation type.
81 * When searching for a match, these tables must be iterated sequentially.
82 *
83 * NOTE: the table order is very important. The settings in the secondary
84 * slot always are priority to the primary slot and should be located
85 * earlier in the table.
86 *
87 * The table lists only states where there is action needs to be taken by
88 * the bootloader, as in starting/finishing a swap operation.
89 */
90static const struct boot_swap_table boot_swap_tables[] = {
91 {
92 .magic_primary_slot = BOOT_MAGIC_ANY,
93 .magic_secondary_slot = BOOT_MAGIC_GOOD,
94 .image_ok_primary_slot = BOOT_FLAG_ANY,
95 .image_ok_secondary_slot = BOOT_FLAG_UNSET,
96 .copy_done_primary_slot = BOOT_FLAG_ANY,
97 .swap_type = BOOT_SWAP_TYPE_TEST,
98 },
99 {
100 .magic_primary_slot = BOOT_MAGIC_ANY,
101 .magic_secondary_slot = BOOT_MAGIC_GOOD,
102 .image_ok_primary_slot = BOOT_FLAG_ANY,
103 .image_ok_secondary_slot = BOOT_FLAG_SET,
104 .copy_done_primary_slot = BOOT_FLAG_ANY,
105 .swap_type = BOOT_SWAP_TYPE_PERM,
106 },
107 {
108 .magic_primary_slot = BOOT_MAGIC_GOOD,
109 .magic_secondary_slot = BOOT_MAGIC_UNSET,
110 .image_ok_primary_slot = BOOT_FLAG_UNSET,
111 .image_ok_secondary_slot = BOOT_FLAG_ANY,
112 .copy_done_primary_slot = BOOT_FLAG_SET,
113 .swap_type = BOOT_SWAP_TYPE_REVERT,
114 },
115};
116
117#define BOOT_SWAP_TABLES_COUNT \
118 (sizeof boot_swap_tables / sizeof boot_swap_tables[0])
119
120static int
121boot_magic_decode(const uint32_t *magic)
122{
123 if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0) {
124 return BOOT_MAGIC_GOOD;
125 }
126 return BOOT_MAGIC_BAD;
127}
128
129static int
130boot_flag_decode(uint8_t flag)
131{
132 if (flag != BOOT_FLAG_SET) {
133 return BOOT_FLAG_BAD;
134 }
135 return BOOT_FLAG_SET;
136}
137
138static inline uint32_t
139boot_magic_off(const struct flash_area *fap)
140{
141 return fap->fa_size - BOOT_MAGIC_SZ;
142}
143
144static inline uint32_t
145boot_image_ok_off(const struct flash_area *fap)
146{
147 return boot_magic_off(fap) - BOOT_MAX_ALIGN;
148}
149
150static inline uint32_t
151boot_copy_done_off(const struct flash_area *fap)
152{
153 return boot_image_ok_off(fap) - BOOT_MAX_ALIGN;
154}
155
156static inline uint32_t
157boot_swap_size_off(const struct flash_area *fap)
158{
159 return boot_swap_info_off(fap) - BOOT_MAX_ALIGN;
160}
161
162uint32_t
163boot_swap_info_off(const struct flash_area *fap)
164{
165 return boot_copy_done_off(fap) - BOOT_MAX_ALIGN;
166}
167
168/**
169 * Determines if a status source table is satisfied by the specified magic
170 * code.
171 *
172 * @param tbl_val A magic field from a status source table.
173 * @param val The magic value in a trailer, encoded as a
174 * BOOT_MAGIC_[...].
175 *
176 * @return 1 if the two values are compatible;
177 * 0 otherwise.
178 */
179int
180boot_magic_compatible_check(uint8_t tbl_val, uint8_t val)
181{
182 switch (tbl_val) {
183 case BOOT_MAGIC_ANY:
184 return 1;
185
186 case BOOT_MAGIC_NOTGOOD:
187 return val != BOOT_MAGIC_GOOD;
188
189 default:
190 return tbl_val == val;
191 }
192}
193
194#ifdef MCUBOOT_ENC_IMAGES
195static inline uint32_t
196boot_enc_key_off(const struct flash_area *fap, uint8_t slot)
197{
198#if MCUBOOT_SWAP_SAVE_ENCTLV
199 return boot_swap_size_off(fap) - ((slot + 1) *
200 ((((BOOT_ENC_TLV_SIZE - 1) / BOOT_MAX_ALIGN) + 1) * BOOT_MAX_ALIGN));
201#else
202 return boot_swap_size_off(fap) - ((slot + 1) * BOOT_ENC_KEY_SIZE);
203#endif
204}
205#endif
206
207bool bootutil_buffer_is_erased(const struct flash_area *area,
208 const void *buffer, size_t len)
209{
210 size_t i;
211 uint8_t *u8b;
212 uint8_t erased_val;
213
214 if (buffer == NULL || len == 0) {
215 return false;
216 }
217
218 erased_val = flash_area_erased_val(area);
219 for (i = 0, u8b = (uint8_t *)buffer; i < len; i++) {
220 if (u8b[i] != erased_val) {
221 return false;
222 }
223 }
224
225 return true;
226}
227
Andrzej Puzdrowski4700b802020-12-09 14:55:36 +0100228static int
229boot_read_flag(const struct flash_area *fap, uint8_t *flag, uint32_t off)
230{
231 int rc;
232
233 rc = flash_area_read(fap, off, flag, sizeof *flag);
234 if (rc < 0) {
235 return BOOT_EFLASH;
236 }
237 if (bootutil_buffer_is_erased(fap, flag, sizeof *flag)) {
238 *flag = BOOT_FLAG_UNSET;
239 } else {
240 *flag = boot_flag_decode(*flag);
241 }
242
243 return 0;
244}
245
246static inline int
247boot_read_copy_done(const struct flash_area *fap, uint8_t *copy_done)
248{
249 return boot_read_flag(fap, copy_done, boot_copy_done_off(fap));
250}
251
252
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100253int
254boot_read_swap_state(const struct flash_area *fap,
255 struct boot_swap_state *state)
256{
257 uint32_t magic[BOOT_MAGIC_ARR_SZ];
258 uint32_t off;
259 uint8_t swap_info;
260 int rc;
261
262 off = boot_magic_off(fap);
263 rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ);
264 if (rc < 0) {
265 return BOOT_EFLASH;
266 }
267 if (bootutil_buffer_is_erased(fap, magic, BOOT_MAGIC_SZ)) {
268 state->magic = BOOT_MAGIC_UNSET;
269 } else {
270 state->magic = boot_magic_decode(magic);
271 }
272
273 off = boot_swap_info_off(fap);
274 rc = flash_area_read(fap, off, &swap_info, sizeof swap_info);
275 if (rc < 0) {
276 return BOOT_EFLASH;
277 }
278
279 /* Extract the swap type and image number */
280 state->swap_type = BOOT_GET_SWAP_TYPE(swap_info);
281 state->image_num = BOOT_GET_IMAGE_NUM(swap_info);
282
283 if (bootutil_buffer_is_erased(fap, &swap_info, sizeof swap_info) ||
284 state->swap_type > BOOT_SWAP_TYPE_REVERT) {
285 state->swap_type = BOOT_SWAP_TYPE_NONE;
286 state->image_num = 0;
287 }
288
Andrzej Puzdrowski4700b802020-12-09 14:55:36 +0100289 rc = boot_read_copy_done(fap, &state->copy_done);
290 if (rc) {
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100291 return BOOT_EFLASH;
292 }
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100293
Andrzej Puzdrowski4700b802020-12-09 14:55:36 +0100294 return boot_read_image_ok(fap, &state->image_ok);
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100295}
296
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100297int
298boot_read_swap_state_by_id(int flash_area_id, struct boot_swap_state *state)
299{
300 const struct flash_area *fap;
301 int rc;
302
303 rc = flash_area_open(flash_area_id, &fap);
304 if (rc != 0) {
305 return BOOT_EFLASH;
306 }
307
308 rc = boot_read_swap_state(fap, state);
309 flash_area_close(fap);
310 return rc;
311}
312
313int
314boot_write_magic(const struct flash_area *fap)
315{
316 uint32_t off;
317 int rc;
318
319 off = boot_magic_off(fap);
320
321 BOOT_LOG_DBG("writing magic; fa_id=%d off=0x%lx (0x%lx)",
322 fap->fa_id, (unsigned long)off,
323 (unsigned long)(fap->fa_off + off));
324 rc = flash_area_write(fap, off, boot_img_magic, BOOT_MAGIC_SZ);
325 if (rc != 0) {
326 return BOOT_EFLASH;
327 }
328
329 return 0;
330}
331
332/**
333 * Write trailer data; status bytes, swap_size, etc
334 *
335 * @returns 0 on success, != 0 on error.
336 */
337static int
338boot_write_trailer(const struct flash_area *fap, uint32_t off,
339 const uint8_t *inbuf, uint8_t inlen)
340{
341 uint8_t buf[BOOT_MAX_ALIGN];
342 uint8_t align;
343 uint8_t erased_val;
344 int rc;
345
346 align = flash_area_align(fap);
347 if (inlen > BOOT_MAX_ALIGN || align > BOOT_MAX_ALIGN) {
348 return -1;
349 }
350 erased_val = flash_area_erased_val(fap);
351 if (align < inlen) {
352 align = inlen;
353 }
354 memcpy(buf, inbuf, inlen);
355 memset(&buf[inlen], erased_val, align - inlen);
356
357 rc = flash_area_write(fap, off, buf, align);
358 if (rc != 0) {
359 return BOOT_EFLASH;
360 }
361
362 return 0;
363}
364
365static int
366boot_write_trailer_flag(const struct flash_area *fap, uint32_t off,
367 uint8_t flag_val)
368{
369 const uint8_t buf[1] = { flag_val };
370 return boot_write_trailer(fap, off, buf, 1);
371}
372
373int
374boot_write_image_ok(const struct flash_area *fap)
375{
376 uint32_t off;
377
378 off = boot_image_ok_off(fap);
379 BOOT_LOG_DBG("writing image_ok; fa_id=%d off=0x%lx (0x%lx)",
380 fap->fa_id, (unsigned long)off,
381 (unsigned long)(fap->fa_off + off));
382 return boot_write_trailer_flag(fap, off, BOOT_FLAG_SET);
383}
384
Andrzej Puzdrowski4700b802020-12-09 14:55:36 +0100385int
386boot_read_image_ok(const struct flash_area *fap, uint8_t *image_ok)
387{
388 return boot_read_flag(fap, image_ok, boot_image_ok_off(fap));
389}
390
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100391/**
392 * Writes the specified value to the `swap-type` field of an image trailer.
393 * This value is persisted so that the boot loader knows what swap operation to
394 * resume in case of an unexpected reset.
395 */
396int
397boot_write_swap_info(const struct flash_area *fap, uint8_t swap_type,
398 uint8_t image_num)
399{
400 uint32_t off;
401 uint8_t swap_info;
402
403 BOOT_SET_SWAP_INFO(swap_info, image_num, swap_type);
404 off = boot_swap_info_off(fap);
405 BOOT_LOG_DBG("writing swap_info; fa_id=%d off=0x%lx (0x%lx), swap_type=0x%x"
406 " image_num=0x%x",
407 fap->fa_id, (unsigned long)off,
408 (unsigned long)(fap->fa_off + off), swap_type, image_num);
409 return boot_write_trailer(fap, off, (const uint8_t *) &swap_info, 1);
410}
411
412int
413boot_swap_type_multi(int image_index)
414{
415 const struct boot_swap_table *table;
416 struct boot_swap_state primary_slot;
417 struct boot_swap_state secondary_slot;
418 int rc;
419 size_t i;
420
421 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(image_index),
422 &primary_slot);
423 if (rc) {
424 return BOOT_SWAP_TYPE_PANIC;
425 }
426
427 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(image_index),
428 &secondary_slot);
429 if (rc) {
430 return BOOT_SWAP_TYPE_PANIC;
431 }
432
433 for (i = 0; i < BOOT_SWAP_TABLES_COUNT; i++) {
434 table = boot_swap_tables + i;
435
436 if (boot_magic_compatible_check(table->magic_primary_slot,
437 primary_slot.magic) &&
438 boot_magic_compatible_check(table->magic_secondary_slot,
439 secondary_slot.magic) &&
440 (table->image_ok_primary_slot == BOOT_FLAG_ANY ||
441 table->image_ok_primary_slot == primary_slot.image_ok) &&
442 (table->image_ok_secondary_slot == BOOT_FLAG_ANY ||
443 table->image_ok_secondary_slot == secondary_slot.image_ok) &&
444 (table->copy_done_primary_slot == BOOT_FLAG_ANY ||
445 table->copy_done_primary_slot == primary_slot.copy_done)) {
446 BOOT_LOG_INF("Swap type: %s",
447 table->swap_type == BOOT_SWAP_TYPE_TEST ? "test" :
448 table->swap_type == BOOT_SWAP_TYPE_PERM ? "perm" :
449 table->swap_type == BOOT_SWAP_TYPE_REVERT ? "revert" :
450 "BUG; can't happen");
451 if (table->swap_type != BOOT_SWAP_TYPE_TEST &&
452 table->swap_type != BOOT_SWAP_TYPE_PERM &&
453 table->swap_type != BOOT_SWAP_TYPE_REVERT) {
454 return BOOT_SWAP_TYPE_PANIC;
455 }
456 return table->swap_type;
457 }
458 }
459
460 BOOT_LOG_INF("Swap type: none");
461 return BOOT_SWAP_TYPE_NONE;
462}
463
464/*
465 * This function is not used by the bootloader itself, but its required API
466 * by external tooling like mcumgr.
467 */
468int
469boot_swap_type(void)
470{
471 return boot_swap_type_multi(0);
472}
473
474/**
475 * Marks the image in the secondary slot as pending. On the next reboot,
476 * the system will perform a one-time boot of the the secondary slot image.
477 *
478 * @param permanent Whether the image should be used permanently or
479 * only tested once:
480 * 0=run image once, then confirm or revert.
481 * 1=run image forever.
482 *
483 * @return 0 on success; nonzero on failure.
484 */
485int
486boot_set_pending(int permanent)
487{
488 const struct flash_area *fap;
489 struct boot_swap_state state_secondary_slot;
490 uint8_t swap_type;
491 int rc;
492
493 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(0),
494 &state_secondary_slot);
495 if (rc != 0) {
496 return rc;
497 }
498
499 switch (state_secondary_slot.magic) {
500 case BOOT_MAGIC_GOOD:
501 /* Swap already scheduled. */
502 return 0;
503
504 case BOOT_MAGIC_UNSET:
505 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(0), &fap);
506 if (rc != 0) {
507 rc = BOOT_EFLASH;
508 } else {
509 rc = boot_write_magic(fap);
510 }
511
512 if (rc == 0 && permanent) {
513 rc = boot_write_image_ok(fap);
514 }
515
516 if (rc == 0) {
517 if (permanent) {
518 swap_type = BOOT_SWAP_TYPE_PERM;
519 } else {
520 swap_type = BOOT_SWAP_TYPE_TEST;
521 }
522 rc = boot_write_swap_info(fap, swap_type, 0);
523 }
524
525 flash_area_close(fap);
526 return rc;
527
528 case BOOT_MAGIC_BAD:
529 /* The image slot is corrupt. There is no way to recover, so erase the
530 * slot to allow future upgrades.
531 */
532 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(0), &fap);
533 if (rc != 0) {
534 return BOOT_EFLASH;
535 }
536
537 flash_area_erase(fap, 0, fap->fa_size);
538 flash_area_close(fap);
539 return BOOT_EBADIMAGE;
540
541 default:
542 assert(0);
543 return BOOT_EBADIMAGE;
544 }
545}
546
547/**
548 * Marks the image in the primary slot as confirmed. The system will continue
549 * booting into the image in the primary slot until told to boot from a
550 * different slot.
551 *
552 * @return 0 on success; nonzero on failure.
553 */
554int
555boot_set_confirmed(void)
556{
557 const struct flash_area *fap;
558 struct boot_swap_state state_primary_slot;
559 int rc;
560
561 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(0),
562 &state_primary_slot);
563 if (rc != 0) {
564 return rc;
565 }
566
567 switch (state_primary_slot.magic) {
568 case BOOT_MAGIC_GOOD:
569 /* Confirm needed; proceed. */
570 break;
571
572 case BOOT_MAGIC_UNSET:
573 /* Already confirmed. */
574 return 0;
575
576 case BOOT_MAGIC_BAD:
577 /* Unexpected state. */
578 return BOOT_EBADVECT;
579 }
580
581 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(0), &fap);
582 if (rc) {
583 rc = BOOT_EFLASH;
584 goto done;
585 }
586
587 if (state_primary_slot.copy_done == BOOT_FLAG_UNSET) {
588 /* Swap never completed. This is unexpected. */
589 rc = BOOT_EBADVECT;
590 goto done;
591 }
592
593 if (state_primary_slot.image_ok != BOOT_FLAG_UNSET) {
594 /* Already confirmed. */
595 goto done;
596 }
597
598 rc = boot_write_image_ok(fap);
599
600done:
601 flash_area_close(fap);
602 return rc;
603}