blob: f3ab9d5af31e2685deb7eccea2c3abdbd0cf358f [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;
48
49 uint8_t bsw_swap_type;
50};
51
52/**
53 * This set of tables maps image trailer contents to swap operation type.
54 * When searching for a match, these tables must be iterated sequentially.
55 */
56static const struct boot_swap_table boot_swap_tables[] = {
57 {
58 /* | slot-0 | slot-1 |
59 *----------+------------+------------|
60 * magic | Unset | Unset |
61 * image-ok | Any | N/A |
62 * ---------+------------+------------'
63 * swap: none |
64 * -----------------------------------'
65 */
66 .bsw_magic_slot0 = BOOT_MAGIC_UNSET,
67 .bsw_magic_slot1 = BOOT_MAGIC_UNSET,
68 .bsw_image_ok_slot0 = 0,
69 .bsw_swap_type = BOOT_SWAP_TYPE_NONE,
70 },
71
72 {
73 /* | slot-0 | slot-1 |
74 *----------+------------+------------|
75 * magic | Any | Good |
76 * image-ok | Any | N/A |
77 * ---------+------------+------------'
78 * swap: test |
79 * -----------------------------------'
80 */
81 .bsw_magic_slot0 = 0,
82 .bsw_magic_slot1 = BOOT_MAGIC_GOOD,
83 .bsw_image_ok_slot0 = 0,
84 .bsw_swap_type = BOOT_SWAP_TYPE_TEST,
85 },
86
87 {
88 /* | slot-0 | slot-1 |
89 *----------+------------+------------|
90 * magic | Good | Unset |
91 * image-ok | 0xff | N/A |
92 * ---------+------------+------------'
93 * swap: revert (test image running) |
94 * -----------------------------------'
95 */
96 .bsw_magic_slot0 = BOOT_MAGIC_GOOD,
97 .bsw_magic_slot1 = BOOT_MAGIC_UNSET,
98 .bsw_image_ok_slot0 = 0xff,
99 .bsw_swap_type = BOOT_SWAP_TYPE_REVERT,
100 },
101
102 {
103 /* | slot-0 | slot-1 |
104 *----------+------------+------------|
105 * magic | Good | Unset |
106 * image-ok | 0x01 | N/A |
107 * ---------+------------+------------'
108 * swap: none (confirmed test image) |
109 * -----------------------------------'
110 */
111 .bsw_magic_slot0 = BOOT_MAGIC_GOOD,
112 .bsw_magic_slot1 = BOOT_MAGIC_UNSET,
113 .bsw_image_ok_slot0 = 0x01,
114 .bsw_swap_type = BOOT_SWAP_TYPE_NONE,
115 },
116};
117
118#define BOOT_SWAP_TABLES_COUNT \
119 (sizeof boot_swap_tables / sizeof boot_swap_tables[0])
120
121int
122boot_magic_code(const uint32_t *magic)
123{
124 int i;
125
126 if (memcmp(magic, boot_img_magic, sizeof boot_img_magic) == 0) {
127 return BOOT_MAGIC_GOOD;
128 }
129
130 for (i = 0; i < 4; i++) {
131 if (magic[i] == 0xffffffff) {
132 return BOOT_MAGIC_UNSET;
133 }
134 }
135
136 return BOOT_MAGIC_BAD;
137}
138
139uint32_t
140boot_status_sz(uint8_t min_write_sz)
141{
142 return BOOT_STATUS_MAX_ENTRIES * BOOT_STATUS_STATE_COUNT * min_write_sz;
143}
144
145uint32_t
146boot_trailer_sz(uint8_t min_write_sz)
147{
148 return sizeof boot_img_magic +
149 boot_status_sz(min_write_sz) +
150 min_write_sz * 2;
151}
152
153static uint32_t
154boot_magic_off(const struct flash_area *fap)
155{
156 uint32_t off_from_end;
157 uint8_t elem_sz;
158
159 elem_sz = flash_area_align(fap);
160
161 off_from_end = boot_trailer_sz(elem_sz);
162
163 assert(off_from_end <= fap->fa_size);
164 return fap->fa_size - off_from_end;
165}
166
167uint32_t
168boot_status_off(const struct flash_area *fap)
169{
170 return boot_magic_off(fap) + sizeof boot_img_magic;
171}
172
173static uint32_t
174boot_copy_done_off(const struct flash_area *fap)
175{
176 return fap->fa_size - flash_area_align(fap) * 2;
177}
178
179static uint32_t
180boot_image_ok_off(const struct flash_area *fap)
181{
182 return fap->fa_size - flash_area_align(fap);
183}
184
185int
186boot_read_swap_state(const struct flash_area *fap,
187 struct boot_swap_state *state)
188{
189 uint32_t magic[4];
190 uint32_t off;
191 int rc;
192
193 off = boot_magic_off(fap);
194 rc = flash_area_read(fap, off, magic, sizeof magic);
195 if (rc != 0) {
196 return BOOT_EFLASH;
197 }
198 state->magic = boot_magic_code(magic);
199
200 off = boot_copy_done_off(fap);
201 rc = flash_area_read(fap, off, &state->copy_done, 1);
202 if (rc != 0) {
203 return BOOT_EFLASH;
204 }
205
206 off = boot_image_ok_off(fap);
207 rc = flash_area_read(fap, off, &state->image_ok, 1);
208 if (rc != 0) {
209 return BOOT_EFLASH;
210 }
211
212 return 0;
213}
214
215/**
216 * Reads the image trailer from the scratch area.
217 */
218int
219boot_read_swap_state_scratch(struct boot_swap_state *state)
220{
221 const struct flash_area *fap;
222 int rc;
223
224 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap);
225 if (rc) {
226 rc = BOOT_EFLASH;
227 goto done;
228 }
229
230 rc = boot_read_swap_state(fap, state);
231 if (rc != 0) {
232 goto done;
233 }
234
235 rc = 0;
236
237done:
238 flash_area_close(fap);
239 return rc;
240}
241
242/**
243 * Reads the image trailer from a given image slot.
244 */
245int
246boot_read_swap_state_img(int slot, struct boot_swap_state *state)
247{
248 const struct flash_area *fap;
249 int area_id;
250 int rc;
251
252 area_id = flash_area_id_from_image_slot(slot);
253 rc = flash_area_open(area_id, &fap);
254 if (rc != 0) {
255 rc = BOOT_EFLASH;
256 goto done;
257 }
258
259 rc = boot_read_swap_state(fap, state);
260 if (rc != 0) {
261 goto done;
262 }
263
264 rc = 0;
265
266done:
267 flash_area_close(fap);
268 return rc;
269}
270
271int
272boot_write_magic(const struct flash_area *fap)
273{
274 uint32_t off;
275 int rc;
276
277 off = boot_magic_off(fap);
278
279 rc = flash_area_write(fap, off, boot_img_magic, sizeof boot_img_magic);
280 if (rc != 0) {
281 return BOOT_EFLASH;
282 }
283
284 return 0;
285}
286
287int
288boot_write_copy_done(const struct flash_area *fap)
289{
290 uint32_t off;
291 uint8_t val;
292 int rc;
293
294 off = boot_copy_done_off(fap);
295
296 val = 1;
297 rc = flash_area_write(fap, off, &val, 1);
298 if (rc != 0) {
299 return BOOT_EFLASH;
300 }
301
302 return 0;
303}
304
305int
306boot_write_image_ok(const struct flash_area *fap)
307{
308 uint32_t off;
309 uint8_t val;
310 int rc;
311
312 off = boot_image_ok_off(fap);
313
314 val = 1;
315 rc = flash_area_write(fap, off, &val, 1);
316 if (rc != 0) {
317 return BOOT_EFLASH;
318 }
319
320 return 0;
321}
322
323int
324boot_swap_type(void)
325{
326 const struct boot_swap_table *table;
327 struct boot_swap_state state_slot0;
328 struct boot_swap_state state_slot1;
329 int rc;
330 int i;
331
332 rc = boot_read_swap_state_img(0, &state_slot0);
333 assert(rc == 0);
334
335 rc = boot_read_swap_state_img(1, &state_slot1);
336 assert(rc == 0);
337
338 for (i = 0; i < BOOT_SWAP_TABLES_COUNT; i++) {
339 table = boot_swap_tables + i;
340
341 if ((table->bsw_magic_slot0 == 0 ||
342 table->bsw_magic_slot0 == state_slot0.magic) &&
343 (table->bsw_magic_slot1 == 0 ||
344 table->bsw_magic_slot1 == state_slot1.magic) &&
345 (table->bsw_image_ok_slot0 == 0 ||
346 table->bsw_image_ok_slot0 == state_slot0.image_ok)) {
347
348 return table->bsw_swap_type;
349 }
350 }
351
352 assert(0);
353 return BOOT_SWAP_TYPE_NONE;
354}
355
356/**
357 * Marks the image in slot 1 as pending. On the next reboot, the system will
358 * perform a one-time boot of the slot 1 image.
359 *
360 * @return 0 on success; nonzero on failure.
361 */
362int
363boot_set_pending(void)
364{
365 const struct flash_area *fap;
366 struct boot_swap_state state_slot1;
367 int area_id;
368 int rc;
369
370 rc = boot_read_swap_state_img(1, &state_slot1);
371 if (rc != 0) {
372 return rc;
373 }
374
375 switch (state_slot1.magic) {
376 case BOOT_MAGIC_GOOD:
377 /* Swap already scheduled. */
378 return 0;
379
380 case BOOT_MAGIC_UNSET:
381 area_id = flash_area_id_from_image_slot(1);
382 rc = flash_area_open(area_id, &fap);
383 if (rc != 0) {
384 rc = BOOT_EFLASH;
385 } else {
386 rc = boot_write_magic(fap);
387 }
388
389 flash_area_close(fap);
390 return rc;
391
392 default:
393 /* XXX: Temporary assert. */
394 assert(0);
395 return -1;
396 }
397}
398
399/**
400 * 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.
401 *
402 * @return 0 on success; nonzero on failure.
403 */
404int
405boot_set_confirmed(void)
406{
407 const struct flash_area *fap;
408 struct boot_swap_state state_slot0;
409 int rc;
410
411 rc = boot_read_swap_state_img(0, &state_slot0);
412 if (rc != 0) {
413 return rc;
414 }
415
416 switch (state_slot0.magic) {
417 case BOOT_MAGIC_GOOD:
418 /* Confirm needed; proceed. */
419 break;
420
421 case BOOT_MAGIC_UNSET:
422 /* Already confirmed. */
423 return 0;
424
425 case BOOT_MAGIC_BAD:
426 /* Unexpected state. */
427 return BOOT_EBADVECT;
428 }
429
430 if (state_slot0.copy_done == 0xff) {
431 /* Swap never completed. This is unexpected. */
432 return BOOT_EBADVECT;
433 }
434
435 if (state_slot0.image_ok != 0xff) {
436 /* Already confirmed. */
437 return 0;
438 }
439
440 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
441 if (rc) {
442 rc = BOOT_EFLASH;
443 goto done;
444 }
445
446 rc = boot_write_image_ok(fap);
447 if (rc != 0) {
448 goto done;
449 }
450
451 rc = 0;
452
453done:
454 flash_area_close(fap);
455 return rc;
456}