blob: 4cddec3a53f7f2e0b7ffe757ad95f8aa12e172f4 [file] [log] [blame]
Tamas Banf70ef8c2017-12-19 15:35:09 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
David Vincze91b71ef2019-06-24 13:06:47 +020020/*
21 * Original code taken from mcuboot project at:
22 * https://github.com/JuulLabs-OSS/mcuboot
David Vincze2ddc1372019-10-25 11:10:08 +020023 * Git SHA of the original version: ac55554059147fff718015be9f4bd3108123f50a
David Vincze91b71ef2019-06-24 13:06:47 +020024 * Modifications are Copyright (c) 2019 Arm Limited.
25 */
26
Tamas Banf70ef8c2017-12-19 15:35:09 +000027#include <assert.h>
28#include <string.h>
29#include <inttypes.h>
30#include <stddef.h>
Tamas Ban056ed0b2019-09-16 12:48:32 +010031#include <stdbool.h>
Tamas Banf70ef8c2017-12-19 15:35:09 +000032
Tamas Banf70ef8c2017-12-19 15:35:09 +000033#include "flash_map/flash_map.h"
Tamas Banf70ef8c2017-12-19 15:35:09 +000034#include "bootutil/image.h"
35#include "bootutil/bootutil.h"
36#include "bootutil_priv.h"
Tamas Banf70ef8c2017-12-19 15:35:09 +000037#include "bootutil/bootutil_log.h"
38
David Vinczecea8b592019-10-29 16:09:51 +010039/* Currently only used by imgmgr */
Tamas Banf70ef8c2017-12-19 15:35:09 +000040int boot_current_slot;
41
42const uint32_t boot_img_magic[] = {
43 0xf395c277,
44 0x7fefd260,
45 0x0f505235,
46 0x8079b62c,
47};
48
David Vincze39e78552018-10-10 17:10:01 +020049#define BOOT_MAGIC_ARR_SZ \
50 (sizeof boot_img_magic / sizeof boot_img_magic[0])
51
Tamas Banf70ef8c2017-12-19 15:35:09 +000052struct boot_swap_table {
David Vincze8bdfc2d2019-03-18 15:49:23 +010053 uint8_t magic_primary_slot;
54 uint8_t magic_secondary_slot;
55 uint8_t image_ok_primary_slot;
56 uint8_t image_ok_secondary_slot;
57 uint8_t copy_done_primary_slot;
Tamas Banf70ef8c2017-12-19 15:35:09 +000058
59 uint8_t swap_type;
60};
61
62/**
63 * This set of tables maps image trailer contents to swap operation type.
64 * When searching for a match, these tables must be iterated sequentially.
65 *
David Vincze8bdfc2d2019-03-18 15:49:23 +010066 * NOTE: the table order is very important. The settings in the secondary
67 * slot always are priority to the primary slot and should be located
68 * earlier in the table.
Tamas Banf70ef8c2017-12-19 15:35:09 +000069 *
70 * The table lists only states where there is action needs to be taken by
71 * the bootloader, as in starting/finishing a swap operation.
72 */
73static const struct boot_swap_table boot_swap_tables[] = {
74 {
David Vincze8bdfc2d2019-03-18 15:49:23 +010075 .magic_primary_slot = BOOT_MAGIC_ANY,
76 .magic_secondary_slot = BOOT_MAGIC_GOOD,
77 .image_ok_primary_slot = BOOT_FLAG_ANY,
78 .image_ok_secondary_slot = BOOT_FLAG_UNSET,
79 .copy_done_primary_slot = BOOT_FLAG_ANY,
80 .swap_type = BOOT_SWAP_TYPE_TEST,
Tamas Banf70ef8c2017-12-19 15:35:09 +000081 },
82 {
David Vincze8bdfc2d2019-03-18 15:49:23 +010083 .magic_primary_slot = BOOT_MAGIC_ANY,
84 .magic_secondary_slot = BOOT_MAGIC_GOOD,
85 .image_ok_primary_slot = BOOT_FLAG_ANY,
86 .image_ok_secondary_slot = BOOT_FLAG_SET,
87 .copy_done_primary_slot = BOOT_FLAG_ANY,
88 .swap_type = BOOT_SWAP_TYPE_PERM,
Tamas Banf70ef8c2017-12-19 15:35:09 +000089 },
90 {
David Vincze8bdfc2d2019-03-18 15:49:23 +010091 .magic_primary_slot = BOOT_MAGIC_GOOD,
92 .magic_secondary_slot = BOOT_MAGIC_UNSET,
93 .image_ok_primary_slot = BOOT_FLAG_UNSET,
94 .image_ok_secondary_slot = BOOT_FLAG_ANY,
95 .copy_done_primary_slot = BOOT_FLAG_SET,
96 .swap_type = BOOT_SWAP_TYPE_REVERT,
Tamas Banf70ef8c2017-12-19 15:35:09 +000097 },
98};
99
100#define BOOT_SWAP_TABLES_COUNT \
Tamas Ban581034a2017-12-19 19:54:37 +0000101 (sizeof(boot_swap_tables) / sizeof(boot_swap_tables[0]))
Tamas Banf70ef8c2017-12-19 15:35:09 +0000102
Raef Coles25b857a2019-09-05 13:59:55 +0100103/**
104 * @brief Determine if the data at two memory addresses is equal
105 *
106 * @param s1 The first memory region to compare.
107 * @param s2 The second memory region to compare.
108 * @param n The amount of bytes to compare.
109 *
110 * @note This function does not comply with the specification of memcmp,
111 * so should not be considered a drop-in replacement.
112 *
113 * @return 0 if memory regions are equal.
114 */
115uint32_t boot_secure_memequal(const void *s1, const void *s2, size_t n)
116{
117 size_t i;
118 uint8_t *s1_p = (uint8_t*) s1;
119 uint8_t *s2_p = (uint8_t*) s2;
120 uint32_t ret = 0;
121
122 for (i = 0; i < n; i++) {
123 ret |= (s1_p[i] ^ s2_p[i]);
124 }
125
126 return ret;
127}
128
David Vincze39e78552018-10-10 17:10:01 +0200129static int
130boot_magic_decode(const uint32_t *magic)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000131{
Raef Coles25b857a2019-09-05 13:59:55 +0100132 if (boot_secure_memequal(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000133 return BOOT_MAGIC_GOOD;
134 }
David Vincze39e78552018-10-10 17:10:01 +0200135 return BOOT_MAGIC_BAD;
136}
Tamas Banf70ef8c2017-12-19 15:35:09 +0000137
David Vincze39e78552018-10-10 17:10:01 +0200138static int
139boot_flag_decode(uint8_t flag)
140{
141 if (flag != BOOT_FLAG_SET) {
142 return BOOT_FLAG_BAD;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000143 }
David Vincze39e78552018-10-10 17:10:01 +0200144 return BOOT_FLAG_SET;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000145}
146
David Vincze401c7422019-06-21 20:44:05 +0200147/**
148 * Determines if a status source table is satisfied by the specified magic
149 * code.
150 *
151 * @param tbl_val A magic field from a status source table.
152 * @param val The magic value in a trailer, encoded as a
153 * BOOT_MAGIC_[...].
154 *
155 * @return 1 if the two values are compatible;
156 * 0 otherwise.
157 */
158int
159boot_magic_compatible_check(uint8_t tbl_val, uint8_t val)
160{
161 switch (tbl_val) {
162 case BOOT_MAGIC_ANY:
163 return 1;
164
165 case BOOT_MAGIC_NOTGOOD:
166 return val != BOOT_MAGIC_GOOD;
167
168 default:
169 return tbl_val == val;
170 }
171}
172
Tamas Banf70ef8c2017-12-19 15:35:09 +0000173uint32_t
Raef Coles204c5b42019-09-05 09:18:47 +0100174boot_trailer_sz(uint32_t min_write_sz)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000175{
176 return /* state for all sectors */
177 BOOT_STATUS_MAX_ENTRIES * BOOT_STATUS_STATE_COUNT * min_write_sz +
David Vincze401c7422019-06-21 20:44:05 +0200178 /* swap_type + copy_done + image_ok + swap_size */
179 BOOT_MAX_ALIGN * 4 +
Tamas Banf70ef8c2017-12-19 15:35:09 +0000180 BOOT_MAGIC_SZ;
181}
182
Tamas Banf70ef8c2017-12-19 15:35:09 +0000183int
David Vinczecea8b592019-10-29 16:09:51 +0100184boot_status_entries(int image_index, const struct flash_area *fap)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000185{
David Vinczebb207982019-08-21 15:13:04 +0200186 if (fap->fa_id == FLASH_AREA_IMAGE_SCRATCH) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000187 return BOOT_STATUS_STATE_COUNT;
David Vinczecea8b592019-10-29 16:09:51 +0100188 } else if ((fap->fa_id == FLASH_AREA_IMAGE_PRIMARY(image_index)) ||
189 (fap->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index))) {
David Vinczebb207982019-08-21 15:13:04 +0200190 return BOOT_STATUS_STATE_COUNT * BOOT_STATUS_MAX_ENTRIES;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000191 }
David Vinczecea8b592019-10-29 16:09:51 +0100192 return -1;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000193}
194
195uint32_t
196boot_status_off(const struct flash_area *fap)
197{
198 uint32_t off_from_end;
Raef Coles204c5b42019-09-05 09:18:47 +0100199 uint32_t elem_sz;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000200
201 elem_sz = flash_area_align(fap);
202
David Vincze401c7422019-06-21 20:44:05 +0200203 off_from_end = boot_trailer_sz(elem_sz);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000204
205 assert(off_from_end <= fap->fa_size);
206 return fap->fa_size - off_from_end;
207}
208
David Vinczecea8b592019-10-29 16:09:51 +0100209static inline uint32_t
210boot_magic_off(const struct flash_area *fap)
211{
212 return fap->fa_size - BOOT_MAGIC_SZ;
213}
214
215static inline uint32_t
216boot_image_ok_off(const struct flash_area *fap)
217{
218 return boot_magic_off(fap) - BOOT_MAX_ALIGN;
219}
220
221static inline uint32_t
222boot_copy_done_off(const struct flash_area *fap)
223{
224 return boot_image_ok_off(fap) - BOOT_MAX_ALIGN;
225}
226
David Vincze401c7422019-06-21 20:44:05 +0200227uint32_t
David Vincze91b71ef2019-06-24 13:06:47 +0200228boot_swap_info_off(const struct flash_area *fap)
David Vincze401c7422019-06-21 20:44:05 +0200229{
David Vinczecea8b592019-10-29 16:09:51 +0100230 return boot_copy_done_off(fap) - BOOT_MAX_ALIGN;
David Vincze401c7422019-06-21 20:44:05 +0200231}
232
David Vinczecea8b592019-10-29 16:09:51 +0100233static inline uint32_t
Tamas Banf70ef8c2017-12-19 15:35:09 +0000234boot_swap_size_off(const struct flash_area *fap)
235{
David Vinczecea8b592019-10-29 16:09:51 +0100236 return boot_swap_info_off(fap) - BOOT_MAX_ALIGN;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000237}
238
239int
240boot_read_swap_state(const struct flash_area *fap,
241 struct boot_swap_state *state)
242{
David Vincze39e78552018-10-10 17:10:01 +0200243 uint32_t magic[BOOT_MAGIC_ARR_SZ];
Tamas Banf70ef8c2017-12-19 15:35:09 +0000244 uint32_t off;
David Vincze91b71ef2019-06-24 13:06:47 +0200245 uint8_t swap_info;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000246 int rc;
247
248 off = boot_magic_off(fap);
David Vincze39e78552018-10-10 17:10:01 +0200249 rc = flash_area_read_is_empty(fap, off, magic, BOOT_MAGIC_SZ);
250 if (rc < 0) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000251 return BOOT_EFLASH;
252 }
David Vincze39e78552018-10-10 17:10:01 +0200253 if (rc == 1) {
254 state->magic = BOOT_MAGIC_UNSET;
255 } else {
256 state->magic = boot_magic_decode(magic);
257 }
Tamas Banf70ef8c2017-12-19 15:35:09 +0000258
David Vincze91b71ef2019-06-24 13:06:47 +0200259 off = boot_swap_info_off(fap);
260 rc = flash_area_read_is_empty(fap, off, &swap_info, sizeof swap_info);
David Vincze401c7422019-06-21 20:44:05 +0200261 if (rc < 0) {
262 return BOOT_EFLASH;
263 }
David Vincze91b71ef2019-06-24 13:06:47 +0200264
265 /* Extract the swap type and image number */
266 state->swap_type = BOOT_GET_SWAP_TYPE(swap_info);
267 state->image_num = BOOT_GET_IMAGE_NUM(swap_info);
268
David Vincze401c7422019-06-21 20:44:05 +0200269 if (rc == 1 || state->swap_type > BOOT_SWAP_TYPE_REVERT) {
270 state->swap_type = BOOT_SWAP_TYPE_NONE;
David Vincze91b71ef2019-06-24 13:06:47 +0200271 state->image_num = 0;
David Vincze401c7422019-06-21 20:44:05 +0200272 }
273
274 off = boot_copy_done_off(fap);
275 rc = flash_area_read_is_empty(fap, off, &state->copy_done,
276 sizeof state->copy_done);
277 if (rc < 0) {
278 return BOOT_EFLASH;
279 }
280 if (rc == 1) {
281 state->copy_done = BOOT_FLAG_UNSET;
282 } else {
283 state->copy_done = boot_flag_decode(state->copy_done);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000284 }
285
286 off = boot_image_ok_off(fap);
David Vincze401c7422019-06-21 20:44:05 +0200287 rc = flash_area_read_is_empty(fap, off, &state->image_ok,
288 sizeof state->image_ok);
David Vincze39e78552018-10-10 17:10:01 +0200289 if (rc < 0) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000290 return BOOT_EFLASH;
291 }
David Vincze39e78552018-10-10 17:10:01 +0200292 if (rc == 1) {
293 state->image_ok = BOOT_FLAG_UNSET;
294 } else {
295 state->image_ok = boot_flag_decode(state->image_ok);
296 }
Tamas Banf70ef8c2017-12-19 15:35:09 +0000297
298 return 0;
299}
300
301/**
302 * Reads the image trailer from the scratch area.
303 */
304int
305boot_read_swap_state_by_id(int flash_area_id, struct boot_swap_state *state)
306{
307 const struct flash_area *fap;
308 int rc;
309
David Vinczecea8b592019-10-29 16:09:51 +0100310 rc = flash_area_open(flash_area_id, &fap);
311 if (rc != 0) {
312 return BOOT_EFLASH;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000313 }
314
315 rc = boot_read_swap_state(fap, state);
316 flash_area_close(fap);
317 return rc;
318}
319
David Vinczecea8b592019-10-29 16:09:51 +0100320/**
321 * This functions tries to locate the status area after an aborted swap,
322 * by looking for the magic in the possible locations.
323 *
324 * If the magic is sucessfully found, a flash_area * is returned and it
325 * is the responsibility of the called to close it.
326 *
327 * @returns 0 on success, -1 on errors
328 */
329static int
330boot_find_status(int image_index, const struct flash_area **fap)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000331{
David Vincze39e78552018-10-10 17:10:01 +0200332 uint32_t magic[BOOT_MAGIC_ARR_SZ];
Tamas Banf70ef8c2017-12-19 15:35:09 +0000333 uint32_t off;
David Vinczecea8b592019-10-29 16:09:51 +0100334 uint8_t areas[2] = {
335 FLASH_AREA_IMAGE_PRIMARY(image_index),
336 FLASH_AREA_IMAGE_SCRATCH,
337 };
338 unsigned int i;
339 int rc;
340
341 /*
342 * In the middle a swap, tries to locate the area that is currently
343 * storing a valid magic, first on the primary slot, then on scratch.
344 * Both "slots" can end up being temporary storage for a swap and it
345 * is assumed that if magic is valid then other metadata is too,
346 * because magic is always written in the last step.
347 */
348
349 for (i = 0; i < sizeof(areas) / sizeof(areas[0]); i++) {
350 rc = flash_area_open(areas[i], fap);
351 if (rc != 0) {
352 return rc;
353 }
354
355 off = boot_magic_off(*fap);
356 rc = flash_area_read(*fap, off, magic, BOOT_MAGIC_SZ);
357 if (rc != 0) {
358 flash_area_close(*fap);
359 return rc;
360 }
361
David Vinczec2566122019-10-25 13:18:54 +0200362 if (boot_secure_memequal(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0) {
David Vinczecea8b592019-10-29 16:09:51 +0100363 return 0;
364 }
365
366 flash_area_close(*fap);
367 }
368
369 /* If we got here, no magic was found */
370 return -1;
371}
372
373int
374boot_read_swap_size(int image_index, uint32_t *swap_size)
375{
376 uint32_t off;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000377 const struct flash_area *fap;
378 int rc;
379
David Vinczecea8b592019-10-29 16:09:51 +0100380 rc = boot_find_status(image_index, &fap);
381 if (rc == 0) {
382 off = boot_swap_size_off(fap);
383 rc = flash_area_read(fap, off, swap_size, sizeof *swap_size);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000384 flash_area_close(fap);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000385 }
386
Tamas Banf70ef8c2017-12-19 15:35:09 +0000387 return rc;
388}
389
Tamas Banf70ef8c2017-12-19 15:35:09 +0000390int
391boot_write_magic(const struct flash_area *fap)
392{
393 uint32_t off;
394 int rc;
395
396 off = boot_magic_off(fap);
397
David Vinczecea8b592019-10-29 16:09:51 +0100398 BOOT_LOG_DBG("writing magic; fa_id=%d off=0x%lx (0x%lx)",
399 fap->fa_id, (unsigned long)off,
400 (unsigned long)(fap->fa_off + off));
Tamas Banf70ef8c2017-12-19 15:35:09 +0000401 rc = flash_area_write(fap, off, boot_img_magic, BOOT_MAGIC_SZ);
402 if (rc != 0) {
403 return BOOT_EFLASH;
404 }
405
406 return 0;
407}
408
David Vinczecea8b592019-10-29 16:09:51 +0100409/**
410 * Write trailer data; status bytes, swap_size, etc
411 *
412 * @returns 0 on success, != 0 on error.
413 */
Tamas Banf70ef8c2017-12-19 15:35:09 +0000414static int
David Vinczecea8b592019-10-29 16:09:51 +0100415boot_write_trailer(const struct flash_area *fap, uint32_t off,
416 const uint8_t *inbuf, uint8_t inlen)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000417{
Tamas Banf70ef8c2017-12-19 15:35:09 +0000418 uint8_t buf[BOOT_MAX_ALIGN];
David Vinczecea8b592019-10-29 16:09:51 +0100419 uint8_t align;
David Vincze39e78552018-10-10 17:10:01 +0200420 uint8_t erased_val;
David Vincze401c7422019-06-21 20:44:05 +0200421 int rc;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000422
Tamas Banc3828852018-02-01 12:24:16 +0000423 align = flash_area_align(fap);
David Vinczecea8b592019-10-29 16:09:51 +0100424 if (inlen > BOOT_MAX_ALIGN || align > BOOT_MAX_ALIGN) {
425 return -1;
426 }
David Vincze39e78552018-10-10 17:10:01 +0200427 erased_val = flash_area_erased_val(fap);
David Vinczecea8b592019-10-29 16:09:51 +0100428 if (align < inlen) {
429 align = inlen;
430 }
431 memcpy(buf, inbuf, inlen);
432 memset(&buf[inlen], erased_val, align - inlen);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000433
434 rc = flash_area_write(fap, off, buf, align);
435 if (rc != 0) {
436 return BOOT_EFLASH;
437 }
438
439 return 0;
440}
441
David Vinczecea8b592019-10-29 16:09:51 +0100442static int
443boot_write_trailer_flag(const struct flash_area *fap, uint32_t off,
444 uint8_t flag_val)
445{
446 const uint8_t buf[1] = { flag_val };
447 return boot_write_trailer(fap, off, buf, 1);
448}
449
Tamas Banf70ef8c2017-12-19 15:35:09 +0000450int
451boot_write_copy_done(const struct flash_area *fap)
452{
David Vincze401c7422019-06-21 20:44:05 +0200453 uint32_t off;
454
455 off = boot_copy_done_off(fap);
David Vinczecea8b592019-10-29 16:09:51 +0100456 BOOT_LOG_DBG("writing copy_done; fa_id=%d off=0x%lx (0x%lx)",
457 fap->fa_id, (unsigned long)off,
458 (unsigned long)(fap->fa_off + off));
459 return boot_write_trailer_flag(fap, off, BOOT_FLAG_SET);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000460}
461
462int
463boot_write_image_ok(const struct flash_area *fap)
464{
David Vincze401c7422019-06-21 20:44:05 +0200465 uint32_t off;
466
467 off = boot_image_ok_off(fap);
David Vinczecea8b592019-10-29 16:09:51 +0100468 BOOT_LOG_DBG("writing image_ok; fa_id=%d off=0x%lx (0x%lx)",
469 fap->fa_id, (unsigned long)off,
470 (unsigned long)(fap->fa_off + off));
471 return boot_write_trailer_flag(fap, off, BOOT_FLAG_SET);
David Vincze401c7422019-06-21 20:44:05 +0200472}
473
474/**
475 * Writes the specified value to the `swap-type` field of an image trailer.
476 * This value is persisted so that the boot loader knows what swap operation to
477 * resume in case of an unexpected reset.
478 */
479int
David Vincze91b71ef2019-06-24 13:06:47 +0200480boot_write_swap_info(const struct flash_area *fap, uint8_t swap_type,
481 uint8_t image_num)
David Vincze401c7422019-06-21 20:44:05 +0200482{
483 uint32_t off;
David Vincze91b71ef2019-06-24 13:06:47 +0200484 uint8_t swap_info;
David Vincze401c7422019-06-21 20:44:05 +0200485
David Vincze91b71ef2019-06-24 13:06:47 +0200486 BOOT_SET_SWAP_INFO(swap_info, image_num, swap_type);
487 off = boot_swap_info_off(fap);
David Vinczecea8b592019-10-29 16:09:51 +0100488 BOOT_LOG_DBG("writing swap_info; fa_id=%d off=0x%lx (0x%lx), swap_type=0x%x"
David Vincze91b71ef2019-06-24 13:06:47 +0200489 " image_num=0x%x",
David Vinczecea8b592019-10-29 16:09:51 +0100490 fap->fa_id, (unsigned long)off,
491 (unsigned long)(fap->fa_off + off), swap_type, image_num);
492 return boot_write_trailer(fap, off, (const uint8_t *) &swap_info, 1);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000493}
494
495int
496boot_write_swap_size(const struct flash_area *fap, uint32_t swap_size)
497{
498 uint32_t off;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000499
500 off = boot_swap_size_off(fap);
David Vinczecea8b592019-10-29 16:09:51 +0100501 BOOT_LOG_DBG("writing swap_size; fa_id=%d off=0x%lx (0x%lx)",
502 fap->fa_id, (unsigned long)off,
503 (unsigned long)fap->fa_off + off);
504 return boot_write_trailer(fap, off, (const uint8_t *) &swap_size, 4);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000505}
506
507int
David Vinczecea8b592019-10-29 16:09:51 +0100508boot_swap_type_multi(int image_index)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000509{
510 const struct boot_swap_table *table;
David Vincze8bdfc2d2019-03-18 15:49:23 +0100511 struct boot_swap_state primary_slot;
512 struct boot_swap_state secondary_slot;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000513 int rc;
David Vincze39e78552018-10-10 17:10:01 +0200514 size_t i;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000515
David Vinczecea8b592019-10-29 16:09:51 +0100516 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(image_index),
517 &primary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000518 if (rc) {
519 return BOOT_SWAP_TYPE_PANIC;
520 }
521
David Vinczecea8b592019-10-29 16:09:51 +0100522 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(image_index),
David Vincze8bdfc2d2019-03-18 15:49:23 +0100523 &secondary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000524 if (rc) {
525 return BOOT_SWAP_TYPE_PANIC;
526 }
527
528 for (i = 0; i < BOOT_SWAP_TABLES_COUNT; i++) {
529 table = boot_swap_tables + i;
530
David Vincze401c7422019-06-21 20:44:05 +0200531 if (boot_magic_compatible_check(table->magic_primary_slot,
532 primary_slot.magic) &&
533 boot_magic_compatible_check(table->magic_secondary_slot,
534 secondary_slot.magic) &&
David Vincze8bdfc2d2019-03-18 15:49:23 +0100535 (table->image_ok_primary_slot == BOOT_FLAG_ANY ||
536 table->image_ok_primary_slot == primary_slot.image_ok) &&
537 (table->image_ok_secondary_slot == BOOT_FLAG_ANY ||
538 table->image_ok_secondary_slot == secondary_slot.image_ok) &&
539 (table->copy_done_primary_slot == BOOT_FLAG_ANY ||
540 table->copy_done_primary_slot == primary_slot.copy_done)) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000541 BOOT_LOG_INF("Swap type: %s",
542 table->swap_type == BOOT_SWAP_TYPE_TEST ? "test" :
543 table->swap_type == BOOT_SWAP_TYPE_PERM ? "perm" :
544 table->swap_type == BOOT_SWAP_TYPE_REVERT ? "revert" :
545 "BUG; can't happen");
David Vinczecea8b592019-10-29 16:09:51 +0100546 if (table->swap_type != BOOT_SWAP_TYPE_TEST &&
547 table->swap_type != BOOT_SWAP_TYPE_PERM &&
548 table->swap_type != BOOT_SWAP_TYPE_REVERT) {
549 return BOOT_SWAP_TYPE_PANIC;
550 }
Tamas Banf70ef8c2017-12-19 15:35:09 +0000551 return table->swap_type;
552 }
553 }
554
555 BOOT_LOG_INF("Swap type: none");
556 return BOOT_SWAP_TYPE_NONE;
557}
558
David Vinczecea8b592019-10-29 16:09:51 +0100559/*
560 * This function is not used by the bootloader itself, but its required API
561 * by external tooling like mcumgr.
562 */
563int
564boot_swap_type(void)
565{
566 return boot_swap_type_multi(0);
567}
568
Tamas Banf70ef8c2017-12-19 15:35:09 +0000569/**
David Vincze8bdfc2d2019-03-18 15:49:23 +0100570 * Marks the image in the secondary slot as pending. On the next reboot,
571 * the system will perform a one-time boot of the the secondary slot image.
Tamas Banf70ef8c2017-12-19 15:35:09 +0000572 *
573 * @param permanent Whether the image should be used permanently or
574 * only tested once:
575 * 0=run image once, then confirm or revert.
576 * 1=run image forever.
577 *
578 * @return 0 on success; nonzero on failure.
579 */
580int
581boot_set_pending(int permanent)
582{
Tamas Ban581034a2017-12-19 19:54:37 +0000583 const struct flash_area *fap = NULL;
David Vincze8bdfc2d2019-03-18 15:49:23 +0100584 struct boot_swap_state state_secondary_slot;
David Vincze401c7422019-06-21 20:44:05 +0200585 uint8_t swap_type;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000586 int rc;
587
David Vinczecea8b592019-10-29 16:09:51 +0100588 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(0),
David Vincze8bdfc2d2019-03-18 15:49:23 +0100589 &state_secondary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000590 if (rc != 0) {
591 return rc;
592 }
593
David Vincze8bdfc2d2019-03-18 15:49:23 +0100594 switch (state_secondary_slot.magic) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000595 case BOOT_MAGIC_GOOD:
596 /* Swap already scheduled. */
597 return 0;
598
599 case BOOT_MAGIC_UNSET:
David Vinczecea8b592019-10-29 16:09:51 +0100600 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(0), &fap);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000601 if (rc != 0) {
602 rc = BOOT_EFLASH;
603 } else {
604 rc = boot_write_magic(fap);
605 }
606
607 if (rc == 0 && permanent) {
608 rc = boot_write_image_ok(fap);
609 }
610
David Vincze401c7422019-06-21 20:44:05 +0200611 if (rc == 0) {
612 if (permanent) {
613 swap_type = BOOT_SWAP_TYPE_PERM;
614 } else {
615 swap_type = BOOT_SWAP_TYPE_TEST;
616 }
David Vincze91b71ef2019-06-24 13:06:47 +0200617 rc = boot_write_swap_info(fap, swap_type, 0);
David Vincze401c7422019-06-21 20:44:05 +0200618 }
619
Tamas Banf70ef8c2017-12-19 15:35:09 +0000620 flash_area_close(fap);
621 return rc;
622
David Vincze401c7422019-06-21 20:44:05 +0200623 case BOOT_MAGIC_BAD:
624 /* The image slot is corrupt. There is no way to recover, so erase the
625 * slot to allow future upgrades.
626 */
David Vinczecea8b592019-10-29 16:09:51 +0100627 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(0), &fap);
David Vincze401c7422019-06-21 20:44:05 +0200628 if (rc != 0) {
629 return BOOT_EFLASH;
630 }
631
632 flash_area_erase(fap, 0, fap->fa_size);
633 flash_area_close(fap);
634 return BOOT_EBADIMAGE;
635
Tamas Banf70ef8c2017-12-19 15:35:09 +0000636 default:
Tamas Banf70ef8c2017-12-19 15:35:09 +0000637 assert(0);
David Vincze401c7422019-06-21 20:44:05 +0200638 return BOOT_EBADIMAGE;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000639 }
640}
641
642/**
David Vincze8bdfc2d2019-03-18 15:49:23 +0100643 * Marks the image in the primary slot as confirmed. The system will continue
644 * booting into the image in the primary slot until told to boot from a
645 * different slot.
Tamas Banf70ef8c2017-12-19 15:35:09 +0000646 *
Tamas Ban581034a2017-12-19 19:54:37 +0000647 * @return 0 on success; non-zero on failure.
Tamas Banf70ef8c2017-12-19 15:35:09 +0000648 */
649int
650boot_set_confirmed(void)
651{
Tamas Ban581034a2017-12-19 19:54:37 +0000652 const struct flash_area *fap = NULL;
David Vincze8bdfc2d2019-03-18 15:49:23 +0100653 struct boot_swap_state state_primary_slot;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000654 int rc;
655
David Vinczecea8b592019-10-29 16:09:51 +0100656 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(0),
David Vincze8bdfc2d2019-03-18 15:49:23 +0100657 &state_primary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000658 if (rc != 0) {
659 return rc;
660 }
661
David Vincze8bdfc2d2019-03-18 15:49:23 +0100662 switch (state_primary_slot.magic) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000663 case BOOT_MAGIC_GOOD:
664 /* Confirm needed; proceed. */
665 break;
666
667 case BOOT_MAGIC_UNSET:
668 /* Already confirmed. */
669 return 0;
670
671 case BOOT_MAGIC_BAD:
672 /* Unexpected state. */
673 return BOOT_EBADVECT;
674 }
675
David Vinczecea8b592019-10-29 16:09:51 +0100676 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(0), &fap);
677 if (rc) {
678 rc = BOOT_EFLASH;
679 goto done;
680 }
681
David Vincze8bdfc2d2019-03-18 15:49:23 +0100682 if (state_primary_slot.copy_done == BOOT_FLAG_UNSET) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000683 /* Swap never completed. This is unexpected. */
David Vincze39e78552018-10-10 17:10:01 +0200684 rc = BOOT_EBADVECT;
685 goto done;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000686 }
687
David Vincze8bdfc2d2019-03-18 15:49:23 +0100688 if (state_primary_slot.image_ok != BOOT_FLAG_UNSET) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000689 /* Already confirmed. */
David Vincze39e78552018-10-10 17:10:01 +0200690 goto done;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000691 }
692
Tamas Banf70ef8c2017-12-19 15:35:09 +0000693 rc = boot_write_image_ok(fap);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000694
695done:
696 flash_area_close(fap);
697 return rc;
698}
David Vinczebb6e3b62019-07-31 14:24:05 +0200699
Tamas Ban056ed0b2019-09-16 12:48:32 +0100700/**
701 * Checks whether on overflow can happen during a summation operation
702 *
703 * @param a First operand of summation
704 *
705 * @param b Second operand of summation
706 *
707 * @return True in case of overflow, false otherwise
708 */
709bool
710boot_add_uint32_overflow_check(uint32_t a, uint32_t b)
711{
712 return (a > UINT32_MAX - b);
713}
714
715/**
716 * Checks whether on overflow can happen during a summation operation
717 *
718 * @param a First operand of summation
719 *
720 * @param b Second operand of summation
721 *
722 * @return True in case of overflow, false otherwise
723 */
724bool
725boot_add_uint16_overflow_check(uint16_t a, uint16_t b)
726{
727 return (a > UINT16_MAX - b);
728}