blob: b1b4f601dffa17cb15b536c7c6e88873e162d286 [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
Dovhal Artem (CSUKR CSS ICW SW FW 1)f7a3d1b2022-04-01 15:07:37 +0000243#ifndef MCUBOOT_SWAP_USING_STATUS
244
Andrzej Puzdrowski4700b802020-12-09 14:55:36 +0100245static inline int
246boot_read_copy_done(const struct flash_area *fap, uint8_t *copy_done)
247{
248 return boot_read_flag(fap, copy_done, boot_copy_done_off(fap));
249}
250
251
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100252int
253boot_read_swap_state(const struct flash_area *fap,
254 struct boot_swap_state *state)
255{
256 uint32_t magic[BOOT_MAGIC_ARR_SZ];
257 uint32_t off;
258 uint8_t swap_info;
259 int rc;
260
261 off = boot_magic_off(fap);
262 rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ);
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300263 if (rc != 0) {
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100264 return BOOT_EFLASH;
265 }
266 if (bootutil_buffer_is_erased(fap, magic, BOOT_MAGIC_SZ)) {
267 state->magic = BOOT_MAGIC_UNSET;
268 } else {
269 state->magic = boot_magic_decode(magic);
270 }
271
272 off = boot_swap_info_off(fap);
273 rc = flash_area_read(fap, off, &swap_info, sizeof swap_info);
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300274 if (rc != 0) {
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100275 return BOOT_EFLASH;
276 }
277
278 /* Extract the swap type and image number */
279 state->swap_type = BOOT_GET_SWAP_TYPE(swap_info);
280 state->image_num = BOOT_GET_IMAGE_NUM(swap_info);
281
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300282 if (swap_info == flash_area_erased_val(fap) ||
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100283 state->swap_type > BOOT_SWAP_TYPE_REVERT) {
284 state->swap_type = BOOT_SWAP_TYPE_NONE;
285 state->image_num = 0;
286 }
287
Andrzej Puzdrowski4700b802020-12-09 14:55:36 +0100288 rc = boot_read_copy_done(fap, &state->copy_done);
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300289 if (rc != 0) {
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100290 return BOOT_EFLASH;
291 }
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100292
Andrzej Puzdrowski4700b802020-12-09 14:55:36 +0100293 return boot_read_image_ok(fap, &state->image_ok);
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100294}
295
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200296#endif /* !MCUBOOT_SWAP_USING_STATUS */
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{
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300301 const struct flash_area *fap = NULL;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100302 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
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200314#ifndef MCUBOOT_SWAP_USING_STATUS
315
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100316int
317boot_write_magic(const struct flash_area *fap)
318{
319 uint32_t off;
320 int rc;
321
322 off = boot_magic_off(fap);
323
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300324 BOOT_LOG_DBG("writing magic; fa_id=%u off=0x%" PRIx32
325 " (0x%" PRIx32 ")", (unsigned)flash_area_get_id(fap),
326 off, flash_area_get_off(fap) + off);
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100327 rc = flash_area_write(fap, off, boot_img_magic, BOOT_MAGIC_SZ);
328 if (rc != 0) {
329 return BOOT_EFLASH;
330 }
331
332 return 0;
333}
334
335/**
336 * Write trailer data; status bytes, swap_size, etc
337 *
338 * @returns 0 on success, != 0 on error.
339 */
Dominik Ermela7f9e9f2021-03-24 17:31:57 +0000340int
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100341boot_write_trailer(const struct flash_area *fap, uint32_t off,
342 const uint8_t *inbuf, uint8_t inlen)
343{
344 uint8_t buf[BOOT_MAX_ALIGN];
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300345 size_t align;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100346 uint8_t erased_val;
347 int rc;
348
349 align = flash_area_align(fap);
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300350 if (align == 0u) {
351 return BOOT_EFLASH;
352 }
Dominik Ermel48281622021-03-24 18:04:54 +0000353 align = (inlen + align - 1) & ~(align - 1);
354 if (align > BOOT_MAX_ALIGN) {
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100355 return -1;
356 }
357 erased_val = flash_area_erased_val(fap);
Dominik Ermel48281622021-03-24 18:04:54 +0000358
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100359 memcpy(buf, inbuf, inlen);
360 memset(&buf[inlen], erased_val, align - inlen);
361
362 rc = flash_area_write(fap, off, buf, align);
363 if (rc != 0) {
364 return BOOT_EFLASH;
365 }
366
367 return 0;
368}
369
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200370#endif /* !MCUBOOT_SWAP_USING_STATUS */
371
Dominik Ermela7f9e9f2021-03-24 17:31:57 +0000372int
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100373boot_write_trailer_flag(const struct flash_area *fap, uint32_t off,
374 uint8_t flag_val)
375{
376 const uint8_t buf[1] = { flag_val };
377 return boot_write_trailer(fap, off, buf, 1);
378}
379
380int
381boot_write_image_ok(const struct flash_area *fap)
382{
383 uint32_t off;
384
385 off = boot_image_ok_off(fap);
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300386 BOOT_LOG_DBG("writing image_ok; fa_id=%u off=0x%" PRIx32
387 " (0x%" PRIx32 ")", (unsigned)flash_area_get_id(fap),
388 off, flash_area_get_off(fap) + off);
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100389 return boot_write_trailer_flag(fap, off, BOOT_FLAG_SET);
390}
391
Andrzej Puzdrowski4700b802020-12-09 14:55:36 +0100392int
393boot_read_image_ok(const struct flash_area *fap, uint8_t *image_ok)
394{
395 return boot_read_flag(fap, image_ok, boot_image_ok_off(fap));
396}
397
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100398/**
399 * Writes the specified value to the `swap-type` field of an image trailer.
400 * This value is persisted so that the boot loader knows what swap operation to
401 * resume in case of an unexpected reset.
402 */
403int
404boot_write_swap_info(const struct flash_area *fap, uint8_t swap_type,
405 uint8_t image_num)
406{
407 uint32_t off;
408 uint8_t swap_info;
409
410 BOOT_SET_SWAP_INFO(swap_info, image_num, swap_type);
411 off = boot_swap_info_off(fap);
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300412 BOOT_LOG_DBG("writing swap_info; fa_id=%u off=0x%" PRIx32
413 " (0x%" PRIx32 "), swap_type=0x%x image_num=0x%x",
414 (unsigned)flash_area_get_id(fap), off,
415 flash_area_get_off(fap) + off,
416 (unsigned)swap_type, (unsigned)image_num);
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100417 return boot_write_trailer(fap, off, (const uint8_t *) &swap_info, 1);
418}
419
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300420#define BOOT_LOG_SWAP_STATE(area, state) \
421 BOOT_LOG_INF("%s: magic=%s, swap_type=0x%x, copy_done=0x%x, image_ok=0x%x", \
422 (area), \
423 ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \
424 (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \
425 "bad"), \
426 (unsigned)(state)->swap_type, \
427 (unsigned)(state)->copy_done, \
428 (unsigned)(state)->image_ok)
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200429
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100430int
431boot_swap_type_multi(int image_index)
432{
Dovhal Artem (CSUKR CSS ICW SW FW 1)f7a3d1b2022-04-01 15:07:37 +0000433 const struct boot_swap_table *table = NULL;
434 struct boot_swap_state primary_slot = {0};
435 struct boot_swap_state secondary_slot = {0};
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100436 int rc;
437 size_t i;
438
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300439 rc = BOOT_HOOK_CALL(boot_read_swap_state_primary_slot_hook,
440 BOOT_HOOK_REGULAR, image_index, &primary_slot);
441 if (rc == BOOT_HOOK_REGULAR)
442 {
443 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(image_index),
444 &primary_slot);
445 }
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100446 if (rc) {
447 return BOOT_SWAP_TYPE_PANIC;
448 }
449
450 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(image_index),
451 &secondary_slot);
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300452 if (rc == BOOT_EFLASH) {
453 BOOT_LOG_INF("Secondary image of image pair (%d.) "
454 "is unreachable. Treat it as empty", image_index);
455 secondary_slot.magic = BOOT_MAGIC_UNSET;
456 secondary_slot.swap_type = BOOT_SWAP_TYPE_NONE;
457 secondary_slot.copy_done = BOOT_FLAG_UNSET;
458 secondary_slot.image_ok = BOOT_FLAG_UNSET;
459 secondary_slot.image_num = 0;
460 } else if (rc) {
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100461 return BOOT_SWAP_TYPE_PANIC;
462 }
463
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200464 BOOT_LOG_SWAP_STATE("boot_swap_type_multi: Primary image", &primary_slot);
465 BOOT_LOG_SWAP_STATE("boot_swap_type_multi: Secondary image", &secondary_slot);
466
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100467 for (i = 0; i < BOOT_SWAP_TABLES_COUNT; i++) {
468 table = boot_swap_tables + i;
469
470 if (boot_magic_compatible_check(table->magic_primary_slot,
471 primary_slot.magic) &&
472 boot_magic_compatible_check(table->magic_secondary_slot,
473 secondary_slot.magic) &&
474 (table->image_ok_primary_slot == BOOT_FLAG_ANY ||
475 table->image_ok_primary_slot == primary_slot.image_ok) &&
476 (table->image_ok_secondary_slot == BOOT_FLAG_ANY ||
477 table->image_ok_secondary_slot == secondary_slot.image_ok) &&
478 (table->copy_done_primary_slot == BOOT_FLAG_ANY ||
479 table->copy_done_primary_slot == primary_slot.copy_done)) {
480 BOOT_LOG_INF("Swap type: %s",
481 table->swap_type == BOOT_SWAP_TYPE_TEST ? "test" :
482 table->swap_type == BOOT_SWAP_TYPE_PERM ? "perm" :
483 table->swap_type == BOOT_SWAP_TYPE_REVERT ? "revert" :
484 "BUG; can't happen");
485 if (table->swap_type != BOOT_SWAP_TYPE_TEST &&
486 table->swap_type != BOOT_SWAP_TYPE_PERM &&
487 table->swap_type != BOOT_SWAP_TYPE_REVERT) {
488 return BOOT_SWAP_TYPE_PANIC;
489 }
490 return table->swap_type;
491 }
492 }
493
494 BOOT_LOG_INF("Swap type: none");
495 return BOOT_SWAP_TYPE_NONE;
496}
497
498/*
499 * This function is not used by the bootloader itself, but its required API
500 * by external tooling like mcumgr.
501 */
502int
503boot_swap_type(void)
504{
505 return boot_swap_type_multi(0);
506}
507
508/**
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300509 * Marks the image with the given index in the secondary slot as pending. On the
510 * next reboot, the system will perform a one-time boot of the the secondary
511 * slot image.
512 *
513 * @param image_index Image pair index.
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100514 *
515 * @param permanent Whether the image should be used permanently or
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300516 * only tested once:
517 * 0=run image once, then confirm or revert.
518 * 1=run image forever.
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100519 *
520 * @return 0 on success; nonzero on failure.
521 */
522int
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300523boot_set_pending_multi(int image_index, int permanent)
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100524{
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300525 const struct flash_area *fap = NULL;
526 struct boot_swap_state state_secondary_slot = {0};
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100527 uint8_t swap_type;
528 int rc;
529
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300530 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index), &fap);
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100531 if (rc != 0) {
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300532 return BOOT_EFLASH;
533 }
534
535 rc = boot_read_swap_state(fap, &state_secondary_slot);
536 if (rc != 0) {
537 goto done;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100538 }
539
540 switch (state_secondary_slot.magic) {
541 case BOOT_MAGIC_GOOD:
542 /* Swap already scheduled. */
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300543 break;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100544
545 case BOOT_MAGIC_UNSET:
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300546 rc = boot_write_magic(fap);
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100547
548 if (rc == 0 && permanent) {
549 rc = boot_write_image_ok(fap);
550 }
551
552 if (rc == 0) {
553 if (permanent) {
554 swap_type = BOOT_SWAP_TYPE_PERM;
555 } else {
556 swap_type = BOOT_SWAP_TYPE_TEST;
557 }
558 rc = boot_write_swap_info(fap, swap_type, 0);
559 }
560
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300561 break;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100562
563 case BOOT_MAGIC_BAD:
564 /* The image slot is corrupt. There is no way to recover, so erase the
565 * slot to allow future upgrades.
566 */
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300567 flash_area_erase(fap, 0, flash_area_get_size(fap));
568 rc = BOOT_EBADIMAGE;
569 break;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100570
571 default:
572 assert(0);
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300573 rc = BOOT_EBADIMAGE;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100574 }
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300575
576done:
577 flash_area_close(fap);
578 return rc;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100579}
580
581/**
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300582 * Marks the image with index 0 in the secondary slot as pending. On the next
583 * reboot, the system will perform a one-time boot of the the secondary slot
584 * image. Note that this API is kept for compatibility. The
585 * boot_set_pending_multi() API is recommended.
586 *
587 * @param permanent Whether the image should be used permanently or
588 * only tested once:
589 * 0=run image once, then confirm or revert.
590 * 1=run image forever.
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100591 *
592 * @return 0 on success; nonzero on failure.
593 */
594int
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300595boot_set_pending(int permanent)
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100596{
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300597 return boot_set_pending_multi(0, permanent);
598}
599
600/**
601 * Marks the image with the given index in the primary slot as confirmed. The
602 * system will continue booting into the image in the primary slot until told to
603 * boot from a different slot.
604 *
605 * @param image_index Image pair index.
606 *
607 * @return 0 on success; nonzero on failure.
608 */
609int
610boot_set_confirmed_multi(int image_index)
611{
612 const struct flash_area *fap = NULL;
613 struct boot_swap_state state_primary_slot = {0};
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100614 int rc;
615
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300616 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index), &fap);
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100617 if (rc != 0) {
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300618 return BOOT_EFLASH;
619 }
620
621 rc = boot_read_swap_state(fap, &state_primary_slot);
622 if (rc != 0) {
623 goto done;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100624 }
625
626 switch (state_primary_slot.magic) {
627 case BOOT_MAGIC_GOOD:
628 /* Confirm needed; proceed. */
629 break;
630
631 case BOOT_MAGIC_UNSET:
632 /* Already confirmed. */
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300633 goto done;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100634
635 case BOOT_MAGIC_BAD:
636 /* Unexpected state. */
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100637 rc = BOOT_EBADVECT;
638 goto done;
639 }
640
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300641 /* Intentionally do not check copy_done flag
642 * so can confirm a padded image which was programed using a programing
643 * interface.
644 */
645
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100646 if (state_primary_slot.image_ok != BOOT_FLAG_UNSET) {
647 /* Already confirmed. */
648 goto done;
649 }
650
651 rc = boot_write_image_ok(fap);
652
653done:
654 flash_area_close(fap);
655 return rc;
656}
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300657
658/**
659 * Marks the image with index 0 in the primary slot as confirmed. The system
660 * will continue booting into the image in the primary slot until told to boot
661 * from a different slot. Note that this API is kept for compatibility. The
662 * boot_set_confirmed_multi() API is recommended.
663 *
664 * @return 0 on success; nonzero on failure.
665 */
666int
667boot_set_confirmed(void)
668{
669 return boot_set_confirmed_multi(0);
670}