blob: 031031b6d7be8d08bb1c1178ce7f3a7a73889f03 [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;
Fabio Utzig644b8d42017-04-20 07:56:05 -0300313 uint8_t buf[BOOT_MAX_ALIGN];
David Brown9d725462017-01-23 15:50:58 -0700314 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);
Fabio Utzig644b8d42017-04-20 07:56:05 -0300319 assert(align <= BOOT_MAX_ALIGN);
320 memset(buf, 0xFF, BOOT_MAX_ALIGN);
David Brown9d725462017-01-23 15:50:58 -0700321 buf[0] = 1;
322
323 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800324 if (rc != 0) {
325 return BOOT_EFLASH;
326 }
327
328 return 0;
329}
330
331int
332boot_write_image_ok(const struct flash_area *fap)
333{
334 uint32_t off;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800335 int rc;
Fabio Utzig644b8d42017-04-20 07:56:05 -0300336 uint8_t buf[BOOT_MAX_ALIGN];
David Brown9d725462017-01-23 15:50:58 -0700337 uint8_t align;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800338
339 off = boot_image_ok_off(fap);
340
David Brown9d725462017-01-23 15:50:58 -0700341 align = hal_flash_align(fap->fa_device_id);
Fabio Utzig644b8d42017-04-20 07:56:05 -0300342 assert(align <= BOOT_MAX_ALIGN);
343 memset(buf, 0xFF, BOOT_MAX_ALIGN);
David Brown9d725462017-01-23 15:50:58 -0700344 buf[0] = 1;
Fabio Utzig644b8d42017-04-20 07:56:05 -0300345
David Brown9d725462017-01-23 15:50:58 -0700346 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800347 if (rc != 0) {
348 return BOOT_EFLASH;
349 }
350
351 return 0;
352}
353
354int
355boot_swap_type(void)
356{
357 const struct boot_swap_table *table;
358 struct boot_swap_state state_slot0;
359 struct boot_swap_state state_slot1;
360 int rc;
361 int i;
362
363 rc = boot_read_swap_state_img(0, &state_slot0);
364 assert(rc == 0);
365
366 rc = boot_read_swap_state_img(1, &state_slot1);
367 assert(rc == 0);
368
369 for (i = 0; i < BOOT_SWAP_TABLES_COUNT; i++) {
370 table = boot_swap_tables + i;
371
372 if ((table->bsw_magic_slot0 == 0 ||
373 table->bsw_magic_slot0 == state_slot0.magic) &&
374 (table->bsw_magic_slot1 == 0 ||
375 table->bsw_magic_slot1 == state_slot1.magic) &&
376 (table->bsw_image_ok_slot0 == 0 ||
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800377 table->bsw_image_ok_slot0 == state_slot0.image_ok) &&
378 (table->bsw_image_ok_slot1 == 0 ||
379 table->bsw_image_ok_slot1 == state_slot1.image_ok)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800380
381 return table->bsw_swap_type;
382 }
383 }
384
385 assert(0);
386 return BOOT_SWAP_TYPE_NONE;
387}
388
389/**
390 * Marks the image in slot 1 as pending. On the next reboot, the system will
391 * perform a one-time boot of the slot 1 image.
392 *
Christopher Collins7835c1e2016-12-21 10:10:51 -0800393 * @param permanent Whether the image should be used permanently or
394 * only tested once:
395 * 0=run image once, then confirm or revert.
396 * 1=run image forever.
397 *
Christopher Collins92ea77f2016-12-12 15:59:26 -0800398 * @return 0 on success; nonzero on failure.
399 */
400int
Christopher Collins7835c1e2016-12-21 10:10:51 -0800401boot_set_pending(int permanent)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800402{
403 const struct flash_area *fap;
404 struct boot_swap_state state_slot1;
405 int area_id;
406 int rc;
407
408 rc = boot_read_swap_state_img(1, &state_slot1);
409 if (rc != 0) {
410 return rc;
411 }
412
413 switch (state_slot1.magic) {
414 case BOOT_MAGIC_GOOD:
415 /* Swap already scheduled. */
416 return 0;
417
418 case BOOT_MAGIC_UNSET:
419 area_id = flash_area_id_from_image_slot(1);
420 rc = flash_area_open(area_id, &fap);
421 if (rc != 0) {
422 rc = BOOT_EFLASH;
423 } else {
424 rc = boot_write_magic(fap);
425 }
426
Christopher Collins7835c1e2016-12-21 10:10:51 -0800427 if (rc == 0 && permanent) {
428 rc = boot_write_image_ok(fap);
429 }
430
Christopher Collins92ea77f2016-12-12 15:59:26 -0800431 flash_area_close(fap);
432 return rc;
433
434 default:
435 /* XXX: Temporary assert. */
436 assert(0);
437 return -1;
438 }
439}
440
441/**
442 * 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.
443 *
444 * @return 0 on success; nonzero on failure.
445 */
446int
447boot_set_confirmed(void)
448{
449 const struct flash_area *fap;
450 struct boot_swap_state state_slot0;
451 int rc;
452
453 rc = boot_read_swap_state_img(0, &state_slot0);
454 if (rc != 0) {
455 return rc;
456 }
457
458 switch (state_slot0.magic) {
459 case BOOT_MAGIC_GOOD:
460 /* Confirm needed; proceed. */
461 break;
462
463 case BOOT_MAGIC_UNSET:
464 /* Already confirmed. */
465 return 0;
466
467 case BOOT_MAGIC_BAD:
468 /* Unexpected state. */
469 return BOOT_EBADVECT;
470 }
471
472 if (state_slot0.copy_done == 0xff) {
473 /* Swap never completed. This is unexpected. */
474 return BOOT_EBADVECT;
475 }
476
477 if (state_slot0.image_ok != 0xff) {
478 /* Already confirmed. */
479 return 0;
480 }
481
482 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
483 if (rc) {
484 rc = BOOT_EFLASH;
485 goto done;
486 }
487
488 rc = boot_write_image_ok(fap);
489 if (rc != 0) {
490 goto done;
491 }
492
493 rc = 0;
494
495done:
496 flash_area_close(fap);
497 return rc;
498}