blob: 793c4e37b3cae7be9b45b3357c6cdc0bafdce341 [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 */
Dominik Ermela7f9e9f2021-03-24 17:31:57 +0000337int
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100338boot_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);
Dominik Ermel48281622021-03-24 18:04:54 +0000347 align = (inlen + align - 1) & ~(align - 1);
348 if (align > BOOT_MAX_ALIGN) {
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100349 return -1;
350 }
351 erased_val = flash_area_erased_val(fap);
Dominik Ermel48281622021-03-24 18:04:54 +0000352
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100353 memcpy(buf, inbuf, inlen);
354 memset(&buf[inlen], erased_val, align - inlen);
355
356 rc = flash_area_write(fap, off, buf, align);
357 if (rc != 0) {
358 return BOOT_EFLASH;
359 }
360
361 return 0;
362}
363
Dominik Ermela7f9e9f2021-03-24 17:31:57 +0000364int
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100365boot_write_trailer_flag(const struct flash_area *fap, uint32_t off,
366 uint8_t flag_val)
367{
368 const uint8_t buf[1] = { flag_val };
369 return boot_write_trailer(fap, off, buf, 1);
370}
371
372int
373boot_write_image_ok(const struct flash_area *fap)
374{
375 uint32_t off;
376
377 off = boot_image_ok_off(fap);
378 BOOT_LOG_DBG("writing image_ok; fa_id=%d off=0x%lx (0x%lx)",
379 fap->fa_id, (unsigned long)off,
380 (unsigned long)(fap->fa_off + off));
381 return boot_write_trailer_flag(fap, off, BOOT_FLAG_SET);
382}
383
Andrzej Puzdrowski4700b802020-12-09 14:55:36 +0100384int
385boot_read_image_ok(const struct flash_area *fap, uint8_t *image_ok)
386{
387 return boot_read_flag(fap, image_ok, boot_image_ok_off(fap));
388}
389
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100390/**
391 * Writes the specified value to the `swap-type` field of an image trailer.
392 * This value is persisted so that the boot loader knows what swap operation to
393 * resume in case of an unexpected reset.
394 */
395int
396boot_write_swap_info(const struct flash_area *fap, uint8_t swap_type,
397 uint8_t image_num)
398{
399 uint32_t off;
400 uint8_t swap_info;
401
402 BOOT_SET_SWAP_INFO(swap_info, image_num, swap_type);
403 off = boot_swap_info_off(fap);
404 BOOT_LOG_DBG("writing swap_info; fa_id=%d off=0x%lx (0x%lx), swap_type=0x%x"
405 " image_num=0x%x",
406 fap->fa_id, (unsigned long)off,
407 (unsigned long)(fap->fa_off + off), swap_type, image_num);
408 return boot_write_trailer(fap, off, (const uint8_t *) &swap_info, 1);
409}
410
411int
412boot_swap_type_multi(int image_index)
413{
414 const struct boot_swap_table *table;
415 struct boot_swap_state primary_slot;
416 struct boot_swap_state secondary_slot;
417 int rc;
418 size_t i;
419
420 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(image_index),
421 &primary_slot);
422 if (rc) {
423 return BOOT_SWAP_TYPE_PANIC;
424 }
425
426 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(image_index),
427 &secondary_slot);
428 if (rc) {
429 return BOOT_SWAP_TYPE_PANIC;
430 }
431
432 for (i = 0; i < BOOT_SWAP_TABLES_COUNT; i++) {
433 table = boot_swap_tables + i;
434
435 if (boot_magic_compatible_check(table->magic_primary_slot,
436 primary_slot.magic) &&
437 boot_magic_compatible_check(table->magic_secondary_slot,
438 secondary_slot.magic) &&
439 (table->image_ok_primary_slot == BOOT_FLAG_ANY ||
440 table->image_ok_primary_slot == primary_slot.image_ok) &&
441 (table->image_ok_secondary_slot == BOOT_FLAG_ANY ||
442 table->image_ok_secondary_slot == secondary_slot.image_ok) &&
443 (table->copy_done_primary_slot == BOOT_FLAG_ANY ||
444 table->copy_done_primary_slot == primary_slot.copy_done)) {
445 BOOT_LOG_INF("Swap type: %s",
446 table->swap_type == BOOT_SWAP_TYPE_TEST ? "test" :
447 table->swap_type == BOOT_SWAP_TYPE_PERM ? "perm" :
448 table->swap_type == BOOT_SWAP_TYPE_REVERT ? "revert" :
449 "BUG; can't happen");
450 if (table->swap_type != BOOT_SWAP_TYPE_TEST &&
451 table->swap_type != BOOT_SWAP_TYPE_PERM &&
452 table->swap_type != BOOT_SWAP_TYPE_REVERT) {
453 return BOOT_SWAP_TYPE_PANIC;
454 }
455 return table->swap_type;
456 }
457 }
458
459 BOOT_LOG_INF("Swap type: none");
460 return BOOT_SWAP_TYPE_NONE;
461}
462
463/*
464 * This function is not used by the bootloader itself, but its required API
465 * by external tooling like mcumgr.
466 */
467int
468boot_swap_type(void)
469{
470 return boot_swap_type_multi(0);
471}
472
473/**
474 * Marks the image in the secondary slot as pending. On the next reboot,
475 * the system will perform a one-time boot of the the secondary slot image.
476 *
477 * @param permanent Whether the image should be used permanently or
478 * only tested once:
479 * 0=run image once, then confirm or revert.
480 * 1=run image forever.
481 *
482 * @return 0 on success; nonzero on failure.
483 */
484int
485boot_set_pending(int permanent)
486{
487 const struct flash_area *fap;
488 struct boot_swap_state state_secondary_slot;
489 uint8_t swap_type;
490 int rc;
491
492 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(0),
493 &state_secondary_slot);
494 if (rc != 0) {
495 return rc;
496 }
497
498 switch (state_secondary_slot.magic) {
499 case BOOT_MAGIC_GOOD:
500 /* Swap already scheduled. */
501 return 0;
502
503 case BOOT_MAGIC_UNSET:
504 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(0), &fap);
505 if (rc != 0) {
506 rc = BOOT_EFLASH;
507 } else {
508 rc = boot_write_magic(fap);
509 }
510
511 if (rc == 0 && permanent) {
512 rc = boot_write_image_ok(fap);
513 }
514
515 if (rc == 0) {
516 if (permanent) {
517 swap_type = BOOT_SWAP_TYPE_PERM;
518 } else {
519 swap_type = BOOT_SWAP_TYPE_TEST;
520 }
521 rc = boot_write_swap_info(fap, swap_type, 0);
522 }
523
524 flash_area_close(fap);
525 return rc;
526
527 case BOOT_MAGIC_BAD:
528 /* The image slot is corrupt. There is no way to recover, so erase the
529 * slot to allow future upgrades.
530 */
531 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(0), &fap);
532 if (rc != 0) {
533 return BOOT_EFLASH;
534 }
535
536 flash_area_erase(fap, 0, fap->fa_size);
537 flash_area_close(fap);
538 return BOOT_EBADIMAGE;
539
540 default:
541 assert(0);
542 return BOOT_EBADIMAGE;
543 }
544}
545
546/**
547 * Marks the image in the primary slot as confirmed. The system will continue
548 * booting into the image in the primary slot until told to boot from a
549 * different slot.
550 *
551 * @return 0 on success; nonzero on failure.
552 */
553int
554boot_set_confirmed(void)
555{
556 const struct flash_area *fap;
557 struct boot_swap_state state_primary_slot;
558 int rc;
559
560 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(0),
561 &state_primary_slot);
562 if (rc != 0) {
563 return rc;
564 }
565
566 switch (state_primary_slot.magic) {
567 case BOOT_MAGIC_GOOD:
568 /* Confirm needed; proceed. */
569 break;
570
571 case BOOT_MAGIC_UNSET:
572 /* Already confirmed. */
573 return 0;
574
575 case BOOT_MAGIC_BAD:
576 /* Unexpected state. */
577 return BOOT_EBADVECT;
578 }
579
580 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(0), &fap);
581 if (rc) {
582 rc = BOOT_EFLASH;
583 goto done;
584 }
585
586 if (state_primary_slot.copy_done == BOOT_FLAG_UNSET) {
587 /* Swap never completed. This is unexpected. */
588 rc = BOOT_EBADVECT;
589 goto done;
590 }
591
592 if (state_primary_slot.image_ok != BOOT_FLAG_UNSET) {
593 /* Already confirmed. */
594 goto done;
595 }
596
597 rc = boot_write_image_ok(fap);
598
599done:
600 flash_area_close(fap);
601 return rc;
602}