blob: 6955474e9184bc1fe19976ac0e0f7c9775ad0bcc [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 */
165 BOOT_LOG_ERR("Detected inconsistent status!");
166
167#if !defined(MCUBOOT_VALIDATE_PRIMARY_SLOT)
168 /* With validation of the primary slot disabled, there is no way
169 * to be sure the swapped primary slot is OK, so abort!
170 */
171 assert(0);
172#endif
173 }
174
175 move_entries = BOOT_MAX_IMG_SECTORS * BOOT_STATUS_MOVE_STATE_COUNT;
176 if (found_idx == -1) {
177 /* no swap status found; nothing to do */
178 } else if (found_idx < move_entries) {
179 bs->op = BOOT_STATUS_OP_MOVE;
180 bs->idx = (found_idx / BOOT_STATUS_MOVE_STATE_COUNT) + BOOT_STATUS_IDX_0;
181 bs->state = (found_idx % BOOT_STATUS_MOVE_STATE_COUNT) + BOOT_STATUS_STATE_0;;
182 } else {
183 bs->op = BOOT_STATUS_OP_SWAP;
184 bs->idx = ((found_idx - move_entries) / BOOT_STATUS_SWAP_STATE_COUNT) + BOOT_STATUS_IDX_0;
185 bs->state = ((found_idx - move_entries) % BOOT_STATUS_SWAP_STATE_COUNT) + BOOT_STATUS_STATE_0;
186 }
187
188 return 0;
189}
190
191uint32_t
192boot_status_internal_off(const struct boot_status *bs, int elem_sz)
193{
194 uint32_t off;
195 int idx_sz;
196
197 idx_sz = elem_sz * ((bs->op == BOOT_STATUS_OP_MOVE) ?
198 BOOT_STATUS_MOVE_STATE_COUNT : BOOT_STATUS_SWAP_STATE_COUNT);
199
200 off = ((bs->op == BOOT_STATUS_OP_MOVE) ?
201 0 : (BOOT_MAX_IMG_SECTORS * BOOT_STATUS_MOVE_STATE_COUNT * elem_sz)) +
202 (bs->idx - BOOT_STATUS_IDX_0) * idx_sz +
203 (bs->state - BOOT_STATUS_STATE_0) * elem_sz;
204
205 return off;
206}
207
208int
209boot_slots_compatible(struct boot_loader_state *state)
210{
211 size_t num_sectors;
212 size_t i;
213
214 num_sectors = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT);
215 if (num_sectors != boot_img_num_sectors(state, BOOT_SECONDARY_SLOT)) {
216 BOOT_LOG_WRN("Cannot upgrade: slots don't have same amount of sectors");
217 return 0;
218 }
219
220 if (num_sectors > BOOT_MAX_IMG_SECTORS) {
221 BOOT_LOG_WRN("Cannot upgrade: more sectors than allowed");
222 return 0;
223 }
224
225 for (i = 0; i < num_sectors; i++) {
226 if (boot_img_sector_size(state, BOOT_PRIMARY_SLOT, i) !=
227 boot_img_sector_size(state, BOOT_SECONDARY_SLOT, i)) {
228 BOOT_LOG_WRN("Cannot upgrade: not same sector layout");
229 return 0;
230 }
231 }
232
233 return 1;
234}
235
236#define BOOT_LOG_SWAP_STATE(area, state) \
237 BOOT_LOG_INF("%s: magic=%s, swap_type=0x%x, copy_done=0x%x, " \
238 "image_ok=0x%x", \
239 (area), \
240 ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \
241 (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \
242 "bad"), \
243 (state)->swap_type, \
244 (state)->copy_done, \
245 (state)->image_ok)
246
247int
248swap_status_source(struct boot_loader_state *state)
249{
250 struct boot_swap_state state_primary_slot;
251 int rc;
252 uint8_t source;
253 uint8_t image_index;
254
255#if (BOOT_IMAGE_NUMBER == 1)
256 (void)state;
257#endif
258
259 image_index = BOOT_CURR_IMG(state);
260
261 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(image_index),
262 &state_primary_slot);
263 assert(rc == 0);
264
265 BOOT_LOG_SWAP_STATE("Primary image", &state_primary_slot);
266
267 if (state_primary_slot.magic == BOOT_MAGIC_GOOD &&
268 state_primary_slot.copy_done == BOOT_FLAG_UNSET) {
269
270 source = BOOT_STATUS_SOURCE_PRIMARY_SLOT;
271
272 BOOT_LOG_INF("Boot source: primary slot");
273 return source;
274 }
275
276 BOOT_LOG_INF("Boot source: none");
277 return BOOT_STATUS_SOURCE_NONE;
278}
279
280/*
281 * "Moves" the sector located at idx - 1 to idx.
282 */
283static void
284boot_move_sector_up(int idx, uint32_t sz, struct boot_loader_state *state,
285 struct boot_status *bs, const struct flash_area *fap_pri,
286 const struct flash_area *fap_sec)
287{
288 uint32_t new_off;
289 uint32_t old_off;
290 int rc;
291
292 /*
293 * FIXME: assuming sectors of size == sz, a single off variable
294 * would be enough
295 */
296
297 /* Calculate offset from start of image area. */
298 new_off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, idx);
299 old_off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, idx - 1);
300
301 if (bs->idx == BOOT_STATUS_IDX_0) {
302 rc = swap_erase_trailer_sectors(state, fap_pri);
303 assert(rc == 0);
304
305 rc = swap_status_init(state, fap_pri, bs);
306 assert(rc == 0);
307
308 rc = swap_erase_trailer_sectors(state, fap_sec);
309 assert(rc == 0);
310 }
311
312 rc = boot_erase_region(fap_pri, new_off, sz);
313 assert(rc == 0);
314
315 rc = boot_copy_region(state, fap_pri, fap_pri, old_off, new_off, sz);
316 assert(rc == 0);
317
318 rc = boot_write_status(state, bs);
319
320 bs->idx++;
321 BOOT_STATUS_ASSERT(rc == 0);
322}
323
324static void
325boot_swap_sectors(int idx, uint32_t sz, struct boot_loader_state *state,
326 struct boot_status *bs, const struct flash_area *fap_pri,
327 const struct flash_area *fap_sec)
328{
329 uint32_t pri_off;
330 uint32_t pri_up_off;
331 uint32_t sec_off;
332 int rc;
333
334 pri_up_off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, idx);
335 pri_off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, idx - 1);
336 sec_off = boot_img_sector_off(state, BOOT_SECONDARY_SLOT, idx - 1);
337
338 if (bs->state == BOOT_STATUS_STATE_0) {
339 rc = boot_erase_region(fap_pri, pri_off, sz);
340 assert(rc == 0);
341
342 rc = boot_copy_region(state, fap_sec, fap_pri, sec_off, pri_off, sz);
343 assert(rc == 0);
344
345 rc = boot_write_status(state, bs);
346 bs->state = BOOT_STATUS_STATE_1;
347 BOOT_STATUS_ASSERT(rc == 0);
348 }
349
350 if (bs->state == BOOT_STATUS_STATE_1) {
351 rc = boot_erase_region(fap_sec, sec_off, sz);
352 assert(rc == 0);
353
354 rc = boot_copy_region(state, fap_pri, fap_sec, pri_up_off, sec_off, sz);
355 assert(rc == 0);
356
357 rc = boot_write_status(state, bs);
358 bs->idx++;
359 bs->state = BOOT_STATUS_STATE_0;
360 BOOT_STATUS_ASSERT(rc == 0);
361 }
362}
363
364/*
365 * When starting a revert the swap status exists in the primary slot, and
366 * the status in the secondary slot is erased. To start the swap, the status
367 * area in the primary slot must be re-initialized; if during the small
368 * window of time between re-initializing it and writing the first metadata
369 * a reset happens, the swap process is broken and cannot be resumed.
370 *
371 * This function handles the issue by making the revert look like a permanent
372 * upgrade (by initializing the secondary slot).
373 */
374void
375fixup_revert(const struct boot_loader_state *state, struct boot_status *bs,
376 const struct flash_area *fap_sec, uint8_t sec_id)
377{
378 struct boot_swap_state swap_state;
379 int rc;
380
381#if (BOOT_IMAGE_NUMBER == 1)
382 (void)state;
383#endif
384
385 /* No fixup required */
386 if (bs->swap_type != BOOT_SWAP_TYPE_REVERT ||
387 bs->op != BOOT_STATUS_OP_MOVE ||
388 bs->idx != BOOT_STATUS_IDX_0) {
389 return;
390 }
391
392 rc = boot_read_swap_state_by_id(sec_id, &swap_state);
393 assert(rc == 0);
394
395 BOOT_LOG_SWAP_STATE("Secondary image", &swap_state);
396
397 if (swap_state.magic == BOOT_MAGIC_UNSET) {
398 rc = swap_erase_trailer_sectors(state, fap_sec);
399 assert(rc == 0);
400
401 rc = boot_write_image_ok(fap_sec);
402 assert(rc == 0);
403
404 rc = boot_write_swap_size(fap_sec, bs->swap_size);
405 assert(rc == 0);
406
407#ifdef MCUBOOT_ENC_IMAGES
408 rc = boot_write_enc_key(fap_sec, 0, bs->enckey[0]);
409 assert(rc == 0);
410
411 rc = boot_write_enc_key(fap_sec, 1, bs->enckey[1]);
412 assert(rc == 0);
413#endif
414
415 rc = boot_write_magic(fap_sec);
416 assert(rc == 0);
417 }
418}
419
420void
421swap_run(struct boot_loader_state *state, struct boot_status *bs,
422 uint32_t copy_size)
423{
424 uint32_t sz;
425 uint32_t idx;
426 uint32_t slot_size;
427 uint8_t image_index;
428 const struct flash_area *fap_pri;
429 const struct flash_area *fap_sec;
430 int rc;
431
432 slot_size = 0;
433 g_last_idx = 0;
434
435 //FIXME: assume all sectors of same size and just do math here...
436 sz = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, 0);
437 while (1) {
438 slot_size += sz;
439 /* Skip to next sector because all sectors will be moved up. */
440 g_last_idx++;
441 if (slot_size >= copy_size) {
442 break;
443 }
444 }
445
446 image_index = BOOT_CURR_IMG(state);
447
448 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index), &fap_pri);
449 assert (rc == 0);
450
451 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index), &fap_sec);
452 assert (rc == 0);
453
454 fixup_revert(state, bs, fap_sec, FLASH_AREA_IMAGE_SECONDARY(image_index));
455
456 /* FIXME: assure last sector does not overrun trailer */
457
458 if (bs->op == BOOT_STATUS_OP_MOVE) {
459 idx = g_last_idx;
460 while (idx > 0) {
461 if (idx <= (g_last_idx - bs->idx + 1)) {
462 boot_move_sector_up(idx, sz, state, bs, fap_pri, fap_sec);
463 }
464 idx--;
465 }
466 bs->idx = BOOT_STATUS_IDX_0;
467 }
468
469 bs->op = BOOT_STATUS_OP_SWAP;
470
471 idx = 1;
472 while (idx <= g_last_idx) {
473 if (idx >= bs->idx) {
474 boot_swap_sectors(idx, sz, state, bs, fap_pri, fap_sec);
475 }
476 idx++;
477 }
478
479 flash_area_close(fap_pri);
480 flash_area_close(fap_sec);
481}
482
483#endif