blob: 01c54d5a3340cb309624fa99d68909fe8e1ded5e [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
Fabio Utzigb0f04732019-07-31 09:49:19 -0300173boot_status_entries(int image_index, const struct flash_area *fap)
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400174{
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;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300177 } else if (fap->fa_id == FLASH_AREA_IMAGE_PRIMARY(image_index) ||
178 fap->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
David Vinczeb75c12a2019-03-22 14:58:33 +0100179 return BOOT_STATUS_STATE_COUNT * BOOT_STATUS_MAX_ENTRIES;
180 } else {
Fabio Utzigb0f04732019-07-31 09:49:19 -0300181 /* FIXME: make error values negative and check on caller */
182 assert(0);
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400183 return BOOT_EBADARGS;
184 }
185}
186
Christopher Collins92ea77f2016-12-12 15:59:26 -0800187uint32_t
188boot_status_off(const struct flash_area *fap)
189{
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300190 uint32_t off_from_end;
191 uint8_t elem_sz;
192
193 elem_sz = flash_area_align(fap);
194
Christopher Collins2adef702019-05-22 14:37:31 -0700195 off_from_end = boot_trailer_sz(elem_sz);
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300196
197 assert(off_from_end <= fap->fa_size);
198 return fap->fa_size - off_from_end;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800199}
200
Christopher Collinsa1c12042019-05-23 14:00:28 -0700201uint32_t
David Vinczee2453472019-06-17 12:31:59 +0200202boot_swap_info_off(const struct flash_area *fap)
Christopher Collinsa1c12042019-05-23 14:00:28 -0700203{
204 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 3;
205}
206
Christopher Collins92ea77f2016-12-12 15:59:26 -0800207static uint32_t
208boot_copy_done_off(const struct flash_area *fap)
209{
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300210 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 2;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800211}
212
213static uint32_t
214boot_image_ok_off(const struct flash_area *fap)
215{
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300216 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800217}
218
Fabio Utzig46490722017-09-04 15:34:32 -0300219static uint32_t
220boot_swap_size_off(const struct flash_area *fap)
221{
Christopher Collins2adef702019-05-22 14:37:31 -0700222 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 4;
Fabio Utzig46490722017-09-04 15:34:32 -0300223}
224
Fabio Utzigba829042018-09-18 08:29:34 -0300225#ifdef MCUBOOT_ENC_IMAGES
226static uint32_t
227boot_enc_key_off(const struct flash_area *fap, uint8_t slot)
228{
Christopher Collins2adef702019-05-22 14:37:31 -0700229 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 4 -
Fabio Utzigba829042018-09-18 08:29:34 -0300230 ((slot + 1) * BOOT_ENC_KEY_SIZE);
231}
232#endif
233
Christopher Collins92ea77f2016-12-12 15:59:26 -0800234int
235boot_read_swap_state(const struct flash_area *fap,
236 struct boot_swap_state *state)
237{
Hovland, Sigvart1d96f362018-09-25 13:23:42 +0200238 uint32_t magic[BOOT_MAGIC_ARR_SZ];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800239 uint32_t off;
David Vinczee2453472019-06-17 12:31:59 +0200240 uint8_t swap_info;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800241 int rc;
242
243 off = boot_magic_off(fap);
Fabio Utzig178be542018-09-19 08:12:56 -0300244 rc = flash_area_read_is_empty(fap, off, magic, BOOT_MAGIC_SZ);
245 if (rc < 0) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800246 return BOOT_EFLASH;
247 }
Fabio Utzig178be542018-09-19 08:12:56 -0300248 if (rc == 1) {
249 state->magic = BOOT_MAGIC_UNSET;
250 } else {
251 state->magic = boot_magic_decode(magic);
252 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800253
David Vinczee2453472019-06-17 12:31:59 +0200254 off = boot_swap_info_off(fap);
255 rc = flash_area_read_is_empty(fap, off, &swap_info, sizeof swap_info);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700256 if (rc < 0) {
257 return BOOT_EFLASH;
258 }
David Vinczee2453472019-06-17 12:31:59 +0200259
260 /* Extract the swap type and image number */
261 state->swap_type = BOOT_GET_SWAP_TYPE(swap_info);
262 state->image_num = BOOT_GET_IMAGE_NUM(swap_info);
263
Christopher Collinsa1c12042019-05-23 14:00:28 -0700264 if (rc == 1 || state->swap_type > BOOT_SWAP_TYPE_REVERT) {
265 state->swap_type = BOOT_SWAP_TYPE_NONE;
David Vinczee2453472019-06-17 12:31:59 +0200266 state->image_num = 0;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700267 }
268
Christopher Collins2adef702019-05-22 14:37:31 -0700269 off = boot_copy_done_off(fap);
270 rc = flash_area_read_is_empty(fap, off, &state->copy_done,
271 sizeof state->copy_done);
272 if (rc < 0) {
273 return BOOT_EFLASH;
274 }
275 if (rc == 1) {
276 state->copy_done = BOOT_FLAG_UNSET;
277 } else {
278 state->copy_done = boot_flag_decode(state->copy_done);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800279 }
280
281 off = boot_image_ok_off(fap);
David Vincze2d736ad2019-02-18 11:50:22 +0100282 rc = flash_area_read_is_empty(fap, off, &state->image_ok,
283 sizeof state->image_ok);
Fabio Utzig178be542018-09-19 08:12:56 -0300284 if (rc < 0) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800285 return BOOT_EFLASH;
286 }
Fabio Utzig178be542018-09-19 08:12:56 -0300287 if (rc == 1) {
288 state->image_ok = BOOT_FLAG_UNSET;
289 } else {
290 state->image_ok = boot_flag_decode(state->image_ok);
291 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800292
293 return 0;
294}
295
296/**
297 * Reads the image trailer from the scratch area.
298 */
299int
Fabio Utzig2473ac02017-05-02 12:45:02 -0300300boot_read_swap_state_by_id(int flash_area_id, struct boot_swap_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800301{
302 const struct flash_area *fap;
303 int rc;
304
Fabio Utzigb0f04732019-07-31 09:49:19 -0300305 rc = flash_area_open(flash_area_id, &fap);
306 if (rc != 0) {
307 return BOOT_EFLASH;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300308 }
309
310 rc = boot_read_swap_state(fap, state);
Fabio Utzigacfba2e2017-05-22 11:06:29 -0400311 flash_area_close(fap);
312 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800313}
314
315int
Fabio Utzigb0f04732019-07-31 09:49:19 -0300316boot_read_swap_size(int image_index, uint32_t *swap_size)
Fabio Utzig46490722017-09-04 15:34:32 -0300317{
Hovland, Sigvart1d96f362018-09-25 13:23:42 +0200318 uint32_t magic[BOOT_MAGIC_ARR_SZ];
Fabio Utzig46490722017-09-04 15:34:32 -0300319 uint32_t off;
320 const struct flash_area *fap;
321 int rc;
322
323 /*
324 * In the middle a swap, tries to locate the saved swap size. Looks
David Vincze2d736ad2019-02-18 11:50:22 +0100325 * for a valid magic, first on the primary slot, then on scratch.
326 * Both "slots" can end up being temporary storage for a swap and it
327 * is assumed that if magic is valid then swap size is too, because
328 * magic is always written in the last step.
Fabio Utzig46490722017-09-04 15:34:32 -0300329 */
330
Fabio Utzigb0f04732019-07-31 09:49:19 -0300331 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index), &fap);
Fabio Utzig46490722017-09-04 15:34:32 -0300332 if (rc != 0) {
333 return BOOT_EFLASH;
334 }
335
336 off = boot_magic_off(fap);
337 rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ);
338 if (rc != 0) {
339 rc = BOOT_EFLASH;
340 goto out;
341 }
342
343 if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) != 0) {
344 /*
David Vincze2d736ad2019-02-18 11:50:22 +0100345 * If the primary slot's magic is not valid, try scratch...
Fabio Utzig46490722017-09-04 15:34:32 -0300346 */
347
348 flash_area_close(fap);
349
350 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap);
351 if (rc != 0) {
352 return BOOT_EFLASH;
353 }
354
355 off = boot_magic_off(fap);
356 rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ);
357 if (rc != 0) {
358 rc = BOOT_EFLASH;
359 goto out;
360 }
361
362 assert(memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0);
363 }
364
365 off = boot_swap_size_off(fap);
366 rc = flash_area_read(fap, off, swap_size, sizeof *swap_size);
367 if (rc != 0) {
368 rc = BOOT_EFLASH;
369 }
370
371out:
372 flash_area_close(fap);
373 return rc;
374}
375
Fabio Utzigba829042018-09-18 08:29:34 -0300376#ifdef MCUBOOT_ENC_IMAGES
377int
Fabio Utzigb0f04732019-07-31 09:49:19 -0300378boot_read_enc_key(int image_index, uint8_t slot, uint8_t *enckey)
Fabio Utzigba829042018-09-18 08:29:34 -0300379{
380 uint32_t magic[BOOT_MAGIC_SZ];
381 uint32_t off;
382 const struct flash_area *fap;
383 int rc;
384
Fabio Utzigb0f04732019-07-31 09:49:19 -0300385 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index), &fap);
Fabio Utzigba829042018-09-18 08:29:34 -0300386 if (rc != 0) {
387 return BOOT_EFLASH;
388 }
389
390 off = boot_magic_off(fap);
391 rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ);
392 if (rc != 0) {
393 rc = BOOT_EFLASH;
394 goto out;
395 }
396
397 if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) != 0) {
398 /*
David Vincze2d736ad2019-02-18 11:50:22 +0100399 * If the primary slot's magic is not valid, try scratch...
Fabio Utzigba829042018-09-18 08:29:34 -0300400 */
401
402 flash_area_close(fap);
403
404 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap);
405 if (rc != 0) {
406 return BOOT_EFLASH;
407 }
408
409 off = boot_magic_off(fap);
410 rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ);
411 if (rc != 0) {
412 rc = BOOT_EFLASH;
413 goto out;
414 }
415
416 assert(memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0);
417 }
418
419 off = boot_enc_key_off(fap, slot);
420 rc = flash_area_read(fap, off, enckey, BOOT_ENC_KEY_SIZE);
421 if (rc != 0) {
422 rc = BOOT_EFLASH;
423 }
424
425out:
426 flash_area_close(fap);
427 return rc;
428}
429#endif
Fabio Utzig46490722017-09-04 15:34:32 -0300430
431int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800432boot_write_magic(const struct flash_area *fap)
433{
434 uint32_t off;
435 int rc;
436
437 off = boot_magic_off(fap);
438
Christopher Collins2c88e692019-05-22 15:10:14 -0700439 BOOT_LOG_DBG("writing magic; fa_id=%d off=0x%x (0x%x)",
440 fap->fa_id, off, fap->fa_off + off);
Fabio Utzig24a273d2017-04-20 08:21:31 -0300441 rc = flash_area_write(fap, off, boot_img_magic, BOOT_MAGIC_SZ);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800442 if (rc != 0) {
443 return BOOT_EFLASH;
444 }
445
446 return 0;
447}
448
Fabio Utzig2473ac02017-05-02 12:45:02 -0300449static int
Christopher Collinsa1c12042019-05-23 14:00:28 -0700450boot_write_trailer_byte(const struct flash_area *fap, uint32_t off,
451 uint8_t val)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800452{
Fabio Utzig644b8d42017-04-20 07:56:05 -0300453 uint8_t buf[BOOT_MAX_ALIGN];
David Brown9d725462017-01-23 15:50:58 -0700454 uint8_t align;
Fabio Utzig39000012018-07-30 12:40:20 -0300455 uint8_t erased_val;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700456 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800457
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200458 align = flash_area_align(fap);
Fabio Utzig644b8d42017-04-20 07:56:05 -0300459 assert(align <= BOOT_MAX_ALIGN);
Fabio Utzig39000012018-07-30 12:40:20 -0300460 erased_val = flash_area_erased_val(fap);
461 memset(buf, erased_val, BOOT_MAX_ALIGN);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700462 buf[0] = val;
David Brown9d725462017-01-23 15:50:58 -0700463
464 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800465 if (rc != 0) {
466 return BOOT_EFLASH;
467 }
468
469 return 0;
470}
471
472int
Fabio Utzig2473ac02017-05-02 12:45:02 -0300473boot_write_copy_done(const struct flash_area *fap)
474{
Christopher Collinsa1c12042019-05-23 14:00:28 -0700475 uint32_t off;
476
477 off = boot_copy_done_off(fap);
478 BOOT_LOG_DBG("writing copy_done; fa_id=%d off=0x%x (0x%x)",
479 fap->fa_id, off, fap->fa_off + off);
480 return boot_write_trailer_byte(fap, off, BOOT_FLAG_SET);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300481}
482
483int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800484boot_write_image_ok(const struct flash_area *fap)
485{
Christopher Collinsa1c12042019-05-23 14:00:28 -0700486 uint32_t off;
487
488 off = boot_image_ok_off(fap);
489 BOOT_LOG_DBG("writing image_ok; fa_id=%d off=0x%x (0x%x)",
490 fap->fa_id, off, fap->fa_off + off);
491 return boot_write_trailer_byte(fap, off, BOOT_FLAG_SET);
492}
493
494/**
495 * Writes the specified value to the `swap-type` field of an image trailer.
496 * This value is persisted so that the boot loader knows what swap operation to
497 * resume in case of an unexpected reset.
498 */
499int
David Vinczee2453472019-06-17 12:31:59 +0200500boot_write_swap_info(const struct flash_area *fap, uint8_t swap_type,
501 uint8_t image_num)
Christopher Collinsa1c12042019-05-23 14:00:28 -0700502{
503 uint32_t off;
David Vinczee2453472019-06-17 12:31:59 +0200504 uint8_t swap_info;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700505
David Vinczee2453472019-06-17 12:31:59 +0200506 BOOT_SET_SWAP_INFO(swap_info, image_num, swap_type);
507 off = boot_swap_info_off(fap);
508 BOOT_LOG_DBG("writing swap_info; fa_id=%d off=0x%x (0x%x), swap_type=0x%x"
509 " image_num=0x%x",
510 fap->fa_id, off, fap->fa_off + off, swap_type, image_num);
511 return boot_write_trailer_byte(fap, off, swap_info);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800512}
513
514int
Fabio Utzig46490722017-09-04 15:34:32 -0300515boot_write_swap_size(const struct flash_area *fap, uint32_t swap_size)
516{
517 uint32_t off;
518 int rc;
519 uint8_t buf[BOOT_MAX_ALIGN];
520 uint8_t align;
Fabio Utzig39000012018-07-30 12:40:20 -0300521 uint8_t erased_val;
Fabio Utzig46490722017-09-04 15:34:32 -0300522
523 off = boot_swap_size_off(fap);
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200524 align = flash_area_align(fap);
Fabio Utzig46490722017-09-04 15:34:32 -0300525 assert(align <= BOOT_MAX_ALIGN);
526 if (align < sizeof swap_size) {
527 align = sizeof swap_size;
528 }
Fabio Utzig39000012018-07-30 12:40:20 -0300529 erased_val = flash_area_erased_val(fap);
530 memset(buf, erased_val, BOOT_MAX_ALIGN);
Fabio Utzig46490722017-09-04 15:34:32 -0300531 memcpy(buf, (uint8_t *)&swap_size, sizeof swap_size);
532
Christopher Collins2c88e692019-05-22 15:10:14 -0700533 BOOT_LOG_DBG("writing swap_size; fa_id=%d off=0x%x (0x%x)",
534 fap->fa_id, off, fap->fa_off + off);
535
Fabio Utzig46490722017-09-04 15:34:32 -0300536 rc = flash_area_write(fap, off, buf, align);
537 if (rc != 0) {
538 return BOOT_EFLASH;
539 }
540
541 return 0;
542}
543
Fabio Utzigba829042018-09-18 08:29:34 -0300544#ifdef MCUBOOT_ENC_IMAGES
545int
546boot_write_enc_key(const struct flash_area *fap, uint8_t slot, const uint8_t *enckey)
547{
548 uint32_t off;
549 int rc;
550
551 off = boot_enc_key_off(fap, slot);
552 rc = flash_area_write(fap, off, enckey, BOOT_ENC_KEY_SIZE);
553 if (rc != 0) {
554 return BOOT_EFLASH;
555 }
556
557 return 0;
558}
559#endif
560
Fabio Utzig46490722017-09-04 15:34:32 -0300561int
Fabio Utzigb0f04732019-07-31 09:49:19 -0300562boot_swap_type_multi(int image_index)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800563{
564 const struct boot_swap_table *table;
David Vincze2d736ad2019-02-18 11:50:22 +0100565 struct boot_swap_state primary_slot;
566 struct boot_swap_state secondary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800567 int rc;
Fabio Utzigcd5774b2017-11-29 10:18:26 -0200568 size_t i;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800569
Fabio Utzigb0f04732019-07-31 09:49:19 -0300570 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(image_index),
571 &primary_slot);
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300572 if (rc) {
573 return BOOT_SWAP_TYPE_PANIC;
574 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800575
Fabio Utzigb0f04732019-07-31 09:49:19 -0300576 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(image_index),
David Vincze2d736ad2019-02-18 11:50:22 +0100577 &secondary_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
582 for (i = 0; i < BOOT_SWAP_TABLES_COUNT; i++) {
583 table = boot_swap_tables + i;
584
Christopher Collinsa1c12042019-05-23 14:00:28 -0700585 if (boot_magic_compatible_check(table->magic_primary_slot,
586 primary_slot.magic) &&
587 boot_magic_compatible_check(table->magic_secondary_slot,
588 secondary_slot.magic) &&
David Vincze2d736ad2019-02-18 11:50:22 +0100589 (table->image_ok_primary_slot == BOOT_FLAG_ANY ||
590 table->image_ok_primary_slot == primary_slot.image_ok) &&
591 (table->image_ok_secondary_slot == BOOT_FLAG_ANY ||
592 table->image_ok_secondary_slot == secondary_slot.image_ok) &&
593 (table->copy_done_primary_slot == BOOT_FLAG_ANY ||
594 table->copy_done_primary_slot == primary_slot.copy_done)) {
Fabio Utzig34e393e2017-05-22 11:07:07 -0400595 BOOT_LOG_INF("Swap type: %s",
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300596 table->swap_type == BOOT_SWAP_TYPE_TEST ? "test" :
597 table->swap_type == BOOT_SWAP_TYPE_PERM ? "perm" :
598 table->swap_type == BOOT_SWAP_TYPE_REVERT ? "revert" :
599 "BUG; can't happen");
600 assert(table->swap_type == BOOT_SWAP_TYPE_TEST ||
601 table->swap_type == BOOT_SWAP_TYPE_PERM ||
602 table->swap_type == BOOT_SWAP_TYPE_REVERT);
603 return table->swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800604 }
605 }
606
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300607 BOOT_LOG_INF("Swap type: none");
Christopher Collins92ea77f2016-12-12 15:59:26 -0800608 return BOOT_SWAP_TYPE_NONE;
609}
610
Fabio Utzigb0f04732019-07-31 09:49:19 -0300611int
612boot_swap_type(void)
613{
614 return boot_swap_type_multi(0);
615}
616
617
Christopher Collins92ea77f2016-12-12 15:59:26 -0800618/**
David Vincze2d736ad2019-02-18 11:50:22 +0100619 * Marks the image in the secondary slot as pending. On the next reboot,
620 * the system will perform a one-time boot of the the secondary slot image.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800621 *
Christopher Collins7835c1e2016-12-21 10:10:51 -0800622 * @param permanent Whether the image should be used permanently or
623 * only tested once:
624 * 0=run image once, then confirm or revert.
625 * 1=run image forever.
626 *
Christopher Collins92ea77f2016-12-12 15:59:26 -0800627 * @return 0 on success; nonzero on failure.
628 */
629int
Christopher Collins7835c1e2016-12-21 10:10:51 -0800630boot_set_pending(int permanent)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800631{
632 const struct flash_area *fap;
David Vincze2d736ad2019-02-18 11:50:22 +0100633 struct boot_swap_state state_secondary_slot;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700634 uint8_t swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800635 int rc;
636
Fabio Utzigb0f04732019-07-31 09:49:19 -0300637 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(0),
David Vincze2d736ad2019-02-18 11:50:22 +0100638 &state_secondary_slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800639 if (rc != 0) {
640 return rc;
641 }
642
David Vincze2d736ad2019-02-18 11:50:22 +0100643 switch (state_secondary_slot.magic) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800644 case BOOT_MAGIC_GOOD:
645 /* Swap already scheduled. */
646 return 0;
647
648 case BOOT_MAGIC_UNSET:
Fabio Utzigb0f04732019-07-31 09:49:19 -0300649 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(0), &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800650 if (rc != 0) {
651 rc = BOOT_EFLASH;
652 } else {
653 rc = boot_write_magic(fap);
654 }
655
Christopher Collins7835c1e2016-12-21 10:10:51 -0800656 if (rc == 0 && permanent) {
657 rc = boot_write_image_ok(fap);
658 }
659
Christopher Collinsa1c12042019-05-23 14:00:28 -0700660 if (rc == 0) {
661 if (permanent) {
662 swap_type = BOOT_SWAP_TYPE_PERM;
663 } else {
664 swap_type = BOOT_SWAP_TYPE_TEST;
665 }
David Vinczee2453472019-06-17 12:31:59 +0200666 rc = boot_write_swap_info(fap, swap_type, 0);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700667 }
668
Christopher Collins92ea77f2016-12-12 15:59:26 -0800669 flash_area_close(fap);
670 return rc;
671
Christopher Collinsae01f152019-01-30 09:56:37 -0800672 case BOOT_MAGIC_BAD:
673 /* The image slot is corrupt. There is no way to recover, so erase the
674 * slot to allow future upgrades.
675 */
Fabio Utzigb0f04732019-07-31 09:49:19 -0300676 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(0), &fap);
Christopher Collinsae01f152019-01-30 09:56:37 -0800677 if (rc != 0) {
678 return BOOT_EFLASH;
679 }
680
681 flash_area_erase(fap, 0, fap->fa_size);
682 flash_area_close(fap);
683 return BOOT_EBADIMAGE;
684
Christopher Collins92ea77f2016-12-12 15:59:26 -0800685 default:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800686 assert(0);
Christopher Collinsae01f152019-01-30 09:56:37 -0800687 return BOOT_EBADIMAGE;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800688 }
689}
690
691/**
David Vincze2d736ad2019-02-18 11:50:22 +0100692 * Marks the image in the primary slot as confirmed. The system will continue
693 * booting into the image in the primary slot until told to boot from a
694 * different slot.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800695 *
696 * @return 0 on success; nonzero on failure.
697 */
698int
699boot_set_confirmed(void)
700{
701 const struct flash_area *fap;
David Vincze2d736ad2019-02-18 11:50:22 +0100702 struct boot_swap_state state_primary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800703 int rc;
704
Fabio Utzigb0f04732019-07-31 09:49:19 -0300705 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(0),
David Vincze2d736ad2019-02-18 11:50:22 +0100706 &state_primary_slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800707 if (rc != 0) {
708 return rc;
709 }
710
David Vincze2d736ad2019-02-18 11:50:22 +0100711 switch (state_primary_slot.magic) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800712 case BOOT_MAGIC_GOOD:
713 /* Confirm needed; proceed. */
714 break;
715
716 case BOOT_MAGIC_UNSET:
717 /* Already confirmed. */
718 return 0;
719
720 case BOOT_MAGIC_BAD:
721 /* Unexpected state. */
722 return BOOT_EBADVECT;
723 }
724
Fabio Utzigb0f04732019-07-31 09:49:19 -0300725 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(0), &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800726 if (rc) {
727 rc = BOOT_EFLASH;
728 goto done;
729 }
730
David Vincze2d736ad2019-02-18 11:50:22 +0100731 if (state_primary_slot.copy_done == BOOT_FLAG_UNSET) {
Fabio Utzig39000012018-07-30 12:40:20 -0300732 /* Swap never completed. This is unexpected. */
733 rc = BOOT_EBADVECT;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800734 goto done;
735 }
736
David Vincze2d736ad2019-02-18 11:50:22 +0100737 if (state_primary_slot.image_ok != BOOT_FLAG_UNSET) {
Fabio Utzig39000012018-07-30 12:40:20 -0300738 /* Already confirmed. */
739 goto done;
740 }
741
742 rc = boot_write_image_ok(fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800743
744done:
745 flash_area_close(fap);
746 return rc;
747}
David Vinczee32483f2019-06-13 10:46:24 +0200748
749#if (BOOT_IMAGE_NUMBER > 1)
750/**
751 * Check if the version of the image is not older than required.
752 *
753 * @param req Required minimal image version.
754 * @param ver Version of the image to be checked.
755 *
756 * @return 0 if the version is sufficient, nonzero otherwise.
757 */
758int
759boot_is_version_sufficient(struct image_version *req,
760 struct image_version *ver)
761{
762 if (ver->iv_major > req->iv_major) {
763 return 0;
764 }
765 if (ver->iv_major < req->iv_major) {
766 return BOOT_EBADVERSION;
767 }
768 /* The major version numbers are equal. */
769 if (ver->iv_minor > req->iv_minor) {
770 return 0;
771 }
772 if (ver->iv_minor < req->iv_minor) {
773 return BOOT_EBADVERSION;
774 }
775 /* The minor version numbers are equal. */
776 if (ver->iv_revision < req->iv_revision) {
777 return BOOT_EBADVERSION;
778 }
779
780 return 0;
781}
782#endif /* BOOT_IMAGE_NUMBER > 1 */