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