blob: 08f2a3ca0e68040adf8749a9fa60c92e4bbddd71 [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>
31
Tamas Banf70ef8c2017-12-19 15:35:09 +000032#include "flash_map/flash_map.h"
Tamas Banf70ef8c2017-12-19 15:35:09 +000033#include "bootutil/image.h"
34#include "bootutil/bootutil.h"
35#include "bootutil_priv.h"
Tamas Banf70ef8c2017-12-19 15:35:09 +000036#include "bootutil/bootutil_log.h"
37
38int boot_current_slot;
39
40const uint32_t boot_img_magic[] = {
41 0xf395c277,
42 0x7fefd260,
43 0x0f505235,
44 0x8079b62c,
45};
46
David Vincze39e78552018-10-10 17:10:01 +020047#define BOOT_MAGIC_ARR_SZ \
48 (sizeof boot_img_magic / sizeof boot_img_magic[0])
49
Tamas Ban581034a2017-12-19 19:54:37 +000050const uint32_t BOOT_MAGIC_SZ = sizeof(boot_img_magic);
Tamas Banf70ef8c2017-12-19 15:35:09 +000051const uint32_t BOOT_MAX_ALIGN = MAX_FLASH_ALIGN;
52
53struct boot_swap_table {
David Vincze8bdfc2d2019-03-18 15:49:23 +010054 uint8_t magic_primary_slot;
55 uint8_t magic_secondary_slot;
56 uint8_t image_ok_primary_slot;
57 uint8_t image_ok_secondary_slot;
58 uint8_t copy_done_primary_slot;
Tamas Banf70ef8c2017-12-19 15:35:09 +000059
60 uint8_t swap_type;
61};
62
63/**
64 * This set of tables maps image trailer contents to swap operation type.
65 * When searching for a match, these tables must be iterated sequentially.
66 *
David Vincze8bdfc2d2019-03-18 15:49:23 +010067 * NOTE: the table order is very important. The settings in the secondary
68 * slot always are priority to the primary slot and should be located
69 * earlier in the table.
Tamas Banf70ef8c2017-12-19 15:35:09 +000070 *
71 * The table lists only states where there is action needs to be taken by
72 * the bootloader, as in starting/finishing a swap operation.
73 */
74static const struct boot_swap_table boot_swap_tables[] = {
75 {
David Vincze8bdfc2d2019-03-18 15:49:23 +010076 .magic_primary_slot = BOOT_MAGIC_ANY,
77 .magic_secondary_slot = BOOT_MAGIC_GOOD,
78 .image_ok_primary_slot = BOOT_FLAG_ANY,
79 .image_ok_secondary_slot = BOOT_FLAG_UNSET,
80 .copy_done_primary_slot = BOOT_FLAG_ANY,
81 .swap_type = BOOT_SWAP_TYPE_TEST,
Tamas Banf70ef8c2017-12-19 15:35:09 +000082 },
83 {
David Vincze8bdfc2d2019-03-18 15:49:23 +010084 .magic_primary_slot = BOOT_MAGIC_ANY,
85 .magic_secondary_slot = BOOT_MAGIC_GOOD,
86 .image_ok_primary_slot = BOOT_FLAG_ANY,
87 .image_ok_secondary_slot = BOOT_FLAG_SET,
88 .copy_done_primary_slot = BOOT_FLAG_ANY,
89 .swap_type = BOOT_SWAP_TYPE_PERM,
Tamas Banf70ef8c2017-12-19 15:35:09 +000090 },
91 {
David Vincze8bdfc2d2019-03-18 15:49:23 +010092 .magic_primary_slot = BOOT_MAGIC_GOOD,
93 .magic_secondary_slot = BOOT_MAGIC_UNSET,
94 .image_ok_primary_slot = BOOT_FLAG_UNSET,
95 .image_ok_secondary_slot = BOOT_FLAG_ANY,
96 .copy_done_primary_slot = BOOT_FLAG_SET,
97 .swap_type = BOOT_SWAP_TYPE_REVERT,
Tamas Banf70ef8c2017-12-19 15:35:09 +000098 },
99};
100
101#define BOOT_SWAP_TABLES_COUNT \
Tamas Ban581034a2017-12-19 19:54:37 +0000102 (sizeof(boot_swap_tables) / sizeof(boot_swap_tables[0]))
Tamas Banf70ef8c2017-12-19 15:35:09 +0000103
Raef Coles25b857a2019-09-05 13:59:55 +0100104/**
105 * @brief Determine if the data at two memory addresses is equal
106 *
107 * @param s1 The first memory region to compare.
108 * @param s2 The second memory region to compare.
109 * @param n The amount of bytes to compare.
110 *
111 * @note This function does not comply with the specification of memcmp,
112 * so should not be considered a drop-in replacement.
113 *
114 * @return 0 if memory regions are equal.
115 */
116uint32_t boot_secure_memequal(const void *s1, const void *s2, size_t n)
117{
118 size_t i;
119 uint8_t *s1_p = (uint8_t*) s1;
120 uint8_t *s2_p = (uint8_t*) s2;
121 uint32_t ret = 0;
122
123 for (i = 0; i < n; i++) {
124 ret |= (s1_p[i] ^ s2_p[i]);
125 }
126
127 return ret;
128}
129
David Vincze39e78552018-10-10 17:10:01 +0200130static int
131boot_magic_decode(const uint32_t *magic)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000132{
Raef Coles25b857a2019-09-05 13:59:55 +0100133 if (boot_secure_memequal(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000134 return BOOT_MAGIC_GOOD;
135 }
David Vincze39e78552018-10-10 17:10:01 +0200136 return BOOT_MAGIC_BAD;
137}
Tamas Banf70ef8c2017-12-19 15:35:09 +0000138
David Vincze39e78552018-10-10 17:10:01 +0200139static int
140boot_flag_decode(uint8_t flag)
141{
142 if (flag != BOOT_FLAG_SET) {
143 return BOOT_FLAG_BAD;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000144 }
David Vincze39e78552018-10-10 17:10:01 +0200145 return BOOT_FLAG_SET;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000146}
147
David Vincze401c7422019-06-21 20:44:05 +0200148/**
149 * Determines if a status source table is satisfied by the specified magic
150 * code.
151 *
152 * @param tbl_val A magic field from a status source table.
153 * @param val The magic value in a trailer, encoded as a
154 * BOOT_MAGIC_[...].
155 *
156 * @return 1 if the two values are compatible;
157 * 0 otherwise.
158 */
159int
160boot_magic_compatible_check(uint8_t tbl_val, uint8_t val)
161{
162 switch (tbl_val) {
163 case BOOT_MAGIC_ANY:
164 return 1;
165
166 case BOOT_MAGIC_NOTGOOD:
167 return val != BOOT_MAGIC_GOOD;
168
169 default:
170 return tbl_val == val;
171 }
172}
173
Tamas Banf70ef8c2017-12-19 15:35:09 +0000174uint32_t
Raef Coles204c5b42019-09-05 09:18:47 +0100175boot_trailer_sz(uint32_t min_write_sz)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000176{
177 return /* state for all sectors */
178 BOOT_STATUS_MAX_ENTRIES * BOOT_STATUS_STATE_COUNT * min_write_sz +
David Vincze401c7422019-06-21 20:44:05 +0200179 /* swap_type + copy_done + image_ok + swap_size */
180 BOOT_MAX_ALIGN * 4 +
Tamas Banf70ef8c2017-12-19 15:35:09 +0000181 BOOT_MAGIC_SZ;
182}
183
184static uint32_t
185boot_magic_off(const struct flash_area *fap)
186{
Tamas Banf70ef8c2017-12-19 15:35:09 +0000187 return fap->fa_size - BOOT_MAGIC_SZ;
188}
189
190int
191boot_status_entries(const struct flash_area *fap)
192{
David Vinczebb207982019-08-21 15:13:04 +0200193 if (fap->fa_id == FLASH_AREA_IMAGE_SCRATCH) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000194 return BOOT_STATUS_STATE_COUNT;
David Vinczebb207982019-08-21 15:13:04 +0200195 } else if ((fap->fa_id == FLASH_AREA_IMAGE_PRIMARY) ||
196 (fap->fa_id == FLASH_AREA_IMAGE_SECONDARY)) {
197 return BOOT_STATUS_STATE_COUNT * BOOT_STATUS_MAX_ENTRIES;
198 } else {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000199 return BOOT_EBADARGS;
200 }
201}
202
203uint32_t
204boot_status_off(const struct flash_area *fap)
205{
206 uint32_t off_from_end;
Raef Coles204c5b42019-09-05 09:18:47 +0100207 uint32_t elem_sz;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000208
209 elem_sz = flash_area_align(fap);
210
David Vincze401c7422019-06-21 20:44:05 +0200211 off_from_end = boot_trailer_sz(elem_sz);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000212
213 assert(off_from_end <= fap->fa_size);
214 return fap->fa_size - off_from_end;
215}
216
David Vincze401c7422019-06-21 20:44:05 +0200217uint32_t
David Vincze91b71ef2019-06-24 13:06:47 +0200218boot_swap_info_off(const struct flash_area *fap)
David Vincze401c7422019-06-21 20:44:05 +0200219{
220 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 3;
221}
222
Tamas Banf70ef8c2017-12-19 15:35:09 +0000223static uint32_t
224boot_copy_done_off(const struct flash_area *fap)
225{
Tamas Banf70ef8c2017-12-19 15:35:09 +0000226 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 2;
227}
228
229static uint32_t
230boot_image_ok_off(const struct flash_area *fap)
231{
Tamas Banf70ef8c2017-12-19 15:35:09 +0000232 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN;
233}
234
235static uint32_t
236boot_swap_size_off(const struct flash_area *fap)
237{
David Vincze401c7422019-06-21 20:44:05 +0200238 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 4;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000239}
240
241int
242boot_read_swap_state(const struct flash_area *fap,
243 struct boot_swap_state *state)
244{
David Vincze39e78552018-10-10 17:10:01 +0200245 uint32_t magic[BOOT_MAGIC_ARR_SZ];
Tamas Banf70ef8c2017-12-19 15:35:09 +0000246 uint32_t off;
David Vincze91b71ef2019-06-24 13:06:47 +0200247 uint8_t swap_info;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000248 int rc;
249
250 off = boot_magic_off(fap);
David Vincze39e78552018-10-10 17:10:01 +0200251 rc = flash_area_read_is_empty(fap, off, magic, BOOT_MAGIC_SZ);
252 if (rc < 0) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000253 return BOOT_EFLASH;
254 }
David Vincze39e78552018-10-10 17:10:01 +0200255 if (rc == 1) {
256 state->magic = BOOT_MAGIC_UNSET;
257 } else {
258 state->magic = boot_magic_decode(magic);
259 }
Tamas Banf70ef8c2017-12-19 15:35:09 +0000260
David Vincze91b71ef2019-06-24 13:06:47 +0200261 off = boot_swap_info_off(fap);
262 rc = flash_area_read_is_empty(fap, off, &swap_info, sizeof swap_info);
David Vincze401c7422019-06-21 20:44:05 +0200263 if (rc < 0) {
264 return BOOT_EFLASH;
265 }
David Vincze91b71ef2019-06-24 13:06:47 +0200266
267 /* Extract the swap type and image number */
268 state->swap_type = BOOT_GET_SWAP_TYPE(swap_info);
269 state->image_num = BOOT_GET_IMAGE_NUM(swap_info);
270
David Vincze401c7422019-06-21 20:44:05 +0200271 if (rc == 1 || state->swap_type > BOOT_SWAP_TYPE_REVERT) {
272 state->swap_type = BOOT_SWAP_TYPE_NONE;
David Vincze91b71ef2019-06-24 13:06:47 +0200273 state->image_num = 0;
David Vincze401c7422019-06-21 20:44:05 +0200274 }
275
276 off = boot_copy_done_off(fap);
277 rc = flash_area_read_is_empty(fap, off, &state->copy_done,
278 sizeof state->copy_done);
279 if (rc < 0) {
280 return BOOT_EFLASH;
281 }
282 if (rc == 1) {
283 state->copy_done = BOOT_FLAG_UNSET;
284 } else {
285 state->copy_done = boot_flag_decode(state->copy_done);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000286 }
287
288 off = boot_image_ok_off(fap);
David Vincze401c7422019-06-21 20:44:05 +0200289 rc = flash_area_read_is_empty(fap, off, &state->image_ok,
290 sizeof state->image_ok);
David Vincze39e78552018-10-10 17:10:01 +0200291 if (rc < 0) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000292 return BOOT_EFLASH;
293 }
David Vincze39e78552018-10-10 17:10:01 +0200294 if (rc == 1) {
295 state->image_ok = BOOT_FLAG_UNSET;
296 } else {
297 state->image_ok = boot_flag_decode(state->image_ok);
298 }
Tamas Banf70ef8c2017-12-19 15:35:09 +0000299
300 return 0;
301}
302
303/**
304 * Reads the image trailer from the scratch area.
305 */
306int
307boot_read_swap_state_by_id(int flash_area_id, struct boot_swap_state *state)
308{
309 const struct flash_area *fap;
310 int rc;
311
David Vinczebb207982019-08-21 15:13:04 +0200312 if (flash_area_id == FLASH_AREA_IMAGE_SCRATCH ||
313 flash_area_id == FLASH_AREA_IMAGE_PRIMARY ||
314 flash_area_id == FLASH_AREA_IMAGE_SECONDARY) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000315 rc = flash_area_open(flash_area_id, &fap);
316 if (rc != 0) {
317 return BOOT_EFLASH;
318 }
David Vinczebb207982019-08-21 15:13:04 +0200319 } else {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000320 return BOOT_EBADARGS;
321 }
322
323 rc = boot_read_swap_state(fap, state);
324 flash_area_close(fap);
325 return rc;
326}
327
328int
329boot_read_swap_size(uint32_t *swap_size)
330{
David Vincze39e78552018-10-10 17:10:01 +0200331 uint32_t magic[BOOT_MAGIC_ARR_SZ];
Tamas Banf70ef8c2017-12-19 15:35:09 +0000332 uint32_t off;
333 const struct flash_area *fap;
334 int rc;
335
336 /*
337 * In the middle a swap, tries to locate the saved swap size. Looks
David Vincze8bdfc2d2019-03-18 15:49:23 +0100338 * for a valid magic, first on the primary slot, then on scratch.
339 * Both "slots" can end up being temporary storage for a swap and it
340 * is assumed that if magic is valid then swap size is too, because
341 * magic is always written in the last step.
Tamas Banf70ef8c2017-12-19 15:35:09 +0000342 */
343
David Vincze8bdfc2d2019-03-18 15:49:23 +0100344 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000345 if (rc != 0) {
346 return BOOT_EFLASH;
347 }
348
349 off = boot_magic_off(fap);
350 rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ);
351 if (rc != 0) {
352 rc = BOOT_EFLASH;
353 goto out;
354 }
355
Raef Coles25b857a2019-09-05 13:59:55 +0100356 if (boot_secure_memequal(magic, boot_img_magic, BOOT_MAGIC_SZ) != 0) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000357 /*
David Vincze8bdfc2d2019-03-18 15:49:23 +0100358 * If the primary slot's magic is not valid, try scratch...
Tamas Banf70ef8c2017-12-19 15:35:09 +0000359 */
360
361 flash_area_close(fap);
362
363 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap);
364 if (rc != 0) {
365 return BOOT_EFLASH;
366 }
367
368 off = boot_magic_off(fap);
369 rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ);
370 if (rc != 0) {
371 rc = BOOT_EFLASH;
372 goto out;
373 }
374
Raef Coles25b857a2019-09-05 13:59:55 +0100375 assert(boot_secure_memequal(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000376 }
377
378 off = boot_swap_size_off(fap);
Tamas Ban581034a2017-12-19 19:54:37 +0000379 rc = flash_area_read(fap, off, swap_size, sizeof(*swap_size));
Tamas Banf70ef8c2017-12-19 15:35:09 +0000380 if (rc != 0) {
381 rc = BOOT_EFLASH;
382 }
383
384out:
385 flash_area_close(fap);
386 return rc;
387}
388
389
390int
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 Vincze401c7422019-06-21 20:44:05 +0200398 BOOT_LOG_DBG("writing magic; fa_id=%d off=0x%x (0x%x)",
399 fap->fa_id, off, fap->fa_off + off);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000400 rc = flash_area_write(fap, off, boot_img_magic, BOOT_MAGIC_SZ);
401 if (rc != 0) {
402 return BOOT_EFLASH;
403 }
404
405 return 0;
406}
407
408static int
David Vincze401c7422019-06-21 20:44:05 +0200409boot_write_trailer_byte(const struct flash_area *fap, uint32_t off,
410 uint8_t val)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000411{
Tamas Banf70ef8c2017-12-19 15:35:09 +0000412 uint8_t buf[BOOT_MAX_ALIGN];
Raef Coles204c5b42019-09-05 09:18:47 +0100413 uint32_t align;
David Vincze39e78552018-10-10 17:10:01 +0200414 uint8_t erased_val;
David Vincze401c7422019-06-21 20:44:05 +0200415 int rc;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000416
Tamas Banc3828852018-02-01 12:24:16 +0000417 align = flash_area_align(fap);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000418 assert(align <= BOOT_MAX_ALIGN);
David Vincze39e78552018-10-10 17:10:01 +0200419 erased_val = flash_area_erased_val(fap);
420 memset(buf, erased_val, BOOT_MAX_ALIGN);
David Vincze401c7422019-06-21 20:44:05 +0200421 buf[0] = val;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000422
423 rc = flash_area_write(fap, off, buf, align);
424 if (rc != 0) {
425 return BOOT_EFLASH;
426 }
427
428 return 0;
429}
430
431int
432boot_write_copy_done(const struct flash_area *fap)
433{
David Vincze401c7422019-06-21 20:44:05 +0200434 uint32_t off;
435
436 off = boot_copy_done_off(fap);
437 BOOT_LOG_DBG("writing copy_done; fa_id=%d off=0x%x (0x%x)",
438 fap->fa_id, off, fap->fa_off + off);
439 return boot_write_trailer_byte(fap, off, BOOT_FLAG_SET);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000440}
441
442int
443boot_write_image_ok(const struct flash_area *fap)
444{
David Vincze401c7422019-06-21 20:44:05 +0200445 uint32_t off;
446
447 off = boot_image_ok_off(fap);
448 BOOT_LOG_DBG("writing image_ok; fa_id=%d off=0x%x (0x%x)",
449 fap->fa_id, off, fap->fa_off + off);
450 return boot_write_trailer_byte(fap, off, BOOT_FLAG_SET);
451}
452
453/**
454 * Writes the specified value to the `swap-type` field of an image trailer.
455 * This value is persisted so that the boot loader knows what swap operation to
456 * resume in case of an unexpected reset.
457 */
458int
David Vincze91b71ef2019-06-24 13:06:47 +0200459boot_write_swap_info(const struct flash_area *fap, uint8_t swap_type,
460 uint8_t image_num)
David Vincze401c7422019-06-21 20:44:05 +0200461{
462 uint32_t off;
David Vincze91b71ef2019-06-24 13:06:47 +0200463 uint8_t swap_info;
David Vincze401c7422019-06-21 20:44:05 +0200464
David Vincze91b71ef2019-06-24 13:06:47 +0200465 BOOT_SET_SWAP_INFO(swap_info, image_num, swap_type);
466 off = boot_swap_info_off(fap);
467 BOOT_LOG_DBG("writing swap_info; fa_id=%d off=0x%x (0x%x), swap_type=0x%x"
468 " image_num=0x%x",
469 fap->fa_id, off, fap->fa_off + off,
470 BOOT_GET_SWAP_TYPE(swap_info),
471 BOOT_GET_IMAGE_NUM(swap_info));
472 return boot_write_trailer_byte(fap, off, swap_info);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000473}
474
475int
476boot_write_swap_size(const struct flash_area *fap, uint32_t swap_size)
477{
478 uint32_t off;
479 int rc;
480 uint8_t buf[BOOT_MAX_ALIGN];
Raef Coles204c5b42019-09-05 09:18:47 +0100481 uint32_t align;
David Vincze39e78552018-10-10 17:10:01 +0200482 uint8_t erased_val;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000483
484 off = boot_swap_size_off(fap);
Tamas Banc3828852018-02-01 12:24:16 +0000485 align = flash_area_align(fap);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000486 assert(align <= BOOT_MAX_ALIGN);
Tamas Ban581034a2017-12-19 19:54:37 +0000487 if (align < sizeof(swap_size)) {
488 align = sizeof(swap_size);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000489 }
David Vincze39e78552018-10-10 17:10:01 +0200490 erased_val = flash_area_erased_val(fap);
491 memset(buf, erased_val, BOOT_MAX_ALIGN);
Tamas Ban581034a2017-12-19 19:54:37 +0000492 memcpy(buf, (uint8_t *)&swap_size, sizeof(swap_size));
Tamas Banf70ef8c2017-12-19 15:35:09 +0000493
David Vincze401c7422019-06-21 20:44:05 +0200494 BOOT_LOG_DBG("writing swap_size; fa_id=%d off=0x%x (0x%x)",
495 fap->fa_id, off, fap->fa_off + off);
496
Tamas Banf70ef8c2017-12-19 15:35:09 +0000497 rc = flash_area_write(fap, off, buf, align);
498 if (rc != 0) {
499 return BOOT_EFLASH;
500 }
501
502 return 0;
503}
504
505int
506boot_swap_type(void)
507{
508 const struct boot_swap_table *table;
David Vincze8bdfc2d2019-03-18 15:49:23 +0100509 struct boot_swap_state primary_slot;
510 struct boot_swap_state secondary_slot;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000511 int rc;
David Vincze39e78552018-10-10 17:10:01 +0200512 size_t i;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000513
David Vincze8bdfc2d2019-03-18 15:49:23 +0100514 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY, &primary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000515 if (rc) {
516 return BOOT_SWAP_TYPE_PANIC;
517 }
518
David Vincze8bdfc2d2019-03-18 15:49:23 +0100519 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY,
520 &secondary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000521 if (rc) {
522 return BOOT_SWAP_TYPE_PANIC;
523 }
524
525 for (i = 0; i < BOOT_SWAP_TABLES_COUNT; i++) {
526 table = boot_swap_tables + i;
527
David Vincze401c7422019-06-21 20:44:05 +0200528 if (boot_magic_compatible_check(table->magic_primary_slot,
529 primary_slot.magic) &&
530 boot_magic_compatible_check(table->magic_secondary_slot,
531 secondary_slot.magic) &&
David Vincze8bdfc2d2019-03-18 15:49:23 +0100532 (table->image_ok_primary_slot == BOOT_FLAG_ANY ||
533 table->image_ok_primary_slot == primary_slot.image_ok) &&
534 (table->image_ok_secondary_slot == BOOT_FLAG_ANY ||
535 table->image_ok_secondary_slot == secondary_slot.image_ok) &&
536 (table->copy_done_primary_slot == BOOT_FLAG_ANY ||
537 table->copy_done_primary_slot == primary_slot.copy_done)) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000538 BOOT_LOG_INF("Swap type: %s",
539 table->swap_type == BOOT_SWAP_TYPE_TEST ? "test" :
540 table->swap_type == BOOT_SWAP_TYPE_PERM ? "perm" :
541 table->swap_type == BOOT_SWAP_TYPE_REVERT ? "revert" :
542 "BUG; can't happen");
543 assert(table->swap_type == BOOT_SWAP_TYPE_TEST ||
544 table->swap_type == BOOT_SWAP_TYPE_PERM ||
545 table->swap_type == BOOT_SWAP_TYPE_REVERT);
546 return table->swap_type;
547 }
548 }
549
550 BOOT_LOG_INF("Swap type: none");
551 return BOOT_SWAP_TYPE_NONE;
552}
553
554/**
David Vincze8bdfc2d2019-03-18 15:49:23 +0100555 * Marks the image in the secondary slot as pending. On the next reboot,
556 * the system will perform a one-time boot of the the secondary slot image.
Tamas Banf70ef8c2017-12-19 15:35:09 +0000557 *
558 * @param permanent Whether the image should be used permanently or
559 * only tested once:
560 * 0=run image once, then confirm or revert.
561 * 1=run image forever.
562 *
563 * @return 0 on success; nonzero on failure.
564 */
565int
566boot_set_pending(int permanent)
567{
Tamas Ban581034a2017-12-19 19:54:37 +0000568 const struct flash_area *fap = NULL;
David Vincze8bdfc2d2019-03-18 15:49:23 +0100569 struct boot_swap_state state_secondary_slot;
David Vincze401c7422019-06-21 20:44:05 +0200570 uint8_t swap_type;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000571 int rc;
572
David Vincze8bdfc2d2019-03-18 15:49:23 +0100573 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY,
574 &state_secondary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000575 if (rc != 0) {
576 return rc;
577 }
578
David Vincze8bdfc2d2019-03-18 15:49:23 +0100579 switch (state_secondary_slot.magic) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000580 case BOOT_MAGIC_GOOD:
581 /* Swap already scheduled. */
582 return 0;
583
584 case BOOT_MAGIC_UNSET:
David Vincze8bdfc2d2019-03-18 15:49:23 +0100585 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY, &fap);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000586 if (rc != 0) {
587 rc = BOOT_EFLASH;
588 } else {
589 rc = boot_write_magic(fap);
590 }
591
592 if (rc == 0 && permanent) {
593 rc = boot_write_image_ok(fap);
594 }
595
David Vincze401c7422019-06-21 20:44:05 +0200596 if (rc == 0) {
597 if (permanent) {
598 swap_type = BOOT_SWAP_TYPE_PERM;
599 } else {
600 swap_type = BOOT_SWAP_TYPE_TEST;
601 }
David Vincze91b71ef2019-06-24 13:06:47 +0200602 rc = boot_write_swap_info(fap, swap_type, 0);
David Vincze401c7422019-06-21 20:44:05 +0200603 }
604
Tamas Banf70ef8c2017-12-19 15:35:09 +0000605 flash_area_close(fap);
606 return rc;
607
David Vincze401c7422019-06-21 20:44:05 +0200608 case BOOT_MAGIC_BAD:
609 /* The image slot is corrupt. There is no way to recover, so erase the
610 * slot to allow future upgrades.
611 */
612 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY, &fap);
613 if (rc != 0) {
614 return BOOT_EFLASH;
615 }
616
617 flash_area_erase(fap, 0, fap->fa_size);
618 flash_area_close(fap);
619 return BOOT_EBADIMAGE;
620
Tamas Banf70ef8c2017-12-19 15:35:09 +0000621 default:
Tamas Banf70ef8c2017-12-19 15:35:09 +0000622 assert(0);
David Vincze401c7422019-06-21 20:44:05 +0200623 return BOOT_EBADIMAGE;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000624 }
625}
626
627/**
David Vincze8bdfc2d2019-03-18 15:49:23 +0100628 * Marks the image in the primary slot as confirmed. The system will continue
629 * booting into the image in the primary slot until told to boot from a
630 * different slot.
Tamas Banf70ef8c2017-12-19 15:35:09 +0000631 *
Tamas Ban581034a2017-12-19 19:54:37 +0000632 * @return 0 on success; non-zero on failure.
Tamas Banf70ef8c2017-12-19 15:35:09 +0000633 */
634int
635boot_set_confirmed(void)
636{
Tamas Ban581034a2017-12-19 19:54:37 +0000637 const struct flash_area *fap = NULL;
David Vincze8bdfc2d2019-03-18 15:49:23 +0100638 struct boot_swap_state state_primary_slot;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000639 int rc;
640
David Vincze8bdfc2d2019-03-18 15:49:23 +0100641 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY,
642 &state_primary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000643 if (rc != 0) {
644 return rc;
645 }
646
David Vincze8bdfc2d2019-03-18 15:49:23 +0100647 switch (state_primary_slot.magic) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000648 case BOOT_MAGIC_GOOD:
649 /* Confirm needed; proceed. */
650 break;
651
652 case BOOT_MAGIC_UNSET:
653 /* Already confirmed. */
654 return 0;
655
656 case BOOT_MAGIC_BAD:
657 /* Unexpected state. */
658 return BOOT_EBADVECT;
659 }
660
David Vincze8bdfc2d2019-03-18 15:49:23 +0100661 if (state_primary_slot.copy_done == BOOT_FLAG_UNSET) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000662 /* Swap never completed. This is unexpected. */
David Vincze39e78552018-10-10 17:10:01 +0200663 rc = BOOT_EBADVECT;
664 goto done;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000665 }
666
David Vincze8bdfc2d2019-03-18 15:49:23 +0100667 if (state_primary_slot.image_ok != BOOT_FLAG_UNSET) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000668 /* Already confirmed. */
David Vincze39e78552018-10-10 17:10:01 +0200669 goto done;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000670 }
671
David Vincze8bdfc2d2019-03-18 15:49:23 +0100672 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000673 if (rc) {
674 rc = BOOT_EFLASH;
675 goto done;
676 }
677
678 rc = boot_write_image_ok(fap);
679 if (rc != 0) {
680 goto done;
681 }
682
683 rc = 0;
684
685done:
686 flash_area_close(fap);
687 return rc;
688}
David Vinczebb6e3b62019-07-31 14:24:05 +0200689
690#if (BOOT_IMAGE_NUMBER > 1)
691/**
692 * Check if the version of the image is not older than required.
693 *
694 * @param req Required minimal image version.
695 * @param ver Version of the image to be checked.
696 *
697 * @return 0 if the version is sufficient, nonzero otherwise.
698 */
699int
700boot_is_version_sufficient(struct image_version *req,
701 struct image_version *ver)
702{
703 if (ver->iv_major > req->iv_major) {
704 return 0;
705 }
706 if (ver->iv_major < req->iv_major) {
707 return BOOT_EBADVERSION;
708 }
709 /* The major version numbers are equal. */
710 if (ver->iv_minor > req->iv_minor) {
711 return 0;
712 }
713 if (ver->iv_minor < req->iv_minor) {
714 return BOOT_EBADVERSION;
715 }
716 /* The minor version numbers are equal. */
717 if (ver->iv_revision < req->iv_revision) {
718 return BOOT_EBADVERSION;
719 }
720
721 return 0;
722}
723#endif /* BOOT_IMAGE_NUMBER > 1 */