blob: 87895bad202f0f406bbb53f3bb16a01991776bf4 [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;
312 uint8_t val;
313 int rc;
314
315 off = boot_copy_done_off(fap);
316
317 val = 1;
318 rc = flash_area_write(fap, off, &val, 1);
319 if (rc != 0) {
320 return BOOT_EFLASH;
321 }
322
323 return 0;
324}
325
326int
327boot_write_image_ok(const struct flash_area *fap)
328{
329 uint32_t off;
330 uint8_t val;
331 int rc;
332
333 off = boot_image_ok_off(fap);
334
335 val = 1;
336 rc = flash_area_write(fap, off, &val, 1);
337 if (rc != 0) {
338 return BOOT_EFLASH;
339 }
340
341 return 0;
342}
343
344int
345boot_swap_type(void)
346{
347 const struct boot_swap_table *table;
348 struct boot_swap_state state_slot0;
349 struct boot_swap_state state_slot1;
350 int rc;
351 int i;
352
353 rc = boot_read_swap_state_img(0, &state_slot0);
354 assert(rc == 0);
355
356 rc = boot_read_swap_state_img(1, &state_slot1);
357 assert(rc == 0);
358
359 for (i = 0; i < BOOT_SWAP_TABLES_COUNT; i++) {
360 table = boot_swap_tables + i;
361
362 if ((table->bsw_magic_slot0 == 0 ||
363 table->bsw_magic_slot0 == state_slot0.magic) &&
364 (table->bsw_magic_slot1 == 0 ||
365 table->bsw_magic_slot1 == state_slot1.magic) &&
366 (table->bsw_image_ok_slot0 == 0 ||
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800367 table->bsw_image_ok_slot0 == state_slot0.image_ok) &&
368 (table->bsw_image_ok_slot1 == 0 ||
369 table->bsw_image_ok_slot1 == state_slot1.image_ok)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800370
371 return table->bsw_swap_type;
372 }
373 }
374
375 assert(0);
376 return BOOT_SWAP_TYPE_NONE;
377}
378
379/**
380 * Marks the image in slot 1 as pending. On the next reboot, the system will
381 * perform a one-time boot of the slot 1 image.
382 *
Christopher Collins7835c1e2016-12-21 10:10:51 -0800383 * @param permanent Whether the image should be used permanently or
384 * only tested once:
385 * 0=run image once, then confirm or revert.
386 * 1=run image forever.
387 *
Christopher Collins92ea77f2016-12-12 15:59:26 -0800388 * @return 0 on success; nonzero on failure.
389 */
390int
Christopher Collins7835c1e2016-12-21 10:10:51 -0800391boot_set_pending(int permanent)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800392{
393 const struct flash_area *fap;
394 struct boot_swap_state state_slot1;
395 int area_id;
396 int rc;
397
398 rc = boot_read_swap_state_img(1, &state_slot1);
399 if (rc != 0) {
400 return rc;
401 }
402
403 switch (state_slot1.magic) {
404 case BOOT_MAGIC_GOOD:
405 /* Swap already scheduled. */
406 return 0;
407
408 case BOOT_MAGIC_UNSET:
409 area_id = flash_area_id_from_image_slot(1);
410 rc = flash_area_open(area_id, &fap);
411 if (rc != 0) {
412 rc = BOOT_EFLASH;
413 } else {
414 rc = boot_write_magic(fap);
415 }
416
Christopher Collins7835c1e2016-12-21 10:10:51 -0800417 if (rc == 0 && permanent) {
418 rc = boot_write_image_ok(fap);
419 }
420
Christopher Collins92ea77f2016-12-12 15:59:26 -0800421 flash_area_close(fap);
422 return rc;
423
424 default:
425 /* XXX: Temporary assert. */
426 assert(0);
427 return -1;
428 }
429}
430
431/**
432 * 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.
433 *
434 * @return 0 on success; nonzero on failure.
435 */
436int
437boot_set_confirmed(void)
438{
439 const struct flash_area *fap;
440 struct boot_swap_state state_slot0;
441 int rc;
442
443 rc = boot_read_swap_state_img(0, &state_slot0);
444 if (rc != 0) {
445 return rc;
446 }
447
448 switch (state_slot0.magic) {
449 case BOOT_MAGIC_GOOD:
450 /* Confirm needed; proceed. */
451 break;
452
453 case BOOT_MAGIC_UNSET:
454 /* Already confirmed. */
455 return 0;
456
457 case BOOT_MAGIC_BAD:
458 /* Unexpected state. */
459 return BOOT_EBADVECT;
460 }
461
462 if (state_slot0.copy_done == 0xff) {
463 /* Swap never completed. This is unexpected. */
464 return BOOT_EBADVECT;
465 }
466
467 if (state_slot0.image_ok != 0xff) {
468 /* Already confirmed. */
469 return 0;
470 }
471
472 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
473 if (rc) {
474 rc = BOOT_EFLASH;
475 goto done;
476 }
477
478 rc = boot_write_image_ok(fap);
479 if (rc != 0) {
480 goto done;
481 }
482
483 rc = 0;
484
485done:
486 flash_area_close(fap);
487 return rc;
488}