blob: c567892dabdb57002404dc660591fb08830427ae [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"
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +010049
Andrzej Puzdrowskidea293a2021-07-09 12:31:35 +020050#include "bootutil/boot_public_hooks.h"
Gustavo Henrique Niheicf120ba2021-11-22 18:44:11 -030051#include "bootutil_priv.h"
Andrzej Puzdrowskidea293a2021-07-09 12:31:35 +020052
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +010053#ifdef CONFIG_MCUBOOT
Carlos Falgueras Garcíaa4b4b0f2021-06-22 10:00:22 +020054BOOT_LOG_MODULE_DECLARE(mcuboot);
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +010055#else
Carlos Falgueras Garcíaa4b4b0f2021-06-22 10:00:22 +020056BOOT_LOG_MODULE_REGISTER(mcuboot_util);
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +010057#endif
58
Gustavo Henrique Niheicf120ba2021-11-22 18:44:11 -030059#if BOOT_MAX_ALIGN == 8
60const union boot_img_magic_t boot_img_magic = {
61 .val = {
62 0x77, 0xc2, 0x95, 0xf3,
63 0x60, 0xd2, 0xef, 0x7f,
64 0x35, 0x52, 0x50, 0x0f,
65 0x2c, 0xb6, 0x79, 0x80
66 }
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +010067};
Gustavo Henrique Niheicf120ba2021-11-22 18:44:11 -030068#else
69const union boot_img_magic_t boot_img_magic = {
70 .align = BOOT_MAX_ALIGN,
71 .magic = {
72 0x2d, 0xe1,
73 0x5d, 0x29, 0x41, 0x0b,
74 0x8d, 0x77, 0x67, 0x9c,
75 0x11, 0x0f, 0x1f, 0x8a
76 }
77};
78#endif
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +010079
80struct boot_swap_table {
81 uint8_t magic_primary_slot;
82 uint8_t magic_secondary_slot;
83 uint8_t image_ok_primary_slot;
84 uint8_t image_ok_secondary_slot;
85 uint8_t copy_done_primary_slot;
86
87 uint8_t swap_type;
88};
89
90/**
91 * This set of tables maps image trailer contents to swap operation type.
92 * When searching for a match, these tables must be iterated sequentially.
93 *
94 * NOTE: the table order is very important. The settings in the secondary
95 * slot always are priority to the primary slot and should be located
96 * earlier in the table.
97 *
98 * The table lists only states where there is action needs to be taken by
99 * the bootloader, as in starting/finishing a swap operation.
100 */
101static const struct boot_swap_table boot_swap_tables[] = {
102 {
103 .magic_primary_slot = BOOT_MAGIC_ANY,
104 .magic_secondary_slot = BOOT_MAGIC_GOOD,
105 .image_ok_primary_slot = BOOT_FLAG_ANY,
106 .image_ok_secondary_slot = BOOT_FLAG_UNSET,
107 .copy_done_primary_slot = BOOT_FLAG_ANY,
108 .swap_type = BOOT_SWAP_TYPE_TEST,
109 },
110 {
111 .magic_primary_slot = BOOT_MAGIC_ANY,
112 .magic_secondary_slot = BOOT_MAGIC_GOOD,
113 .image_ok_primary_slot = BOOT_FLAG_ANY,
114 .image_ok_secondary_slot = BOOT_FLAG_SET,
115 .copy_done_primary_slot = BOOT_FLAG_ANY,
116 .swap_type = BOOT_SWAP_TYPE_PERM,
117 },
118 {
119 .magic_primary_slot = BOOT_MAGIC_GOOD,
120 .magic_secondary_slot = BOOT_MAGIC_UNSET,
121 .image_ok_primary_slot = BOOT_FLAG_UNSET,
122 .image_ok_secondary_slot = BOOT_FLAG_ANY,
123 .copy_done_primary_slot = BOOT_FLAG_SET,
124 .swap_type = BOOT_SWAP_TYPE_REVERT,
125 },
126};
127
128#define BOOT_SWAP_TABLES_COUNT \
129 (sizeof boot_swap_tables / sizeof boot_swap_tables[0])
130
131static int
Gustavo Henrique Niheicf120ba2021-11-22 18:44:11 -0300132boot_magic_decode(const uint8_t *magic)
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100133{
Gustavo Henrique Niheicf120ba2021-11-22 18:44:11 -0300134 if (memcmp(magic, BOOT_IMG_MAGIC, BOOT_MAGIC_SZ) == 0) {
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100135 return BOOT_MAGIC_GOOD;
136 }
137 return BOOT_MAGIC_BAD;
138}
139
140static int
141boot_flag_decode(uint8_t flag)
142{
143 if (flag != BOOT_FLAG_SET) {
144 return BOOT_FLAG_BAD;
145 }
146 return BOOT_FLAG_SET;
147}
148
149static inline uint32_t
150boot_magic_off(const struct flash_area *fap)
151{
Dominik Ermel260ae092021-04-23 05:38:45 +0000152 return flash_area_get_size(fap) - BOOT_MAGIC_SZ;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100153}
154
155static inline uint32_t
156boot_image_ok_off(const struct flash_area *fap)
157{
Kristine Jassmann73c38c62021-02-03 16:56:14 +0000158 return ALIGN_DOWN(boot_magic_off(fap) - BOOT_MAX_ALIGN, BOOT_MAX_ALIGN);
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100159}
160
161static inline uint32_t
162boot_copy_done_off(const struct flash_area *fap)
163{
164 return boot_image_ok_off(fap) - BOOT_MAX_ALIGN;
165}
166
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100167uint32_t
168boot_swap_info_off(const struct flash_area *fap)
169{
170 return boot_copy_done_off(fap) - BOOT_MAX_ALIGN;
171}
172
173/**
174 * Determines if a status source table is satisfied by the specified magic
175 * code.
176 *
177 * @param tbl_val A magic field from a status source table.
178 * @param val The magic value in a trailer, encoded as a
179 * BOOT_MAGIC_[...].
180 *
181 * @return 1 if the two values are compatible;
182 * 0 otherwise.
183 */
184int
185boot_magic_compatible_check(uint8_t tbl_val, uint8_t val)
186{
187 switch (tbl_val) {
188 case BOOT_MAGIC_ANY:
189 return 1;
190
191 case BOOT_MAGIC_NOTGOOD:
192 return val != BOOT_MAGIC_GOOD;
193
194 default:
195 return tbl_val == val;
196 }
197}
198
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100199bool bootutil_buffer_is_erased(const struct flash_area *area,
200 const void *buffer, size_t len)
201{
202 size_t i;
203 uint8_t *u8b;
204 uint8_t erased_val;
205
206 if (buffer == NULL || len == 0) {
207 return false;
208 }
209
210 erased_val = flash_area_erased_val(area);
211 for (i = 0, u8b = (uint8_t *)buffer; i < len; i++) {
212 if (u8b[i] != erased_val) {
213 return false;
214 }
215 }
216
217 return true;
218}
219
Andrzej Puzdrowski4700b802020-12-09 14:55:36 +0100220static int
221boot_read_flag(const struct flash_area *fap, uint8_t *flag, uint32_t off)
222{
223 int rc;
224
225 rc = flash_area_read(fap, off, flag, sizeof *flag);
226 if (rc < 0) {
227 return BOOT_EFLASH;
228 }
229 if (bootutil_buffer_is_erased(fap, flag, sizeof *flag)) {
230 *flag = BOOT_FLAG_UNSET;
231 } else {
232 *flag = boot_flag_decode(*flag);
233 }
234
235 return 0;
236}
237
238static inline int
239boot_read_copy_done(const struct flash_area *fap, uint8_t *copy_done)
240{
241 return boot_read_flag(fap, copy_done, boot_copy_done_off(fap));
242}
243
244
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100245int
246boot_read_swap_state(const struct flash_area *fap,
247 struct boot_swap_state *state)
248{
Gustavo Henrique Niheicf120ba2021-11-22 18:44:11 -0300249 uint8_t magic[BOOT_MAGIC_SZ];
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100250 uint32_t off;
251 uint8_t swap_info;
252 int rc;
253
254 off = boot_magic_off(fap);
255 rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ);
256 if (rc < 0) {
257 return BOOT_EFLASH;
258 }
259 if (bootutil_buffer_is_erased(fap, magic, BOOT_MAGIC_SZ)) {
260 state->magic = BOOT_MAGIC_UNSET;
261 } else {
262 state->magic = boot_magic_decode(magic);
263 }
264
265 off = boot_swap_info_off(fap);
266 rc = flash_area_read(fap, off, &swap_info, sizeof swap_info);
267 if (rc < 0) {
268 return BOOT_EFLASH;
269 }
270
271 /* Extract the swap type and image number */
272 state->swap_type = BOOT_GET_SWAP_TYPE(swap_info);
273 state->image_num = BOOT_GET_IMAGE_NUM(swap_info);
274
275 if (bootutil_buffer_is_erased(fap, &swap_info, sizeof swap_info) ||
276 state->swap_type > BOOT_SWAP_TYPE_REVERT) {
277 state->swap_type = BOOT_SWAP_TYPE_NONE;
278 state->image_num = 0;
279 }
280
Andrzej Puzdrowski4700b802020-12-09 14:55:36 +0100281 rc = boot_read_copy_done(fap, &state->copy_done);
282 if (rc) {
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100283 return BOOT_EFLASH;
284 }
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100285
Andrzej Puzdrowski4700b802020-12-09 14:55:36 +0100286 return boot_read_image_ok(fap, &state->image_ok);
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100287}
288
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100289int
290boot_read_swap_state_by_id(int flash_area_id, struct boot_swap_state *state)
291{
292 const struct flash_area *fap;
293 int rc;
294
295 rc = flash_area_open(flash_area_id, &fap);
296 if (rc != 0) {
297 return BOOT_EFLASH;
298 }
299
300 rc = boot_read_swap_state(fap, state);
301 flash_area_close(fap);
302 return rc;
303}
304
305int
306boot_write_magic(const struct flash_area *fap)
307{
308 uint32_t off;
Kristine Jassmann73c38c62021-02-03 16:56:14 +0000309 uint32_t pad_off;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100310 int rc;
Kristine Jassmann73c38c62021-02-03 16:56:14 +0000311 uint8_t magic[BOOT_MAGIC_ALIGN_SIZE];
312 uint8_t erased_val;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100313
314 off = boot_magic_off(fap);
315
Kristine Jassmann73c38c62021-02-03 16:56:14 +0000316 /* image_trailer structure was modified with additional padding such that
317 * the pad+magic ends up in a flash minimum write region. The address
318 * returned by boot_magic_off() is the start of magic which is not the
319 * start of the flash write boundary and thus writes to the magic will fail.
320 * To account for this change, write to magic is first padded with 0xFF
321 * before writing to the trailer.
322 */
323 pad_off = ALIGN_DOWN(off, BOOT_MAX_ALIGN);
324
325 erased_val = flash_area_erased_val(fap);
326
327 memset(&magic[0], erased_val, sizeof(magic));
Gustavo Henrique Niheicf120ba2021-11-22 18:44:11 -0300328 memcpy(&magic[BOOT_MAGIC_ALIGN_SIZE - BOOT_MAGIC_SZ], BOOT_IMG_MAGIC, BOOT_MAGIC_SZ);
Kristine Jassmann73c38c62021-02-03 16:56:14 +0000329
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100330 BOOT_LOG_DBG("writing magic; fa_id=%d off=0x%lx (0x%lx)",
Dominik Ermel260ae092021-04-23 05:38:45 +0000331 flash_area_get_id(fap), (unsigned long)off,
332 (unsigned long)(flash_area_get_off(fap) + off));
Kristine Jassmann73c38c62021-02-03 16:56:14 +0000333 rc = flash_area_write(fap, pad_off, &magic[0], BOOT_MAGIC_ALIGN_SIZE);
334
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100335 if (rc != 0) {
336 return BOOT_EFLASH;
337 }
338
339 return 0;
340}
341
342/**
343 * Write trailer data; status bytes, swap_size, etc
344 *
345 * @returns 0 on success, != 0 on error.
346 */
Dominik Ermela7f9e9f2021-03-24 17:31:57 +0000347int
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100348boot_write_trailer(const struct flash_area *fap, uint32_t off,
349 const uint8_t *inbuf, uint8_t inlen)
350{
351 uint8_t buf[BOOT_MAX_ALIGN];
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100352 uint8_t erased_val;
Gustavo Henrique Nihei4aa286d2021-11-24 14:54:56 -0300353 uint32_t align;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100354 int rc;
355
356 align = flash_area_align(fap);
Kristine Jassmann73c38c62021-02-03 16:56:14 +0000357 align = ALIGN_UP(inlen, align);
Dominik Ermel48281622021-03-24 18:04:54 +0000358 if (align > BOOT_MAX_ALIGN) {
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100359 return -1;
360 }
361 erased_val = flash_area_erased_val(fap);
Dominik Ermel48281622021-03-24 18:04:54 +0000362
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100363 memcpy(buf, inbuf, inlen);
364 memset(&buf[inlen], erased_val, align - inlen);
365
366 rc = flash_area_write(fap, off, buf, align);
367 if (rc != 0) {
368 return BOOT_EFLASH;
369 }
370
371 return 0;
372}
373
Dominik Ermela7f9e9f2021-03-24 17:31:57 +0000374int
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100375boot_write_trailer_flag(const struct flash_area *fap, uint32_t off,
376 uint8_t flag_val)
377{
378 const uint8_t buf[1] = { flag_val };
379 return boot_write_trailer(fap, off, buf, 1);
380}
381
382int
383boot_write_image_ok(const struct flash_area *fap)
384{
385 uint32_t off;
386
387 off = boot_image_ok_off(fap);
388 BOOT_LOG_DBG("writing image_ok; fa_id=%d off=0x%lx (0x%lx)",
Dominik Ermel260ae092021-04-23 05:38:45 +0000389 flash_area_get_id(fap), (unsigned long)off,
390 (unsigned long)(flash_area_get_off(fap) + off));
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100391 return boot_write_trailer_flag(fap, off, BOOT_FLAG_SET);
392}
393
Andrzej Puzdrowski4700b802020-12-09 14:55:36 +0100394int
395boot_read_image_ok(const struct flash_area *fap, uint8_t *image_ok)
396{
397 return boot_read_flag(fap, image_ok, boot_image_ok_off(fap));
398}
399
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100400/**
401 * Writes the specified value to the `swap-type` field of an image trailer.
402 * This value is persisted so that the boot loader knows what swap operation to
403 * resume in case of an unexpected reset.
404 */
405int
406boot_write_swap_info(const struct flash_area *fap, uint8_t swap_type,
407 uint8_t image_num)
408{
409 uint32_t off;
410 uint8_t swap_info;
411
412 BOOT_SET_SWAP_INFO(swap_info, image_num, swap_type);
413 off = boot_swap_info_off(fap);
414 BOOT_LOG_DBG("writing swap_info; fa_id=%d off=0x%lx (0x%lx), swap_type=0x%x"
415 " image_num=0x%x",
Dominik Ermel260ae092021-04-23 05:38:45 +0000416 flash_area_get_id(fap), (unsigned long)off,
417 (unsigned long)(flash_area_get_off(fap) + off),
418 swap_type, image_num);
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100419 return boot_write_trailer(fap, off, (const uint8_t *) &swap_info, 1);
420}
421
422int
423boot_swap_type_multi(int image_index)
424{
425 const struct boot_swap_table *table;
426 struct boot_swap_state primary_slot;
427 struct boot_swap_state secondary_slot;
428 int rc;
429 size_t i;
430
Andrzej Puzdrowskidea293a2021-07-09 12:31:35 +0200431 rc = BOOT_HOOK_CALL(boot_read_swap_state_primary_slot_hook,
432 BOOT_HOOK_REGULAR, image_index, &primary_slot);
433 if (rc == BOOT_HOOK_REGULAR)
434 {
435 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(image_index),
436 &primary_slot);
437 }
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100438 if (rc) {
439 return BOOT_SWAP_TYPE_PANIC;
440 }
441
442 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(image_index),
443 &secondary_slot);
Andrzej Puzdrowski85da97f2021-06-24 16:39:31 +0200444 if (rc == BOOT_EFLASH) {
445 BOOT_LOG_INF("Secondary image of image pair (%d.) "
446 "is unreachable. Treat it as empty", image_index);
447 secondary_slot.magic = BOOT_MAGIC_UNSET;
448 secondary_slot.swap_type = BOOT_SWAP_TYPE_NONE;
449 secondary_slot.copy_done = BOOT_FLAG_UNSET;
450 secondary_slot.image_ok = BOOT_FLAG_UNSET;
451 secondary_slot.image_num = 0;
452 } else if (rc) {
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100453 return BOOT_SWAP_TYPE_PANIC;
454 }
455
456 for (i = 0; i < BOOT_SWAP_TABLES_COUNT; i++) {
457 table = boot_swap_tables + i;
458
459 if (boot_magic_compatible_check(table->magic_primary_slot,
460 primary_slot.magic) &&
461 boot_magic_compatible_check(table->magic_secondary_slot,
462 secondary_slot.magic) &&
463 (table->image_ok_primary_slot == BOOT_FLAG_ANY ||
464 table->image_ok_primary_slot == primary_slot.image_ok) &&
465 (table->image_ok_secondary_slot == BOOT_FLAG_ANY ||
466 table->image_ok_secondary_slot == secondary_slot.image_ok) &&
467 (table->copy_done_primary_slot == BOOT_FLAG_ANY ||
468 table->copy_done_primary_slot == primary_slot.copy_done)) {
469 BOOT_LOG_INF("Swap type: %s",
470 table->swap_type == BOOT_SWAP_TYPE_TEST ? "test" :
471 table->swap_type == BOOT_SWAP_TYPE_PERM ? "perm" :
472 table->swap_type == BOOT_SWAP_TYPE_REVERT ? "revert" :
473 "BUG; can't happen");
474 if (table->swap_type != BOOT_SWAP_TYPE_TEST &&
475 table->swap_type != BOOT_SWAP_TYPE_PERM &&
476 table->swap_type != BOOT_SWAP_TYPE_REVERT) {
477 return BOOT_SWAP_TYPE_PANIC;
478 }
479 return table->swap_type;
480 }
481 }
482
483 BOOT_LOG_INF("Swap type: none");
484 return BOOT_SWAP_TYPE_NONE;
485}
486
487/*
488 * This function is not used by the bootloader itself, but its required API
489 * by external tooling like mcumgr.
490 */
491int
492boot_swap_type(void)
493{
494 return boot_swap_type_multi(0);
495}
496
497/**
Sherry Zhangfbeef9b2021-05-12 15:42:30 +0800498 * Marks the image with the given index in the secondary slot as pending. On the
499 * next reboot, the system will perform a one-time boot of the the secondary
500 * slot image.
501 *
502 * @param image_index Image pair index.
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100503 *
504 * @param permanent Whether the image should be used permanently or
Sherry Zhangfbeef9b2021-05-12 15:42:30 +0800505 * only tested once:
506 * 0=run image once, then confirm or revert.
507 * 1=run image forever.
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100508 *
509 * @return 0 on success; nonzero on failure.
510 */
511int
Sherry Zhangfbeef9b2021-05-12 15:42:30 +0800512boot_set_pending_multi(int image_index, int permanent)
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100513{
514 const struct flash_area *fap;
515 struct boot_swap_state state_secondary_slot;
516 uint8_t swap_type;
517 int rc;
518
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000519 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index), &fap);
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100520 if (rc != 0) {
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000521 return BOOT_EFLASH;
522 }
523
524 rc = boot_read_swap_state(fap, &state_secondary_slot);
525 if (rc != 0) {
526 goto done;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100527 }
528
529 switch (state_secondary_slot.magic) {
530 case BOOT_MAGIC_GOOD:
531 /* Swap already scheduled. */
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000532 break;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100533
534 case BOOT_MAGIC_UNSET:
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000535 rc = boot_write_magic(fap);
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100536
537 if (rc == 0 && permanent) {
538 rc = boot_write_image_ok(fap);
539 }
540
541 if (rc == 0) {
542 if (permanent) {
543 swap_type = BOOT_SWAP_TYPE_PERM;
544 } else {
545 swap_type = BOOT_SWAP_TYPE_TEST;
546 }
547 rc = boot_write_swap_info(fap, swap_type, 0);
548 }
549
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000550 break;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100551
552 case BOOT_MAGIC_BAD:
553 /* The image slot is corrupt. There is no way to recover, so erase the
554 * slot to allow future upgrades.
555 */
Dominik Ermel260ae092021-04-23 05:38:45 +0000556 flash_area_erase(fap, 0, flash_area_get_size(fap));
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000557 rc = BOOT_EBADIMAGE;
558 break;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100559
560 default:
561 assert(0);
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000562 rc = BOOT_EBADIMAGE;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100563 }
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000564
565done:
566 flash_area_close(fap);
567 return rc;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100568}
569
570/**
Sherry Zhangfbeef9b2021-05-12 15:42:30 +0800571 * Marks the image with index 0 in the secondary slot as pending. On the next
572 * reboot, the system will perform a one-time boot of the the secondary slot
573 * image. Note that this API is kept for compatibility. The
574 * boot_set_pending_multi() API is recommended.
575 *
576 * @param permanent Whether the image should be used permanently or
577 * only tested once:
578 * 0=run image once, then confirm or revert.
579 * 1=run image forever.
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100580 *
581 * @return 0 on success; nonzero on failure.
582 */
583int
Sherry Zhangfbeef9b2021-05-12 15:42:30 +0800584boot_set_pending(int permanent)
585{
586 return boot_set_pending_multi(0, permanent);
587}
588
589/**
590 * Marks the image with the given index in the primary slot as confirmed. The
591 * system will continue booting into the image in the primary slot until told to
592 * boot from a different slot.
593 *
594 * @param image_index Image pair index.
595 *
596 * @return 0 on success; nonzero on failure.
597 */
598int
599boot_set_confirmed_multi(int image_index)
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100600{
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000601 const struct flash_area *fap = NULL;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100602 struct boot_swap_state state_primary_slot;
603 int rc;
604
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000605 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index), &fap);
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100606 if (rc != 0) {
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000607 return BOOT_EFLASH;
608 }
609
610 rc = boot_read_swap_state(fap, &state_primary_slot);
611 if (rc != 0) {
612 goto done;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100613 }
614
615 switch (state_primary_slot.magic) {
616 case BOOT_MAGIC_GOOD:
617 /* Confirm needed; proceed. */
618 break;
619
620 case BOOT_MAGIC_UNSET:
621 /* Already confirmed. */
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000622 goto done;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100623
624 case BOOT_MAGIC_BAD:
625 /* Unexpected state. */
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000626 rc = BOOT_EBADVECT;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100627 goto done;
628 }
629
Andrzej Puzdrowski22b856b2021-05-07 12:14:31 +0200630 /* Intentionally do not check copy_done flag
631 * so can confirm a padded image which was programed using a programing
632 * interface.
633 */
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100634
635 if (state_primary_slot.image_ok != BOOT_FLAG_UNSET) {
636 /* Already confirmed. */
637 goto done;
638 }
639
640 rc = boot_write_image_ok(fap);
641
642done:
643 flash_area_close(fap);
644 return rc;
645}
Sherry Zhangfbeef9b2021-05-12 15:42:30 +0800646
647/**
648 * Marks the image with index 0 in the primary slot as confirmed. The system
649 * will continue booting into the image in the primary slot until told to boot
650 * from a different slot. Note that this API is kept for compatibility. The
651 * boot_set_confirmed_multi() API is recommended.
652 *
653 * @return 0 on success; nonzero on failure.
654 */
655int
656boot_set_confirmed(void)
657{
658 return boot_set_confirmed_multi(0);
659}