blob: 8dcca2a8208f3789cfd4878166a1b8c1859aab55 [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"
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
Hovland, Sigvart1d96f362018-09-25 13:23:42 +020046#define BOOT_MAGIC_ARR_SZ \
47 (sizeof boot_img_magic / sizeof boot_img_magic[0])
48
Fabio Utzig24a273d2017-04-20 08:21:31 -030049const uint32_t BOOT_MAGIC_SZ = sizeof boot_img_magic;
Fabio Utziga0bc9b52017-06-28 09:19:55 -030050const uint32_t BOOT_MAX_ALIGN = MAX_FLASH_ALIGN;
Fabio Utzig24a273d2017-04-20 08:21:31 -030051
Christopher Collins92ea77f2016-12-12 15:59:26 -080052struct boot_swap_table {
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 Utzig39000012018-07-30 12:40:20 -030074 .magic_slot0 = BOOT_MAGIC_ANY,
Fabio Utzigb5b2f552017-06-30 10:03:47 -030075 .magic_slot1 = BOOT_MAGIC_GOOD,
Fabio Utzig39000012018-07-30 12:40:20 -030076 .image_ok_slot0 = BOOT_FLAG_ANY,
77 .image_ok_slot1 = BOOT_FLAG_UNSET,
78 .copy_done_slot0 = BOOT_FLAG_ANY,
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 Utzig39000012018-07-30 12:40:20 -030082 .magic_slot0 = BOOT_MAGIC_ANY,
Fabio Utzigb5b2f552017-06-30 10:03:47 -030083 .magic_slot1 = BOOT_MAGIC_GOOD,
Fabio Utzig39000012018-07-30 12:40:20 -030084 .image_ok_slot0 = BOOT_FLAG_ANY,
85 .image_ok_slot1 = BOOT_FLAG_SET,
86 .copy_done_slot0 = BOOT_FLAG_ANY,
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,
Fabio Utzig39000012018-07-30 12:40:20 -030092 .image_ok_slot0 = BOOT_FLAG_UNSET,
93 .image_ok_slot1 = BOOT_FLAG_ANY,
94 .copy_done_slot0 = BOOT_FLAG_SET,
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
Fabio Utzig39000012018-07-30 12:40:20 -0300102static int
Fabio Utzig178be542018-09-19 08:12:56 -0300103boot_magic_decode(const uint32_t *magic)
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 }
Fabio Utzig178be542018-09-19 08:12:56 -0300108 return BOOT_MAGIC_BAD;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800109}
110
Fabio Utzig39000012018-07-30 12:40:20 -0300111static int
Fabio Utzig178be542018-09-19 08:12:56 -0300112boot_flag_decode(uint8_t flag)
Fabio Utzig39000012018-07-30 12:40:20 -0300113{
Fabio Utzig39000012018-07-30 12:40:20 -0300114 if (flag != BOOT_FLAG_SET) {
115 return BOOT_FLAG_BAD;
116 }
117 return BOOT_FLAG_SET;
118}
119
Christopher Collins92ea77f2016-12-12 15:59:26 -0800120uint32_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{
Hovland, Sigvart1d96f362018-09-25 13:23:42 +0200209 uint32_t magic[BOOT_MAGIC_ARR_SZ];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800210 uint32_t off;
211 int rc;
212
213 off = boot_magic_off(fap);
Fabio Utzig178be542018-09-19 08:12:56 -0300214 rc = flash_area_read_is_empty(fap, off, magic, BOOT_MAGIC_SZ);
215 if (rc < 0) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800216 return BOOT_EFLASH;
217 }
Fabio Utzig178be542018-09-19 08:12:56 -0300218 if (rc == 1) {
219 state->magic = BOOT_MAGIC_UNSET;
220 } else {
221 state->magic = boot_magic_decode(magic);
222 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800223
Fabio Utzig2473ac02017-05-02 12:45:02 -0300224 if (fap->fa_id != FLASH_AREA_IMAGE_SCRATCH) {
225 off = boot_copy_done_off(fap);
Fabio Utzig178be542018-09-19 08:12:56 -0300226 rc = flash_area_read_is_empty(fap, off, &state->copy_done,
227 sizeof state->copy_done);
228 if (rc < 0) {
Fabio Utzig2473ac02017-05-02 12:45:02 -0300229 return BOOT_EFLASH;
230 }
Fabio Utzig178be542018-09-19 08:12:56 -0300231 if (rc == 1) {
232 state->copy_done = BOOT_FLAG_UNSET;
233 } else {
234 state->copy_done = boot_flag_decode(state->copy_done);
235 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800236 }
237
238 off = boot_image_ok_off(fap);
Fabio Utzig178be542018-09-19 08:12:56 -0300239 rc = flash_area_read_is_empty(fap, off, &state->image_ok, sizeof state->image_ok);
240 if (rc < 0) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800241 return BOOT_EFLASH;
242 }
Fabio Utzig178be542018-09-19 08:12:56 -0300243 if (rc == 1) {
244 state->image_ok = BOOT_FLAG_UNSET;
245 } else {
246 state->image_ok = boot_flag_decode(state->image_ok);
247 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800248
249 return 0;
250}
251
252/**
253 * Reads the image trailer from the scratch area.
254 */
255int
Fabio Utzig2473ac02017-05-02 12:45:02 -0300256boot_read_swap_state_by_id(int flash_area_id, struct boot_swap_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800257{
258 const struct flash_area *fap;
259 int rc;
260
Fabio Utzig2473ac02017-05-02 12:45:02 -0300261 switch (flash_area_id) {
262 case FLASH_AREA_IMAGE_SCRATCH:
263 case FLASH_AREA_IMAGE_0:
264 case FLASH_AREA_IMAGE_1:
265 rc = flash_area_open(flash_area_id, &fap);
266 if (rc != 0) {
267 return BOOT_EFLASH;
268 }
269 break;
270 default:
Fabio Utzig856f7832017-05-22 11:04:44 -0400271 return BOOT_EBADARGS;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300272 }
273
274 rc = boot_read_swap_state(fap, state);
Fabio Utzigacfba2e2017-05-22 11:06:29 -0400275 flash_area_close(fap);
276 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800277}
278
279int
Fabio Utzig46490722017-09-04 15:34:32 -0300280boot_read_swap_size(uint32_t *swap_size)
281{
Hovland, Sigvart1d96f362018-09-25 13:23:42 +0200282 uint32_t magic[BOOT_MAGIC_ARR_SZ];
Fabio Utzig46490722017-09-04 15:34:32 -0300283 uint32_t off;
284 const struct flash_area *fap;
285 int rc;
286
287 /*
288 * In the middle a swap, tries to locate the saved swap size. Looks
289 * for a valid magic, first on Slot 0, then on scratch. Both "slots"
290 * can end up being temporary storage for a swap and it is assumed
291 * that if magic is valid then swap size is too, because magic is
292 * always written in the last step.
293 */
294
295 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
296 if (rc != 0) {
297 return BOOT_EFLASH;
298 }
299
300 off = boot_magic_off(fap);
301 rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ);
302 if (rc != 0) {
303 rc = BOOT_EFLASH;
304 goto out;
305 }
306
307 if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) != 0) {
308 /*
309 * If Slot 0 's magic is not valid, try scratch...
310 */
311
312 flash_area_close(fap);
313
314 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap);
315 if (rc != 0) {
316 return BOOT_EFLASH;
317 }
318
319 off = boot_magic_off(fap);
320 rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ);
321 if (rc != 0) {
322 rc = BOOT_EFLASH;
323 goto out;
324 }
325
326 assert(memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0);
327 }
328
329 off = boot_swap_size_off(fap);
330 rc = flash_area_read(fap, off, swap_size, sizeof *swap_size);
331 if (rc != 0) {
332 rc = BOOT_EFLASH;
333 }
334
335out:
336 flash_area_close(fap);
337 return rc;
338}
339
340
341int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800342boot_write_magic(const struct flash_area *fap)
343{
344 uint32_t off;
345 int rc;
346
347 off = boot_magic_off(fap);
348
Fabio Utzig24a273d2017-04-20 08:21:31 -0300349 rc = flash_area_write(fap, off, boot_img_magic, BOOT_MAGIC_SZ);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800350 if (rc != 0) {
351 return BOOT_EFLASH;
352 }
353
354 return 0;
355}
356
Fabio Utzig2473ac02017-05-02 12:45:02 -0300357static int
358boot_write_flag(int flag, const struct flash_area *fap)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800359{
360 uint32_t off;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800361 int rc;
Fabio Utzig644b8d42017-04-20 07:56:05 -0300362 uint8_t buf[BOOT_MAX_ALIGN];
David Brown9d725462017-01-23 15:50:58 -0700363 uint8_t align;
Fabio Utzig39000012018-07-30 12:40:20 -0300364 uint8_t erased_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800365
Fabio Utzig2473ac02017-05-02 12:45:02 -0300366 switch (flag) {
367 case BOOT_FLAG_COPY_DONE:
368 off = boot_copy_done_off(fap);
369 break;
370 case BOOT_FLAG_IMAGE_OK:
371 off = boot_image_ok_off(fap);
372 break;
373 default:
Fabio Utzig856f7832017-05-22 11:04:44 -0400374 return BOOT_EBADARGS;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300375 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800376
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200377 align = flash_area_align(fap);
Fabio Utzig644b8d42017-04-20 07:56:05 -0300378 assert(align <= BOOT_MAX_ALIGN);
Fabio Utzig39000012018-07-30 12:40:20 -0300379 erased_val = flash_area_erased_val(fap);
380 memset(buf, erased_val, BOOT_MAX_ALIGN);
Fabio Utzigde8a38a2017-05-23 11:15:01 -0400381 buf[0] = BOOT_FLAG_SET;
David Brown9d725462017-01-23 15:50:58 -0700382
383 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800384 if (rc != 0) {
385 return BOOT_EFLASH;
386 }
387
388 return 0;
389}
390
391int
Fabio Utzig2473ac02017-05-02 12:45:02 -0300392boot_write_copy_done(const struct flash_area *fap)
393{
394 return boot_write_flag(BOOT_FLAG_COPY_DONE, fap);
395}
396
397int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800398boot_write_image_ok(const struct flash_area *fap)
399{
Fabio Utzig2473ac02017-05-02 12:45:02 -0300400 return boot_write_flag(BOOT_FLAG_IMAGE_OK, fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800401}
402
403int
Fabio Utzig46490722017-09-04 15:34:32 -0300404boot_write_swap_size(const struct flash_area *fap, uint32_t swap_size)
405{
406 uint32_t off;
407 int rc;
408 uint8_t buf[BOOT_MAX_ALIGN];
409 uint8_t align;
Fabio Utzig39000012018-07-30 12:40:20 -0300410 uint8_t erased_val;
Fabio Utzig46490722017-09-04 15:34:32 -0300411
412 off = boot_swap_size_off(fap);
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200413 align = flash_area_align(fap);
Fabio Utzig46490722017-09-04 15:34:32 -0300414 assert(align <= BOOT_MAX_ALIGN);
415 if (align < sizeof swap_size) {
416 align = sizeof swap_size;
417 }
Fabio Utzig39000012018-07-30 12:40:20 -0300418 erased_val = flash_area_erased_val(fap);
419 memset(buf, erased_val, BOOT_MAX_ALIGN);
Fabio Utzig46490722017-09-04 15:34:32 -0300420 memcpy(buf, (uint8_t *)&swap_size, sizeof swap_size);
421
422 rc = flash_area_write(fap, off, buf, align);
423 if (rc != 0) {
424 return BOOT_EFLASH;
425 }
426
427 return 0;
428}
429
430int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800431boot_swap_type(void)
432{
433 const struct boot_swap_table *table;
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300434 struct boot_swap_state slot0;
435 struct boot_swap_state slot1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800436 int rc;
Fabio Utzigcd5774b2017-11-29 10:18:26 -0200437 size_t i;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800438
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300439 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_0, &slot0);
440 if (rc) {
441 return BOOT_SWAP_TYPE_PANIC;
442 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800443
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300444 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_1, &slot1);
445 if (rc) {
446 return BOOT_SWAP_TYPE_PANIC;
447 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800448
449 for (i = 0; i < BOOT_SWAP_TABLES_COUNT; i++) {
450 table = boot_swap_tables + i;
451
Fabio Utzig39000012018-07-30 12:40:20 -0300452 if ((table->magic_slot0 == BOOT_MAGIC_ANY ||
453 table->magic_slot0 == slot0.magic) &&
454 (table->magic_slot1 == BOOT_MAGIC_ANY ||
455 table->magic_slot1 == slot1.magic) &&
456 (table->image_ok_slot0 == BOOT_FLAG_ANY ||
457 table->image_ok_slot0 == slot0.image_ok) &&
458 (table->image_ok_slot1 == BOOT_FLAG_ANY ||
459 table->image_ok_slot1 == slot1.image_ok) &&
460 (table->copy_done_slot0 == BOOT_FLAG_ANY ||
461 table->copy_done_slot0 == slot0.copy_done)) {
Fabio Utzig34e393e2017-05-22 11:07:07 -0400462 BOOT_LOG_INF("Swap type: %s",
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300463 table->swap_type == BOOT_SWAP_TYPE_TEST ? "test" :
464 table->swap_type == BOOT_SWAP_TYPE_PERM ? "perm" :
465 table->swap_type == BOOT_SWAP_TYPE_REVERT ? "revert" :
466 "BUG; can't happen");
467 assert(table->swap_type == BOOT_SWAP_TYPE_TEST ||
468 table->swap_type == BOOT_SWAP_TYPE_PERM ||
469 table->swap_type == BOOT_SWAP_TYPE_REVERT);
470 return table->swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800471 }
472 }
473
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300474 BOOT_LOG_INF("Swap type: none");
Christopher Collins92ea77f2016-12-12 15:59:26 -0800475 return BOOT_SWAP_TYPE_NONE;
476}
477
478/**
479 * Marks the image in slot 1 as pending. On the next reboot, the system will
480 * perform a one-time boot of the slot 1 image.
481 *
Christopher Collins7835c1e2016-12-21 10:10:51 -0800482 * @param permanent Whether the image should be used permanently or
483 * only tested once:
484 * 0=run image once, then confirm or revert.
485 * 1=run image forever.
486 *
Christopher Collins92ea77f2016-12-12 15:59:26 -0800487 * @return 0 on success; nonzero on failure.
488 */
489int
Christopher Collins7835c1e2016-12-21 10:10:51 -0800490boot_set_pending(int permanent)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800491{
492 const struct flash_area *fap;
493 struct boot_swap_state state_slot1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800494 int rc;
495
Fabio Utzig2473ac02017-05-02 12:45:02 -0300496 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_1, &state_slot1);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800497 if (rc != 0) {
498 return rc;
499 }
500
501 switch (state_slot1.magic) {
502 case BOOT_MAGIC_GOOD:
503 /* Swap already scheduled. */
504 return 0;
505
506 case BOOT_MAGIC_UNSET:
Fabio Utzig2473ac02017-05-02 12:45:02 -0300507 rc = flash_area_open(FLASH_AREA_IMAGE_1, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800508 if (rc != 0) {
509 rc = BOOT_EFLASH;
510 } else {
511 rc = boot_write_magic(fap);
512 }
513
Christopher Collins7835c1e2016-12-21 10:10:51 -0800514 if (rc == 0 && permanent) {
515 rc = boot_write_image_ok(fap);
516 }
517
Christopher Collins92ea77f2016-12-12 15:59:26 -0800518 flash_area_close(fap);
519 return rc;
520
521 default:
522 /* XXX: Temporary assert. */
523 assert(0);
524 return -1;
525 }
526}
527
528/**
529 * 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.
530 *
531 * @return 0 on success; nonzero on failure.
532 */
533int
534boot_set_confirmed(void)
535{
536 const struct flash_area *fap;
537 struct boot_swap_state state_slot0;
538 int rc;
539
Fabio Utzig2473ac02017-05-02 12:45:02 -0300540 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_0, &state_slot0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800541 if (rc != 0) {
542 return rc;
543 }
544
545 switch (state_slot0.magic) {
546 case BOOT_MAGIC_GOOD:
547 /* Confirm needed; proceed. */
548 break;
549
550 case BOOT_MAGIC_UNSET:
551 /* Already confirmed. */
552 return 0;
553
554 case BOOT_MAGIC_BAD:
555 /* Unexpected state. */
556 return BOOT_EBADVECT;
557 }
558
Christopher Collins92ea77f2016-12-12 15:59:26 -0800559 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
560 if (rc) {
561 rc = BOOT_EFLASH;
562 goto done;
563 }
564
Fabio Utzig08fa2672018-09-26 12:16:18 -0300565 if (state_slot0.copy_done == BOOT_FLAG_UNSET) {
Fabio Utzig39000012018-07-30 12:40:20 -0300566 /* Swap never completed. This is unexpected. */
567 rc = BOOT_EBADVECT;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800568 goto done;
569 }
570
Ɓukasz Rymanowskia1927f42018-09-26 15:31:50 +0200571 if (state_slot0.image_ok != BOOT_FLAG_UNSET) {
Fabio Utzig39000012018-07-30 12:40:20 -0300572 /* Already confirmed. */
573 goto done;
574 }
575
576 rc = boot_write_image_ok(fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800577
578done:
579 flash_area_close(fap);
580 return rc;
581}