blob: b18c206c1c2be3f55f67947b7abeba317d68398c [file] [log] [blame]
Fabio Utzig74aef312019-11-28 11:05:34 -03001/*
2 * Copyright (c) 2019 JUUL Labs
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <assert.h>
18#include <stddef.h>
19#include <stdbool.h>
20#include <inttypes.h>
21#include <stdlib.h>
22#include <string.h>
23#include "bootutil/bootutil.h"
24#include "bootutil_priv.h"
25#include "swap_priv.h"
26#include "bootutil/bootutil_log.h"
27
28#include "mcuboot_config/mcuboot_config.h"
29
30MCUBOOT_LOG_MODULE_DECLARE(mcuboot);
31
32#ifdef MCUBOOT_SWAP_USING_MOVE
33
34#if defined(MCUBOOT_VALIDATE_PRIMARY_SLOT)
35/*
36 * FIXME: this might have to be updated for threaded sim
37 */
38int boot_status_fails = 0;
39#define BOOT_STATUS_ASSERT(x) \
40 do { \
41 if (!(x)) { \
42 boot_status_fails++; \
43 } \
44 } while (0)
45#else
46#define BOOT_STATUS_ASSERT(x) ASSERT(x)
47#endif
48
49static uint32_t g_last_idx = UINT32_MAX;
50
51int
52boot_read_image_header(struct boot_loader_state *state, int slot,
53 struct image_header *out_hdr, struct boot_status *bs)
54{
55 const struct flash_area *fap;
56 uint32_t off;
57 uint32_t sz;
58 int area_id;
59 int rc;
60
61#if (BOOT_IMAGE_NUMBER == 1)
62 (void)state;
63#endif
64
65 off = 0;
66 if (bs) {
67 sz = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, 0);
68 if (bs->op == BOOT_STATUS_OP_MOVE) {
69 if (slot == 0 && bs->idx > g_last_idx) {
70 /* second sector */
71 off = sz;
72 }
73 } else if (bs->op == BOOT_STATUS_OP_SWAP) {
74 if (bs->idx > 1 && bs->idx <= g_last_idx) {
75 if (slot == 0) {
76 slot = 1;
77 } else {
78 slot = 0;
79 }
80 } else if (bs->idx == 1) {
81 if (slot == 0) {
82 off = sz;
83 }
84 if (slot == 1 && bs->state == 2) {
85 slot = 0;
86 }
87 }
88 }
89 }
90
91 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
92 rc = flash_area_open(area_id, &fap);
93 if (rc != 0) {
94 rc = BOOT_EFLASH;
95 goto done;
96 }
97
98 rc = flash_area_read(fap, off, out_hdr, sizeof *out_hdr);
99 if (rc != 0) {
100 rc = BOOT_EFLASH;
101 goto done;
102 }
103
104 /* We only know where the headers are located when bs is valid */
105 if (bs != NULL && out_hdr->ih_magic != IMAGE_MAGIC) {
106 rc = -1;
107 goto done;
108 }
109
110 rc = 0;
111
112done:
113 flash_area_close(fap);
114 return rc;
115}
116
117int
118swap_read_status_bytes(const struct flash_area *fap,
119 struct boot_loader_state *state, struct boot_status *bs)
120{
121 uint32_t off;
122 uint8_t status;
123 int max_entries;
124 int found_idx;
125 uint8_t write_sz;
126 int move_entries;
127 int rc;
128 int last_rc;
129 int erased_sections;
130 int i;
131
132 max_entries = boot_status_entries(BOOT_CURR_IMG(state), fap);
133 if (max_entries < 0) {
134 return BOOT_EBADARGS;
135 }
136
137 erased_sections = 0;
138 found_idx = -1;
139 /* skip erased sectors at the end */
140 last_rc = 1;
141 write_sz = BOOT_WRITE_SZ(state);
142 off = boot_status_off(fap);
143 for (i = max_entries; i > 0; i--) {
144 rc = flash_area_read_is_empty(fap, off + (i - 1) * write_sz, &status, 1);
145 if (rc < 0) {
146 return BOOT_EFLASH;
147 }
148
149 if (rc == 1) {
150 if (rc != last_rc) {
151 erased_sections++;
152 }
153 } else {
154 if (found_idx == -1) {
155 found_idx = i;
156 }
157 }
158 last_rc = rc;
159 }
160
161 if (erased_sections > 1) {
162 /* This means there was an error writing status on the last
163 * swap. Tell user and move on to validation!
164 */
David Brown098de832019-12-10 11:58:01 -0700165#if !defined(__BOOTSIM__)
Fabio Utzig74aef312019-11-28 11:05:34 -0300166 BOOT_LOG_ERR("Detected inconsistent status!");
David Brown098de832019-12-10 11:58:01 -0700167#endif
Fabio Utzig74aef312019-11-28 11:05:34 -0300168
169#if !defined(MCUBOOT_VALIDATE_PRIMARY_SLOT)
170 /* With validation of the primary slot disabled, there is no way
171 * to be sure the swapped primary slot is OK, so abort!
172 */
173 assert(0);
174#endif
175 }
176
177 move_entries = BOOT_MAX_IMG_SECTORS * BOOT_STATUS_MOVE_STATE_COUNT;
178 if (found_idx == -1) {
179 /* no swap status found; nothing to do */
180 } else if (found_idx < move_entries) {
181 bs->op = BOOT_STATUS_OP_MOVE;
182 bs->idx = (found_idx / BOOT_STATUS_MOVE_STATE_COUNT) + BOOT_STATUS_IDX_0;
183 bs->state = (found_idx % BOOT_STATUS_MOVE_STATE_COUNT) + BOOT_STATUS_STATE_0;;
184 } else {
185 bs->op = BOOT_STATUS_OP_SWAP;
186 bs->idx = ((found_idx - move_entries) / BOOT_STATUS_SWAP_STATE_COUNT) + BOOT_STATUS_IDX_0;
187 bs->state = ((found_idx - move_entries) % BOOT_STATUS_SWAP_STATE_COUNT) + BOOT_STATUS_STATE_0;
188 }
189
190 return 0;
191}
192
193uint32_t
194boot_status_internal_off(const struct boot_status *bs, int elem_sz)
195{
196 uint32_t off;
197 int idx_sz;
198
199 idx_sz = elem_sz * ((bs->op == BOOT_STATUS_OP_MOVE) ?
200 BOOT_STATUS_MOVE_STATE_COUNT : BOOT_STATUS_SWAP_STATE_COUNT);
201
202 off = ((bs->op == BOOT_STATUS_OP_MOVE) ?
203 0 : (BOOT_MAX_IMG_SECTORS * BOOT_STATUS_MOVE_STATE_COUNT * elem_sz)) +
204 (bs->idx - BOOT_STATUS_IDX_0) * idx_sz +
205 (bs->state - BOOT_STATUS_STATE_0) * elem_sz;
206
207 return off;
208}
209
210int
211boot_slots_compatible(struct boot_loader_state *state)
212{
213 size_t num_sectors;
214 size_t i;
215
216 num_sectors = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT);
217 if (num_sectors != boot_img_num_sectors(state, BOOT_SECONDARY_SLOT)) {
218 BOOT_LOG_WRN("Cannot upgrade: slots don't have same amount of sectors");
219 return 0;
220 }
221
222 if (num_sectors > BOOT_MAX_IMG_SECTORS) {
223 BOOT_LOG_WRN("Cannot upgrade: more sectors than allowed");
224 return 0;
225 }
226
227 for (i = 0; i < num_sectors; i++) {
228 if (boot_img_sector_size(state, BOOT_PRIMARY_SLOT, i) !=
229 boot_img_sector_size(state, BOOT_SECONDARY_SLOT, i)) {
230 BOOT_LOG_WRN("Cannot upgrade: not same sector layout");
231 return 0;
232 }
233 }
234
235 return 1;
236}
237
238#define BOOT_LOG_SWAP_STATE(area, state) \
239 BOOT_LOG_INF("%s: magic=%s, swap_type=0x%x, copy_done=0x%x, " \
240 "image_ok=0x%x", \
241 (area), \
242 ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \
243 (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \
244 "bad"), \
245 (state)->swap_type, \
246 (state)->copy_done, \
247 (state)->image_ok)
248
249int
250swap_status_source(struct boot_loader_state *state)
251{
252 struct boot_swap_state state_primary_slot;
253 int rc;
254 uint8_t source;
255 uint8_t image_index;
256
257#if (BOOT_IMAGE_NUMBER == 1)
258 (void)state;
259#endif
260
261 image_index = BOOT_CURR_IMG(state);
262
263 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(image_index),
264 &state_primary_slot);
265 assert(rc == 0);
266
267 BOOT_LOG_SWAP_STATE("Primary image", &state_primary_slot);
268
269 if (state_primary_slot.magic == BOOT_MAGIC_GOOD &&
270 state_primary_slot.copy_done == BOOT_FLAG_UNSET) {
271
272 source = BOOT_STATUS_SOURCE_PRIMARY_SLOT;
273
274 BOOT_LOG_INF("Boot source: primary slot");
275 return source;
276 }
277
278 BOOT_LOG_INF("Boot source: none");
279 return BOOT_STATUS_SOURCE_NONE;
280}
281
282/*
283 * "Moves" the sector located at idx - 1 to idx.
284 */
285static void
286boot_move_sector_up(int idx, uint32_t sz, struct boot_loader_state *state,
287 struct boot_status *bs, const struct flash_area *fap_pri,
288 const struct flash_area *fap_sec)
289{
290 uint32_t new_off;
291 uint32_t old_off;
292 int rc;
293
294 /*
295 * FIXME: assuming sectors of size == sz, a single off variable
296 * would be enough
297 */
298
299 /* Calculate offset from start of image area. */
300 new_off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, idx);
301 old_off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, idx - 1);
302
303 if (bs->idx == BOOT_STATUS_IDX_0) {
Fabio Utzigb86e6882019-12-10 09:51:17 -0300304 if (bs->source != BOOT_STATUS_SOURCE_PRIMARY_SLOT) {
305 rc = swap_erase_trailer_sectors(state, fap_pri);
306 assert(rc == 0);
Fabio Utzig74aef312019-11-28 11:05:34 -0300307
Fabio Utzigb86e6882019-12-10 09:51:17 -0300308 rc = swap_status_init(state, fap_pri, bs);
309 assert(rc == 0);
310 }
Fabio Utzig74aef312019-11-28 11:05:34 -0300311
312 rc = swap_erase_trailer_sectors(state, fap_sec);
313 assert(rc == 0);
314 }
315
316 rc = boot_erase_region(fap_pri, new_off, sz);
317 assert(rc == 0);
318
319 rc = boot_copy_region(state, fap_pri, fap_pri, old_off, new_off, sz);
320 assert(rc == 0);
321
322 rc = boot_write_status(state, bs);
323
324 bs->idx++;
325 BOOT_STATUS_ASSERT(rc == 0);
326}
327
328static void
329boot_swap_sectors(int idx, uint32_t sz, struct boot_loader_state *state,
330 struct boot_status *bs, const struct flash_area *fap_pri,
331 const struct flash_area *fap_sec)
332{
333 uint32_t pri_off;
334 uint32_t pri_up_off;
335 uint32_t sec_off;
336 int rc;
337
338 pri_up_off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, idx);
339 pri_off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, idx - 1);
340 sec_off = boot_img_sector_off(state, BOOT_SECONDARY_SLOT, idx - 1);
341
342 if (bs->state == BOOT_STATUS_STATE_0) {
343 rc = boot_erase_region(fap_pri, pri_off, sz);
344 assert(rc == 0);
345
346 rc = boot_copy_region(state, fap_sec, fap_pri, sec_off, pri_off, sz);
347 assert(rc == 0);
348
349 rc = boot_write_status(state, bs);
350 bs->state = BOOT_STATUS_STATE_1;
351 BOOT_STATUS_ASSERT(rc == 0);
352 }
353
354 if (bs->state == BOOT_STATUS_STATE_1) {
355 rc = boot_erase_region(fap_sec, sec_off, sz);
356 assert(rc == 0);
357
358 rc = boot_copy_region(state, fap_pri, fap_sec, pri_up_off, sec_off, sz);
359 assert(rc == 0);
360
361 rc = boot_write_status(state, bs);
362 bs->idx++;
363 bs->state = BOOT_STATUS_STATE_0;
364 BOOT_STATUS_ASSERT(rc == 0);
365 }
366}
367
368/*
369 * When starting a revert the swap status exists in the primary slot, and
370 * the status in the secondary slot is erased. To start the swap, the status
371 * area in the primary slot must be re-initialized; if during the small
372 * window of time between re-initializing it and writing the first metadata
373 * a reset happens, the swap process is broken and cannot be resumed.
374 *
375 * This function handles the issue by making the revert look like a permanent
376 * upgrade (by initializing the secondary slot).
377 */
378void
379fixup_revert(const struct boot_loader_state *state, struct boot_status *bs,
380 const struct flash_area *fap_sec, uint8_t sec_id)
381{
382 struct boot_swap_state swap_state;
383 int rc;
384
385#if (BOOT_IMAGE_NUMBER == 1)
386 (void)state;
387#endif
388
389 /* No fixup required */
390 if (bs->swap_type != BOOT_SWAP_TYPE_REVERT ||
391 bs->op != BOOT_STATUS_OP_MOVE ||
392 bs->idx != BOOT_STATUS_IDX_0) {
393 return;
394 }
395
396 rc = boot_read_swap_state_by_id(sec_id, &swap_state);
397 assert(rc == 0);
398
399 BOOT_LOG_SWAP_STATE("Secondary image", &swap_state);
400
401 if (swap_state.magic == BOOT_MAGIC_UNSET) {
402 rc = swap_erase_trailer_sectors(state, fap_sec);
403 assert(rc == 0);
404
405 rc = boot_write_image_ok(fap_sec);
406 assert(rc == 0);
407
408 rc = boot_write_swap_size(fap_sec, bs->swap_size);
409 assert(rc == 0);
410
Fabio Utzig74aef312019-11-28 11:05:34 -0300411 rc = boot_write_magic(fap_sec);
412 assert(rc == 0);
413 }
414}
415
416void
417swap_run(struct boot_loader_state *state, struct boot_status *bs,
418 uint32_t copy_size)
419{
420 uint32_t sz;
421 uint32_t idx;
422 uint32_t slot_size;
423 uint8_t image_index;
424 const struct flash_area *fap_pri;
425 const struct flash_area *fap_sec;
426 int rc;
427
428 slot_size = 0;
429 g_last_idx = 0;
430
431 //FIXME: assume all sectors of same size and just do math here...
432 sz = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, 0);
433 while (1) {
434 slot_size += sz;
435 /* Skip to next sector because all sectors will be moved up. */
436 g_last_idx++;
437 if (slot_size >= copy_size) {
438 break;
439 }
440 }
441
442 image_index = BOOT_CURR_IMG(state);
443
444 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index), &fap_pri);
445 assert (rc == 0);
446
447 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index), &fap_sec);
448 assert (rc == 0);
449
450 fixup_revert(state, bs, fap_sec, FLASH_AREA_IMAGE_SECONDARY(image_index));
451
452 /* FIXME: assure last sector does not overrun trailer */
453
454 if (bs->op == BOOT_STATUS_OP_MOVE) {
455 idx = g_last_idx;
456 while (idx > 0) {
457 if (idx <= (g_last_idx - bs->idx + 1)) {
458 boot_move_sector_up(idx, sz, state, bs, fap_pri, fap_sec);
459 }
460 idx--;
461 }
462 bs->idx = BOOT_STATUS_IDX_0;
463 }
464
465 bs->op = BOOT_STATUS_OP_SWAP;
466
467 idx = 1;
468 while (idx <= g_last_idx) {
469 if (idx >= bs->idx) {
470 boot_swap_sectors(idx, sz, state, bs, fap_pri, fap_sec);
471 }
472 idx++;
473 }
474
475 flash_area_close(fap_pri);
476 flash_area_close(fap_sec);
477}
478
479#endif