blob: 82763d20318d6229cdce03047296870bad65ccae [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"
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +030055#include "bootutil_priv.h"
Roman Okhrimenko977b3752022-03-31 14:40:48 +030056
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +010057#ifdef CONFIG_MCUBOOT
Roman Okhrimenko977b3752022-03-31 14:40:48 +030058BOOT_LOG_MODULE_DECLARE(mcuboot);
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +010059#else
Roman Okhrimenko977b3752022-03-31 14:40:48 +030060BOOT_LOG_MODULE_REGISTER(mcuboot_util);
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +010061#endif
62
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +030063#if BOOT_MAX_ALIGN == 8
64const union boot_img_magic_t boot_img_magic = {
65 .val = {
66 0x77, 0xc2, 0x95, 0xf3,
67 0x60, 0xd2, 0xef, 0x7f,
68 0x35, 0x52, 0x50, 0x0f,
69 0x2c, 0xb6, 0x79, 0x80
70 }
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +010071};
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +030072#else
73const union boot_img_magic_t boot_img_magic = {
74 .align = BOOT_MAX_ALIGN,
75 .magic = {
76 0x2d, 0xe1,
77 0x5d, 0x29, 0x41, 0x0b,
78 0x8d, 0x77, 0x67, 0x9c,
79 0x11, 0x0f, 0x1f, 0x8a
80 }
81};
82#endif
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +010083
84struct boot_swap_table {
85 uint8_t magic_primary_slot;
86 uint8_t magic_secondary_slot;
87 uint8_t image_ok_primary_slot;
88 uint8_t image_ok_secondary_slot;
89 uint8_t copy_done_primary_slot;
90
91 uint8_t swap_type;
92};
93
94/**
95 * This set of tables maps image trailer contents to swap operation type.
96 * When searching for a match, these tables must be iterated sequentially.
97 *
98 * NOTE: the table order is very important. The settings in the secondary
99 * slot always are priority to the primary slot and should be located
100 * earlier in the table.
101 *
102 * The table lists only states where there is action needs to be taken by
103 * the bootloader, as in starting/finishing a swap operation.
104 */
105static const struct boot_swap_table boot_swap_tables[] = {
106 {
107 .magic_primary_slot = BOOT_MAGIC_ANY,
108 .magic_secondary_slot = BOOT_MAGIC_GOOD,
109 .image_ok_primary_slot = BOOT_FLAG_ANY,
110 .image_ok_secondary_slot = BOOT_FLAG_UNSET,
111 .copy_done_primary_slot = BOOT_FLAG_ANY,
112 .swap_type = BOOT_SWAP_TYPE_TEST,
113 },
114 {
115 .magic_primary_slot = BOOT_MAGIC_ANY,
116 .magic_secondary_slot = BOOT_MAGIC_GOOD,
117 .image_ok_primary_slot = BOOT_FLAG_ANY,
118 .image_ok_secondary_slot = BOOT_FLAG_SET,
119 .copy_done_primary_slot = BOOT_FLAG_ANY,
120 .swap_type = BOOT_SWAP_TYPE_PERM,
121 },
122 {
123 .magic_primary_slot = BOOT_MAGIC_GOOD,
124 .magic_secondary_slot = BOOT_MAGIC_UNSET,
125 .image_ok_primary_slot = BOOT_FLAG_UNSET,
126 .image_ok_secondary_slot = BOOT_FLAG_ANY,
127 .copy_done_primary_slot = BOOT_FLAG_SET,
128 .swap_type = BOOT_SWAP_TYPE_REVERT,
129 },
130};
131
132#define BOOT_SWAP_TABLES_COUNT \
133 (sizeof boot_swap_tables / sizeof boot_swap_tables[0])
134
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200135#ifndef MCUBOOT_SWAP_USING_STATUS
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100136static int
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300137boot_magic_decode(const uint8_t *magic)
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100138{
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300139 if (memcmp(magic, BOOT_IMG_MAGIC, BOOT_MAGIC_SZ) == 0) {
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100140 return BOOT_MAGIC_GOOD;
141 }
142 return BOOT_MAGIC_BAD;
143}
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200144#endif /* !MCUBOOT_SWAP_USING_STATUS */
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100145
146static int
147boot_flag_decode(uint8_t flag)
148{
149 if (flag != BOOT_FLAG_SET) {
150 return BOOT_FLAG_BAD;
151 }
152 return BOOT_FLAG_SET;
153}
154
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200155#ifndef MCUBOOT_SWAP_USING_STATUS
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100156static inline uint32_t
157boot_magic_off(const struct flash_area *fap)
158{
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300159 return flash_area_get_size(fap) - BOOT_MAGIC_SZ;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100160}
161
162static inline uint32_t
163boot_image_ok_off(const struct flash_area *fap)
164{
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300165 return ALIGN_DOWN(boot_magic_off(fap) - BOOT_MAX_ALIGN, BOOT_MAX_ALIGN);
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100166}
167
168static inline uint32_t
169boot_copy_done_off(const struct flash_area *fap)
170{
171 return boot_image_ok_off(fap) - BOOT_MAX_ALIGN;
172}
173
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100174uint32_t
175boot_swap_info_off(const struct flash_area *fap)
176{
177 return boot_copy_done_off(fap) - BOOT_MAX_ALIGN;
178}
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200179#endif /* !MCUBOOT_SWAP_USING_STATUS */
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100180
181/**
182 * Determines if a status source table is satisfied by the specified magic
183 * code.
184 *
185 * @param tbl_val A magic field from a status source table.
186 * @param val The magic value in a trailer, encoded as a
187 * BOOT_MAGIC_[...].
188 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300189 * @return true - if the two values are compatible;
190 * false - otherwise.
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100191 */
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300192bool boot_magic_compatible_check(uint8_t tbl_val, uint8_t val)
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100193{
194 switch (tbl_val) {
195 case BOOT_MAGIC_ANY:
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300196 return true;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100197
198 case BOOT_MAGIC_NOTGOOD:
199 return val != BOOT_MAGIC_GOOD;
200
201 default:
202 return tbl_val == val;
203 }
204}
205
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300206bool bootutil_buffer_is_filled(const void *buffer, uint8_t fill, size_t len)
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100207{
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300208 uint8_t *p;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100209
210 if (buffer == NULL || len == 0) {
211 return false;
212 }
213
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300214 for (p = (uint8_t *)buffer; len-- > 0; p++) {
215 if (*p != fill) {
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100216 return false;
217 }
218 }
219
220 return true;
221}
222
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300223bool bootutil_buffer_is_erased(const struct flash_area *area,
224 const void *buffer, size_t len)
225{
226 uint8_t erased_val;
227
228 if (area == NULL) {
229 return false;
230 }
231
232 erased_val = flash_area_erased_val(area);
233
234 return bootutil_buffer_is_filled(buffer, erased_val, len);
235}
236
Andrzej Puzdrowski4700b802020-12-09 14:55:36 +0100237static int
238boot_read_flag(const struct flash_area *fap, uint8_t *flag, uint32_t off)
239{
240 int rc;
241
242 rc = flash_area_read(fap, off, flag, sizeof *flag);
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300243 if (rc != 0) {
Andrzej Puzdrowski4700b802020-12-09 14:55:36 +0100244 return BOOT_EFLASH;
245 }
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300246 if (*flag == flash_area_erased_val(fap)) {
Andrzej Puzdrowski4700b802020-12-09 14:55:36 +0100247 *flag = BOOT_FLAG_UNSET;
248 } else {
249 *flag = boot_flag_decode(*flag);
250 }
251
252 return 0;
253}
254
Dovhal Artem (CSUKR CSS ICW SW FW 1)f7a3d1b2022-04-01 15:07:37 +0000255#ifndef MCUBOOT_SWAP_USING_STATUS
256
Andrzej Puzdrowski4700b802020-12-09 14:55:36 +0100257static inline int
258boot_read_copy_done(const struct flash_area *fap, uint8_t *copy_done)
259{
260 return boot_read_flag(fap, copy_done, boot_copy_done_off(fap));
261}
262
263
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100264int
265boot_read_swap_state(const struct flash_area *fap,
266 struct boot_swap_state *state)
267{
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300268 uint8_t magic[BOOT_MAGIC_SZ];
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100269 uint32_t off;
270 uint8_t swap_info;
271 int rc;
272
273 off = boot_magic_off(fap);
274 rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ);
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300275 if (rc != 0) {
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100276 return BOOT_EFLASH;
277 }
278 if (bootutil_buffer_is_erased(fap, magic, BOOT_MAGIC_SZ)) {
279 state->magic = BOOT_MAGIC_UNSET;
280 } else {
281 state->magic = boot_magic_decode(magic);
282 }
283
284 off = boot_swap_info_off(fap);
285 rc = flash_area_read(fap, off, &swap_info, sizeof swap_info);
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300286 if (rc != 0) {
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100287 return BOOT_EFLASH;
288 }
289
290 /* Extract the swap type and image number */
291 state->swap_type = BOOT_GET_SWAP_TYPE(swap_info);
292 state->image_num = BOOT_GET_IMAGE_NUM(swap_info);
293
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300294 if (swap_info == flash_area_erased_val(fap) ||
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100295 state->swap_type > BOOT_SWAP_TYPE_REVERT) {
296 state->swap_type = BOOT_SWAP_TYPE_NONE;
297 state->image_num = 0;
298 }
299
Andrzej Puzdrowski4700b802020-12-09 14:55:36 +0100300 rc = boot_read_copy_done(fap, &state->copy_done);
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300301 if (rc != 0) {
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100302 return BOOT_EFLASH;
303 }
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100304
Andrzej Puzdrowski4700b802020-12-09 14:55:36 +0100305 return boot_read_image_ok(fap, &state->image_ok);
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100306}
307
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200308#endif /* !MCUBOOT_SWAP_USING_STATUS */
309
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100310int
311boot_read_swap_state_by_id(int flash_area_id, struct boot_swap_state *state)
312{
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300313 const struct flash_area *fap = NULL;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100314 int rc;
315
316 rc = flash_area_open(flash_area_id, &fap);
317 if (rc != 0) {
318 return BOOT_EFLASH;
319 }
320
321 rc = boot_read_swap_state(fap, state);
322 flash_area_close(fap);
323 return rc;
324}
325
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200326#ifndef MCUBOOT_SWAP_USING_STATUS
327
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100328int
329boot_write_magic(const struct flash_area *fap)
330{
331 uint32_t off;
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300332 uint32_t pad_off;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100333 int rc;
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300334 uint8_t magic[BOOT_MAGIC_ALIGN_SIZE];
335 uint8_t erased_val;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100336
337 off = boot_magic_off(fap);
338
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300339 /* image_trailer structure was modified with additional padding such that
340 * the pad+magic ends up in a flash minimum write region. The address
341 * returned by boot_magic_off() is the start of magic which is not the
342 * start of the flash write boundary and thus writes to the magic will fail.
343 * To account for this change, write to magic is first padded with 0xFF
344 * before writing to the trailer.
345 */
346 pad_off = ALIGN_DOWN(off, BOOT_MAX_ALIGN);
347
348 erased_val = flash_area_erased_val(fap);
349
350 (void)memset(&magic[0], erased_val, sizeof(magic));
351 (void)memcpy(&magic[BOOT_MAGIC_ALIGN_SIZE - BOOT_MAGIC_SZ], BOOT_IMG_MAGIC, BOOT_MAGIC_SZ);
352
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300353 BOOT_LOG_DBG("writing magic; fa_id=%u off=0x%" PRIx32
354 " (0x%" PRIx32 ")", (unsigned)flash_area_get_id(fap),
355 off, flash_area_get_off(fap) + off);
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300356 rc = flash_area_write(fap, pad_off, &magic[0], BOOT_MAGIC_ALIGN_SIZE);
357
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100358 if (rc != 0) {
359 return BOOT_EFLASH;
360 }
361
362 return 0;
363}
364
365/**
366 * Write trailer data; status bytes, swap_size, etc
367 *
368 * @returns 0 on success, != 0 on error.
369 */
Dominik Ermela7f9e9f2021-03-24 17:31:57 +0000370int
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100371boot_write_trailer(const struct flash_area *fap, uint32_t off,
372 const uint8_t *inbuf, uint8_t inlen)
373{
374 uint8_t buf[BOOT_MAX_ALIGN];
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100375 uint8_t erased_val;
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300376 uint32_t align;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100377 int rc;
378
379 align = flash_area_align(fap);
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300380
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300381 if (align == 0u) {
382 return BOOT_EFLASH;
383 }
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300384
385 align = ALIGN_UP(inlen, align);
Dominik Ermel48281622021-03-24 18:04:54 +0000386 if (align > BOOT_MAX_ALIGN) {
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100387 return -1;
388 }
389 erased_val = flash_area_erased_val(fap);
Dominik Ermel48281622021-03-24 18:04:54 +0000390
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300391 (void)memcpy(buf, inbuf, inlen);
392 (void)memset(&buf[inlen], erased_val, align - inlen);
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100393
394 rc = flash_area_write(fap, off, buf, align);
395 if (rc != 0) {
396 return BOOT_EFLASH;
397 }
398
399 return 0;
400}
401
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200402#endif /* !MCUBOOT_SWAP_USING_STATUS */
403
Dominik Ermela7f9e9f2021-03-24 17:31:57 +0000404int
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100405boot_write_trailer_flag(const struct flash_area *fap, uint32_t off,
406 uint8_t flag_val)
407{
408 const uint8_t buf[1] = { flag_val };
409 return boot_write_trailer(fap, off, buf, 1);
410}
411
412int
413boot_write_image_ok(const struct flash_area *fap)
414{
415 uint32_t off;
416
417 off = boot_image_ok_off(fap);
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300418 BOOT_LOG_DBG("writing image_ok; fa_id=%u off=0x%" PRIx32
419 " (0x%" PRIx32 ")", (unsigned)flash_area_get_id(fap),
420 off, flash_area_get_off(fap) + off);
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100421 return boot_write_trailer_flag(fap, off, BOOT_FLAG_SET);
422}
423
Andrzej Puzdrowski4700b802020-12-09 14:55:36 +0100424int
425boot_read_image_ok(const struct flash_area *fap, uint8_t *image_ok)
426{
427 return boot_read_flag(fap, image_ok, boot_image_ok_off(fap));
428}
429
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100430/**
431 * Writes the specified value to the `swap-type` field of an image trailer.
432 * This value is persisted so that the boot loader knows what swap operation to
433 * resume in case of an unexpected reset.
434 */
435int
436boot_write_swap_info(const struct flash_area *fap, uint8_t swap_type,
437 uint8_t image_num)
438{
439 uint32_t off;
440 uint8_t swap_info;
441
442 BOOT_SET_SWAP_INFO(swap_info, image_num, swap_type);
443 off = boot_swap_info_off(fap);
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300444 BOOT_LOG_DBG("writing swap_info; fa_id=%u off=0x%" PRIx32
445 " (0x%" PRIx32 "), swap_type=0x%x image_num=0x%x",
446 (unsigned)flash_area_get_id(fap), off,
447 flash_area_get_off(fap) + off,
448 (unsigned)swap_type, (unsigned)image_num);
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100449 return boot_write_trailer(fap, off, (const uint8_t *) &swap_info, 1);
450}
451
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300452#define BOOT_LOG_SWAP_STATE(area, state) \
453 BOOT_LOG_INF("%s: magic=%s, swap_type=0x%x, copy_done=0x%x, image_ok=0x%x", \
454 (area), \
455 ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \
456 (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \
457 "bad"), \
458 (unsigned)(state)->swap_type, \
459 (unsigned)(state)->copy_done, \
460 (unsigned)(state)->image_ok)
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200461
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100462int
463boot_swap_type_multi(int image_index)
464{
Dovhal Artem (CSUKR CSS ICW SW FW 1)f7a3d1b2022-04-01 15:07:37 +0000465 const struct boot_swap_table *table = NULL;
466 struct boot_swap_state primary_slot = {0};
467 struct boot_swap_state secondary_slot = {0};
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100468 int rc;
469 size_t i;
470
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300471 rc = BOOT_HOOK_CALL(boot_read_swap_state_primary_slot_hook,
472 BOOT_HOOK_REGULAR, image_index, &primary_slot);
473 if (rc == BOOT_HOOK_REGULAR)
474 {
475 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(image_index),
476 &primary_slot);
477 }
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100478 if (rc) {
479 return BOOT_SWAP_TYPE_PANIC;
480 }
481
482 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(image_index),
483 &secondary_slot);
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300484 if (rc == BOOT_EFLASH) {
485 BOOT_LOG_INF("Secondary image of image pair (%d.) "
486 "is unreachable. Treat it as empty", image_index);
487 secondary_slot.magic = BOOT_MAGIC_UNSET;
488 secondary_slot.swap_type = BOOT_SWAP_TYPE_NONE;
489 secondary_slot.copy_done = BOOT_FLAG_UNSET;
490 secondary_slot.image_ok = BOOT_FLAG_UNSET;
491 secondary_slot.image_num = 0;
492 } else if (rc) {
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100493 return BOOT_SWAP_TYPE_PANIC;
494 }
495
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200496 BOOT_LOG_SWAP_STATE("boot_swap_type_multi: Primary image", &primary_slot);
497 BOOT_LOG_SWAP_STATE("boot_swap_type_multi: Secondary image", &secondary_slot);
498
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100499 for (i = 0; i < BOOT_SWAP_TABLES_COUNT; i++) {
500 table = boot_swap_tables + i;
501
502 if (boot_magic_compatible_check(table->magic_primary_slot,
503 primary_slot.magic) &&
504 boot_magic_compatible_check(table->magic_secondary_slot,
505 secondary_slot.magic) &&
506 (table->image_ok_primary_slot == BOOT_FLAG_ANY ||
507 table->image_ok_primary_slot == primary_slot.image_ok) &&
508 (table->image_ok_secondary_slot == BOOT_FLAG_ANY ||
509 table->image_ok_secondary_slot == secondary_slot.image_ok) &&
510 (table->copy_done_primary_slot == BOOT_FLAG_ANY ||
511 table->copy_done_primary_slot == primary_slot.copy_done)) {
512 BOOT_LOG_INF("Swap type: %s",
513 table->swap_type == BOOT_SWAP_TYPE_TEST ? "test" :
514 table->swap_type == BOOT_SWAP_TYPE_PERM ? "perm" :
515 table->swap_type == BOOT_SWAP_TYPE_REVERT ? "revert" :
516 "BUG; can't happen");
517 if (table->swap_type != BOOT_SWAP_TYPE_TEST &&
518 table->swap_type != BOOT_SWAP_TYPE_PERM &&
519 table->swap_type != BOOT_SWAP_TYPE_REVERT) {
520 return BOOT_SWAP_TYPE_PANIC;
521 }
522 return table->swap_type;
523 }
524 }
525
526 BOOT_LOG_INF("Swap type: none");
527 return BOOT_SWAP_TYPE_NONE;
528}
529
530/*
531 * This function is not used by the bootloader itself, but its required API
532 * by external tooling like mcumgr.
533 */
534int
535boot_swap_type(void)
536{
537 return boot_swap_type_multi(0);
538}
539
540/**
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300541 * Marks the image with the given index in the secondary slot as pending. On the
542 * next reboot, the system will perform a one-time boot of the the secondary
543 * slot image.
544 *
545 * @param image_index Image pair index.
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100546 *
547 * @param permanent Whether the image should be used permanently or
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300548 * only tested once:
549 * 0=run image once, then confirm or revert.
550 * 1=run image forever.
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100551 *
552 * @return 0 on success; nonzero on failure.
553 */
554int
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300555boot_set_pending_multi(int image_index, int permanent)
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100556{
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300557 const struct flash_area *fap = NULL;
558 struct boot_swap_state state_secondary_slot = {0};
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100559 uint8_t swap_type;
560 int rc;
561
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300562 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index), &fap);
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100563 if (rc != 0) {
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300564 return BOOT_EFLASH;
565 }
566
567 rc = boot_read_swap_state(fap, &state_secondary_slot);
568 if (rc != 0) {
569 goto done;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100570 }
571
572 switch (state_secondary_slot.magic) {
573 case BOOT_MAGIC_GOOD:
574 /* Swap already scheduled. */
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300575 break;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100576
577 case BOOT_MAGIC_UNSET:
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300578 rc = boot_write_magic(fap);
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100579
580 if (rc == 0 && permanent) {
581 rc = boot_write_image_ok(fap);
582 }
583
584 if (rc == 0) {
585 if (permanent) {
586 swap_type = BOOT_SWAP_TYPE_PERM;
587 } else {
588 swap_type = BOOT_SWAP_TYPE_TEST;
589 }
590 rc = boot_write_swap_info(fap, swap_type, 0);
591 }
592
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300593 break;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100594
595 case BOOT_MAGIC_BAD:
596 /* The image slot is corrupt. There is no way to recover, so erase the
597 * slot to allow future upgrades.
598 */
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300599 flash_area_erase(fap, 0, flash_area_get_size(fap));
600 rc = BOOT_EBADIMAGE;
601 break;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100602
603 default:
604 assert(0);
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300605 rc = BOOT_EBADIMAGE;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100606 }
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300607
608done:
609 flash_area_close(fap);
610 return rc;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100611}
612
613/**
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300614 * Marks the image with index 0 in the secondary slot as pending. On the next
615 * reboot, the system will perform a one-time boot of the the secondary slot
616 * image. Note that this API is kept for compatibility. The
617 * boot_set_pending_multi() API is recommended.
618 *
619 * @param permanent Whether the image should be used permanently or
620 * only tested once:
621 * 0=run image once, then confirm or revert.
622 * 1=run image forever.
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100623 *
624 * @return 0 on success; nonzero on failure.
625 */
626int
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300627boot_set_pending(int permanent)
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100628{
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300629 return boot_set_pending_multi(0, permanent);
630}
631
632/**
633 * Marks the image with the given index in the primary slot as confirmed. The
634 * system will continue booting into the image in the primary slot until told to
635 * boot from a different slot.
636 *
637 * @param image_index Image pair index.
638 *
639 * @return 0 on success; nonzero on failure.
640 */
641int
642boot_set_confirmed_multi(int image_index)
643{
644 const struct flash_area *fap = NULL;
645 struct boot_swap_state state_primary_slot = {0};
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100646 int rc;
647
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300648 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index), &fap);
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100649 if (rc != 0) {
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300650 return BOOT_EFLASH;
651 }
652
653 rc = boot_read_swap_state(fap, &state_primary_slot);
654 if (rc != 0) {
655 goto done;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100656 }
657
658 switch (state_primary_slot.magic) {
659 case BOOT_MAGIC_GOOD:
660 /* Confirm needed; proceed. */
661 break;
662
663 case BOOT_MAGIC_UNSET:
664 /* Already confirmed. */
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300665 goto done;
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100666
667 case BOOT_MAGIC_BAD:
668 /* Unexpected state. */
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100669 rc = BOOT_EBADVECT;
670 goto done;
671 }
672
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300673 /* Intentionally do not check copy_done flag
674 * so can confirm a padded image which was programed using a programing
675 * interface.
676 */
677
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100678 if (state_primary_slot.image_ok != BOOT_FLAG_UNSET) {
679 /* Already confirmed. */
680 goto done;
681 }
682
683 rc = boot_write_image_ok(fap);
684
685done:
686 flash_area_close(fap);
687 return rc;
688}
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300689
690/**
691 * Marks the image with index 0 in the primary slot as confirmed. The system
692 * will continue booting into the image in the primary slot until told to boot
693 * from a different slot. Note that this API is kept for compatibility. The
694 * boot_set_confirmed_multi() API is recommended.
695 *
696 * @return 0 on success; nonzero on failure.
697 */
698int
699boot_set_confirmed(void)
700{
701 return boot_set_confirmed_multi(0);
702}