blob: 1b266362eb1b8aa737859376a12449aa3e3d3b17 [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
Fabio Utzig10ee6482019-08-01 12:04:52 -030042/* Currently only used by imgmgr */
Christopher Collins92ea77f2016-12-12 15:59:26 -080043int boot_current_slot;
44
Fabio Utzig24a273d2017-04-20 08:21:31 -030045const uint32_t boot_img_magic[] = {
Christopher Collins92ea77f2016-12-12 15:59:26 -080046 0xf395c277,
47 0x7fefd260,
48 0x0f505235,
49 0x8079b62c,
50};
51
Hovland, Sigvart1d96f362018-09-25 13:23:42 +020052#define BOOT_MAGIC_ARR_SZ \
53 (sizeof boot_img_magic / sizeof boot_img_magic[0])
54
Fabio Utzig24a273d2017-04-20 08:21:31 -030055const uint32_t BOOT_MAGIC_SZ = sizeof boot_img_magic;
Fabio Utziga0bc9b52017-06-28 09:19:55 -030056const uint32_t BOOT_MAX_ALIGN = MAX_FLASH_ALIGN;
Fabio Utzig24a273d2017-04-20 08:21:31 -030057
Christopher Collins92ea77f2016-12-12 15:59:26 -080058struct boot_swap_table {
David Vincze2d736ad2019-02-18 11:50:22 +010059 uint8_t magic_primary_slot;
60 uint8_t magic_secondary_slot;
61 uint8_t image_ok_primary_slot;
62 uint8_t image_ok_secondary_slot;
63 uint8_t copy_done_primary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -080064
Fabio Utzigb5b2f552017-06-30 10:03:47 -030065 uint8_t swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -080066};
67
68/**
69 * This set of tables maps image trailer contents to swap operation type.
70 * When searching for a match, these tables must be iterated sequentially.
Fabio Utzigb5b2f552017-06-30 10:03:47 -030071 *
David Vincze2d736ad2019-02-18 11:50:22 +010072 * NOTE: the table order is very important. The settings in the secondary
73 * slot always are priority to the primary slot and should be located
74 * earlier in the table.
Fabio Utzigb5b2f552017-06-30 10:03:47 -030075 *
76 * The table lists only states where there is action needs to be taken by
77 * the bootloader, as in starting/finishing a swap operation.
Christopher Collins92ea77f2016-12-12 15:59:26 -080078 */
79static const struct boot_swap_table boot_swap_tables[] = {
80 {
David Vincze2d736ad2019-02-18 11:50:22 +010081 .magic_primary_slot = BOOT_MAGIC_ANY,
82 .magic_secondary_slot = BOOT_MAGIC_GOOD,
83 .image_ok_primary_slot = BOOT_FLAG_ANY,
84 .image_ok_secondary_slot = BOOT_FLAG_UNSET,
85 .copy_done_primary_slot = BOOT_FLAG_ANY,
86 .swap_type = BOOT_SWAP_TYPE_TEST,
Christopher Collins92ea77f2016-12-12 15:59:26 -080087 },
Christopher Collins92ea77f2016-12-12 15:59:26 -080088 {
David Vincze2d736ad2019-02-18 11:50:22 +010089 .magic_primary_slot = BOOT_MAGIC_ANY,
90 .magic_secondary_slot = BOOT_MAGIC_GOOD,
91 .image_ok_primary_slot = BOOT_FLAG_ANY,
92 .image_ok_secondary_slot = BOOT_FLAG_SET,
93 .copy_done_primary_slot = BOOT_FLAG_ANY,
94 .swap_type = BOOT_SWAP_TYPE_PERM,
Christopher Collins92ea77f2016-12-12 15:59:26 -080095 },
Christopher Collins92ea77f2016-12-12 15:59:26 -080096 {
David Vincze2d736ad2019-02-18 11:50:22 +010097 .magic_primary_slot = BOOT_MAGIC_GOOD,
98 .magic_secondary_slot = BOOT_MAGIC_UNSET,
99 .image_ok_primary_slot = BOOT_FLAG_UNSET,
100 .image_ok_secondary_slot = BOOT_FLAG_ANY,
101 .copy_done_primary_slot = BOOT_FLAG_SET,
102 .swap_type = BOOT_SWAP_TYPE_REVERT,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800103 },
104};
105
106#define BOOT_SWAP_TABLES_COUNT \
107 (sizeof boot_swap_tables / sizeof boot_swap_tables[0])
108
Fabio Utzig39000012018-07-30 12:40:20 -0300109static int
Fabio Utzig178be542018-09-19 08:12:56 -0300110boot_magic_decode(const uint32_t *magic)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800111{
Fabio Utzig24a273d2017-04-20 08:21:31 -0300112 if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800113 return BOOT_MAGIC_GOOD;
114 }
Fabio Utzig178be542018-09-19 08:12:56 -0300115 return BOOT_MAGIC_BAD;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800116}
117
Fabio Utzig39000012018-07-30 12:40:20 -0300118static int
Fabio Utzig178be542018-09-19 08:12:56 -0300119boot_flag_decode(uint8_t flag)
Fabio Utzig39000012018-07-30 12:40:20 -0300120{
Fabio Utzig39000012018-07-30 12:40:20 -0300121 if (flag != BOOT_FLAG_SET) {
122 return BOOT_FLAG_BAD;
123 }
124 return BOOT_FLAG_SET;
125}
126
Christopher Collinsa1c12042019-05-23 14:00:28 -0700127/**
128 * Determines if a status source table is satisfied by the specified magic
129 * code.
130 *
131 * @param tbl_val A magic field from a status source table.
132 * @param val The magic value in a trailer, encoded as a
133 * BOOT_MAGIC_[...].
134 *
135 * @return 1 if the two values are compatible;
136 * 0 otherwise.
137 */
138int
139boot_magic_compatible_check(uint8_t tbl_val, uint8_t val)
140{
141 switch (tbl_val) {
142 case BOOT_MAGIC_ANY:
143 return 1;
144
145 case BOOT_MAGIC_NOTGOOD:
146 return val != BOOT_MAGIC_GOOD;
147
148 default:
149 return tbl_val == val;
150 }
151}
152
Christopher Collins92ea77f2016-12-12 15:59:26 -0800153uint32_t
Christopher Collins2adef702019-05-22 14:37:31 -0700154boot_trailer_sz(uint8_t min_write_sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800155{
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300156 return /* state for all sectors */
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300157 BOOT_STATUS_MAX_ENTRIES * BOOT_STATUS_STATE_COUNT * min_write_sz +
Fabio Utzigba829042018-09-18 08:29:34 -0300158#ifdef MCUBOOT_ENC_IMAGES
159 /* encryption keys */
160 BOOT_ENC_KEY_SIZE * 2 +
161#endif
Christopher Collins2adef702019-05-22 14:37:31 -0700162 /* swap_type + copy_done + image_ok + swap_size */
163 BOOT_MAX_ALIGN * 4 +
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300164 BOOT_MAGIC_SZ;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800165}
166
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400167int
Fabio Utzigb0f04732019-07-31 09:49:19 -0300168boot_status_entries(int image_index, const struct flash_area *fap)
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400169{
David Vinczeb75c12a2019-03-22 14:58:33 +0100170 if (fap->fa_id == FLASH_AREA_IMAGE_SCRATCH) {
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400171 return BOOT_STATUS_STATE_COUNT;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300172 } else if (fap->fa_id == FLASH_AREA_IMAGE_PRIMARY(image_index) ||
173 fap->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
David Vinczeb75c12a2019-03-22 14:58:33 +0100174 return BOOT_STATUS_STATE_COUNT * BOOT_STATUS_MAX_ENTRIES;
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400175 }
Fabio Utzig9d160092019-08-09 07:46:34 -0300176 return -1;
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400177}
178
Christopher Collins92ea77f2016-12-12 15:59:26 -0800179uint32_t
180boot_status_off(const struct flash_area *fap)
181{
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300182 uint32_t off_from_end;
183 uint8_t elem_sz;
184
185 elem_sz = flash_area_align(fap);
186
Christopher Collins2adef702019-05-22 14:37:31 -0700187 off_from_end = boot_trailer_sz(elem_sz);
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300188
189 assert(off_from_end <= fap->fa_size);
190 return fap->fa_size - off_from_end;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800191}
192
Fabio Utzig4e8113b2019-08-09 09:11:18 -0300193static inline uint32_t
194boot_magic_off(const struct flash_area *fap)
195{
196 return fap->fa_size - BOOT_MAGIC_SZ;
197}
198
199static inline uint32_t
200boot_image_ok_off(const struct flash_area *fap)
201{
202 return boot_magic_off(fap) - BOOT_MAX_ALIGN;
203}
204
205static inline uint32_t
206boot_copy_done_off(const struct flash_area *fap)
207{
208 return boot_image_ok_off(fap) - BOOT_MAX_ALIGN;
209}
210
Christopher Collinsa1c12042019-05-23 14:00:28 -0700211uint32_t
David Vinczee2453472019-06-17 12:31:59 +0200212boot_swap_info_off(const struct flash_area *fap)
Christopher Collinsa1c12042019-05-23 14:00:28 -0700213{
Fabio Utzig4e8113b2019-08-09 09:11:18 -0300214 return boot_copy_done_off(fap) - BOOT_MAX_ALIGN;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700215}
216
Fabio Utzig4e8113b2019-08-09 09:11:18 -0300217static inline uint32_t
Fabio Utzig46490722017-09-04 15:34:32 -0300218boot_swap_size_off(const struct flash_area *fap)
219{
Fabio Utzig4e8113b2019-08-09 09:11:18 -0300220 return boot_swap_info_off(fap) - BOOT_MAX_ALIGN;
Fabio Utzig46490722017-09-04 15:34:32 -0300221}
222
Fabio Utzigba829042018-09-18 08:29:34 -0300223#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig4e8113b2019-08-09 09:11:18 -0300224static inline uint32_t
Fabio Utzigba829042018-09-18 08:29:34 -0300225boot_enc_key_off(const struct flash_area *fap, uint8_t slot)
226{
Fabio Utzig4e8113b2019-08-09 09:11:18 -0300227 return boot_swap_size_off(fap) - ((slot + 1) * BOOT_ENC_KEY_SIZE);
Fabio Utzigba829042018-09-18 08:29:34 -0300228}
229#endif
230
Christopher Collins92ea77f2016-12-12 15:59:26 -0800231int
232boot_read_swap_state(const struct flash_area *fap,
233 struct boot_swap_state *state)
234{
Hovland, Sigvart1d96f362018-09-25 13:23:42 +0200235 uint32_t magic[BOOT_MAGIC_ARR_SZ];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800236 uint32_t off;
David Vinczee2453472019-06-17 12:31:59 +0200237 uint8_t swap_info;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800238 int rc;
239
240 off = boot_magic_off(fap);
Fabio Utzig178be542018-09-19 08:12:56 -0300241 rc = flash_area_read_is_empty(fap, off, magic, BOOT_MAGIC_SZ);
242 if (rc < 0) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800243 return BOOT_EFLASH;
244 }
Fabio Utzig178be542018-09-19 08:12:56 -0300245 if (rc == 1) {
246 state->magic = BOOT_MAGIC_UNSET;
247 } else {
248 state->magic = boot_magic_decode(magic);
249 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800250
David Vinczee2453472019-06-17 12:31:59 +0200251 off = boot_swap_info_off(fap);
252 rc = flash_area_read_is_empty(fap, off, &swap_info, sizeof swap_info);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700253 if (rc < 0) {
254 return BOOT_EFLASH;
255 }
David Vinczee2453472019-06-17 12:31:59 +0200256
257 /* Extract the swap type and image number */
258 state->swap_type = BOOT_GET_SWAP_TYPE(swap_info);
259 state->image_num = BOOT_GET_IMAGE_NUM(swap_info);
260
Christopher Collinsa1c12042019-05-23 14:00:28 -0700261 if (rc == 1 || state->swap_type > BOOT_SWAP_TYPE_REVERT) {
262 state->swap_type = BOOT_SWAP_TYPE_NONE;
David Vinczee2453472019-06-17 12:31:59 +0200263 state->image_num = 0;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700264 }
265
Christopher Collins2adef702019-05-22 14:37:31 -0700266 off = boot_copy_done_off(fap);
267 rc = flash_area_read_is_empty(fap, off, &state->copy_done,
268 sizeof state->copy_done);
269 if (rc < 0) {
270 return BOOT_EFLASH;
271 }
272 if (rc == 1) {
273 state->copy_done = BOOT_FLAG_UNSET;
274 } else {
275 state->copy_done = boot_flag_decode(state->copy_done);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800276 }
277
278 off = boot_image_ok_off(fap);
David Vincze2d736ad2019-02-18 11:50:22 +0100279 rc = flash_area_read_is_empty(fap, off, &state->image_ok,
280 sizeof state->image_ok);
Fabio Utzig178be542018-09-19 08:12:56 -0300281 if (rc < 0) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800282 return BOOT_EFLASH;
283 }
Fabio Utzig178be542018-09-19 08:12:56 -0300284 if (rc == 1) {
285 state->image_ok = BOOT_FLAG_UNSET;
286 } else {
287 state->image_ok = boot_flag_decode(state->image_ok);
288 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800289
290 return 0;
291}
292
293/**
294 * Reads the image trailer from the scratch area.
295 */
296int
Fabio Utzig2473ac02017-05-02 12:45:02 -0300297boot_read_swap_state_by_id(int flash_area_id, struct boot_swap_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800298{
299 const struct flash_area *fap;
300 int rc;
301
Fabio Utzigb0f04732019-07-31 09:49:19 -0300302 rc = flash_area_open(flash_area_id, &fap);
303 if (rc != 0) {
304 return BOOT_EFLASH;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300305 }
306
307 rc = boot_read_swap_state(fap, state);
Fabio Utzigacfba2e2017-05-22 11:06:29 -0400308 flash_area_close(fap);
309 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800310}
311
Fabio Utzigdf0cc502019-08-09 09:10:41 -0300312/**
313 * This functions tries to locate the status area after an aborted swap,
314 * by looking for the magic in the possible locations.
315 *
316 * If the magic is sucessfully found, a flash_area * is returned and it
317 * is the responsibility of the called to close it.
318 *
319 * @returns 0 on success, -1 on errors
320 */
321static int
322boot_find_status(int image_index, const struct flash_area **fap)
323{
324 uint32_t magic[BOOT_MAGIC_ARR_SZ];
325 uint32_t off;
326 uint8_t areas[2] = {
327 FLASH_AREA_IMAGE_PRIMARY(image_index),
328 FLASH_AREA_IMAGE_SCRATCH,
329 };
330 unsigned int i;
331 int rc;
332
333 /*
334 * In the middle a swap, tries to locate the area that is currently
335 * storing a valid magic, first on the primary slot, then on scratch.
336 * Both "slots" can end up being temporary storage for a swap and it
337 * is assumed that if magic is valid then other metadata is too,
338 * because magic is always written in the last step.
339 */
340
341 for (i = 0; i < sizeof(areas) / sizeof(areas[0]); i++) {
342 rc = flash_area_open(areas[i], fap);
343 if (rc != 0) {
344 return rc;
345 }
346
347 off = boot_magic_off(*fap);
348 rc = flash_area_read(*fap, off, magic, BOOT_MAGIC_SZ);
349 if (rc != 0) {
350 flash_area_close(*fap);
351 return rc;
352 }
353
354 if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0) {
355 return 0;
356 }
357
358 flash_area_close(*fap);
359 }
360
361 /* If we got here, no magic was found */
362 return -1;
363}
364
Christopher Collins92ea77f2016-12-12 15:59:26 -0800365int
Fabio Utzigb0f04732019-07-31 09:49:19 -0300366boot_read_swap_size(int image_index, uint32_t *swap_size)
Fabio Utzig46490722017-09-04 15:34:32 -0300367{
Fabio Utzig46490722017-09-04 15:34:32 -0300368 uint32_t off;
369 const struct flash_area *fap;
370 int rc;
371
Fabio Utzigdf0cc502019-08-09 09:10:41 -0300372 rc = boot_find_status(image_index, &fap);
373 if (rc == 0) {
374 off = boot_swap_size_off(fap);
375 rc = flash_area_read(fap, off, swap_size, sizeof *swap_size);
Fabio Utzig46490722017-09-04 15:34:32 -0300376 flash_area_close(fap);
Fabio Utzig46490722017-09-04 15:34:32 -0300377 }
378
Fabio Utzig46490722017-09-04 15:34:32 -0300379 return rc;
380}
381
Fabio Utzigdf0cc502019-08-09 09:10:41 -0300382
Fabio Utzigba829042018-09-18 08:29:34 -0300383#ifdef MCUBOOT_ENC_IMAGES
384int
Fabio Utzigb0f04732019-07-31 09:49:19 -0300385boot_read_enc_key(int image_index, uint8_t slot, uint8_t *enckey)
Fabio Utzigba829042018-09-18 08:29:34 -0300386{
Fabio Utzigba829042018-09-18 08:29:34 -0300387 uint32_t off;
388 const struct flash_area *fap;
389 int rc;
390
Fabio Utzigdf0cc502019-08-09 09:10:41 -0300391 rc = boot_find_status(image_index, &fap);
392 if (rc == 0) {
393 off = boot_enc_key_off(fap, slot);
394 rc = flash_area_read(fap, off, enckey, BOOT_ENC_KEY_SIZE);
Fabio Utzigba829042018-09-18 08:29:34 -0300395 flash_area_close(fap);
Fabio Utzigba829042018-09-18 08:29:34 -0300396 }
397
Fabio Utzigba829042018-09-18 08:29:34 -0300398 return rc;
399}
400#endif
Fabio Utzig46490722017-09-04 15:34:32 -0300401
402int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800403boot_write_magic(const struct flash_area *fap)
404{
405 uint32_t off;
406 int rc;
407
408 off = boot_magic_off(fap);
409
Christopher Collins2c88e692019-05-22 15:10:14 -0700410 BOOT_LOG_DBG("writing magic; fa_id=%d off=0x%x (0x%x)",
411 fap->fa_id, off, fap->fa_off + off);
Fabio Utzig24a273d2017-04-20 08:21:31 -0300412 rc = flash_area_write(fap, off, boot_img_magic, BOOT_MAGIC_SZ);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800413 if (rc != 0) {
414 return BOOT_EFLASH;
415 }
416
417 return 0;
418}
419
Fabio Utzig1a1ec172019-08-09 10:21:43 -0300420/**
421 * Write trailer data; status bytes, swap_size, etc
422 *
423 * @returns 0 on success, != 0 on error.
424 */
Fabio Utzig2473ac02017-05-02 12:45:02 -0300425static int
Fabio Utzig1a1ec172019-08-09 10:21:43 -0300426boot_write_trailer(const struct flash_area *fap, uint32_t off,
427 const uint8_t *inbuf, uint8_t inlen)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800428{
Fabio Utzig644b8d42017-04-20 07:56:05 -0300429 uint8_t buf[BOOT_MAX_ALIGN];
David Brown9d725462017-01-23 15:50:58 -0700430 uint8_t align;
Fabio Utzig39000012018-07-30 12:40:20 -0300431 uint8_t erased_val;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700432 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800433
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200434 align = flash_area_align(fap);
Fabio Utzig1a1ec172019-08-09 10:21:43 -0300435 if (inlen > BOOT_MAX_ALIGN || align > BOOT_MAX_ALIGN) {
436 return -1;
437 }
Fabio Utzig39000012018-07-30 12:40:20 -0300438 erased_val = flash_area_erased_val(fap);
Fabio Utzig1a1ec172019-08-09 10:21:43 -0300439 if (align < inlen) {
440 align = inlen;
441 }
442 memcpy(buf, inbuf, inlen);
443 memset(&buf[inlen], erased_val, align - inlen);
David Brown9d725462017-01-23 15:50:58 -0700444
445 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800446 if (rc != 0) {
447 return BOOT_EFLASH;
448 }
449
450 return 0;
451}
452
Fabio Utzig1a1ec172019-08-09 10:21:43 -0300453static int
454boot_write_trailer_flag(const struct flash_area *fap, uint32_t off,
455 uint8_t flag_val)
456{
457 const uint8_t buf[1] = { flag_val };
458 return boot_write_trailer(fap, off, buf, 1);
459}
460
Christopher Collins92ea77f2016-12-12 15:59:26 -0800461int
Fabio Utzig2473ac02017-05-02 12:45:02 -0300462boot_write_copy_done(const struct flash_area *fap)
463{
Christopher Collinsa1c12042019-05-23 14:00:28 -0700464 uint32_t off;
465
466 off = boot_copy_done_off(fap);
467 BOOT_LOG_DBG("writing copy_done; fa_id=%d off=0x%x (0x%x)",
468 fap->fa_id, off, fap->fa_off + off);
Fabio Utzig1a1ec172019-08-09 10:21:43 -0300469 return boot_write_trailer_flag(fap, off, BOOT_FLAG_SET);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300470}
471
472int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800473boot_write_image_ok(const struct flash_area *fap)
474{
Christopher Collinsa1c12042019-05-23 14:00:28 -0700475 uint32_t off;
476
477 off = boot_image_ok_off(fap);
478 BOOT_LOG_DBG("writing image_ok; fa_id=%d off=0x%x (0x%x)",
479 fap->fa_id, off, fap->fa_off + off);
Fabio Utzig1a1ec172019-08-09 10:21:43 -0300480 return boot_write_trailer_flag(fap, off, BOOT_FLAG_SET);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700481}
482
483/**
484 * Writes the specified value to the `swap-type` field of an image trailer.
485 * This value is persisted so that the boot loader knows what swap operation to
486 * resume in case of an unexpected reset.
487 */
488int
David Vinczee2453472019-06-17 12:31:59 +0200489boot_write_swap_info(const struct flash_area *fap, uint8_t swap_type,
490 uint8_t image_num)
Christopher Collinsa1c12042019-05-23 14:00:28 -0700491{
492 uint32_t off;
David Vinczee2453472019-06-17 12:31:59 +0200493 uint8_t swap_info;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700494
David Vinczee2453472019-06-17 12:31:59 +0200495 BOOT_SET_SWAP_INFO(swap_info, image_num, swap_type);
496 off = boot_swap_info_off(fap);
497 BOOT_LOG_DBG("writing swap_info; fa_id=%d off=0x%x (0x%x), swap_type=0x%x"
498 " image_num=0x%x",
499 fap->fa_id, off, fap->fa_off + off, swap_type, image_num);
Fabio Utzig1a1ec172019-08-09 10:21:43 -0300500 return boot_write_trailer(fap, off, (const uint8_t *) &swap_info, 1);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800501}
502
503int
Fabio Utzig46490722017-09-04 15:34:32 -0300504boot_write_swap_size(const struct flash_area *fap, uint32_t swap_size)
505{
506 uint32_t off;
Fabio Utzig46490722017-09-04 15:34:32 -0300507
508 off = boot_swap_size_off(fap);
Christopher Collins2c88e692019-05-22 15:10:14 -0700509 BOOT_LOG_DBG("writing swap_size; fa_id=%d off=0x%x (0x%x)",
510 fap->fa_id, off, fap->fa_off + off);
Fabio Utzig1a1ec172019-08-09 10:21:43 -0300511 return boot_write_trailer(fap, off, (const uint8_t *) &swap_size, 4);
Fabio Utzig46490722017-09-04 15:34:32 -0300512}
513
Fabio Utzigba829042018-09-18 08:29:34 -0300514#ifdef MCUBOOT_ENC_IMAGES
515int
516boot_write_enc_key(const struct flash_area *fap, uint8_t slot, const uint8_t *enckey)
517{
518 uint32_t off;
519 int rc;
520
521 off = boot_enc_key_off(fap, slot);
522 rc = flash_area_write(fap, off, enckey, BOOT_ENC_KEY_SIZE);
523 if (rc != 0) {
524 return BOOT_EFLASH;
525 }
526
527 return 0;
528}
529#endif
530
Fabio Utzig46490722017-09-04 15:34:32 -0300531int
Fabio Utzigb0f04732019-07-31 09:49:19 -0300532boot_swap_type_multi(int image_index)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800533{
534 const struct boot_swap_table *table;
David Vincze2d736ad2019-02-18 11:50:22 +0100535 struct boot_swap_state primary_slot;
536 struct boot_swap_state secondary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800537 int rc;
Fabio Utzigcd5774b2017-11-29 10:18:26 -0200538 size_t i;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800539
Fabio Utzigb0f04732019-07-31 09:49:19 -0300540 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(image_index),
541 &primary_slot);
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300542 if (rc) {
543 return BOOT_SWAP_TYPE_PANIC;
544 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800545
Fabio Utzigb0f04732019-07-31 09:49:19 -0300546 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(image_index),
David Vincze2d736ad2019-02-18 11:50:22 +0100547 &secondary_slot);
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300548 if (rc) {
549 return BOOT_SWAP_TYPE_PANIC;
550 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800551
552 for (i = 0; i < BOOT_SWAP_TABLES_COUNT; i++) {
553 table = boot_swap_tables + i;
554
Christopher Collinsa1c12042019-05-23 14:00:28 -0700555 if (boot_magic_compatible_check(table->magic_primary_slot,
556 primary_slot.magic) &&
557 boot_magic_compatible_check(table->magic_secondary_slot,
558 secondary_slot.magic) &&
David Vincze2d736ad2019-02-18 11:50:22 +0100559 (table->image_ok_primary_slot == BOOT_FLAG_ANY ||
560 table->image_ok_primary_slot == primary_slot.image_ok) &&
561 (table->image_ok_secondary_slot == BOOT_FLAG_ANY ||
562 table->image_ok_secondary_slot == secondary_slot.image_ok) &&
563 (table->copy_done_primary_slot == BOOT_FLAG_ANY ||
564 table->copy_done_primary_slot == primary_slot.copy_done)) {
Fabio Utzig34e393e2017-05-22 11:07:07 -0400565 BOOT_LOG_INF("Swap type: %s",
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300566 table->swap_type == BOOT_SWAP_TYPE_TEST ? "test" :
567 table->swap_type == BOOT_SWAP_TYPE_PERM ? "perm" :
568 table->swap_type == BOOT_SWAP_TYPE_REVERT ? "revert" :
569 "BUG; can't happen");
Fabio Utzigf0dbd422019-08-09 10:22:05 -0300570 if (table->swap_type != BOOT_SWAP_TYPE_TEST &&
571 table->swap_type != BOOT_SWAP_TYPE_PERM &&
572 table->swap_type != BOOT_SWAP_TYPE_REVERT) {
573 return BOOT_SWAP_TYPE_PANIC;
574 }
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300575 return table->swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800576 }
577 }
578
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300579 BOOT_LOG_INF("Swap type: none");
Christopher Collins92ea77f2016-12-12 15:59:26 -0800580 return BOOT_SWAP_TYPE_NONE;
581}
582
Fabio Utzig75e9a592019-08-27 09:10:36 -0300583/*
584 * This function is not used by the bootloader itself, but its required API
585 * by external tooling like mcumgr.
586 */
Fabio Utzigb0f04732019-07-31 09:49:19 -0300587int
588boot_swap_type(void)
589{
590 return boot_swap_type_multi(0);
591}
592
Christopher Collins92ea77f2016-12-12 15:59:26 -0800593/**
David Vincze2d736ad2019-02-18 11:50:22 +0100594 * Marks the image in the secondary slot as pending. On the next reboot,
595 * the system will perform a one-time boot of the the secondary slot image.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800596 *
Christopher Collins7835c1e2016-12-21 10:10:51 -0800597 * @param permanent Whether the image should be used permanently or
598 * only tested once:
599 * 0=run image once, then confirm or revert.
600 * 1=run image forever.
601 *
Christopher Collins92ea77f2016-12-12 15:59:26 -0800602 * @return 0 on success; nonzero on failure.
603 */
604int
Christopher Collins7835c1e2016-12-21 10:10:51 -0800605boot_set_pending(int permanent)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800606{
607 const struct flash_area *fap;
David Vincze2d736ad2019-02-18 11:50:22 +0100608 struct boot_swap_state state_secondary_slot;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700609 uint8_t swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800610 int rc;
611
Fabio Utzigb0f04732019-07-31 09:49:19 -0300612 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(0),
David Vincze2d736ad2019-02-18 11:50:22 +0100613 &state_secondary_slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800614 if (rc != 0) {
615 return rc;
616 }
617
David Vincze2d736ad2019-02-18 11:50:22 +0100618 switch (state_secondary_slot.magic) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800619 case BOOT_MAGIC_GOOD:
620 /* Swap already scheduled. */
621 return 0;
622
623 case BOOT_MAGIC_UNSET:
Fabio Utzigb0f04732019-07-31 09:49:19 -0300624 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(0), &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800625 if (rc != 0) {
626 rc = BOOT_EFLASH;
627 } else {
628 rc = boot_write_magic(fap);
629 }
630
Christopher Collins7835c1e2016-12-21 10:10:51 -0800631 if (rc == 0 && permanent) {
632 rc = boot_write_image_ok(fap);
633 }
634
Christopher Collinsa1c12042019-05-23 14:00:28 -0700635 if (rc == 0) {
636 if (permanent) {
637 swap_type = BOOT_SWAP_TYPE_PERM;
638 } else {
639 swap_type = BOOT_SWAP_TYPE_TEST;
640 }
David Vinczee2453472019-06-17 12:31:59 +0200641 rc = boot_write_swap_info(fap, swap_type, 0);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700642 }
643
Christopher Collins92ea77f2016-12-12 15:59:26 -0800644 flash_area_close(fap);
645 return rc;
646
Christopher Collinsae01f152019-01-30 09:56:37 -0800647 case BOOT_MAGIC_BAD:
648 /* The image slot is corrupt. There is no way to recover, so erase the
649 * slot to allow future upgrades.
650 */
Fabio Utzigb0f04732019-07-31 09:49:19 -0300651 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(0), &fap);
Christopher Collinsae01f152019-01-30 09:56:37 -0800652 if (rc != 0) {
653 return BOOT_EFLASH;
654 }
655
656 flash_area_erase(fap, 0, fap->fa_size);
657 flash_area_close(fap);
658 return BOOT_EBADIMAGE;
659
Christopher Collins92ea77f2016-12-12 15:59:26 -0800660 default:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800661 assert(0);
Christopher Collinsae01f152019-01-30 09:56:37 -0800662 return BOOT_EBADIMAGE;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800663 }
664}
665
666/**
David Vincze2d736ad2019-02-18 11:50:22 +0100667 * Marks the image in the primary slot as confirmed. The system will continue
668 * booting into the image in the primary slot until told to boot from a
669 * different slot.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800670 *
671 * @return 0 on success; nonzero on failure.
672 */
673int
674boot_set_confirmed(void)
675{
676 const struct flash_area *fap;
David Vincze2d736ad2019-02-18 11:50:22 +0100677 struct boot_swap_state state_primary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800678 int rc;
679
Fabio Utzigb0f04732019-07-31 09:49:19 -0300680 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(0),
David Vincze2d736ad2019-02-18 11:50:22 +0100681 &state_primary_slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800682 if (rc != 0) {
683 return rc;
684 }
685
David Vincze2d736ad2019-02-18 11:50:22 +0100686 switch (state_primary_slot.magic) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800687 case BOOT_MAGIC_GOOD:
688 /* Confirm needed; proceed. */
689 break;
690
691 case BOOT_MAGIC_UNSET:
692 /* Already confirmed. */
693 return 0;
694
695 case BOOT_MAGIC_BAD:
696 /* Unexpected state. */
697 return BOOT_EBADVECT;
698 }
699
Fabio Utzigb0f04732019-07-31 09:49:19 -0300700 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(0), &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800701 if (rc) {
702 rc = BOOT_EFLASH;
703 goto done;
704 }
705
David Vincze2d736ad2019-02-18 11:50:22 +0100706 if (state_primary_slot.copy_done == BOOT_FLAG_UNSET) {
Fabio Utzig39000012018-07-30 12:40:20 -0300707 /* Swap never completed. This is unexpected. */
708 rc = BOOT_EBADVECT;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800709 goto done;
710 }
711
David Vincze2d736ad2019-02-18 11:50:22 +0100712 if (state_primary_slot.image_ok != BOOT_FLAG_UNSET) {
Fabio Utzig39000012018-07-30 12:40:20 -0300713 /* Already confirmed. */
714 goto done;
715 }
716
717 rc = boot_write_image_ok(fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800718
719done:
720 flash_area_close(fap);
721 return rc;
722}
David Vinczee32483f2019-06-13 10:46:24 +0200723
724#if (BOOT_IMAGE_NUMBER > 1)
725/**
726 * Check if the version of the image is not older than required.
727 *
728 * @param req Required minimal image version.
729 * @param ver Version of the image to be checked.
730 *
731 * @return 0 if the version is sufficient, nonzero otherwise.
732 */
733int
734boot_is_version_sufficient(struct image_version *req,
735 struct image_version *ver)
736{
737 if (ver->iv_major > req->iv_major) {
738 return 0;
739 }
740 if (ver->iv_major < req->iv_major) {
741 return BOOT_EBADVERSION;
742 }
743 /* The major version numbers are equal. */
744 if (ver->iv_minor > req->iv_minor) {
745 return 0;
746 }
747 if (ver->iv_minor < req->iv_minor) {
748 return BOOT_EBADVERSION;
749 }
750 /* The minor version numbers are equal. */
751 if (ver->iv_revision < req->iv_revision) {
752 return BOOT_EBADVERSION;
753 }
754
755 return 0;
756}
757#endif /* BOOT_IMAGE_NUMBER > 1 */