blob: 5cda14f82d07a778a4062460acda8b5a622eeefc [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{
Dominik Ermel260ae092021-04-23 05:38:45 +0000141 return flash_area_get_size(fap) - BOOT_MAGIC_SZ;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100142}
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)",
Dominik Ermel260ae092021-04-23 05:38:45 +0000322 flash_area_get_id(fap), (unsigned long)off,
323 (unsigned long)(flash_area_get_off(fap) + off));
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100324 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)",
Dominik Ermel260ae092021-04-23 05:38:45 +0000379 flash_area_get_id(fap), (unsigned long)off,
380 (unsigned long)(flash_area_get_off(fap) + off));
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100381 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",
Dominik Ermel260ae092021-04-23 05:38:45 +0000406 flash_area_get_id(fap), (unsigned long)off,
407 (unsigned long)(flash_area_get_off(fap) + off),
408 swap_type, image_num);
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100409 return boot_write_trailer(fap, off, (const uint8_t *) &swap_info, 1);
410}
411
412int
413boot_swap_type_multi(int image_index)
414{
415 const struct boot_swap_table *table;
416 struct boot_swap_state primary_slot;
417 struct boot_swap_state secondary_slot;
418 int rc;
419 size_t i;
420
421 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(image_index),
422 &primary_slot);
423 if (rc) {
424 return BOOT_SWAP_TYPE_PANIC;
425 }
426
427 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(image_index),
428 &secondary_slot);
Andrzej Puzdrowski85da97f2021-06-24 16:39:31 +0200429 if (rc == BOOT_EFLASH) {
430 BOOT_LOG_INF("Secondary image of image pair (%d.) "
431 "is unreachable. Treat it as empty", image_index);
432 secondary_slot.magic = BOOT_MAGIC_UNSET;
433 secondary_slot.swap_type = BOOT_SWAP_TYPE_NONE;
434 secondary_slot.copy_done = BOOT_FLAG_UNSET;
435 secondary_slot.image_ok = BOOT_FLAG_UNSET;
436 secondary_slot.image_num = 0;
437 } else if (rc) {
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100438 return BOOT_SWAP_TYPE_PANIC;
439 }
440
441 for (i = 0; i < BOOT_SWAP_TABLES_COUNT; i++) {
442 table = boot_swap_tables + i;
443
444 if (boot_magic_compatible_check(table->magic_primary_slot,
445 primary_slot.magic) &&
446 boot_magic_compatible_check(table->magic_secondary_slot,
447 secondary_slot.magic) &&
448 (table->image_ok_primary_slot == BOOT_FLAG_ANY ||
449 table->image_ok_primary_slot == primary_slot.image_ok) &&
450 (table->image_ok_secondary_slot == BOOT_FLAG_ANY ||
451 table->image_ok_secondary_slot == secondary_slot.image_ok) &&
452 (table->copy_done_primary_slot == BOOT_FLAG_ANY ||
453 table->copy_done_primary_slot == primary_slot.copy_done)) {
454 BOOT_LOG_INF("Swap type: %s",
455 table->swap_type == BOOT_SWAP_TYPE_TEST ? "test" :
456 table->swap_type == BOOT_SWAP_TYPE_PERM ? "perm" :
457 table->swap_type == BOOT_SWAP_TYPE_REVERT ? "revert" :
458 "BUG; can't happen");
459 if (table->swap_type != BOOT_SWAP_TYPE_TEST &&
460 table->swap_type != BOOT_SWAP_TYPE_PERM &&
461 table->swap_type != BOOT_SWAP_TYPE_REVERT) {
462 return BOOT_SWAP_TYPE_PANIC;
463 }
464 return table->swap_type;
465 }
466 }
467
468 BOOT_LOG_INF("Swap type: none");
469 return BOOT_SWAP_TYPE_NONE;
470}
471
472/*
473 * This function is not used by the bootloader itself, but its required API
474 * by external tooling like mcumgr.
475 */
476int
477boot_swap_type(void)
478{
479 return boot_swap_type_multi(0);
480}
481
482/**
Sherry Zhangfbeef9b2021-05-12 15:42:30 +0800483 * Marks the image with the given index in the secondary slot as pending. On the
484 * next reboot, the system will perform a one-time boot of the the secondary
485 * slot image.
486 *
487 * @param image_index Image pair index.
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100488 *
489 * @param permanent Whether the image should be used permanently or
Sherry Zhangfbeef9b2021-05-12 15:42:30 +0800490 * only tested once:
491 * 0=run image once, then confirm or revert.
492 * 1=run image forever.
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100493 *
494 * @return 0 on success; nonzero on failure.
495 */
496int
Sherry Zhangfbeef9b2021-05-12 15:42:30 +0800497boot_set_pending_multi(int image_index, int permanent)
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100498{
499 const struct flash_area *fap;
500 struct boot_swap_state state_secondary_slot;
501 uint8_t swap_type;
502 int rc;
503
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000504 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index), &fap);
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100505 if (rc != 0) {
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000506 return BOOT_EFLASH;
507 }
508
509 rc = boot_read_swap_state(fap, &state_secondary_slot);
510 if (rc != 0) {
511 goto done;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100512 }
513
514 switch (state_secondary_slot.magic) {
515 case BOOT_MAGIC_GOOD:
516 /* Swap already scheduled. */
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000517 break;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100518
519 case BOOT_MAGIC_UNSET:
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000520 rc = boot_write_magic(fap);
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100521
522 if (rc == 0 && permanent) {
523 rc = boot_write_image_ok(fap);
524 }
525
526 if (rc == 0) {
527 if (permanent) {
528 swap_type = BOOT_SWAP_TYPE_PERM;
529 } else {
530 swap_type = BOOT_SWAP_TYPE_TEST;
531 }
532 rc = boot_write_swap_info(fap, swap_type, 0);
533 }
534
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000535 break;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100536
537 case BOOT_MAGIC_BAD:
538 /* The image slot is corrupt. There is no way to recover, so erase the
539 * slot to allow future upgrades.
540 */
Dominik Ermel260ae092021-04-23 05:38:45 +0000541 flash_area_erase(fap, 0, flash_area_get_size(fap));
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000542 rc = BOOT_EBADIMAGE;
543 break;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100544
545 default:
546 assert(0);
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000547 rc = BOOT_EBADIMAGE;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100548 }
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000549
550done:
551 flash_area_close(fap);
552 return rc;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100553}
554
555/**
Sherry Zhangfbeef9b2021-05-12 15:42:30 +0800556 * Marks the image with index 0 in the secondary slot as pending. On the next
557 * reboot, the system will perform a one-time boot of the the secondary slot
558 * image. Note that this API is kept for compatibility. The
559 * boot_set_pending_multi() API is recommended.
560 *
561 * @param permanent Whether the image should be used permanently or
562 * only tested once:
563 * 0=run image once, then confirm or revert.
564 * 1=run image forever.
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100565 *
566 * @return 0 on success; nonzero on failure.
567 */
568int
Sherry Zhangfbeef9b2021-05-12 15:42:30 +0800569boot_set_pending(int permanent)
570{
571 return boot_set_pending_multi(0, permanent);
572}
573
574/**
575 * Marks the image with the given index in the primary slot as confirmed. The
576 * system will continue booting into the image in the primary slot until told to
577 * boot from a different slot.
578 *
579 * @param image_index Image pair index.
580 *
581 * @return 0 on success; nonzero on failure.
582 */
583int
584boot_set_confirmed_multi(int image_index)
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100585{
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000586 const struct flash_area *fap = NULL;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100587 struct boot_swap_state state_primary_slot;
588 int rc;
589
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000590 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index), &fap);
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100591 if (rc != 0) {
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000592 return BOOT_EFLASH;
593 }
594
595 rc = boot_read_swap_state(fap, &state_primary_slot);
596 if (rc != 0) {
597 goto done;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100598 }
599
600 switch (state_primary_slot.magic) {
601 case BOOT_MAGIC_GOOD:
602 /* Confirm needed; proceed. */
603 break;
604
605 case BOOT_MAGIC_UNSET:
606 /* Already confirmed. */
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000607 goto done;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100608
609 case BOOT_MAGIC_BAD:
610 /* Unexpected state. */
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000611 rc = BOOT_EBADVECT;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100612 goto done;
613 }
614
Andrzej Puzdrowski22b856b2021-05-07 12:14:31 +0200615 /* Intentionally do not check copy_done flag
616 * so can confirm a padded image which was programed using a programing
617 * interface.
618 */
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100619
620 if (state_primary_slot.image_ok != BOOT_FLAG_UNSET) {
621 /* Already confirmed. */
622 goto done;
623 }
624
625 rc = boot_write_image_ok(fap);
626
627done:
628 flash_area_close(fap);
629 return rc;
630}
Sherry Zhangfbeef9b2021-05-12 15:42:30 +0800631
632/**
633 * Marks the image with index 0 in the primary slot as confirmed. The system
634 * will continue booting into the image in the primary slot until told to boot
635 * from a different slot. Note that this API is kept for compatibility. The
636 * boot_set_confirmed_multi() API is recommended.
637 *
638 * @return 0 on success; nonzero on failure.
639 */
640int
641boot_set_confirmed(void)
642{
643 return boot_set_confirmed_multi(0);
644}