blob: 6f85ff1f5d14689157f7b39b306cf9903a6ccd30 [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"
26#include "hal/hal_bsp.h"
27#include "hal/hal_flash.h"
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020028
29#include "flash_map_backend/flash_map_backend.h"
30
Christopher Collins92ea77f2016-12-12 15:59:26 -080031#include "os/os.h"
32#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
Christopher Collins92ea77f2016-12-12 15:59:26 -080040int boot_current_slot;
41
Fabio Utzig24a273d2017-04-20 08:21:31 -030042const uint32_t boot_img_magic[] = {
Christopher Collins92ea77f2016-12-12 15:59:26 -080043 0xf395c277,
44 0x7fefd260,
45 0x0f505235,
46 0x8079b62c,
47};
48
Hovland, Sigvart1d96f362018-09-25 13:23:42 +020049#define BOOT_MAGIC_ARR_SZ \
50 (sizeof boot_img_magic / sizeof boot_img_magic[0])
51
Fabio Utzig24a273d2017-04-20 08:21:31 -030052const uint32_t BOOT_MAGIC_SZ = sizeof boot_img_magic;
Fabio Utziga0bc9b52017-06-28 09:19:55 -030053const uint32_t BOOT_MAX_ALIGN = MAX_FLASH_ALIGN;
Fabio Utzig24a273d2017-04-20 08:21:31 -030054
Christopher Collins92ea77f2016-12-12 15:59:26 -080055struct boot_swap_table {
Fabio Utzigb5b2f552017-06-30 10:03:47 -030056 uint8_t magic_slot0;
57 uint8_t magic_slot1;
58 uint8_t image_ok_slot0;
59 uint8_t image_ok_slot1;
Fabio Utzigd7d20752017-07-13 16:03:25 -030060 uint8_t copy_done_slot0;
Christopher Collins92ea77f2016-12-12 15:59:26 -080061
Fabio Utzigb5b2f552017-06-30 10:03:47 -030062 uint8_t swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -080063};
64
65/**
66 * This set of tables maps image trailer contents to swap operation type.
67 * When searching for a match, these tables must be iterated sequentially.
Fabio Utzigb5b2f552017-06-30 10:03:47 -030068 *
69 * NOTE: the table order is very important. The settings in Slot 1 always
70 * are priority to Slot 0 and should be located earlier in the table.
71 *
72 * The table lists only states where there is action needs to be taken by
73 * the bootloader, as in starting/finishing a swap operation.
Christopher Collins92ea77f2016-12-12 15:59:26 -080074 */
75static const struct boot_swap_table boot_swap_tables[] = {
76 {
Fabio Utzig39000012018-07-30 12:40:20 -030077 .magic_slot0 = BOOT_MAGIC_ANY,
Fabio Utzigb5b2f552017-06-30 10:03:47 -030078 .magic_slot1 = BOOT_MAGIC_GOOD,
Fabio Utzig39000012018-07-30 12:40:20 -030079 .image_ok_slot0 = BOOT_FLAG_ANY,
80 .image_ok_slot1 = BOOT_FLAG_UNSET,
81 .copy_done_slot0 = BOOT_FLAG_ANY,
Fabio Utzigb5b2f552017-06-30 10:03:47 -030082 .swap_type = BOOT_SWAP_TYPE_TEST,
Christopher Collins92ea77f2016-12-12 15:59:26 -080083 },
Christopher Collins92ea77f2016-12-12 15:59:26 -080084 {
Fabio Utzig39000012018-07-30 12:40:20 -030085 .magic_slot0 = BOOT_MAGIC_ANY,
Fabio Utzigb5b2f552017-06-30 10:03:47 -030086 .magic_slot1 = BOOT_MAGIC_GOOD,
Fabio Utzig39000012018-07-30 12:40:20 -030087 .image_ok_slot0 = BOOT_FLAG_ANY,
88 .image_ok_slot1 = BOOT_FLAG_SET,
89 .copy_done_slot0 = BOOT_FLAG_ANY,
Fabio Utzigb5b2f552017-06-30 10:03:47 -030090 .swap_type = BOOT_SWAP_TYPE_PERM,
Christopher Collins92ea77f2016-12-12 15:59:26 -080091 },
Christopher Collins92ea77f2016-12-12 15:59:26 -080092 {
Fabio Utzigb5b2f552017-06-30 10:03:47 -030093 .magic_slot0 = BOOT_MAGIC_GOOD,
94 .magic_slot1 = BOOT_MAGIC_UNSET,
Fabio Utzig39000012018-07-30 12:40:20 -030095 .image_ok_slot0 = BOOT_FLAG_UNSET,
96 .image_ok_slot1 = BOOT_FLAG_ANY,
97 .copy_done_slot0 = BOOT_FLAG_SET,
Fabio Utzigb5b2f552017-06-30 10:03:47 -030098 .swap_type = BOOT_SWAP_TYPE_REVERT,
Christopher Collins92ea77f2016-12-12 15:59:26 -080099 },
100};
101
102#define BOOT_SWAP_TABLES_COUNT \
103 (sizeof boot_swap_tables / sizeof boot_swap_tables[0])
104
Fabio Utzig39000012018-07-30 12:40:20 -0300105static int
Fabio Utzig178be542018-09-19 08:12:56 -0300106boot_magic_decode(const uint32_t *magic)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800107{
Fabio Utzig24a273d2017-04-20 08:21:31 -0300108 if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800109 return BOOT_MAGIC_GOOD;
110 }
Fabio Utzig178be542018-09-19 08:12:56 -0300111 return BOOT_MAGIC_BAD;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800112}
113
Fabio Utzig39000012018-07-30 12:40:20 -0300114static int
Fabio Utzig178be542018-09-19 08:12:56 -0300115boot_flag_decode(uint8_t flag)
Fabio Utzig39000012018-07-30 12:40:20 -0300116{
Fabio Utzig39000012018-07-30 12:40:20 -0300117 if (flag != BOOT_FLAG_SET) {
118 return BOOT_FLAG_BAD;
119 }
120 return BOOT_FLAG_SET;
121}
122
Christopher Collins92ea77f2016-12-12 15:59:26 -0800123uint32_t
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300124boot_slots_trailer_sz(uint8_t min_write_sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800125{
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300126 return /* state for all sectors */
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300127 BOOT_STATUS_MAX_ENTRIES * BOOT_STATUS_STATE_COUNT * min_write_sz +
Fabio Utzigba829042018-09-18 08:29:34 -0300128#ifdef MCUBOOT_ENC_IMAGES
129 /* encryption keys */
130 BOOT_ENC_KEY_SIZE * 2 +
131#endif
132 /* copy_done + image_ok + swap_size */
133 BOOT_MAX_ALIGN * 3 +
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300134 BOOT_MAGIC_SZ;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800135}
136
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300137static uint32_t
138boot_scratch_trailer_sz(uint8_t min_write_sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800139{
Fabio Utzigba829042018-09-18 08:29:34 -0300140 /* state for one sector */
141 return BOOT_STATUS_STATE_COUNT * min_write_sz +
142#ifdef MCUBOOT_ENC_IMAGES
143 /* encryption keys */
144 BOOT_ENC_KEY_SIZE * 2 +
145#endif
146 /* image_ok + swap_size */
147 BOOT_MAX_ALIGN * 2 +
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300148 BOOT_MAGIC_SZ;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800149}
150
151static uint32_t
152boot_magic_off(const struct flash_area *fap)
153{
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300154 assert(offsetof(struct image_trailer, magic) == 16);
155 return fap->fa_size - BOOT_MAGIC_SZ;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800156}
157
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400158int
159boot_status_entries(const struct flash_area *fap)
160{
161 switch (fap->fa_id) {
162 case FLASH_AREA_IMAGE_0:
163 case FLASH_AREA_IMAGE_1:
164 return BOOT_STATUS_STATE_COUNT * BOOT_STATUS_MAX_ENTRIES;
165 case FLASH_AREA_IMAGE_SCRATCH:
166 return BOOT_STATUS_STATE_COUNT;
167 default:
168 return BOOT_EBADARGS;
169 }
170}
171
Christopher Collins92ea77f2016-12-12 15:59:26 -0800172uint32_t
173boot_status_off(const struct flash_area *fap)
174{
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300175 uint32_t off_from_end;
176 uint8_t elem_sz;
177
178 elem_sz = flash_area_align(fap);
179
180 if (fap->fa_id == FLASH_AREA_IMAGE_SCRATCH) {
181 off_from_end = boot_scratch_trailer_sz(elem_sz);
182 } else {
183 off_from_end = boot_slots_trailer_sz(elem_sz);
184 }
185
186 assert(off_from_end <= fap->fa_size);
187 return fap->fa_size - off_from_end;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800188}
189
190static uint32_t
191boot_copy_done_off(const struct flash_area *fap)
192{
Fabio Utzig2473ac02017-05-02 12:45:02 -0300193 assert(fap->fa_id != FLASH_AREA_IMAGE_SCRATCH);
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300194 assert(offsetof(struct image_trailer, copy_done) == 0);
195 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 2;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800196}
197
198static uint32_t
199boot_image_ok_off(const struct flash_area *fap)
200{
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300201 assert(offsetof(struct image_trailer, image_ok) == 8);
202 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800203}
204
Fabio Utzig46490722017-09-04 15:34:32 -0300205static uint32_t
206boot_swap_size_off(const struct flash_area *fap)
207{
208 /*
209 * The "swap_size" field if located just before the trailer.
210 * The scratch slot doesn't store "copy_done"...
211 */
212 if (fap->fa_id == FLASH_AREA_IMAGE_SCRATCH) {
213 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 2;
214 }
215
216 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 3;
217}
218
Fabio Utzigba829042018-09-18 08:29:34 -0300219#ifdef MCUBOOT_ENC_IMAGES
220static uint32_t
221boot_enc_key_off(const struct flash_area *fap, uint8_t slot)
222{
223 if (fap->fa_id == FLASH_AREA_IMAGE_SCRATCH) {
224 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 2 -
225 ((slot + 1) * BOOT_ENC_KEY_SIZE);
226 }
227
228 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 3 -
229 ((slot + 1) * BOOT_ENC_KEY_SIZE);
230}
231#endif
232
Christopher Collins92ea77f2016-12-12 15:59:26 -0800233int
234boot_read_swap_state(const struct flash_area *fap,
235 struct boot_swap_state *state)
236{
Hovland, Sigvart1d96f362018-09-25 13:23:42 +0200237 uint32_t magic[BOOT_MAGIC_ARR_SZ];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800238 uint32_t off;
239 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
Fabio Utzig2473ac02017-05-02 12:45:02 -0300252 if (fap->fa_id != FLASH_AREA_IMAGE_SCRATCH) {
253 off = boot_copy_done_off(fap);
Fabio Utzig178be542018-09-19 08:12:56 -0300254 rc = flash_area_read_is_empty(fap, off, &state->copy_done,
255 sizeof state->copy_done);
256 if (rc < 0) {
Fabio Utzig2473ac02017-05-02 12:45:02 -0300257 return BOOT_EFLASH;
258 }
Fabio Utzig178be542018-09-19 08:12:56 -0300259 if (rc == 1) {
260 state->copy_done = BOOT_FLAG_UNSET;
261 } else {
262 state->copy_done = boot_flag_decode(state->copy_done);
263 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800264 }
265
266 off = boot_image_ok_off(fap);
Fabio Utzig178be542018-09-19 08:12:56 -0300267 rc = flash_area_read_is_empty(fap, off, &state->image_ok, sizeof state->image_ok);
268 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:
291 case FLASH_AREA_IMAGE_0:
292 case FLASH_AREA_IMAGE_1:
293 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
317 * for a valid magic, first on Slot 0, then on scratch. Both "slots"
318 * can end up being temporary storage for a swap and it is assumed
319 * that if magic is valid then swap size is too, because magic is
320 * always written in the last step.
321 */
322
323 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
324 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 /*
337 * If Slot 0 's magic is not valid, try scratch...
338 */
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
377 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
378 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 /*
391 * If Slot 0 's magic is not valid, try scratch...
392 */
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;
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300533 struct boot_swap_state slot0;
534 struct boot_swap_state slot1;
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
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300538 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_0, &slot0);
539 if (rc) {
540 return BOOT_SWAP_TYPE_PANIC;
541 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800542
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300543 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_1, &slot1);
544 if (rc) {
545 return BOOT_SWAP_TYPE_PANIC;
546 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800547
548 for (i = 0; i < BOOT_SWAP_TABLES_COUNT; i++) {
549 table = boot_swap_tables + i;
550
Fabio Utzig39000012018-07-30 12:40:20 -0300551 if ((table->magic_slot0 == BOOT_MAGIC_ANY ||
552 table->magic_slot0 == slot0.magic) &&
553 (table->magic_slot1 == BOOT_MAGIC_ANY ||
554 table->magic_slot1 == slot1.magic) &&
555 (table->image_ok_slot0 == BOOT_FLAG_ANY ||
556 table->image_ok_slot0 == slot0.image_ok) &&
557 (table->image_ok_slot1 == BOOT_FLAG_ANY ||
558 table->image_ok_slot1 == slot1.image_ok) &&
559 (table->copy_done_slot0 == BOOT_FLAG_ANY ||
560 table->copy_done_slot0 == slot0.copy_done)) {
Fabio Utzig34e393e2017-05-22 11:07:07 -0400561 BOOT_LOG_INF("Swap type: %s",
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300562 table->swap_type == BOOT_SWAP_TYPE_TEST ? "test" :
563 table->swap_type == BOOT_SWAP_TYPE_PERM ? "perm" :
564 table->swap_type == BOOT_SWAP_TYPE_REVERT ? "revert" :
565 "BUG; can't happen");
566 assert(table->swap_type == BOOT_SWAP_TYPE_TEST ||
567 table->swap_type == BOOT_SWAP_TYPE_PERM ||
568 table->swap_type == BOOT_SWAP_TYPE_REVERT);
569 return table->swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800570 }
571 }
572
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300573 BOOT_LOG_INF("Swap type: none");
Christopher Collins92ea77f2016-12-12 15:59:26 -0800574 return BOOT_SWAP_TYPE_NONE;
575}
576
577/**
578 * Marks the image in slot 1 as pending. On the next reboot, the system will
579 * perform a one-time boot of the slot 1 image.
580 *
Christopher Collins7835c1e2016-12-21 10:10:51 -0800581 * @param permanent Whether the image should be used permanently or
582 * only tested once:
583 * 0=run image once, then confirm or revert.
584 * 1=run image forever.
585 *
Christopher Collins92ea77f2016-12-12 15:59:26 -0800586 * @return 0 on success; nonzero on failure.
587 */
588int
Christopher Collins7835c1e2016-12-21 10:10:51 -0800589boot_set_pending(int permanent)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800590{
591 const struct flash_area *fap;
592 struct boot_swap_state state_slot1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800593 int rc;
594
Fabio Utzig2473ac02017-05-02 12:45:02 -0300595 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_1, &state_slot1);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800596 if (rc != 0) {
597 return rc;
598 }
599
600 switch (state_slot1.magic) {
601 case BOOT_MAGIC_GOOD:
602 /* Swap already scheduled. */
603 return 0;
604
605 case BOOT_MAGIC_UNSET:
Fabio Utzig2473ac02017-05-02 12:45:02 -0300606 rc = flash_area_open(FLASH_AREA_IMAGE_1, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800607 if (rc != 0) {
608 rc = BOOT_EFLASH;
609 } else {
610 rc = boot_write_magic(fap);
611 }
612
Christopher Collins7835c1e2016-12-21 10:10:51 -0800613 if (rc == 0 && permanent) {
614 rc = boot_write_image_ok(fap);
615 }
616
Christopher Collins92ea77f2016-12-12 15:59:26 -0800617 flash_area_close(fap);
618 return rc;
619
620 default:
621 /* XXX: Temporary assert. */
622 assert(0);
623 return -1;
624 }
625}
626
627/**
628 * Marks the image in slot 0 as confirmed. The system will continue booting into the image in slot 0 until told to boot from a different slot.
629 *
630 * @return 0 on success; nonzero on failure.
631 */
632int
633boot_set_confirmed(void)
634{
635 const struct flash_area *fap;
636 struct boot_swap_state state_slot0;
637 int rc;
638
Fabio Utzig2473ac02017-05-02 12:45:02 -0300639 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_0, &state_slot0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800640 if (rc != 0) {
641 return rc;
642 }
643
644 switch (state_slot0.magic) {
645 case BOOT_MAGIC_GOOD:
646 /* Confirm needed; proceed. */
647 break;
648
649 case BOOT_MAGIC_UNSET:
650 /* Already confirmed. */
651 return 0;
652
653 case BOOT_MAGIC_BAD:
654 /* Unexpected state. */
655 return BOOT_EBADVECT;
656 }
657
Christopher Collins92ea77f2016-12-12 15:59:26 -0800658 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
659 if (rc) {
660 rc = BOOT_EFLASH;
661 goto done;
662 }
663
Fabio Utzig08fa2672018-09-26 12:16:18 -0300664 if (state_slot0.copy_done == BOOT_FLAG_UNSET) {
Fabio Utzig39000012018-07-30 12:40:20 -0300665 /* Swap never completed. This is unexpected. */
666 rc = BOOT_EBADVECT;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800667 goto done;
668 }
669
Ɓukasz Rymanowskia1927f42018-09-26 15:31:50 +0200670 if (state_slot0.image_ok != BOOT_FLAG_UNSET) {
Fabio Utzig39000012018-07-30 12:40:20 -0300671 /* Already confirmed. */
672 goto done;
673 }
674
675 rc = boot_write_image_ok(fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800676
677done:
678 flash_area_close(fap);
679 return rc;
680}