blob: 1a72699f4bbe520a4e6002b7d772e469d5043e8c [file] [log] [blame]
Christopher Collins92ea77f2016-12-12 15:59:26 -08001/*
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
20#include <assert.h>
21#include <string.h>
22#include <inttypes.h>
Fabio Utziga0bc9b52017-06-28 09:19:55 -030023#include <stddef.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080024
Christopher Collins92ea77f2016-12-12 15:59:26 -080025#include "sysflash/sysflash.h"
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020026#include "flash_map_backend/flash_map_backend.h"
27
Christopher Collins92ea77f2016-12-12 15:59:26 -080028#include "bootutil/image.h"
29#include "bootutil/bootutil.h"
30#include "bootutil_priv.h"
Fabio Utzig7ebb7c22017-04-26 10:59:31 -030031#include "bootutil/bootutil_log.h"
Fabio Utzigba829042018-09-18 08:29:34 -030032#ifdef MCUBOOT_ENC_IMAGES
33#include "bootutil/enc_key.h"
34#endif
Fabio Utzig7ebb7c22017-04-26 10:59:31 -030035
Emanuele Di Santo9f1933d2018-11-20 10:59:59 +010036MCUBOOT_LOG_MODULE_DECLARE(mcuboot);
37
Christopher Collins92ea77f2016-12-12 15:59:26 -080038int boot_current_slot;
39
Fabio Utzig24a273d2017-04-20 08:21:31 -030040const uint32_t boot_img_magic[] = {
Christopher Collins92ea77f2016-12-12 15:59:26 -080041 0xf395c277,
42 0x7fefd260,
43 0x0f505235,
44 0x8079b62c,
45};
46
Hovland, Sigvart1d96f362018-09-25 13:23:42 +020047#define BOOT_MAGIC_ARR_SZ \
48 (sizeof boot_img_magic / sizeof boot_img_magic[0])
49
Fabio Utzig24a273d2017-04-20 08:21:31 -030050const uint32_t BOOT_MAGIC_SZ = sizeof boot_img_magic;
Fabio Utziga0bc9b52017-06-28 09:19:55 -030051const uint32_t BOOT_MAX_ALIGN = MAX_FLASH_ALIGN;
Fabio Utzig24a273d2017-04-20 08:21:31 -030052
Christopher Collins92ea77f2016-12-12 15:59:26 -080053struct boot_swap_table {
David Vincze2d736ad2019-02-18 11:50:22 +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;
Christopher Collins92ea77f2016-12-12 15:59:26 -080059
Fabio Utzigb5b2f552017-06-30 10:03:47 -030060 uint8_t swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -080061};
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.
Fabio Utzigb5b2f552017-06-30 10:03:47 -030066 *
David Vincze2d736ad2019-02-18 11:50:22 +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.
Fabio Utzigb5b2f552017-06-30 10:03:47 -030070 *
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.
Christopher Collins92ea77f2016-12-12 15:59:26 -080073 */
74static const struct boot_swap_table boot_swap_tables[] = {
75 {
David Vincze2d736ad2019-02-18 11:50:22 +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,
Christopher Collins92ea77f2016-12-12 15:59:26 -080082 },
Christopher Collins92ea77f2016-12-12 15:59:26 -080083 {
David Vincze2d736ad2019-02-18 11:50:22 +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,
Christopher Collins92ea77f2016-12-12 15:59:26 -080090 },
Christopher Collins92ea77f2016-12-12 15:59:26 -080091 {
David Vincze2d736ad2019-02-18 11:50:22 +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,
Christopher Collins92ea77f2016-12-12 15:59:26 -080098 },
99};
100
101#define BOOT_SWAP_TABLES_COUNT \
102 (sizeof boot_swap_tables / sizeof boot_swap_tables[0])
103
Fabio Utzig39000012018-07-30 12:40:20 -0300104static int
Fabio Utzig178be542018-09-19 08:12:56 -0300105boot_magic_decode(const uint32_t *magic)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800106{
Fabio Utzig24a273d2017-04-20 08:21:31 -0300107 if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800108 return BOOT_MAGIC_GOOD;
109 }
Fabio Utzig178be542018-09-19 08:12:56 -0300110 return BOOT_MAGIC_BAD;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800111}
112
Fabio Utzig39000012018-07-30 12:40:20 -0300113static int
Fabio Utzig178be542018-09-19 08:12:56 -0300114boot_flag_decode(uint8_t flag)
Fabio Utzig39000012018-07-30 12:40:20 -0300115{
Fabio Utzig39000012018-07-30 12:40:20 -0300116 if (flag != BOOT_FLAG_SET) {
117 return BOOT_FLAG_BAD;
118 }
119 return BOOT_FLAG_SET;
120}
121
Christopher Collins92ea77f2016-12-12 15:59:26 -0800122uint32_t
Christopher Collins2adef702019-05-22 14:37:31 -0700123boot_trailer_sz(uint8_t min_write_sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800124{
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300125 return /* state for all sectors */
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300126 BOOT_STATUS_MAX_ENTRIES * BOOT_STATUS_STATE_COUNT * min_write_sz +
Fabio Utzigba829042018-09-18 08:29:34 -0300127#ifdef MCUBOOT_ENC_IMAGES
128 /* encryption keys */
129 BOOT_ENC_KEY_SIZE * 2 +
130#endif
Christopher Collins2adef702019-05-22 14:37:31 -0700131 /* swap_type + copy_done + image_ok + swap_size */
132 BOOT_MAX_ALIGN * 4 +
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300133 BOOT_MAGIC_SZ;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800134}
135
136static uint32_t
137boot_magic_off(const struct flash_area *fap)
138{
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300139 assert(offsetof(struct image_trailer, magic) == 16);
140 return fap->fa_size - BOOT_MAGIC_SZ;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800141}
142
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400143int
144boot_status_entries(const struct flash_area *fap)
145{
146 switch (fap->fa_id) {
David Vincze2d736ad2019-02-18 11:50:22 +0100147 case FLASH_AREA_IMAGE_PRIMARY:
148 case FLASH_AREA_IMAGE_SECONDARY:
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400149 return BOOT_STATUS_STATE_COUNT * BOOT_STATUS_MAX_ENTRIES;
150 case FLASH_AREA_IMAGE_SCRATCH:
151 return BOOT_STATUS_STATE_COUNT;
152 default:
153 return BOOT_EBADARGS;
154 }
155}
156
Christopher Collins92ea77f2016-12-12 15:59:26 -0800157uint32_t
158boot_status_off(const struct flash_area *fap)
159{
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300160 uint32_t off_from_end;
161 uint8_t elem_sz;
162
163 elem_sz = flash_area_align(fap);
164
Christopher Collins2adef702019-05-22 14:37:31 -0700165 off_from_end = boot_trailer_sz(elem_sz);
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300166
167 assert(off_from_end <= fap->fa_size);
168 return fap->fa_size - off_from_end;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800169}
170
171static uint32_t
172boot_copy_done_off(const struct flash_area *fap)
173{
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300174 assert(offsetof(struct image_trailer, copy_done) == 0);
175 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 2;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800176}
177
178static uint32_t
179boot_image_ok_off(const struct flash_area *fap)
180{
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300181 assert(offsetof(struct image_trailer, image_ok) == 8);
182 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800183}
184
Fabio Utzig46490722017-09-04 15:34:32 -0300185static uint32_t
186boot_swap_size_off(const struct flash_area *fap)
187{
Christopher Collins2adef702019-05-22 14:37:31 -0700188 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 4;
Fabio Utzig46490722017-09-04 15:34:32 -0300189}
190
Fabio Utzigba829042018-09-18 08:29:34 -0300191#ifdef MCUBOOT_ENC_IMAGES
192static uint32_t
193boot_enc_key_off(const struct flash_area *fap, uint8_t slot)
194{
Christopher Collins2adef702019-05-22 14:37:31 -0700195 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 4 -
Fabio Utzigba829042018-09-18 08:29:34 -0300196 ((slot + 1) * BOOT_ENC_KEY_SIZE);
197}
198#endif
199
Christopher Collins92ea77f2016-12-12 15:59:26 -0800200int
201boot_read_swap_state(const struct flash_area *fap,
202 struct boot_swap_state *state)
203{
Hovland, Sigvart1d96f362018-09-25 13:23:42 +0200204 uint32_t magic[BOOT_MAGIC_ARR_SZ];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800205 uint32_t off;
206 int rc;
207
208 off = boot_magic_off(fap);
Fabio Utzig178be542018-09-19 08:12:56 -0300209 rc = flash_area_read_is_empty(fap, off, magic, BOOT_MAGIC_SZ);
210 if (rc < 0) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800211 return BOOT_EFLASH;
212 }
Fabio Utzig178be542018-09-19 08:12:56 -0300213 if (rc == 1) {
214 state->magic = BOOT_MAGIC_UNSET;
215 } else {
216 state->magic = boot_magic_decode(magic);
217 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800218
Christopher Collins2adef702019-05-22 14:37:31 -0700219 off = boot_copy_done_off(fap);
220 rc = flash_area_read_is_empty(fap, off, &state->copy_done,
221 sizeof state->copy_done);
222 if (rc < 0) {
223 return BOOT_EFLASH;
224 }
225 if (rc == 1) {
226 state->copy_done = BOOT_FLAG_UNSET;
227 } else {
228 state->copy_done = boot_flag_decode(state->copy_done);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800229 }
230
231 off = boot_image_ok_off(fap);
David Vincze2d736ad2019-02-18 11:50:22 +0100232 rc = flash_area_read_is_empty(fap, off, &state->image_ok,
233 sizeof state->image_ok);
Fabio Utzig178be542018-09-19 08:12:56 -0300234 if (rc < 0) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800235 return BOOT_EFLASH;
236 }
Fabio Utzig178be542018-09-19 08:12:56 -0300237 if (rc == 1) {
238 state->image_ok = BOOT_FLAG_UNSET;
239 } else {
240 state->image_ok = boot_flag_decode(state->image_ok);
241 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800242
243 return 0;
244}
245
246/**
247 * Reads the image trailer from the scratch area.
248 */
249int
Fabio Utzig2473ac02017-05-02 12:45:02 -0300250boot_read_swap_state_by_id(int flash_area_id, struct boot_swap_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800251{
252 const struct flash_area *fap;
253 int rc;
254
Fabio Utzig2473ac02017-05-02 12:45:02 -0300255 switch (flash_area_id) {
256 case FLASH_AREA_IMAGE_SCRATCH:
David Vincze2d736ad2019-02-18 11:50:22 +0100257 case FLASH_AREA_IMAGE_PRIMARY:
258 case FLASH_AREA_IMAGE_SECONDARY:
Fabio Utzig2473ac02017-05-02 12:45:02 -0300259 rc = flash_area_open(flash_area_id, &fap);
260 if (rc != 0) {
261 return BOOT_EFLASH;
262 }
263 break;
264 default:
Fabio Utzig856f7832017-05-22 11:04:44 -0400265 return BOOT_EBADARGS;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300266 }
267
268 rc = boot_read_swap_state(fap, state);
Fabio Utzigacfba2e2017-05-22 11:06:29 -0400269 flash_area_close(fap);
270 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800271}
272
273int
Fabio Utzig46490722017-09-04 15:34:32 -0300274boot_read_swap_size(uint32_t *swap_size)
275{
Hovland, Sigvart1d96f362018-09-25 13:23:42 +0200276 uint32_t magic[BOOT_MAGIC_ARR_SZ];
Fabio Utzig46490722017-09-04 15:34:32 -0300277 uint32_t off;
278 const struct flash_area *fap;
279 int rc;
280
281 /*
282 * In the middle a swap, tries to locate the saved swap size. Looks
David Vincze2d736ad2019-02-18 11:50:22 +0100283 * for a valid magic, first on the primary slot, then on scratch.
284 * Both "slots" can end up being temporary storage for a swap and it
285 * is assumed that if magic is valid then swap size is too, because
286 * magic is always written in the last step.
Fabio Utzig46490722017-09-04 15:34:32 -0300287 */
288
David Vincze2d736ad2019-02-18 11:50:22 +0100289 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap);
Fabio Utzig46490722017-09-04 15:34:32 -0300290 if (rc != 0) {
291 return BOOT_EFLASH;
292 }
293
294 off = boot_magic_off(fap);
295 rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ);
296 if (rc != 0) {
297 rc = BOOT_EFLASH;
298 goto out;
299 }
300
301 if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) != 0) {
302 /*
David Vincze2d736ad2019-02-18 11:50:22 +0100303 * If the primary slot's magic is not valid, try scratch...
Fabio Utzig46490722017-09-04 15:34:32 -0300304 */
305
306 flash_area_close(fap);
307
308 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap);
309 if (rc != 0) {
310 return BOOT_EFLASH;
311 }
312
313 off = boot_magic_off(fap);
314 rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ);
315 if (rc != 0) {
316 rc = BOOT_EFLASH;
317 goto out;
318 }
319
320 assert(memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0);
321 }
322
323 off = boot_swap_size_off(fap);
324 rc = flash_area_read(fap, off, swap_size, sizeof *swap_size);
325 if (rc != 0) {
326 rc = BOOT_EFLASH;
327 }
328
329out:
330 flash_area_close(fap);
331 return rc;
332}
333
Fabio Utzigba829042018-09-18 08:29:34 -0300334#ifdef MCUBOOT_ENC_IMAGES
335int
336boot_read_enc_key(uint8_t slot, uint8_t *enckey)
337{
338 uint32_t magic[BOOT_MAGIC_SZ];
339 uint32_t off;
340 const struct flash_area *fap;
341 int rc;
342
David Vincze2d736ad2019-02-18 11:50:22 +0100343 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap);
Fabio Utzigba829042018-09-18 08:29:34 -0300344 if (rc != 0) {
345 return BOOT_EFLASH;
346 }
347
348 off = boot_magic_off(fap);
349 rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ);
350 if (rc != 0) {
351 rc = BOOT_EFLASH;
352 goto out;
353 }
354
355 if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) != 0) {
356 /*
David Vincze2d736ad2019-02-18 11:50:22 +0100357 * If the primary slot's magic is not valid, try scratch...
Fabio Utzigba829042018-09-18 08:29:34 -0300358 */
359
360 flash_area_close(fap);
361
362 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap);
363 if (rc != 0) {
364 return BOOT_EFLASH;
365 }
366
367 off = boot_magic_off(fap);
368 rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ);
369 if (rc != 0) {
370 rc = BOOT_EFLASH;
371 goto out;
372 }
373
374 assert(memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0);
375 }
376
377 off = boot_enc_key_off(fap, slot);
378 rc = flash_area_read(fap, off, enckey, BOOT_ENC_KEY_SIZE);
379 if (rc != 0) {
380 rc = BOOT_EFLASH;
381 }
382
383out:
384 flash_area_close(fap);
385 return rc;
386}
387#endif
Fabio Utzig46490722017-09-04 15:34:32 -0300388
389int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800390boot_write_magic(const struct flash_area *fap)
391{
392 uint32_t off;
393 int rc;
394
395 off = boot_magic_off(fap);
396
Fabio Utzig24a273d2017-04-20 08:21:31 -0300397 rc = flash_area_write(fap, off, boot_img_magic, BOOT_MAGIC_SZ);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800398 if (rc != 0) {
399 return BOOT_EFLASH;
400 }
401
402 return 0;
403}
404
Fabio Utzig2473ac02017-05-02 12:45:02 -0300405static int
406boot_write_flag(int flag, const struct flash_area *fap)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800407{
408 uint32_t off;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800409 int rc;
Fabio Utzig644b8d42017-04-20 07:56:05 -0300410 uint8_t buf[BOOT_MAX_ALIGN];
David Brown9d725462017-01-23 15:50:58 -0700411 uint8_t align;
Fabio Utzig39000012018-07-30 12:40:20 -0300412 uint8_t erased_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800413
Fabio Utzig2473ac02017-05-02 12:45:02 -0300414 switch (flag) {
415 case BOOT_FLAG_COPY_DONE:
416 off = boot_copy_done_off(fap);
417 break;
418 case BOOT_FLAG_IMAGE_OK:
419 off = boot_image_ok_off(fap);
420 break;
421 default:
Fabio Utzig856f7832017-05-22 11:04:44 -0400422 return BOOT_EBADARGS;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300423 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800424
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200425 align = flash_area_align(fap);
Fabio Utzig644b8d42017-04-20 07:56:05 -0300426 assert(align <= BOOT_MAX_ALIGN);
Fabio Utzig39000012018-07-30 12:40:20 -0300427 erased_val = flash_area_erased_val(fap);
428 memset(buf, erased_val, BOOT_MAX_ALIGN);
Fabio Utzigde8a38a2017-05-23 11:15:01 -0400429 buf[0] = BOOT_FLAG_SET;
David Brown9d725462017-01-23 15:50:58 -0700430
431 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800432 if (rc != 0) {
433 return BOOT_EFLASH;
434 }
435
436 return 0;
437}
438
439int
Fabio Utzig2473ac02017-05-02 12:45:02 -0300440boot_write_copy_done(const struct flash_area *fap)
441{
442 return boot_write_flag(BOOT_FLAG_COPY_DONE, fap);
443}
444
445int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800446boot_write_image_ok(const struct flash_area *fap)
447{
Fabio Utzig2473ac02017-05-02 12:45:02 -0300448 return boot_write_flag(BOOT_FLAG_IMAGE_OK, fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800449}
450
451int
Fabio Utzig46490722017-09-04 15:34:32 -0300452boot_write_swap_size(const struct flash_area *fap, uint32_t swap_size)
453{
454 uint32_t off;
455 int rc;
456 uint8_t buf[BOOT_MAX_ALIGN];
457 uint8_t align;
Fabio Utzig39000012018-07-30 12:40:20 -0300458 uint8_t erased_val;
Fabio Utzig46490722017-09-04 15:34:32 -0300459
460 off = boot_swap_size_off(fap);
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200461 align = flash_area_align(fap);
Fabio Utzig46490722017-09-04 15:34:32 -0300462 assert(align <= BOOT_MAX_ALIGN);
463 if (align < sizeof swap_size) {
464 align = sizeof swap_size;
465 }
Fabio Utzig39000012018-07-30 12:40:20 -0300466 erased_val = flash_area_erased_val(fap);
467 memset(buf, erased_val, BOOT_MAX_ALIGN);
Fabio Utzig46490722017-09-04 15:34:32 -0300468 memcpy(buf, (uint8_t *)&swap_size, sizeof swap_size);
469
470 rc = flash_area_write(fap, off, buf, align);
471 if (rc != 0) {
472 return BOOT_EFLASH;
473 }
474
475 return 0;
476}
477
Fabio Utzigba829042018-09-18 08:29:34 -0300478#ifdef MCUBOOT_ENC_IMAGES
479int
480boot_write_enc_key(const struct flash_area *fap, uint8_t slot, const uint8_t *enckey)
481{
482 uint32_t off;
483 int rc;
484
485 off = boot_enc_key_off(fap, slot);
486 rc = flash_area_write(fap, off, enckey, BOOT_ENC_KEY_SIZE);
487 if (rc != 0) {
488 return BOOT_EFLASH;
489 }
490
491 return 0;
492}
493#endif
494
Fabio Utzig46490722017-09-04 15:34:32 -0300495int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800496boot_swap_type(void)
497{
498 const struct boot_swap_table *table;
David Vincze2d736ad2019-02-18 11:50:22 +0100499 struct boot_swap_state primary_slot;
500 struct boot_swap_state secondary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800501 int rc;
Fabio Utzigcd5774b2017-11-29 10:18:26 -0200502 size_t i;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800503
David Vincze2d736ad2019-02-18 11:50:22 +0100504 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY, &primary_slot);
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300505 if (rc) {
506 return BOOT_SWAP_TYPE_PANIC;
507 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800508
David Vincze2d736ad2019-02-18 11:50:22 +0100509 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY,
510 &secondary_slot);
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300511 if (rc) {
512 return BOOT_SWAP_TYPE_PANIC;
513 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800514
515 for (i = 0; i < BOOT_SWAP_TABLES_COUNT; i++) {
516 table = boot_swap_tables + i;
517
David Vincze2d736ad2019-02-18 11:50:22 +0100518 if ((table->magic_primary_slot == BOOT_MAGIC_ANY ||
519 table->magic_primary_slot == primary_slot.magic) &&
520 (table->magic_secondary_slot == BOOT_MAGIC_ANY ||
521 table->magic_secondary_slot == secondary_slot.magic) &&
522 (table->image_ok_primary_slot == BOOT_FLAG_ANY ||
523 table->image_ok_primary_slot == primary_slot.image_ok) &&
524 (table->image_ok_secondary_slot == BOOT_FLAG_ANY ||
525 table->image_ok_secondary_slot == secondary_slot.image_ok) &&
526 (table->copy_done_primary_slot == BOOT_FLAG_ANY ||
527 table->copy_done_primary_slot == primary_slot.copy_done)) {
Fabio Utzig34e393e2017-05-22 11:07:07 -0400528 BOOT_LOG_INF("Swap type: %s",
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300529 table->swap_type == BOOT_SWAP_TYPE_TEST ? "test" :
530 table->swap_type == BOOT_SWAP_TYPE_PERM ? "perm" :
531 table->swap_type == BOOT_SWAP_TYPE_REVERT ? "revert" :
532 "BUG; can't happen");
533 assert(table->swap_type == BOOT_SWAP_TYPE_TEST ||
534 table->swap_type == BOOT_SWAP_TYPE_PERM ||
535 table->swap_type == BOOT_SWAP_TYPE_REVERT);
536 return table->swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800537 }
538 }
539
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300540 BOOT_LOG_INF("Swap type: none");
Christopher Collins92ea77f2016-12-12 15:59:26 -0800541 return BOOT_SWAP_TYPE_NONE;
542}
543
544/**
David Vincze2d736ad2019-02-18 11:50:22 +0100545 * Marks the image in the secondary slot as pending. On the next reboot,
546 * the system will perform a one-time boot of the the secondary slot image.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800547 *
Christopher Collins7835c1e2016-12-21 10:10:51 -0800548 * @param permanent Whether the image should be used permanently or
549 * only tested once:
550 * 0=run image once, then confirm or revert.
551 * 1=run image forever.
552 *
Christopher Collins92ea77f2016-12-12 15:59:26 -0800553 * @return 0 on success; nonzero on failure.
554 */
555int
Christopher Collins7835c1e2016-12-21 10:10:51 -0800556boot_set_pending(int permanent)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800557{
558 const struct flash_area *fap;
David Vincze2d736ad2019-02-18 11:50:22 +0100559 struct boot_swap_state state_secondary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800560 int rc;
561
David Vincze2d736ad2019-02-18 11:50:22 +0100562 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY,
563 &state_secondary_slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800564 if (rc != 0) {
565 return rc;
566 }
567
David Vincze2d736ad2019-02-18 11:50:22 +0100568 switch (state_secondary_slot.magic) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800569 case BOOT_MAGIC_GOOD:
570 /* Swap already scheduled. */
571 return 0;
572
573 case BOOT_MAGIC_UNSET:
David Vincze2d736ad2019-02-18 11:50:22 +0100574 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800575 if (rc != 0) {
576 rc = BOOT_EFLASH;
577 } else {
578 rc = boot_write_magic(fap);
579 }
580
Christopher Collins7835c1e2016-12-21 10:10:51 -0800581 if (rc == 0 && permanent) {
582 rc = boot_write_image_ok(fap);
583 }
584
Christopher Collins92ea77f2016-12-12 15:59:26 -0800585 flash_area_close(fap);
586 return rc;
587
Christopher Collinsae01f152019-01-30 09:56:37 -0800588 case BOOT_MAGIC_BAD:
589 /* The image slot is corrupt. There is no way to recover, so erase the
590 * slot to allow future upgrades.
591 */
David Vincze2d736ad2019-02-18 11:50:22 +0100592 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY, &fap);
Christopher Collinsae01f152019-01-30 09:56:37 -0800593 if (rc != 0) {
594 return BOOT_EFLASH;
595 }
596
597 flash_area_erase(fap, 0, fap->fa_size);
598 flash_area_close(fap);
599 return BOOT_EBADIMAGE;
600
Christopher Collins92ea77f2016-12-12 15:59:26 -0800601 default:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800602 assert(0);
Christopher Collinsae01f152019-01-30 09:56:37 -0800603 return BOOT_EBADIMAGE;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800604 }
605}
606
607/**
David Vincze2d736ad2019-02-18 11:50:22 +0100608 * Marks the image in the primary slot as confirmed. The system will continue
609 * booting into the image in the primary slot until told to boot from a
610 * different slot.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800611 *
612 * @return 0 on success; nonzero on failure.
613 */
614int
615boot_set_confirmed(void)
616{
617 const struct flash_area *fap;
David Vincze2d736ad2019-02-18 11:50:22 +0100618 struct boot_swap_state state_primary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800619 int rc;
620
David Vincze2d736ad2019-02-18 11:50:22 +0100621 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY,
622 &state_primary_slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800623 if (rc != 0) {
624 return rc;
625 }
626
David Vincze2d736ad2019-02-18 11:50:22 +0100627 switch (state_primary_slot.magic) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800628 case BOOT_MAGIC_GOOD:
629 /* Confirm needed; proceed. */
630 break;
631
632 case BOOT_MAGIC_UNSET:
633 /* Already confirmed. */
634 return 0;
635
636 case BOOT_MAGIC_BAD:
637 /* Unexpected state. */
638 return BOOT_EBADVECT;
639 }
640
David Vincze2d736ad2019-02-18 11:50:22 +0100641 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800642 if (rc) {
643 rc = BOOT_EFLASH;
644 goto done;
645 }
646
David Vincze2d736ad2019-02-18 11:50:22 +0100647 if (state_primary_slot.copy_done == BOOT_FLAG_UNSET) {
Fabio Utzig39000012018-07-30 12:40:20 -0300648 /* Swap never completed. This is unexpected. */
649 rc = BOOT_EBADVECT;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800650 goto done;
651 }
652
David Vincze2d736ad2019-02-18 11:50:22 +0100653 if (state_primary_slot.image_ok != BOOT_FLAG_UNSET) {
Fabio Utzig39000012018-07-30 12:40:20 -0300654 /* Already confirmed. */
655 goto done;
656 }
657
658 rc = boot_write_image_ok(fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800659
660done:
661 flash_area_close(fap);
662 return rc;
663}