blob: b2a07d09738b971dfc26cc76bedede4126ec9e53 [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{
David Vinczeb75c12a2019-03-22 14:58:33 +0100175 if (fap->fa_id == FLASH_AREA_IMAGE_SCRATCH) {
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400176 return BOOT_STATUS_STATE_COUNT;
David Vinczeb75c12a2019-03-22 14:58:33 +0100177 } else if ((fap->fa_id == FLASH_AREA_IMAGE_PRIMARY) ||
178 (fap->fa_id == FLASH_AREA_IMAGE_SECONDARY)) {
179 return BOOT_STATUS_STATE_COUNT * BOOT_STATUS_MAX_ENTRIES;
180 } else {
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400181 return BOOT_EBADARGS;
182 }
183}
184
Christopher Collins92ea77f2016-12-12 15:59:26 -0800185uint32_t
186boot_status_off(const struct flash_area *fap)
187{
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300188 uint32_t off_from_end;
189 uint8_t elem_sz;
190
191 elem_sz = flash_area_align(fap);
192
Christopher Collins2adef702019-05-22 14:37:31 -0700193 off_from_end = boot_trailer_sz(elem_sz);
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300194
195 assert(off_from_end <= fap->fa_size);
196 return fap->fa_size - off_from_end;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800197}
198
Christopher Collinsa1c12042019-05-23 14:00:28 -0700199uint32_t
David Vinczee2453472019-06-17 12:31:59 +0200200boot_swap_info_off(const struct flash_area *fap)
Christopher Collinsa1c12042019-05-23 14:00:28 -0700201{
202 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 3;
203}
204
Christopher Collins92ea77f2016-12-12 15:59:26 -0800205static uint32_t
206boot_copy_done_off(const struct flash_area *fap)
207{
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300208 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 2;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800209}
210
211static uint32_t
212boot_image_ok_off(const struct flash_area *fap)
213{
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300214 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800215}
216
Fabio Utzig46490722017-09-04 15:34:32 -0300217static uint32_t
218boot_swap_size_off(const struct flash_area *fap)
219{
Christopher Collins2adef702019-05-22 14:37:31 -0700220 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 4;
Fabio Utzig46490722017-09-04 15:34:32 -0300221}
222
Fabio Utzigba829042018-09-18 08:29:34 -0300223#ifdef MCUBOOT_ENC_IMAGES
224static uint32_t
225boot_enc_key_off(const struct flash_area *fap, uint8_t slot)
226{
Christopher Collins2adef702019-05-22 14:37:31 -0700227 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 4 -
Fabio Utzigba829042018-09-18 08:29:34 -0300228 ((slot + 1) * BOOT_ENC_KEY_SIZE);
229}
230#endif
231
Christopher Collins92ea77f2016-12-12 15:59:26 -0800232int
233boot_read_swap_state(const struct flash_area *fap,
234 struct boot_swap_state *state)
235{
Hovland, Sigvart1d96f362018-09-25 13:23:42 +0200236 uint32_t magic[BOOT_MAGIC_ARR_SZ];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800237 uint32_t off;
David Vinczee2453472019-06-17 12:31:59 +0200238 uint8_t swap_info;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800239 int rc;
240
241 off = boot_magic_off(fap);
Fabio Utzig178be542018-09-19 08:12:56 -0300242 rc = flash_area_read_is_empty(fap, off, magic, BOOT_MAGIC_SZ);
243 if (rc < 0) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800244 return BOOT_EFLASH;
245 }
Fabio Utzig178be542018-09-19 08:12:56 -0300246 if (rc == 1) {
247 state->magic = BOOT_MAGIC_UNSET;
248 } else {
249 state->magic = boot_magic_decode(magic);
250 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800251
David Vinczee2453472019-06-17 12:31:59 +0200252 off = boot_swap_info_off(fap);
253 rc = flash_area_read_is_empty(fap, off, &swap_info, sizeof swap_info);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700254 if (rc < 0) {
255 return BOOT_EFLASH;
256 }
David Vinczee2453472019-06-17 12:31:59 +0200257
258 /* Extract the swap type and image number */
259 state->swap_type = BOOT_GET_SWAP_TYPE(swap_info);
260 state->image_num = BOOT_GET_IMAGE_NUM(swap_info);
261
Christopher Collinsa1c12042019-05-23 14:00:28 -0700262 if (rc == 1 || state->swap_type > BOOT_SWAP_TYPE_REVERT) {
263 state->swap_type = BOOT_SWAP_TYPE_NONE;
David Vinczee2453472019-06-17 12:31:59 +0200264 state->image_num = 0;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700265 }
266
Christopher Collins2adef702019-05-22 14:37:31 -0700267 off = boot_copy_done_off(fap);
268 rc = flash_area_read_is_empty(fap, off, &state->copy_done,
269 sizeof state->copy_done);
270 if (rc < 0) {
271 return BOOT_EFLASH;
272 }
273 if (rc == 1) {
274 state->copy_done = BOOT_FLAG_UNSET;
275 } else {
276 state->copy_done = boot_flag_decode(state->copy_done);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800277 }
278
279 off = boot_image_ok_off(fap);
David Vincze2d736ad2019-02-18 11:50:22 +0100280 rc = flash_area_read_is_empty(fap, off, &state->image_ok,
281 sizeof state->image_ok);
Fabio Utzig178be542018-09-19 08:12:56 -0300282 if (rc < 0) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800283 return BOOT_EFLASH;
284 }
Fabio Utzig178be542018-09-19 08:12:56 -0300285 if (rc == 1) {
286 state->image_ok = BOOT_FLAG_UNSET;
287 } else {
288 state->image_ok = boot_flag_decode(state->image_ok);
289 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800290
291 return 0;
292}
293
294/**
295 * Reads the image trailer from the scratch area.
296 */
297int
Fabio Utzig2473ac02017-05-02 12:45:02 -0300298boot_read_swap_state_by_id(int flash_area_id, struct boot_swap_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800299{
300 const struct flash_area *fap;
301 int rc;
302
David Vinczeb75c12a2019-03-22 14:58:33 +0100303 if (flash_area_id == FLASH_AREA_IMAGE_SCRATCH ||
304 flash_area_id == FLASH_AREA_IMAGE_PRIMARY ||
305 flash_area_id == FLASH_AREA_IMAGE_SECONDARY) {
Fabio Utzig2473ac02017-05-02 12:45:02 -0300306 rc = flash_area_open(flash_area_id, &fap);
307 if (rc != 0) {
308 return BOOT_EFLASH;
309 }
David Vinczeb75c12a2019-03-22 14:58:33 +0100310 } else {
Fabio Utzig856f7832017-05-22 11:04:44 -0400311 return BOOT_EBADARGS;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300312 }
313
314 rc = boot_read_swap_state(fap, state);
Fabio Utzigacfba2e2017-05-22 11:06:29 -0400315 flash_area_close(fap);
316 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800317}
318
319int
Fabio Utzig46490722017-09-04 15:34:32 -0300320boot_read_swap_size(uint32_t *swap_size)
321{
Hovland, Sigvart1d96f362018-09-25 13:23:42 +0200322 uint32_t magic[BOOT_MAGIC_ARR_SZ];
Fabio Utzig46490722017-09-04 15:34:32 -0300323 uint32_t off;
324 const struct flash_area *fap;
325 int rc;
326
327 /*
328 * In the middle a swap, tries to locate the saved swap size. Looks
David Vincze2d736ad2019-02-18 11:50:22 +0100329 * for a valid magic, first on the primary slot, then on scratch.
330 * Both "slots" can end up being temporary storage for a swap and it
331 * is assumed that if magic is valid then swap size is too, because
332 * magic is always written in the last step.
Fabio Utzig46490722017-09-04 15:34:32 -0300333 */
334
David Vincze2d736ad2019-02-18 11:50:22 +0100335 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap);
Fabio Utzig46490722017-09-04 15:34:32 -0300336 if (rc != 0) {
337 return BOOT_EFLASH;
338 }
339
340 off = boot_magic_off(fap);
341 rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ);
342 if (rc != 0) {
343 rc = BOOT_EFLASH;
344 goto out;
345 }
346
347 if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) != 0) {
348 /*
David Vincze2d736ad2019-02-18 11:50:22 +0100349 * If the primary slot's magic is not valid, try scratch...
Fabio Utzig46490722017-09-04 15:34:32 -0300350 */
351
352 flash_area_close(fap);
353
354 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap);
355 if (rc != 0) {
356 return BOOT_EFLASH;
357 }
358
359 off = boot_magic_off(fap);
360 rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ);
361 if (rc != 0) {
362 rc = BOOT_EFLASH;
363 goto out;
364 }
365
366 assert(memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0);
367 }
368
369 off = boot_swap_size_off(fap);
370 rc = flash_area_read(fap, off, swap_size, sizeof *swap_size);
371 if (rc != 0) {
372 rc = BOOT_EFLASH;
373 }
374
375out:
376 flash_area_close(fap);
377 return rc;
378}
379
Fabio Utzigba829042018-09-18 08:29:34 -0300380#ifdef MCUBOOT_ENC_IMAGES
381int
382boot_read_enc_key(uint8_t slot, uint8_t *enckey)
383{
384 uint32_t magic[BOOT_MAGIC_SZ];
385 uint32_t off;
386 const struct flash_area *fap;
387 int rc;
388
David Vincze2d736ad2019-02-18 11:50:22 +0100389 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap);
Fabio Utzigba829042018-09-18 08:29:34 -0300390 if (rc != 0) {
391 return BOOT_EFLASH;
392 }
393
394 off = boot_magic_off(fap);
395 rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ);
396 if (rc != 0) {
397 rc = BOOT_EFLASH;
398 goto out;
399 }
400
401 if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) != 0) {
402 /*
David Vincze2d736ad2019-02-18 11:50:22 +0100403 * If the primary slot's magic is not valid, try scratch...
Fabio Utzigba829042018-09-18 08:29:34 -0300404 */
405
406 flash_area_close(fap);
407
408 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap);
409 if (rc != 0) {
410 return BOOT_EFLASH;
411 }
412
413 off = boot_magic_off(fap);
414 rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ);
415 if (rc != 0) {
416 rc = BOOT_EFLASH;
417 goto out;
418 }
419
420 assert(memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0);
421 }
422
423 off = boot_enc_key_off(fap, slot);
424 rc = flash_area_read(fap, off, enckey, BOOT_ENC_KEY_SIZE);
425 if (rc != 0) {
426 rc = BOOT_EFLASH;
427 }
428
429out:
430 flash_area_close(fap);
431 return rc;
432}
433#endif
Fabio Utzig46490722017-09-04 15:34:32 -0300434
435int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800436boot_write_magic(const struct flash_area *fap)
437{
438 uint32_t off;
439 int rc;
440
441 off = boot_magic_off(fap);
442
Christopher Collins2c88e692019-05-22 15:10:14 -0700443 BOOT_LOG_DBG("writing magic; fa_id=%d off=0x%x (0x%x)",
444 fap->fa_id, off, fap->fa_off + off);
Fabio Utzig24a273d2017-04-20 08:21:31 -0300445 rc = flash_area_write(fap, off, boot_img_magic, BOOT_MAGIC_SZ);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800446 if (rc != 0) {
447 return BOOT_EFLASH;
448 }
449
450 return 0;
451}
452
Fabio Utzig2473ac02017-05-02 12:45:02 -0300453static int
Christopher Collinsa1c12042019-05-23 14:00:28 -0700454boot_write_trailer_byte(const struct flash_area *fap, uint32_t off,
455 uint8_t val)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800456{
Fabio Utzig644b8d42017-04-20 07:56:05 -0300457 uint8_t buf[BOOT_MAX_ALIGN];
David Brown9d725462017-01-23 15:50:58 -0700458 uint8_t align;
Fabio Utzig39000012018-07-30 12:40:20 -0300459 uint8_t erased_val;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700460 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800461
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200462 align = flash_area_align(fap);
Fabio Utzig644b8d42017-04-20 07:56:05 -0300463 assert(align <= BOOT_MAX_ALIGN);
Fabio Utzig39000012018-07-30 12:40:20 -0300464 erased_val = flash_area_erased_val(fap);
465 memset(buf, erased_val, BOOT_MAX_ALIGN);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700466 buf[0] = val;
David Brown9d725462017-01-23 15:50:58 -0700467
468 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800469 if (rc != 0) {
470 return BOOT_EFLASH;
471 }
472
473 return 0;
474}
475
476int
Fabio Utzig2473ac02017-05-02 12:45:02 -0300477boot_write_copy_done(const struct flash_area *fap)
478{
Christopher Collinsa1c12042019-05-23 14:00:28 -0700479 uint32_t off;
480
481 off = boot_copy_done_off(fap);
482 BOOT_LOG_DBG("writing copy_done; fa_id=%d off=0x%x (0x%x)",
483 fap->fa_id, off, fap->fa_off + off);
484 return boot_write_trailer_byte(fap, off, BOOT_FLAG_SET);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300485}
486
487int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800488boot_write_image_ok(const struct flash_area *fap)
489{
Christopher Collinsa1c12042019-05-23 14:00:28 -0700490 uint32_t off;
491
492 off = boot_image_ok_off(fap);
493 BOOT_LOG_DBG("writing image_ok; fa_id=%d off=0x%x (0x%x)",
494 fap->fa_id, off, fap->fa_off + off);
495 return boot_write_trailer_byte(fap, off, BOOT_FLAG_SET);
496}
497
498/**
499 * Writes the specified value to the `swap-type` field of an image trailer.
500 * This value is persisted so that the boot loader knows what swap operation to
501 * resume in case of an unexpected reset.
502 */
503int
David Vinczee2453472019-06-17 12:31:59 +0200504boot_write_swap_info(const struct flash_area *fap, uint8_t swap_type,
505 uint8_t image_num)
Christopher Collinsa1c12042019-05-23 14:00:28 -0700506{
507 uint32_t off;
David Vinczee2453472019-06-17 12:31:59 +0200508 uint8_t swap_info;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700509
David Vinczee2453472019-06-17 12:31:59 +0200510 BOOT_SET_SWAP_INFO(swap_info, image_num, swap_type);
511 off = boot_swap_info_off(fap);
512 BOOT_LOG_DBG("writing swap_info; fa_id=%d off=0x%x (0x%x), swap_type=0x%x"
513 " image_num=0x%x",
514 fap->fa_id, off, fap->fa_off + off, swap_type, image_num);
515 return boot_write_trailer_byte(fap, off, swap_info);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800516}
517
518int
Fabio Utzig46490722017-09-04 15:34:32 -0300519boot_write_swap_size(const struct flash_area *fap, uint32_t swap_size)
520{
521 uint32_t off;
522 int rc;
523 uint8_t buf[BOOT_MAX_ALIGN];
524 uint8_t align;
Fabio Utzig39000012018-07-30 12:40:20 -0300525 uint8_t erased_val;
Fabio Utzig46490722017-09-04 15:34:32 -0300526
527 off = boot_swap_size_off(fap);
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200528 align = flash_area_align(fap);
Fabio Utzig46490722017-09-04 15:34:32 -0300529 assert(align <= BOOT_MAX_ALIGN);
530 if (align < sizeof swap_size) {
531 align = sizeof swap_size;
532 }
Fabio Utzig39000012018-07-30 12:40:20 -0300533 erased_val = flash_area_erased_val(fap);
534 memset(buf, erased_val, BOOT_MAX_ALIGN);
Fabio Utzig46490722017-09-04 15:34:32 -0300535 memcpy(buf, (uint8_t *)&swap_size, sizeof swap_size);
536
Christopher Collins2c88e692019-05-22 15:10:14 -0700537 BOOT_LOG_DBG("writing swap_size; fa_id=%d off=0x%x (0x%x)",
538 fap->fa_id, off, fap->fa_off + off);
539
Fabio Utzig46490722017-09-04 15:34:32 -0300540 rc = flash_area_write(fap, off, buf, align);
541 if (rc != 0) {
542 return BOOT_EFLASH;
543 }
544
545 return 0;
546}
547
Fabio Utzigba829042018-09-18 08:29:34 -0300548#ifdef MCUBOOT_ENC_IMAGES
549int
550boot_write_enc_key(const struct flash_area *fap, uint8_t slot, const uint8_t *enckey)
551{
552 uint32_t off;
553 int rc;
554
555 off = boot_enc_key_off(fap, slot);
556 rc = flash_area_write(fap, off, enckey, BOOT_ENC_KEY_SIZE);
557 if (rc != 0) {
558 return BOOT_EFLASH;
559 }
560
561 return 0;
562}
563#endif
564
Fabio Utzig46490722017-09-04 15:34:32 -0300565int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800566boot_swap_type(void)
567{
568 const struct boot_swap_table *table;
David Vincze2d736ad2019-02-18 11:50:22 +0100569 struct boot_swap_state primary_slot;
570 struct boot_swap_state secondary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800571 int rc;
Fabio Utzigcd5774b2017-11-29 10:18:26 -0200572 size_t i;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800573
David Vincze2d736ad2019-02-18 11:50:22 +0100574 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY, &primary_slot);
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300575 if (rc) {
576 return BOOT_SWAP_TYPE_PANIC;
577 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800578
David Vincze2d736ad2019-02-18 11:50:22 +0100579 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY,
580 &secondary_slot);
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300581 if (rc) {
582 return BOOT_SWAP_TYPE_PANIC;
583 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800584
585 for (i = 0; i < BOOT_SWAP_TABLES_COUNT; i++) {
586 table = boot_swap_tables + i;
587
Christopher Collinsa1c12042019-05-23 14:00:28 -0700588 if (boot_magic_compatible_check(table->magic_primary_slot,
589 primary_slot.magic) &&
590 boot_magic_compatible_check(table->magic_secondary_slot,
591 secondary_slot.magic) &&
David Vincze2d736ad2019-02-18 11:50:22 +0100592 (table->image_ok_primary_slot == BOOT_FLAG_ANY ||
593 table->image_ok_primary_slot == primary_slot.image_ok) &&
594 (table->image_ok_secondary_slot == BOOT_FLAG_ANY ||
595 table->image_ok_secondary_slot == secondary_slot.image_ok) &&
596 (table->copy_done_primary_slot == BOOT_FLAG_ANY ||
597 table->copy_done_primary_slot == primary_slot.copy_done)) {
Fabio Utzig34e393e2017-05-22 11:07:07 -0400598 BOOT_LOG_INF("Swap type: %s",
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300599 table->swap_type == BOOT_SWAP_TYPE_TEST ? "test" :
600 table->swap_type == BOOT_SWAP_TYPE_PERM ? "perm" :
601 table->swap_type == BOOT_SWAP_TYPE_REVERT ? "revert" :
602 "BUG; can't happen");
603 assert(table->swap_type == BOOT_SWAP_TYPE_TEST ||
604 table->swap_type == BOOT_SWAP_TYPE_PERM ||
605 table->swap_type == BOOT_SWAP_TYPE_REVERT);
606 return table->swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800607 }
608 }
609
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300610 BOOT_LOG_INF("Swap type: none");
Christopher Collins92ea77f2016-12-12 15:59:26 -0800611 return BOOT_SWAP_TYPE_NONE;
612}
613
614/**
David Vincze2d736ad2019-02-18 11:50:22 +0100615 * Marks the image in the secondary slot as pending. On the next reboot,
616 * the system will perform a one-time boot of the the secondary slot image.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800617 *
Christopher Collins7835c1e2016-12-21 10:10:51 -0800618 * @param permanent Whether the image should be used permanently or
619 * only tested once:
620 * 0=run image once, then confirm or revert.
621 * 1=run image forever.
622 *
Christopher Collins92ea77f2016-12-12 15:59:26 -0800623 * @return 0 on success; nonzero on failure.
624 */
625int
Christopher Collins7835c1e2016-12-21 10:10:51 -0800626boot_set_pending(int permanent)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800627{
628 const struct flash_area *fap;
David Vincze2d736ad2019-02-18 11:50:22 +0100629 struct boot_swap_state state_secondary_slot;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700630 uint8_t swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800631 int rc;
632
David Vincze2d736ad2019-02-18 11:50:22 +0100633 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY,
634 &state_secondary_slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800635 if (rc != 0) {
636 return rc;
637 }
638
David Vincze2d736ad2019-02-18 11:50:22 +0100639 switch (state_secondary_slot.magic) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800640 case BOOT_MAGIC_GOOD:
641 /* Swap already scheduled. */
642 return 0;
643
644 case BOOT_MAGIC_UNSET:
David Vincze2d736ad2019-02-18 11:50:22 +0100645 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800646 if (rc != 0) {
647 rc = BOOT_EFLASH;
648 } else {
649 rc = boot_write_magic(fap);
650 }
651
Christopher Collins7835c1e2016-12-21 10:10:51 -0800652 if (rc == 0 && permanent) {
653 rc = boot_write_image_ok(fap);
654 }
655
Christopher Collinsa1c12042019-05-23 14:00:28 -0700656 if (rc == 0) {
657 if (permanent) {
658 swap_type = BOOT_SWAP_TYPE_PERM;
659 } else {
660 swap_type = BOOT_SWAP_TYPE_TEST;
661 }
David Vinczee2453472019-06-17 12:31:59 +0200662 rc = boot_write_swap_info(fap, swap_type, 0);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700663 }
664
Christopher Collins92ea77f2016-12-12 15:59:26 -0800665 flash_area_close(fap);
666 return rc;
667
Christopher Collinsae01f152019-01-30 09:56:37 -0800668 case BOOT_MAGIC_BAD:
669 /* The image slot is corrupt. There is no way to recover, so erase the
670 * slot to allow future upgrades.
671 */
David Vincze2d736ad2019-02-18 11:50:22 +0100672 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY, &fap);
Christopher Collinsae01f152019-01-30 09:56:37 -0800673 if (rc != 0) {
674 return BOOT_EFLASH;
675 }
676
677 flash_area_erase(fap, 0, fap->fa_size);
678 flash_area_close(fap);
679 return BOOT_EBADIMAGE;
680
Christopher Collins92ea77f2016-12-12 15:59:26 -0800681 default:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800682 assert(0);
Christopher Collinsae01f152019-01-30 09:56:37 -0800683 return BOOT_EBADIMAGE;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800684 }
685}
686
687/**
David Vincze2d736ad2019-02-18 11:50:22 +0100688 * Marks the image in the primary slot as confirmed. The system will continue
689 * booting into the image in the primary slot until told to boot from a
690 * different slot.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800691 *
692 * @return 0 on success; nonzero on failure.
693 */
694int
695boot_set_confirmed(void)
696{
697 const struct flash_area *fap;
David Vincze2d736ad2019-02-18 11:50:22 +0100698 struct boot_swap_state state_primary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800699 int rc;
700
David Vincze2d736ad2019-02-18 11:50:22 +0100701 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY,
702 &state_primary_slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800703 if (rc != 0) {
704 return rc;
705 }
706
David Vincze2d736ad2019-02-18 11:50:22 +0100707 switch (state_primary_slot.magic) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800708 case BOOT_MAGIC_GOOD:
709 /* Confirm needed; proceed. */
710 break;
711
712 case BOOT_MAGIC_UNSET:
713 /* Already confirmed. */
714 return 0;
715
716 case BOOT_MAGIC_BAD:
717 /* Unexpected state. */
718 return BOOT_EBADVECT;
719 }
720
David Vincze2d736ad2019-02-18 11:50:22 +0100721 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800722 if (rc) {
723 rc = BOOT_EFLASH;
724 goto done;
725 }
726
David Vincze2d736ad2019-02-18 11:50:22 +0100727 if (state_primary_slot.copy_done == BOOT_FLAG_UNSET) {
Fabio Utzig39000012018-07-30 12:40:20 -0300728 /* Swap never completed. This is unexpected. */
729 rc = BOOT_EBADVECT;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800730 goto done;
731 }
732
David Vincze2d736ad2019-02-18 11:50:22 +0100733 if (state_primary_slot.image_ok != BOOT_FLAG_UNSET) {
Fabio Utzig39000012018-07-30 12:40:20 -0300734 /* Already confirmed. */
735 goto done;
736 }
737
738 rc = boot_write_image_ok(fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800739
740done:
741 flash_area_close(fap);
742 return rc;
743}