blob: 6ce09d06c79ee1ae82d906e52b40e89b10811a98 [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
Sherry Zhangfbeef9b2021-05-12 15:42:30 +08006 * Copyright (c) 2019-2021 Arm Limited
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +01007 * 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
Andrzej Puzdrowskidea293a2021-07-09 12:31:35 +020053#include "bootutil/boot_public_hooks.h"
Gustavo Henrique Niheicf120ba2021-11-22 18:44:11 -030054#include "bootutil_priv.h"
Andrzej Puzdrowskidea293a2021-07-09 12:31:35 +020055
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +010056#ifdef CONFIG_MCUBOOT
Carlos Falgueras Garcíaa4b4b0f2021-06-22 10:00:22 +020057BOOT_LOG_MODULE_DECLARE(mcuboot);
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +010058#else
Carlos Falgueras Garcíaa4b4b0f2021-06-22 10:00:22 +020059BOOT_LOG_MODULE_REGISTER(mcuboot_util);
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +010060#endif
61
Gustavo Henrique Niheicf120ba2021-11-22 18:44:11 -030062#if BOOT_MAX_ALIGN == 8
63const union boot_img_magic_t boot_img_magic = {
64 .val = {
65 0x77, 0xc2, 0x95, 0xf3,
66 0x60, 0xd2, 0xef, 0x7f,
67 0x35, 0x52, 0x50, 0x0f,
68 0x2c, 0xb6, 0x79, 0x80
69 }
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +010070};
Gustavo Henrique Niheicf120ba2021-11-22 18:44:11 -030071#else
72const union boot_img_magic_t boot_img_magic = {
73 .align = BOOT_MAX_ALIGN,
74 .magic = {
75 0x2d, 0xe1,
76 0x5d, 0x29, 0x41, 0x0b,
77 0x8d, 0x77, 0x67, 0x9c,
78 0x11, 0x0f, 0x1f, 0x8a
79 }
80};
81#endif
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +010082
83struct boot_swap_table {
84 uint8_t magic_primary_slot;
85 uint8_t magic_secondary_slot;
86 uint8_t image_ok_primary_slot;
87 uint8_t image_ok_secondary_slot;
88 uint8_t copy_done_primary_slot;
89
90 uint8_t swap_type;
91};
92
93/**
94 * This set of tables maps image trailer contents to swap operation type.
95 * When searching for a match, these tables must be iterated sequentially.
96 *
97 * NOTE: the table order is very important. The settings in the secondary
98 * slot always are priority to the primary slot and should be located
99 * earlier in the table.
100 *
101 * The table lists only states where there is action needs to be taken by
102 * the bootloader, as in starting/finishing a swap operation.
103 */
104static const struct boot_swap_table boot_swap_tables[] = {
105 {
106 .magic_primary_slot = BOOT_MAGIC_ANY,
107 .magic_secondary_slot = BOOT_MAGIC_GOOD,
108 .image_ok_primary_slot = BOOT_FLAG_ANY,
109 .image_ok_secondary_slot = BOOT_FLAG_UNSET,
110 .copy_done_primary_slot = BOOT_FLAG_ANY,
111 .swap_type = BOOT_SWAP_TYPE_TEST,
112 },
113 {
114 .magic_primary_slot = BOOT_MAGIC_ANY,
115 .magic_secondary_slot = BOOT_MAGIC_GOOD,
116 .image_ok_primary_slot = BOOT_FLAG_ANY,
117 .image_ok_secondary_slot = BOOT_FLAG_SET,
118 .copy_done_primary_slot = BOOT_FLAG_ANY,
119 .swap_type = BOOT_SWAP_TYPE_PERM,
120 },
121 {
122 .magic_primary_slot = BOOT_MAGIC_GOOD,
123 .magic_secondary_slot = BOOT_MAGIC_UNSET,
124 .image_ok_primary_slot = BOOT_FLAG_UNSET,
125 .image_ok_secondary_slot = BOOT_FLAG_ANY,
126 .copy_done_primary_slot = BOOT_FLAG_SET,
127 .swap_type = BOOT_SWAP_TYPE_REVERT,
128 },
129};
130
131#define BOOT_SWAP_TABLES_COUNT \
132 (sizeof boot_swap_tables / sizeof boot_swap_tables[0])
133
134static int
Gustavo Henrique Niheicf120ba2021-11-22 18:44:11 -0300135boot_magic_decode(const uint8_t *magic)
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100136{
Gustavo Henrique Niheicf120ba2021-11-22 18:44:11 -0300137 if (memcmp(magic, BOOT_IMG_MAGIC, BOOT_MAGIC_SZ) == 0) {
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100138 return BOOT_MAGIC_GOOD;
139 }
140 return BOOT_MAGIC_BAD;
141}
142
143static int
144boot_flag_decode(uint8_t flag)
145{
146 if (flag != BOOT_FLAG_SET) {
147 return BOOT_FLAG_BAD;
148 }
149 return BOOT_FLAG_SET;
150}
151
152static inline uint32_t
153boot_magic_off(const struct flash_area *fap)
154{
Dominik Ermel260ae092021-04-23 05:38:45 +0000155 return flash_area_get_size(fap) - BOOT_MAGIC_SZ;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100156}
157
158static inline uint32_t
159boot_image_ok_off(const struct flash_area *fap)
160{
Kristine Jassmann73c38c62021-02-03 16:56:14 +0000161 return ALIGN_DOWN(boot_magic_off(fap) - BOOT_MAX_ALIGN, BOOT_MAX_ALIGN);
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100162}
163
164static inline uint32_t
165boot_copy_done_off(const struct flash_area *fap)
166{
167 return boot_image_ok_off(fap) - BOOT_MAX_ALIGN;
168}
169
170static inline uint32_t
171boot_swap_size_off(const struct flash_area *fap)
172{
173 return boot_swap_info_off(fap) - BOOT_MAX_ALIGN;
174}
175
176uint32_t
177boot_swap_info_off(const struct flash_area *fap)
178{
179 return boot_copy_done_off(fap) - BOOT_MAX_ALIGN;
180}
181
182/**
183 * Determines if a status source table is satisfied by the specified magic
184 * code.
185 *
186 * @param tbl_val A magic field from a status source table.
187 * @param val The magic value in a trailer, encoded as a
188 * BOOT_MAGIC_[...].
189 *
190 * @return 1 if the two values are compatible;
191 * 0 otherwise.
192 */
193int
194boot_magic_compatible_check(uint8_t tbl_val, uint8_t val)
195{
196 switch (tbl_val) {
197 case BOOT_MAGIC_ANY:
198 return 1;
199
200 case BOOT_MAGIC_NOTGOOD:
201 return val != BOOT_MAGIC_GOOD;
202
203 default:
204 return tbl_val == val;
205 }
206}
207
208#ifdef MCUBOOT_ENC_IMAGES
209static inline uint32_t
210boot_enc_key_off(const struct flash_area *fap, uint8_t slot)
211{
212#if MCUBOOT_SWAP_SAVE_ENCTLV
Kristine Jassmann73c38c62021-02-03 16:56:14 +0000213 return boot_swap_size_off(fap) - ((slot + 1) * BOOT_ENC_TLV_ALIGN_SIZE);
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100214#else
Kristine Jassmann73c38c62021-02-03 16:56:14 +0000215 return boot_swap_size_off(fap) - ((slot + 1) * BOOT_ENC_KEY_ALIGN_SIZE);
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100216#endif
217}
218#endif
219
220bool bootutil_buffer_is_erased(const struct flash_area *area,
221 const void *buffer, size_t len)
222{
223 size_t i;
224 uint8_t *u8b;
225 uint8_t erased_val;
226
227 if (buffer == NULL || len == 0) {
228 return false;
229 }
230
231 erased_val = flash_area_erased_val(area);
232 for (i = 0, u8b = (uint8_t *)buffer; i < len; i++) {
233 if (u8b[i] != erased_val) {
234 return false;
235 }
236 }
237
238 return true;
239}
240
Andrzej Puzdrowski4700b802020-12-09 14:55:36 +0100241static int
242boot_read_flag(const struct flash_area *fap, uint8_t *flag, uint32_t off)
243{
244 int rc;
245
246 rc = flash_area_read(fap, off, flag, sizeof *flag);
247 if (rc < 0) {
248 return BOOT_EFLASH;
249 }
250 if (bootutil_buffer_is_erased(fap, flag, sizeof *flag)) {
251 *flag = BOOT_FLAG_UNSET;
252 } else {
253 *flag = boot_flag_decode(*flag);
254 }
255
256 return 0;
257}
258
259static inline int
260boot_read_copy_done(const struct flash_area *fap, uint8_t *copy_done)
261{
262 return boot_read_flag(fap, copy_done, boot_copy_done_off(fap));
263}
264
265
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100266int
267boot_read_swap_state(const struct flash_area *fap,
268 struct boot_swap_state *state)
269{
Gustavo Henrique Niheicf120ba2021-11-22 18:44:11 -0300270 uint8_t magic[BOOT_MAGIC_SZ];
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100271 uint32_t off;
272 uint8_t swap_info;
273 int rc;
274
275 off = boot_magic_off(fap);
276 rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ);
277 if (rc < 0) {
278 return BOOT_EFLASH;
279 }
280 if (bootutil_buffer_is_erased(fap, magic, BOOT_MAGIC_SZ)) {
281 state->magic = BOOT_MAGIC_UNSET;
282 } else {
283 state->magic = boot_magic_decode(magic);
284 }
285
286 off = boot_swap_info_off(fap);
287 rc = flash_area_read(fap, off, &swap_info, sizeof swap_info);
288 if (rc < 0) {
289 return BOOT_EFLASH;
290 }
291
292 /* Extract the swap type and image number */
293 state->swap_type = BOOT_GET_SWAP_TYPE(swap_info);
294 state->image_num = BOOT_GET_IMAGE_NUM(swap_info);
295
296 if (bootutil_buffer_is_erased(fap, &swap_info, sizeof swap_info) ||
297 state->swap_type > BOOT_SWAP_TYPE_REVERT) {
298 state->swap_type = BOOT_SWAP_TYPE_NONE;
299 state->image_num = 0;
300 }
301
Andrzej Puzdrowski4700b802020-12-09 14:55:36 +0100302 rc = boot_read_copy_done(fap, &state->copy_done);
303 if (rc) {
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100304 return BOOT_EFLASH;
305 }
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100306
Andrzej Puzdrowski4700b802020-12-09 14:55:36 +0100307 return boot_read_image_ok(fap, &state->image_ok);
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100308}
309
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100310int
311boot_read_swap_state_by_id(int flash_area_id, struct boot_swap_state *state)
312{
313 const struct flash_area *fap;
314 int rc;
315
316 rc = flash_area_open(flash_area_id, &fap);
317 if (rc != 0) {
318 return BOOT_EFLASH;
319 }
320
321 rc = boot_read_swap_state(fap, state);
322 flash_area_close(fap);
323 return rc;
324}
325
326int
327boot_write_magic(const struct flash_area *fap)
328{
329 uint32_t off;
Kristine Jassmann73c38c62021-02-03 16:56:14 +0000330 uint32_t pad_off;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100331 int rc;
Kristine Jassmann73c38c62021-02-03 16:56:14 +0000332 uint8_t magic[BOOT_MAGIC_ALIGN_SIZE];
333 uint8_t erased_val;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100334
335 off = boot_magic_off(fap);
336
Kristine Jassmann73c38c62021-02-03 16:56:14 +0000337 /* image_trailer structure was modified with additional padding such that
338 * the pad+magic ends up in a flash minimum write region. The address
339 * returned by boot_magic_off() is the start of magic which is not the
340 * start of the flash write boundary and thus writes to the magic will fail.
341 * To account for this change, write to magic is first padded with 0xFF
342 * before writing to the trailer.
343 */
344 pad_off = ALIGN_DOWN(off, BOOT_MAX_ALIGN);
345
346 erased_val = flash_area_erased_val(fap);
347
348 memset(&magic[0], erased_val, sizeof(magic));
Gustavo Henrique Niheicf120ba2021-11-22 18:44:11 -0300349 memcpy(&magic[BOOT_MAGIC_ALIGN_SIZE - BOOT_MAGIC_SZ], BOOT_IMG_MAGIC, BOOT_MAGIC_SZ);
Kristine Jassmann73c38c62021-02-03 16:56:14 +0000350
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100351 BOOT_LOG_DBG("writing magic; fa_id=%d off=0x%lx (0x%lx)",
Dominik Ermel260ae092021-04-23 05:38:45 +0000352 flash_area_get_id(fap), (unsigned long)off,
353 (unsigned long)(flash_area_get_off(fap) + off));
Kristine Jassmann73c38c62021-02-03 16:56:14 +0000354 rc = flash_area_write(fap, pad_off, &magic[0], BOOT_MAGIC_ALIGN_SIZE);
355
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100356 if (rc != 0) {
357 return BOOT_EFLASH;
358 }
359
360 return 0;
361}
362
363/**
364 * Write trailer data; status bytes, swap_size, etc
365 *
366 * @returns 0 on success, != 0 on error.
367 */
Dominik Ermela7f9e9f2021-03-24 17:31:57 +0000368int
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100369boot_write_trailer(const struct flash_area *fap, uint32_t off,
370 const uint8_t *inbuf, uint8_t inlen)
371{
372 uint8_t buf[BOOT_MAX_ALIGN];
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100373 uint8_t erased_val;
Gustavo Henrique Nihei4aa286d2021-11-24 14:54:56 -0300374 uint32_t align;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100375 int rc;
376
377 align = flash_area_align(fap);
Kristine Jassmann73c38c62021-02-03 16:56:14 +0000378 align = ALIGN_UP(inlen, align);
Dominik Ermel48281622021-03-24 18:04:54 +0000379 if (align > BOOT_MAX_ALIGN) {
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100380 return -1;
381 }
382 erased_val = flash_area_erased_val(fap);
Dominik Ermel48281622021-03-24 18:04:54 +0000383
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100384 memcpy(buf, inbuf, inlen);
385 memset(&buf[inlen], erased_val, align - inlen);
386
387 rc = flash_area_write(fap, off, buf, align);
388 if (rc != 0) {
389 return BOOT_EFLASH;
390 }
391
392 return 0;
393}
394
Dominik Ermela7f9e9f2021-03-24 17:31:57 +0000395int
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100396boot_write_trailer_flag(const struct flash_area *fap, uint32_t off,
397 uint8_t flag_val)
398{
399 const uint8_t buf[1] = { flag_val };
400 return boot_write_trailer(fap, off, buf, 1);
401}
402
403int
404boot_write_image_ok(const struct flash_area *fap)
405{
406 uint32_t off;
407
408 off = boot_image_ok_off(fap);
409 BOOT_LOG_DBG("writing image_ok; fa_id=%d off=0x%lx (0x%lx)",
Dominik Ermel260ae092021-04-23 05:38:45 +0000410 flash_area_get_id(fap), (unsigned long)off,
411 (unsigned long)(flash_area_get_off(fap) + off));
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100412 return boot_write_trailer_flag(fap, off, BOOT_FLAG_SET);
413}
414
Andrzej Puzdrowski4700b802020-12-09 14:55:36 +0100415int
416boot_read_image_ok(const struct flash_area *fap, uint8_t *image_ok)
417{
418 return boot_read_flag(fap, image_ok, boot_image_ok_off(fap));
419}
420
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100421/**
422 * Writes the specified value to the `swap-type` field of an image trailer.
423 * This value is persisted so that the boot loader knows what swap operation to
424 * resume in case of an unexpected reset.
425 */
426int
427boot_write_swap_info(const struct flash_area *fap, uint8_t swap_type,
428 uint8_t image_num)
429{
430 uint32_t off;
431 uint8_t swap_info;
432
433 BOOT_SET_SWAP_INFO(swap_info, image_num, swap_type);
434 off = boot_swap_info_off(fap);
435 BOOT_LOG_DBG("writing swap_info; fa_id=%d off=0x%lx (0x%lx), swap_type=0x%x"
436 " image_num=0x%x",
Dominik Ermel260ae092021-04-23 05:38:45 +0000437 flash_area_get_id(fap), (unsigned long)off,
438 (unsigned long)(flash_area_get_off(fap) + off),
439 swap_type, image_num);
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100440 return boot_write_trailer(fap, off, (const uint8_t *) &swap_info, 1);
441}
442
443int
444boot_swap_type_multi(int image_index)
445{
446 const struct boot_swap_table *table;
447 struct boot_swap_state primary_slot;
448 struct boot_swap_state secondary_slot;
449 int rc;
450 size_t i;
451
Andrzej Puzdrowskidea293a2021-07-09 12:31:35 +0200452 rc = BOOT_HOOK_CALL(boot_read_swap_state_primary_slot_hook,
453 BOOT_HOOK_REGULAR, image_index, &primary_slot);
454 if (rc == BOOT_HOOK_REGULAR)
455 {
456 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(image_index),
457 &primary_slot);
458 }
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100459 if (rc) {
460 return BOOT_SWAP_TYPE_PANIC;
461 }
462
463 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(image_index),
464 &secondary_slot);
Andrzej Puzdrowski85da97f2021-06-24 16:39:31 +0200465 if (rc == BOOT_EFLASH) {
466 BOOT_LOG_INF("Secondary image of image pair (%d.) "
467 "is unreachable. Treat it as empty", image_index);
468 secondary_slot.magic = BOOT_MAGIC_UNSET;
469 secondary_slot.swap_type = BOOT_SWAP_TYPE_NONE;
470 secondary_slot.copy_done = BOOT_FLAG_UNSET;
471 secondary_slot.image_ok = BOOT_FLAG_UNSET;
472 secondary_slot.image_num = 0;
473 } else if (rc) {
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100474 return BOOT_SWAP_TYPE_PANIC;
475 }
476
477 for (i = 0; i < BOOT_SWAP_TABLES_COUNT; i++) {
478 table = boot_swap_tables + i;
479
480 if (boot_magic_compatible_check(table->magic_primary_slot,
481 primary_slot.magic) &&
482 boot_magic_compatible_check(table->magic_secondary_slot,
483 secondary_slot.magic) &&
484 (table->image_ok_primary_slot == BOOT_FLAG_ANY ||
485 table->image_ok_primary_slot == primary_slot.image_ok) &&
486 (table->image_ok_secondary_slot == BOOT_FLAG_ANY ||
487 table->image_ok_secondary_slot == secondary_slot.image_ok) &&
488 (table->copy_done_primary_slot == BOOT_FLAG_ANY ||
489 table->copy_done_primary_slot == primary_slot.copy_done)) {
490 BOOT_LOG_INF("Swap type: %s",
491 table->swap_type == BOOT_SWAP_TYPE_TEST ? "test" :
492 table->swap_type == BOOT_SWAP_TYPE_PERM ? "perm" :
493 table->swap_type == BOOT_SWAP_TYPE_REVERT ? "revert" :
494 "BUG; can't happen");
495 if (table->swap_type != BOOT_SWAP_TYPE_TEST &&
496 table->swap_type != BOOT_SWAP_TYPE_PERM &&
497 table->swap_type != BOOT_SWAP_TYPE_REVERT) {
498 return BOOT_SWAP_TYPE_PANIC;
499 }
500 return table->swap_type;
501 }
502 }
503
504 BOOT_LOG_INF("Swap type: none");
505 return BOOT_SWAP_TYPE_NONE;
506}
507
508/*
509 * This function is not used by the bootloader itself, but its required API
510 * by external tooling like mcumgr.
511 */
512int
513boot_swap_type(void)
514{
515 return boot_swap_type_multi(0);
516}
517
518/**
Sherry Zhangfbeef9b2021-05-12 15:42:30 +0800519 * Marks the image with the given index in the secondary slot as pending. On the
520 * next reboot, the system will perform a one-time boot of the the secondary
521 * slot image.
522 *
523 * @param image_index Image pair index.
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100524 *
525 * @param permanent Whether the image should be used permanently or
Sherry Zhangfbeef9b2021-05-12 15:42:30 +0800526 * only tested once:
527 * 0=run image once, then confirm or revert.
528 * 1=run image forever.
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100529 *
530 * @return 0 on success; nonzero on failure.
531 */
532int
Sherry Zhangfbeef9b2021-05-12 15:42:30 +0800533boot_set_pending_multi(int image_index, int permanent)
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100534{
535 const struct flash_area *fap;
536 struct boot_swap_state state_secondary_slot;
537 uint8_t swap_type;
538 int rc;
539
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000540 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index), &fap);
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100541 if (rc != 0) {
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000542 return BOOT_EFLASH;
543 }
544
545 rc = boot_read_swap_state(fap, &state_secondary_slot);
546 if (rc != 0) {
547 goto done;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100548 }
549
550 switch (state_secondary_slot.magic) {
551 case BOOT_MAGIC_GOOD:
552 /* Swap already scheduled. */
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000553 break;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100554
555 case BOOT_MAGIC_UNSET:
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000556 rc = boot_write_magic(fap);
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100557
558 if (rc == 0 && permanent) {
559 rc = boot_write_image_ok(fap);
560 }
561
562 if (rc == 0) {
563 if (permanent) {
564 swap_type = BOOT_SWAP_TYPE_PERM;
565 } else {
566 swap_type = BOOT_SWAP_TYPE_TEST;
567 }
568 rc = boot_write_swap_info(fap, swap_type, 0);
569 }
570
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000571 break;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100572
573 case BOOT_MAGIC_BAD:
574 /* The image slot is corrupt. There is no way to recover, so erase the
575 * slot to allow future upgrades.
576 */
Dominik Ermel260ae092021-04-23 05:38:45 +0000577 flash_area_erase(fap, 0, flash_area_get_size(fap));
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000578 rc = BOOT_EBADIMAGE;
579 break;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100580
581 default:
582 assert(0);
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000583 rc = BOOT_EBADIMAGE;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100584 }
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000585
586done:
587 flash_area_close(fap);
588 return rc;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100589}
590
591/**
Sherry Zhangfbeef9b2021-05-12 15:42:30 +0800592 * Marks the image with index 0 in the secondary slot as pending. On the next
593 * reboot, the system will perform a one-time boot of the the secondary slot
594 * image. Note that this API is kept for compatibility. The
595 * boot_set_pending_multi() API is recommended.
596 *
597 * @param permanent Whether the image should be used permanently or
598 * only tested once:
599 * 0=run image once, then confirm or revert.
600 * 1=run image forever.
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100601 *
602 * @return 0 on success; nonzero on failure.
603 */
604int
Sherry Zhangfbeef9b2021-05-12 15:42:30 +0800605boot_set_pending(int permanent)
606{
607 return boot_set_pending_multi(0, permanent);
608}
609
610/**
611 * Marks the image with the given index in the primary slot as confirmed. The
612 * system will continue booting into the image in the primary slot until told to
613 * boot from a different slot.
614 *
615 * @param image_index Image pair index.
616 *
617 * @return 0 on success; nonzero on failure.
618 */
619int
620boot_set_confirmed_multi(int image_index)
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100621{
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000622 const struct flash_area *fap = NULL;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100623 struct boot_swap_state state_primary_slot;
624 int rc;
625
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000626 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index), &fap);
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100627 if (rc != 0) {
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000628 return BOOT_EFLASH;
629 }
630
631 rc = boot_read_swap_state(fap, &state_primary_slot);
632 if (rc != 0) {
633 goto done;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100634 }
635
636 switch (state_primary_slot.magic) {
637 case BOOT_MAGIC_GOOD:
638 /* Confirm needed; proceed. */
639 break;
640
641 case BOOT_MAGIC_UNSET:
642 /* Already confirmed. */
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000643 goto done;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100644
645 case BOOT_MAGIC_BAD:
646 /* Unexpected state. */
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000647 rc = BOOT_EBADVECT;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100648 goto done;
649 }
650
Andrzej Puzdrowski22b856b2021-05-07 12:14:31 +0200651 /* Intentionally do not check copy_done flag
652 * so can confirm a padded image which was programed using a programing
653 * interface.
654 */
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100655
656 if (state_primary_slot.image_ok != BOOT_FLAG_UNSET) {
657 /* Already confirmed. */
658 goto done;
659 }
660
661 rc = boot_write_image_ok(fap);
662
663done:
664 flash_area_close(fap);
665 return rc;
666}
Sherry Zhangfbeef9b2021-05-12 15:42:30 +0800667
668/**
669 * Marks the image with index 0 in the primary slot as confirmed. The system
670 * will continue booting into the image in the primary slot until told to boot
671 * from a different slot. Note that this API is kept for compatibility. The
672 * boot_set_confirmed_multi() API is recommended.
673 *
674 * @return 0 on success; nonzero on failure.
675 */
676int
677boot_set_confirmed(void)
678{
679 return boot_set_confirmed_multi(0);
680}