blob: c076c310f1ca781eb03ca69394f9ffcc98853b3e [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
David Vincze39e78552018-10-10 17:10:01 +0200104static int
105boot_magic_decode(const uint32_t *magic)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000106{
Tamas Banf70ef8c2017-12-19 15:35:09 +0000107 if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0) {
108 return BOOT_MAGIC_GOOD;
109 }
David Vincze39e78552018-10-10 17:10:01 +0200110 return BOOT_MAGIC_BAD;
111}
Tamas Banf70ef8c2017-12-19 15:35:09 +0000112
David Vincze39e78552018-10-10 17:10:01 +0200113static int
114boot_flag_decode(uint8_t flag)
115{
116 if (flag != BOOT_FLAG_SET) {
117 return BOOT_FLAG_BAD;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000118 }
David Vincze39e78552018-10-10 17:10:01 +0200119 return BOOT_FLAG_SET;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000120}
121
David Vincze401c7422019-06-21 20:44:05 +0200122/**
123 * Determines if a status source table is satisfied by the specified magic
124 * code.
125 *
126 * @param tbl_val A magic field from a status source table.
127 * @param val The magic value in a trailer, encoded as a
128 * BOOT_MAGIC_[...].
129 *
130 * @return 1 if the two values are compatible;
131 * 0 otherwise.
132 */
133int
134boot_magic_compatible_check(uint8_t tbl_val, uint8_t val)
135{
136 switch (tbl_val) {
137 case BOOT_MAGIC_ANY:
138 return 1;
139
140 case BOOT_MAGIC_NOTGOOD:
141 return val != BOOT_MAGIC_GOOD;
142
143 default:
144 return tbl_val == val;
145 }
146}
147
Tamas Banf70ef8c2017-12-19 15:35:09 +0000148uint32_t
David Vincze401c7422019-06-21 20:44:05 +0200149boot_trailer_sz(uint8_t min_write_sz)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000150{
151 return /* state for all sectors */
152 BOOT_STATUS_MAX_ENTRIES * BOOT_STATUS_STATE_COUNT * min_write_sz +
David Vincze401c7422019-06-21 20:44:05 +0200153 /* swap_type + copy_done + image_ok + swap_size */
154 BOOT_MAX_ALIGN * 4 +
Tamas Banf70ef8c2017-12-19 15:35:09 +0000155 BOOT_MAGIC_SZ;
156}
157
158static uint32_t
159boot_magic_off(const struct flash_area *fap)
160{
Tamas Banf70ef8c2017-12-19 15:35:09 +0000161 return fap->fa_size - BOOT_MAGIC_SZ;
162}
163
164int
165boot_status_entries(const struct flash_area *fap)
166{
167 switch (fap->fa_id) {
David Vincze8bdfc2d2019-03-18 15:49:23 +0100168 case FLASH_AREA_IMAGE_PRIMARY:
169 case FLASH_AREA_IMAGE_SECONDARY:
Tamas Banf70ef8c2017-12-19 15:35:09 +0000170 return BOOT_STATUS_STATE_COUNT * BOOT_STATUS_MAX_ENTRIES;
171 case FLASH_AREA_IMAGE_SCRATCH:
172 return BOOT_STATUS_STATE_COUNT;
173 default:
174 return BOOT_EBADARGS;
175 }
176}
177
178uint32_t
179boot_status_off(const struct flash_area *fap)
180{
181 uint32_t off_from_end;
182 uint8_t elem_sz;
183
184 elem_sz = flash_area_align(fap);
185
David Vincze401c7422019-06-21 20:44:05 +0200186 off_from_end = boot_trailer_sz(elem_sz);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000187
188 assert(off_from_end <= fap->fa_size);
189 return fap->fa_size - off_from_end;
190}
191
David Vincze401c7422019-06-21 20:44:05 +0200192uint32_t
David Vincze91b71ef2019-06-24 13:06:47 +0200193boot_swap_info_off(const struct flash_area *fap)
David Vincze401c7422019-06-21 20:44:05 +0200194{
195 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 3;
196}
197
Tamas Banf70ef8c2017-12-19 15:35:09 +0000198static uint32_t
199boot_copy_done_off(const struct flash_area *fap)
200{
Tamas Banf70ef8c2017-12-19 15:35:09 +0000201 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 2;
202}
203
204static uint32_t
205boot_image_ok_off(const struct flash_area *fap)
206{
Tamas Banf70ef8c2017-12-19 15:35:09 +0000207 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN;
208}
209
210static uint32_t
211boot_swap_size_off(const struct flash_area *fap)
212{
David Vincze401c7422019-06-21 20:44:05 +0200213 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 4;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000214}
215
216int
217boot_read_swap_state(const struct flash_area *fap,
218 struct boot_swap_state *state)
219{
David Vincze39e78552018-10-10 17:10:01 +0200220 uint32_t magic[BOOT_MAGIC_ARR_SZ];
Tamas Banf70ef8c2017-12-19 15:35:09 +0000221 uint32_t off;
David Vincze91b71ef2019-06-24 13:06:47 +0200222 uint8_t swap_info;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000223 int rc;
224
225 off = boot_magic_off(fap);
David Vincze39e78552018-10-10 17:10:01 +0200226 rc = flash_area_read_is_empty(fap, off, magic, BOOT_MAGIC_SZ);
227 if (rc < 0) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000228 return BOOT_EFLASH;
229 }
David Vincze39e78552018-10-10 17:10:01 +0200230 if (rc == 1) {
231 state->magic = BOOT_MAGIC_UNSET;
232 } else {
233 state->magic = boot_magic_decode(magic);
234 }
Tamas Banf70ef8c2017-12-19 15:35:09 +0000235
David Vincze91b71ef2019-06-24 13:06:47 +0200236 off = boot_swap_info_off(fap);
237 rc = flash_area_read_is_empty(fap, off, &swap_info, sizeof swap_info);
David Vincze401c7422019-06-21 20:44:05 +0200238 if (rc < 0) {
239 return BOOT_EFLASH;
240 }
David Vincze91b71ef2019-06-24 13:06:47 +0200241
242 /* Extract the swap type and image number */
243 state->swap_type = BOOT_GET_SWAP_TYPE(swap_info);
244 state->image_num = BOOT_GET_IMAGE_NUM(swap_info);
245
David Vincze401c7422019-06-21 20:44:05 +0200246 if (rc == 1 || state->swap_type > BOOT_SWAP_TYPE_REVERT) {
247 state->swap_type = BOOT_SWAP_TYPE_NONE;
David Vincze91b71ef2019-06-24 13:06:47 +0200248 state->image_num = 0;
David Vincze401c7422019-06-21 20:44:05 +0200249 }
250
251 off = boot_copy_done_off(fap);
252 rc = flash_area_read_is_empty(fap, off, &state->copy_done,
253 sizeof state->copy_done);
254 if (rc < 0) {
255 return BOOT_EFLASH;
256 }
257 if (rc == 1) {
258 state->copy_done = BOOT_FLAG_UNSET;
259 } else {
260 state->copy_done = boot_flag_decode(state->copy_done);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000261 }
262
263 off = boot_image_ok_off(fap);
David Vincze401c7422019-06-21 20:44:05 +0200264 rc = flash_area_read_is_empty(fap, off, &state->image_ok,
265 sizeof state->image_ok);
David Vincze39e78552018-10-10 17:10:01 +0200266 if (rc < 0) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000267 return BOOT_EFLASH;
268 }
David Vincze39e78552018-10-10 17:10:01 +0200269 if (rc == 1) {
270 state->image_ok = BOOT_FLAG_UNSET;
271 } else {
272 state->image_ok = boot_flag_decode(state->image_ok);
273 }
Tamas Banf70ef8c2017-12-19 15:35:09 +0000274
275 return 0;
276}
277
278/**
279 * Reads the image trailer from the scratch area.
280 */
281int
282boot_read_swap_state_by_id(int flash_area_id, struct boot_swap_state *state)
283{
284 const struct flash_area *fap;
285 int rc;
286
287 switch (flash_area_id) {
288 case FLASH_AREA_IMAGE_SCRATCH:
David Vincze8bdfc2d2019-03-18 15:49:23 +0100289 case FLASH_AREA_IMAGE_PRIMARY:
290 case FLASH_AREA_IMAGE_SECONDARY:
Tamas Banf70ef8c2017-12-19 15:35:09 +0000291 rc = flash_area_open(flash_area_id, &fap);
292 if (rc != 0) {
293 return BOOT_EFLASH;
294 }
295 break;
296 default:
297 return BOOT_EBADARGS;
298 }
299
300 rc = boot_read_swap_state(fap, state);
301 flash_area_close(fap);
302 return rc;
303}
304
305int
306boot_read_swap_size(uint32_t *swap_size)
307{
David Vincze39e78552018-10-10 17:10:01 +0200308 uint32_t magic[BOOT_MAGIC_ARR_SZ];
Tamas Banf70ef8c2017-12-19 15:35:09 +0000309 uint32_t off;
310 const struct flash_area *fap;
311 int rc;
312
313 /*
314 * In the middle a swap, tries to locate the saved swap size. Looks
David Vincze8bdfc2d2019-03-18 15:49:23 +0100315 * for a valid magic, first on the primary slot, then on scratch.
316 * Both "slots" can end up being temporary storage for a swap and it
317 * is assumed that if magic is valid then swap size is too, because
318 * magic is always written in the last step.
Tamas Banf70ef8c2017-12-19 15:35:09 +0000319 */
320
David Vincze8bdfc2d2019-03-18 15:49:23 +0100321 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000322 if (rc != 0) {
323 return BOOT_EFLASH;
324 }
325
326 off = boot_magic_off(fap);
327 rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ);
328 if (rc != 0) {
329 rc = BOOT_EFLASH;
330 goto out;
331 }
332
333 if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) != 0) {
334 /*
David Vincze8bdfc2d2019-03-18 15:49:23 +0100335 * If the primary slot's magic is not valid, try scratch...
Tamas Banf70ef8c2017-12-19 15:35:09 +0000336 */
337
338 flash_area_close(fap);
339
340 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap);
341 if (rc != 0) {
342 return BOOT_EFLASH;
343 }
344
345 off = boot_magic_off(fap);
346 rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ);
347 if (rc != 0) {
348 rc = BOOT_EFLASH;
349 goto out;
350 }
351
352 assert(memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0);
353 }
354
355 off = boot_swap_size_off(fap);
Tamas Ban581034a2017-12-19 19:54:37 +0000356 rc = flash_area_read(fap, off, swap_size, sizeof(*swap_size));
Tamas Banf70ef8c2017-12-19 15:35:09 +0000357 if (rc != 0) {
358 rc = BOOT_EFLASH;
359 }
360
361out:
362 flash_area_close(fap);
363 return rc;
364}
365
366
367int
368boot_write_magic(const struct flash_area *fap)
369{
370 uint32_t off;
371 int rc;
372
373 off = boot_magic_off(fap);
374
David Vincze401c7422019-06-21 20:44:05 +0200375 BOOT_LOG_DBG("writing magic; fa_id=%d off=0x%x (0x%x)",
376 fap->fa_id, off, fap->fa_off + off);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000377 rc = flash_area_write(fap, off, boot_img_magic, BOOT_MAGIC_SZ);
378 if (rc != 0) {
379 return BOOT_EFLASH;
380 }
381
382 return 0;
383}
384
385static int
David Vincze401c7422019-06-21 20:44:05 +0200386boot_write_trailer_byte(const struct flash_area *fap, uint32_t off,
387 uint8_t val)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000388{
Tamas Banf70ef8c2017-12-19 15:35:09 +0000389 uint8_t buf[BOOT_MAX_ALIGN];
390 uint8_t align;
David Vincze39e78552018-10-10 17:10:01 +0200391 uint8_t erased_val;
David Vincze401c7422019-06-21 20:44:05 +0200392 int rc;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000393
Tamas Banc3828852018-02-01 12:24:16 +0000394 align = flash_area_align(fap);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000395 assert(align <= BOOT_MAX_ALIGN);
David Vincze39e78552018-10-10 17:10:01 +0200396 erased_val = flash_area_erased_val(fap);
397 memset(buf, erased_val, BOOT_MAX_ALIGN);
David Vincze401c7422019-06-21 20:44:05 +0200398 buf[0] = val;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000399
400 rc = flash_area_write(fap, off, buf, align);
401 if (rc != 0) {
402 return BOOT_EFLASH;
403 }
404
405 return 0;
406}
407
408int
409boot_write_copy_done(const struct flash_area *fap)
410{
David Vincze401c7422019-06-21 20:44:05 +0200411 uint32_t off;
412
413 off = boot_copy_done_off(fap);
414 BOOT_LOG_DBG("writing copy_done; fa_id=%d off=0x%x (0x%x)",
415 fap->fa_id, off, fap->fa_off + off);
416 return boot_write_trailer_byte(fap, off, BOOT_FLAG_SET);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000417}
418
419int
420boot_write_image_ok(const struct flash_area *fap)
421{
David Vincze401c7422019-06-21 20:44:05 +0200422 uint32_t off;
423
424 off = boot_image_ok_off(fap);
425 BOOT_LOG_DBG("writing image_ok; fa_id=%d off=0x%x (0x%x)",
426 fap->fa_id, off, fap->fa_off + off);
427 return boot_write_trailer_byte(fap, off, BOOT_FLAG_SET);
428}
429
430/**
431 * Writes the specified value to the `swap-type` field of an image trailer.
432 * This value is persisted so that the boot loader knows what swap operation to
433 * resume in case of an unexpected reset.
434 */
435int
David Vincze91b71ef2019-06-24 13:06:47 +0200436boot_write_swap_info(const struct flash_area *fap, uint8_t swap_type,
437 uint8_t image_num)
David Vincze401c7422019-06-21 20:44:05 +0200438{
439 uint32_t off;
David Vincze91b71ef2019-06-24 13:06:47 +0200440 uint8_t swap_info;
David Vincze401c7422019-06-21 20:44:05 +0200441
David Vincze91b71ef2019-06-24 13:06:47 +0200442 BOOT_SET_SWAP_INFO(swap_info, image_num, swap_type);
443 off = boot_swap_info_off(fap);
444 BOOT_LOG_DBG("writing swap_info; fa_id=%d off=0x%x (0x%x), swap_type=0x%x"
445 " image_num=0x%x",
446 fap->fa_id, off, fap->fa_off + off,
447 BOOT_GET_SWAP_TYPE(swap_info),
448 BOOT_GET_IMAGE_NUM(swap_info));
449 return boot_write_trailer_byte(fap, off, swap_info);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000450}
451
452int
453boot_write_swap_size(const struct flash_area *fap, uint32_t swap_size)
454{
455 uint32_t off;
456 int rc;
457 uint8_t buf[BOOT_MAX_ALIGN];
458 uint8_t align;
David Vincze39e78552018-10-10 17:10:01 +0200459 uint8_t erased_val;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000460
461 off = boot_swap_size_off(fap);
Tamas Banc3828852018-02-01 12:24:16 +0000462 align = flash_area_align(fap);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000463 assert(align <= BOOT_MAX_ALIGN);
Tamas Ban581034a2017-12-19 19:54:37 +0000464 if (align < sizeof(swap_size)) {
465 align = sizeof(swap_size);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000466 }
David Vincze39e78552018-10-10 17:10:01 +0200467 erased_val = flash_area_erased_val(fap);
468 memset(buf, erased_val, BOOT_MAX_ALIGN);
Tamas Ban581034a2017-12-19 19:54:37 +0000469 memcpy(buf, (uint8_t *)&swap_size, sizeof(swap_size));
Tamas Banf70ef8c2017-12-19 15:35:09 +0000470
David Vincze401c7422019-06-21 20:44:05 +0200471 BOOT_LOG_DBG("writing swap_size; fa_id=%d off=0x%x (0x%x)",
472 fap->fa_id, off, fap->fa_off + off);
473
Tamas Banf70ef8c2017-12-19 15:35:09 +0000474 rc = flash_area_write(fap, off, buf, align);
475 if (rc != 0) {
476 return BOOT_EFLASH;
477 }
478
479 return 0;
480}
481
482int
483boot_swap_type(void)
484{
485 const struct boot_swap_table *table;
David Vincze8bdfc2d2019-03-18 15:49:23 +0100486 struct boot_swap_state primary_slot;
487 struct boot_swap_state secondary_slot;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000488 int rc;
David Vincze39e78552018-10-10 17:10:01 +0200489 size_t i;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000490
David Vincze8bdfc2d2019-03-18 15:49:23 +0100491 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY, &primary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000492 if (rc) {
493 return BOOT_SWAP_TYPE_PANIC;
494 }
495
David Vincze8bdfc2d2019-03-18 15:49:23 +0100496 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY,
497 &secondary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000498 if (rc) {
499 return BOOT_SWAP_TYPE_PANIC;
500 }
501
502 for (i = 0; i < BOOT_SWAP_TABLES_COUNT; i++) {
503 table = boot_swap_tables + i;
504
David Vincze401c7422019-06-21 20:44:05 +0200505 if (boot_magic_compatible_check(table->magic_primary_slot,
506 primary_slot.magic) &&
507 boot_magic_compatible_check(table->magic_secondary_slot,
508 secondary_slot.magic) &&
David Vincze8bdfc2d2019-03-18 15:49:23 +0100509 (table->image_ok_primary_slot == BOOT_FLAG_ANY ||
510 table->image_ok_primary_slot == primary_slot.image_ok) &&
511 (table->image_ok_secondary_slot == BOOT_FLAG_ANY ||
512 table->image_ok_secondary_slot == secondary_slot.image_ok) &&
513 (table->copy_done_primary_slot == BOOT_FLAG_ANY ||
514 table->copy_done_primary_slot == primary_slot.copy_done)) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000515 BOOT_LOG_INF("Swap type: %s",
516 table->swap_type == BOOT_SWAP_TYPE_TEST ? "test" :
517 table->swap_type == BOOT_SWAP_TYPE_PERM ? "perm" :
518 table->swap_type == BOOT_SWAP_TYPE_REVERT ? "revert" :
519 "BUG; can't happen");
520 assert(table->swap_type == BOOT_SWAP_TYPE_TEST ||
521 table->swap_type == BOOT_SWAP_TYPE_PERM ||
522 table->swap_type == BOOT_SWAP_TYPE_REVERT);
523 return table->swap_type;
524 }
525 }
526
527 BOOT_LOG_INF("Swap type: none");
528 return BOOT_SWAP_TYPE_NONE;
529}
530
531/**
David Vincze8bdfc2d2019-03-18 15:49:23 +0100532 * Marks the image in the secondary slot as pending. On the next reboot,
533 * the system will perform a one-time boot of the the secondary slot image.
Tamas Banf70ef8c2017-12-19 15:35:09 +0000534 *
535 * @param permanent Whether the image should be used permanently or
536 * only tested once:
537 * 0=run image once, then confirm or revert.
538 * 1=run image forever.
539 *
540 * @return 0 on success; nonzero on failure.
541 */
542int
543boot_set_pending(int permanent)
544{
Tamas Ban581034a2017-12-19 19:54:37 +0000545 const struct flash_area *fap = NULL;
David Vincze8bdfc2d2019-03-18 15:49:23 +0100546 struct boot_swap_state state_secondary_slot;
David Vincze401c7422019-06-21 20:44:05 +0200547 uint8_t swap_type;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000548 int rc;
549
David Vincze8bdfc2d2019-03-18 15:49:23 +0100550 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY,
551 &state_secondary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000552 if (rc != 0) {
553 return rc;
554 }
555
David Vincze8bdfc2d2019-03-18 15:49:23 +0100556 switch (state_secondary_slot.magic) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000557 case BOOT_MAGIC_GOOD:
558 /* Swap already scheduled. */
559 return 0;
560
561 case BOOT_MAGIC_UNSET:
David Vincze8bdfc2d2019-03-18 15:49:23 +0100562 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY, &fap);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000563 if (rc != 0) {
564 rc = BOOT_EFLASH;
565 } else {
566 rc = boot_write_magic(fap);
567 }
568
569 if (rc == 0 && permanent) {
570 rc = boot_write_image_ok(fap);
571 }
572
David Vincze401c7422019-06-21 20:44:05 +0200573 if (rc == 0) {
574 if (permanent) {
575 swap_type = BOOT_SWAP_TYPE_PERM;
576 } else {
577 swap_type = BOOT_SWAP_TYPE_TEST;
578 }
David Vincze91b71ef2019-06-24 13:06:47 +0200579 rc = boot_write_swap_info(fap, swap_type, 0);
David Vincze401c7422019-06-21 20:44:05 +0200580 }
581
Tamas Banf70ef8c2017-12-19 15:35:09 +0000582 flash_area_close(fap);
583 return rc;
584
David Vincze401c7422019-06-21 20:44:05 +0200585 case BOOT_MAGIC_BAD:
586 /* The image slot is corrupt. There is no way to recover, so erase the
587 * slot to allow future upgrades.
588 */
589 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY, &fap);
590 if (rc != 0) {
591 return BOOT_EFLASH;
592 }
593
594 flash_area_erase(fap, 0, fap->fa_size);
595 flash_area_close(fap);
596 return BOOT_EBADIMAGE;
597
Tamas Banf70ef8c2017-12-19 15:35:09 +0000598 default:
Tamas Banf70ef8c2017-12-19 15:35:09 +0000599 assert(0);
David Vincze401c7422019-06-21 20:44:05 +0200600 return BOOT_EBADIMAGE;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000601 }
602}
603
604/**
David Vincze8bdfc2d2019-03-18 15:49:23 +0100605 * Marks the image in the primary slot as confirmed. The system will continue
606 * booting into the image in the primary slot until told to boot from a
607 * different slot.
Tamas Banf70ef8c2017-12-19 15:35:09 +0000608 *
Tamas Ban581034a2017-12-19 19:54:37 +0000609 * @return 0 on success; non-zero on failure.
Tamas Banf70ef8c2017-12-19 15:35:09 +0000610 */
611int
612boot_set_confirmed(void)
613{
Tamas Ban581034a2017-12-19 19:54:37 +0000614 const struct flash_area *fap = NULL;
David Vincze8bdfc2d2019-03-18 15:49:23 +0100615 struct boot_swap_state state_primary_slot;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000616 int rc;
617
David Vincze8bdfc2d2019-03-18 15:49:23 +0100618 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY,
619 &state_primary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000620 if (rc != 0) {
621 return rc;
622 }
623
David Vincze8bdfc2d2019-03-18 15:49:23 +0100624 switch (state_primary_slot.magic) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000625 case BOOT_MAGIC_GOOD:
626 /* Confirm needed; proceed. */
627 break;
628
629 case BOOT_MAGIC_UNSET:
630 /* Already confirmed. */
631 return 0;
632
633 case BOOT_MAGIC_BAD:
634 /* Unexpected state. */
635 return BOOT_EBADVECT;
636 }
637
David Vincze8bdfc2d2019-03-18 15:49:23 +0100638 if (state_primary_slot.copy_done == BOOT_FLAG_UNSET) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000639 /* Swap never completed. This is unexpected. */
David Vincze39e78552018-10-10 17:10:01 +0200640 rc = BOOT_EBADVECT;
641 goto done;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000642 }
643
David Vincze8bdfc2d2019-03-18 15:49:23 +0100644 if (state_primary_slot.image_ok != BOOT_FLAG_UNSET) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000645 /* Already confirmed. */
David Vincze39e78552018-10-10 17:10:01 +0200646 goto done;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000647 }
648
David Vincze8bdfc2d2019-03-18 15:49:23 +0100649 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000650 if (rc) {
651 rc = BOOT_EFLASH;
652 goto done;
653 }
654
655 rc = boot_write_image_ok(fap);
656 if (rc != 0) {
657 goto done;
658 }
659
660 rc = 0;
661
662done:
663 flash_area_close(fap);
664 return rc;
665}