blob: ecd287423422bc451bfbc59975a6ac9c388117c7 [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{
David Vinczebb207982019-08-21 15:13:04 +0200167 if (fap->fa_id == FLASH_AREA_IMAGE_SCRATCH) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000168 return BOOT_STATUS_STATE_COUNT;
David Vinczebb207982019-08-21 15:13:04 +0200169 } else if ((fap->fa_id == FLASH_AREA_IMAGE_PRIMARY) ||
170 (fap->fa_id == FLASH_AREA_IMAGE_SECONDARY)) {
171 return BOOT_STATUS_STATE_COUNT * BOOT_STATUS_MAX_ENTRIES;
172 } else {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000173 return BOOT_EBADARGS;
174 }
175}
176
177uint32_t
178boot_status_off(const struct flash_area *fap)
179{
180 uint32_t off_from_end;
181 uint8_t elem_sz;
182
183 elem_sz = flash_area_align(fap);
184
David Vincze401c7422019-06-21 20:44:05 +0200185 off_from_end = boot_trailer_sz(elem_sz);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000186
187 assert(off_from_end <= fap->fa_size);
188 return fap->fa_size - off_from_end;
189}
190
David Vincze401c7422019-06-21 20:44:05 +0200191uint32_t
David Vincze91b71ef2019-06-24 13:06:47 +0200192boot_swap_info_off(const struct flash_area *fap)
David Vincze401c7422019-06-21 20:44:05 +0200193{
194 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 3;
195}
196
Tamas Banf70ef8c2017-12-19 15:35:09 +0000197static uint32_t
198boot_copy_done_off(const struct flash_area *fap)
199{
Tamas Banf70ef8c2017-12-19 15:35:09 +0000200 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 2;
201}
202
203static uint32_t
204boot_image_ok_off(const struct flash_area *fap)
205{
Tamas Banf70ef8c2017-12-19 15:35:09 +0000206 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN;
207}
208
209static uint32_t
210boot_swap_size_off(const struct flash_area *fap)
211{
David Vincze401c7422019-06-21 20:44:05 +0200212 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 4;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000213}
214
215int
216boot_read_swap_state(const struct flash_area *fap,
217 struct boot_swap_state *state)
218{
David Vincze39e78552018-10-10 17:10:01 +0200219 uint32_t magic[BOOT_MAGIC_ARR_SZ];
Tamas Banf70ef8c2017-12-19 15:35:09 +0000220 uint32_t off;
David Vincze91b71ef2019-06-24 13:06:47 +0200221 uint8_t swap_info;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000222 int rc;
223
224 off = boot_magic_off(fap);
David Vincze39e78552018-10-10 17:10:01 +0200225 rc = flash_area_read_is_empty(fap, off, magic, BOOT_MAGIC_SZ);
226 if (rc < 0) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000227 return BOOT_EFLASH;
228 }
David Vincze39e78552018-10-10 17:10:01 +0200229 if (rc == 1) {
230 state->magic = BOOT_MAGIC_UNSET;
231 } else {
232 state->magic = boot_magic_decode(magic);
233 }
Tamas Banf70ef8c2017-12-19 15:35:09 +0000234
David Vincze91b71ef2019-06-24 13:06:47 +0200235 off = boot_swap_info_off(fap);
236 rc = flash_area_read_is_empty(fap, off, &swap_info, sizeof swap_info);
David Vincze401c7422019-06-21 20:44:05 +0200237 if (rc < 0) {
238 return BOOT_EFLASH;
239 }
David Vincze91b71ef2019-06-24 13:06:47 +0200240
241 /* Extract the swap type and image number */
242 state->swap_type = BOOT_GET_SWAP_TYPE(swap_info);
243 state->image_num = BOOT_GET_IMAGE_NUM(swap_info);
244
David Vincze401c7422019-06-21 20:44:05 +0200245 if (rc == 1 || state->swap_type > BOOT_SWAP_TYPE_REVERT) {
246 state->swap_type = BOOT_SWAP_TYPE_NONE;
David Vincze91b71ef2019-06-24 13:06:47 +0200247 state->image_num = 0;
David Vincze401c7422019-06-21 20:44:05 +0200248 }
249
250 off = boot_copy_done_off(fap);
251 rc = flash_area_read_is_empty(fap, off, &state->copy_done,
252 sizeof state->copy_done);
253 if (rc < 0) {
254 return BOOT_EFLASH;
255 }
256 if (rc == 1) {
257 state->copy_done = BOOT_FLAG_UNSET;
258 } else {
259 state->copy_done = boot_flag_decode(state->copy_done);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000260 }
261
262 off = boot_image_ok_off(fap);
David Vincze401c7422019-06-21 20:44:05 +0200263 rc = flash_area_read_is_empty(fap, off, &state->image_ok,
264 sizeof state->image_ok);
David Vincze39e78552018-10-10 17:10:01 +0200265 if (rc < 0) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000266 return BOOT_EFLASH;
267 }
David Vincze39e78552018-10-10 17:10:01 +0200268 if (rc == 1) {
269 state->image_ok = BOOT_FLAG_UNSET;
270 } else {
271 state->image_ok = boot_flag_decode(state->image_ok);
272 }
Tamas Banf70ef8c2017-12-19 15:35:09 +0000273
274 return 0;
275}
276
277/**
278 * Reads the image trailer from the scratch area.
279 */
280int
281boot_read_swap_state_by_id(int flash_area_id, struct boot_swap_state *state)
282{
283 const struct flash_area *fap;
284 int rc;
285
David Vinczebb207982019-08-21 15:13:04 +0200286 if (flash_area_id == FLASH_AREA_IMAGE_SCRATCH ||
287 flash_area_id == FLASH_AREA_IMAGE_PRIMARY ||
288 flash_area_id == FLASH_AREA_IMAGE_SECONDARY) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000289 rc = flash_area_open(flash_area_id, &fap);
290 if (rc != 0) {
291 return BOOT_EFLASH;
292 }
David Vinczebb207982019-08-21 15:13:04 +0200293 } else {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000294 return BOOT_EBADARGS;
295 }
296
297 rc = boot_read_swap_state(fap, state);
298 flash_area_close(fap);
299 return rc;
300}
301
302int
303boot_read_swap_size(uint32_t *swap_size)
304{
David Vincze39e78552018-10-10 17:10:01 +0200305 uint32_t magic[BOOT_MAGIC_ARR_SZ];
Tamas Banf70ef8c2017-12-19 15:35:09 +0000306 uint32_t off;
307 const struct flash_area *fap;
308 int rc;
309
310 /*
311 * In the middle a swap, tries to locate the saved swap size. Looks
David Vincze8bdfc2d2019-03-18 15:49:23 +0100312 * for a valid magic, first on the primary slot, then on scratch.
313 * Both "slots" can end up being temporary storage for a swap and it
314 * is assumed that if magic is valid then swap size is too, because
315 * magic is always written in the last step.
Tamas Banf70ef8c2017-12-19 15:35:09 +0000316 */
317
David Vincze8bdfc2d2019-03-18 15:49:23 +0100318 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000319 if (rc != 0) {
320 return BOOT_EFLASH;
321 }
322
323 off = boot_magic_off(fap);
324 rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ);
325 if (rc != 0) {
326 rc = BOOT_EFLASH;
327 goto out;
328 }
329
330 if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) != 0) {
331 /*
David Vincze8bdfc2d2019-03-18 15:49:23 +0100332 * If the primary slot's magic is not valid, try scratch...
Tamas Banf70ef8c2017-12-19 15:35:09 +0000333 */
334
335 flash_area_close(fap);
336
337 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap);
338 if (rc != 0) {
339 return BOOT_EFLASH;
340 }
341
342 off = boot_magic_off(fap);
343 rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ);
344 if (rc != 0) {
345 rc = BOOT_EFLASH;
346 goto out;
347 }
348
349 assert(memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0);
350 }
351
352 off = boot_swap_size_off(fap);
Tamas Ban581034a2017-12-19 19:54:37 +0000353 rc = flash_area_read(fap, off, swap_size, sizeof(*swap_size));
Tamas Banf70ef8c2017-12-19 15:35:09 +0000354 if (rc != 0) {
355 rc = BOOT_EFLASH;
356 }
357
358out:
359 flash_area_close(fap);
360 return rc;
361}
362
363
364int
365boot_write_magic(const struct flash_area *fap)
366{
367 uint32_t off;
368 int rc;
369
370 off = boot_magic_off(fap);
371
David Vincze401c7422019-06-21 20:44:05 +0200372 BOOT_LOG_DBG("writing magic; fa_id=%d off=0x%x (0x%x)",
373 fap->fa_id, off, fap->fa_off + off);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000374 rc = flash_area_write(fap, off, boot_img_magic, BOOT_MAGIC_SZ);
375 if (rc != 0) {
376 return BOOT_EFLASH;
377 }
378
379 return 0;
380}
381
382static int
David Vincze401c7422019-06-21 20:44:05 +0200383boot_write_trailer_byte(const struct flash_area *fap, uint32_t off,
384 uint8_t val)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000385{
Tamas Banf70ef8c2017-12-19 15:35:09 +0000386 uint8_t buf[BOOT_MAX_ALIGN];
387 uint8_t align;
David Vincze39e78552018-10-10 17:10:01 +0200388 uint8_t erased_val;
David Vincze401c7422019-06-21 20:44:05 +0200389 int rc;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000390
Tamas Banc3828852018-02-01 12:24:16 +0000391 align = flash_area_align(fap);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000392 assert(align <= BOOT_MAX_ALIGN);
David Vincze39e78552018-10-10 17:10:01 +0200393 erased_val = flash_area_erased_val(fap);
394 memset(buf, erased_val, BOOT_MAX_ALIGN);
David Vincze401c7422019-06-21 20:44:05 +0200395 buf[0] = val;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000396
397 rc = flash_area_write(fap, off, buf, align);
398 if (rc != 0) {
399 return BOOT_EFLASH;
400 }
401
402 return 0;
403}
404
405int
406boot_write_copy_done(const struct flash_area *fap)
407{
David Vincze401c7422019-06-21 20:44:05 +0200408 uint32_t off;
409
410 off = boot_copy_done_off(fap);
411 BOOT_LOG_DBG("writing copy_done; fa_id=%d off=0x%x (0x%x)",
412 fap->fa_id, off, fap->fa_off + off);
413 return boot_write_trailer_byte(fap, off, BOOT_FLAG_SET);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000414}
415
416int
417boot_write_image_ok(const struct flash_area *fap)
418{
David Vincze401c7422019-06-21 20:44:05 +0200419 uint32_t off;
420
421 off = boot_image_ok_off(fap);
422 BOOT_LOG_DBG("writing image_ok; fa_id=%d off=0x%x (0x%x)",
423 fap->fa_id, off, fap->fa_off + off);
424 return boot_write_trailer_byte(fap, off, BOOT_FLAG_SET);
425}
426
427/**
428 * Writes the specified value to the `swap-type` field of an image trailer.
429 * This value is persisted so that the boot loader knows what swap operation to
430 * resume in case of an unexpected reset.
431 */
432int
David Vincze91b71ef2019-06-24 13:06:47 +0200433boot_write_swap_info(const struct flash_area *fap, uint8_t swap_type,
434 uint8_t image_num)
David Vincze401c7422019-06-21 20:44:05 +0200435{
436 uint32_t off;
David Vincze91b71ef2019-06-24 13:06:47 +0200437 uint8_t swap_info;
David Vincze401c7422019-06-21 20:44:05 +0200438
David Vincze91b71ef2019-06-24 13:06:47 +0200439 BOOT_SET_SWAP_INFO(swap_info, image_num, swap_type);
440 off = boot_swap_info_off(fap);
441 BOOT_LOG_DBG("writing swap_info; fa_id=%d off=0x%x (0x%x), swap_type=0x%x"
442 " image_num=0x%x",
443 fap->fa_id, off, fap->fa_off + off,
444 BOOT_GET_SWAP_TYPE(swap_info),
445 BOOT_GET_IMAGE_NUM(swap_info));
446 return boot_write_trailer_byte(fap, off, swap_info);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000447}
448
449int
450boot_write_swap_size(const struct flash_area *fap, uint32_t swap_size)
451{
452 uint32_t off;
453 int rc;
454 uint8_t buf[BOOT_MAX_ALIGN];
455 uint8_t align;
David Vincze39e78552018-10-10 17:10:01 +0200456 uint8_t erased_val;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000457
458 off = boot_swap_size_off(fap);
Tamas Banc3828852018-02-01 12:24:16 +0000459 align = flash_area_align(fap);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000460 assert(align <= BOOT_MAX_ALIGN);
Tamas Ban581034a2017-12-19 19:54:37 +0000461 if (align < sizeof(swap_size)) {
462 align = sizeof(swap_size);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000463 }
David Vincze39e78552018-10-10 17:10:01 +0200464 erased_val = flash_area_erased_val(fap);
465 memset(buf, erased_val, BOOT_MAX_ALIGN);
Tamas Ban581034a2017-12-19 19:54:37 +0000466 memcpy(buf, (uint8_t *)&swap_size, sizeof(swap_size));
Tamas Banf70ef8c2017-12-19 15:35:09 +0000467
David Vincze401c7422019-06-21 20:44:05 +0200468 BOOT_LOG_DBG("writing swap_size; fa_id=%d off=0x%x (0x%x)",
469 fap->fa_id, off, fap->fa_off + off);
470
Tamas Banf70ef8c2017-12-19 15:35:09 +0000471 rc = flash_area_write(fap, off, buf, align);
472 if (rc != 0) {
473 return BOOT_EFLASH;
474 }
475
476 return 0;
477}
478
479int
480boot_swap_type(void)
481{
482 const struct boot_swap_table *table;
David Vincze8bdfc2d2019-03-18 15:49:23 +0100483 struct boot_swap_state primary_slot;
484 struct boot_swap_state secondary_slot;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000485 int rc;
David Vincze39e78552018-10-10 17:10:01 +0200486 size_t i;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000487
David Vincze8bdfc2d2019-03-18 15:49:23 +0100488 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY, &primary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000489 if (rc) {
490 return BOOT_SWAP_TYPE_PANIC;
491 }
492
David Vincze8bdfc2d2019-03-18 15:49:23 +0100493 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY,
494 &secondary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000495 if (rc) {
496 return BOOT_SWAP_TYPE_PANIC;
497 }
498
499 for (i = 0; i < BOOT_SWAP_TABLES_COUNT; i++) {
500 table = boot_swap_tables + i;
501
David Vincze401c7422019-06-21 20:44:05 +0200502 if (boot_magic_compatible_check(table->magic_primary_slot,
503 primary_slot.magic) &&
504 boot_magic_compatible_check(table->magic_secondary_slot,
505 secondary_slot.magic) &&
David Vincze8bdfc2d2019-03-18 15:49:23 +0100506 (table->image_ok_primary_slot == BOOT_FLAG_ANY ||
507 table->image_ok_primary_slot == primary_slot.image_ok) &&
508 (table->image_ok_secondary_slot == BOOT_FLAG_ANY ||
509 table->image_ok_secondary_slot == secondary_slot.image_ok) &&
510 (table->copy_done_primary_slot == BOOT_FLAG_ANY ||
511 table->copy_done_primary_slot == primary_slot.copy_done)) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000512 BOOT_LOG_INF("Swap type: %s",
513 table->swap_type == BOOT_SWAP_TYPE_TEST ? "test" :
514 table->swap_type == BOOT_SWAP_TYPE_PERM ? "perm" :
515 table->swap_type == BOOT_SWAP_TYPE_REVERT ? "revert" :
516 "BUG; can't happen");
517 assert(table->swap_type == BOOT_SWAP_TYPE_TEST ||
518 table->swap_type == BOOT_SWAP_TYPE_PERM ||
519 table->swap_type == BOOT_SWAP_TYPE_REVERT);
520 return table->swap_type;
521 }
522 }
523
524 BOOT_LOG_INF("Swap type: none");
525 return BOOT_SWAP_TYPE_NONE;
526}
527
528/**
David Vincze8bdfc2d2019-03-18 15:49:23 +0100529 * Marks the image in the secondary slot as pending. On the next reboot,
530 * the system will perform a one-time boot of the the secondary slot image.
Tamas Banf70ef8c2017-12-19 15:35:09 +0000531 *
532 * @param permanent Whether the image should be used permanently or
533 * only tested once:
534 * 0=run image once, then confirm or revert.
535 * 1=run image forever.
536 *
537 * @return 0 on success; nonzero on failure.
538 */
539int
540boot_set_pending(int permanent)
541{
Tamas Ban581034a2017-12-19 19:54:37 +0000542 const struct flash_area *fap = NULL;
David Vincze8bdfc2d2019-03-18 15:49:23 +0100543 struct boot_swap_state state_secondary_slot;
David Vincze401c7422019-06-21 20:44:05 +0200544 uint8_t swap_type;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000545 int rc;
546
David Vincze8bdfc2d2019-03-18 15:49:23 +0100547 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY,
548 &state_secondary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000549 if (rc != 0) {
550 return rc;
551 }
552
David Vincze8bdfc2d2019-03-18 15:49:23 +0100553 switch (state_secondary_slot.magic) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000554 case BOOT_MAGIC_GOOD:
555 /* Swap already scheduled. */
556 return 0;
557
558 case BOOT_MAGIC_UNSET:
David Vincze8bdfc2d2019-03-18 15:49:23 +0100559 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY, &fap);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000560 if (rc != 0) {
561 rc = BOOT_EFLASH;
562 } else {
563 rc = boot_write_magic(fap);
564 }
565
566 if (rc == 0 && permanent) {
567 rc = boot_write_image_ok(fap);
568 }
569
David Vincze401c7422019-06-21 20:44:05 +0200570 if (rc == 0) {
571 if (permanent) {
572 swap_type = BOOT_SWAP_TYPE_PERM;
573 } else {
574 swap_type = BOOT_SWAP_TYPE_TEST;
575 }
David Vincze91b71ef2019-06-24 13:06:47 +0200576 rc = boot_write_swap_info(fap, swap_type, 0);
David Vincze401c7422019-06-21 20:44:05 +0200577 }
578
Tamas Banf70ef8c2017-12-19 15:35:09 +0000579 flash_area_close(fap);
580 return rc;
581
David Vincze401c7422019-06-21 20:44:05 +0200582 case BOOT_MAGIC_BAD:
583 /* The image slot is corrupt. There is no way to recover, so erase the
584 * slot to allow future upgrades.
585 */
586 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY, &fap);
587 if (rc != 0) {
588 return BOOT_EFLASH;
589 }
590
591 flash_area_erase(fap, 0, fap->fa_size);
592 flash_area_close(fap);
593 return BOOT_EBADIMAGE;
594
Tamas Banf70ef8c2017-12-19 15:35:09 +0000595 default:
Tamas Banf70ef8c2017-12-19 15:35:09 +0000596 assert(0);
David Vincze401c7422019-06-21 20:44:05 +0200597 return BOOT_EBADIMAGE;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000598 }
599}
600
601/**
David Vincze8bdfc2d2019-03-18 15:49:23 +0100602 * Marks the image in the primary slot as confirmed. The system will continue
603 * booting into the image in the primary slot until told to boot from a
604 * different slot.
Tamas Banf70ef8c2017-12-19 15:35:09 +0000605 *
Tamas Ban581034a2017-12-19 19:54:37 +0000606 * @return 0 on success; non-zero on failure.
Tamas Banf70ef8c2017-12-19 15:35:09 +0000607 */
608int
609boot_set_confirmed(void)
610{
Tamas Ban581034a2017-12-19 19:54:37 +0000611 const struct flash_area *fap = NULL;
David Vincze8bdfc2d2019-03-18 15:49:23 +0100612 struct boot_swap_state state_primary_slot;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000613 int rc;
614
David Vincze8bdfc2d2019-03-18 15:49:23 +0100615 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY,
616 &state_primary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000617 if (rc != 0) {
618 return rc;
619 }
620
David Vincze8bdfc2d2019-03-18 15:49:23 +0100621 switch (state_primary_slot.magic) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000622 case BOOT_MAGIC_GOOD:
623 /* Confirm needed; proceed. */
624 break;
625
626 case BOOT_MAGIC_UNSET:
627 /* Already confirmed. */
628 return 0;
629
630 case BOOT_MAGIC_BAD:
631 /* Unexpected state. */
632 return BOOT_EBADVECT;
633 }
634
David Vincze8bdfc2d2019-03-18 15:49:23 +0100635 if (state_primary_slot.copy_done == BOOT_FLAG_UNSET) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000636 /* Swap never completed. This is unexpected. */
David Vincze39e78552018-10-10 17:10:01 +0200637 rc = BOOT_EBADVECT;
638 goto done;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000639 }
640
David Vincze8bdfc2d2019-03-18 15:49:23 +0100641 if (state_primary_slot.image_ok != BOOT_FLAG_UNSET) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000642 /* Already confirmed. */
David Vincze39e78552018-10-10 17:10:01 +0200643 goto done;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000644 }
645
David Vincze8bdfc2d2019-03-18 15:49:23 +0100646 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000647 if (rc) {
648 rc = BOOT_EFLASH;
649 goto done;
650 }
651
652 rc = boot_write_image_ok(fap);
653 if (rc != 0) {
654 goto done;
655 }
656
657 rc = 0;
658
659done:
660 flash_area_close(fap);
661 return rc;
662}
David Vinczebb6e3b62019-07-31 14:24:05 +0200663
664#if (BOOT_IMAGE_NUMBER > 1)
665/**
666 * Check if the version of the image is not older than required.
667 *
668 * @param req Required minimal image version.
669 * @param ver Version of the image to be checked.
670 *
671 * @return 0 if the version is sufficient, nonzero otherwise.
672 */
673int
674boot_is_version_sufficient(struct image_version *req,
675 struct image_version *ver)
676{
677 if (ver->iv_major > req->iv_major) {
678 return 0;
679 }
680 if (ver->iv_major < req->iv_major) {
681 return BOOT_EBADVERSION;
682 }
683 /* The major version numbers are equal. */
684 if (ver->iv_minor > req->iv_minor) {
685 return 0;
686 }
687 if (ver->iv_minor < req->iv_minor) {
688 return BOOT_EBADVERSION;
689 }
690 /* The minor version numbers are equal. */
691 if (ver->iv_revision < req->iv_revision) {
692 return BOOT_EBADVERSION;
693 }
694
695 return 0;
696}
697#endif /* BOOT_IMAGE_NUMBER > 1 */