blob: a71df729c66e4e2a644c422657c56bdba0120d29 [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"
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
Fabio Utzig7ebb7c22017-04-26 10:59:31 -030034#define BOOT_LOG_LEVEL BOOT_LOG_LEVEL_INFO
35#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
Fabio Utzig24a273d2017-04-20 08:21:31 -030046const uint32_t BOOT_MAGIC_SZ = sizeof boot_img_magic;
Fabio Utziga0bc9b52017-06-28 09:19:55 -030047const uint32_t BOOT_MAX_ALIGN = MAX_FLASH_ALIGN;
Fabio Utzig24a273d2017-04-20 08:21:31 -030048
Christopher Collins92ea77f2016-12-12 15:59:26 -080049struct boot_swap_table {
50 /** * For each field, a value of 0 means "any". */
Fabio Utzigb5b2f552017-06-30 10:03:47 -030051 uint8_t magic_slot0;
52 uint8_t magic_slot1;
53 uint8_t image_ok_slot0;
54 uint8_t image_ok_slot1;
Fabio Utzigd7d20752017-07-13 16:03:25 -030055 uint8_t copy_done_slot0;
Christopher Collins92ea77f2016-12-12 15:59:26 -080056
Fabio Utzigb5b2f552017-06-30 10:03:47 -030057 uint8_t swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -080058};
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.
Fabio Utzigb5b2f552017-06-30 10:03:47 -030063 *
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.
Christopher Collins92ea77f2016-12-12 15:59:26 -080069 */
70static const struct boot_swap_table boot_swap_tables[] = {
71 {
Fabio Utzigb5b2f552017-06-30 10:03:47 -030072 .magic_slot0 = 0,
73 .magic_slot1 = BOOT_MAGIC_GOOD,
74 .image_ok_slot0 = 0,
75 .image_ok_slot1 = 0xff,
Fabio Utzigd7d20752017-07-13 16:03:25 -030076 .copy_done_slot0 = 0,
Fabio Utzigb5b2f552017-06-30 10:03:47 -030077 .swap_type = BOOT_SWAP_TYPE_TEST,
Christopher Collins92ea77f2016-12-12 15:59:26 -080078 },
Christopher Collins92ea77f2016-12-12 15:59:26 -080079 {
Fabio Utzigb5b2f552017-06-30 10:03:47 -030080 .magic_slot0 = 0,
81 .magic_slot1 = BOOT_MAGIC_GOOD,
82 .image_ok_slot0 = 0,
83 .image_ok_slot1 = 0x01,
Fabio Utzigd7d20752017-07-13 16:03:25 -030084 .copy_done_slot0 = 0,
Fabio Utzigb5b2f552017-06-30 10:03:47 -030085 .swap_type = BOOT_SWAP_TYPE_PERM,
Christopher Collins92ea77f2016-12-12 15:59:26 -080086 },
Christopher Collins92ea77f2016-12-12 15:59:26 -080087 {
Fabio Utzigb5b2f552017-06-30 10:03:47 -030088 .magic_slot0 = BOOT_MAGIC_GOOD,
89 .magic_slot1 = BOOT_MAGIC_UNSET,
90 .image_ok_slot0 = 0xff,
91 .image_ok_slot1 = 0,
Fabio Utzigd7d20752017-07-13 16:03:25 -030092 .copy_done_slot0 = 0x01,
Fabio Utzigb5b2f552017-06-30 10:03:47 -030093 .swap_type = BOOT_SWAP_TYPE_REVERT,
Christopher Collins92ea77f2016-12-12 15:59:26 -080094 },
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
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 }
108
Fabio Utzig24a273d2017-04-20 08:21:31 -0300109 for (i = 0; i < BOOT_MAGIC_SZ / sizeof *magic; i++) {
110 if (magic[i] != 0xffffffff) {
111 return BOOT_MAGIC_BAD;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800112 }
113 }
114
Fabio Utzig24a273d2017-04-20 08:21:31 -0300115 return BOOT_MAGIC_UNSET;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800116}
117
118uint32_t
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300119boot_slots_trailer_sz(uint8_t min_write_sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800120{
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300121 return /* state for all sectors */
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300122 BOOT_STATUS_MAX_ENTRIES * BOOT_STATUS_STATE_COUNT * min_write_sz +
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300123 BOOT_MAX_ALIGN * 2 /* copy_done + image_ok */ +
124 BOOT_MAGIC_SZ;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800125}
126
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300127static uint32_t
128boot_scratch_trailer_sz(uint8_t min_write_sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800129{
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300130 return BOOT_STATUS_STATE_COUNT * min_write_sz + /* state for one sector */
131 BOOT_MAX_ALIGN + /* image_ok */
132 BOOT_MAGIC_SZ;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800133}
134
135static uint32_t
136boot_magic_off(const struct flash_area *fap)
137{
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300138 assert(offsetof(struct image_trailer, magic) == 16);
139 return fap->fa_size - BOOT_MAGIC_SZ;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800140}
141
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400142int
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
Christopher Collins92ea77f2016-12-12 15:59:26 -0800156uint32_t
157boot_status_off(const struct flash_area *fap)
158{
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300159 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;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800172}
173
174static uint32_t
175boot_copy_done_off(const struct flash_area *fap)
176{
Fabio Utzig2473ac02017-05-02 12:45:02 -0300177 assert(fap->fa_id != FLASH_AREA_IMAGE_SCRATCH);
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300178 assert(offsetof(struct image_trailer, copy_done) == 0);
179 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 2;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800180}
181
182static uint32_t
183boot_image_ok_off(const struct flash_area *fap)
184{
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300185 assert(offsetof(struct image_trailer, image_ok) == 8);
186 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800187}
188
189int
190boot_read_swap_state(const struct flash_area *fap,
191 struct boot_swap_state *state)
192{
Fabio Utzig24a273d2017-04-20 08:21:31 -0300193 uint32_t magic[BOOT_MAGIC_SZ];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800194 uint32_t off;
195 int rc;
196
197 off = boot_magic_off(fap);
Fabio Utzig24a273d2017-04-20 08:21:31 -0300198 rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800199 if (rc != 0) {
200 return BOOT_EFLASH;
201 }
202 state->magic = boot_magic_code(magic);
203
Fabio Utzig2473ac02017-05-02 12:45:02 -0300204 if (fap->fa_id != FLASH_AREA_IMAGE_SCRATCH) {
205 off = boot_copy_done_off(fap);
206 rc = flash_area_read(fap, off, &state->copy_done, 1);
207 if (rc != 0) {
208 return BOOT_EFLASH;
209 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800210 }
211
212 off = boot_image_ok_off(fap);
213 rc = flash_area_read(fap, off, &state->image_ok, 1);
214 if (rc != 0) {
215 return BOOT_EFLASH;
216 }
217
218 return 0;
219}
220
221/**
222 * Reads the image trailer from the scratch area.
223 */
224int
Fabio Utzig2473ac02017-05-02 12:45:02 -0300225boot_read_swap_state_by_id(int flash_area_id, struct boot_swap_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800226{
227 const struct flash_area *fap;
228 int rc;
229
Fabio Utzig2473ac02017-05-02 12:45:02 -0300230 switch (flash_area_id) {
231 case FLASH_AREA_IMAGE_SCRATCH:
232 case FLASH_AREA_IMAGE_0:
233 case FLASH_AREA_IMAGE_1:
234 rc = flash_area_open(flash_area_id, &fap);
235 if (rc != 0) {
236 return BOOT_EFLASH;
237 }
238 break;
239 default:
Fabio Utzig856f7832017-05-22 11:04:44 -0400240 return BOOT_EBADARGS;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300241 }
242
243 rc = boot_read_swap_state(fap, state);
Fabio Utzigacfba2e2017-05-22 11:06:29 -0400244 flash_area_close(fap);
245 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800246}
247
248int
249boot_write_magic(const struct flash_area *fap)
250{
251 uint32_t off;
252 int rc;
253
254 off = boot_magic_off(fap);
255
Fabio Utzig24a273d2017-04-20 08:21:31 -0300256 rc = flash_area_write(fap, off, boot_img_magic, BOOT_MAGIC_SZ);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800257 if (rc != 0) {
258 return BOOT_EFLASH;
259 }
260
261 return 0;
262}
263
Fabio Utzig2473ac02017-05-02 12:45:02 -0300264static int
265boot_write_flag(int flag, const struct flash_area *fap)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800266{
267 uint32_t off;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800268 int rc;
Fabio Utzig644b8d42017-04-20 07:56:05 -0300269 uint8_t buf[BOOT_MAX_ALIGN];
David Brown9d725462017-01-23 15:50:58 -0700270 uint8_t align;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800271
Fabio Utzig2473ac02017-05-02 12:45:02 -0300272 switch (flag) {
273 case BOOT_FLAG_COPY_DONE:
274 off = boot_copy_done_off(fap);
275 break;
276 case BOOT_FLAG_IMAGE_OK:
277 off = boot_image_ok_off(fap);
278 break;
279 default:
Fabio Utzig856f7832017-05-22 11:04:44 -0400280 return BOOT_EBADARGS;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300281 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800282
David Brown9d725462017-01-23 15:50:58 -0700283 align = hal_flash_align(fap->fa_device_id);
Fabio Utzig644b8d42017-04-20 07:56:05 -0300284 assert(align <= BOOT_MAX_ALIGN);
285 memset(buf, 0xFF, BOOT_MAX_ALIGN);
Fabio Utzigde8a38a2017-05-23 11:15:01 -0400286 buf[0] = BOOT_FLAG_SET;
David Brown9d725462017-01-23 15:50:58 -0700287
288 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800289 if (rc != 0) {
290 return BOOT_EFLASH;
291 }
292
293 return 0;
294}
295
296int
Fabio Utzig2473ac02017-05-02 12:45:02 -0300297boot_write_copy_done(const struct flash_area *fap)
298{
299 return boot_write_flag(BOOT_FLAG_COPY_DONE, fap);
300}
301
302int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800303boot_write_image_ok(const struct flash_area *fap)
304{
Fabio Utzig2473ac02017-05-02 12:45:02 -0300305 return boot_write_flag(BOOT_FLAG_IMAGE_OK, fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800306}
307
308int
309boot_swap_type(void)
310{
311 const struct boot_swap_table *table;
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300312 struct boot_swap_state slot0;
313 struct boot_swap_state slot1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800314 int rc;
315 int i;
316
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300317 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_0, &slot0);
318 if (rc) {
319 return BOOT_SWAP_TYPE_PANIC;
320 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800321
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300322 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_1, &slot1);
323 if (rc) {
324 return BOOT_SWAP_TYPE_PANIC;
325 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800326
327 for (i = 0; i < BOOT_SWAP_TABLES_COUNT; i++) {
328 table = boot_swap_tables + i;
329
Fabio Utzigd7d20752017-07-13 16:03:25 -0300330 if ((!table->magic_slot0 || table->magic_slot0 == slot0.magic ) &&
331 (!table->magic_slot1 || table->magic_slot1 == slot1.magic ) &&
332 (!table->image_ok_slot0 || table->image_ok_slot0 == slot0.image_ok ) &&
333 (!table->image_ok_slot1 || table->image_ok_slot1 == slot1.image_ok ) &&
334 (!table->copy_done_slot0 || table->copy_done_slot0 == slot0.copy_done)) {
Fabio Utzig34e393e2017-05-22 11:07:07 -0400335 BOOT_LOG_INF("Swap type: %s",
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300336 table->swap_type == BOOT_SWAP_TYPE_TEST ? "test" :
337 table->swap_type == BOOT_SWAP_TYPE_PERM ? "perm" :
338 table->swap_type == BOOT_SWAP_TYPE_REVERT ? "revert" :
339 "BUG; can't happen");
340 assert(table->swap_type == BOOT_SWAP_TYPE_TEST ||
341 table->swap_type == BOOT_SWAP_TYPE_PERM ||
342 table->swap_type == BOOT_SWAP_TYPE_REVERT);
343 return table->swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800344 }
345 }
346
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300347 BOOT_LOG_INF("Swap type: none");
Christopher Collins92ea77f2016-12-12 15:59:26 -0800348 return BOOT_SWAP_TYPE_NONE;
349}
350
351/**
352 * Marks the image in slot 1 as pending. On the next reboot, the system will
353 * perform a one-time boot of the slot 1 image.
354 *
Christopher Collins7835c1e2016-12-21 10:10:51 -0800355 * @param permanent Whether the image should be used permanently or
356 * only tested once:
357 * 0=run image once, then confirm or revert.
358 * 1=run image forever.
359 *
Christopher Collins92ea77f2016-12-12 15:59:26 -0800360 * @return 0 on success; nonzero on failure.
361 */
362int
Christopher Collins7835c1e2016-12-21 10:10:51 -0800363boot_set_pending(int permanent)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800364{
365 const struct flash_area *fap;
366 struct boot_swap_state state_slot1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800367 int rc;
368
Fabio Utzig2473ac02017-05-02 12:45:02 -0300369 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_1, &state_slot1);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800370 if (rc != 0) {
371 return rc;
372 }
373
374 switch (state_slot1.magic) {
375 case BOOT_MAGIC_GOOD:
376 /* Swap already scheduled. */
377 return 0;
378
379 case BOOT_MAGIC_UNSET:
Fabio Utzig2473ac02017-05-02 12:45:02 -0300380 rc = flash_area_open(FLASH_AREA_IMAGE_1, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800381 if (rc != 0) {
382 rc = BOOT_EFLASH;
383 } else {
384 rc = boot_write_magic(fap);
385 }
386
Christopher Collins7835c1e2016-12-21 10:10:51 -0800387 if (rc == 0 && permanent) {
388 rc = boot_write_image_ok(fap);
389 }
390
Christopher Collins92ea77f2016-12-12 15:59:26 -0800391 flash_area_close(fap);
392 return rc;
393
394 default:
395 /* XXX: Temporary assert. */
396 assert(0);
397 return -1;
398 }
399}
400
401/**
402 * 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.
403 *
404 * @return 0 on success; nonzero on failure.
405 */
406int
407boot_set_confirmed(void)
408{
409 const struct flash_area *fap;
410 struct boot_swap_state state_slot0;
411 int rc;
412
Fabio Utzig2473ac02017-05-02 12:45:02 -0300413 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_0, &state_slot0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800414 if (rc != 0) {
415 return rc;
416 }
417
418 switch (state_slot0.magic) {
419 case BOOT_MAGIC_GOOD:
420 /* Confirm needed; proceed. */
421 break;
422
423 case BOOT_MAGIC_UNSET:
424 /* Already confirmed. */
425 return 0;
426
427 case BOOT_MAGIC_BAD:
428 /* Unexpected state. */
429 return BOOT_EBADVECT;
430 }
431
Fabio Utzigde8a38a2017-05-23 11:15:01 -0400432 if (state_slot0.copy_done == BOOT_FLAG_UNSET) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800433 /* Swap never completed. This is unexpected. */
434 return BOOT_EBADVECT;
435 }
436
Fabio Utzigde8a38a2017-05-23 11:15:01 -0400437 if (state_slot0.image_ok != BOOT_FLAG_UNSET) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800438 /* Already confirmed. */
439 return 0;
440 }
441
442 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
443 if (rc) {
444 rc = BOOT_EFLASH;
445 goto done;
446 }
447
448 rc = boot_write_image_ok(fap);
449 if (rc != 0) {
450 goto done;
451 }
452
453 rc = 0;
454
455done:
456 flash_area_close(fap);
457 return rc;
458}