blob: 15cdb0ecff6bac5a23bca8b2e05971f402a3d78c [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"
54
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +010055#ifdef CONFIG_MCUBOOT
Carlos Falgueras Garcíaa4b4b0f2021-06-22 10:00:22 +020056BOOT_LOG_MODULE_DECLARE(mcuboot);
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +010057#else
Carlos Falgueras Garcíaa4b4b0f2021-06-22 10:00:22 +020058BOOT_LOG_MODULE_REGISTER(mcuboot_util);
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +010059#endif
60
61const uint32_t boot_img_magic[] = {
62 0xf395c277,
63 0x7fefd260,
64 0x0f505235,
65 0x8079b62c,
66};
67
68#define BOOT_MAGIC_ARR_SZ \
69 (sizeof boot_img_magic / sizeof boot_img_magic[0])
70
71struct boot_swap_table {
72 uint8_t magic_primary_slot;
73 uint8_t magic_secondary_slot;
74 uint8_t image_ok_primary_slot;
75 uint8_t image_ok_secondary_slot;
76 uint8_t copy_done_primary_slot;
77
78 uint8_t swap_type;
79};
80
81/**
82 * This set of tables maps image trailer contents to swap operation type.
83 * When searching for a match, these tables must be iterated sequentially.
84 *
85 * NOTE: the table order is very important. The settings in the secondary
86 * slot always are priority to the primary slot and should be located
87 * earlier in the table.
88 *
89 * The table lists only states where there is action needs to be taken by
90 * the bootloader, as in starting/finishing a swap operation.
91 */
92static const struct boot_swap_table boot_swap_tables[] = {
93 {
94 .magic_primary_slot = BOOT_MAGIC_ANY,
95 .magic_secondary_slot = BOOT_MAGIC_GOOD,
96 .image_ok_primary_slot = BOOT_FLAG_ANY,
97 .image_ok_secondary_slot = BOOT_FLAG_UNSET,
98 .copy_done_primary_slot = BOOT_FLAG_ANY,
99 .swap_type = BOOT_SWAP_TYPE_TEST,
100 },
101 {
102 .magic_primary_slot = BOOT_MAGIC_ANY,
103 .magic_secondary_slot = BOOT_MAGIC_GOOD,
104 .image_ok_primary_slot = BOOT_FLAG_ANY,
105 .image_ok_secondary_slot = BOOT_FLAG_SET,
106 .copy_done_primary_slot = BOOT_FLAG_ANY,
107 .swap_type = BOOT_SWAP_TYPE_PERM,
108 },
109 {
110 .magic_primary_slot = BOOT_MAGIC_GOOD,
111 .magic_secondary_slot = BOOT_MAGIC_UNSET,
112 .image_ok_primary_slot = BOOT_FLAG_UNSET,
113 .image_ok_secondary_slot = BOOT_FLAG_ANY,
114 .copy_done_primary_slot = BOOT_FLAG_SET,
115 .swap_type = BOOT_SWAP_TYPE_REVERT,
116 },
117};
118
119#define BOOT_SWAP_TABLES_COUNT \
120 (sizeof boot_swap_tables / sizeof boot_swap_tables[0])
121
122static int
123boot_magic_decode(const uint32_t *magic)
124{
125 if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0) {
126 return BOOT_MAGIC_GOOD;
127 }
128 return BOOT_MAGIC_BAD;
129}
130
131static int
132boot_flag_decode(uint8_t flag)
133{
134 if (flag != BOOT_FLAG_SET) {
135 return BOOT_FLAG_BAD;
136 }
137 return BOOT_FLAG_SET;
138}
139
140static inline uint32_t
141boot_magic_off(const struct flash_area *fap)
142{
Dominik Ermel260ae092021-04-23 05:38:45 +0000143 return flash_area_get_size(fap) - BOOT_MAGIC_SZ;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100144}
145
146static inline uint32_t
147boot_image_ok_off(const struct flash_area *fap)
148{
149 return boot_magic_off(fap) - BOOT_MAX_ALIGN;
150}
151
152static inline uint32_t
153boot_copy_done_off(const struct flash_area *fap)
154{
155 return boot_image_ok_off(fap) - BOOT_MAX_ALIGN;
156}
157
158static inline uint32_t
159boot_swap_size_off(const struct flash_area *fap)
160{
161 return boot_swap_info_off(fap) - BOOT_MAX_ALIGN;
162}
163
164uint32_t
165boot_swap_info_off(const struct flash_area *fap)
166{
167 return boot_copy_done_off(fap) - BOOT_MAX_ALIGN;
168}
169
170/**
171 * Determines if a status source table is satisfied by the specified magic
172 * code.
173 *
174 * @param tbl_val A magic field from a status source table.
175 * @param val The magic value in a trailer, encoded as a
176 * BOOT_MAGIC_[...].
177 *
178 * @return 1 if the two values are compatible;
179 * 0 otherwise.
180 */
181int
182boot_magic_compatible_check(uint8_t tbl_val, uint8_t val)
183{
184 switch (tbl_val) {
185 case BOOT_MAGIC_ANY:
186 return 1;
187
188 case BOOT_MAGIC_NOTGOOD:
189 return val != BOOT_MAGIC_GOOD;
190
191 default:
192 return tbl_val == val;
193 }
194}
195
196#ifdef MCUBOOT_ENC_IMAGES
197static inline uint32_t
198boot_enc_key_off(const struct flash_area *fap, uint8_t slot)
199{
200#if MCUBOOT_SWAP_SAVE_ENCTLV
201 return boot_swap_size_off(fap) - ((slot + 1) *
202 ((((BOOT_ENC_TLV_SIZE - 1) / BOOT_MAX_ALIGN) + 1) * BOOT_MAX_ALIGN));
203#else
204 return boot_swap_size_off(fap) - ((slot + 1) * BOOT_ENC_KEY_SIZE);
205#endif
206}
207#endif
208
209bool bootutil_buffer_is_erased(const struct flash_area *area,
210 const void *buffer, size_t len)
211{
212 size_t i;
213 uint8_t *u8b;
214 uint8_t erased_val;
215
216 if (buffer == NULL || len == 0) {
217 return false;
218 }
219
220 erased_val = flash_area_erased_val(area);
221 for (i = 0, u8b = (uint8_t *)buffer; i < len; i++) {
222 if (u8b[i] != erased_val) {
223 return false;
224 }
225 }
226
227 return true;
228}
229
Andrzej Puzdrowski4700b802020-12-09 14:55:36 +0100230static int
231boot_read_flag(const struct flash_area *fap, uint8_t *flag, uint32_t off)
232{
233 int rc;
234
235 rc = flash_area_read(fap, off, flag, sizeof *flag);
236 if (rc < 0) {
237 return BOOT_EFLASH;
238 }
239 if (bootutil_buffer_is_erased(fap, flag, sizeof *flag)) {
240 *flag = BOOT_FLAG_UNSET;
241 } else {
242 *flag = boot_flag_decode(*flag);
243 }
244
245 return 0;
246}
247
248static inline int
249boot_read_copy_done(const struct flash_area *fap, uint8_t *copy_done)
250{
251 return boot_read_flag(fap, copy_done, boot_copy_done_off(fap));
252}
253
254
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100255int
256boot_read_swap_state(const struct flash_area *fap,
257 struct boot_swap_state *state)
258{
259 uint32_t magic[BOOT_MAGIC_ARR_SZ];
260 uint32_t off;
261 uint8_t swap_info;
262 int rc;
263
264 off = boot_magic_off(fap);
265 rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ);
266 if (rc < 0) {
267 return BOOT_EFLASH;
268 }
269 if (bootutil_buffer_is_erased(fap, magic, BOOT_MAGIC_SZ)) {
270 state->magic = BOOT_MAGIC_UNSET;
271 } else {
272 state->magic = boot_magic_decode(magic);
273 }
274
275 off = boot_swap_info_off(fap);
276 rc = flash_area_read(fap, off, &swap_info, sizeof swap_info);
277 if (rc < 0) {
278 return BOOT_EFLASH;
279 }
280
281 /* Extract the swap type and image number */
282 state->swap_type = BOOT_GET_SWAP_TYPE(swap_info);
283 state->image_num = BOOT_GET_IMAGE_NUM(swap_info);
284
285 if (bootutil_buffer_is_erased(fap, &swap_info, sizeof swap_info) ||
286 state->swap_type > BOOT_SWAP_TYPE_REVERT) {
287 state->swap_type = BOOT_SWAP_TYPE_NONE;
288 state->image_num = 0;
289 }
290
Andrzej Puzdrowski4700b802020-12-09 14:55:36 +0100291 rc = boot_read_copy_done(fap, &state->copy_done);
292 if (rc) {
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100293 return BOOT_EFLASH;
294 }
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100295
Andrzej Puzdrowski4700b802020-12-09 14:55:36 +0100296 return boot_read_image_ok(fap, &state->image_ok);
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100297}
298
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100299int
300boot_read_swap_state_by_id(int flash_area_id, struct boot_swap_state *state)
301{
302 const struct flash_area *fap;
303 int rc;
304
305 rc = flash_area_open(flash_area_id, &fap);
306 if (rc != 0) {
307 return BOOT_EFLASH;
308 }
309
310 rc = boot_read_swap_state(fap, state);
311 flash_area_close(fap);
312 return rc;
313}
314
315int
316boot_write_magic(const struct flash_area *fap)
317{
318 uint32_t off;
319 int rc;
320
321 off = boot_magic_off(fap);
322
323 BOOT_LOG_DBG("writing magic; fa_id=%d off=0x%lx (0x%lx)",
Dominik Ermel260ae092021-04-23 05:38:45 +0000324 flash_area_get_id(fap), (unsigned long)off,
325 (unsigned long)(flash_area_get_off(fap) + off));
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100326 rc = flash_area_write(fap, off, boot_img_magic, BOOT_MAGIC_SZ);
327 if (rc != 0) {
328 return BOOT_EFLASH;
329 }
330
331 return 0;
332}
333
334/**
335 * Write trailer data; status bytes, swap_size, etc
336 *
337 * @returns 0 on success, != 0 on error.
338 */
Dominik Ermela7f9e9f2021-03-24 17:31:57 +0000339int
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100340boot_write_trailer(const struct flash_area *fap, uint32_t off,
341 const uint8_t *inbuf, uint8_t inlen)
342{
343 uint8_t buf[BOOT_MAX_ALIGN];
344 uint8_t align;
345 uint8_t erased_val;
346 int rc;
347
348 align = flash_area_align(fap);
Dominik Ermel48281622021-03-24 18:04:54 +0000349 align = (inlen + align - 1) & ~(align - 1);
350 if (align > BOOT_MAX_ALIGN) {
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100351 return -1;
352 }
353 erased_val = flash_area_erased_val(fap);
Dominik Ermel48281622021-03-24 18:04:54 +0000354
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100355 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
Dominik Ermela7f9e9f2021-03-24 17:31:57 +0000366int
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100367boot_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)",
Dominik Ermel260ae092021-04-23 05:38:45 +0000381 flash_area_get_id(fap), (unsigned long)off,
382 (unsigned long)(flash_area_get_off(fap) + off));
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100383 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",
Dominik Ermel260ae092021-04-23 05:38:45 +0000408 flash_area_get_id(fap), (unsigned long)off,
409 (unsigned long)(flash_area_get_off(fap) + off),
410 swap_type, image_num);
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100411 return boot_write_trailer(fap, off, (const uint8_t *) &swap_info, 1);
412}
413
414int
415boot_swap_type_multi(int image_index)
416{
417 const struct boot_swap_table *table;
418 struct boot_swap_state primary_slot;
419 struct boot_swap_state secondary_slot;
420 int rc;
421 size_t i;
422
Andrzej Puzdrowskidea293a2021-07-09 12:31:35 +0200423 rc = BOOT_HOOK_CALL(boot_read_swap_state_primary_slot_hook,
424 BOOT_HOOK_REGULAR, image_index, &primary_slot);
425 if (rc == BOOT_HOOK_REGULAR)
426 {
427 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(image_index),
428 &primary_slot);
429 }
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100430 if (rc) {
431 return BOOT_SWAP_TYPE_PANIC;
432 }
433
434 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(image_index),
435 &secondary_slot);
Andrzej Puzdrowski85da97f2021-06-24 16:39:31 +0200436 if (rc == BOOT_EFLASH) {
437 BOOT_LOG_INF("Secondary image of image pair (%d.) "
438 "is unreachable. Treat it as empty", image_index);
439 secondary_slot.magic = BOOT_MAGIC_UNSET;
440 secondary_slot.swap_type = BOOT_SWAP_TYPE_NONE;
441 secondary_slot.copy_done = BOOT_FLAG_UNSET;
442 secondary_slot.image_ok = BOOT_FLAG_UNSET;
443 secondary_slot.image_num = 0;
444 } else if (rc) {
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100445 return BOOT_SWAP_TYPE_PANIC;
446 }
447
448 for (i = 0; i < BOOT_SWAP_TABLES_COUNT; i++) {
449 table = boot_swap_tables + i;
450
451 if (boot_magic_compatible_check(table->magic_primary_slot,
452 primary_slot.magic) &&
453 boot_magic_compatible_check(table->magic_secondary_slot,
454 secondary_slot.magic) &&
455 (table->image_ok_primary_slot == BOOT_FLAG_ANY ||
456 table->image_ok_primary_slot == primary_slot.image_ok) &&
457 (table->image_ok_secondary_slot == BOOT_FLAG_ANY ||
458 table->image_ok_secondary_slot == secondary_slot.image_ok) &&
459 (table->copy_done_primary_slot == BOOT_FLAG_ANY ||
460 table->copy_done_primary_slot == primary_slot.copy_done)) {
461 BOOT_LOG_INF("Swap type: %s",
462 table->swap_type == BOOT_SWAP_TYPE_TEST ? "test" :
463 table->swap_type == BOOT_SWAP_TYPE_PERM ? "perm" :
464 table->swap_type == BOOT_SWAP_TYPE_REVERT ? "revert" :
465 "BUG; can't happen");
466 if (table->swap_type != BOOT_SWAP_TYPE_TEST &&
467 table->swap_type != BOOT_SWAP_TYPE_PERM &&
468 table->swap_type != BOOT_SWAP_TYPE_REVERT) {
469 return BOOT_SWAP_TYPE_PANIC;
470 }
471 return table->swap_type;
472 }
473 }
474
475 BOOT_LOG_INF("Swap type: none");
476 return BOOT_SWAP_TYPE_NONE;
477}
478
479/*
480 * This function is not used by the bootloader itself, but its required API
481 * by external tooling like mcumgr.
482 */
483int
484boot_swap_type(void)
485{
486 return boot_swap_type_multi(0);
487}
488
489/**
Sherry Zhangfbeef9b2021-05-12 15:42:30 +0800490 * Marks the image with the given index in the secondary slot as pending. On the
491 * next reboot, the system will perform a one-time boot of the the secondary
492 * slot image.
493 *
494 * @param image_index Image pair index.
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100495 *
496 * @param permanent Whether the image should be used permanently or
Sherry Zhangfbeef9b2021-05-12 15:42:30 +0800497 * only tested once:
498 * 0=run image once, then confirm or revert.
499 * 1=run image forever.
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100500 *
501 * @return 0 on success; nonzero on failure.
502 */
503int
Sherry Zhangfbeef9b2021-05-12 15:42:30 +0800504boot_set_pending_multi(int image_index, int permanent)
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100505{
506 const struct flash_area *fap;
507 struct boot_swap_state state_secondary_slot;
508 uint8_t swap_type;
509 int rc;
510
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000511 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index), &fap);
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100512 if (rc != 0) {
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000513 return BOOT_EFLASH;
514 }
515
516 rc = boot_read_swap_state(fap, &state_secondary_slot);
517 if (rc != 0) {
518 goto done;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100519 }
520
521 switch (state_secondary_slot.magic) {
522 case BOOT_MAGIC_GOOD:
523 /* Swap already scheduled. */
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000524 break;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100525
526 case BOOT_MAGIC_UNSET:
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000527 rc = boot_write_magic(fap);
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100528
529 if (rc == 0 && permanent) {
530 rc = boot_write_image_ok(fap);
531 }
532
533 if (rc == 0) {
534 if (permanent) {
535 swap_type = BOOT_SWAP_TYPE_PERM;
536 } else {
537 swap_type = BOOT_SWAP_TYPE_TEST;
538 }
539 rc = boot_write_swap_info(fap, swap_type, 0);
540 }
541
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000542 break;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100543
544 case BOOT_MAGIC_BAD:
545 /* The image slot is corrupt. There is no way to recover, so erase the
546 * slot to allow future upgrades.
547 */
Dominik Ermel260ae092021-04-23 05:38:45 +0000548 flash_area_erase(fap, 0, flash_area_get_size(fap));
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000549 rc = BOOT_EBADIMAGE;
550 break;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100551
552 default:
553 assert(0);
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000554 rc = BOOT_EBADIMAGE;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100555 }
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000556
557done:
558 flash_area_close(fap);
559 return rc;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100560}
561
562/**
Sherry Zhangfbeef9b2021-05-12 15:42:30 +0800563 * Marks the image with index 0 in the secondary slot as pending. On the next
564 * reboot, the system will perform a one-time boot of the the secondary slot
565 * image. Note that this API is kept for compatibility. The
566 * boot_set_pending_multi() API is recommended.
567 *
568 * @param permanent Whether the image should be used permanently or
569 * only tested once:
570 * 0=run image once, then confirm or revert.
571 * 1=run image forever.
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100572 *
573 * @return 0 on success; nonzero on failure.
574 */
575int
Sherry Zhangfbeef9b2021-05-12 15:42:30 +0800576boot_set_pending(int permanent)
577{
578 return boot_set_pending_multi(0, permanent);
579}
580
581/**
582 * Marks the image with the given index in the primary slot as confirmed. The
583 * system will continue booting into the image in the primary slot until told to
584 * boot from a different slot.
585 *
586 * @param image_index Image pair index.
587 *
588 * @return 0 on success; nonzero on failure.
589 */
590int
591boot_set_confirmed_multi(int image_index)
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100592{
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000593 const struct flash_area *fap = NULL;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100594 struct boot_swap_state state_primary_slot;
595 int rc;
596
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000597 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index), &fap);
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100598 if (rc != 0) {
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000599 return BOOT_EFLASH;
600 }
601
602 rc = boot_read_swap_state(fap, &state_primary_slot);
603 if (rc != 0) {
604 goto done;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100605 }
606
607 switch (state_primary_slot.magic) {
608 case BOOT_MAGIC_GOOD:
609 /* Confirm needed; proceed. */
610 break;
611
612 case BOOT_MAGIC_UNSET:
613 /* Already confirmed. */
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000614 goto done;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100615
616 case BOOT_MAGIC_BAD:
617 /* Unexpected state. */
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000618 rc = BOOT_EBADVECT;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100619 goto done;
620 }
621
Andrzej Puzdrowski22b856b2021-05-07 12:14:31 +0200622 /* Intentionally do not check copy_done flag
623 * so can confirm a padded image which was programed using a programing
624 * interface.
625 */
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100626
627 if (state_primary_slot.image_ok != BOOT_FLAG_UNSET) {
628 /* Already confirmed. */
629 goto done;
630 }
631
632 rc = boot_write_image_ok(fap);
633
634done:
635 flash_area_close(fap);
636 return rc;
637}
Sherry Zhangfbeef9b2021-05-12 15:42:30 +0800638
639/**
640 * Marks the image with index 0 in the primary slot as confirmed. The system
641 * will continue booting into the image in the primary slot until told to boot
642 * from a different slot. Note that this API is kept for compatibility. The
643 * boot_set_confirmed_multi() API is recommended.
644 *
645 * @return 0 on success; nonzero on failure.
646 */
647int
648boot_set_confirmed(void)
649{
650 return boot_set_confirmed_multi(0);
651}