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