blob: bf4e9b8a910843fe18d0a70614b91a4e5879b30c [file] [log] [blame]
Tamas Banf70ef8c2017-12-19 15:35:09 +00001/*
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#include <stddef.h>
24
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
34#define BOOT_LOG_LEVEL BOOT_LOG_LEVEL_INFO
35#include "bootutil/bootutil_log.h"
36
37int boot_current_slot;
38
39const uint32_t boot_img_magic[] = {
40 0xf395c277,
41 0x7fefd260,
42 0x0f505235,
43 0x8079b62c,
44};
45
46const uint32_t BOOT_MAGIC_SZ = sizeof boot_img_magic;
47const uint32_t BOOT_MAX_ALIGN = MAX_FLASH_ALIGN;
48
49struct boot_swap_table {
50 /** * For each field, a value of 0 means "any". */
51 uint8_t magic_slot0;
52 uint8_t magic_slot1;
53 uint8_t image_ok_slot0;
54 uint8_t image_ok_slot1;
55 uint8_t copy_done_slot0;
56
57 uint8_t swap_type;
58};
59
60/**
61 * This set of tables maps image trailer contents to swap operation type.
62 * When searching for a match, these tables must be iterated sequentially.
63 *
64 * NOTE: the table order is very important. The settings in Slot 1 always
65 * are priority to Slot 0 and should be located earlier in the table.
66 *
67 * The table lists only states where there is action needs to be taken by
68 * the bootloader, as in starting/finishing a swap operation.
69 */
70static const struct boot_swap_table boot_swap_tables[] = {
71 {
72 .magic_slot0 = 0,
73 .magic_slot1 = BOOT_MAGIC_GOOD,
74 .image_ok_slot0 = 0,
75 .image_ok_slot1 = 0xff,
76 .copy_done_slot0 = 0,
77 .swap_type = BOOT_SWAP_TYPE_TEST,
78 },
79 {
80 .magic_slot0 = 0,
81 .magic_slot1 = BOOT_MAGIC_GOOD,
82 .image_ok_slot0 = 0,
83 .image_ok_slot1 = 0x01,
84 .copy_done_slot0 = 0,
85 .swap_type = BOOT_SWAP_TYPE_PERM,
86 },
87 {
88 .magic_slot0 = BOOT_MAGIC_GOOD,
89 .magic_slot1 = BOOT_MAGIC_UNSET,
90 .image_ok_slot0 = 0xff,
91 .image_ok_slot1 = 0,
92 .copy_done_slot0 = 0x01,
93 .swap_type = BOOT_SWAP_TYPE_REVERT,
94 },
95};
96
97#define BOOT_SWAP_TABLES_COUNT \
98 (sizeof boot_swap_tables / sizeof boot_swap_tables[0])
99
100int
101boot_magic_code(const uint32_t *magic)
102{
103 int i;
104
105 if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0) {
106 return BOOT_MAGIC_GOOD;
107 }
108
109 for (i = 0; i < BOOT_MAGIC_SZ / sizeof *magic; i++) {
110 if (magic[i] != 0xffffffff) {
111 return BOOT_MAGIC_BAD;
112 }
113 }
114
115 return BOOT_MAGIC_UNSET;
116}
117
118uint32_t
119boot_slots_trailer_sz(uint8_t min_write_sz)
120{
121 return /* state for all sectors */
122 BOOT_STATUS_MAX_ENTRIES * BOOT_STATUS_STATE_COUNT * min_write_sz +
123 BOOT_MAX_ALIGN * 3 /* copy_done + image_ok + swap_size */ +
124 BOOT_MAGIC_SZ;
125}
126
127static uint32_t
128boot_scratch_trailer_sz(uint8_t min_write_sz)
129{
130 return BOOT_STATUS_STATE_COUNT * min_write_sz + /* state for one sector */
131 BOOT_MAX_ALIGN * 2 + /* image_ok + swap_size */
132 BOOT_MAGIC_SZ;
133}
134
135static uint32_t
136boot_magic_off(const struct flash_area *fap)
137{
138 assert(offsetof(struct image_trailer, magic) == 16);
139 return fap->fa_size - BOOT_MAGIC_SZ;
140}
141
142int
143boot_status_entries(const struct flash_area *fap)
144{
145 switch (fap->fa_id) {
146 case FLASH_AREA_IMAGE_0:
147 case FLASH_AREA_IMAGE_1:
148 return BOOT_STATUS_STATE_COUNT * BOOT_STATUS_MAX_ENTRIES;
149 case FLASH_AREA_IMAGE_SCRATCH:
150 return BOOT_STATUS_STATE_COUNT;
151 default:
152 return BOOT_EBADARGS;
153 }
154}
155
156uint32_t
157boot_status_off(const struct flash_area *fap)
158{
159 uint32_t off_from_end;
160 uint8_t elem_sz;
161
162 elem_sz = flash_area_align(fap);
163
164 if (fap->fa_id == FLASH_AREA_IMAGE_SCRATCH) {
165 off_from_end = boot_scratch_trailer_sz(elem_sz);
166 } else {
167 off_from_end = boot_slots_trailer_sz(elem_sz);
168 }
169
170 assert(off_from_end <= fap->fa_size);
171 return fap->fa_size - off_from_end;
172}
173
174static uint32_t
175boot_copy_done_off(const struct flash_area *fap)
176{
177 assert(fap->fa_id != FLASH_AREA_IMAGE_SCRATCH);
178 assert(offsetof(struct image_trailer, copy_done) == 0);
179 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 2;
180}
181
182static uint32_t
183boot_image_ok_off(const struct flash_area *fap)
184{
185 assert(offsetof(struct image_trailer, image_ok) == 8);
186 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN;
187}
188
189static uint32_t
190boot_swap_size_off(const struct flash_area *fap)
191{
192 /*
193 * The "swap_size" field if located just before the trailer.
194 * The scratch slot doesn't store "copy_done"...
195 */
196 if (fap->fa_id == FLASH_AREA_IMAGE_SCRATCH) {
197 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 2;
198 }
199
200 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 3;
201}
202
203int
204boot_read_swap_state(const struct flash_area *fap,
205 struct boot_swap_state *state)
206{
207 uint32_t magic[BOOT_MAGIC_SZ];
208 uint32_t off;
209 int rc;
210
211 off = boot_magic_off(fap);
212 rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ);
213 if (rc != 0) {
214 return BOOT_EFLASH;
215 }
216 state->magic = boot_magic_code(magic);
217
218 if (fap->fa_id != FLASH_AREA_IMAGE_SCRATCH) {
219 off = boot_copy_done_off(fap);
220 rc = flash_area_read(fap, off, &state->copy_done, sizeof state->copy_done);
221 if (rc != 0) {
222 return BOOT_EFLASH;
223 }
224 }
225
226 off = boot_image_ok_off(fap);
227 rc = flash_area_read(fap, off, &state->image_ok, sizeof state->image_ok);
228 if (rc != 0) {
229 return BOOT_EFLASH;
230 }
231
232 return 0;
233}
234
235/**
236 * Reads the image trailer from the scratch area.
237 */
238int
239boot_read_swap_state_by_id(int flash_area_id, struct boot_swap_state *state)
240{
241 const struct flash_area *fap;
242 int rc;
243
244 switch (flash_area_id) {
245 case FLASH_AREA_IMAGE_SCRATCH:
246 case FLASH_AREA_IMAGE_0:
247 case FLASH_AREA_IMAGE_1:
248 rc = flash_area_open(flash_area_id, &fap);
249 if (rc != 0) {
250 return BOOT_EFLASH;
251 }
252 break;
253 default:
254 return BOOT_EBADARGS;
255 }
256
257 rc = boot_read_swap_state(fap, state);
258 flash_area_close(fap);
259 return rc;
260}
261
262int
263boot_read_swap_size(uint32_t *swap_size)
264{
265 uint32_t magic[BOOT_MAGIC_SZ];
266 uint32_t off;
267 const struct flash_area *fap;
268 int rc;
269
270 /*
271 * In the middle a swap, tries to locate the saved swap size. Looks
272 * for a valid magic, first on Slot 0, then on scratch. Both "slots"
273 * can end up being temporary storage for a swap and it is assumed
274 * that if magic is valid then swap size is too, because magic is
275 * always written in the last step.
276 */
277
278 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
279 if (rc != 0) {
280 return BOOT_EFLASH;
281 }
282
283 off = boot_magic_off(fap);
284 rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ);
285 if (rc != 0) {
286 rc = BOOT_EFLASH;
287 goto out;
288 }
289
290 if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) != 0) {
291 /*
292 * If Slot 0 's magic is not valid, try scratch...
293 */
294
295 flash_area_close(fap);
296
297 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap);
298 if (rc != 0) {
299 return BOOT_EFLASH;
300 }
301
302 off = boot_magic_off(fap);
303 rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ);
304 if (rc != 0) {
305 rc = BOOT_EFLASH;
306 goto out;
307 }
308
309 assert(memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0);
310 }
311
312 off = boot_swap_size_off(fap);
313 rc = flash_area_read(fap, off, swap_size, sizeof *swap_size);
314 if (rc != 0) {
315 rc = BOOT_EFLASH;
316 }
317
318out:
319 flash_area_close(fap);
320 return rc;
321}
322
323
324int
325boot_write_magic(const struct flash_area *fap)
326{
327 uint32_t off;
328 int rc;
329
330 off = boot_magic_off(fap);
331
332 rc = flash_area_write(fap, off, boot_img_magic, BOOT_MAGIC_SZ);
333 if (rc != 0) {
334 return BOOT_EFLASH;
335 }
336
337 return 0;
338}
339
340static int
341boot_write_flag(int flag, const struct flash_area *fap)
342{
343 uint32_t off;
344 int rc;
345 uint8_t buf[BOOT_MAX_ALIGN];
346 uint8_t align;
347
348 switch (flag) {
349 case BOOT_FLAG_COPY_DONE:
350 off = boot_copy_done_off(fap);
351 break;
352 case BOOT_FLAG_IMAGE_OK:
353 off = boot_image_ok_off(fap);
354 break;
355 default:
356 return BOOT_EBADARGS;
357 }
358
359 align = hal_flash_align(fap->fa_device_id);
360 assert(align <= BOOT_MAX_ALIGN);
361 memset(buf, 0xFF, BOOT_MAX_ALIGN);
362 buf[0] = BOOT_FLAG_SET;
363
364 rc = flash_area_write(fap, off, buf, align);
365 if (rc != 0) {
366 return BOOT_EFLASH;
367 }
368
369 return 0;
370}
371
372int
373boot_write_copy_done(const struct flash_area *fap)
374{
375 return boot_write_flag(BOOT_FLAG_COPY_DONE, fap);
376}
377
378int
379boot_write_image_ok(const struct flash_area *fap)
380{
381 return boot_write_flag(BOOT_FLAG_IMAGE_OK, fap);
382}
383
384int
385boot_write_swap_size(const struct flash_area *fap, uint32_t swap_size)
386{
387 uint32_t off;
388 int rc;
389 uint8_t buf[BOOT_MAX_ALIGN];
390 uint8_t align;
391
392 off = boot_swap_size_off(fap);
393 align = hal_flash_align(fap->fa_device_id);
394 assert(align <= BOOT_MAX_ALIGN);
395 if (align < sizeof swap_size) {
396 align = sizeof swap_size;
397 }
398 memset(buf, 0xFF, BOOT_MAX_ALIGN);
399 memcpy(buf, (uint8_t *)&swap_size, sizeof swap_size);
400
401 rc = flash_area_write(fap, off, buf, align);
402 if (rc != 0) {
403 return BOOT_EFLASH;
404 }
405
406 return 0;
407}
408
409int
410boot_swap_type(void)
411{
412 const struct boot_swap_table *table;
413 struct boot_swap_state slot0;
414 struct boot_swap_state slot1;
415 int rc;
416 int i;
417
418 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_0, &slot0);
419 if (rc) {
420 return BOOT_SWAP_TYPE_PANIC;
421 }
422
423 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_1, &slot1);
424 if (rc) {
425 return BOOT_SWAP_TYPE_PANIC;
426 }
427
428 for (i = 0; i < BOOT_SWAP_TABLES_COUNT; i++) {
429 table = boot_swap_tables + i;
430
431 if ((!table->magic_slot0 || table->magic_slot0 == slot0.magic ) &&
432 (!table->magic_slot1 || table->magic_slot1 == slot1.magic ) &&
433 (!table->image_ok_slot0 || table->image_ok_slot0 == slot0.image_ok ) &&
434 (!table->image_ok_slot1 || table->image_ok_slot1 == slot1.image_ok ) &&
435 (!table->copy_done_slot0 || table->copy_done_slot0 == slot0.copy_done)) {
436 BOOT_LOG_INF("Swap type: %s",
437 table->swap_type == BOOT_SWAP_TYPE_TEST ? "test" :
438 table->swap_type == BOOT_SWAP_TYPE_PERM ? "perm" :
439 table->swap_type == BOOT_SWAP_TYPE_REVERT ? "revert" :
440 "BUG; can't happen");
441 assert(table->swap_type == BOOT_SWAP_TYPE_TEST ||
442 table->swap_type == BOOT_SWAP_TYPE_PERM ||
443 table->swap_type == BOOT_SWAP_TYPE_REVERT);
444 return table->swap_type;
445 }
446 }
447
448 BOOT_LOG_INF("Swap type: none");
449 return BOOT_SWAP_TYPE_NONE;
450}
451
452/**
453 * Marks the image in slot 1 as pending. On the next reboot, the system will
454 * perform a one-time boot of the slot 1 image.
455 *
456 * @param permanent Whether the image should be used permanently or
457 * only tested once:
458 * 0=run image once, then confirm or revert.
459 * 1=run image forever.
460 *
461 * @return 0 on success; nonzero on failure.
462 */
463int
464boot_set_pending(int permanent)
465{
466 const struct flash_area *fap;
467 struct boot_swap_state state_slot1;
468 int rc;
469
470 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_1, &state_slot1);
471 if (rc != 0) {
472 return rc;
473 }
474
475 switch (state_slot1.magic) {
476 case BOOT_MAGIC_GOOD:
477 /* Swap already scheduled. */
478 return 0;
479
480 case BOOT_MAGIC_UNSET:
481 rc = flash_area_open(FLASH_AREA_IMAGE_1, &fap);
482 if (rc != 0) {
483 rc = BOOT_EFLASH;
484 } else {
485 rc = boot_write_magic(fap);
486 }
487
488 if (rc == 0 && permanent) {
489 rc = boot_write_image_ok(fap);
490 }
491
492 flash_area_close(fap);
493 return rc;
494
495 default:
496 /* XXX: Temporary assert. */
497 assert(0);
498 return -1;
499 }
500}
501
502/**
503 * 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.
504 *
505 * @return 0 on success; nonzero on failure.
506 */
507int
508boot_set_confirmed(void)
509{
510 const struct flash_area *fap;
511 struct boot_swap_state state_slot0;
512 int rc;
513
514 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_0, &state_slot0);
515 if (rc != 0) {
516 return rc;
517 }
518
519 switch (state_slot0.magic) {
520 case BOOT_MAGIC_GOOD:
521 /* Confirm needed; proceed. */
522 break;
523
524 case BOOT_MAGIC_UNSET:
525 /* Already confirmed. */
526 return 0;
527
528 case BOOT_MAGIC_BAD:
529 /* Unexpected state. */
530 return BOOT_EBADVECT;
531 }
532
533 if (state_slot0.copy_done == BOOT_FLAG_UNSET) {
534 /* Swap never completed. This is unexpected. */
535 return BOOT_EBADVECT;
536 }
537
538 if (state_slot0.image_ok != BOOT_FLAG_UNSET) {
539 /* Already confirmed. */
540 return 0;
541 }
542
543 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
544 if (rc) {
545 rc = BOOT_EFLASH;
546 goto done;
547 }
548
549 rc = boot_write_image_ok(fap);
550 if (rc != 0) {
551 goto done;
552 }
553
554 rc = 0;
555
556done:
557 flash_area_close(fap);
558 return rc;
559}