blob: 0debf185078d8c9f613b108b7c3a71ca6accd1b3 [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
Christopher Collins2c88e692019-05-22 15:10:14 -0700397 BOOT_LOG_DBG("writing magic; fa_id=%d off=0x%x (0x%x)",
398 fap->fa_id, off, fap->fa_off + off);
Fabio Utzig24a273d2017-04-20 08:21:31 -0300399 rc = flash_area_write(fap, off, boot_img_magic, BOOT_MAGIC_SZ);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800400 if (rc != 0) {
401 return BOOT_EFLASH;
402 }
403
404 return 0;
405}
406
Fabio Utzig2473ac02017-05-02 12:45:02 -0300407static int
408boot_write_flag(int flag, const struct flash_area *fap)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800409{
410 uint32_t off;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800411 int rc;
Fabio Utzig644b8d42017-04-20 07:56:05 -0300412 uint8_t buf[BOOT_MAX_ALIGN];
David Brown9d725462017-01-23 15:50:58 -0700413 uint8_t align;
Fabio Utzig39000012018-07-30 12:40:20 -0300414 uint8_t erased_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800415
Fabio Utzig2473ac02017-05-02 12:45:02 -0300416 switch (flag) {
417 case BOOT_FLAG_COPY_DONE:
418 off = boot_copy_done_off(fap);
Christopher Collins2c88e692019-05-22 15:10:14 -0700419 BOOT_LOG_DBG("writing copy_done; fa_id=%d off=0x%x (0x%x)",
420 fap->fa_id, off, fap->fa_off + off);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300421 break;
422 case BOOT_FLAG_IMAGE_OK:
423 off = boot_image_ok_off(fap);
Christopher Collins2c88e692019-05-22 15:10:14 -0700424 BOOT_LOG_DBG("writing image_ok; fa_id=%d off=0x%x (0x%x)",
425 fap->fa_id, off, fap->fa_off + off);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300426 break;
427 default:
Fabio Utzig856f7832017-05-22 11:04:44 -0400428 return BOOT_EBADARGS;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300429 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800430
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200431 align = flash_area_align(fap);
Fabio Utzig644b8d42017-04-20 07:56:05 -0300432 assert(align <= BOOT_MAX_ALIGN);
Fabio Utzig39000012018-07-30 12:40:20 -0300433 erased_val = flash_area_erased_val(fap);
434 memset(buf, erased_val, BOOT_MAX_ALIGN);
Fabio Utzigde8a38a2017-05-23 11:15:01 -0400435 buf[0] = BOOT_FLAG_SET;
David Brown9d725462017-01-23 15:50:58 -0700436
437 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800438 if (rc != 0) {
439 return BOOT_EFLASH;
440 }
441
442 return 0;
443}
444
445int
Fabio Utzig2473ac02017-05-02 12:45:02 -0300446boot_write_copy_done(const struct flash_area *fap)
447{
448 return boot_write_flag(BOOT_FLAG_COPY_DONE, fap);
449}
450
451int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800452boot_write_image_ok(const struct flash_area *fap)
453{
Fabio Utzig2473ac02017-05-02 12:45:02 -0300454 return boot_write_flag(BOOT_FLAG_IMAGE_OK, fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800455}
456
457int
Fabio Utzig46490722017-09-04 15:34:32 -0300458boot_write_swap_size(const struct flash_area *fap, uint32_t swap_size)
459{
460 uint32_t off;
461 int rc;
462 uint8_t buf[BOOT_MAX_ALIGN];
463 uint8_t align;
Fabio Utzig39000012018-07-30 12:40:20 -0300464 uint8_t erased_val;
Fabio Utzig46490722017-09-04 15:34:32 -0300465
466 off = boot_swap_size_off(fap);
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200467 align = flash_area_align(fap);
Fabio Utzig46490722017-09-04 15:34:32 -0300468 assert(align <= BOOT_MAX_ALIGN);
469 if (align < sizeof swap_size) {
470 align = sizeof swap_size;
471 }
Fabio Utzig39000012018-07-30 12:40:20 -0300472 erased_val = flash_area_erased_val(fap);
473 memset(buf, erased_val, BOOT_MAX_ALIGN);
Fabio Utzig46490722017-09-04 15:34:32 -0300474 memcpy(buf, (uint8_t *)&swap_size, sizeof swap_size);
475
Christopher Collins2c88e692019-05-22 15:10:14 -0700476 BOOT_LOG_DBG("writing swap_size; fa_id=%d off=0x%x (0x%x)",
477 fap->fa_id, off, fap->fa_off + off);
478
Fabio Utzig46490722017-09-04 15:34:32 -0300479 rc = flash_area_write(fap, off, buf, align);
480 if (rc != 0) {
481 return BOOT_EFLASH;
482 }
483
484 return 0;
485}
486
Fabio Utzigba829042018-09-18 08:29:34 -0300487#ifdef MCUBOOT_ENC_IMAGES
488int
489boot_write_enc_key(const struct flash_area *fap, uint8_t slot, const uint8_t *enckey)
490{
491 uint32_t off;
492 int rc;
493
494 off = boot_enc_key_off(fap, slot);
495 rc = flash_area_write(fap, off, enckey, BOOT_ENC_KEY_SIZE);
496 if (rc != 0) {
497 return BOOT_EFLASH;
498 }
499
500 return 0;
501}
502#endif
503
Fabio Utzig46490722017-09-04 15:34:32 -0300504int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800505boot_swap_type(void)
506{
507 const struct boot_swap_table *table;
David Vincze2d736ad2019-02-18 11:50:22 +0100508 struct boot_swap_state primary_slot;
509 struct boot_swap_state secondary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800510 int rc;
Fabio Utzigcd5774b2017-11-29 10:18:26 -0200511 size_t i;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800512
David Vincze2d736ad2019-02-18 11:50:22 +0100513 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY, &primary_slot);
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300514 if (rc) {
515 return BOOT_SWAP_TYPE_PANIC;
516 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800517
David Vincze2d736ad2019-02-18 11:50:22 +0100518 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY,
519 &secondary_slot);
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300520 if (rc) {
521 return BOOT_SWAP_TYPE_PANIC;
522 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800523
524 for (i = 0; i < BOOT_SWAP_TABLES_COUNT; i++) {
525 table = boot_swap_tables + i;
526
David Vincze2d736ad2019-02-18 11:50:22 +0100527 if ((table->magic_primary_slot == BOOT_MAGIC_ANY ||
528 table->magic_primary_slot == primary_slot.magic) &&
529 (table->magic_secondary_slot == BOOT_MAGIC_ANY ||
530 table->magic_secondary_slot == secondary_slot.magic) &&
531 (table->image_ok_primary_slot == BOOT_FLAG_ANY ||
532 table->image_ok_primary_slot == primary_slot.image_ok) &&
533 (table->image_ok_secondary_slot == BOOT_FLAG_ANY ||
534 table->image_ok_secondary_slot == secondary_slot.image_ok) &&
535 (table->copy_done_primary_slot == BOOT_FLAG_ANY ||
536 table->copy_done_primary_slot == primary_slot.copy_done)) {
Fabio Utzig34e393e2017-05-22 11:07:07 -0400537 BOOT_LOG_INF("Swap type: %s",
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300538 table->swap_type == BOOT_SWAP_TYPE_TEST ? "test" :
539 table->swap_type == BOOT_SWAP_TYPE_PERM ? "perm" :
540 table->swap_type == BOOT_SWAP_TYPE_REVERT ? "revert" :
541 "BUG; can't happen");
542 assert(table->swap_type == BOOT_SWAP_TYPE_TEST ||
543 table->swap_type == BOOT_SWAP_TYPE_PERM ||
544 table->swap_type == BOOT_SWAP_TYPE_REVERT);
545 return table->swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800546 }
547 }
548
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300549 BOOT_LOG_INF("Swap type: none");
Christopher Collins92ea77f2016-12-12 15:59:26 -0800550 return BOOT_SWAP_TYPE_NONE;
551}
552
553/**
David Vincze2d736ad2019-02-18 11:50:22 +0100554 * Marks the image in the secondary slot as pending. On the next reboot,
555 * the system will perform a one-time boot of the the secondary slot image.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800556 *
Christopher Collins7835c1e2016-12-21 10:10:51 -0800557 * @param permanent Whether the image should be used permanently or
558 * only tested once:
559 * 0=run image once, then confirm or revert.
560 * 1=run image forever.
561 *
Christopher Collins92ea77f2016-12-12 15:59:26 -0800562 * @return 0 on success; nonzero on failure.
563 */
564int
Christopher Collins7835c1e2016-12-21 10:10:51 -0800565boot_set_pending(int permanent)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800566{
567 const struct flash_area *fap;
David Vincze2d736ad2019-02-18 11:50:22 +0100568 struct boot_swap_state state_secondary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800569 int rc;
570
David Vincze2d736ad2019-02-18 11:50:22 +0100571 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY,
572 &state_secondary_slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800573 if (rc != 0) {
574 return rc;
575 }
576
David Vincze2d736ad2019-02-18 11:50:22 +0100577 switch (state_secondary_slot.magic) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800578 case BOOT_MAGIC_GOOD:
579 /* Swap already scheduled. */
580 return 0;
581
582 case BOOT_MAGIC_UNSET:
David Vincze2d736ad2019-02-18 11:50:22 +0100583 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800584 if (rc != 0) {
585 rc = BOOT_EFLASH;
586 } else {
587 rc = boot_write_magic(fap);
588 }
589
Christopher Collins7835c1e2016-12-21 10:10:51 -0800590 if (rc == 0 && permanent) {
591 rc = boot_write_image_ok(fap);
592 }
593
Christopher Collins92ea77f2016-12-12 15:59:26 -0800594 flash_area_close(fap);
595 return rc;
596
Christopher Collinsae01f152019-01-30 09:56:37 -0800597 case BOOT_MAGIC_BAD:
598 /* The image slot is corrupt. There is no way to recover, so erase the
599 * slot to allow future upgrades.
600 */
David Vincze2d736ad2019-02-18 11:50:22 +0100601 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY, &fap);
Christopher Collinsae01f152019-01-30 09:56:37 -0800602 if (rc != 0) {
603 return BOOT_EFLASH;
604 }
605
606 flash_area_erase(fap, 0, fap->fa_size);
607 flash_area_close(fap);
608 return BOOT_EBADIMAGE;
609
Christopher Collins92ea77f2016-12-12 15:59:26 -0800610 default:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800611 assert(0);
Christopher Collinsae01f152019-01-30 09:56:37 -0800612 return BOOT_EBADIMAGE;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800613 }
614}
615
616/**
David Vincze2d736ad2019-02-18 11:50:22 +0100617 * Marks the image in the primary slot as confirmed. The system will continue
618 * booting into the image in the primary slot until told to boot from a
619 * different slot.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800620 *
621 * @return 0 on success; nonzero on failure.
622 */
623int
624boot_set_confirmed(void)
625{
626 const struct flash_area *fap;
David Vincze2d736ad2019-02-18 11:50:22 +0100627 struct boot_swap_state state_primary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800628 int rc;
629
David Vincze2d736ad2019-02-18 11:50:22 +0100630 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY,
631 &state_primary_slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800632 if (rc != 0) {
633 return rc;
634 }
635
David Vincze2d736ad2019-02-18 11:50:22 +0100636 switch (state_primary_slot.magic) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800637 case BOOT_MAGIC_GOOD:
638 /* Confirm needed; proceed. */
639 break;
640
641 case BOOT_MAGIC_UNSET:
642 /* Already confirmed. */
643 return 0;
644
645 case BOOT_MAGIC_BAD:
646 /* Unexpected state. */
647 return BOOT_EBADVECT;
648 }
649
David Vincze2d736ad2019-02-18 11:50:22 +0100650 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800651 if (rc) {
652 rc = BOOT_EFLASH;
653 goto done;
654 }
655
David Vincze2d736ad2019-02-18 11:50:22 +0100656 if (state_primary_slot.copy_done == BOOT_FLAG_UNSET) {
Fabio Utzig39000012018-07-30 12:40:20 -0300657 /* Swap never completed. This is unexpected. */
658 rc = BOOT_EBADVECT;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800659 goto done;
660 }
661
David Vincze2d736ad2019-02-18 11:50:22 +0100662 if (state_primary_slot.image_ok != BOOT_FLAG_UNSET) {
Fabio Utzig39000012018-07-30 12:40:20 -0300663 /* Already confirmed. */
664 goto done;
665 }
666
667 rc = boot_write_image_ok(fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800668
669done:
670 flash_area_close(fap);
671 return rc;
672}