blob: 726fc36f040d2cc52a275ac4486efdb17d7a3803 [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
53#ifdef CONFIG_MCUBOOT
54MCUBOOT_LOG_MODULE_DECLARE(mcuboot);
55#else
56MCUBOOT_LOG_MODULE_REGISTER(mcuboot_util);
57#endif
58
59const uint32_t boot_img_magic[] = {
60 0xf395c277,
61 0x7fefd260,
62 0x0f505235,
63 0x8079b62c,
64};
65
66#define BOOT_MAGIC_ARR_SZ \
67 (sizeof boot_img_magic / sizeof boot_img_magic[0])
68
69struct boot_swap_table {
70 uint8_t magic_primary_slot;
71 uint8_t magic_secondary_slot;
72 uint8_t image_ok_primary_slot;
73 uint8_t image_ok_secondary_slot;
74 uint8_t copy_done_primary_slot;
75
76 uint8_t swap_type;
77};
78
79/**
80 * This set of tables maps image trailer contents to swap operation type.
81 * When searching for a match, these tables must be iterated sequentially.
82 *
83 * NOTE: the table order is very important. The settings in the secondary
84 * slot always are priority to the primary slot and should be located
85 * earlier in the table.
86 *
87 * The table lists only states where there is action needs to be taken by
88 * the bootloader, as in starting/finishing a swap operation.
89 */
90static const struct boot_swap_table boot_swap_tables[] = {
91 {
92 .magic_primary_slot = BOOT_MAGIC_ANY,
93 .magic_secondary_slot = BOOT_MAGIC_GOOD,
94 .image_ok_primary_slot = BOOT_FLAG_ANY,
95 .image_ok_secondary_slot = BOOT_FLAG_UNSET,
96 .copy_done_primary_slot = BOOT_FLAG_ANY,
97 .swap_type = BOOT_SWAP_TYPE_TEST,
98 },
99 {
100 .magic_primary_slot = BOOT_MAGIC_ANY,
101 .magic_secondary_slot = BOOT_MAGIC_GOOD,
102 .image_ok_primary_slot = BOOT_FLAG_ANY,
103 .image_ok_secondary_slot = BOOT_FLAG_SET,
104 .copy_done_primary_slot = BOOT_FLAG_ANY,
105 .swap_type = BOOT_SWAP_TYPE_PERM,
106 },
107 {
108 .magic_primary_slot = BOOT_MAGIC_GOOD,
109 .magic_secondary_slot = BOOT_MAGIC_UNSET,
110 .image_ok_primary_slot = BOOT_FLAG_UNSET,
111 .image_ok_secondary_slot = BOOT_FLAG_ANY,
112 .copy_done_primary_slot = BOOT_FLAG_SET,
113 .swap_type = BOOT_SWAP_TYPE_REVERT,
114 },
115};
116
117#define BOOT_SWAP_TABLES_COUNT \
118 (sizeof boot_swap_tables / sizeof boot_swap_tables[0])
119
120static int
121boot_magic_decode(const uint32_t *magic)
122{
123 if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0) {
124 return BOOT_MAGIC_GOOD;
125 }
126 return BOOT_MAGIC_BAD;
127}
128
129static int
130boot_flag_decode(uint8_t flag)
131{
132 if (flag != BOOT_FLAG_SET) {
133 return BOOT_FLAG_BAD;
134 }
135 return BOOT_FLAG_SET;
136}
137
138static inline uint32_t
139boot_magic_off(const struct flash_area *fap)
140{
141 return fap->fa_size - BOOT_MAGIC_SZ;
142}
143
144static inline uint32_t
145boot_image_ok_off(const struct flash_area *fap)
146{
147 return boot_magic_off(fap) - BOOT_MAX_ALIGN;
148}
149
150static inline uint32_t
151boot_copy_done_off(const struct flash_area *fap)
152{
153 return boot_image_ok_off(fap) - BOOT_MAX_ALIGN;
154}
155
156static inline uint32_t
157boot_swap_size_off(const struct flash_area *fap)
158{
159 return boot_swap_info_off(fap) - BOOT_MAX_ALIGN;
160}
161
162uint32_t
163boot_swap_info_off(const struct flash_area *fap)
164{
165 return boot_copy_done_off(fap) - BOOT_MAX_ALIGN;
166}
167
168/**
169 * Determines if a status source table is satisfied by the specified magic
170 * code.
171 *
172 * @param tbl_val A magic field from a status source table.
173 * @param val The magic value in a trailer, encoded as a
174 * BOOT_MAGIC_[...].
175 *
176 * @return 1 if the two values are compatible;
177 * 0 otherwise.
178 */
179int
180boot_magic_compatible_check(uint8_t tbl_val, uint8_t val)
181{
182 switch (tbl_val) {
183 case BOOT_MAGIC_ANY:
184 return 1;
185
186 case BOOT_MAGIC_NOTGOOD:
187 return val != BOOT_MAGIC_GOOD;
188
189 default:
190 return tbl_val == val;
191 }
192}
193
194#ifdef MCUBOOT_ENC_IMAGES
195static inline uint32_t
196boot_enc_key_off(const struct flash_area *fap, uint8_t slot)
197{
198#if MCUBOOT_SWAP_SAVE_ENCTLV
199 return boot_swap_size_off(fap) - ((slot + 1) *
200 ((((BOOT_ENC_TLV_SIZE - 1) / BOOT_MAX_ALIGN) + 1) * BOOT_MAX_ALIGN));
201#else
202 return boot_swap_size_off(fap) - ((slot + 1) * BOOT_ENC_KEY_SIZE);
203#endif
204}
205#endif
206
207bool bootutil_buffer_is_erased(const struct flash_area *area,
208 const void *buffer, size_t len)
209{
210 size_t i;
211 uint8_t *u8b;
212 uint8_t erased_val;
213
214 if (buffer == NULL || len == 0) {
215 return false;
216 }
217
218 erased_val = flash_area_erased_val(area);
219 for (i = 0, u8b = (uint8_t *)buffer; i < len; i++) {
220 if (u8b[i] != erased_val) {
221 return false;
222 }
223 }
224
225 return true;
226}
227
Andrzej Puzdrowski4700b802020-12-09 14:55:36 +0100228static int
229boot_read_flag(const struct flash_area *fap, uint8_t *flag, uint32_t off)
230{
231 int rc;
232
233 rc = flash_area_read(fap, off, flag, sizeof *flag);
234 if (rc < 0) {
235 return BOOT_EFLASH;
236 }
237 if (bootutil_buffer_is_erased(fap, flag, sizeof *flag)) {
238 *flag = BOOT_FLAG_UNSET;
239 } else {
240 *flag = boot_flag_decode(*flag);
241 }
242
243 return 0;
244}
245
246static inline int
247boot_read_copy_done(const struct flash_area *fap, uint8_t *copy_done)
248{
249 return boot_read_flag(fap, copy_done, boot_copy_done_off(fap));
250}
251
252
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100253int
254boot_read_swap_state(const struct flash_area *fap,
255 struct boot_swap_state *state)
256{
257 uint32_t magic[BOOT_MAGIC_ARR_SZ];
258 uint32_t off;
259 uint8_t swap_info;
260 int rc;
261
262 off = boot_magic_off(fap);
263 rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ);
264 if (rc < 0) {
265 return BOOT_EFLASH;
266 }
267 if (bootutil_buffer_is_erased(fap, magic, BOOT_MAGIC_SZ)) {
268 state->magic = BOOT_MAGIC_UNSET;
269 } else {
270 state->magic = boot_magic_decode(magic);
271 }
272
273 off = boot_swap_info_off(fap);
274 rc = flash_area_read(fap, off, &swap_info, sizeof swap_info);
275 if (rc < 0) {
276 return BOOT_EFLASH;
277 }
278
279 /* Extract the swap type and image number */
280 state->swap_type = BOOT_GET_SWAP_TYPE(swap_info);
281 state->image_num = BOOT_GET_IMAGE_NUM(swap_info);
282
283 if (bootutil_buffer_is_erased(fap, &swap_info, sizeof swap_info) ||
284 state->swap_type > BOOT_SWAP_TYPE_REVERT) {
285 state->swap_type = BOOT_SWAP_TYPE_NONE;
286 state->image_num = 0;
287 }
288
Andrzej Puzdrowski4700b802020-12-09 14:55:36 +0100289 rc = boot_read_copy_done(fap, &state->copy_done);
290 if (rc) {
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100291 return BOOT_EFLASH;
292 }
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100293
Andrzej Puzdrowski4700b802020-12-09 14:55:36 +0100294 return boot_read_image_ok(fap, &state->image_ok);
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100295}
296
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100297int
298boot_read_swap_state_by_id(int flash_area_id, struct boot_swap_state *state)
299{
300 const struct flash_area *fap;
301 int rc;
302
303 rc = flash_area_open(flash_area_id, &fap);
304 if (rc != 0) {
305 return BOOT_EFLASH;
306 }
307
308 rc = boot_read_swap_state(fap, state);
309 flash_area_close(fap);
310 return rc;
311}
312
313int
314boot_write_magic(const struct flash_area *fap)
315{
316 uint32_t off;
317 int rc;
318
319 off = boot_magic_off(fap);
320
321 BOOT_LOG_DBG("writing magic; fa_id=%d off=0x%lx (0x%lx)",
322 fap->fa_id, (unsigned long)off,
323 (unsigned long)(fap->fa_off + off));
324 rc = flash_area_write(fap, off, boot_img_magic, BOOT_MAGIC_SZ);
325 if (rc != 0) {
326 return BOOT_EFLASH;
327 }
328
329 return 0;
330}
331
332/**
333 * Write trailer data; status bytes, swap_size, etc
334 *
335 * @returns 0 on success, != 0 on error.
336 */
Dominik Ermela7f9e9f2021-03-24 17:31:57 +0000337int
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100338boot_write_trailer(const struct flash_area *fap, uint32_t off,
339 const uint8_t *inbuf, uint8_t inlen)
340{
341 uint8_t buf[BOOT_MAX_ALIGN];
342 uint8_t align;
343 uint8_t erased_val;
344 int rc;
345
346 align = flash_area_align(fap);
Dominik Ermel48281622021-03-24 18:04:54 +0000347 align = (inlen + align - 1) & ~(align - 1);
348 if (align > BOOT_MAX_ALIGN) {
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100349 return -1;
350 }
351 erased_val = flash_area_erased_val(fap);
Dominik Ermel48281622021-03-24 18:04:54 +0000352
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100353 memcpy(buf, inbuf, inlen);
354 memset(&buf[inlen], erased_val, align - inlen);
355
356 rc = flash_area_write(fap, off, buf, align);
357 if (rc != 0) {
358 return BOOT_EFLASH;
359 }
360
361 return 0;
362}
363
Dominik Ermela7f9e9f2021-03-24 17:31:57 +0000364int
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100365boot_write_trailer_flag(const struct flash_area *fap, uint32_t off,
366 uint8_t flag_val)
367{
368 const uint8_t buf[1] = { flag_val };
369 return boot_write_trailer(fap, off, buf, 1);
370}
371
372int
373boot_write_image_ok(const struct flash_area *fap)
374{
375 uint32_t off;
376
377 off = boot_image_ok_off(fap);
378 BOOT_LOG_DBG("writing image_ok; fa_id=%d off=0x%lx (0x%lx)",
379 fap->fa_id, (unsigned long)off,
380 (unsigned long)(fap->fa_off + off));
381 return boot_write_trailer_flag(fap, off, BOOT_FLAG_SET);
382}
383
Andrzej Puzdrowski4700b802020-12-09 14:55:36 +0100384int
385boot_read_image_ok(const struct flash_area *fap, uint8_t *image_ok)
386{
387 return boot_read_flag(fap, image_ok, boot_image_ok_off(fap));
388}
389
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100390/**
391 * Writes the specified value to the `swap-type` field of an image trailer.
392 * This value is persisted so that the boot loader knows what swap operation to
393 * resume in case of an unexpected reset.
394 */
395int
396boot_write_swap_info(const struct flash_area *fap, uint8_t swap_type,
397 uint8_t image_num)
398{
399 uint32_t off;
400 uint8_t swap_info;
401
402 BOOT_SET_SWAP_INFO(swap_info, image_num, swap_type);
403 off = boot_swap_info_off(fap);
404 BOOT_LOG_DBG("writing swap_info; fa_id=%d off=0x%lx (0x%lx), swap_type=0x%x"
405 " image_num=0x%x",
406 fap->fa_id, (unsigned long)off,
407 (unsigned long)(fap->fa_off + off), swap_type, image_num);
408 return boot_write_trailer(fap, off, (const uint8_t *) &swap_info, 1);
409}
410
411int
412boot_swap_type_multi(int image_index)
413{
414 const struct boot_swap_table *table;
415 struct boot_swap_state primary_slot;
416 struct boot_swap_state secondary_slot;
417 int rc;
418 size_t i;
419
420 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(image_index),
421 &primary_slot);
422 if (rc) {
423 return BOOT_SWAP_TYPE_PANIC;
424 }
425
426 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(image_index),
427 &secondary_slot);
Andrzej Puzdrowski85da97f2021-06-24 16:39:31 +0200428 if (rc == BOOT_EFLASH) {
429 BOOT_LOG_INF("Secondary image of image pair (%d.) "
430 "is unreachable. Treat it as empty", image_index);
431 secondary_slot.magic = BOOT_MAGIC_UNSET;
432 secondary_slot.swap_type = BOOT_SWAP_TYPE_NONE;
433 secondary_slot.copy_done = BOOT_FLAG_UNSET;
434 secondary_slot.image_ok = BOOT_FLAG_UNSET;
435 secondary_slot.image_num = 0;
436 } else if (rc) {
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100437 return BOOT_SWAP_TYPE_PANIC;
438 }
439
440 for (i = 0; i < BOOT_SWAP_TABLES_COUNT; i++) {
441 table = boot_swap_tables + i;
442
443 if (boot_magic_compatible_check(table->magic_primary_slot,
444 primary_slot.magic) &&
445 boot_magic_compatible_check(table->magic_secondary_slot,
446 secondary_slot.magic) &&
447 (table->image_ok_primary_slot == BOOT_FLAG_ANY ||
448 table->image_ok_primary_slot == primary_slot.image_ok) &&
449 (table->image_ok_secondary_slot == BOOT_FLAG_ANY ||
450 table->image_ok_secondary_slot == secondary_slot.image_ok) &&
451 (table->copy_done_primary_slot == BOOT_FLAG_ANY ||
452 table->copy_done_primary_slot == primary_slot.copy_done)) {
453 BOOT_LOG_INF("Swap type: %s",
454 table->swap_type == BOOT_SWAP_TYPE_TEST ? "test" :
455 table->swap_type == BOOT_SWAP_TYPE_PERM ? "perm" :
456 table->swap_type == BOOT_SWAP_TYPE_REVERT ? "revert" :
457 "BUG; can't happen");
458 if (table->swap_type != BOOT_SWAP_TYPE_TEST &&
459 table->swap_type != BOOT_SWAP_TYPE_PERM &&
460 table->swap_type != BOOT_SWAP_TYPE_REVERT) {
461 return BOOT_SWAP_TYPE_PANIC;
462 }
463 return table->swap_type;
464 }
465 }
466
467 BOOT_LOG_INF("Swap type: none");
468 return BOOT_SWAP_TYPE_NONE;
469}
470
471/*
472 * This function is not used by the bootloader itself, but its required API
473 * by external tooling like mcumgr.
474 */
475int
476boot_swap_type(void)
477{
478 return boot_swap_type_multi(0);
479}
480
481/**
Sherry Zhangfbeef9b2021-05-12 15:42:30 +0800482 * Marks the image with the given index in the secondary slot as pending. On the
483 * next reboot, the system will perform a one-time boot of the the secondary
484 * slot image.
485 *
486 * @param image_index Image pair index.
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100487 *
488 * @param permanent Whether the image should be used permanently or
Sherry Zhangfbeef9b2021-05-12 15:42:30 +0800489 * only tested once:
490 * 0=run image once, then confirm or revert.
491 * 1=run image forever.
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100492 *
493 * @return 0 on success; nonzero on failure.
494 */
495int
Sherry Zhangfbeef9b2021-05-12 15:42:30 +0800496boot_set_pending_multi(int image_index, int permanent)
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100497{
498 const struct flash_area *fap;
499 struct boot_swap_state state_secondary_slot;
500 uint8_t swap_type;
501 int rc;
502
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000503 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index), &fap);
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100504 if (rc != 0) {
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000505 return BOOT_EFLASH;
506 }
507
508 rc = boot_read_swap_state(fap, &state_secondary_slot);
509 if (rc != 0) {
510 goto done;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100511 }
512
513 switch (state_secondary_slot.magic) {
514 case BOOT_MAGIC_GOOD:
515 /* Swap already scheduled. */
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000516 break;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100517
518 case BOOT_MAGIC_UNSET:
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000519 rc = boot_write_magic(fap);
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100520
521 if (rc == 0 && permanent) {
522 rc = boot_write_image_ok(fap);
523 }
524
525 if (rc == 0) {
526 if (permanent) {
527 swap_type = BOOT_SWAP_TYPE_PERM;
528 } else {
529 swap_type = BOOT_SWAP_TYPE_TEST;
530 }
531 rc = boot_write_swap_info(fap, swap_type, 0);
532 }
533
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000534 break;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100535
536 case BOOT_MAGIC_BAD:
537 /* The image slot is corrupt. There is no way to recover, so erase the
538 * slot to allow future upgrades.
539 */
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100540 flash_area_erase(fap, 0, fap->fa_size);
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000541 rc = BOOT_EBADIMAGE;
542 break;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100543
544 default:
545 assert(0);
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000546 rc = BOOT_EBADIMAGE;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100547 }
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000548
549done:
550 flash_area_close(fap);
551 return rc;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100552}
553
554/**
Sherry Zhangfbeef9b2021-05-12 15:42:30 +0800555 * Marks the image with index 0 in the secondary slot as pending. On the next
556 * reboot, the system will perform a one-time boot of the the secondary slot
557 * image. Note that this API is kept for compatibility. The
558 * boot_set_pending_multi() API is recommended.
559 *
560 * @param permanent Whether the image should be used permanently or
561 * only tested once:
562 * 0=run image once, then confirm or revert.
563 * 1=run image forever.
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100564 *
565 * @return 0 on success; nonzero on failure.
566 */
567int
Sherry Zhangfbeef9b2021-05-12 15:42:30 +0800568boot_set_pending(int permanent)
569{
570 return boot_set_pending_multi(0, permanent);
571}
572
573/**
574 * Marks the image with the given index in the primary slot as confirmed. The
575 * system will continue booting into the image in the primary slot until told to
576 * boot from a different slot.
577 *
578 * @param image_index Image pair index.
579 *
580 * @return 0 on success; nonzero on failure.
581 */
582int
583boot_set_confirmed_multi(int image_index)
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100584{
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000585 const struct flash_area *fap = NULL;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100586 struct boot_swap_state state_primary_slot;
587 int rc;
588
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000589 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index), &fap);
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100590 if (rc != 0) {
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000591 return BOOT_EFLASH;
592 }
593
594 rc = boot_read_swap_state(fap, &state_primary_slot);
595 if (rc != 0) {
596 goto done;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100597 }
598
599 switch (state_primary_slot.magic) {
600 case BOOT_MAGIC_GOOD:
601 /* Confirm needed; proceed. */
602 break;
603
604 case BOOT_MAGIC_UNSET:
605 /* Already confirmed. */
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000606 goto done;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100607
608 case BOOT_MAGIC_BAD:
609 /* Unexpected state. */
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000610 rc = BOOT_EBADVECT;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100611 goto done;
612 }
613
Andrzej Puzdrowski22b856b2021-05-07 12:14:31 +0200614 /* Intentionally do not check copy_done flag
615 * so can confirm a padded image which was programed using a programing
616 * interface.
617 */
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100618
619 if (state_primary_slot.image_ok != BOOT_FLAG_UNSET) {
620 /* Already confirmed. */
621 goto done;
622 }
623
624 rc = boot_write_image_ok(fap);
625
626done:
627 flash_area_close(fap);
628 return rc;
629}
Sherry Zhangfbeef9b2021-05-12 15:42:30 +0800630
631/**
632 * Marks the image with index 0 in the primary slot as confirmed. The system
633 * will continue booting into the image in the primary slot until told to boot
634 * from a different slot. Note that this API is kept for compatibility. The
635 * boot_set_confirmed_multi() API is recommended.
636 *
637 * @return 0 on success; nonzero on failure.
638 */
639int
640boot_set_confirmed(void)
641{
642 return boot_set_confirmed_multi(0);
643}