blob: 2fe08e345a4aea74f1481650fe475357cbc6f20b [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>
23
24#include "syscfg/syscfg.h"
25#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
34int boot_current_slot;
35
36const uint32_t boot_img_magic[4] = {
37 0xf395c277,
38 0x7fefd260,
39 0x0f505235,
40 0x8079b62c,
41};
42
43struct boot_swap_table {
44 /** * For each field, a value of 0 means "any". */
45 uint8_t bsw_magic_slot0;
46 uint8_t bsw_magic_slot1;
47 uint8_t bsw_image_ok_slot0;
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -080048 uint8_t bsw_image_ok_slot1;
Christopher Collins92ea77f2016-12-12 15:59:26 -080049
50 uint8_t bsw_swap_type;
51};
52
53/**
54 * This set of tables maps image trailer contents to swap operation type.
55 * When searching for a match, these tables must be iterated sequentially.
56 */
57static const struct boot_swap_table boot_swap_tables[] = {
58 {
59 /* | slot-0 | slot-1 |
60 *----------+------------+------------|
61 * magic | Unset | Unset |
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -080062 * image-ok | Any | Any |
Christopher Collins92ea77f2016-12-12 15:59:26 -080063 * ---------+------------+------------'
64 * swap: none |
65 * -----------------------------------'
66 */
67 .bsw_magic_slot0 = BOOT_MAGIC_UNSET,
68 .bsw_magic_slot1 = BOOT_MAGIC_UNSET,
69 .bsw_image_ok_slot0 = 0,
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -080070 .bsw_image_ok_slot1 = 0,
Christopher Collins92ea77f2016-12-12 15:59:26 -080071 .bsw_swap_type = BOOT_SWAP_TYPE_NONE,
72 },
73
74 {
75 /* | slot-0 | slot-1 |
76 *----------+------------+------------|
77 * magic | Any | Good |
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -080078 * image-ok | Any | Unset |
79 * ---------+------------+------------`
80 * swap: test |
81 * -----------------------------------'
Christopher Collins92ea77f2016-12-12 15:59:26 -080082 */
83 .bsw_magic_slot0 = 0,
84 .bsw_magic_slot1 = BOOT_MAGIC_GOOD,
85 .bsw_image_ok_slot0 = 0,
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -080086 .bsw_image_ok_slot1 = 0xff,
Christopher Collins92ea77f2016-12-12 15:59:26 -080087 .bsw_swap_type = BOOT_SWAP_TYPE_TEST,
88 },
89
90 {
91 /* | slot-0 | slot-1 |
92 *----------+------------+------------|
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -080093 * magic | Any | Good |
94 * image-ok | Any | 0x01 |
95 * ---------+------------+------------`
96 * swap: permanent |
97 * -----------------------------------'
98 */
99 .bsw_magic_slot0 = 0,
100 .bsw_magic_slot1 = BOOT_MAGIC_GOOD,
101 .bsw_image_ok_slot0 = 0,
102 .bsw_image_ok_slot1 = 0x01,
103 .bsw_swap_type = BOOT_SWAP_TYPE_PERM,
104 },
105
106 {
107 /* | slot-0 | slot-1 |
108 *----------+------------+------------|
Christopher Collins92ea77f2016-12-12 15:59:26 -0800109 * magic | Good | Unset |
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800110 * image-ok | 0xff | Any |
Christopher Collins92ea77f2016-12-12 15:59:26 -0800111 * ---------+------------+------------'
112 * swap: revert (test image running) |
113 * -----------------------------------'
114 */
115 .bsw_magic_slot0 = BOOT_MAGIC_GOOD,
116 .bsw_magic_slot1 = BOOT_MAGIC_UNSET,
117 .bsw_image_ok_slot0 = 0xff,
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800118 .bsw_image_ok_slot1 = 0,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800119 .bsw_swap_type = BOOT_SWAP_TYPE_REVERT,
120 },
121
122 {
123 /* | slot-0 | slot-1 |
124 *----------+------------+------------|
125 * magic | Good | Unset |
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800126 * image-ok | 0x01 | Any |
Christopher Collins92ea77f2016-12-12 15:59:26 -0800127 * ---------+------------+------------'
128 * swap: none (confirmed test image) |
129 * -----------------------------------'
130 */
131 .bsw_magic_slot0 = BOOT_MAGIC_GOOD,
132 .bsw_magic_slot1 = BOOT_MAGIC_UNSET,
133 .bsw_image_ok_slot0 = 0x01,
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800134 .bsw_image_ok_slot1 = 0,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800135 .bsw_swap_type = BOOT_SWAP_TYPE_NONE,
136 },
137};
138
139#define BOOT_SWAP_TABLES_COUNT \
140 (sizeof boot_swap_tables / sizeof boot_swap_tables[0])
141
142int
143boot_magic_code(const uint32_t *magic)
144{
145 int i;
146
147 if (memcmp(magic, boot_img_magic, sizeof boot_img_magic) == 0) {
148 return BOOT_MAGIC_GOOD;
149 }
150
151 for (i = 0; i < 4; i++) {
152 if (magic[i] == 0xffffffff) {
153 return BOOT_MAGIC_UNSET;
154 }
155 }
156
157 return BOOT_MAGIC_BAD;
158}
159
160uint32_t
161boot_status_sz(uint8_t min_write_sz)
162{
163 return BOOT_STATUS_MAX_ENTRIES * BOOT_STATUS_STATE_COUNT * min_write_sz;
164}
165
166uint32_t
167boot_trailer_sz(uint8_t min_write_sz)
168{
169 return sizeof boot_img_magic +
170 boot_status_sz(min_write_sz) +
171 min_write_sz * 2;
172}
173
174static uint32_t
175boot_magic_off(const struct flash_area *fap)
176{
177 uint32_t off_from_end;
178 uint8_t elem_sz;
179
180 elem_sz = flash_area_align(fap);
181
182 off_from_end = boot_trailer_sz(elem_sz);
183
184 assert(off_from_end <= fap->fa_size);
185 return fap->fa_size - off_from_end;
186}
187
188uint32_t
189boot_status_off(const struct flash_area *fap)
190{
191 return boot_magic_off(fap) + sizeof boot_img_magic;
192}
193
194static uint32_t
195boot_copy_done_off(const struct flash_area *fap)
196{
197 return fap->fa_size - flash_area_align(fap) * 2;
198}
199
200static uint32_t
201boot_image_ok_off(const struct flash_area *fap)
202{
203 return fap->fa_size - flash_area_align(fap);
204}
205
206int
207boot_read_swap_state(const struct flash_area *fap,
208 struct boot_swap_state *state)
209{
210 uint32_t magic[4];
211 uint32_t off;
212 int rc;
213
214 off = boot_magic_off(fap);
215 rc = flash_area_read(fap, off, magic, sizeof magic);
216 if (rc != 0) {
217 return BOOT_EFLASH;
218 }
219 state->magic = boot_magic_code(magic);
220
221 off = boot_copy_done_off(fap);
222 rc = flash_area_read(fap, off, &state->copy_done, 1);
223 if (rc != 0) {
224 return BOOT_EFLASH;
225 }
226
227 off = boot_image_ok_off(fap);
228 rc = flash_area_read(fap, off, &state->image_ok, 1);
229 if (rc != 0) {
230 return BOOT_EFLASH;
231 }
232
233 return 0;
234}
235
236/**
237 * Reads the image trailer from the scratch area.
238 */
239int
240boot_read_swap_state_scratch(struct boot_swap_state *state)
241{
242 const struct flash_area *fap;
243 int rc;
244
245 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap);
246 if (rc) {
247 rc = BOOT_EFLASH;
248 goto done;
249 }
250
251 rc = boot_read_swap_state(fap, state);
252 if (rc != 0) {
253 goto done;
254 }
255
256 rc = 0;
257
258done:
259 flash_area_close(fap);
260 return rc;
261}
262
263/**
264 * Reads the image trailer from a given image slot.
265 */
266int
267boot_read_swap_state_img(int slot, struct boot_swap_state *state)
268{
269 const struct flash_area *fap;
270 int area_id;
271 int rc;
272
273 area_id = flash_area_id_from_image_slot(slot);
274 rc = flash_area_open(area_id, &fap);
275 if (rc != 0) {
276 rc = BOOT_EFLASH;
277 goto done;
278 }
279
280 rc = boot_read_swap_state(fap, state);
281 if (rc != 0) {
282 goto done;
283 }
284
285 rc = 0;
286
287done:
288 flash_area_close(fap);
289 return rc;
290}
291
292int
293boot_write_magic(const struct flash_area *fap)
294{
295 uint32_t off;
296 int rc;
297
298 off = boot_magic_off(fap);
299
300 rc = flash_area_write(fap, off, boot_img_magic, sizeof boot_img_magic);
301 if (rc != 0) {
302 return BOOT_EFLASH;
303 }
304
305 return 0;
306}
307
308int
309boot_write_copy_done(const struct flash_area *fap)
310{
311 uint32_t off;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800312 int rc;
David Brown9d725462017-01-23 15:50:58 -0700313 uint8_t buf[8];
314 uint8_t align;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800315
316 off = boot_copy_done_off(fap);
317
David Brown9d725462017-01-23 15:50:58 -0700318 align = hal_flash_align(fap->fa_device_id);
319 memset(buf, 0xFF, 8);
320 buf[0] = 1;
321
322 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800323 if (rc != 0) {
324 return BOOT_EFLASH;
325 }
326
327 return 0;
328}
329
330int
331boot_write_image_ok(const struct flash_area *fap)
332{
333 uint32_t off;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800334 int rc;
David Brown9d725462017-01-23 15:50:58 -0700335 uint8_t buf[8];
336 uint8_t align;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800337
338 off = boot_image_ok_off(fap);
339
David Brown9d725462017-01-23 15:50:58 -0700340 align = hal_flash_align(fap->fa_device_id);
341 memset(buf, 0xFF, 8);
342 buf[0] = 1;
343 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800344 if (rc != 0) {
345 return BOOT_EFLASH;
346 }
347
348 return 0;
349}
350
351int
352boot_swap_type(void)
353{
354 const struct boot_swap_table *table;
355 struct boot_swap_state state_slot0;
356 struct boot_swap_state state_slot1;
357 int rc;
358 int i;
359
360 rc = boot_read_swap_state_img(0, &state_slot0);
361 assert(rc == 0);
362
363 rc = boot_read_swap_state_img(1, &state_slot1);
364 assert(rc == 0);
365
366 for (i = 0; i < BOOT_SWAP_TABLES_COUNT; i++) {
367 table = boot_swap_tables + i;
368
369 if ((table->bsw_magic_slot0 == 0 ||
370 table->bsw_magic_slot0 == state_slot0.magic) &&
371 (table->bsw_magic_slot1 == 0 ||
372 table->bsw_magic_slot1 == state_slot1.magic) &&
373 (table->bsw_image_ok_slot0 == 0 ||
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800374 table->bsw_image_ok_slot0 == state_slot0.image_ok) &&
375 (table->bsw_image_ok_slot1 == 0 ||
376 table->bsw_image_ok_slot1 == state_slot1.image_ok)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800377
378 return table->bsw_swap_type;
379 }
380 }
381
382 assert(0);
383 return BOOT_SWAP_TYPE_NONE;
384}
385
386/**
387 * Marks the image in slot 1 as pending. On the next reboot, the system will
388 * perform a one-time boot of the slot 1 image.
389 *
Christopher Collins7835c1e2016-12-21 10:10:51 -0800390 * @param permanent Whether the image should be used permanently or
391 * only tested once:
392 * 0=run image once, then confirm or revert.
393 * 1=run image forever.
394 *
Christopher Collins92ea77f2016-12-12 15:59:26 -0800395 * @return 0 on success; nonzero on failure.
396 */
397int
Christopher Collins7835c1e2016-12-21 10:10:51 -0800398boot_set_pending(int permanent)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800399{
400 const struct flash_area *fap;
401 struct boot_swap_state state_slot1;
402 int area_id;
403 int rc;
404
405 rc = boot_read_swap_state_img(1, &state_slot1);
406 if (rc != 0) {
407 return rc;
408 }
409
410 switch (state_slot1.magic) {
411 case BOOT_MAGIC_GOOD:
412 /* Swap already scheduled. */
413 return 0;
414
415 case BOOT_MAGIC_UNSET:
416 area_id = flash_area_id_from_image_slot(1);
417 rc = flash_area_open(area_id, &fap);
418 if (rc != 0) {
419 rc = BOOT_EFLASH;
420 } else {
421 rc = boot_write_magic(fap);
422 }
423
Christopher Collins7835c1e2016-12-21 10:10:51 -0800424 if (rc == 0 && permanent) {
425 rc = boot_write_image_ok(fap);
426 }
427
Christopher Collins92ea77f2016-12-12 15:59:26 -0800428 flash_area_close(fap);
429 return rc;
430
431 default:
432 /* XXX: Temporary assert. */
433 assert(0);
434 return -1;
435 }
436}
437
438/**
439 * 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.
440 *
441 * @return 0 on success; nonzero on failure.
442 */
443int
444boot_set_confirmed(void)
445{
446 const struct flash_area *fap;
447 struct boot_swap_state state_slot0;
448 int rc;
449
450 rc = boot_read_swap_state_img(0, &state_slot0);
451 if (rc != 0) {
452 return rc;
453 }
454
455 switch (state_slot0.magic) {
456 case BOOT_MAGIC_GOOD:
457 /* Confirm needed; proceed. */
458 break;
459
460 case BOOT_MAGIC_UNSET:
461 /* Already confirmed. */
462 return 0;
463
464 case BOOT_MAGIC_BAD:
465 /* Unexpected state. */
466 return BOOT_EBADVECT;
467 }
468
469 if (state_slot0.copy_done == 0xff) {
470 /* Swap never completed. This is unexpected. */
471 return BOOT_EBADVECT;
472 }
473
474 if (state_slot0.image_ok != 0xff) {
475 /* Already confirmed. */
476 return 0;
477 }
478
479 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
480 if (rc) {
481 rc = BOOT_EFLASH;
482 goto done;
483 }
484
485 rc = boot_write_image_ok(fap);
486 if (rc != 0) {
487 goto done;
488 }
489
490 rc = 0;
491
492done:
493 flash_area_close(fap);
494 return rc;
495}