blob: 514d6698ef81ed0c85c9ffe2336f5d611332fb5d [file] [log] [blame]
Christopher Collins92ea77f2016-12-12 15:59:26 -08001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20#include <assert.h>
21#include <string.h>
22#include <inttypes.h>
Fabio Utziga0bc9b52017-06-28 09:19:55 -030023#include <stddef.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080024
Christopher Collins92ea77f2016-12-12 15:59:26 -080025#include "sysflash/sysflash.h"
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020026#include "flash_map_backend/flash_map_backend.h"
27
Christopher Collins92ea77f2016-12-12 15:59:26 -080028#include "bootutil/image.h"
29#include "bootutil/bootutil.h"
30#include "bootutil_priv.h"
Fabio Utzig7ebb7c22017-04-26 10:59:31 -030031#include "bootutil/bootutil_log.h"
Fabio Utzigba829042018-09-18 08:29:34 -030032#ifdef MCUBOOT_ENC_IMAGES
33#include "bootutil/enc_key.h"
34#endif
Fabio Utzig7ebb7c22017-04-26 10:59:31 -030035
Emanuele Di Santo9f1933d2018-11-20 10:59:59 +010036MCUBOOT_LOG_MODULE_DECLARE(mcuboot);
37
Christopher Collins92ea77f2016-12-12 15:59:26 -080038int boot_current_slot;
39
Fabio Utzig24a273d2017-04-20 08:21:31 -030040const uint32_t boot_img_magic[] = {
Christopher Collins92ea77f2016-12-12 15:59:26 -080041 0xf395c277,
42 0x7fefd260,
43 0x0f505235,
44 0x8079b62c,
45};
46
Hovland, Sigvart1d96f362018-09-25 13:23:42 +020047#define BOOT_MAGIC_ARR_SZ \
48 (sizeof boot_img_magic / sizeof boot_img_magic[0])
49
Fabio Utzig24a273d2017-04-20 08:21:31 -030050const uint32_t BOOT_MAGIC_SZ = sizeof boot_img_magic;
Fabio Utziga0bc9b52017-06-28 09:19:55 -030051const uint32_t BOOT_MAX_ALIGN = MAX_FLASH_ALIGN;
Fabio Utzig24a273d2017-04-20 08:21:31 -030052
Christopher Collins92ea77f2016-12-12 15:59:26 -080053struct boot_swap_table {
David Vincze2d736ad2019-02-18 11:50:22 +010054 uint8_t magic_primary_slot;
55 uint8_t magic_secondary_slot;
56 uint8_t image_ok_primary_slot;
57 uint8_t image_ok_secondary_slot;
58 uint8_t copy_done_primary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -080059
Fabio Utzigb5b2f552017-06-30 10:03:47 -030060 uint8_t swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -080061};
62
63/**
64 * This set of tables maps image trailer contents to swap operation type.
65 * When searching for a match, these tables must be iterated sequentially.
Fabio Utzigb5b2f552017-06-30 10:03:47 -030066 *
David Vincze2d736ad2019-02-18 11:50:22 +010067 * NOTE: the table order is very important. The settings in the secondary
68 * slot always are priority to the primary slot and should be located
69 * earlier in the table.
Fabio Utzigb5b2f552017-06-30 10:03:47 -030070 *
71 * The table lists only states where there is action needs to be taken by
72 * the bootloader, as in starting/finishing a swap operation.
Christopher Collins92ea77f2016-12-12 15:59:26 -080073 */
74static const struct boot_swap_table boot_swap_tables[] = {
75 {
David Vincze2d736ad2019-02-18 11:50:22 +010076 .magic_primary_slot = BOOT_MAGIC_ANY,
77 .magic_secondary_slot = BOOT_MAGIC_GOOD,
78 .image_ok_primary_slot = BOOT_FLAG_ANY,
79 .image_ok_secondary_slot = BOOT_FLAG_UNSET,
80 .copy_done_primary_slot = BOOT_FLAG_ANY,
81 .swap_type = BOOT_SWAP_TYPE_TEST,
Christopher Collins92ea77f2016-12-12 15:59:26 -080082 },
Christopher Collins92ea77f2016-12-12 15:59:26 -080083 {
David Vincze2d736ad2019-02-18 11:50:22 +010084 .magic_primary_slot = BOOT_MAGIC_ANY,
85 .magic_secondary_slot = BOOT_MAGIC_GOOD,
86 .image_ok_primary_slot = BOOT_FLAG_ANY,
87 .image_ok_secondary_slot = BOOT_FLAG_SET,
88 .copy_done_primary_slot = BOOT_FLAG_ANY,
89 .swap_type = BOOT_SWAP_TYPE_PERM,
Christopher Collins92ea77f2016-12-12 15:59:26 -080090 },
Christopher Collins92ea77f2016-12-12 15:59:26 -080091 {
David Vincze2d736ad2019-02-18 11:50:22 +010092 .magic_primary_slot = BOOT_MAGIC_GOOD,
93 .magic_secondary_slot = BOOT_MAGIC_UNSET,
94 .image_ok_primary_slot = BOOT_FLAG_UNSET,
95 .image_ok_secondary_slot = BOOT_FLAG_ANY,
96 .copy_done_primary_slot = BOOT_FLAG_SET,
97 .swap_type = BOOT_SWAP_TYPE_REVERT,
Christopher Collins92ea77f2016-12-12 15:59:26 -080098 },
99};
100
101#define BOOT_SWAP_TABLES_COUNT \
102 (sizeof boot_swap_tables / sizeof boot_swap_tables[0])
103
Fabio Utzig39000012018-07-30 12:40:20 -0300104static int
Fabio Utzig178be542018-09-19 08:12:56 -0300105boot_magic_decode(const uint32_t *magic)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800106{
Fabio Utzig24a273d2017-04-20 08:21:31 -0300107 if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800108 return BOOT_MAGIC_GOOD;
109 }
Fabio Utzig178be542018-09-19 08:12:56 -0300110 return BOOT_MAGIC_BAD;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800111}
112
Fabio Utzig39000012018-07-30 12:40:20 -0300113static int
Fabio Utzig178be542018-09-19 08:12:56 -0300114boot_flag_decode(uint8_t flag)
Fabio Utzig39000012018-07-30 12:40:20 -0300115{
Fabio Utzig39000012018-07-30 12:40:20 -0300116 if (flag != BOOT_FLAG_SET) {
117 return BOOT_FLAG_BAD;
118 }
119 return BOOT_FLAG_SET;
120}
121
Christopher Collins92ea77f2016-12-12 15:59:26 -0800122uint32_t
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300123boot_slots_trailer_sz(uint8_t min_write_sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800124{
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300125 return /* state for all sectors */
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300126 BOOT_STATUS_MAX_ENTRIES * BOOT_STATUS_STATE_COUNT * min_write_sz +
Fabio Utzigba829042018-09-18 08:29:34 -0300127#ifdef MCUBOOT_ENC_IMAGES
128 /* encryption keys */
129 BOOT_ENC_KEY_SIZE * 2 +
130#endif
131 /* copy_done + image_ok + swap_size */
132 BOOT_MAX_ALIGN * 3 +
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300133 BOOT_MAGIC_SZ;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800134}
135
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300136static uint32_t
137boot_scratch_trailer_sz(uint8_t min_write_sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800138{
Fabio Utzigba829042018-09-18 08:29:34 -0300139 /* state for one sector */
140 return BOOT_STATUS_STATE_COUNT * min_write_sz +
141#ifdef MCUBOOT_ENC_IMAGES
142 /* encryption keys */
143 BOOT_ENC_KEY_SIZE * 2 +
144#endif
145 /* image_ok + swap_size */
146 BOOT_MAX_ALIGN * 2 +
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300147 BOOT_MAGIC_SZ;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800148}
149
150static uint32_t
151boot_magic_off(const struct flash_area *fap)
152{
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300153 assert(offsetof(struct image_trailer, magic) == 16);
154 return fap->fa_size - BOOT_MAGIC_SZ;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800155}
156
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400157int
158boot_status_entries(const struct flash_area *fap)
159{
160 switch (fap->fa_id) {
David Vincze2d736ad2019-02-18 11:50:22 +0100161 case FLASH_AREA_IMAGE_PRIMARY:
162 case FLASH_AREA_IMAGE_SECONDARY:
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400163 return BOOT_STATUS_STATE_COUNT * BOOT_STATUS_MAX_ENTRIES;
164 case FLASH_AREA_IMAGE_SCRATCH:
165 return BOOT_STATUS_STATE_COUNT;
166 default:
167 return BOOT_EBADARGS;
168 }
169}
170
Christopher Collins92ea77f2016-12-12 15:59:26 -0800171uint32_t
172boot_status_off(const struct flash_area *fap)
173{
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300174 uint32_t off_from_end;
175 uint8_t elem_sz;
176
177 elem_sz = flash_area_align(fap);
178
179 if (fap->fa_id == FLASH_AREA_IMAGE_SCRATCH) {
180 off_from_end = boot_scratch_trailer_sz(elem_sz);
181 } else {
182 off_from_end = boot_slots_trailer_sz(elem_sz);
183 }
184
185 assert(off_from_end <= fap->fa_size);
186 return fap->fa_size - off_from_end;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800187}
188
189static uint32_t
190boot_copy_done_off(const struct flash_area *fap)
191{
Fabio Utzig2473ac02017-05-02 12:45:02 -0300192 assert(fap->fa_id != FLASH_AREA_IMAGE_SCRATCH);
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300193 assert(offsetof(struct image_trailer, copy_done) == 0);
194 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 2;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800195}
196
197static uint32_t
198boot_image_ok_off(const struct flash_area *fap)
199{
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300200 assert(offsetof(struct image_trailer, image_ok) == 8);
201 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800202}
203
Fabio Utzig46490722017-09-04 15:34:32 -0300204static uint32_t
205boot_swap_size_off(const struct flash_area *fap)
206{
207 /*
208 * The "swap_size" field if located just before the trailer.
209 * The scratch slot doesn't store "copy_done"...
210 */
211 if (fap->fa_id == FLASH_AREA_IMAGE_SCRATCH) {
212 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 2;
213 }
214
215 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 3;
216}
217
Fabio Utzigba829042018-09-18 08:29:34 -0300218#ifdef MCUBOOT_ENC_IMAGES
219static uint32_t
220boot_enc_key_off(const struct flash_area *fap, uint8_t slot)
221{
222 if (fap->fa_id == FLASH_AREA_IMAGE_SCRATCH) {
223 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 2 -
224 ((slot + 1) * BOOT_ENC_KEY_SIZE);
225 }
226
227 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 3 -
228 ((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;
238 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
Fabio Utzig2473ac02017-05-02 12:45:02 -0300251 if (fap->fa_id != FLASH_AREA_IMAGE_SCRATCH) {
252 off = boot_copy_done_off(fap);
Fabio Utzig178be542018-09-19 08:12:56 -0300253 rc = flash_area_read_is_empty(fap, off, &state->copy_done,
254 sizeof state->copy_done);
255 if (rc < 0) {
Fabio Utzig2473ac02017-05-02 12:45:02 -0300256 return BOOT_EFLASH;
257 }
Fabio Utzig178be542018-09-19 08:12:56 -0300258 if (rc == 1) {
259 state->copy_done = BOOT_FLAG_UNSET;
260 } else {
261 state->copy_done = boot_flag_decode(state->copy_done);
262 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800263 }
264
265 off = boot_image_ok_off(fap);
David Vincze2d736ad2019-02-18 11:50:22 +0100266 rc = flash_area_read_is_empty(fap, off, &state->image_ok,
267 sizeof state->image_ok);
Fabio Utzig178be542018-09-19 08:12:56 -0300268 if (rc < 0) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800269 return BOOT_EFLASH;
270 }
Fabio Utzig178be542018-09-19 08:12:56 -0300271 if (rc == 1) {
272 state->image_ok = BOOT_FLAG_UNSET;
273 } else {
274 state->image_ok = boot_flag_decode(state->image_ok);
275 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800276
277 return 0;
278}
279
280/**
281 * Reads the image trailer from the scratch area.
282 */
283int
Fabio Utzig2473ac02017-05-02 12:45:02 -0300284boot_read_swap_state_by_id(int flash_area_id, struct boot_swap_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800285{
286 const struct flash_area *fap;
287 int rc;
288
Fabio Utzig2473ac02017-05-02 12:45:02 -0300289 switch (flash_area_id) {
290 case FLASH_AREA_IMAGE_SCRATCH:
David Vincze2d736ad2019-02-18 11:50:22 +0100291 case FLASH_AREA_IMAGE_PRIMARY:
292 case FLASH_AREA_IMAGE_SECONDARY:
Fabio Utzig2473ac02017-05-02 12:45:02 -0300293 rc = flash_area_open(flash_area_id, &fap);
294 if (rc != 0) {
295 return BOOT_EFLASH;
296 }
297 break;
298 default:
Fabio Utzig856f7832017-05-22 11:04:44 -0400299 return BOOT_EBADARGS;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300300 }
301
302 rc = boot_read_swap_state(fap, state);
Fabio Utzigacfba2e2017-05-22 11:06:29 -0400303 flash_area_close(fap);
304 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800305}
306
307int
Fabio Utzig46490722017-09-04 15:34:32 -0300308boot_read_swap_size(uint32_t *swap_size)
309{
Hovland, Sigvart1d96f362018-09-25 13:23:42 +0200310 uint32_t magic[BOOT_MAGIC_ARR_SZ];
Fabio Utzig46490722017-09-04 15:34:32 -0300311 uint32_t off;
312 const struct flash_area *fap;
313 int rc;
314
315 /*
316 * In the middle a swap, tries to locate the saved swap size. Looks
David Vincze2d736ad2019-02-18 11:50:22 +0100317 * for a valid magic, first on the primary slot, then on scratch.
318 * Both "slots" can end up being temporary storage for a swap and it
319 * is assumed that if magic is valid then swap size is too, because
320 * magic is always written in the last step.
Fabio Utzig46490722017-09-04 15:34:32 -0300321 */
322
David Vincze2d736ad2019-02-18 11:50:22 +0100323 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap);
Fabio Utzig46490722017-09-04 15:34:32 -0300324 if (rc != 0) {
325 return BOOT_EFLASH;
326 }
327
328 off = boot_magic_off(fap);
329 rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ);
330 if (rc != 0) {
331 rc = BOOT_EFLASH;
332 goto out;
333 }
334
335 if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) != 0) {
336 /*
David Vincze2d736ad2019-02-18 11:50:22 +0100337 * If the primary slot's magic is not valid, try scratch...
Fabio Utzig46490722017-09-04 15:34:32 -0300338 */
339
340 flash_area_close(fap);
341
342 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap);
343 if (rc != 0) {
344 return BOOT_EFLASH;
345 }
346
347 off = boot_magic_off(fap);
348 rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ);
349 if (rc != 0) {
350 rc = BOOT_EFLASH;
351 goto out;
352 }
353
354 assert(memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0);
355 }
356
357 off = boot_swap_size_off(fap);
358 rc = flash_area_read(fap, off, swap_size, sizeof *swap_size);
359 if (rc != 0) {
360 rc = BOOT_EFLASH;
361 }
362
363out:
364 flash_area_close(fap);
365 return rc;
366}
367
Fabio Utzigba829042018-09-18 08:29:34 -0300368#ifdef MCUBOOT_ENC_IMAGES
369int
370boot_read_enc_key(uint8_t slot, uint8_t *enckey)
371{
372 uint32_t magic[BOOT_MAGIC_SZ];
373 uint32_t off;
374 const struct flash_area *fap;
375 int rc;
376
David Vincze2d736ad2019-02-18 11:50:22 +0100377 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap);
Fabio Utzigba829042018-09-18 08:29:34 -0300378 if (rc != 0) {
379 return BOOT_EFLASH;
380 }
381
382 off = boot_magic_off(fap);
383 rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ);
384 if (rc != 0) {
385 rc = BOOT_EFLASH;
386 goto out;
387 }
388
389 if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) != 0) {
390 /*
David Vincze2d736ad2019-02-18 11:50:22 +0100391 * If the primary slot's magic is not valid, try scratch...
Fabio Utzigba829042018-09-18 08:29:34 -0300392 */
393
394 flash_area_close(fap);
395
396 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap);
397 if (rc != 0) {
398 return BOOT_EFLASH;
399 }
400
401 off = boot_magic_off(fap);
402 rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ);
403 if (rc != 0) {
404 rc = BOOT_EFLASH;
405 goto out;
406 }
407
408 assert(memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0);
409 }
410
411 off = boot_enc_key_off(fap, slot);
412 rc = flash_area_read(fap, off, enckey, BOOT_ENC_KEY_SIZE);
413 if (rc != 0) {
414 rc = BOOT_EFLASH;
415 }
416
417out:
418 flash_area_close(fap);
419 return rc;
420}
421#endif
Fabio Utzig46490722017-09-04 15:34:32 -0300422
423int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800424boot_write_magic(const struct flash_area *fap)
425{
426 uint32_t off;
427 int rc;
428
429 off = boot_magic_off(fap);
430
Fabio Utzig24a273d2017-04-20 08:21:31 -0300431 rc = flash_area_write(fap, off, boot_img_magic, BOOT_MAGIC_SZ);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800432 if (rc != 0) {
433 return BOOT_EFLASH;
434 }
435
436 return 0;
437}
438
Fabio Utzig2473ac02017-05-02 12:45:02 -0300439static int
440boot_write_flag(int flag, const struct flash_area *fap)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800441{
442 uint32_t off;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800443 int rc;
Fabio Utzig644b8d42017-04-20 07:56:05 -0300444 uint8_t buf[BOOT_MAX_ALIGN];
David Brown9d725462017-01-23 15:50:58 -0700445 uint8_t align;
Fabio Utzig39000012018-07-30 12:40:20 -0300446 uint8_t erased_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800447
Fabio Utzig2473ac02017-05-02 12:45:02 -0300448 switch (flag) {
449 case BOOT_FLAG_COPY_DONE:
450 off = boot_copy_done_off(fap);
451 break;
452 case BOOT_FLAG_IMAGE_OK:
453 off = boot_image_ok_off(fap);
454 break;
455 default:
Fabio Utzig856f7832017-05-22 11:04:44 -0400456 return BOOT_EBADARGS;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300457 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800458
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200459 align = flash_area_align(fap);
Fabio Utzig644b8d42017-04-20 07:56:05 -0300460 assert(align <= BOOT_MAX_ALIGN);
Fabio Utzig39000012018-07-30 12:40:20 -0300461 erased_val = flash_area_erased_val(fap);
462 memset(buf, erased_val, BOOT_MAX_ALIGN);
Fabio Utzigde8a38a2017-05-23 11:15:01 -0400463 buf[0] = BOOT_FLAG_SET;
David Brown9d725462017-01-23 15:50:58 -0700464
465 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800466 if (rc != 0) {
467 return BOOT_EFLASH;
468 }
469
470 return 0;
471}
472
473int
Fabio Utzig2473ac02017-05-02 12:45:02 -0300474boot_write_copy_done(const struct flash_area *fap)
475{
476 return boot_write_flag(BOOT_FLAG_COPY_DONE, fap);
477}
478
479int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800480boot_write_image_ok(const struct flash_area *fap)
481{
Fabio Utzig2473ac02017-05-02 12:45:02 -0300482 return boot_write_flag(BOOT_FLAG_IMAGE_OK, fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800483}
484
485int
Fabio Utzig46490722017-09-04 15:34:32 -0300486boot_write_swap_size(const struct flash_area *fap, uint32_t swap_size)
487{
488 uint32_t off;
489 int rc;
490 uint8_t buf[BOOT_MAX_ALIGN];
491 uint8_t align;
Fabio Utzig39000012018-07-30 12:40:20 -0300492 uint8_t erased_val;
Fabio Utzig46490722017-09-04 15:34:32 -0300493
494 off = boot_swap_size_off(fap);
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200495 align = flash_area_align(fap);
Fabio Utzig46490722017-09-04 15:34:32 -0300496 assert(align <= BOOT_MAX_ALIGN);
497 if (align < sizeof swap_size) {
498 align = sizeof swap_size;
499 }
Fabio Utzig39000012018-07-30 12:40:20 -0300500 erased_val = flash_area_erased_val(fap);
501 memset(buf, erased_val, BOOT_MAX_ALIGN);
Fabio Utzig46490722017-09-04 15:34:32 -0300502 memcpy(buf, (uint8_t *)&swap_size, sizeof swap_size);
503
504 rc = flash_area_write(fap, off, buf, align);
505 if (rc != 0) {
506 return BOOT_EFLASH;
507 }
508
509 return 0;
510}
511
Fabio Utzigba829042018-09-18 08:29:34 -0300512#ifdef MCUBOOT_ENC_IMAGES
513int
514boot_write_enc_key(const struct flash_area *fap, uint8_t slot, const uint8_t *enckey)
515{
516 uint32_t off;
517 int rc;
518
519 off = boot_enc_key_off(fap, slot);
520 rc = flash_area_write(fap, off, enckey, BOOT_ENC_KEY_SIZE);
521 if (rc != 0) {
522 return BOOT_EFLASH;
523 }
524
525 return 0;
526}
527#endif
528
Fabio Utzig46490722017-09-04 15:34:32 -0300529int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800530boot_swap_type(void)
531{
532 const struct boot_swap_table *table;
David Vincze2d736ad2019-02-18 11:50:22 +0100533 struct boot_swap_state primary_slot;
534 struct boot_swap_state secondary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800535 int rc;
Fabio Utzigcd5774b2017-11-29 10:18:26 -0200536 size_t i;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800537
David Vincze2d736ad2019-02-18 11:50:22 +0100538 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY, &primary_slot);
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300539 if (rc) {
540 return BOOT_SWAP_TYPE_PANIC;
541 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800542
David Vincze2d736ad2019-02-18 11:50:22 +0100543 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY,
544 &secondary_slot);
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300545 if (rc) {
546 return BOOT_SWAP_TYPE_PANIC;
547 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800548
549 for (i = 0; i < BOOT_SWAP_TABLES_COUNT; i++) {
550 table = boot_swap_tables + i;
551
David Vincze2d736ad2019-02-18 11:50:22 +0100552 if ((table->magic_primary_slot == BOOT_MAGIC_ANY ||
553 table->magic_primary_slot == primary_slot.magic) &&
554 (table->magic_secondary_slot == BOOT_MAGIC_ANY ||
555 table->magic_secondary_slot == secondary_slot.magic) &&
556 (table->image_ok_primary_slot == BOOT_FLAG_ANY ||
557 table->image_ok_primary_slot == primary_slot.image_ok) &&
558 (table->image_ok_secondary_slot == BOOT_FLAG_ANY ||
559 table->image_ok_secondary_slot == secondary_slot.image_ok) &&
560 (table->copy_done_primary_slot == BOOT_FLAG_ANY ||
561 table->copy_done_primary_slot == primary_slot.copy_done)) {
Fabio Utzig34e393e2017-05-22 11:07:07 -0400562 BOOT_LOG_INF("Swap type: %s",
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300563 table->swap_type == BOOT_SWAP_TYPE_TEST ? "test" :
564 table->swap_type == BOOT_SWAP_TYPE_PERM ? "perm" :
565 table->swap_type == BOOT_SWAP_TYPE_REVERT ? "revert" :
566 "BUG; can't happen");
567 assert(table->swap_type == BOOT_SWAP_TYPE_TEST ||
568 table->swap_type == BOOT_SWAP_TYPE_PERM ||
569 table->swap_type == BOOT_SWAP_TYPE_REVERT);
570 return table->swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800571 }
572 }
573
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300574 BOOT_LOG_INF("Swap type: none");
Christopher Collins92ea77f2016-12-12 15:59:26 -0800575 return BOOT_SWAP_TYPE_NONE;
576}
577
578/**
David Vincze2d736ad2019-02-18 11:50:22 +0100579 * Marks the image in the secondary slot as pending. On the next reboot,
580 * the system will perform a one-time boot of the the secondary slot image.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800581 *
Christopher Collins7835c1e2016-12-21 10:10:51 -0800582 * @param permanent Whether the image should be used permanently or
583 * only tested once:
584 * 0=run image once, then confirm or revert.
585 * 1=run image forever.
586 *
Christopher Collins92ea77f2016-12-12 15:59:26 -0800587 * @return 0 on success; nonzero on failure.
588 */
589int
Christopher Collins7835c1e2016-12-21 10:10:51 -0800590boot_set_pending(int permanent)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800591{
592 const struct flash_area *fap;
David Vincze2d736ad2019-02-18 11:50:22 +0100593 struct boot_swap_state state_secondary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800594 int rc;
595
David Vincze2d736ad2019-02-18 11:50:22 +0100596 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY,
597 &state_secondary_slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800598 if (rc != 0) {
599 return rc;
600 }
601
David Vincze2d736ad2019-02-18 11:50:22 +0100602 switch (state_secondary_slot.magic) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800603 case BOOT_MAGIC_GOOD:
604 /* Swap already scheduled. */
605 return 0;
606
607 case BOOT_MAGIC_UNSET:
David Vincze2d736ad2019-02-18 11:50:22 +0100608 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800609 if (rc != 0) {
610 rc = BOOT_EFLASH;
611 } else {
612 rc = boot_write_magic(fap);
613 }
614
Christopher Collins7835c1e2016-12-21 10:10:51 -0800615 if (rc == 0 && permanent) {
616 rc = boot_write_image_ok(fap);
617 }
618
Christopher Collins92ea77f2016-12-12 15:59:26 -0800619 flash_area_close(fap);
620 return rc;
621
Christopher Collinsae01f152019-01-30 09:56:37 -0800622 case BOOT_MAGIC_BAD:
623 /* The image slot is corrupt. There is no way to recover, so erase the
624 * slot to allow future upgrades.
625 */
David Vincze2d736ad2019-02-18 11:50:22 +0100626 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY, &fap);
Christopher Collinsae01f152019-01-30 09:56:37 -0800627 if (rc != 0) {
628 return BOOT_EFLASH;
629 }
630
631 flash_area_erase(fap, 0, fap->fa_size);
632 flash_area_close(fap);
633 return BOOT_EBADIMAGE;
634
Christopher Collins92ea77f2016-12-12 15:59:26 -0800635 default:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800636 assert(0);
Christopher Collinsae01f152019-01-30 09:56:37 -0800637 return BOOT_EBADIMAGE;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800638 }
639}
640
641/**
David Vincze2d736ad2019-02-18 11:50:22 +0100642 * Marks the image in the primary slot as confirmed. The system will continue
643 * booting into the image in the primary slot until told to boot from a
644 * different slot.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800645 *
646 * @return 0 on success; nonzero on failure.
647 */
648int
649boot_set_confirmed(void)
650{
651 const struct flash_area *fap;
David Vincze2d736ad2019-02-18 11:50:22 +0100652 struct boot_swap_state state_primary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800653 int rc;
654
David Vincze2d736ad2019-02-18 11:50:22 +0100655 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY,
656 &state_primary_slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800657 if (rc != 0) {
658 return rc;
659 }
660
David Vincze2d736ad2019-02-18 11:50:22 +0100661 switch (state_primary_slot.magic) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800662 case BOOT_MAGIC_GOOD:
663 /* Confirm needed; proceed. */
664 break;
665
666 case BOOT_MAGIC_UNSET:
667 /* Already confirmed. */
668 return 0;
669
670 case BOOT_MAGIC_BAD:
671 /* Unexpected state. */
672 return BOOT_EBADVECT;
673 }
674
David Vincze2d736ad2019-02-18 11:50:22 +0100675 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800676 if (rc) {
677 rc = BOOT_EFLASH;
678 goto done;
679 }
680
David Vincze2d736ad2019-02-18 11:50:22 +0100681 if (state_primary_slot.copy_done == BOOT_FLAG_UNSET) {
Fabio Utzig39000012018-07-30 12:40:20 -0300682 /* Swap never completed. This is unexpected. */
683 rc = BOOT_EBADVECT;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800684 goto done;
685 }
686
David Vincze2d736ad2019-02-18 11:50:22 +0100687 if (state_primary_slot.image_ok != BOOT_FLAG_UNSET) {
Fabio Utzig39000012018-07-30 12:40:20 -0300688 /* Already confirmed. */
689 goto done;
690 }
691
692 rc = boot_write_image_ok(fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800693
694done:
695 flash_area_close(fap);
696 return rc;
697}