blob: f83db99458cdc5b396b2a48987d6b89f674d5713 [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
23 * Git SHA of the original version: 3c469bc698a9767859ed73cd0201c44161204d5c
24 * 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
39int boot_current_slot;
40
41const uint32_t boot_img_magic[] = {
42 0xf395c277,
43 0x7fefd260,
44 0x0f505235,
45 0x8079b62c,
46};
47
David Vincze39e78552018-10-10 17:10:01 +020048#define BOOT_MAGIC_ARR_SZ \
49 (sizeof boot_img_magic / sizeof boot_img_magic[0])
50
Tamas Ban581034a2017-12-19 19:54:37 +000051const uint32_t BOOT_MAGIC_SZ = sizeof(boot_img_magic);
Tamas Banf70ef8c2017-12-19 15:35:09 +000052const uint32_t BOOT_MAX_ALIGN = MAX_FLASH_ALIGN;
53
54struct boot_swap_table {
David Vincze8bdfc2d2019-03-18 15:49:23 +010055 uint8_t magic_primary_slot;
56 uint8_t magic_secondary_slot;
57 uint8_t image_ok_primary_slot;
58 uint8_t image_ok_secondary_slot;
59 uint8_t copy_done_primary_slot;
Tamas Banf70ef8c2017-12-19 15:35:09 +000060
61 uint8_t swap_type;
62};
63
64/**
65 * This set of tables maps image trailer contents to swap operation type.
66 * When searching for a match, these tables must be iterated sequentially.
67 *
David Vincze8bdfc2d2019-03-18 15:49:23 +010068 * NOTE: the table order is very important. The settings in the secondary
69 * slot always are priority to the primary slot and should be located
70 * earlier in the table.
Tamas Banf70ef8c2017-12-19 15:35:09 +000071 *
72 * The table lists only states where there is action needs to be taken by
73 * the bootloader, as in starting/finishing a swap operation.
74 */
75static const struct boot_swap_table boot_swap_tables[] = {
76 {
David Vincze8bdfc2d2019-03-18 15:49:23 +010077 .magic_primary_slot = BOOT_MAGIC_ANY,
78 .magic_secondary_slot = BOOT_MAGIC_GOOD,
79 .image_ok_primary_slot = BOOT_FLAG_ANY,
80 .image_ok_secondary_slot = BOOT_FLAG_UNSET,
81 .copy_done_primary_slot = BOOT_FLAG_ANY,
82 .swap_type = BOOT_SWAP_TYPE_TEST,
Tamas Banf70ef8c2017-12-19 15:35:09 +000083 },
84 {
David Vincze8bdfc2d2019-03-18 15:49:23 +010085 .magic_primary_slot = BOOT_MAGIC_ANY,
86 .magic_secondary_slot = BOOT_MAGIC_GOOD,
87 .image_ok_primary_slot = BOOT_FLAG_ANY,
88 .image_ok_secondary_slot = BOOT_FLAG_SET,
89 .copy_done_primary_slot = BOOT_FLAG_ANY,
90 .swap_type = BOOT_SWAP_TYPE_PERM,
Tamas Banf70ef8c2017-12-19 15:35:09 +000091 },
92 {
David Vincze8bdfc2d2019-03-18 15:49:23 +010093 .magic_primary_slot = BOOT_MAGIC_GOOD,
94 .magic_secondary_slot = BOOT_MAGIC_UNSET,
95 .image_ok_primary_slot = BOOT_FLAG_UNSET,
96 .image_ok_secondary_slot = BOOT_FLAG_ANY,
97 .copy_done_primary_slot = BOOT_FLAG_SET,
98 .swap_type = BOOT_SWAP_TYPE_REVERT,
Tamas Banf70ef8c2017-12-19 15:35:09 +000099 },
100};
101
102#define BOOT_SWAP_TABLES_COUNT \
Tamas Ban581034a2017-12-19 19:54:37 +0000103 (sizeof(boot_swap_tables) / sizeof(boot_swap_tables[0]))
Tamas Banf70ef8c2017-12-19 15:35:09 +0000104
Raef Coles25b857a2019-09-05 13:59:55 +0100105/**
106 * @brief Determine if the data at two memory addresses is equal
107 *
108 * @param s1 The first memory region to compare.
109 * @param s2 The second memory region to compare.
110 * @param n The amount of bytes to compare.
111 *
112 * @note This function does not comply with the specification of memcmp,
113 * so should not be considered a drop-in replacement.
114 *
115 * @return 0 if memory regions are equal.
116 */
117uint32_t boot_secure_memequal(const void *s1, const void *s2, size_t n)
118{
119 size_t i;
120 uint8_t *s1_p = (uint8_t*) s1;
121 uint8_t *s2_p = (uint8_t*) s2;
122 uint32_t ret = 0;
123
124 for (i = 0; i < n; i++) {
125 ret |= (s1_p[i] ^ s2_p[i]);
126 }
127
128 return ret;
129}
130
David Vincze39e78552018-10-10 17:10:01 +0200131static int
132boot_magic_decode(const uint32_t *magic)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000133{
Raef Coles25b857a2019-09-05 13:59:55 +0100134 if (boot_secure_memequal(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000135 return BOOT_MAGIC_GOOD;
136 }
David Vincze39e78552018-10-10 17:10:01 +0200137 return BOOT_MAGIC_BAD;
138}
Tamas Banf70ef8c2017-12-19 15:35:09 +0000139
David Vincze39e78552018-10-10 17:10:01 +0200140static int
141boot_flag_decode(uint8_t flag)
142{
143 if (flag != BOOT_FLAG_SET) {
144 return BOOT_FLAG_BAD;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000145 }
David Vincze39e78552018-10-10 17:10:01 +0200146 return BOOT_FLAG_SET;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000147}
148
David Vincze401c7422019-06-21 20:44:05 +0200149/**
150 * Determines if a status source table is satisfied by the specified magic
151 * code.
152 *
153 * @param tbl_val A magic field from a status source table.
154 * @param val The magic value in a trailer, encoded as a
155 * BOOT_MAGIC_[...].
156 *
157 * @return 1 if the two values are compatible;
158 * 0 otherwise.
159 */
160int
161boot_magic_compatible_check(uint8_t tbl_val, uint8_t val)
162{
163 switch (tbl_val) {
164 case BOOT_MAGIC_ANY:
165 return 1;
166
167 case BOOT_MAGIC_NOTGOOD:
168 return val != BOOT_MAGIC_GOOD;
169
170 default:
171 return tbl_val == val;
172 }
173}
174
Tamas Banf70ef8c2017-12-19 15:35:09 +0000175uint32_t
Raef Coles204c5b42019-09-05 09:18:47 +0100176boot_trailer_sz(uint32_t min_write_sz)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000177{
178 return /* state for all sectors */
179 BOOT_STATUS_MAX_ENTRIES * BOOT_STATUS_STATE_COUNT * min_write_sz +
David Vincze401c7422019-06-21 20:44:05 +0200180 /* swap_type + copy_done + image_ok + swap_size */
181 BOOT_MAX_ALIGN * 4 +
Tamas Banf70ef8c2017-12-19 15:35:09 +0000182 BOOT_MAGIC_SZ;
183}
184
185static uint32_t
186boot_magic_off(const struct flash_area *fap)
187{
Tamas Banf70ef8c2017-12-19 15:35:09 +0000188 return fap->fa_size - BOOT_MAGIC_SZ;
189}
190
191int
192boot_status_entries(const struct flash_area *fap)
193{
David Vinczebb207982019-08-21 15:13:04 +0200194 if (fap->fa_id == FLASH_AREA_IMAGE_SCRATCH) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000195 return BOOT_STATUS_STATE_COUNT;
David Vinczebb207982019-08-21 15:13:04 +0200196 } else if ((fap->fa_id == FLASH_AREA_IMAGE_PRIMARY) ||
197 (fap->fa_id == FLASH_AREA_IMAGE_SECONDARY)) {
198 return BOOT_STATUS_STATE_COUNT * BOOT_STATUS_MAX_ENTRIES;
199 } else {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000200 return BOOT_EBADARGS;
201 }
202}
203
204uint32_t
205boot_status_off(const struct flash_area *fap)
206{
207 uint32_t off_from_end;
Raef Coles204c5b42019-09-05 09:18:47 +0100208 uint32_t elem_sz;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000209
210 elem_sz = flash_area_align(fap);
211
David Vincze401c7422019-06-21 20:44:05 +0200212 off_from_end = boot_trailer_sz(elem_sz);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000213
214 assert(off_from_end <= fap->fa_size);
215 return fap->fa_size - off_from_end;
216}
217
David Vincze401c7422019-06-21 20:44:05 +0200218uint32_t
David Vincze91b71ef2019-06-24 13:06:47 +0200219boot_swap_info_off(const struct flash_area *fap)
David Vincze401c7422019-06-21 20:44:05 +0200220{
221 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 3;
222}
223
Tamas Banf70ef8c2017-12-19 15:35:09 +0000224static uint32_t
225boot_copy_done_off(const struct flash_area *fap)
226{
Tamas Banf70ef8c2017-12-19 15:35:09 +0000227 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 2;
228}
229
230static uint32_t
231boot_image_ok_off(const struct flash_area *fap)
232{
Tamas Banf70ef8c2017-12-19 15:35:09 +0000233 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN;
234}
235
236static uint32_t
237boot_swap_size_off(const struct flash_area *fap)
238{
David Vincze401c7422019-06-21 20:44:05 +0200239 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 4;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000240}
241
242int
243boot_read_swap_state(const struct flash_area *fap,
244 struct boot_swap_state *state)
245{
David Vincze39e78552018-10-10 17:10:01 +0200246 uint32_t magic[BOOT_MAGIC_ARR_SZ];
Tamas Banf70ef8c2017-12-19 15:35:09 +0000247 uint32_t off;
David Vincze91b71ef2019-06-24 13:06:47 +0200248 uint8_t swap_info;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000249 int rc;
250
251 off = boot_magic_off(fap);
David Vincze39e78552018-10-10 17:10:01 +0200252 rc = flash_area_read_is_empty(fap, off, magic, BOOT_MAGIC_SZ);
253 if (rc < 0) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000254 return BOOT_EFLASH;
255 }
David Vincze39e78552018-10-10 17:10:01 +0200256 if (rc == 1) {
257 state->magic = BOOT_MAGIC_UNSET;
258 } else {
259 state->magic = boot_magic_decode(magic);
260 }
Tamas Banf70ef8c2017-12-19 15:35:09 +0000261
David Vincze91b71ef2019-06-24 13:06:47 +0200262 off = boot_swap_info_off(fap);
263 rc = flash_area_read_is_empty(fap, off, &swap_info, sizeof swap_info);
David Vincze401c7422019-06-21 20:44:05 +0200264 if (rc < 0) {
265 return BOOT_EFLASH;
266 }
David Vincze91b71ef2019-06-24 13:06:47 +0200267
268 /* Extract the swap type and image number */
269 state->swap_type = BOOT_GET_SWAP_TYPE(swap_info);
270 state->image_num = BOOT_GET_IMAGE_NUM(swap_info);
271
David Vincze401c7422019-06-21 20:44:05 +0200272 if (rc == 1 || state->swap_type > BOOT_SWAP_TYPE_REVERT) {
273 state->swap_type = BOOT_SWAP_TYPE_NONE;
David Vincze91b71ef2019-06-24 13:06:47 +0200274 state->image_num = 0;
David Vincze401c7422019-06-21 20:44:05 +0200275 }
276
277 off = boot_copy_done_off(fap);
278 rc = flash_area_read_is_empty(fap, off, &state->copy_done,
279 sizeof state->copy_done);
280 if (rc < 0) {
281 return BOOT_EFLASH;
282 }
283 if (rc == 1) {
284 state->copy_done = BOOT_FLAG_UNSET;
285 } else {
286 state->copy_done = boot_flag_decode(state->copy_done);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000287 }
288
289 off = boot_image_ok_off(fap);
David Vincze401c7422019-06-21 20:44:05 +0200290 rc = flash_area_read_is_empty(fap, off, &state->image_ok,
291 sizeof state->image_ok);
David Vincze39e78552018-10-10 17:10:01 +0200292 if (rc < 0) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000293 return BOOT_EFLASH;
294 }
David Vincze39e78552018-10-10 17:10:01 +0200295 if (rc == 1) {
296 state->image_ok = BOOT_FLAG_UNSET;
297 } else {
298 state->image_ok = boot_flag_decode(state->image_ok);
299 }
Tamas Banf70ef8c2017-12-19 15:35:09 +0000300
301 return 0;
302}
303
304/**
305 * Reads the image trailer from the scratch area.
306 */
307int
308boot_read_swap_state_by_id(int flash_area_id, struct boot_swap_state *state)
309{
310 const struct flash_area *fap;
311 int rc;
312
David Vinczebb207982019-08-21 15:13:04 +0200313 if (flash_area_id == FLASH_AREA_IMAGE_SCRATCH ||
314 flash_area_id == FLASH_AREA_IMAGE_PRIMARY ||
315 flash_area_id == FLASH_AREA_IMAGE_SECONDARY) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000316 rc = flash_area_open(flash_area_id, &fap);
317 if (rc != 0) {
318 return BOOT_EFLASH;
319 }
David Vinczebb207982019-08-21 15:13:04 +0200320 } else {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000321 return BOOT_EBADARGS;
322 }
323
324 rc = boot_read_swap_state(fap, state);
325 flash_area_close(fap);
326 return rc;
327}
328
329int
330boot_read_swap_size(uint32_t *swap_size)
331{
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;
334 const struct flash_area *fap;
335 int rc;
336
337 /*
338 * In the middle a swap, tries to locate the saved swap size. Looks
David Vincze8bdfc2d2019-03-18 15:49:23 +0100339 * for a valid magic, first on the primary slot, then on scratch.
340 * Both "slots" can end up being temporary storage for a swap and it
341 * is assumed that if magic is valid then swap size is too, because
342 * magic is always written in the last step.
Tamas Banf70ef8c2017-12-19 15:35:09 +0000343 */
344
David Vincze8bdfc2d2019-03-18 15:49:23 +0100345 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000346 if (rc != 0) {
347 return BOOT_EFLASH;
348 }
349
350 off = boot_magic_off(fap);
351 rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ);
352 if (rc != 0) {
353 rc = BOOT_EFLASH;
354 goto out;
355 }
356
Raef Coles25b857a2019-09-05 13:59:55 +0100357 if (boot_secure_memequal(magic, boot_img_magic, BOOT_MAGIC_SZ) != 0) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000358 /*
David Vincze8bdfc2d2019-03-18 15:49:23 +0100359 * If the primary slot's magic is not valid, try scratch...
Tamas Banf70ef8c2017-12-19 15:35:09 +0000360 */
361
362 flash_area_close(fap);
363
364 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap);
365 if (rc != 0) {
366 return BOOT_EFLASH;
367 }
368
369 off = boot_magic_off(fap);
370 rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ);
371 if (rc != 0) {
372 rc = BOOT_EFLASH;
373 goto out;
374 }
375
Raef Coles25b857a2019-09-05 13:59:55 +0100376 assert(boot_secure_memequal(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000377 }
378
379 off = boot_swap_size_off(fap);
Tamas Ban581034a2017-12-19 19:54:37 +0000380 rc = flash_area_read(fap, off, swap_size, sizeof(*swap_size));
Tamas Banf70ef8c2017-12-19 15:35:09 +0000381 if (rc != 0) {
382 rc = BOOT_EFLASH;
383 }
384
385out:
386 flash_area_close(fap);
387 return rc;
388}
389
390
391int
392boot_write_magic(const struct flash_area *fap)
393{
394 uint32_t off;
395 int rc;
396
397 off = boot_magic_off(fap);
398
David Vincze401c7422019-06-21 20:44:05 +0200399 BOOT_LOG_DBG("writing magic; fa_id=%d off=0x%x (0x%x)",
400 fap->fa_id, off, 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
409static int
David Vincze401c7422019-06-21 20:44:05 +0200410boot_write_trailer_byte(const struct flash_area *fap, uint32_t off,
411 uint8_t val)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000412{
Tamas Banf70ef8c2017-12-19 15:35:09 +0000413 uint8_t buf[BOOT_MAX_ALIGN];
Raef Coles204c5b42019-09-05 09:18:47 +0100414 uint32_t align;
David Vincze39e78552018-10-10 17:10:01 +0200415 uint8_t erased_val;
David Vincze401c7422019-06-21 20:44:05 +0200416 int rc;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000417
Tamas Banc3828852018-02-01 12:24:16 +0000418 align = flash_area_align(fap);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000419 assert(align <= BOOT_MAX_ALIGN);
David Vincze39e78552018-10-10 17:10:01 +0200420 erased_val = flash_area_erased_val(fap);
421 memset(buf, erased_val, BOOT_MAX_ALIGN);
David Vincze401c7422019-06-21 20:44:05 +0200422 buf[0] = val;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000423
424 rc = flash_area_write(fap, off, buf, align);
425 if (rc != 0) {
426 return BOOT_EFLASH;
427 }
428
429 return 0;
430}
431
432int
433boot_write_copy_done(const struct flash_area *fap)
434{
David Vincze401c7422019-06-21 20:44:05 +0200435 uint32_t off;
436
437 off = boot_copy_done_off(fap);
438 BOOT_LOG_DBG("writing copy_done; fa_id=%d off=0x%x (0x%x)",
439 fap->fa_id, off, fap->fa_off + off);
440 return boot_write_trailer_byte(fap, off, BOOT_FLAG_SET);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000441}
442
443int
444boot_write_image_ok(const struct flash_area *fap)
445{
David Vincze401c7422019-06-21 20:44:05 +0200446 uint32_t off;
447
448 off = boot_image_ok_off(fap);
449 BOOT_LOG_DBG("writing image_ok; fa_id=%d off=0x%x (0x%x)",
450 fap->fa_id, off, fap->fa_off + off);
451 return boot_write_trailer_byte(fap, off, BOOT_FLAG_SET);
452}
453
454/**
455 * Writes the specified value to the `swap-type` field of an image trailer.
456 * This value is persisted so that the boot loader knows what swap operation to
457 * resume in case of an unexpected reset.
458 */
459int
David Vincze91b71ef2019-06-24 13:06:47 +0200460boot_write_swap_info(const struct flash_area *fap, uint8_t swap_type,
461 uint8_t image_num)
David Vincze401c7422019-06-21 20:44:05 +0200462{
463 uint32_t off;
David Vincze91b71ef2019-06-24 13:06:47 +0200464 uint8_t swap_info;
David Vincze401c7422019-06-21 20:44:05 +0200465
David Vincze91b71ef2019-06-24 13:06:47 +0200466 BOOT_SET_SWAP_INFO(swap_info, image_num, swap_type);
467 off = boot_swap_info_off(fap);
468 BOOT_LOG_DBG("writing swap_info; fa_id=%d off=0x%x (0x%x), swap_type=0x%x"
469 " image_num=0x%x",
470 fap->fa_id, off, fap->fa_off + off,
471 BOOT_GET_SWAP_TYPE(swap_info),
472 BOOT_GET_IMAGE_NUM(swap_info));
473 return boot_write_trailer_byte(fap, off, swap_info);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000474}
475
476int
477boot_write_swap_size(const struct flash_area *fap, uint32_t swap_size)
478{
479 uint32_t off;
480 int rc;
481 uint8_t buf[BOOT_MAX_ALIGN];
Raef Coles204c5b42019-09-05 09:18:47 +0100482 uint32_t align;
David Vincze39e78552018-10-10 17:10:01 +0200483 uint8_t erased_val;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000484
485 off = boot_swap_size_off(fap);
Tamas Banc3828852018-02-01 12:24:16 +0000486 align = flash_area_align(fap);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000487 assert(align <= BOOT_MAX_ALIGN);
Tamas Ban581034a2017-12-19 19:54:37 +0000488 if (align < sizeof(swap_size)) {
489 align = sizeof(swap_size);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000490 }
David Vincze39e78552018-10-10 17:10:01 +0200491 erased_val = flash_area_erased_val(fap);
492 memset(buf, erased_val, BOOT_MAX_ALIGN);
Tamas Ban581034a2017-12-19 19:54:37 +0000493 memcpy(buf, (uint8_t *)&swap_size, sizeof(swap_size));
Tamas Banf70ef8c2017-12-19 15:35:09 +0000494
David Vincze401c7422019-06-21 20:44:05 +0200495 BOOT_LOG_DBG("writing swap_size; fa_id=%d off=0x%x (0x%x)",
496 fap->fa_id, off, fap->fa_off + off);
497
Tamas Banf70ef8c2017-12-19 15:35:09 +0000498 rc = flash_area_write(fap, off, buf, align);
499 if (rc != 0) {
500 return BOOT_EFLASH;
501 }
502
503 return 0;
504}
505
506int
507boot_swap_type(void)
508{
509 const struct boot_swap_table *table;
David Vincze8bdfc2d2019-03-18 15:49:23 +0100510 struct boot_swap_state primary_slot;
511 struct boot_swap_state secondary_slot;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000512 int rc;
David Vincze39e78552018-10-10 17:10:01 +0200513 size_t i;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000514
David Vincze8bdfc2d2019-03-18 15:49:23 +0100515 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY, &primary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000516 if (rc) {
517 return BOOT_SWAP_TYPE_PANIC;
518 }
519
David Vincze8bdfc2d2019-03-18 15:49:23 +0100520 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY,
521 &secondary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000522 if (rc) {
523 return BOOT_SWAP_TYPE_PANIC;
524 }
525
526 for (i = 0; i < BOOT_SWAP_TABLES_COUNT; i++) {
527 table = boot_swap_tables + i;
528
David Vincze401c7422019-06-21 20:44:05 +0200529 if (boot_magic_compatible_check(table->magic_primary_slot,
530 primary_slot.magic) &&
531 boot_magic_compatible_check(table->magic_secondary_slot,
532 secondary_slot.magic) &&
David Vincze8bdfc2d2019-03-18 15:49:23 +0100533 (table->image_ok_primary_slot == BOOT_FLAG_ANY ||
534 table->image_ok_primary_slot == primary_slot.image_ok) &&
535 (table->image_ok_secondary_slot == BOOT_FLAG_ANY ||
536 table->image_ok_secondary_slot == secondary_slot.image_ok) &&
537 (table->copy_done_primary_slot == BOOT_FLAG_ANY ||
538 table->copy_done_primary_slot == primary_slot.copy_done)) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000539 BOOT_LOG_INF("Swap type: %s",
540 table->swap_type == BOOT_SWAP_TYPE_TEST ? "test" :
541 table->swap_type == BOOT_SWAP_TYPE_PERM ? "perm" :
542 table->swap_type == BOOT_SWAP_TYPE_REVERT ? "revert" :
543 "BUG; can't happen");
544 assert(table->swap_type == BOOT_SWAP_TYPE_TEST ||
545 table->swap_type == BOOT_SWAP_TYPE_PERM ||
546 table->swap_type == BOOT_SWAP_TYPE_REVERT);
547 return table->swap_type;
548 }
549 }
550
551 BOOT_LOG_INF("Swap type: none");
552 return BOOT_SWAP_TYPE_NONE;
553}
554
555/**
David Vincze8bdfc2d2019-03-18 15:49:23 +0100556 * Marks the image in the secondary slot as pending. On the next reboot,
557 * the system will perform a one-time boot of the the secondary slot image.
Tamas Banf70ef8c2017-12-19 15:35:09 +0000558 *
559 * @param permanent Whether the image should be used permanently or
560 * only tested once:
561 * 0=run image once, then confirm or revert.
562 * 1=run image forever.
563 *
564 * @return 0 on success; nonzero on failure.
565 */
566int
567boot_set_pending(int permanent)
568{
Tamas Ban581034a2017-12-19 19:54:37 +0000569 const struct flash_area *fap = NULL;
David Vincze8bdfc2d2019-03-18 15:49:23 +0100570 struct boot_swap_state state_secondary_slot;
David Vincze401c7422019-06-21 20:44:05 +0200571 uint8_t swap_type;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000572 int rc;
573
David Vincze8bdfc2d2019-03-18 15:49:23 +0100574 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY,
575 &state_secondary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000576 if (rc != 0) {
577 return rc;
578 }
579
David Vincze8bdfc2d2019-03-18 15:49:23 +0100580 switch (state_secondary_slot.magic) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000581 case BOOT_MAGIC_GOOD:
582 /* Swap already scheduled. */
583 return 0;
584
585 case BOOT_MAGIC_UNSET:
David Vincze8bdfc2d2019-03-18 15:49:23 +0100586 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY, &fap);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000587 if (rc != 0) {
588 rc = BOOT_EFLASH;
589 } else {
590 rc = boot_write_magic(fap);
591 }
592
593 if (rc == 0 && permanent) {
594 rc = boot_write_image_ok(fap);
595 }
596
David Vincze401c7422019-06-21 20:44:05 +0200597 if (rc == 0) {
598 if (permanent) {
599 swap_type = BOOT_SWAP_TYPE_PERM;
600 } else {
601 swap_type = BOOT_SWAP_TYPE_TEST;
602 }
David Vincze91b71ef2019-06-24 13:06:47 +0200603 rc = boot_write_swap_info(fap, swap_type, 0);
David Vincze401c7422019-06-21 20:44:05 +0200604 }
605
Tamas Banf70ef8c2017-12-19 15:35:09 +0000606 flash_area_close(fap);
607 return rc;
608
David Vincze401c7422019-06-21 20:44:05 +0200609 case BOOT_MAGIC_BAD:
610 /* The image slot is corrupt. There is no way to recover, so erase the
611 * slot to allow future upgrades.
612 */
613 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY, &fap);
614 if (rc != 0) {
615 return BOOT_EFLASH;
616 }
617
618 flash_area_erase(fap, 0, fap->fa_size);
619 flash_area_close(fap);
620 return BOOT_EBADIMAGE;
621
Tamas Banf70ef8c2017-12-19 15:35:09 +0000622 default:
Tamas Banf70ef8c2017-12-19 15:35:09 +0000623 assert(0);
David Vincze401c7422019-06-21 20:44:05 +0200624 return BOOT_EBADIMAGE;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000625 }
626}
627
628/**
David Vincze8bdfc2d2019-03-18 15:49:23 +0100629 * Marks the image in the primary slot as confirmed. The system will continue
630 * booting into the image in the primary slot until told to boot from a
631 * different slot.
Tamas Banf70ef8c2017-12-19 15:35:09 +0000632 *
Tamas Ban581034a2017-12-19 19:54:37 +0000633 * @return 0 on success; non-zero on failure.
Tamas Banf70ef8c2017-12-19 15:35:09 +0000634 */
635int
636boot_set_confirmed(void)
637{
Tamas Ban581034a2017-12-19 19:54:37 +0000638 const struct flash_area *fap = NULL;
David Vincze8bdfc2d2019-03-18 15:49:23 +0100639 struct boot_swap_state state_primary_slot;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000640 int rc;
641
David Vincze8bdfc2d2019-03-18 15:49:23 +0100642 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY,
643 &state_primary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000644 if (rc != 0) {
645 return rc;
646 }
647
David Vincze8bdfc2d2019-03-18 15:49:23 +0100648 switch (state_primary_slot.magic) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000649 case BOOT_MAGIC_GOOD:
650 /* Confirm needed; proceed. */
651 break;
652
653 case BOOT_MAGIC_UNSET:
654 /* Already confirmed. */
655 return 0;
656
657 case BOOT_MAGIC_BAD:
658 /* Unexpected state. */
659 return BOOT_EBADVECT;
660 }
661
David Vincze8bdfc2d2019-03-18 15:49:23 +0100662 if (state_primary_slot.copy_done == BOOT_FLAG_UNSET) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000663 /* Swap never completed. This is unexpected. */
David Vincze39e78552018-10-10 17:10:01 +0200664 rc = BOOT_EBADVECT;
665 goto done;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000666 }
667
David Vincze8bdfc2d2019-03-18 15:49:23 +0100668 if (state_primary_slot.image_ok != BOOT_FLAG_UNSET) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000669 /* Already confirmed. */
David Vincze39e78552018-10-10 17:10:01 +0200670 goto done;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000671 }
672
David Vincze8bdfc2d2019-03-18 15:49:23 +0100673 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000674 if (rc) {
675 rc = BOOT_EFLASH;
676 goto done;
677 }
678
679 rc = boot_write_image_ok(fap);
680 if (rc != 0) {
681 goto done;
682 }
683
684 rc = 0;
685
686done:
687 flash_area_close(fap);
688 return rc;
689}
David Vinczebb6e3b62019-07-31 14:24:05 +0200690
691#if (BOOT_IMAGE_NUMBER > 1)
692/**
693 * Check if the version of the image is not older than required.
694 *
695 * @param req Required minimal image version.
696 * @param ver Version of the image to be checked.
697 *
698 * @return 0 if the version is sufficient, nonzero otherwise.
699 */
700int
701boot_is_version_sufficient(struct image_version *req,
702 struct image_version *ver)
703{
704 if (ver->iv_major > req->iv_major) {
705 return 0;
706 }
707 if (ver->iv_major < req->iv_major) {
708 return BOOT_EBADVERSION;
709 }
710 /* The major version numbers are equal. */
711 if (ver->iv_minor > req->iv_minor) {
712 return 0;
713 }
714 if (ver->iv_minor < req->iv_minor) {
715 return BOOT_EBADVERSION;
716 }
717 /* The minor version numbers are equal. */
718 if (ver->iv_revision < req->iv_revision) {
719 return BOOT_EBADVERSION;
720 }
721
722 return 0;
723}
724#endif /* BOOT_IMAGE_NUMBER > 1 */
Tamas Ban056ed0b2019-09-16 12:48:32 +0100725
726/**
727 * Checks whether on overflow can happen during a summation operation
728 *
729 * @param a First operand of summation
730 *
731 * @param b Second operand of summation
732 *
733 * @return True in case of overflow, false otherwise
734 */
735bool
736boot_add_uint32_overflow_check(uint32_t a, uint32_t b)
737{
738 return (a > UINT32_MAX - b);
739}
740
741/**
742 * Checks whether on overflow can happen during a summation operation
743 *
744 * @param a First operand of summation
745 *
746 * @param b Second operand of summation
747 *
748 * @return True in case of overflow, false otherwise
749 */
750bool
751boot_add_uint16_overflow_check(uint16_t a, uint16_t b)
752{
753 return (a > UINT16_MAX - b);
754}