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