blob: 0ec86059524dd0f5a8936936260ca5ca33e94af2 [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"
28#include "flash_map/flash_map.h"
29#include "os/os.h"
30#include "bootutil/image.h"
31#include "bootutil/bootutil.h"
32#include "bootutil_priv.h"
33
Fabio Utzig7ebb7c22017-04-26 10:59:31 -030034#define BOOT_LOG_LEVEL BOOT_LOG_LEVEL_INFO
35#include "bootutil/bootutil_log.h"
36
Christopher Collins92ea77f2016-12-12 15:59:26 -080037int boot_current_slot;
38
Fabio Utzig24a273d2017-04-20 08:21:31 -030039const uint32_t boot_img_magic[] = {
Christopher Collins92ea77f2016-12-12 15:59:26 -080040 0xf395c277,
41 0x7fefd260,
42 0x0f505235,
43 0x8079b62c,
44};
45
Fabio Utzig24a273d2017-04-20 08:21:31 -030046const uint32_t BOOT_MAGIC_SZ = sizeof boot_img_magic;
Fabio Utziga0bc9b52017-06-28 09:19:55 -030047const uint32_t BOOT_MAX_ALIGN = MAX_FLASH_ALIGN;
Fabio Utzig24a273d2017-04-20 08:21:31 -030048
Christopher Collins92ea77f2016-12-12 15:59:26 -080049struct boot_swap_table {
50 /** * For each field, a value of 0 means "any". */
Fabio Utzigb5b2f552017-06-30 10:03:47 -030051 uint8_t magic_slot0;
52 uint8_t magic_slot1;
53 uint8_t image_ok_slot0;
54 uint8_t image_ok_slot1;
Fabio Utzigd7d20752017-07-13 16:03:25 -030055 uint8_t copy_done_slot0;
Christopher Collins92ea77f2016-12-12 15:59:26 -080056
Fabio Utzigb5b2f552017-06-30 10:03:47 -030057 uint8_t swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -080058};
59
60/**
61 * This set of tables maps image trailer contents to swap operation type.
62 * When searching for a match, these tables must be iterated sequentially.
Fabio Utzigb5b2f552017-06-30 10:03:47 -030063 *
64 * NOTE: the table order is very important. The settings in Slot 1 always
65 * are priority to Slot 0 and should be located earlier in the table.
66 *
67 * The table lists only states where there is action needs to be taken by
68 * the bootloader, as in starting/finishing a swap operation.
Christopher Collins92ea77f2016-12-12 15:59:26 -080069 */
70static const struct boot_swap_table boot_swap_tables[] = {
71 {
Fabio Utzigb5b2f552017-06-30 10:03:47 -030072 .magic_slot0 = 0,
73 .magic_slot1 = BOOT_MAGIC_GOOD,
74 .image_ok_slot0 = 0,
75 .image_ok_slot1 = 0xff,
Fabio Utzigd7d20752017-07-13 16:03:25 -030076 .copy_done_slot0 = 0,
Fabio Utzigb5b2f552017-06-30 10:03:47 -030077 .swap_type = BOOT_SWAP_TYPE_TEST,
Christopher Collins92ea77f2016-12-12 15:59:26 -080078 },
Christopher Collins92ea77f2016-12-12 15:59:26 -080079 {
Fabio Utzigb5b2f552017-06-30 10:03:47 -030080 .magic_slot0 = 0,
81 .magic_slot1 = BOOT_MAGIC_GOOD,
82 .image_ok_slot0 = 0,
83 .image_ok_slot1 = 0x01,
Fabio Utzigd7d20752017-07-13 16:03:25 -030084 .copy_done_slot0 = 0,
Fabio Utzigb5b2f552017-06-30 10:03:47 -030085 .swap_type = BOOT_SWAP_TYPE_PERM,
Christopher Collins92ea77f2016-12-12 15:59:26 -080086 },
Christopher Collins92ea77f2016-12-12 15:59:26 -080087 {
Fabio Utzigb5b2f552017-06-30 10:03:47 -030088 .magic_slot0 = BOOT_MAGIC_GOOD,
89 .magic_slot1 = BOOT_MAGIC_UNSET,
90 .image_ok_slot0 = 0xff,
91 .image_ok_slot1 = 0,
Fabio Utzigd7d20752017-07-13 16:03:25 -030092 .copy_done_slot0 = 0x01,
Fabio Utzigb5b2f552017-06-30 10:03:47 -030093 .swap_type = BOOT_SWAP_TYPE_REVERT,
Christopher Collins92ea77f2016-12-12 15:59:26 -080094 },
95};
96
97#define BOOT_SWAP_TABLES_COUNT \
98 (sizeof boot_swap_tables / sizeof boot_swap_tables[0])
99
100int
101boot_magic_code(const uint32_t *magic)
102{
Fabio Utzigcd5774b2017-11-29 10:18:26 -0200103 size_t i;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800104
Fabio Utzig24a273d2017-04-20 08:21:31 -0300105 if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800106 return BOOT_MAGIC_GOOD;
107 }
108
Fabio Utzig24a273d2017-04-20 08:21:31 -0300109 for (i = 0; i < BOOT_MAGIC_SZ / sizeof *magic; i++) {
110 if (magic[i] != 0xffffffff) {
111 return BOOT_MAGIC_BAD;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800112 }
113 }
114
Fabio Utzig24a273d2017-04-20 08:21:31 -0300115 return BOOT_MAGIC_UNSET;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800116}
117
118uint32_t
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300119boot_slots_trailer_sz(uint8_t min_write_sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800120{
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300121 return /* state for all sectors */
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300122 BOOT_STATUS_MAX_ENTRIES * BOOT_STATUS_STATE_COUNT * min_write_sz +
Fabio Utzig46490722017-09-04 15:34:32 -0300123 BOOT_MAX_ALIGN * 3 /* copy_done + image_ok + swap_size */ +
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300124 BOOT_MAGIC_SZ;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800125}
126
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300127static uint32_t
128boot_scratch_trailer_sz(uint8_t min_write_sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800129{
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300130 return BOOT_STATUS_STATE_COUNT * min_write_sz + /* state for one sector */
Fabio Utzig46490722017-09-04 15:34:32 -0300131 BOOT_MAX_ALIGN * 2 + /* image_ok + swap_size */
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300132 BOOT_MAGIC_SZ;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800133}
134
135static uint32_t
136boot_magic_off(const struct flash_area *fap)
137{
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300138 assert(offsetof(struct image_trailer, magic) == 16);
139 return fap->fa_size - BOOT_MAGIC_SZ;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800140}
141
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400142int
143boot_status_entries(const struct flash_area *fap)
144{
145 switch (fap->fa_id) {
146 case FLASH_AREA_IMAGE_0:
147 case FLASH_AREA_IMAGE_1:
148 return BOOT_STATUS_STATE_COUNT * BOOT_STATUS_MAX_ENTRIES;
149 case FLASH_AREA_IMAGE_SCRATCH:
150 return BOOT_STATUS_STATE_COUNT;
151 default:
152 return BOOT_EBADARGS;
153 }
154}
155
Christopher Collins92ea77f2016-12-12 15:59:26 -0800156uint32_t
157boot_status_off(const struct flash_area *fap)
158{
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300159 uint32_t off_from_end;
160 uint8_t elem_sz;
161
162 elem_sz = flash_area_align(fap);
163
164 if (fap->fa_id == FLASH_AREA_IMAGE_SCRATCH) {
165 off_from_end = boot_scratch_trailer_sz(elem_sz);
166 } else {
167 off_from_end = boot_slots_trailer_sz(elem_sz);
168 }
169
170 assert(off_from_end <= fap->fa_size);
171 return fap->fa_size - off_from_end;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800172}
173
174static uint32_t
175boot_copy_done_off(const struct flash_area *fap)
176{
Fabio Utzig2473ac02017-05-02 12:45:02 -0300177 assert(fap->fa_id != FLASH_AREA_IMAGE_SCRATCH);
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300178 assert(offsetof(struct image_trailer, copy_done) == 0);
179 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 2;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800180}
181
182static uint32_t
183boot_image_ok_off(const struct flash_area *fap)
184{
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300185 assert(offsetof(struct image_trailer, image_ok) == 8);
186 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800187}
188
Fabio Utzig46490722017-09-04 15:34:32 -0300189static uint32_t
190boot_swap_size_off(const struct flash_area *fap)
191{
192 /*
193 * The "swap_size" field if located just before the trailer.
194 * The scratch slot doesn't store "copy_done"...
195 */
196 if (fap->fa_id == FLASH_AREA_IMAGE_SCRATCH) {
197 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 2;
198 }
199
200 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 3;
201}
202
Christopher Collins92ea77f2016-12-12 15:59:26 -0800203int
204boot_read_swap_state(const struct flash_area *fap,
205 struct boot_swap_state *state)
206{
Fabio Utzig24a273d2017-04-20 08:21:31 -0300207 uint32_t magic[BOOT_MAGIC_SZ];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800208 uint32_t off;
209 int rc;
210
211 off = boot_magic_off(fap);
Fabio Utzig24a273d2017-04-20 08:21:31 -0300212 rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800213 if (rc != 0) {
214 return BOOT_EFLASH;
215 }
216 state->magic = boot_magic_code(magic);
217
Fabio Utzig2473ac02017-05-02 12:45:02 -0300218 if (fap->fa_id != FLASH_AREA_IMAGE_SCRATCH) {
219 off = boot_copy_done_off(fap);
Fabio Utzig46490722017-09-04 15:34:32 -0300220 rc = flash_area_read(fap, off, &state->copy_done, sizeof state->copy_done);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300221 if (rc != 0) {
222 return BOOT_EFLASH;
223 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800224 }
225
226 off = boot_image_ok_off(fap);
Fabio Utzig46490722017-09-04 15:34:32 -0300227 rc = flash_area_read(fap, off, &state->image_ok, sizeof state->image_ok);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800228 if (rc != 0) {
229 return BOOT_EFLASH;
230 }
231
232 return 0;
233}
234
235/**
236 * Reads the image trailer from the scratch area.
237 */
238int
Fabio Utzig2473ac02017-05-02 12:45:02 -0300239boot_read_swap_state_by_id(int flash_area_id, struct boot_swap_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800240{
241 const struct flash_area *fap;
242 int rc;
243
Fabio Utzig2473ac02017-05-02 12:45:02 -0300244 switch (flash_area_id) {
245 case FLASH_AREA_IMAGE_SCRATCH:
246 case FLASH_AREA_IMAGE_0:
247 case FLASH_AREA_IMAGE_1:
248 rc = flash_area_open(flash_area_id, &fap);
249 if (rc != 0) {
250 return BOOT_EFLASH;
251 }
252 break;
253 default:
Fabio Utzig856f7832017-05-22 11:04:44 -0400254 return BOOT_EBADARGS;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300255 }
256
257 rc = boot_read_swap_state(fap, state);
Fabio Utzigacfba2e2017-05-22 11:06:29 -0400258 flash_area_close(fap);
259 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800260}
261
262int
Fabio Utzig46490722017-09-04 15:34:32 -0300263boot_read_swap_size(uint32_t *swap_size)
264{
265 uint32_t magic[BOOT_MAGIC_SZ];
266 uint32_t off;
267 const struct flash_area *fap;
268 int rc;
269
270 /*
271 * In the middle a swap, tries to locate the saved swap size. Looks
272 * for a valid magic, first on Slot 0, then on scratch. Both "slots"
273 * can end up being temporary storage for a swap and it is assumed
274 * that if magic is valid then swap size is too, because magic is
275 * always written in the last step.
276 */
277
278 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
279 if (rc != 0) {
280 return BOOT_EFLASH;
281 }
282
283 off = boot_magic_off(fap);
284 rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ);
285 if (rc != 0) {
286 rc = BOOT_EFLASH;
287 goto out;
288 }
289
290 if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) != 0) {
291 /*
292 * If Slot 0 's magic is not valid, try scratch...
293 */
294
295 flash_area_close(fap);
296
297 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap);
298 if (rc != 0) {
299 return BOOT_EFLASH;
300 }
301
302 off = boot_magic_off(fap);
303 rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ);
304 if (rc != 0) {
305 rc = BOOT_EFLASH;
306 goto out;
307 }
308
309 assert(memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0);
310 }
311
312 off = boot_swap_size_off(fap);
313 rc = flash_area_read(fap, off, swap_size, sizeof *swap_size);
314 if (rc != 0) {
315 rc = BOOT_EFLASH;
316 }
317
318out:
319 flash_area_close(fap);
320 return rc;
321}
322
323
324int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800325boot_write_magic(const struct flash_area *fap)
326{
327 uint32_t off;
328 int rc;
329
330 off = boot_magic_off(fap);
331
Fabio Utzig24a273d2017-04-20 08:21:31 -0300332 rc = flash_area_write(fap, off, boot_img_magic, BOOT_MAGIC_SZ);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800333 if (rc != 0) {
334 return BOOT_EFLASH;
335 }
336
337 return 0;
338}
339
Fabio Utzig2473ac02017-05-02 12:45:02 -0300340static int
341boot_write_flag(int flag, const struct flash_area *fap)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800342{
343 uint32_t off;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800344 int rc;
Fabio Utzig644b8d42017-04-20 07:56:05 -0300345 uint8_t buf[BOOT_MAX_ALIGN];
David Brown9d725462017-01-23 15:50:58 -0700346 uint8_t align;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800347
Fabio Utzig2473ac02017-05-02 12:45:02 -0300348 switch (flag) {
349 case BOOT_FLAG_COPY_DONE:
350 off = boot_copy_done_off(fap);
351 break;
352 case BOOT_FLAG_IMAGE_OK:
353 off = boot_image_ok_off(fap);
354 break;
355 default:
Fabio Utzig856f7832017-05-22 11:04:44 -0400356 return BOOT_EBADARGS;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300357 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800358
David Brown9d725462017-01-23 15:50:58 -0700359 align = hal_flash_align(fap->fa_device_id);
Fabio Utzig644b8d42017-04-20 07:56:05 -0300360 assert(align <= BOOT_MAX_ALIGN);
361 memset(buf, 0xFF, BOOT_MAX_ALIGN);
Fabio Utzigde8a38a2017-05-23 11:15:01 -0400362 buf[0] = BOOT_FLAG_SET;
David Brown9d725462017-01-23 15:50:58 -0700363
364 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800365 if (rc != 0) {
366 return BOOT_EFLASH;
367 }
368
369 return 0;
370}
371
372int
Fabio Utzig2473ac02017-05-02 12:45:02 -0300373boot_write_copy_done(const struct flash_area *fap)
374{
375 return boot_write_flag(BOOT_FLAG_COPY_DONE, fap);
376}
377
378int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800379boot_write_image_ok(const struct flash_area *fap)
380{
Fabio Utzig2473ac02017-05-02 12:45:02 -0300381 return boot_write_flag(BOOT_FLAG_IMAGE_OK, fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800382}
383
384int
Fabio Utzig46490722017-09-04 15:34:32 -0300385boot_write_swap_size(const struct flash_area *fap, uint32_t swap_size)
386{
387 uint32_t off;
388 int rc;
389 uint8_t buf[BOOT_MAX_ALIGN];
390 uint8_t align;
391
392 off = boot_swap_size_off(fap);
393 align = hal_flash_align(fap->fa_device_id);
394 assert(align <= BOOT_MAX_ALIGN);
395 if (align < sizeof swap_size) {
396 align = sizeof swap_size;
397 }
398 memset(buf, 0xFF, BOOT_MAX_ALIGN);
399 memcpy(buf, (uint8_t *)&swap_size, sizeof swap_size);
400
401 rc = flash_area_write(fap, off, buf, align);
402 if (rc != 0) {
403 return BOOT_EFLASH;
404 }
405
406 return 0;
407}
408
409int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800410boot_swap_type(void)
411{
412 const struct boot_swap_table *table;
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300413 struct boot_swap_state slot0;
414 struct boot_swap_state slot1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800415 int rc;
Fabio Utzigcd5774b2017-11-29 10:18:26 -0200416 size_t i;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800417
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300418 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_0, &slot0);
419 if (rc) {
420 return BOOT_SWAP_TYPE_PANIC;
421 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800422
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300423 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_1, &slot1);
424 if (rc) {
425 return BOOT_SWAP_TYPE_PANIC;
426 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800427
428 for (i = 0; i < BOOT_SWAP_TABLES_COUNT; i++) {
429 table = boot_swap_tables + i;
430
Fabio Utzigd7d20752017-07-13 16:03:25 -0300431 if ((!table->magic_slot0 || table->magic_slot0 == slot0.magic ) &&
432 (!table->magic_slot1 || table->magic_slot1 == slot1.magic ) &&
433 (!table->image_ok_slot0 || table->image_ok_slot0 == slot0.image_ok ) &&
434 (!table->image_ok_slot1 || table->image_ok_slot1 == slot1.image_ok ) &&
435 (!table->copy_done_slot0 || table->copy_done_slot0 == slot0.copy_done)) {
Fabio Utzig34e393e2017-05-22 11:07:07 -0400436 BOOT_LOG_INF("Swap type: %s",
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300437 table->swap_type == BOOT_SWAP_TYPE_TEST ? "test" :
438 table->swap_type == BOOT_SWAP_TYPE_PERM ? "perm" :
439 table->swap_type == BOOT_SWAP_TYPE_REVERT ? "revert" :
440 "BUG; can't happen");
441 assert(table->swap_type == BOOT_SWAP_TYPE_TEST ||
442 table->swap_type == BOOT_SWAP_TYPE_PERM ||
443 table->swap_type == BOOT_SWAP_TYPE_REVERT);
444 return table->swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800445 }
446 }
447
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300448 BOOT_LOG_INF("Swap type: none");
Christopher Collins92ea77f2016-12-12 15:59:26 -0800449 return BOOT_SWAP_TYPE_NONE;
450}
451
452/**
453 * Marks the image in slot 1 as pending. On the next reboot, the system will
454 * perform a one-time boot of the slot 1 image.
455 *
Christopher Collins7835c1e2016-12-21 10:10:51 -0800456 * @param permanent Whether the image should be used permanently or
457 * only tested once:
458 * 0=run image once, then confirm or revert.
459 * 1=run image forever.
460 *
Christopher Collins92ea77f2016-12-12 15:59:26 -0800461 * @return 0 on success; nonzero on failure.
462 */
463int
Christopher Collins7835c1e2016-12-21 10:10:51 -0800464boot_set_pending(int permanent)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800465{
466 const struct flash_area *fap;
467 struct boot_swap_state state_slot1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800468 int rc;
469
Fabio Utzig2473ac02017-05-02 12:45:02 -0300470 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_1, &state_slot1);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800471 if (rc != 0) {
472 return rc;
473 }
474
475 switch (state_slot1.magic) {
476 case BOOT_MAGIC_GOOD:
477 /* Swap already scheduled. */
478 return 0;
479
480 case BOOT_MAGIC_UNSET:
Fabio Utzig2473ac02017-05-02 12:45:02 -0300481 rc = flash_area_open(FLASH_AREA_IMAGE_1, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800482 if (rc != 0) {
483 rc = BOOT_EFLASH;
484 } else {
485 rc = boot_write_magic(fap);
486 }
487
Christopher Collins7835c1e2016-12-21 10:10:51 -0800488 if (rc == 0 && permanent) {
489 rc = boot_write_image_ok(fap);
490 }
491
Christopher Collins92ea77f2016-12-12 15:59:26 -0800492 flash_area_close(fap);
493 return rc;
494
495 default:
496 /* XXX: Temporary assert. */
497 assert(0);
498 return -1;
499 }
500}
501
502/**
503 * 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.
504 *
505 * @return 0 on success; nonzero on failure.
506 */
507int
508boot_set_confirmed(void)
509{
510 const struct flash_area *fap;
511 struct boot_swap_state state_slot0;
512 int rc;
513
Fabio Utzig2473ac02017-05-02 12:45:02 -0300514 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_0, &state_slot0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800515 if (rc != 0) {
516 return rc;
517 }
518
519 switch (state_slot0.magic) {
520 case BOOT_MAGIC_GOOD:
521 /* Confirm needed; proceed. */
522 break;
523
524 case BOOT_MAGIC_UNSET:
525 /* Already confirmed. */
526 return 0;
527
528 case BOOT_MAGIC_BAD:
529 /* Unexpected state. */
530 return BOOT_EBADVECT;
531 }
532
Fabio Utzigde8a38a2017-05-23 11:15:01 -0400533 if (state_slot0.copy_done == BOOT_FLAG_UNSET) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800534 /* Swap never completed. This is unexpected. */
535 return BOOT_EBADVECT;
536 }
537
Fabio Utzigde8a38a2017-05-23 11:15:01 -0400538 if (state_slot0.image_ok != BOOT_FLAG_UNSET) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800539 /* Already confirmed. */
540 return 0;
541 }
542
543 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
544 if (rc) {
545 rc = BOOT_EFLASH;
546 goto done;
547 }
548
549 rc = boot_write_image_ok(fap);
550 if (rc != 0) {
551 goto done;
552 }
553
554 rc = 0;
555
556done:
557 flash_area_close(fap);
558 return rc;
559}