blob: 041f6a2da00f90dda35bafff1183b808dd48c005 [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
David Vinczee2453472019-06-17 12:31:59 +020020/*
21 * Modifications are Copyright (c) 2019 Arm Limited.
22 */
23
Christopher Collins92ea77f2016-12-12 15:59:26 -080024#include <assert.h>
25#include <string.h>
26#include <inttypes.h>
Fabio Utziga0bc9b52017-06-28 09:19:55 -030027#include <stddef.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080028
Christopher Collins92ea77f2016-12-12 15:59:26 -080029#include "sysflash/sysflash.h"
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020030#include "flash_map_backend/flash_map_backend.h"
31
Christopher Collins92ea77f2016-12-12 15:59:26 -080032#include "bootutil/image.h"
33#include "bootutil/bootutil.h"
34#include "bootutil_priv.h"
Fabio Utzig7ebb7c22017-04-26 10:59:31 -030035#include "bootutil/bootutil_log.h"
Fabio Utzigba829042018-09-18 08:29:34 -030036#ifdef MCUBOOT_ENC_IMAGES
37#include "bootutil/enc_key.h"
38#endif
Fabio Utzig7ebb7c22017-04-26 10:59:31 -030039
Emanuele Di Santo9f1933d2018-11-20 10:59:59 +010040MCUBOOT_LOG_MODULE_DECLARE(mcuboot);
41
Christopher Collins92ea77f2016-12-12 15:59:26 -080042int boot_current_slot;
43
Fabio Utzig24a273d2017-04-20 08:21:31 -030044const uint32_t boot_img_magic[] = {
Christopher Collins92ea77f2016-12-12 15:59:26 -080045 0xf395c277,
46 0x7fefd260,
47 0x0f505235,
48 0x8079b62c,
49};
50
Hovland, Sigvart1d96f362018-09-25 13:23:42 +020051#define BOOT_MAGIC_ARR_SZ \
52 (sizeof boot_img_magic / sizeof boot_img_magic[0])
53
Fabio Utzig24a273d2017-04-20 08:21:31 -030054const uint32_t BOOT_MAGIC_SZ = sizeof boot_img_magic;
Fabio Utziga0bc9b52017-06-28 09:19:55 -030055const uint32_t BOOT_MAX_ALIGN = MAX_FLASH_ALIGN;
Fabio Utzig24a273d2017-04-20 08:21:31 -030056
Christopher Collins92ea77f2016-12-12 15:59:26 -080057struct boot_swap_table {
David Vincze2d736ad2019-02-18 11:50:22 +010058 uint8_t magic_primary_slot;
59 uint8_t magic_secondary_slot;
60 uint8_t image_ok_primary_slot;
61 uint8_t image_ok_secondary_slot;
62 uint8_t copy_done_primary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -080063
Fabio Utzigb5b2f552017-06-30 10:03:47 -030064 uint8_t swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -080065};
66
67/**
68 * This set of tables maps image trailer contents to swap operation type.
69 * When searching for a match, these tables must be iterated sequentially.
Fabio Utzigb5b2f552017-06-30 10:03:47 -030070 *
David Vincze2d736ad2019-02-18 11:50:22 +010071 * NOTE: the table order is very important. The settings in the secondary
72 * slot always are priority to the primary slot and should be located
73 * earlier in the table.
Fabio Utzigb5b2f552017-06-30 10:03:47 -030074 *
75 * The table lists only states where there is action needs to be taken by
76 * the bootloader, as in starting/finishing a swap operation.
Christopher Collins92ea77f2016-12-12 15:59:26 -080077 */
78static const struct boot_swap_table boot_swap_tables[] = {
79 {
David Vincze2d736ad2019-02-18 11:50:22 +010080 .magic_primary_slot = BOOT_MAGIC_ANY,
81 .magic_secondary_slot = BOOT_MAGIC_GOOD,
82 .image_ok_primary_slot = BOOT_FLAG_ANY,
83 .image_ok_secondary_slot = BOOT_FLAG_UNSET,
84 .copy_done_primary_slot = BOOT_FLAG_ANY,
85 .swap_type = BOOT_SWAP_TYPE_TEST,
Christopher Collins92ea77f2016-12-12 15:59:26 -080086 },
Christopher Collins92ea77f2016-12-12 15:59:26 -080087 {
David Vincze2d736ad2019-02-18 11:50:22 +010088 .magic_primary_slot = BOOT_MAGIC_ANY,
89 .magic_secondary_slot = BOOT_MAGIC_GOOD,
90 .image_ok_primary_slot = BOOT_FLAG_ANY,
91 .image_ok_secondary_slot = BOOT_FLAG_SET,
92 .copy_done_primary_slot = BOOT_FLAG_ANY,
93 .swap_type = BOOT_SWAP_TYPE_PERM,
Christopher Collins92ea77f2016-12-12 15:59:26 -080094 },
Christopher Collins92ea77f2016-12-12 15:59:26 -080095 {
David Vincze2d736ad2019-02-18 11:50:22 +010096 .magic_primary_slot = BOOT_MAGIC_GOOD,
97 .magic_secondary_slot = BOOT_MAGIC_UNSET,
98 .image_ok_primary_slot = BOOT_FLAG_UNSET,
99 .image_ok_secondary_slot = BOOT_FLAG_ANY,
100 .copy_done_primary_slot = BOOT_FLAG_SET,
101 .swap_type = BOOT_SWAP_TYPE_REVERT,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800102 },
103};
104
105#define BOOT_SWAP_TABLES_COUNT \
106 (sizeof boot_swap_tables / sizeof boot_swap_tables[0])
107
Fabio Utzig39000012018-07-30 12:40:20 -0300108static int
Fabio Utzig178be542018-09-19 08:12:56 -0300109boot_magic_decode(const uint32_t *magic)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800110{
Fabio Utzig24a273d2017-04-20 08:21:31 -0300111 if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800112 return BOOT_MAGIC_GOOD;
113 }
Fabio Utzig178be542018-09-19 08:12:56 -0300114 return BOOT_MAGIC_BAD;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800115}
116
Fabio Utzig39000012018-07-30 12:40:20 -0300117static int
Fabio Utzig178be542018-09-19 08:12:56 -0300118boot_flag_decode(uint8_t flag)
Fabio Utzig39000012018-07-30 12:40:20 -0300119{
Fabio Utzig39000012018-07-30 12:40:20 -0300120 if (flag != BOOT_FLAG_SET) {
121 return BOOT_FLAG_BAD;
122 }
123 return BOOT_FLAG_SET;
124}
125
Christopher Collinsa1c12042019-05-23 14:00:28 -0700126/**
127 * Determines if a status source table is satisfied by the specified magic
128 * code.
129 *
130 * @param tbl_val A magic field from a status source table.
131 * @param val The magic value in a trailer, encoded as a
132 * BOOT_MAGIC_[...].
133 *
134 * @return 1 if the two values are compatible;
135 * 0 otherwise.
136 */
137int
138boot_magic_compatible_check(uint8_t tbl_val, uint8_t val)
139{
140 switch (tbl_val) {
141 case BOOT_MAGIC_ANY:
142 return 1;
143
144 case BOOT_MAGIC_NOTGOOD:
145 return val != BOOT_MAGIC_GOOD;
146
147 default:
148 return tbl_val == val;
149 }
150}
151
Christopher Collins92ea77f2016-12-12 15:59:26 -0800152uint32_t
Christopher Collins2adef702019-05-22 14:37:31 -0700153boot_trailer_sz(uint8_t min_write_sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800154{
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300155 return /* state for all sectors */
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300156 BOOT_STATUS_MAX_ENTRIES * BOOT_STATUS_STATE_COUNT * min_write_sz +
Fabio Utzigba829042018-09-18 08:29:34 -0300157#ifdef MCUBOOT_ENC_IMAGES
158 /* encryption keys */
159 BOOT_ENC_KEY_SIZE * 2 +
160#endif
Christopher Collins2adef702019-05-22 14:37:31 -0700161 /* swap_type + copy_done + image_ok + swap_size */
162 BOOT_MAX_ALIGN * 4 +
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300163 BOOT_MAGIC_SZ;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800164}
165
166static uint32_t
167boot_magic_off(const struct flash_area *fap)
168{
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300169 return fap->fa_size - BOOT_MAGIC_SZ;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800170}
171
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400172int
173boot_status_entries(const struct flash_area *fap)
174{
175 switch (fap->fa_id) {
David Vincze2d736ad2019-02-18 11:50:22 +0100176 case FLASH_AREA_IMAGE_PRIMARY:
177 case FLASH_AREA_IMAGE_SECONDARY:
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400178 return BOOT_STATUS_STATE_COUNT * BOOT_STATUS_MAX_ENTRIES;
179 case FLASH_AREA_IMAGE_SCRATCH:
180 return BOOT_STATUS_STATE_COUNT;
181 default:
182 return BOOT_EBADARGS;
183 }
184}
185
Christopher Collins92ea77f2016-12-12 15:59:26 -0800186uint32_t
187boot_status_off(const struct flash_area *fap)
188{
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300189 uint32_t off_from_end;
190 uint8_t elem_sz;
191
192 elem_sz = flash_area_align(fap);
193
Christopher Collins2adef702019-05-22 14:37:31 -0700194 off_from_end = boot_trailer_sz(elem_sz);
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300195
196 assert(off_from_end <= fap->fa_size);
197 return fap->fa_size - off_from_end;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800198}
199
Christopher Collinsa1c12042019-05-23 14:00:28 -0700200uint32_t
David Vinczee2453472019-06-17 12:31:59 +0200201boot_swap_info_off(const struct flash_area *fap)
Christopher Collinsa1c12042019-05-23 14:00:28 -0700202{
203 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 3;
204}
205
Christopher Collins92ea77f2016-12-12 15:59:26 -0800206static uint32_t
207boot_copy_done_off(const struct flash_area *fap)
208{
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300209 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 2;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800210}
211
212static uint32_t
213boot_image_ok_off(const struct flash_area *fap)
214{
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300215 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800216}
217
Fabio Utzig46490722017-09-04 15:34:32 -0300218static uint32_t
219boot_swap_size_off(const struct flash_area *fap)
220{
Christopher Collins2adef702019-05-22 14:37:31 -0700221 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 4;
Fabio Utzig46490722017-09-04 15:34:32 -0300222}
223
Fabio Utzigba829042018-09-18 08:29:34 -0300224#ifdef MCUBOOT_ENC_IMAGES
225static uint32_t
226boot_enc_key_off(const struct flash_area *fap, uint8_t slot)
227{
Christopher Collins2adef702019-05-22 14:37:31 -0700228 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 4 -
Fabio Utzigba829042018-09-18 08:29:34 -0300229 ((slot + 1) * BOOT_ENC_KEY_SIZE);
230}
231#endif
232
Christopher Collins92ea77f2016-12-12 15:59:26 -0800233int
234boot_read_swap_state(const struct flash_area *fap,
235 struct boot_swap_state *state)
236{
Hovland, Sigvart1d96f362018-09-25 13:23:42 +0200237 uint32_t magic[BOOT_MAGIC_ARR_SZ];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800238 uint32_t off;
David Vinczee2453472019-06-17 12:31:59 +0200239 uint8_t swap_info;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800240 int rc;
241
242 off = boot_magic_off(fap);
Fabio Utzig178be542018-09-19 08:12:56 -0300243 rc = flash_area_read_is_empty(fap, off, magic, BOOT_MAGIC_SZ);
244 if (rc < 0) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800245 return BOOT_EFLASH;
246 }
Fabio Utzig178be542018-09-19 08:12:56 -0300247 if (rc == 1) {
248 state->magic = BOOT_MAGIC_UNSET;
249 } else {
250 state->magic = boot_magic_decode(magic);
251 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800252
David Vinczee2453472019-06-17 12:31:59 +0200253 off = boot_swap_info_off(fap);
254 rc = flash_area_read_is_empty(fap, off, &swap_info, sizeof swap_info);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700255 if (rc < 0) {
256 return BOOT_EFLASH;
257 }
David Vinczee2453472019-06-17 12:31:59 +0200258
259 /* Extract the swap type and image number */
260 state->swap_type = BOOT_GET_SWAP_TYPE(swap_info);
261 state->image_num = BOOT_GET_IMAGE_NUM(swap_info);
262
Christopher Collinsa1c12042019-05-23 14:00:28 -0700263 if (rc == 1 || state->swap_type > BOOT_SWAP_TYPE_REVERT) {
264 state->swap_type = BOOT_SWAP_TYPE_NONE;
David Vinczee2453472019-06-17 12:31:59 +0200265 state->image_num = 0;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700266 }
267
Christopher Collins2adef702019-05-22 14:37:31 -0700268 off = boot_copy_done_off(fap);
269 rc = flash_area_read_is_empty(fap, off, &state->copy_done,
270 sizeof state->copy_done);
271 if (rc < 0) {
272 return BOOT_EFLASH;
273 }
274 if (rc == 1) {
275 state->copy_done = BOOT_FLAG_UNSET;
276 } else {
277 state->copy_done = boot_flag_decode(state->copy_done);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800278 }
279
280 off = boot_image_ok_off(fap);
David Vincze2d736ad2019-02-18 11:50:22 +0100281 rc = flash_area_read_is_empty(fap, off, &state->image_ok,
282 sizeof state->image_ok);
Fabio Utzig178be542018-09-19 08:12:56 -0300283 if (rc < 0) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800284 return BOOT_EFLASH;
285 }
Fabio Utzig178be542018-09-19 08:12:56 -0300286 if (rc == 1) {
287 state->image_ok = BOOT_FLAG_UNSET;
288 } else {
289 state->image_ok = boot_flag_decode(state->image_ok);
290 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800291
292 return 0;
293}
294
295/**
296 * Reads the image trailer from the scratch area.
297 */
298int
Fabio Utzig2473ac02017-05-02 12:45:02 -0300299boot_read_swap_state_by_id(int flash_area_id, struct boot_swap_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800300{
301 const struct flash_area *fap;
302 int rc;
303
Fabio Utzig2473ac02017-05-02 12:45:02 -0300304 switch (flash_area_id) {
305 case FLASH_AREA_IMAGE_SCRATCH:
David Vincze2d736ad2019-02-18 11:50:22 +0100306 case FLASH_AREA_IMAGE_PRIMARY:
307 case FLASH_AREA_IMAGE_SECONDARY:
Fabio Utzig2473ac02017-05-02 12:45:02 -0300308 rc = flash_area_open(flash_area_id, &fap);
309 if (rc != 0) {
310 return BOOT_EFLASH;
311 }
312 break;
313 default:
Fabio Utzig856f7832017-05-22 11:04:44 -0400314 return BOOT_EBADARGS;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300315 }
316
317 rc = boot_read_swap_state(fap, state);
Fabio Utzigacfba2e2017-05-22 11:06:29 -0400318 flash_area_close(fap);
319 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800320}
321
322int
Fabio Utzig46490722017-09-04 15:34:32 -0300323boot_read_swap_size(uint32_t *swap_size)
324{
Hovland, Sigvart1d96f362018-09-25 13:23:42 +0200325 uint32_t magic[BOOT_MAGIC_ARR_SZ];
Fabio Utzig46490722017-09-04 15:34:32 -0300326 uint32_t off;
327 const struct flash_area *fap;
328 int rc;
329
330 /*
331 * In the middle a swap, tries to locate the saved swap size. Looks
David Vincze2d736ad2019-02-18 11:50:22 +0100332 * for a valid magic, first on the primary slot, then on scratch.
333 * Both "slots" can end up being temporary storage for a swap and it
334 * is assumed that if magic is valid then swap size is too, because
335 * magic is always written in the last step.
Fabio Utzig46490722017-09-04 15:34:32 -0300336 */
337
David Vincze2d736ad2019-02-18 11:50:22 +0100338 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap);
Fabio Utzig46490722017-09-04 15:34:32 -0300339 if (rc != 0) {
340 return BOOT_EFLASH;
341 }
342
343 off = boot_magic_off(fap);
344 rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ);
345 if (rc != 0) {
346 rc = BOOT_EFLASH;
347 goto out;
348 }
349
350 if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) != 0) {
351 /*
David Vincze2d736ad2019-02-18 11:50:22 +0100352 * If the primary slot's magic is not valid, try scratch...
Fabio Utzig46490722017-09-04 15:34:32 -0300353 */
354
355 flash_area_close(fap);
356
357 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap);
358 if (rc != 0) {
359 return BOOT_EFLASH;
360 }
361
362 off = boot_magic_off(fap);
363 rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ);
364 if (rc != 0) {
365 rc = BOOT_EFLASH;
366 goto out;
367 }
368
369 assert(memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0);
370 }
371
372 off = boot_swap_size_off(fap);
373 rc = flash_area_read(fap, off, swap_size, sizeof *swap_size);
374 if (rc != 0) {
375 rc = BOOT_EFLASH;
376 }
377
378out:
379 flash_area_close(fap);
380 return rc;
381}
382
Fabio Utzigba829042018-09-18 08:29:34 -0300383#ifdef MCUBOOT_ENC_IMAGES
384int
385boot_read_enc_key(uint8_t slot, uint8_t *enckey)
386{
387 uint32_t magic[BOOT_MAGIC_SZ];
388 uint32_t off;
389 const struct flash_area *fap;
390 int rc;
391
David Vincze2d736ad2019-02-18 11:50:22 +0100392 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap);
Fabio Utzigba829042018-09-18 08:29:34 -0300393 if (rc != 0) {
394 return BOOT_EFLASH;
395 }
396
397 off = boot_magic_off(fap);
398 rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ);
399 if (rc != 0) {
400 rc = BOOT_EFLASH;
401 goto out;
402 }
403
404 if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) != 0) {
405 /*
David Vincze2d736ad2019-02-18 11:50:22 +0100406 * If the primary slot's magic is not valid, try scratch...
Fabio Utzigba829042018-09-18 08:29:34 -0300407 */
408
409 flash_area_close(fap);
410
411 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap);
412 if (rc != 0) {
413 return BOOT_EFLASH;
414 }
415
416 off = boot_magic_off(fap);
417 rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ);
418 if (rc != 0) {
419 rc = BOOT_EFLASH;
420 goto out;
421 }
422
423 assert(memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0);
424 }
425
426 off = boot_enc_key_off(fap, slot);
427 rc = flash_area_read(fap, off, enckey, BOOT_ENC_KEY_SIZE);
428 if (rc != 0) {
429 rc = BOOT_EFLASH;
430 }
431
432out:
433 flash_area_close(fap);
434 return rc;
435}
436#endif
Fabio Utzig46490722017-09-04 15:34:32 -0300437
438int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800439boot_write_magic(const struct flash_area *fap)
440{
441 uint32_t off;
442 int rc;
443
444 off = boot_magic_off(fap);
445
Christopher Collins2c88e692019-05-22 15:10:14 -0700446 BOOT_LOG_DBG("writing magic; fa_id=%d off=0x%x (0x%x)",
447 fap->fa_id, off, fap->fa_off + off);
Fabio Utzig24a273d2017-04-20 08:21:31 -0300448 rc = flash_area_write(fap, off, boot_img_magic, BOOT_MAGIC_SZ);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800449 if (rc != 0) {
450 return BOOT_EFLASH;
451 }
452
453 return 0;
454}
455
Fabio Utzig2473ac02017-05-02 12:45:02 -0300456static int
Christopher Collinsa1c12042019-05-23 14:00:28 -0700457boot_write_trailer_byte(const struct flash_area *fap, uint32_t off,
458 uint8_t val)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800459{
Fabio Utzig644b8d42017-04-20 07:56:05 -0300460 uint8_t buf[BOOT_MAX_ALIGN];
David Brown9d725462017-01-23 15:50:58 -0700461 uint8_t align;
Fabio Utzig39000012018-07-30 12:40:20 -0300462 uint8_t erased_val;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700463 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800464
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200465 align = flash_area_align(fap);
Fabio Utzig644b8d42017-04-20 07:56:05 -0300466 assert(align <= BOOT_MAX_ALIGN);
Fabio Utzig39000012018-07-30 12:40:20 -0300467 erased_val = flash_area_erased_val(fap);
468 memset(buf, erased_val, BOOT_MAX_ALIGN);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700469 buf[0] = val;
David Brown9d725462017-01-23 15:50:58 -0700470
471 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800472 if (rc != 0) {
473 return BOOT_EFLASH;
474 }
475
476 return 0;
477}
478
479int
Fabio Utzig2473ac02017-05-02 12:45:02 -0300480boot_write_copy_done(const struct flash_area *fap)
481{
Christopher Collinsa1c12042019-05-23 14:00:28 -0700482 uint32_t off;
483
484 off = boot_copy_done_off(fap);
485 BOOT_LOG_DBG("writing copy_done; fa_id=%d off=0x%x (0x%x)",
486 fap->fa_id, off, fap->fa_off + off);
487 return boot_write_trailer_byte(fap, off, BOOT_FLAG_SET);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300488}
489
490int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800491boot_write_image_ok(const struct flash_area *fap)
492{
Christopher Collinsa1c12042019-05-23 14:00:28 -0700493 uint32_t off;
494
495 off = boot_image_ok_off(fap);
496 BOOT_LOG_DBG("writing image_ok; fa_id=%d off=0x%x (0x%x)",
497 fap->fa_id, off, fap->fa_off + off);
498 return boot_write_trailer_byte(fap, off, BOOT_FLAG_SET);
499}
500
501/**
502 * Writes the specified value to the `swap-type` field of an image trailer.
503 * This value is persisted so that the boot loader knows what swap operation to
504 * resume in case of an unexpected reset.
505 */
506int
David Vinczee2453472019-06-17 12:31:59 +0200507boot_write_swap_info(const struct flash_area *fap, uint8_t swap_type,
508 uint8_t image_num)
Christopher Collinsa1c12042019-05-23 14:00:28 -0700509{
510 uint32_t off;
David Vinczee2453472019-06-17 12:31:59 +0200511 uint8_t swap_info;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700512
David Vinczee2453472019-06-17 12:31:59 +0200513 BOOT_SET_SWAP_INFO(swap_info, image_num, swap_type);
514 off = boot_swap_info_off(fap);
515 BOOT_LOG_DBG("writing swap_info; fa_id=%d off=0x%x (0x%x), swap_type=0x%x"
516 " image_num=0x%x",
517 fap->fa_id, off, fap->fa_off + off, swap_type, image_num);
518 return boot_write_trailer_byte(fap, off, swap_info);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800519}
520
521int
Fabio Utzig46490722017-09-04 15:34:32 -0300522boot_write_swap_size(const struct flash_area *fap, uint32_t swap_size)
523{
524 uint32_t off;
525 int rc;
526 uint8_t buf[BOOT_MAX_ALIGN];
527 uint8_t align;
Fabio Utzig39000012018-07-30 12:40:20 -0300528 uint8_t erased_val;
Fabio Utzig46490722017-09-04 15:34:32 -0300529
530 off = boot_swap_size_off(fap);
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200531 align = flash_area_align(fap);
Fabio Utzig46490722017-09-04 15:34:32 -0300532 assert(align <= BOOT_MAX_ALIGN);
533 if (align < sizeof swap_size) {
534 align = sizeof swap_size;
535 }
Fabio Utzig39000012018-07-30 12:40:20 -0300536 erased_val = flash_area_erased_val(fap);
537 memset(buf, erased_val, BOOT_MAX_ALIGN);
Fabio Utzig46490722017-09-04 15:34:32 -0300538 memcpy(buf, (uint8_t *)&swap_size, sizeof swap_size);
539
Christopher Collins2c88e692019-05-22 15:10:14 -0700540 BOOT_LOG_DBG("writing swap_size; fa_id=%d off=0x%x (0x%x)",
541 fap->fa_id, off, fap->fa_off + off);
542
Fabio Utzig46490722017-09-04 15:34:32 -0300543 rc = flash_area_write(fap, off, buf, align);
544 if (rc != 0) {
545 return BOOT_EFLASH;
546 }
547
548 return 0;
549}
550
Fabio Utzigba829042018-09-18 08:29:34 -0300551#ifdef MCUBOOT_ENC_IMAGES
552int
553boot_write_enc_key(const struct flash_area *fap, uint8_t slot, const uint8_t *enckey)
554{
555 uint32_t off;
556 int rc;
557
558 off = boot_enc_key_off(fap, slot);
559 rc = flash_area_write(fap, off, enckey, BOOT_ENC_KEY_SIZE);
560 if (rc != 0) {
561 return BOOT_EFLASH;
562 }
563
564 return 0;
565}
566#endif
567
Fabio Utzig46490722017-09-04 15:34:32 -0300568int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800569boot_swap_type(void)
570{
571 const struct boot_swap_table *table;
David Vincze2d736ad2019-02-18 11:50:22 +0100572 struct boot_swap_state primary_slot;
573 struct boot_swap_state secondary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800574 int rc;
Fabio Utzigcd5774b2017-11-29 10:18:26 -0200575 size_t i;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800576
David Vincze2d736ad2019-02-18 11:50:22 +0100577 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY, &primary_slot);
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300578 if (rc) {
579 return BOOT_SWAP_TYPE_PANIC;
580 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800581
David Vincze2d736ad2019-02-18 11:50:22 +0100582 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY,
583 &secondary_slot);
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300584 if (rc) {
585 return BOOT_SWAP_TYPE_PANIC;
586 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800587
588 for (i = 0; i < BOOT_SWAP_TABLES_COUNT; i++) {
589 table = boot_swap_tables + i;
590
Christopher Collinsa1c12042019-05-23 14:00:28 -0700591 if (boot_magic_compatible_check(table->magic_primary_slot,
592 primary_slot.magic) &&
593 boot_magic_compatible_check(table->magic_secondary_slot,
594 secondary_slot.magic) &&
David Vincze2d736ad2019-02-18 11:50:22 +0100595 (table->image_ok_primary_slot == BOOT_FLAG_ANY ||
596 table->image_ok_primary_slot == primary_slot.image_ok) &&
597 (table->image_ok_secondary_slot == BOOT_FLAG_ANY ||
598 table->image_ok_secondary_slot == secondary_slot.image_ok) &&
599 (table->copy_done_primary_slot == BOOT_FLAG_ANY ||
600 table->copy_done_primary_slot == primary_slot.copy_done)) {
Fabio Utzig34e393e2017-05-22 11:07:07 -0400601 BOOT_LOG_INF("Swap type: %s",
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300602 table->swap_type == BOOT_SWAP_TYPE_TEST ? "test" :
603 table->swap_type == BOOT_SWAP_TYPE_PERM ? "perm" :
604 table->swap_type == BOOT_SWAP_TYPE_REVERT ? "revert" :
605 "BUG; can't happen");
606 assert(table->swap_type == BOOT_SWAP_TYPE_TEST ||
607 table->swap_type == BOOT_SWAP_TYPE_PERM ||
608 table->swap_type == BOOT_SWAP_TYPE_REVERT);
609 return table->swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800610 }
611 }
612
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300613 BOOT_LOG_INF("Swap type: none");
Christopher Collins92ea77f2016-12-12 15:59:26 -0800614 return BOOT_SWAP_TYPE_NONE;
615}
616
617/**
David Vincze2d736ad2019-02-18 11:50:22 +0100618 * Marks the image in the secondary slot as pending. On the next reboot,
619 * the system will perform a one-time boot of the the secondary slot image.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800620 *
Christopher Collins7835c1e2016-12-21 10:10:51 -0800621 * @param permanent Whether the image should be used permanently or
622 * only tested once:
623 * 0=run image once, then confirm or revert.
624 * 1=run image forever.
625 *
Christopher Collins92ea77f2016-12-12 15:59:26 -0800626 * @return 0 on success; nonzero on failure.
627 */
628int
Christopher Collins7835c1e2016-12-21 10:10:51 -0800629boot_set_pending(int permanent)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800630{
631 const struct flash_area *fap;
David Vincze2d736ad2019-02-18 11:50:22 +0100632 struct boot_swap_state state_secondary_slot;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700633 uint8_t swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800634 int rc;
635
David Vincze2d736ad2019-02-18 11:50:22 +0100636 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY,
637 &state_secondary_slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800638 if (rc != 0) {
639 return rc;
640 }
641
David Vincze2d736ad2019-02-18 11:50:22 +0100642 switch (state_secondary_slot.magic) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800643 case BOOT_MAGIC_GOOD:
644 /* Swap already scheduled. */
645 return 0;
646
647 case BOOT_MAGIC_UNSET:
David Vincze2d736ad2019-02-18 11:50:22 +0100648 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800649 if (rc != 0) {
650 rc = BOOT_EFLASH;
651 } else {
652 rc = boot_write_magic(fap);
653 }
654
Christopher Collins7835c1e2016-12-21 10:10:51 -0800655 if (rc == 0 && permanent) {
656 rc = boot_write_image_ok(fap);
657 }
658
Christopher Collinsa1c12042019-05-23 14:00:28 -0700659 if (rc == 0) {
660 if (permanent) {
661 swap_type = BOOT_SWAP_TYPE_PERM;
662 } else {
663 swap_type = BOOT_SWAP_TYPE_TEST;
664 }
David Vinczee2453472019-06-17 12:31:59 +0200665 rc = boot_write_swap_info(fap, swap_type, 0);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700666 }
667
Christopher Collins92ea77f2016-12-12 15:59:26 -0800668 flash_area_close(fap);
669 return rc;
670
Christopher Collinsae01f152019-01-30 09:56:37 -0800671 case BOOT_MAGIC_BAD:
672 /* The image slot is corrupt. There is no way to recover, so erase the
673 * slot to allow future upgrades.
674 */
David Vincze2d736ad2019-02-18 11:50:22 +0100675 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY, &fap);
Christopher Collinsae01f152019-01-30 09:56:37 -0800676 if (rc != 0) {
677 return BOOT_EFLASH;
678 }
679
680 flash_area_erase(fap, 0, fap->fa_size);
681 flash_area_close(fap);
682 return BOOT_EBADIMAGE;
683
Christopher Collins92ea77f2016-12-12 15:59:26 -0800684 default:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800685 assert(0);
Christopher Collinsae01f152019-01-30 09:56:37 -0800686 return BOOT_EBADIMAGE;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800687 }
688}
689
690/**
David Vincze2d736ad2019-02-18 11:50:22 +0100691 * Marks the image in the primary slot as confirmed. The system will continue
692 * booting into the image in the primary slot until told to boot from a
693 * different slot.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800694 *
695 * @return 0 on success; nonzero on failure.
696 */
697int
698boot_set_confirmed(void)
699{
700 const struct flash_area *fap;
David Vincze2d736ad2019-02-18 11:50:22 +0100701 struct boot_swap_state state_primary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800702 int rc;
703
David Vincze2d736ad2019-02-18 11:50:22 +0100704 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY,
705 &state_primary_slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800706 if (rc != 0) {
707 return rc;
708 }
709
David Vincze2d736ad2019-02-18 11:50:22 +0100710 switch (state_primary_slot.magic) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800711 case BOOT_MAGIC_GOOD:
712 /* Confirm needed; proceed. */
713 break;
714
715 case BOOT_MAGIC_UNSET:
716 /* Already confirmed. */
717 return 0;
718
719 case BOOT_MAGIC_BAD:
720 /* Unexpected state. */
721 return BOOT_EBADVECT;
722 }
723
David Vincze2d736ad2019-02-18 11:50:22 +0100724 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800725 if (rc) {
726 rc = BOOT_EFLASH;
727 goto done;
728 }
729
David Vincze2d736ad2019-02-18 11:50:22 +0100730 if (state_primary_slot.copy_done == BOOT_FLAG_UNSET) {
Fabio Utzig39000012018-07-30 12:40:20 -0300731 /* Swap never completed. This is unexpected. */
732 rc = BOOT_EBADVECT;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800733 goto done;
734 }
735
David Vincze2d736ad2019-02-18 11:50:22 +0100736 if (state_primary_slot.image_ok != BOOT_FLAG_UNSET) {
Fabio Utzig39000012018-07-30 12:40:20 -0300737 /* Already confirmed. */
738 goto done;
739 }
740
741 rc = boot_write_image_ok(fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800742
743done:
744 flash_area_close(fap);
745 return rc;
746}