blob: 0b8b6717688fa0f95cd3e195bbc5e0afc1155be8 [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"
35
Fabio Utzig7ebb7c22017-04-26 10:59:31 -030036#define BOOT_LOG_LEVEL BOOT_LOG_LEVEL_INFO
37#include "bootutil/bootutil_log.h"
38
Christopher Collins92ea77f2016-12-12 15:59:26 -080039int boot_current_slot;
40
Fabio Utzig24a273d2017-04-20 08:21:31 -030041const uint32_t boot_img_magic[] = {
Christopher Collins92ea77f2016-12-12 15:59:26 -080042 0xf395c277,
43 0x7fefd260,
44 0x0f505235,
45 0x8079b62c,
46};
47
Fabio Utzig24a273d2017-04-20 08:21:31 -030048const uint32_t BOOT_MAGIC_SZ = sizeof boot_img_magic;
Fabio Utziga0bc9b52017-06-28 09:19:55 -030049const uint32_t BOOT_MAX_ALIGN = MAX_FLASH_ALIGN;
Fabio Utzig24a273d2017-04-20 08:21:31 -030050
Christopher Collins92ea77f2016-12-12 15:59:26 -080051struct boot_swap_table {
52 /** * For each field, a value of 0 means "any". */
Fabio Utzigb5b2f552017-06-30 10:03:47 -030053 uint8_t magic_slot0;
54 uint8_t magic_slot1;
55 uint8_t image_ok_slot0;
56 uint8_t image_ok_slot1;
Fabio Utzigd7d20752017-07-13 16:03:25 -030057 uint8_t copy_done_slot0;
Christopher Collins92ea77f2016-12-12 15:59:26 -080058
Fabio Utzigb5b2f552017-06-30 10:03:47 -030059 uint8_t swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -080060};
61
62/**
63 * This set of tables maps image trailer contents to swap operation type.
64 * When searching for a match, these tables must be iterated sequentially.
Fabio Utzigb5b2f552017-06-30 10:03:47 -030065 *
66 * NOTE: the table order is very important. The settings in Slot 1 always
67 * are priority to Slot 0 and should be located earlier in the table.
68 *
69 * The table lists only states where there is action needs to be taken by
70 * the bootloader, as in starting/finishing a swap operation.
Christopher Collins92ea77f2016-12-12 15:59:26 -080071 */
72static const struct boot_swap_table boot_swap_tables[] = {
73 {
Fabio Utzigb5b2f552017-06-30 10:03:47 -030074 .magic_slot0 = 0,
75 .magic_slot1 = BOOT_MAGIC_GOOD,
76 .image_ok_slot0 = 0,
77 .image_ok_slot1 = 0xff,
Fabio Utzigd7d20752017-07-13 16:03:25 -030078 .copy_done_slot0 = 0,
Fabio Utzigb5b2f552017-06-30 10:03:47 -030079 .swap_type = BOOT_SWAP_TYPE_TEST,
Christopher Collins92ea77f2016-12-12 15:59:26 -080080 },
Christopher Collins92ea77f2016-12-12 15:59:26 -080081 {
Fabio Utzigb5b2f552017-06-30 10:03:47 -030082 .magic_slot0 = 0,
83 .magic_slot1 = BOOT_MAGIC_GOOD,
84 .image_ok_slot0 = 0,
85 .image_ok_slot1 = 0x01,
Fabio Utzigd7d20752017-07-13 16:03:25 -030086 .copy_done_slot0 = 0,
Fabio Utzigb5b2f552017-06-30 10:03:47 -030087 .swap_type = BOOT_SWAP_TYPE_PERM,
Christopher Collins92ea77f2016-12-12 15:59:26 -080088 },
Christopher Collins92ea77f2016-12-12 15:59:26 -080089 {
Fabio Utzigb5b2f552017-06-30 10:03:47 -030090 .magic_slot0 = BOOT_MAGIC_GOOD,
91 .magic_slot1 = BOOT_MAGIC_UNSET,
92 .image_ok_slot0 = 0xff,
93 .image_ok_slot1 = 0,
Fabio Utzigd7d20752017-07-13 16:03:25 -030094 .copy_done_slot0 = 0x01,
Fabio Utzigb5b2f552017-06-30 10:03:47 -030095 .swap_type = BOOT_SWAP_TYPE_REVERT,
Christopher Collins92ea77f2016-12-12 15:59:26 -080096 },
97};
98
99#define BOOT_SWAP_TABLES_COUNT \
100 (sizeof boot_swap_tables / sizeof boot_swap_tables[0])
101
102int
103boot_magic_code(const uint32_t *magic)
104{
Fabio Utzigcd5774b2017-11-29 10:18:26 -0200105 size_t i;
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 }
110
Fabio Utzig24a273d2017-04-20 08:21:31 -0300111 for (i = 0; i < BOOT_MAGIC_SZ / sizeof *magic; i++) {
112 if (magic[i] != 0xffffffff) {
113 return BOOT_MAGIC_BAD;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800114 }
115 }
116
Fabio Utzig24a273d2017-04-20 08:21:31 -0300117 return BOOT_MAGIC_UNSET;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800118}
119
120uint32_t
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300121boot_slots_trailer_sz(uint8_t min_write_sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800122{
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300123 return /* state for all sectors */
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300124 BOOT_STATUS_MAX_ENTRIES * BOOT_STATUS_STATE_COUNT * min_write_sz +
Fabio Utzig46490722017-09-04 15:34:32 -0300125 BOOT_MAX_ALIGN * 3 /* copy_done + image_ok + swap_size */ +
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300126 BOOT_MAGIC_SZ;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800127}
128
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300129static uint32_t
130boot_scratch_trailer_sz(uint8_t min_write_sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800131{
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300132 return BOOT_STATUS_STATE_COUNT * min_write_sz + /* state for one sector */
Fabio Utzig46490722017-09-04 15:34:32 -0300133 BOOT_MAX_ALIGN * 2 + /* image_ok + swap_size */
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300134 BOOT_MAGIC_SZ;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800135}
136
137static uint32_t
138boot_magic_off(const struct flash_area *fap)
139{
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300140 assert(offsetof(struct image_trailer, magic) == 16);
141 return fap->fa_size - BOOT_MAGIC_SZ;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800142}
143
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400144int
145boot_status_entries(const struct flash_area *fap)
146{
147 switch (fap->fa_id) {
148 case FLASH_AREA_IMAGE_0:
149 case FLASH_AREA_IMAGE_1:
150 return BOOT_STATUS_STATE_COUNT * BOOT_STATUS_MAX_ENTRIES;
151 case FLASH_AREA_IMAGE_SCRATCH:
152 return BOOT_STATUS_STATE_COUNT;
153 default:
154 return BOOT_EBADARGS;
155 }
156}
157
Christopher Collins92ea77f2016-12-12 15:59:26 -0800158uint32_t
159boot_status_off(const struct flash_area *fap)
160{
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300161 uint32_t off_from_end;
162 uint8_t elem_sz;
163
164 elem_sz = flash_area_align(fap);
165
166 if (fap->fa_id == FLASH_AREA_IMAGE_SCRATCH) {
167 off_from_end = boot_scratch_trailer_sz(elem_sz);
168 } else {
169 off_from_end = boot_slots_trailer_sz(elem_sz);
170 }
171
172 assert(off_from_end <= fap->fa_size);
173 return fap->fa_size - off_from_end;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800174}
175
176static uint32_t
177boot_copy_done_off(const struct flash_area *fap)
178{
Fabio Utzig2473ac02017-05-02 12:45:02 -0300179 assert(fap->fa_id != FLASH_AREA_IMAGE_SCRATCH);
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300180 assert(offsetof(struct image_trailer, copy_done) == 0);
181 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 2;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800182}
183
184static uint32_t
185boot_image_ok_off(const struct flash_area *fap)
186{
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300187 assert(offsetof(struct image_trailer, image_ok) == 8);
188 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800189}
190
Fabio Utzig46490722017-09-04 15:34:32 -0300191static uint32_t
192boot_swap_size_off(const struct flash_area *fap)
193{
194 /*
195 * The "swap_size" field if located just before the trailer.
196 * The scratch slot doesn't store "copy_done"...
197 */
198 if (fap->fa_id == FLASH_AREA_IMAGE_SCRATCH) {
199 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 2;
200 }
201
202 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 3;
203}
204
Christopher Collins92ea77f2016-12-12 15:59:26 -0800205int
206boot_read_swap_state(const struct flash_area *fap,
207 struct boot_swap_state *state)
208{
Fabio Utzig24a273d2017-04-20 08:21:31 -0300209 uint32_t magic[BOOT_MAGIC_SZ];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800210 uint32_t off;
211 int rc;
212
213 off = boot_magic_off(fap);
Fabio Utzig24a273d2017-04-20 08:21:31 -0300214 rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800215 if (rc != 0) {
216 return BOOT_EFLASH;
217 }
218 state->magic = boot_magic_code(magic);
219
Fabio Utzig2473ac02017-05-02 12:45:02 -0300220 if (fap->fa_id != FLASH_AREA_IMAGE_SCRATCH) {
221 off = boot_copy_done_off(fap);
Fabio Utzig46490722017-09-04 15:34:32 -0300222 rc = flash_area_read(fap, off, &state->copy_done, sizeof state->copy_done);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300223 if (rc != 0) {
224 return BOOT_EFLASH;
225 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800226 }
227
228 off = boot_image_ok_off(fap);
Fabio Utzig46490722017-09-04 15:34:32 -0300229 rc = flash_area_read(fap, off, &state->image_ok, sizeof state->image_ok);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800230 if (rc != 0) {
231 return BOOT_EFLASH;
232 }
233
234 return 0;
235}
236
237/**
238 * Reads the image trailer from the scratch area.
239 */
240int
Fabio Utzig2473ac02017-05-02 12:45:02 -0300241boot_read_swap_state_by_id(int flash_area_id, struct boot_swap_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800242{
243 const struct flash_area *fap;
244 int rc;
245
Fabio Utzig2473ac02017-05-02 12:45:02 -0300246 switch (flash_area_id) {
247 case FLASH_AREA_IMAGE_SCRATCH:
248 case FLASH_AREA_IMAGE_0:
249 case FLASH_AREA_IMAGE_1:
250 rc = flash_area_open(flash_area_id, &fap);
251 if (rc != 0) {
252 return BOOT_EFLASH;
253 }
254 break;
255 default:
Fabio Utzig856f7832017-05-22 11:04:44 -0400256 return BOOT_EBADARGS;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300257 }
258
259 rc = boot_read_swap_state(fap, state);
Fabio Utzigacfba2e2017-05-22 11:06:29 -0400260 flash_area_close(fap);
261 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800262}
263
264int
Fabio Utzig46490722017-09-04 15:34:32 -0300265boot_read_swap_size(uint32_t *swap_size)
266{
267 uint32_t magic[BOOT_MAGIC_SZ];
268 uint32_t off;
269 const struct flash_area *fap;
270 int rc;
271
272 /*
273 * In the middle a swap, tries to locate the saved swap size. Looks
274 * for a valid magic, first on Slot 0, then on scratch. Both "slots"
275 * can end up being temporary storage for a swap and it is assumed
276 * that if magic is valid then swap size is too, because magic is
277 * always written in the last step.
278 */
279
280 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
281 if (rc != 0) {
282 return BOOT_EFLASH;
283 }
284
285 off = boot_magic_off(fap);
286 rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ);
287 if (rc != 0) {
288 rc = BOOT_EFLASH;
289 goto out;
290 }
291
292 if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) != 0) {
293 /*
294 * If Slot 0 's magic is not valid, try scratch...
295 */
296
297 flash_area_close(fap);
298
299 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap);
300 if (rc != 0) {
301 return BOOT_EFLASH;
302 }
303
304 off = boot_magic_off(fap);
305 rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ);
306 if (rc != 0) {
307 rc = BOOT_EFLASH;
308 goto out;
309 }
310
311 assert(memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0);
312 }
313
314 off = boot_swap_size_off(fap);
315 rc = flash_area_read(fap, off, swap_size, sizeof *swap_size);
316 if (rc != 0) {
317 rc = BOOT_EFLASH;
318 }
319
320out:
321 flash_area_close(fap);
322 return rc;
323}
324
325
326int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800327boot_write_magic(const struct flash_area *fap)
328{
329 uint32_t off;
330 int rc;
331
332 off = boot_magic_off(fap);
333
Fabio Utzig24a273d2017-04-20 08:21:31 -0300334 rc = flash_area_write(fap, off, boot_img_magic, BOOT_MAGIC_SZ);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800335 if (rc != 0) {
336 return BOOT_EFLASH;
337 }
338
339 return 0;
340}
341
Fabio Utzig2473ac02017-05-02 12:45:02 -0300342static int
343boot_write_flag(int flag, const struct flash_area *fap)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800344{
345 uint32_t off;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800346 int rc;
Fabio Utzig644b8d42017-04-20 07:56:05 -0300347 uint8_t buf[BOOT_MAX_ALIGN];
David Brown9d725462017-01-23 15:50:58 -0700348 uint8_t align;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800349
Fabio Utzig2473ac02017-05-02 12:45:02 -0300350 switch (flag) {
351 case BOOT_FLAG_COPY_DONE:
352 off = boot_copy_done_off(fap);
353 break;
354 case BOOT_FLAG_IMAGE_OK:
355 off = boot_image_ok_off(fap);
356 break;
357 default:
Fabio Utzig856f7832017-05-22 11:04:44 -0400358 return BOOT_EBADARGS;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300359 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800360
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200361 align = flash_area_align(fap);
Fabio Utzig644b8d42017-04-20 07:56:05 -0300362 assert(align <= BOOT_MAX_ALIGN);
363 memset(buf, 0xFF, BOOT_MAX_ALIGN);
Fabio Utzigde8a38a2017-05-23 11:15:01 -0400364 buf[0] = BOOT_FLAG_SET;
David Brown9d725462017-01-23 15:50:58 -0700365
366 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800367 if (rc != 0) {
368 return BOOT_EFLASH;
369 }
370
371 return 0;
372}
373
374int
Fabio Utzig2473ac02017-05-02 12:45:02 -0300375boot_write_copy_done(const struct flash_area *fap)
376{
377 return boot_write_flag(BOOT_FLAG_COPY_DONE, fap);
378}
379
380int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800381boot_write_image_ok(const struct flash_area *fap)
382{
Fabio Utzig2473ac02017-05-02 12:45:02 -0300383 return boot_write_flag(BOOT_FLAG_IMAGE_OK, fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800384}
385
386int
Fabio Utzig46490722017-09-04 15:34:32 -0300387boot_write_swap_size(const struct flash_area *fap, uint32_t swap_size)
388{
389 uint32_t off;
390 int rc;
391 uint8_t buf[BOOT_MAX_ALIGN];
392 uint8_t align;
393
394 off = boot_swap_size_off(fap);
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200395 align = flash_area_align(fap);
Fabio Utzig46490722017-09-04 15:34:32 -0300396 assert(align <= BOOT_MAX_ALIGN);
397 if (align < sizeof swap_size) {
398 align = sizeof swap_size;
399 }
400 memset(buf, 0xFF, BOOT_MAX_ALIGN);
401 memcpy(buf, (uint8_t *)&swap_size, sizeof swap_size);
402
403 rc = flash_area_write(fap, off, buf, align);
404 if (rc != 0) {
405 return BOOT_EFLASH;
406 }
407
408 return 0;
409}
410
411int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800412boot_swap_type(void)
413{
414 const struct boot_swap_table *table;
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300415 struct boot_swap_state slot0;
416 struct boot_swap_state slot1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800417 int rc;
Fabio Utzigcd5774b2017-11-29 10:18:26 -0200418 size_t i;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800419
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300420 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_0, &slot0);
421 if (rc) {
422 return BOOT_SWAP_TYPE_PANIC;
423 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800424
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300425 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_1, &slot1);
426 if (rc) {
427 return BOOT_SWAP_TYPE_PANIC;
428 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800429
430 for (i = 0; i < BOOT_SWAP_TABLES_COUNT; i++) {
431 table = boot_swap_tables + i;
432
Fabio Utzigd7d20752017-07-13 16:03:25 -0300433 if ((!table->magic_slot0 || table->magic_slot0 == slot0.magic ) &&
434 (!table->magic_slot1 || table->magic_slot1 == slot1.magic ) &&
435 (!table->image_ok_slot0 || table->image_ok_slot0 == slot0.image_ok ) &&
436 (!table->image_ok_slot1 || table->image_ok_slot1 == slot1.image_ok ) &&
437 (!table->copy_done_slot0 || table->copy_done_slot0 == slot0.copy_done)) {
Fabio Utzig34e393e2017-05-22 11:07:07 -0400438 BOOT_LOG_INF("Swap type: %s",
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300439 table->swap_type == BOOT_SWAP_TYPE_TEST ? "test" :
440 table->swap_type == BOOT_SWAP_TYPE_PERM ? "perm" :
441 table->swap_type == BOOT_SWAP_TYPE_REVERT ? "revert" :
442 "BUG; can't happen");
443 assert(table->swap_type == BOOT_SWAP_TYPE_TEST ||
444 table->swap_type == BOOT_SWAP_TYPE_PERM ||
445 table->swap_type == BOOT_SWAP_TYPE_REVERT);
446 return table->swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800447 }
448 }
449
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300450 BOOT_LOG_INF("Swap type: none");
Christopher Collins92ea77f2016-12-12 15:59:26 -0800451 return BOOT_SWAP_TYPE_NONE;
452}
453
454/**
455 * Marks the image in slot 1 as pending. On the next reboot, the system will
456 * perform a one-time boot of the slot 1 image.
457 *
Christopher Collins7835c1e2016-12-21 10:10:51 -0800458 * @param permanent Whether the image should be used permanently or
459 * only tested once:
460 * 0=run image once, then confirm or revert.
461 * 1=run image forever.
462 *
Christopher Collins92ea77f2016-12-12 15:59:26 -0800463 * @return 0 on success; nonzero on failure.
464 */
465int
Christopher Collins7835c1e2016-12-21 10:10:51 -0800466boot_set_pending(int permanent)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800467{
468 const struct flash_area *fap;
469 struct boot_swap_state state_slot1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800470 int rc;
471
Fabio Utzig2473ac02017-05-02 12:45:02 -0300472 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_1, &state_slot1);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800473 if (rc != 0) {
474 return rc;
475 }
476
477 switch (state_slot1.magic) {
478 case BOOT_MAGIC_GOOD:
479 /* Swap already scheduled. */
480 return 0;
481
482 case BOOT_MAGIC_UNSET:
Fabio Utzig2473ac02017-05-02 12:45:02 -0300483 rc = flash_area_open(FLASH_AREA_IMAGE_1, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800484 if (rc != 0) {
485 rc = BOOT_EFLASH;
486 } else {
487 rc = boot_write_magic(fap);
488 }
489
Christopher Collins7835c1e2016-12-21 10:10:51 -0800490 if (rc == 0 && permanent) {
491 rc = boot_write_image_ok(fap);
492 }
493
Christopher Collins92ea77f2016-12-12 15:59:26 -0800494 flash_area_close(fap);
495 return rc;
496
497 default:
498 /* XXX: Temporary assert. */
499 assert(0);
500 return -1;
501 }
502}
503
504/**
505 * 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.
506 *
507 * @return 0 on success; nonzero on failure.
508 */
509int
510boot_set_confirmed(void)
511{
512 const struct flash_area *fap;
513 struct boot_swap_state state_slot0;
514 int rc;
515
Fabio Utzig2473ac02017-05-02 12:45:02 -0300516 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_0, &state_slot0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800517 if (rc != 0) {
518 return rc;
519 }
520
521 switch (state_slot0.magic) {
522 case BOOT_MAGIC_GOOD:
523 /* Confirm needed; proceed. */
524 break;
525
526 case BOOT_MAGIC_UNSET:
527 /* Already confirmed. */
528 return 0;
529
530 case BOOT_MAGIC_BAD:
531 /* Unexpected state. */
532 return BOOT_EBADVECT;
533 }
534
Fabio Utzigde8a38a2017-05-23 11:15:01 -0400535 if (state_slot0.copy_done == BOOT_FLAG_UNSET) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800536 /* Swap never completed. This is unexpected. */
537 return BOOT_EBADVECT;
538 }
539
Fabio Utzigde8a38a2017-05-23 11:15:01 -0400540 if (state_slot0.image_ok != BOOT_FLAG_UNSET) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800541 /* Already confirmed. */
542 return 0;
543 }
544
545 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
546 if (rc) {
547 rc = BOOT_EFLASH;
548 goto done;
549 }
550
551 rc = boot_write_image_ok(fap);
552 if (rc != 0) {
553 goto done;
554 }
555
556 rc = 0;
557
558done:
559 flash_area_close(fap);
560 return rc;
561}