blob: 47b41fda32732c6e2e850e06dbdda83075b7caa8 [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 {
Fabio Utzigb5b2f552017-06-30 10:03:47 -030054 uint8_t magic_slot0;
55 uint8_t magic_slot1;
56 uint8_t image_ok_slot0;
57 uint8_t image_ok_slot1;
Fabio Utzigd7d20752017-07-13 16:03:25 -030058 uint8_t copy_done_slot0;
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 *
67 * NOTE: the table order is very important. The settings in Slot 1 always
68 * are priority to Slot 0 and should be located earlier in the table.
69 *
70 * The table lists only states where there is action needs to be taken by
71 * the bootloader, as in starting/finishing a swap operation.
Christopher Collins92ea77f2016-12-12 15:59:26 -080072 */
73static const struct boot_swap_table boot_swap_tables[] = {
74 {
Fabio Utzig39000012018-07-30 12:40:20 -030075 .magic_slot0 = BOOT_MAGIC_ANY,
Fabio Utzigb5b2f552017-06-30 10:03:47 -030076 .magic_slot1 = BOOT_MAGIC_GOOD,
Fabio Utzig39000012018-07-30 12:40:20 -030077 .image_ok_slot0 = BOOT_FLAG_ANY,
78 .image_ok_slot1 = BOOT_FLAG_UNSET,
79 .copy_done_slot0 = BOOT_FLAG_ANY,
Fabio Utzigb5b2f552017-06-30 10:03:47 -030080 .swap_type = BOOT_SWAP_TYPE_TEST,
Christopher Collins92ea77f2016-12-12 15:59:26 -080081 },
Christopher Collins92ea77f2016-12-12 15:59:26 -080082 {
Fabio Utzig39000012018-07-30 12:40:20 -030083 .magic_slot0 = BOOT_MAGIC_ANY,
Fabio Utzigb5b2f552017-06-30 10:03:47 -030084 .magic_slot1 = BOOT_MAGIC_GOOD,
Fabio Utzig39000012018-07-30 12:40:20 -030085 .image_ok_slot0 = BOOT_FLAG_ANY,
86 .image_ok_slot1 = BOOT_FLAG_SET,
87 .copy_done_slot0 = BOOT_FLAG_ANY,
Fabio Utzigb5b2f552017-06-30 10:03:47 -030088 .swap_type = BOOT_SWAP_TYPE_PERM,
Christopher Collins92ea77f2016-12-12 15:59:26 -080089 },
Christopher Collins92ea77f2016-12-12 15:59:26 -080090 {
Fabio Utzigb5b2f552017-06-30 10:03:47 -030091 .magic_slot0 = BOOT_MAGIC_GOOD,
92 .magic_slot1 = BOOT_MAGIC_UNSET,
Fabio Utzig39000012018-07-30 12:40:20 -030093 .image_ok_slot0 = BOOT_FLAG_UNSET,
94 .image_ok_slot1 = BOOT_FLAG_ANY,
95 .copy_done_slot0 = BOOT_FLAG_SET,
Fabio Utzigb5b2f552017-06-30 10:03:47 -030096 .swap_type = BOOT_SWAP_TYPE_REVERT,
Christopher Collins92ea77f2016-12-12 15:59:26 -080097 },
98};
99
100#define BOOT_SWAP_TABLES_COUNT \
101 (sizeof boot_swap_tables / sizeof boot_swap_tables[0])
102
Fabio Utzig39000012018-07-30 12:40:20 -0300103static int
Fabio Utzig178be542018-09-19 08:12:56 -0300104boot_magic_decode(const uint32_t *magic)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800105{
Fabio Utzig24a273d2017-04-20 08:21:31 -0300106 if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800107 return BOOT_MAGIC_GOOD;
108 }
Fabio Utzig178be542018-09-19 08:12:56 -0300109 return BOOT_MAGIC_BAD;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800110}
111
Fabio Utzig39000012018-07-30 12:40:20 -0300112static int
Fabio Utzig178be542018-09-19 08:12:56 -0300113boot_flag_decode(uint8_t flag)
Fabio Utzig39000012018-07-30 12:40:20 -0300114{
Fabio Utzig39000012018-07-30 12:40:20 -0300115 if (flag != BOOT_FLAG_SET) {
116 return BOOT_FLAG_BAD;
117 }
118 return BOOT_FLAG_SET;
119}
120
Christopher Collins92ea77f2016-12-12 15:59:26 -0800121uint32_t
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300122boot_slots_trailer_sz(uint8_t min_write_sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800123{
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300124 return /* state for all sectors */
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300125 BOOT_STATUS_MAX_ENTRIES * BOOT_STATUS_STATE_COUNT * min_write_sz +
Fabio Utzigba829042018-09-18 08:29:34 -0300126#ifdef MCUBOOT_ENC_IMAGES
127 /* encryption keys */
128 BOOT_ENC_KEY_SIZE * 2 +
129#endif
130 /* copy_done + image_ok + swap_size */
131 BOOT_MAX_ALIGN * 3 +
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300132 BOOT_MAGIC_SZ;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800133}
134
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300135static uint32_t
136boot_scratch_trailer_sz(uint8_t min_write_sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800137{
Fabio Utzigba829042018-09-18 08:29:34 -0300138 /* state for one sector */
139 return BOOT_STATUS_STATE_COUNT * min_write_sz +
140#ifdef MCUBOOT_ENC_IMAGES
141 /* encryption keys */
142 BOOT_ENC_KEY_SIZE * 2 +
143#endif
144 /* image_ok + swap_size */
145 BOOT_MAX_ALIGN * 2 +
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300146 BOOT_MAGIC_SZ;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800147}
148
149static uint32_t
150boot_magic_off(const struct flash_area *fap)
151{
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300152 assert(offsetof(struct image_trailer, magic) == 16);
153 return fap->fa_size - BOOT_MAGIC_SZ;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800154}
155
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400156int
157boot_status_entries(const struct flash_area *fap)
158{
159 switch (fap->fa_id) {
160 case FLASH_AREA_IMAGE_0:
161 case FLASH_AREA_IMAGE_1:
162 return BOOT_STATUS_STATE_COUNT * BOOT_STATUS_MAX_ENTRIES;
163 case FLASH_AREA_IMAGE_SCRATCH:
164 return BOOT_STATUS_STATE_COUNT;
165 default:
166 return BOOT_EBADARGS;
167 }
168}
169
Christopher Collins92ea77f2016-12-12 15:59:26 -0800170uint32_t
171boot_status_off(const struct flash_area *fap)
172{
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300173 uint32_t off_from_end;
174 uint8_t elem_sz;
175
176 elem_sz = flash_area_align(fap);
177
178 if (fap->fa_id == FLASH_AREA_IMAGE_SCRATCH) {
179 off_from_end = boot_scratch_trailer_sz(elem_sz);
180 } else {
181 off_from_end = boot_slots_trailer_sz(elem_sz);
182 }
183
184 assert(off_from_end <= fap->fa_size);
185 return fap->fa_size - off_from_end;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800186}
187
188static uint32_t
189boot_copy_done_off(const struct flash_area *fap)
190{
Fabio Utzig2473ac02017-05-02 12:45:02 -0300191 assert(fap->fa_id != FLASH_AREA_IMAGE_SCRATCH);
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300192 assert(offsetof(struct image_trailer, copy_done) == 0);
193 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 2;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800194}
195
196static uint32_t
197boot_image_ok_off(const struct flash_area *fap)
198{
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300199 assert(offsetof(struct image_trailer, image_ok) == 8);
200 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800201}
202
Fabio Utzig46490722017-09-04 15:34:32 -0300203static uint32_t
204boot_swap_size_off(const struct flash_area *fap)
205{
206 /*
207 * The "swap_size" field if located just before the trailer.
208 * The scratch slot doesn't store "copy_done"...
209 */
210 if (fap->fa_id == FLASH_AREA_IMAGE_SCRATCH) {
211 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 2;
212 }
213
214 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 3;
215}
216
Fabio Utzigba829042018-09-18 08:29:34 -0300217#ifdef MCUBOOT_ENC_IMAGES
218static uint32_t
219boot_enc_key_off(const struct flash_area *fap, uint8_t slot)
220{
221 if (fap->fa_id == FLASH_AREA_IMAGE_SCRATCH) {
222 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 2 -
223 ((slot + 1) * BOOT_ENC_KEY_SIZE);
224 }
225
226 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 3 -
227 ((slot + 1) * BOOT_ENC_KEY_SIZE);
228}
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;
237 int rc;
238
239 off = boot_magic_off(fap);
Fabio Utzig178be542018-09-19 08:12:56 -0300240 rc = flash_area_read_is_empty(fap, off, magic, BOOT_MAGIC_SZ);
241 if (rc < 0) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800242 return BOOT_EFLASH;
243 }
Fabio Utzig178be542018-09-19 08:12:56 -0300244 if (rc == 1) {
245 state->magic = BOOT_MAGIC_UNSET;
246 } else {
247 state->magic = boot_magic_decode(magic);
248 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800249
Fabio Utzig2473ac02017-05-02 12:45:02 -0300250 if (fap->fa_id != FLASH_AREA_IMAGE_SCRATCH) {
251 off = boot_copy_done_off(fap);
Fabio Utzig178be542018-09-19 08:12:56 -0300252 rc = flash_area_read_is_empty(fap, off, &state->copy_done,
253 sizeof state->copy_done);
254 if (rc < 0) {
Fabio Utzig2473ac02017-05-02 12:45:02 -0300255 return BOOT_EFLASH;
256 }
Fabio Utzig178be542018-09-19 08:12:56 -0300257 if (rc == 1) {
258 state->copy_done = BOOT_FLAG_UNSET;
259 } else {
260 state->copy_done = boot_flag_decode(state->copy_done);
261 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800262 }
263
264 off = boot_image_ok_off(fap);
Fabio Utzig178be542018-09-19 08:12:56 -0300265 rc = flash_area_read_is_empty(fap, off, &state->image_ok, sizeof state->image_ok);
266 if (rc < 0) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800267 return BOOT_EFLASH;
268 }
Fabio Utzig178be542018-09-19 08:12:56 -0300269 if (rc == 1) {
270 state->image_ok = BOOT_FLAG_UNSET;
271 } else {
272 state->image_ok = boot_flag_decode(state->image_ok);
273 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800274
275 return 0;
276}
277
278/**
279 * Reads the image trailer from the scratch area.
280 */
281int
Fabio Utzig2473ac02017-05-02 12:45:02 -0300282boot_read_swap_state_by_id(int flash_area_id, struct boot_swap_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800283{
284 const struct flash_area *fap;
285 int rc;
286
Fabio Utzig2473ac02017-05-02 12:45:02 -0300287 switch (flash_area_id) {
288 case FLASH_AREA_IMAGE_SCRATCH:
289 case FLASH_AREA_IMAGE_0:
290 case FLASH_AREA_IMAGE_1:
291 rc = flash_area_open(flash_area_id, &fap);
292 if (rc != 0) {
293 return BOOT_EFLASH;
294 }
295 break;
296 default:
Fabio Utzig856f7832017-05-22 11:04:44 -0400297 return BOOT_EBADARGS;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300298 }
299
300 rc = boot_read_swap_state(fap, state);
Fabio Utzigacfba2e2017-05-22 11:06:29 -0400301 flash_area_close(fap);
302 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800303}
304
305int
Fabio Utzig46490722017-09-04 15:34:32 -0300306boot_read_swap_size(uint32_t *swap_size)
307{
Hovland, Sigvart1d96f362018-09-25 13:23:42 +0200308 uint32_t magic[BOOT_MAGIC_ARR_SZ];
Fabio Utzig46490722017-09-04 15:34:32 -0300309 uint32_t off;
310 const struct flash_area *fap;
311 int rc;
312
313 /*
314 * In the middle a swap, tries to locate the saved swap size. Looks
315 * for a valid magic, first on Slot 0, then on scratch. Both "slots"
316 * can end up being temporary storage for a swap and it is assumed
317 * that if magic is valid then swap size is too, because magic is
318 * always written in the last step.
319 */
320
321 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
322 if (rc != 0) {
323 return BOOT_EFLASH;
324 }
325
326 off = boot_magic_off(fap);
327 rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ);
328 if (rc != 0) {
329 rc = BOOT_EFLASH;
330 goto out;
331 }
332
333 if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) != 0) {
334 /*
335 * If Slot 0 's magic is not valid, try scratch...
336 */
337
338 flash_area_close(fap);
339
340 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap);
341 if (rc != 0) {
342 return BOOT_EFLASH;
343 }
344
345 off = boot_magic_off(fap);
346 rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ);
347 if (rc != 0) {
348 rc = BOOT_EFLASH;
349 goto out;
350 }
351
352 assert(memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0);
353 }
354
355 off = boot_swap_size_off(fap);
356 rc = flash_area_read(fap, off, swap_size, sizeof *swap_size);
357 if (rc != 0) {
358 rc = BOOT_EFLASH;
359 }
360
361out:
362 flash_area_close(fap);
363 return rc;
364}
365
Fabio Utzigba829042018-09-18 08:29:34 -0300366#ifdef MCUBOOT_ENC_IMAGES
367int
368boot_read_enc_key(uint8_t slot, uint8_t *enckey)
369{
370 uint32_t magic[BOOT_MAGIC_SZ];
371 uint32_t off;
372 const struct flash_area *fap;
373 int rc;
374
375 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
376 if (rc != 0) {
377 return BOOT_EFLASH;
378 }
379
380 off = boot_magic_off(fap);
381 rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ);
382 if (rc != 0) {
383 rc = BOOT_EFLASH;
384 goto out;
385 }
386
387 if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) != 0) {
388 /*
389 * If Slot 0 's magic is not valid, try scratch...
390 */
391
392 flash_area_close(fap);
393
394 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap);
395 if (rc != 0) {
396 return BOOT_EFLASH;
397 }
398
399 off = boot_magic_off(fap);
400 rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ);
401 if (rc != 0) {
402 rc = BOOT_EFLASH;
403 goto out;
404 }
405
406 assert(memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0);
407 }
408
409 off = boot_enc_key_off(fap, slot);
410 rc = flash_area_read(fap, off, enckey, BOOT_ENC_KEY_SIZE);
411 if (rc != 0) {
412 rc = BOOT_EFLASH;
413 }
414
415out:
416 flash_area_close(fap);
417 return rc;
418}
419#endif
Fabio Utzig46490722017-09-04 15:34:32 -0300420
421int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800422boot_write_magic(const struct flash_area *fap)
423{
424 uint32_t off;
425 int rc;
426
427 off = boot_magic_off(fap);
428
Fabio Utzig24a273d2017-04-20 08:21:31 -0300429 rc = flash_area_write(fap, off, boot_img_magic, BOOT_MAGIC_SZ);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800430 if (rc != 0) {
431 return BOOT_EFLASH;
432 }
433
434 return 0;
435}
436
Fabio Utzig2473ac02017-05-02 12:45:02 -0300437static int
438boot_write_flag(int flag, const struct flash_area *fap)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800439{
440 uint32_t off;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800441 int rc;
Fabio Utzig644b8d42017-04-20 07:56:05 -0300442 uint8_t buf[BOOT_MAX_ALIGN];
David Brown9d725462017-01-23 15:50:58 -0700443 uint8_t align;
Fabio Utzig39000012018-07-30 12:40:20 -0300444 uint8_t erased_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800445
Fabio Utzig2473ac02017-05-02 12:45:02 -0300446 switch (flag) {
447 case BOOT_FLAG_COPY_DONE:
448 off = boot_copy_done_off(fap);
449 break;
450 case BOOT_FLAG_IMAGE_OK:
451 off = boot_image_ok_off(fap);
452 break;
453 default:
Fabio Utzig856f7832017-05-22 11:04:44 -0400454 return BOOT_EBADARGS;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300455 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800456
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200457 align = flash_area_align(fap);
Fabio Utzig644b8d42017-04-20 07:56:05 -0300458 assert(align <= BOOT_MAX_ALIGN);
Fabio Utzig39000012018-07-30 12:40:20 -0300459 erased_val = flash_area_erased_val(fap);
460 memset(buf, erased_val, BOOT_MAX_ALIGN);
Fabio Utzigde8a38a2017-05-23 11:15:01 -0400461 buf[0] = BOOT_FLAG_SET;
David Brown9d725462017-01-23 15:50:58 -0700462
463 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800464 if (rc != 0) {
465 return BOOT_EFLASH;
466 }
467
468 return 0;
469}
470
471int
Fabio Utzig2473ac02017-05-02 12:45:02 -0300472boot_write_copy_done(const struct flash_area *fap)
473{
474 return boot_write_flag(BOOT_FLAG_COPY_DONE, fap);
475}
476
477int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800478boot_write_image_ok(const struct flash_area *fap)
479{
Fabio Utzig2473ac02017-05-02 12:45:02 -0300480 return boot_write_flag(BOOT_FLAG_IMAGE_OK, fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800481}
482
483int
Fabio Utzig46490722017-09-04 15:34:32 -0300484boot_write_swap_size(const struct flash_area *fap, uint32_t swap_size)
485{
486 uint32_t off;
487 int rc;
488 uint8_t buf[BOOT_MAX_ALIGN];
489 uint8_t align;
Fabio Utzig39000012018-07-30 12:40:20 -0300490 uint8_t erased_val;
Fabio Utzig46490722017-09-04 15:34:32 -0300491
492 off = boot_swap_size_off(fap);
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200493 align = flash_area_align(fap);
Fabio Utzig46490722017-09-04 15:34:32 -0300494 assert(align <= BOOT_MAX_ALIGN);
495 if (align < sizeof swap_size) {
496 align = sizeof swap_size;
497 }
Fabio Utzig39000012018-07-30 12:40:20 -0300498 erased_val = flash_area_erased_val(fap);
499 memset(buf, erased_val, BOOT_MAX_ALIGN);
Fabio Utzig46490722017-09-04 15:34:32 -0300500 memcpy(buf, (uint8_t *)&swap_size, sizeof swap_size);
501
502 rc = flash_area_write(fap, off, buf, align);
503 if (rc != 0) {
504 return BOOT_EFLASH;
505 }
506
507 return 0;
508}
509
Fabio Utzigba829042018-09-18 08:29:34 -0300510#ifdef MCUBOOT_ENC_IMAGES
511int
512boot_write_enc_key(const struct flash_area *fap, uint8_t slot, const uint8_t *enckey)
513{
514 uint32_t off;
515 int rc;
516
517 off = boot_enc_key_off(fap, slot);
518 rc = flash_area_write(fap, off, enckey, BOOT_ENC_KEY_SIZE);
519 if (rc != 0) {
520 return BOOT_EFLASH;
521 }
522
523 return 0;
524}
525#endif
526
Fabio Utzig46490722017-09-04 15:34:32 -0300527int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800528boot_swap_type(void)
529{
530 const struct boot_swap_table *table;
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300531 struct boot_swap_state slot0;
532 struct boot_swap_state slot1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800533 int rc;
Fabio Utzigcd5774b2017-11-29 10:18:26 -0200534 size_t i;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800535
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300536 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_0, &slot0);
537 if (rc) {
538 return BOOT_SWAP_TYPE_PANIC;
539 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800540
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300541 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_1, &slot1);
542 if (rc) {
543 return BOOT_SWAP_TYPE_PANIC;
544 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800545
546 for (i = 0; i < BOOT_SWAP_TABLES_COUNT; i++) {
547 table = boot_swap_tables + i;
548
Fabio Utzig39000012018-07-30 12:40:20 -0300549 if ((table->magic_slot0 == BOOT_MAGIC_ANY ||
550 table->magic_slot0 == slot0.magic) &&
551 (table->magic_slot1 == BOOT_MAGIC_ANY ||
552 table->magic_slot1 == slot1.magic) &&
553 (table->image_ok_slot0 == BOOT_FLAG_ANY ||
554 table->image_ok_slot0 == slot0.image_ok) &&
555 (table->image_ok_slot1 == BOOT_FLAG_ANY ||
556 table->image_ok_slot1 == slot1.image_ok) &&
557 (table->copy_done_slot0 == BOOT_FLAG_ANY ||
558 table->copy_done_slot0 == slot0.copy_done)) {
Fabio Utzig34e393e2017-05-22 11:07:07 -0400559 BOOT_LOG_INF("Swap type: %s",
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300560 table->swap_type == BOOT_SWAP_TYPE_TEST ? "test" :
561 table->swap_type == BOOT_SWAP_TYPE_PERM ? "perm" :
562 table->swap_type == BOOT_SWAP_TYPE_REVERT ? "revert" :
563 "BUG; can't happen");
564 assert(table->swap_type == BOOT_SWAP_TYPE_TEST ||
565 table->swap_type == BOOT_SWAP_TYPE_PERM ||
566 table->swap_type == BOOT_SWAP_TYPE_REVERT);
567 return table->swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800568 }
569 }
570
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300571 BOOT_LOG_INF("Swap type: none");
Christopher Collins92ea77f2016-12-12 15:59:26 -0800572 return BOOT_SWAP_TYPE_NONE;
573}
574
575/**
576 * Marks the image in slot 1 as pending. On the next reboot, the system will
577 * perform a one-time boot of the slot 1 image.
578 *
Christopher Collins7835c1e2016-12-21 10:10:51 -0800579 * @param permanent Whether the image should be used permanently or
580 * only tested once:
581 * 0=run image once, then confirm or revert.
582 * 1=run image forever.
583 *
Christopher Collins92ea77f2016-12-12 15:59:26 -0800584 * @return 0 on success; nonzero on failure.
585 */
586int
Christopher Collins7835c1e2016-12-21 10:10:51 -0800587boot_set_pending(int permanent)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800588{
589 const struct flash_area *fap;
590 struct boot_swap_state state_slot1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800591 int rc;
592
Fabio Utzig2473ac02017-05-02 12:45:02 -0300593 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_1, &state_slot1);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800594 if (rc != 0) {
595 return rc;
596 }
597
598 switch (state_slot1.magic) {
599 case BOOT_MAGIC_GOOD:
600 /* Swap already scheduled. */
601 return 0;
602
603 case BOOT_MAGIC_UNSET:
Fabio Utzig2473ac02017-05-02 12:45:02 -0300604 rc = flash_area_open(FLASH_AREA_IMAGE_1, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800605 if (rc != 0) {
606 rc = BOOT_EFLASH;
607 } else {
608 rc = boot_write_magic(fap);
609 }
610
Christopher Collins7835c1e2016-12-21 10:10:51 -0800611 if (rc == 0 && permanent) {
612 rc = boot_write_image_ok(fap);
613 }
614
Christopher Collins92ea77f2016-12-12 15:59:26 -0800615 flash_area_close(fap);
616 return rc;
617
Christopher Collinsae01f152019-01-30 09:56:37 -0800618 case BOOT_MAGIC_BAD:
619 /* The image slot is corrupt. There is no way to recover, so erase the
620 * slot to allow future upgrades.
621 */
622 rc = flash_area_open(FLASH_AREA_IMAGE_1, &fap);
623 if (rc != 0) {
624 return BOOT_EFLASH;
625 }
626
627 flash_area_erase(fap, 0, fap->fa_size);
628 flash_area_close(fap);
629 return BOOT_EBADIMAGE;
630
Christopher Collins92ea77f2016-12-12 15:59:26 -0800631 default:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800632 assert(0);
Christopher Collinsae01f152019-01-30 09:56:37 -0800633 return BOOT_EBADIMAGE;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800634 }
635}
636
637/**
638 * 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.
639 *
640 * @return 0 on success; nonzero on failure.
641 */
642int
643boot_set_confirmed(void)
644{
645 const struct flash_area *fap;
646 struct boot_swap_state state_slot0;
647 int rc;
648
Fabio Utzig2473ac02017-05-02 12:45:02 -0300649 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_0, &state_slot0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800650 if (rc != 0) {
651 return rc;
652 }
653
654 switch (state_slot0.magic) {
655 case BOOT_MAGIC_GOOD:
656 /* Confirm needed; proceed. */
657 break;
658
659 case BOOT_MAGIC_UNSET:
660 /* Already confirmed. */
661 return 0;
662
663 case BOOT_MAGIC_BAD:
664 /* Unexpected state. */
665 return BOOT_EBADVECT;
666 }
667
Christopher Collins92ea77f2016-12-12 15:59:26 -0800668 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
669 if (rc) {
670 rc = BOOT_EFLASH;
671 goto done;
672 }
673
Fabio Utzig08fa2672018-09-26 12:16:18 -0300674 if (state_slot0.copy_done == BOOT_FLAG_UNSET) {
Fabio Utzig39000012018-07-30 12:40:20 -0300675 /* Swap never completed. This is unexpected. */
676 rc = BOOT_EBADVECT;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800677 goto done;
678 }
679
Ɓukasz Rymanowskia1927f42018-09-26 15:31:50 +0200680 if (state_slot0.image_ok != BOOT_FLAG_UNSET) {
Fabio Utzig39000012018-07-30 12:40:20 -0300681 /* Already confirmed. */
682 goto done;
683 }
684
685 rc = boot_write_image_ok(fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800686
687done:
688 flash_area_close(fap);
689 return rc;
690}