blob: 63092f2833c08b37e114e34a5238bf43fa14b558 [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{
Kristine Jassmann73c38c62021-02-03 16:56:14 +0000149 return ALIGN_DOWN(boot_magic_off(fap) - BOOT_MAX_ALIGN, BOOT_MAX_ALIGN);
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100150}
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
Kristine Jassmann73c38c62021-02-03 16:56:14 +0000201 return boot_swap_size_off(fap) - ((slot + 1) * BOOT_ENC_TLV_ALIGN_SIZE);
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100202#else
Kristine Jassmann73c38c62021-02-03 16:56:14 +0000203 return boot_swap_size_off(fap) - ((slot + 1) * BOOT_ENC_KEY_ALIGN_SIZE);
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100204#endif
205}
206#endif
207
208bool bootutil_buffer_is_erased(const struct flash_area *area,
209 const void *buffer, size_t len)
210{
211 size_t i;
212 uint8_t *u8b;
213 uint8_t erased_val;
214
215 if (buffer == NULL || len == 0) {
216 return false;
217 }
218
219 erased_val = flash_area_erased_val(area);
220 for (i = 0, u8b = (uint8_t *)buffer; i < len; i++) {
221 if (u8b[i] != erased_val) {
222 return false;
223 }
224 }
225
226 return true;
227}
228
Andrzej Puzdrowski4700b802020-12-09 14:55:36 +0100229static int
230boot_read_flag(const struct flash_area *fap, uint8_t *flag, uint32_t off)
231{
232 int rc;
233
234 rc = flash_area_read(fap, off, flag, sizeof *flag);
235 if (rc < 0) {
236 return BOOT_EFLASH;
237 }
238 if (bootutil_buffer_is_erased(fap, flag, sizeof *flag)) {
239 *flag = BOOT_FLAG_UNSET;
240 } else {
241 *flag = boot_flag_decode(*flag);
242 }
243
244 return 0;
245}
246
247static inline int
248boot_read_copy_done(const struct flash_area *fap, uint8_t *copy_done)
249{
250 return boot_read_flag(fap, copy_done, boot_copy_done_off(fap));
251}
252
253
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100254int
255boot_read_swap_state(const struct flash_area *fap,
256 struct boot_swap_state *state)
257{
258 uint32_t magic[BOOT_MAGIC_ARR_SZ];
259 uint32_t off;
260 uint8_t swap_info;
261 int rc;
262
263 off = boot_magic_off(fap);
264 rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ);
265 if (rc < 0) {
266 return BOOT_EFLASH;
267 }
268 if (bootutil_buffer_is_erased(fap, magic, BOOT_MAGIC_SZ)) {
269 state->magic = BOOT_MAGIC_UNSET;
270 } else {
271 state->magic = boot_magic_decode(magic);
272 }
273
274 off = boot_swap_info_off(fap);
275 rc = flash_area_read(fap, off, &swap_info, sizeof swap_info);
276 if (rc < 0) {
277 return BOOT_EFLASH;
278 }
279
280 /* Extract the swap type and image number */
281 state->swap_type = BOOT_GET_SWAP_TYPE(swap_info);
282 state->image_num = BOOT_GET_IMAGE_NUM(swap_info);
283
284 if (bootutil_buffer_is_erased(fap, &swap_info, sizeof swap_info) ||
285 state->swap_type > BOOT_SWAP_TYPE_REVERT) {
286 state->swap_type = BOOT_SWAP_TYPE_NONE;
287 state->image_num = 0;
288 }
289
Andrzej Puzdrowski4700b802020-12-09 14:55:36 +0100290 rc = boot_read_copy_done(fap, &state->copy_done);
291 if (rc) {
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100292 return BOOT_EFLASH;
293 }
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100294
Andrzej Puzdrowski4700b802020-12-09 14:55:36 +0100295 return boot_read_image_ok(fap, &state->image_ok);
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100296}
297
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100298int
299boot_read_swap_state_by_id(int flash_area_id, struct boot_swap_state *state)
300{
301 const struct flash_area *fap;
302 int rc;
303
304 rc = flash_area_open(flash_area_id, &fap);
305 if (rc != 0) {
306 return BOOT_EFLASH;
307 }
308
309 rc = boot_read_swap_state(fap, state);
310 flash_area_close(fap);
311 return rc;
312}
313
314int
315boot_write_magic(const struct flash_area *fap)
316{
317 uint32_t off;
Kristine Jassmann73c38c62021-02-03 16:56:14 +0000318 uint32_t pad_off;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100319 int rc;
Kristine Jassmann73c38c62021-02-03 16:56:14 +0000320 uint8_t magic[BOOT_MAGIC_ALIGN_SIZE];
321 uint8_t erased_val;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100322
323 off = boot_magic_off(fap);
324
Kristine Jassmann73c38c62021-02-03 16:56:14 +0000325 /* image_trailer structure was modified with additional padding such that
326 * the pad+magic ends up in a flash minimum write region. The address
327 * returned by boot_magic_off() is the start of magic which is not the
328 * start of the flash write boundary and thus writes to the magic will fail.
329 * To account for this change, write to magic is first padded with 0xFF
330 * before writing to the trailer.
331 */
332 pad_off = ALIGN_DOWN(off, BOOT_MAX_ALIGN);
333
334 erased_val = flash_area_erased_val(fap);
335
336 memset(&magic[0], erased_val, sizeof(magic));
337 memcpy(&magic[BOOT_MAGIC_ALIGN_SIZE - BOOT_MAGIC_SZ], boot_img_magic, BOOT_MAGIC_SZ);
338
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100339 BOOT_LOG_DBG("writing magic; fa_id=%d off=0x%lx (0x%lx)",
Dominik Ermel260ae092021-04-23 05:38:45 +0000340 flash_area_get_id(fap), (unsigned long)off,
341 (unsigned long)(flash_area_get_off(fap) + off));
Kristine Jassmann73c38c62021-02-03 16:56:14 +0000342 rc = flash_area_write(fap, pad_off, &magic[0], BOOT_MAGIC_ALIGN_SIZE);
343
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100344 if (rc != 0) {
345 return BOOT_EFLASH;
346 }
347
348 return 0;
349}
350
351/**
352 * Write trailer data; status bytes, swap_size, etc
353 *
354 * @returns 0 on success, != 0 on error.
355 */
Dominik Ermela7f9e9f2021-03-24 17:31:57 +0000356int
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100357boot_write_trailer(const struct flash_area *fap, uint32_t off,
358 const uint8_t *inbuf, uint8_t inlen)
359{
360 uint8_t buf[BOOT_MAX_ALIGN];
361 uint8_t align;
362 uint8_t erased_val;
363 int rc;
364
365 align = flash_area_align(fap);
Kristine Jassmann73c38c62021-02-03 16:56:14 +0000366 align = ALIGN_UP(inlen, align);
Dominik Ermel48281622021-03-24 18:04:54 +0000367 if (align > BOOT_MAX_ALIGN) {
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100368 return -1;
369 }
370 erased_val = flash_area_erased_val(fap);
Dominik Ermel48281622021-03-24 18:04:54 +0000371
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100372 memcpy(buf, inbuf, inlen);
373 memset(&buf[inlen], erased_val, align - inlen);
374
375 rc = flash_area_write(fap, off, buf, align);
376 if (rc != 0) {
377 return BOOT_EFLASH;
378 }
379
380 return 0;
381}
382
Dominik Ermela7f9e9f2021-03-24 17:31:57 +0000383int
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100384boot_write_trailer_flag(const struct flash_area *fap, uint32_t off,
385 uint8_t flag_val)
386{
387 const uint8_t buf[1] = { flag_val };
388 return boot_write_trailer(fap, off, buf, 1);
389}
390
391int
392boot_write_image_ok(const struct flash_area *fap)
393{
394 uint32_t off;
395
396 off = boot_image_ok_off(fap);
397 BOOT_LOG_DBG("writing image_ok; fa_id=%d off=0x%lx (0x%lx)",
Dominik Ermel260ae092021-04-23 05:38:45 +0000398 flash_area_get_id(fap), (unsigned long)off,
399 (unsigned long)(flash_area_get_off(fap) + off));
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100400 return boot_write_trailer_flag(fap, off, BOOT_FLAG_SET);
401}
402
Andrzej Puzdrowski4700b802020-12-09 14:55:36 +0100403int
404boot_read_image_ok(const struct flash_area *fap, uint8_t *image_ok)
405{
406 return boot_read_flag(fap, image_ok, boot_image_ok_off(fap));
407}
408
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100409/**
410 * Writes the specified value to the `swap-type` field of an image trailer.
411 * This value is persisted so that the boot loader knows what swap operation to
412 * resume in case of an unexpected reset.
413 */
414int
415boot_write_swap_info(const struct flash_area *fap, uint8_t swap_type,
416 uint8_t image_num)
417{
418 uint32_t off;
419 uint8_t swap_info;
420
421 BOOT_SET_SWAP_INFO(swap_info, image_num, swap_type);
422 off = boot_swap_info_off(fap);
423 BOOT_LOG_DBG("writing swap_info; fa_id=%d off=0x%lx (0x%lx), swap_type=0x%x"
424 " image_num=0x%x",
Dominik Ermel260ae092021-04-23 05:38:45 +0000425 flash_area_get_id(fap), (unsigned long)off,
426 (unsigned long)(flash_area_get_off(fap) + off),
427 swap_type, image_num);
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100428 return boot_write_trailer(fap, off, (const uint8_t *) &swap_info, 1);
429}
430
431int
432boot_swap_type_multi(int image_index)
433{
434 const struct boot_swap_table *table;
435 struct boot_swap_state primary_slot;
436 struct boot_swap_state secondary_slot;
437 int rc;
438 size_t i;
439
Andrzej Puzdrowskidea293a2021-07-09 12:31:35 +0200440 rc = BOOT_HOOK_CALL(boot_read_swap_state_primary_slot_hook,
441 BOOT_HOOK_REGULAR, image_index, &primary_slot);
442 if (rc == BOOT_HOOK_REGULAR)
443 {
444 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(image_index),
445 &primary_slot);
446 }
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100447 if (rc) {
448 return BOOT_SWAP_TYPE_PANIC;
449 }
450
451 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(image_index),
452 &secondary_slot);
Andrzej Puzdrowski85da97f2021-06-24 16:39:31 +0200453 if (rc == BOOT_EFLASH) {
454 BOOT_LOG_INF("Secondary image of image pair (%d.) "
455 "is unreachable. Treat it as empty", image_index);
456 secondary_slot.magic = BOOT_MAGIC_UNSET;
457 secondary_slot.swap_type = BOOT_SWAP_TYPE_NONE;
458 secondary_slot.copy_done = BOOT_FLAG_UNSET;
459 secondary_slot.image_ok = BOOT_FLAG_UNSET;
460 secondary_slot.image_num = 0;
461 } else if (rc) {
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100462 return BOOT_SWAP_TYPE_PANIC;
463 }
464
465 for (i = 0; i < BOOT_SWAP_TABLES_COUNT; i++) {
466 table = boot_swap_tables + i;
467
468 if (boot_magic_compatible_check(table->magic_primary_slot,
469 primary_slot.magic) &&
470 boot_magic_compatible_check(table->magic_secondary_slot,
471 secondary_slot.magic) &&
472 (table->image_ok_primary_slot == BOOT_FLAG_ANY ||
473 table->image_ok_primary_slot == primary_slot.image_ok) &&
474 (table->image_ok_secondary_slot == BOOT_FLAG_ANY ||
475 table->image_ok_secondary_slot == secondary_slot.image_ok) &&
476 (table->copy_done_primary_slot == BOOT_FLAG_ANY ||
477 table->copy_done_primary_slot == primary_slot.copy_done)) {
478 BOOT_LOG_INF("Swap type: %s",
479 table->swap_type == BOOT_SWAP_TYPE_TEST ? "test" :
480 table->swap_type == BOOT_SWAP_TYPE_PERM ? "perm" :
481 table->swap_type == BOOT_SWAP_TYPE_REVERT ? "revert" :
482 "BUG; can't happen");
483 if (table->swap_type != BOOT_SWAP_TYPE_TEST &&
484 table->swap_type != BOOT_SWAP_TYPE_PERM &&
485 table->swap_type != BOOT_SWAP_TYPE_REVERT) {
486 return BOOT_SWAP_TYPE_PANIC;
487 }
488 return table->swap_type;
489 }
490 }
491
492 BOOT_LOG_INF("Swap type: none");
493 return BOOT_SWAP_TYPE_NONE;
494}
495
496/*
497 * This function is not used by the bootloader itself, but its required API
498 * by external tooling like mcumgr.
499 */
500int
501boot_swap_type(void)
502{
503 return boot_swap_type_multi(0);
504}
505
506/**
Sherry Zhangfbeef9b2021-05-12 15:42:30 +0800507 * Marks the image with the given index in the secondary slot as pending. On the
508 * next reboot, the system will perform a one-time boot of the the secondary
509 * slot image.
510 *
511 * @param image_index Image pair index.
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100512 *
513 * @param permanent Whether the image should be used permanently or
Sherry Zhangfbeef9b2021-05-12 15:42:30 +0800514 * only tested once:
515 * 0=run image once, then confirm or revert.
516 * 1=run image forever.
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100517 *
518 * @return 0 on success; nonzero on failure.
519 */
520int
Sherry Zhangfbeef9b2021-05-12 15:42:30 +0800521boot_set_pending_multi(int image_index, int permanent)
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100522{
523 const struct flash_area *fap;
524 struct boot_swap_state state_secondary_slot;
525 uint8_t swap_type;
526 int rc;
527
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000528 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index), &fap);
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100529 if (rc != 0) {
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000530 return BOOT_EFLASH;
531 }
532
533 rc = boot_read_swap_state(fap, &state_secondary_slot);
534 if (rc != 0) {
535 goto done;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100536 }
537
538 switch (state_secondary_slot.magic) {
539 case BOOT_MAGIC_GOOD:
540 /* Swap already scheduled. */
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000541 break;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100542
543 case BOOT_MAGIC_UNSET:
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000544 rc = boot_write_magic(fap);
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100545
546 if (rc == 0 && permanent) {
547 rc = boot_write_image_ok(fap);
548 }
549
550 if (rc == 0) {
551 if (permanent) {
552 swap_type = BOOT_SWAP_TYPE_PERM;
553 } else {
554 swap_type = BOOT_SWAP_TYPE_TEST;
555 }
556 rc = boot_write_swap_info(fap, swap_type, 0);
557 }
558
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000559 break;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100560
561 case BOOT_MAGIC_BAD:
562 /* The image slot is corrupt. There is no way to recover, so erase the
563 * slot to allow future upgrades.
564 */
Dominik Ermel260ae092021-04-23 05:38:45 +0000565 flash_area_erase(fap, 0, flash_area_get_size(fap));
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000566 rc = BOOT_EBADIMAGE;
567 break;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100568
569 default:
570 assert(0);
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000571 rc = BOOT_EBADIMAGE;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100572 }
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000573
574done:
575 flash_area_close(fap);
576 return rc;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100577}
578
579/**
Sherry Zhangfbeef9b2021-05-12 15:42:30 +0800580 * Marks the image with index 0 in the secondary slot as pending. On the next
581 * reboot, the system will perform a one-time boot of the the secondary slot
582 * image. Note that this API is kept for compatibility. The
583 * boot_set_pending_multi() API is recommended.
584 *
585 * @param permanent Whether the image should be used permanently or
586 * only tested once:
587 * 0=run image once, then confirm or revert.
588 * 1=run image forever.
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100589 *
590 * @return 0 on success; nonzero on failure.
591 */
592int
Sherry Zhangfbeef9b2021-05-12 15:42:30 +0800593boot_set_pending(int permanent)
594{
595 return boot_set_pending_multi(0, permanent);
596}
597
598/**
599 * Marks the image with the given index in the primary slot as confirmed. The
600 * system will continue booting into the image in the primary slot until told to
601 * boot from a different slot.
602 *
603 * @param image_index Image pair index.
604 *
605 * @return 0 on success; nonzero on failure.
606 */
607int
608boot_set_confirmed_multi(int image_index)
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100609{
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000610 const struct flash_area *fap = NULL;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100611 struct boot_swap_state state_primary_slot;
612 int rc;
613
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000614 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index), &fap);
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100615 if (rc != 0) {
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000616 return BOOT_EFLASH;
617 }
618
619 rc = boot_read_swap_state(fap, &state_primary_slot);
620 if (rc != 0) {
621 goto done;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100622 }
623
624 switch (state_primary_slot.magic) {
625 case BOOT_MAGIC_GOOD:
626 /* Confirm needed; proceed. */
627 break;
628
629 case BOOT_MAGIC_UNSET:
630 /* Already confirmed. */
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000631 goto done;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100632
633 case BOOT_MAGIC_BAD:
634 /* Unexpected state. */
Dominik Ermel29aed1d2021-05-25 11:59:19 +0000635 rc = BOOT_EBADVECT;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100636 goto done;
637 }
638
Andrzej Puzdrowski22b856b2021-05-07 12:14:31 +0200639 /* Intentionally do not check copy_done flag
640 * so can confirm a padded image which was programed using a programing
641 * interface.
642 */
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100643
644 if (state_primary_slot.image_ok != BOOT_FLAG_UNSET) {
645 /* Already confirmed. */
646 goto done;
647 }
648
649 rc = boot_write_image_ok(fap);
650
651done:
652 flash_area_close(fap);
653 return rc;
654}
Sherry Zhangfbeef9b2021-05-12 15:42:30 +0800655
656/**
657 * Marks the image with index 0 in the primary slot as confirmed. The system
658 * will continue booting into the image in the primary slot until told to boot
659 * from a different slot. Note that this API is kept for compatibility. The
660 * boot_set_confirmed_multi() API is recommended.
661 *
662 * @return 0 on success; nonzero on failure.
663 */
664int
665boot_set_confirmed(void)
666{
667 return boot_set_confirmed_multi(0);
668}