blob: 8af74ca983d1f592a7e8270b7fa33f3545d7998c [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 Vinczecea8b592019-10-29 16:09:51 +010023 * Git SHA of the original version: 4f0ea747c314547daa6b6299ccbd77ae4dee6758
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 Ban581034a2017-12-19 19:54:37 +000052const uint32_t BOOT_MAGIC_SZ = sizeof(boot_img_magic);
Tamas Banf70ef8c2017-12-19 15:35:09 +000053const uint32_t BOOT_MAX_ALIGN = MAX_FLASH_ALIGN;
54
55struct boot_swap_table {
David Vincze8bdfc2d2019-03-18 15:49:23 +010056 uint8_t magic_primary_slot;
57 uint8_t magic_secondary_slot;
58 uint8_t image_ok_primary_slot;
59 uint8_t image_ok_secondary_slot;
60 uint8_t copy_done_primary_slot;
Tamas Banf70ef8c2017-12-19 15:35:09 +000061
62 uint8_t swap_type;
63};
64
65/**
66 * This set of tables maps image trailer contents to swap operation type.
67 * When searching for a match, these tables must be iterated sequentially.
68 *
David Vincze8bdfc2d2019-03-18 15:49:23 +010069 * NOTE: the table order is very important. The settings in the secondary
70 * slot always are priority to the primary slot and should be located
71 * earlier in the table.
Tamas Banf70ef8c2017-12-19 15:35:09 +000072 *
73 * The table lists only states where there is action needs to be taken by
74 * the bootloader, as in starting/finishing a swap operation.
75 */
76static const struct boot_swap_table boot_swap_tables[] = {
77 {
David Vincze8bdfc2d2019-03-18 15:49:23 +010078 .magic_primary_slot = BOOT_MAGIC_ANY,
79 .magic_secondary_slot = BOOT_MAGIC_GOOD,
80 .image_ok_primary_slot = BOOT_FLAG_ANY,
81 .image_ok_secondary_slot = BOOT_FLAG_UNSET,
82 .copy_done_primary_slot = BOOT_FLAG_ANY,
83 .swap_type = BOOT_SWAP_TYPE_TEST,
Tamas Banf70ef8c2017-12-19 15:35:09 +000084 },
85 {
David Vincze8bdfc2d2019-03-18 15:49:23 +010086 .magic_primary_slot = BOOT_MAGIC_ANY,
87 .magic_secondary_slot = BOOT_MAGIC_GOOD,
88 .image_ok_primary_slot = BOOT_FLAG_ANY,
89 .image_ok_secondary_slot = BOOT_FLAG_SET,
90 .copy_done_primary_slot = BOOT_FLAG_ANY,
91 .swap_type = BOOT_SWAP_TYPE_PERM,
Tamas Banf70ef8c2017-12-19 15:35:09 +000092 },
93 {
David Vincze8bdfc2d2019-03-18 15:49:23 +010094 .magic_primary_slot = BOOT_MAGIC_GOOD,
95 .magic_secondary_slot = BOOT_MAGIC_UNSET,
96 .image_ok_primary_slot = BOOT_FLAG_UNSET,
97 .image_ok_secondary_slot = BOOT_FLAG_ANY,
98 .copy_done_primary_slot = BOOT_FLAG_SET,
99 .swap_type = BOOT_SWAP_TYPE_REVERT,
Tamas Banf70ef8c2017-12-19 15:35:09 +0000100 },
101};
102
103#define BOOT_SWAP_TABLES_COUNT \
Tamas Ban581034a2017-12-19 19:54:37 +0000104 (sizeof(boot_swap_tables) / sizeof(boot_swap_tables[0]))
Tamas Banf70ef8c2017-12-19 15:35:09 +0000105
Raef Coles25b857a2019-09-05 13:59:55 +0100106/**
107 * @brief Determine if the data at two memory addresses is equal
108 *
109 * @param s1 The first memory region to compare.
110 * @param s2 The second memory region to compare.
111 * @param n The amount of bytes to compare.
112 *
113 * @note This function does not comply with the specification of memcmp,
114 * so should not be considered a drop-in replacement.
115 *
116 * @return 0 if memory regions are equal.
117 */
118uint32_t boot_secure_memequal(const void *s1, const void *s2, size_t n)
119{
120 size_t i;
121 uint8_t *s1_p = (uint8_t*) s1;
122 uint8_t *s2_p = (uint8_t*) s2;
123 uint32_t ret = 0;
124
125 for (i = 0; i < n; i++) {
126 ret |= (s1_p[i] ^ s2_p[i]);
127 }
128
129 return ret;
130}
131
David Vincze39e78552018-10-10 17:10:01 +0200132static int
133boot_magic_decode(const uint32_t *magic)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000134{
Raef Coles25b857a2019-09-05 13:59:55 +0100135 if (boot_secure_memequal(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000136 return BOOT_MAGIC_GOOD;
137 }
David Vincze39e78552018-10-10 17:10:01 +0200138 return BOOT_MAGIC_BAD;
139}
Tamas Banf70ef8c2017-12-19 15:35:09 +0000140
David Vincze39e78552018-10-10 17:10:01 +0200141static int
142boot_flag_decode(uint8_t flag)
143{
144 if (flag != BOOT_FLAG_SET) {
145 return BOOT_FLAG_BAD;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000146 }
David Vincze39e78552018-10-10 17:10:01 +0200147 return BOOT_FLAG_SET;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000148}
149
David Vincze401c7422019-06-21 20:44:05 +0200150/**
151 * Determines if a status source table is satisfied by the specified magic
152 * code.
153 *
154 * @param tbl_val A magic field from a status source table.
155 * @param val The magic value in a trailer, encoded as a
156 * BOOT_MAGIC_[...].
157 *
158 * @return 1 if the two values are compatible;
159 * 0 otherwise.
160 */
161int
162boot_magic_compatible_check(uint8_t tbl_val, uint8_t val)
163{
164 switch (tbl_val) {
165 case BOOT_MAGIC_ANY:
166 return 1;
167
168 case BOOT_MAGIC_NOTGOOD:
169 return val != BOOT_MAGIC_GOOD;
170
171 default:
172 return tbl_val == val;
173 }
174}
175
Tamas Banf70ef8c2017-12-19 15:35:09 +0000176uint32_t
Raef Coles204c5b42019-09-05 09:18:47 +0100177boot_trailer_sz(uint32_t min_write_sz)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000178{
179 return /* state for all sectors */
180 BOOT_STATUS_MAX_ENTRIES * BOOT_STATUS_STATE_COUNT * min_write_sz +
David Vincze401c7422019-06-21 20:44:05 +0200181 /* swap_type + copy_done + image_ok + swap_size */
182 BOOT_MAX_ALIGN * 4 +
Tamas Banf70ef8c2017-12-19 15:35:09 +0000183 BOOT_MAGIC_SZ;
184}
185
Tamas Banf70ef8c2017-12-19 15:35:09 +0000186int
David Vinczecea8b592019-10-29 16:09:51 +0100187boot_status_entries(int image_index, const struct flash_area *fap)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000188{
David Vinczebb207982019-08-21 15:13:04 +0200189 if (fap->fa_id == FLASH_AREA_IMAGE_SCRATCH) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000190 return BOOT_STATUS_STATE_COUNT;
David Vinczecea8b592019-10-29 16:09:51 +0100191 } else if ((fap->fa_id == FLASH_AREA_IMAGE_PRIMARY(image_index)) ||
192 (fap->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index))) {
David Vinczebb207982019-08-21 15:13:04 +0200193 return BOOT_STATUS_STATE_COUNT * BOOT_STATUS_MAX_ENTRIES;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000194 }
David Vinczecea8b592019-10-29 16:09:51 +0100195 return -1;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000196}
197
198uint32_t
199boot_status_off(const struct flash_area *fap)
200{
201 uint32_t off_from_end;
Raef Coles204c5b42019-09-05 09:18:47 +0100202 uint32_t elem_sz;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000203
204 elem_sz = flash_area_align(fap);
205
David Vincze401c7422019-06-21 20:44:05 +0200206 off_from_end = boot_trailer_sz(elem_sz);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000207
208 assert(off_from_end <= fap->fa_size);
209 return fap->fa_size - off_from_end;
210}
211
David Vinczecea8b592019-10-29 16:09:51 +0100212static inline uint32_t
213boot_magic_off(const struct flash_area *fap)
214{
215 return fap->fa_size - BOOT_MAGIC_SZ;
216}
217
218static inline uint32_t
219boot_image_ok_off(const struct flash_area *fap)
220{
221 return boot_magic_off(fap) - BOOT_MAX_ALIGN;
222}
223
224static inline uint32_t
225boot_copy_done_off(const struct flash_area *fap)
226{
227 return boot_image_ok_off(fap) - BOOT_MAX_ALIGN;
228}
229
David Vincze401c7422019-06-21 20:44:05 +0200230uint32_t
David Vincze91b71ef2019-06-24 13:06:47 +0200231boot_swap_info_off(const struct flash_area *fap)
David Vincze401c7422019-06-21 20:44:05 +0200232{
David Vinczecea8b592019-10-29 16:09:51 +0100233 return boot_copy_done_off(fap) - BOOT_MAX_ALIGN;
David Vincze401c7422019-06-21 20:44:05 +0200234}
235
David Vinczecea8b592019-10-29 16:09:51 +0100236static inline uint32_t
Tamas Banf70ef8c2017-12-19 15:35:09 +0000237boot_swap_size_off(const struct flash_area *fap)
238{
David Vinczecea8b592019-10-29 16:09:51 +0100239 return boot_swap_info_off(fap) - BOOT_MAX_ALIGN;
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 Vinczecea8b592019-10-29 16:09:51 +0100313 rc = flash_area_open(flash_area_id, &fap);
314 if (rc != 0) {
315 return BOOT_EFLASH;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000316 }
317
318 rc = boot_read_swap_state(fap, state);
319 flash_area_close(fap);
320 return rc;
321}
322
David Vinczecea8b592019-10-29 16:09:51 +0100323/**
324 * This functions tries to locate the status area after an aborted swap,
325 * by looking for the magic in the possible locations.
326 *
327 * If the magic is sucessfully found, a flash_area * is returned and it
328 * is the responsibility of the called to close it.
329 *
330 * @returns 0 on success, -1 on errors
331 */
332static int
333boot_find_status(int image_index, const struct flash_area **fap)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000334{
David Vincze39e78552018-10-10 17:10:01 +0200335 uint32_t magic[BOOT_MAGIC_ARR_SZ];
Tamas Banf70ef8c2017-12-19 15:35:09 +0000336 uint32_t off;
David Vinczecea8b592019-10-29 16:09:51 +0100337 uint8_t areas[2] = {
338 FLASH_AREA_IMAGE_PRIMARY(image_index),
339 FLASH_AREA_IMAGE_SCRATCH,
340 };
341 unsigned int i;
342 int rc;
343
344 /*
345 * In the middle a swap, tries to locate the area that is currently
346 * storing a valid magic, first on the primary slot, then on scratch.
347 * Both "slots" can end up being temporary storage for a swap and it
348 * is assumed that if magic is valid then other metadata is too,
349 * because magic is always written in the last step.
350 */
351
352 for (i = 0; i < sizeof(areas) / sizeof(areas[0]); i++) {
353 rc = flash_area_open(areas[i], fap);
354 if (rc != 0) {
355 return rc;
356 }
357
358 off = boot_magic_off(*fap);
359 rc = flash_area_read(*fap, off, magic, BOOT_MAGIC_SZ);
360 if (rc != 0) {
361 flash_area_close(*fap);
362 return rc;
363 }
364
365 if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0) {
366 return 0;
367 }
368
369 flash_area_close(*fap);
370 }
371
372 /* If we got here, no magic was found */
373 return -1;
374}
375
376int
377boot_read_swap_size(int image_index, uint32_t *swap_size)
378{
379 uint32_t off;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000380 const struct flash_area *fap;
381 int rc;
382
David Vinczecea8b592019-10-29 16:09:51 +0100383 rc = boot_find_status(image_index, &fap);
384 if (rc == 0) {
385 off = boot_swap_size_off(fap);
386 rc = flash_area_read(fap, off, swap_size, sizeof *swap_size);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000387 flash_area_close(fap);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000388 }
389
Tamas Banf70ef8c2017-12-19 15:35:09 +0000390 return rc;
391}
392
Tamas Banf70ef8c2017-12-19 15:35:09 +0000393int
394boot_write_magic(const struct flash_area *fap)
395{
396 uint32_t off;
397 int rc;
398
399 off = boot_magic_off(fap);
400
David Vinczecea8b592019-10-29 16:09:51 +0100401 BOOT_LOG_DBG("writing magic; fa_id=%d off=0x%lx (0x%lx)",
402 fap->fa_id, (unsigned long)off,
403 (unsigned long)(fap->fa_off + off));
Tamas Banf70ef8c2017-12-19 15:35:09 +0000404 rc = flash_area_write(fap, off, boot_img_magic, BOOT_MAGIC_SZ);
405 if (rc != 0) {
406 return BOOT_EFLASH;
407 }
408
409 return 0;
410}
411
David Vinczecea8b592019-10-29 16:09:51 +0100412/**
413 * Write trailer data; status bytes, swap_size, etc
414 *
415 * @returns 0 on success, != 0 on error.
416 */
Tamas Banf70ef8c2017-12-19 15:35:09 +0000417static int
David Vinczecea8b592019-10-29 16:09:51 +0100418boot_write_trailer(const struct flash_area *fap, uint32_t off,
419 const uint8_t *inbuf, uint8_t inlen)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000420{
Tamas Banf70ef8c2017-12-19 15:35:09 +0000421 uint8_t buf[BOOT_MAX_ALIGN];
David Vinczecea8b592019-10-29 16:09:51 +0100422 uint8_t align;
David Vincze39e78552018-10-10 17:10:01 +0200423 uint8_t erased_val;
David Vincze401c7422019-06-21 20:44:05 +0200424 int rc;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000425
Tamas Banc3828852018-02-01 12:24:16 +0000426 align = flash_area_align(fap);
David Vinczecea8b592019-10-29 16:09:51 +0100427 if (inlen > BOOT_MAX_ALIGN || align > BOOT_MAX_ALIGN) {
428 return -1;
429 }
David Vincze39e78552018-10-10 17:10:01 +0200430 erased_val = flash_area_erased_val(fap);
David Vinczecea8b592019-10-29 16:09:51 +0100431 if (align < inlen) {
432 align = inlen;
433 }
434 memcpy(buf, inbuf, inlen);
435 memset(&buf[inlen], erased_val, align - inlen);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000436
437 rc = flash_area_write(fap, off, buf, align);
438 if (rc != 0) {
439 return BOOT_EFLASH;
440 }
441
442 return 0;
443}
444
David Vinczecea8b592019-10-29 16:09:51 +0100445static int
446boot_write_trailer_flag(const struct flash_area *fap, uint32_t off,
447 uint8_t flag_val)
448{
449 const uint8_t buf[1] = { flag_val };
450 return boot_write_trailer(fap, off, buf, 1);
451}
452
Tamas Banf70ef8c2017-12-19 15:35:09 +0000453int
454boot_write_copy_done(const struct flash_area *fap)
455{
David Vincze401c7422019-06-21 20:44:05 +0200456 uint32_t off;
457
458 off = boot_copy_done_off(fap);
David Vinczecea8b592019-10-29 16:09:51 +0100459 BOOT_LOG_DBG("writing copy_done; fa_id=%d off=0x%lx (0x%lx)",
460 fap->fa_id, (unsigned long)off,
461 (unsigned long)(fap->fa_off + off));
462 return boot_write_trailer_flag(fap, off, BOOT_FLAG_SET);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000463}
464
465int
466boot_write_image_ok(const struct flash_area *fap)
467{
David Vincze401c7422019-06-21 20:44:05 +0200468 uint32_t off;
469
470 off = boot_image_ok_off(fap);
David Vinczecea8b592019-10-29 16:09:51 +0100471 BOOT_LOG_DBG("writing image_ok; fa_id=%d off=0x%lx (0x%lx)",
472 fap->fa_id, (unsigned long)off,
473 (unsigned long)(fap->fa_off + off));
474 return boot_write_trailer_flag(fap, off, BOOT_FLAG_SET);
David Vincze401c7422019-06-21 20:44:05 +0200475}
476
477/**
478 * Writes the specified value to the `swap-type` field of an image trailer.
479 * This value is persisted so that the boot loader knows what swap operation to
480 * resume in case of an unexpected reset.
481 */
482int
David Vincze91b71ef2019-06-24 13:06:47 +0200483boot_write_swap_info(const struct flash_area *fap, uint8_t swap_type,
484 uint8_t image_num)
David Vincze401c7422019-06-21 20:44:05 +0200485{
486 uint32_t off;
David Vincze91b71ef2019-06-24 13:06:47 +0200487 uint8_t swap_info;
David Vincze401c7422019-06-21 20:44:05 +0200488
David Vincze91b71ef2019-06-24 13:06:47 +0200489 BOOT_SET_SWAP_INFO(swap_info, image_num, swap_type);
490 off = boot_swap_info_off(fap);
David Vinczecea8b592019-10-29 16:09:51 +0100491 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 +0200492 " image_num=0x%x",
David Vinczecea8b592019-10-29 16:09:51 +0100493 fap->fa_id, (unsigned long)off,
494 (unsigned long)(fap->fa_off + off), swap_type, image_num);
495 return boot_write_trailer(fap, off, (const uint8_t *) &swap_info, 1);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000496}
497
498int
499boot_write_swap_size(const struct flash_area *fap, uint32_t swap_size)
500{
501 uint32_t off;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000502
503 off = boot_swap_size_off(fap);
David Vinczecea8b592019-10-29 16:09:51 +0100504 BOOT_LOG_DBG("writing swap_size; fa_id=%d off=0x%lx (0x%lx)",
505 fap->fa_id, (unsigned long)off,
506 (unsigned long)fap->fa_off + off);
507 return boot_write_trailer(fap, off, (const uint8_t *) &swap_size, 4);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000508}
509
510int
David Vinczecea8b592019-10-29 16:09:51 +0100511boot_swap_type_multi(int image_index)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000512{
513 const struct boot_swap_table *table;
David Vincze8bdfc2d2019-03-18 15:49:23 +0100514 struct boot_swap_state primary_slot;
515 struct boot_swap_state secondary_slot;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000516 int rc;
David Vincze39e78552018-10-10 17:10:01 +0200517 size_t i;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000518
David Vinczecea8b592019-10-29 16:09:51 +0100519 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(image_index),
520 &primary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000521 if (rc) {
522 return BOOT_SWAP_TYPE_PANIC;
523 }
524
David Vinczecea8b592019-10-29 16:09:51 +0100525 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(image_index),
David Vincze8bdfc2d2019-03-18 15:49:23 +0100526 &secondary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000527 if (rc) {
528 return BOOT_SWAP_TYPE_PANIC;
529 }
530
531 for (i = 0; i < BOOT_SWAP_TABLES_COUNT; i++) {
532 table = boot_swap_tables + i;
533
David Vincze401c7422019-06-21 20:44:05 +0200534 if (boot_magic_compatible_check(table->magic_primary_slot,
535 primary_slot.magic) &&
536 boot_magic_compatible_check(table->magic_secondary_slot,
537 secondary_slot.magic) &&
David Vincze8bdfc2d2019-03-18 15:49:23 +0100538 (table->image_ok_primary_slot == BOOT_FLAG_ANY ||
539 table->image_ok_primary_slot == primary_slot.image_ok) &&
540 (table->image_ok_secondary_slot == BOOT_FLAG_ANY ||
541 table->image_ok_secondary_slot == secondary_slot.image_ok) &&
542 (table->copy_done_primary_slot == BOOT_FLAG_ANY ||
543 table->copy_done_primary_slot == primary_slot.copy_done)) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000544 BOOT_LOG_INF("Swap type: %s",
545 table->swap_type == BOOT_SWAP_TYPE_TEST ? "test" :
546 table->swap_type == BOOT_SWAP_TYPE_PERM ? "perm" :
547 table->swap_type == BOOT_SWAP_TYPE_REVERT ? "revert" :
548 "BUG; can't happen");
David Vinczecea8b592019-10-29 16:09:51 +0100549 if (table->swap_type != BOOT_SWAP_TYPE_TEST &&
550 table->swap_type != BOOT_SWAP_TYPE_PERM &&
551 table->swap_type != BOOT_SWAP_TYPE_REVERT) {
552 return BOOT_SWAP_TYPE_PANIC;
553 }
Tamas Banf70ef8c2017-12-19 15:35:09 +0000554 return table->swap_type;
555 }
556 }
557
558 BOOT_LOG_INF("Swap type: none");
559 return BOOT_SWAP_TYPE_NONE;
560}
561
David Vinczecea8b592019-10-29 16:09:51 +0100562/*
563 * This function is not used by the bootloader itself, but its required API
564 * by external tooling like mcumgr.
565 */
566int
567boot_swap_type(void)
568{
569 return boot_swap_type_multi(0);
570}
571
Tamas Banf70ef8c2017-12-19 15:35:09 +0000572/**
David Vincze8bdfc2d2019-03-18 15:49:23 +0100573 * Marks the image in the secondary slot as pending. On the next reboot,
574 * the system will perform a one-time boot of the the secondary slot image.
Tamas Banf70ef8c2017-12-19 15:35:09 +0000575 *
576 * @param permanent Whether the image should be used permanently or
577 * only tested once:
578 * 0=run image once, then confirm or revert.
579 * 1=run image forever.
580 *
581 * @return 0 on success; nonzero on failure.
582 */
583int
584boot_set_pending(int permanent)
585{
Tamas Ban581034a2017-12-19 19:54:37 +0000586 const struct flash_area *fap = NULL;
David Vincze8bdfc2d2019-03-18 15:49:23 +0100587 struct boot_swap_state state_secondary_slot;
David Vincze401c7422019-06-21 20:44:05 +0200588 uint8_t swap_type;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000589 int rc;
590
David Vinczecea8b592019-10-29 16:09:51 +0100591 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(0),
David Vincze8bdfc2d2019-03-18 15:49:23 +0100592 &state_secondary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000593 if (rc != 0) {
594 return rc;
595 }
596
David Vincze8bdfc2d2019-03-18 15:49:23 +0100597 switch (state_secondary_slot.magic) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000598 case BOOT_MAGIC_GOOD:
599 /* Swap already scheduled. */
600 return 0;
601
602 case BOOT_MAGIC_UNSET:
David Vinczecea8b592019-10-29 16:09:51 +0100603 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(0), &fap);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000604 if (rc != 0) {
605 rc = BOOT_EFLASH;
606 } else {
607 rc = boot_write_magic(fap);
608 }
609
610 if (rc == 0 && permanent) {
611 rc = boot_write_image_ok(fap);
612 }
613
David Vincze401c7422019-06-21 20:44:05 +0200614 if (rc == 0) {
615 if (permanent) {
616 swap_type = BOOT_SWAP_TYPE_PERM;
617 } else {
618 swap_type = BOOT_SWAP_TYPE_TEST;
619 }
David Vincze91b71ef2019-06-24 13:06:47 +0200620 rc = boot_write_swap_info(fap, swap_type, 0);
David Vincze401c7422019-06-21 20:44:05 +0200621 }
622
Tamas Banf70ef8c2017-12-19 15:35:09 +0000623 flash_area_close(fap);
624 return rc;
625
David Vincze401c7422019-06-21 20:44:05 +0200626 case BOOT_MAGIC_BAD:
627 /* The image slot is corrupt. There is no way to recover, so erase the
628 * slot to allow future upgrades.
629 */
David Vinczecea8b592019-10-29 16:09:51 +0100630 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(0), &fap);
David Vincze401c7422019-06-21 20:44:05 +0200631 if (rc != 0) {
632 return BOOT_EFLASH;
633 }
634
635 flash_area_erase(fap, 0, fap->fa_size);
636 flash_area_close(fap);
637 return BOOT_EBADIMAGE;
638
Tamas Banf70ef8c2017-12-19 15:35:09 +0000639 default:
Tamas Banf70ef8c2017-12-19 15:35:09 +0000640 assert(0);
David Vincze401c7422019-06-21 20:44:05 +0200641 return BOOT_EBADIMAGE;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000642 }
643}
644
645/**
David Vincze8bdfc2d2019-03-18 15:49:23 +0100646 * Marks the image in the primary slot as confirmed. The system will continue
647 * booting into the image in the primary slot until told to boot from a
648 * different slot.
Tamas Banf70ef8c2017-12-19 15:35:09 +0000649 *
Tamas Ban581034a2017-12-19 19:54:37 +0000650 * @return 0 on success; non-zero on failure.
Tamas Banf70ef8c2017-12-19 15:35:09 +0000651 */
652int
653boot_set_confirmed(void)
654{
Tamas Ban581034a2017-12-19 19:54:37 +0000655 const struct flash_area *fap = NULL;
David Vincze8bdfc2d2019-03-18 15:49:23 +0100656 struct boot_swap_state state_primary_slot;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000657 int rc;
658
David Vinczecea8b592019-10-29 16:09:51 +0100659 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(0),
David Vincze8bdfc2d2019-03-18 15:49:23 +0100660 &state_primary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000661 if (rc != 0) {
662 return rc;
663 }
664
David Vincze8bdfc2d2019-03-18 15:49:23 +0100665 switch (state_primary_slot.magic) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000666 case BOOT_MAGIC_GOOD:
667 /* Confirm needed; proceed. */
668 break;
669
670 case BOOT_MAGIC_UNSET:
671 /* Already confirmed. */
672 return 0;
673
674 case BOOT_MAGIC_BAD:
675 /* Unexpected state. */
676 return BOOT_EBADVECT;
677 }
678
David Vinczecea8b592019-10-29 16:09:51 +0100679 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(0), &fap);
680 if (rc) {
681 rc = BOOT_EFLASH;
682 goto done;
683 }
684
David Vincze8bdfc2d2019-03-18 15:49:23 +0100685 if (state_primary_slot.copy_done == BOOT_FLAG_UNSET) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000686 /* Swap never completed. This is unexpected. */
David Vincze39e78552018-10-10 17:10:01 +0200687 rc = BOOT_EBADVECT;
688 goto done;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000689 }
690
David Vincze8bdfc2d2019-03-18 15:49:23 +0100691 if (state_primary_slot.image_ok != BOOT_FLAG_UNSET) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000692 /* Already confirmed. */
David Vincze39e78552018-10-10 17:10:01 +0200693 goto done;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000694 }
695
Tamas Banf70ef8c2017-12-19 15:35:09 +0000696 rc = boot_write_image_ok(fap);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000697
698done:
699 flash_area_close(fap);
700 return rc;
701}
David Vinczebb6e3b62019-07-31 14:24:05 +0200702
Tamas Ban056ed0b2019-09-16 12:48:32 +0100703/**
704 * Checks whether on overflow can happen during a summation operation
705 *
706 * @param a First operand of summation
707 *
708 * @param b Second operand of summation
709 *
710 * @return True in case of overflow, false otherwise
711 */
712bool
713boot_add_uint32_overflow_check(uint32_t a, uint32_t b)
714{
715 return (a > UINT32_MAX - b);
716}
717
718/**
719 * Checks whether on overflow can happen during a summation operation
720 *
721 * @param a First operand of summation
722 *
723 * @param b Second operand of summation
724 *
725 * @return True in case of overflow, false otherwise
726 */
727bool
728boot_add_uint16_overflow_check(uint16_t a, uint16_t b)
729{
730 return (a > UINT16_MAX - b);
731}