blob: f045fae0d9de63bbe562da1936e246d590ae8b68 [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
Roman Okhrimenko977b3752022-03-31 14:40:48 +03006 * 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"
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +020049#include "swap_status.h"
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +010050#ifdef MCUBOOT_ENC_IMAGES
Andrzej Puzdrowski360763d2021-02-04 18:01:01 +010051#include "bootutil/enc_key_public.h"
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +010052#endif
53
Roman Okhrimenko977b3752022-03-31 14:40:48 +030054#include "bootutil/boot_public_hooks.h"
55
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +010056#ifdef CONFIG_MCUBOOT
Roman Okhrimenko977b3752022-03-31 14:40:48 +030057BOOT_LOG_MODULE_DECLARE(mcuboot);
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +010058#else
Roman Okhrimenko977b3752022-03-31 14:40:48 +030059BOOT_LOG_MODULE_REGISTER(mcuboot_util);
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +010060#endif
61
62const uint32_t boot_img_magic[] = {
63 0xf395c277,
64 0x7fefd260,
65 0x0f505235,
66 0x8079b62c,
67};
68
69#define BOOT_MAGIC_ARR_SZ \
70 (sizeof boot_img_magic / sizeof boot_img_magic[0])
71
72struct boot_swap_table {
73 uint8_t magic_primary_slot;
74 uint8_t magic_secondary_slot;
75 uint8_t image_ok_primary_slot;
76 uint8_t image_ok_secondary_slot;
77 uint8_t copy_done_primary_slot;
78
79 uint8_t swap_type;
80};
81
82/**
83 * This set of tables maps image trailer contents to swap operation type.
84 * When searching for a match, these tables must be iterated sequentially.
85 *
86 * NOTE: the table order is very important. The settings in the secondary
87 * slot always are priority to the primary slot and should be located
88 * earlier in the table.
89 *
90 * The table lists only states where there is action needs to be taken by
91 * the bootloader, as in starting/finishing a swap operation.
92 */
93static const struct boot_swap_table boot_swap_tables[] = {
94 {
95 .magic_primary_slot = BOOT_MAGIC_ANY,
96 .magic_secondary_slot = BOOT_MAGIC_GOOD,
97 .image_ok_primary_slot = BOOT_FLAG_ANY,
98 .image_ok_secondary_slot = BOOT_FLAG_UNSET,
99 .copy_done_primary_slot = BOOT_FLAG_ANY,
100 .swap_type = BOOT_SWAP_TYPE_TEST,
101 },
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_SET,
107 .copy_done_primary_slot = BOOT_FLAG_ANY,
108 .swap_type = BOOT_SWAP_TYPE_PERM,
109 },
110 {
111 .magic_primary_slot = BOOT_MAGIC_GOOD,
112 .magic_secondary_slot = BOOT_MAGIC_UNSET,
113 .image_ok_primary_slot = BOOT_FLAG_UNSET,
114 .image_ok_secondary_slot = BOOT_FLAG_ANY,
115 .copy_done_primary_slot = BOOT_FLAG_SET,
116 .swap_type = BOOT_SWAP_TYPE_REVERT,
117 },
118};
119
120#define BOOT_SWAP_TABLES_COUNT \
121 (sizeof boot_swap_tables / sizeof boot_swap_tables[0])
122
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200123#ifndef MCUBOOT_SWAP_USING_STATUS
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100124static int
125boot_magic_decode(const uint32_t *magic)
126{
127 if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0) {
128 return BOOT_MAGIC_GOOD;
129 }
130 return BOOT_MAGIC_BAD;
131}
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200132#endif /* !MCUBOOT_SWAP_USING_STATUS */
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100133
134static int
135boot_flag_decode(uint8_t flag)
136{
137 if (flag != BOOT_FLAG_SET) {
138 return BOOT_FLAG_BAD;
139 }
140 return BOOT_FLAG_SET;
141}
142
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200143#ifndef MCUBOOT_SWAP_USING_STATUS
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100144static inline uint32_t
145boot_magic_off(const struct flash_area *fap)
146{
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300147 return flash_area_get_size(fap) - BOOT_MAGIC_SZ;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100148}
149
150static inline uint32_t
151boot_image_ok_off(const struct flash_area *fap)
152{
153 return boot_magic_off(fap) - BOOT_MAX_ALIGN;
154}
155
156static inline uint32_t
157boot_copy_done_off(const struct flash_area *fap)
158{
159 return boot_image_ok_off(fap) - BOOT_MAX_ALIGN;
160}
161
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100162uint32_t
163boot_swap_info_off(const struct flash_area *fap)
164{
165 return boot_copy_done_off(fap) - BOOT_MAX_ALIGN;
166}
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200167#endif /* !MCUBOOT_SWAP_USING_STATUS */
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100168
169/**
170 * Determines if a status source table is satisfied by the specified magic
171 * code.
172 *
173 * @param tbl_val A magic field from a status source table.
174 * @param val The magic value in a trailer, encoded as a
175 * BOOT_MAGIC_[...].
176 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300177 * @return true - if the two values are compatible;
178 * false - otherwise.
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100179 */
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300180bool boot_magic_compatible_check(uint8_t tbl_val, uint8_t val)
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100181{
182 switch (tbl_val) {
183 case BOOT_MAGIC_ANY:
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300184 return true;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100185
186 case BOOT_MAGIC_NOTGOOD:
187 return val != BOOT_MAGIC_GOOD;
188
189 default:
190 return tbl_val == val;
191 }
192}
193
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300194bool bootutil_buffer_is_filled(const void *buffer, uint8_t fill, size_t len)
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100195{
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300196 uint8_t *p;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100197
198 if (buffer == NULL || len == 0) {
199 return false;
200 }
201
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300202 for (p = (uint8_t *)buffer; len-- > 0; p++) {
203 if (*p != fill) {
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100204 return false;
205 }
206 }
207
208 return true;
209}
210
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300211bool bootutil_buffer_is_erased(const struct flash_area *area,
212 const void *buffer, size_t len)
213{
214 uint8_t erased_val;
215
216 if (area == NULL) {
217 return false;
218 }
219
220 erased_val = flash_area_erased_val(area);
221
222 return bootutil_buffer_is_filled(buffer, erased_val, len);
223}
224
Andrzej Puzdrowski4700b802020-12-09 14:55:36 +0100225static int
226boot_read_flag(const struct flash_area *fap, uint8_t *flag, uint32_t off)
227{
228 int rc;
229
230 rc = flash_area_read(fap, off, flag, sizeof *flag);
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300231 if (rc != 0) {
Andrzej Puzdrowski4700b802020-12-09 14:55:36 +0100232 return BOOT_EFLASH;
233 }
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300234 if (*flag == flash_area_erased_val(fap)) {
Andrzej Puzdrowski4700b802020-12-09 14:55:36 +0100235 *flag = BOOT_FLAG_UNSET;
236 } else {
237 *flag = boot_flag_decode(*flag);
238 }
239
240 return 0;
241}
242
243static inline int
244boot_read_copy_done(const struct flash_area *fap, uint8_t *copy_done)
245{
246 return boot_read_flag(fap, copy_done, boot_copy_done_off(fap));
247}
248
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200249#ifndef MCUBOOT_SWAP_USING_STATUS
Andrzej Puzdrowski4700b802020-12-09 14:55:36 +0100250
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100251int
252boot_read_swap_state(const struct flash_area *fap,
253 struct boot_swap_state *state)
254{
255 uint32_t magic[BOOT_MAGIC_ARR_SZ];
256 uint32_t off;
257 uint8_t swap_info;
258 int rc;
259
260 off = boot_magic_off(fap);
261 rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ);
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300262 if (rc != 0) {
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100263 return BOOT_EFLASH;
264 }
265 if (bootutil_buffer_is_erased(fap, magic, BOOT_MAGIC_SZ)) {
266 state->magic = BOOT_MAGIC_UNSET;
267 } else {
268 state->magic = boot_magic_decode(magic);
269 }
270
271 off = boot_swap_info_off(fap);
272 rc = flash_area_read(fap, off, &swap_info, sizeof swap_info);
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300273 if (rc != 0) {
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100274 return BOOT_EFLASH;
275 }
276
277 /* Extract the swap type and image number */
278 state->swap_type = BOOT_GET_SWAP_TYPE(swap_info);
279 state->image_num = BOOT_GET_IMAGE_NUM(swap_info);
280
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300281 if (swap_info == flash_area_erased_val(fap) ||
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100282 state->swap_type > BOOT_SWAP_TYPE_REVERT) {
283 state->swap_type = BOOT_SWAP_TYPE_NONE;
284 state->image_num = 0;
285 }
286
Andrzej Puzdrowski4700b802020-12-09 14:55:36 +0100287 rc = boot_read_copy_done(fap, &state->copy_done);
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300288 if (rc != 0) {
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100289 return BOOT_EFLASH;
290 }
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100291
Andrzej Puzdrowski4700b802020-12-09 14:55:36 +0100292 return boot_read_image_ok(fap, &state->image_ok);
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100293}
294
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200295#endif /* !MCUBOOT_SWAP_USING_STATUS */
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{
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300300 const struct flash_area *fap = NULL;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100301 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
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200313#ifndef MCUBOOT_SWAP_USING_STATUS
314
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100315int
316boot_write_magic(const struct flash_area *fap)
317{
318 uint32_t off;
319 int rc;
320
321 off = boot_magic_off(fap);
322
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300323 BOOT_LOG_DBG("writing magic; fa_id=%u off=0x%" PRIx32
324 " (0x%" PRIx32 ")", (unsigned)flash_area_get_id(fap),
325 off, 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];
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300344 size_t align;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100345 uint8_t erased_val;
346 int rc;
347
348 align = flash_area_align(fap);
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300349 if (align == 0u) {
350 return BOOT_EFLASH;
351 }
Dominik Ermel48281622021-03-24 18:04:54 +0000352 align = (inlen + align - 1) & ~(align - 1);
353 if (align > BOOT_MAX_ALIGN) {
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100354 return -1;
355 }
356 erased_val = flash_area_erased_val(fap);
Dominik Ermel48281622021-03-24 18:04:54 +0000357
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100358 memcpy(buf, inbuf, inlen);
359 memset(&buf[inlen], erased_val, align - inlen);
360
361 rc = flash_area_write(fap, off, buf, align);
362 if (rc != 0) {
363 return BOOT_EFLASH;
364 }
365
366 return 0;
367}
368
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200369#endif /* !MCUBOOT_SWAP_USING_STATUS */
370
Dominik Ermela7f9e9f2021-03-24 17:31:57 +0000371int
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100372boot_write_trailer_flag(const struct flash_area *fap, uint32_t off,
373 uint8_t flag_val)
374{
375 const uint8_t buf[1] = { flag_val };
376 return boot_write_trailer(fap, off, buf, 1);
377}
378
379int
380boot_write_image_ok(const struct flash_area *fap)
381{
382 uint32_t off;
383
384 off = boot_image_ok_off(fap);
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300385 BOOT_LOG_DBG("writing image_ok; fa_id=%u off=0x%" PRIx32
386 " (0x%" PRIx32 ")", (unsigned)flash_area_get_id(fap),
387 off, flash_area_get_off(fap) + off);
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100388 return boot_write_trailer_flag(fap, off, BOOT_FLAG_SET);
389}
390
Andrzej Puzdrowski4700b802020-12-09 14:55:36 +0100391int
392boot_read_image_ok(const struct flash_area *fap, uint8_t *image_ok)
393{
394 return boot_read_flag(fap, image_ok, boot_image_ok_off(fap));
395}
396
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100397/**
398 * Writes the specified value to the `swap-type` field of an image trailer.
399 * This value is persisted so that the boot loader knows what swap operation to
400 * resume in case of an unexpected reset.
401 */
402int
403boot_write_swap_info(const struct flash_area *fap, uint8_t swap_type,
404 uint8_t image_num)
405{
406 uint32_t off;
407 uint8_t swap_info;
408
409 BOOT_SET_SWAP_INFO(swap_info, image_num, swap_type);
410 off = boot_swap_info_off(fap);
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300411 BOOT_LOG_DBG("writing swap_info; fa_id=%u off=0x%" PRIx32
412 " (0x%" PRIx32 "), swap_type=0x%x image_num=0x%x",
413 (unsigned)flash_area_get_id(fap), off,
414 flash_area_get_off(fap) + off,
415 (unsigned)swap_type, (unsigned)image_num);
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100416 return boot_write_trailer(fap, off, (const uint8_t *) &swap_info, 1);
417}
418
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300419#define BOOT_LOG_SWAP_STATE(area, state) \
420 BOOT_LOG_INF("%s: magic=%s, swap_type=0x%x, copy_done=0x%x, image_ok=0x%x", \
421 (area), \
422 ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \
423 (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \
424 "bad"), \
425 (unsigned)(state)->swap_type, \
426 (unsigned)(state)->copy_done, \
427 (unsigned)(state)->image_ok)
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200428
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100429int
430boot_swap_type_multi(int image_index)
431{
432 const struct boot_swap_table *table;
433 struct boot_swap_state primary_slot;
434 struct boot_swap_state secondary_slot;
435 int rc;
436 size_t i;
437
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300438 rc = BOOT_HOOK_CALL(boot_read_swap_state_primary_slot_hook,
439 BOOT_HOOK_REGULAR, image_index, &primary_slot);
440 if (rc == BOOT_HOOK_REGULAR)
441 {
442 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(image_index),
443 &primary_slot);
444 }
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100445 if (rc) {
446 return BOOT_SWAP_TYPE_PANIC;
447 }
448
449 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(image_index),
450 &secondary_slot);
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300451 if (rc == BOOT_EFLASH) {
452 BOOT_LOG_INF("Secondary image of image pair (%d.) "
453 "is unreachable. Treat it as empty", image_index);
454 secondary_slot.magic = BOOT_MAGIC_UNSET;
455 secondary_slot.swap_type = BOOT_SWAP_TYPE_NONE;
456 secondary_slot.copy_done = BOOT_FLAG_UNSET;
457 secondary_slot.image_ok = BOOT_FLAG_UNSET;
458 secondary_slot.image_num = 0;
459 } else if (rc) {
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100460 return BOOT_SWAP_TYPE_PANIC;
461 }
462
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200463 BOOT_LOG_SWAP_STATE("boot_swap_type_multi: Primary image", &primary_slot);
464 BOOT_LOG_SWAP_STATE("boot_swap_type_multi: Secondary image", &secondary_slot);
465
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100466 for (i = 0; i < BOOT_SWAP_TABLES_COUNT; i++) {
467 table = boot_swap_tables + i;
468
469 if (boot_magic_compatible_check(table->magic_primary_slot,
470 primary_slot.magic) &&
471 boot_magic_compatible_check(table->magic_secondary_slot,
472 secondary_slot.magic) &&
473 (table->image_ok_primary_slot == BOOT_FLAG_ANY ||
474 table->image_ok_primary_slot == primary_slot.image_ok) &&
475 (table->image_ok_secondary_slot == BOOT_FLAG_ANY ||
476 table->image_ok_secondary_slot == secondary_slot.image_ok) &&
477 (table->copy_done_primary_slot == BOOT_FLAG_ANY ||
478 table->copy_done_primary_slot == primary_slot.copy_done)) {
479 BOOT_LOG_INF("Swap type: %s",
480 table->swap_type == BOOT_SWAP_TYPE_TEST ? "test" :
481 table->swap_type == BOOT_SWAP_TYPE_PERM ? "perm" :
482 table->swap_type == BOOT_SWAP_TYPE_REVERT ? "revert" :
483 "BUG; can't happen");
484 if (table->swap_type != BOOT_SWAP_TYPE_TEST &&
485 table->swap_type != BOOT_SWAP_TYPE_PERM &&
486 table->swap_type != BOOT_SWAP_TYPE_REVERT) {
487 return BOOT_SWAP_TYPE_PANIC;
488 }
489 return table->swap_type;
490 }
491 }
492
493 BOOT_LOG_INF("Swap type: none");
494 return BOOT_SWAP_TYPE_NONE;
495}
496
497/*
498 * This function is not used by the bootloader itself, but its required API
499 * by external tooling like mcumgr.
500 */
501int
502boot_swap_type(void)
503{
504 return boot_swap_type_multi(0);
505}
506
507/**
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300508 * Marks the image with the given index in the secondary slot as pending. On the
509 * next reboot, the system will perform a one-time boot of the the secondary
510 * slot image.
511 *
512 * @param image_index Image pair index.
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100513 *
514 * @param permanent Whether the image should be used permanently or
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300515 * only tested once:
516 * 0=run image once, then confirm or revert.
517 * 1=run image forever.
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100518 *
519 * @return 0 on success; nonzero on failure.
520 */
521int
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300522boot_set_pending_multi(int image_index, int permanent)
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100523{
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300524 const struct flash_area *fap = NULL;
525 struct boot_swap_state state_secondary_slot = {0};
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100526 uint8_t swap_type;
527 int rc;
528
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300529 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index), &fap);
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100530 if (rc != 0) {
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300531 return BOOT_EFLASH;
532 }
533
534 rc = boot_read_swap_state(fap, &state_secondary_slot);
535 if (rc != 0) {
536 goto done;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100537 }
538
539 switch (state_secondary_slot.magic) {
540 case BOOT_MAGIC_GOOD:
541 /* Swap already scheduled. */
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300542 break;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100543
544 case BOOT_MAGIC_UNSET:
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300545 rc = boot_write_magic(fap);
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100546
547 if (rc == 0 && permanent) {
548 rc = boot_write_image_ok(fap);
549 }
550
551 if (rc == 0) {
552 if (permanent) {
553 swap_type = BOOT_SWAP_TYPE_PERM;
554 } else {
555 swap_type = BOOT_SWAP_TYPE_TEST;
556 }
557 rc = boot_write_swap_info(fap, swap_type, 0);
558 }
559
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300560 break;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100561
562 case BOOT_MAGIC_BAD:
563 /* The image slot is corrupt. There is no way to recover, so erase the
564 * slot to allow future upgrades.
565 */
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300566 flash_area_erase(fap, 0, flash_area_get_size(fap));
567 rc = BOOT_EBADIMAGE;
568 break;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100569
570 default:
571 assert(0);
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300572 rc = BOOT_EBADIMAGE;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100573 }
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300574
575done:
576 flash_area_close(fap);
577 return rc;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100578}
579
580/**
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300581 * Marks the image with index 0 in the secondary slot as pending. On the next
582 * reboot, the system will perform a one-time boot of the the secondary slot
583 * image. Note that this API is kept for compatibility. The
584 * boot_set_pending_multi() API is recommended.
585 *
586 * @param permanent Whether the image should be used permanently or
587 * only tested once:
588 * 0=run image once, then confirm or revert.
589 * 1=run image forever.
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100590 *
591 * @return 0 on success; nonzero on failure.
592 */
593int
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300594boot_set_pending(int permanent)
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100595{
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300596 return boot_set_pending_multi(0, permanent);
597}
598
599/**
600 * Marks the image with the given index in the primary slot as confirmed. The
601 * system will continue booting into the image in the primary slot until told to
602 * boot from a different slot.
603 *
604 * @param image_index Image pair index.
605 *
606 * @return 0 on success; nonzero on failure.
607 */
608int
609boot_set_confirmed_multi(int image_index)
610{
611 const struct flash_area *fap = NULL;
612 struct boot_swap_state state_primary_slot = {0};
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100613 int rc;
614
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300615 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index), &fap);
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100616 if (rc != 0) {
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300617 return BOOT_EFLASH;
618 }
619
620 rc = boot_read_swap_state(fap, &state_primary_slot);
621 if (rc != 0) {
622 goto done;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100623 }
624
625 switch (state_primary_slot.magic) {
626 case BOOT_MAGIC_GOOD:
627 /* Confirm needed; proceed. */
628 break;
629
630 case BOOT_MAGIC_UNSET:
631 /* Already confirmed. */
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300632 goto done;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100633
634 case BOOT_MAGIC_BAD:
635 /* Unexpected state. */
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100636 rc = BOOT_EBADVECT;
637 goto done;
638 }
639
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300640 /* Intentionally do not check copy_done flag
641 * so can confirm a padded image which was programed using a programing
642 * interface.
643 */
644
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100645 if (state_primary_slot.image_ok != BOOT_FLAG_UNSET) {
646 /* Already confirmed. */
647 goto done;
648 }
649
650 rc = boot_write_image_ok(fap);
651
652done:
653 flash_area_close(fap);
654 return rc;
655}
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300656
657/**
658 * Marks the image with index 0 in the primary slot as confirmed. The system
659 * will continue booting into the image in the primary slot until told to boot
660 * from a different slot. Note that this API is kept for compatibility. The
661 * boot_set_confirmed_multi() API is recommended.
662 *
663 * @return 0 on success; nonzero on failure.
664 */
665int
666boot_set_confirmed(void)
667{
668 return boot_set_confirmed_multi(0);
669}