blob: ab17b70bd7947e403ff85be442a785086fd86af1 [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) {
Fabio Utzigb86e6882019-12-10 09:51:17 -0300302 if (bs->source != BOOT_STATUS_SOURCE_PRIMARY_SLOT) {
303 rc = swap_erase_trailer_sectors(state, fap_pri);
304 assert(rc == 0);
Fabio Utzig74aef312019-11-28 11:05:34 -0300305
Fabio Utzigb86e6882019-12-10 09:51:17 -0300306 rc = swap_status_init(state, fap_pri, bs);
307 assert(rc == 0);
308 }
Fabio Utzig74aef312019-11-28 11:05:34 -0300309
310 rc = swap_erase_trailer_sectors(state, fap_sec);
311 assert(rc == 0);
312 }
313
314 rc = boot_erase_region(fap_pri, new_off, sz);
315 assert(rc == 0);
316
317 rc = boot_copy_region(state, fap_pri, fap_pri, old_off, new_off, sz);
318 assert(rc == 0);
319
320 rc = boot_write_status(state, bs);
321
322 bs->idx++;
323 BOOT_STATUS_ASSERT(rc == 0);
324}
325
326static void
327boot_swap_sectors(int idx, uint32_t sz, struct boot_loader_state *state,
328 struct boot_status *bs, const struct flash_area *fap_pri,
329 const struct flash_area *fap_sec)
330{
331 uint32_t pri_off;
332 uint32_t pri_up_off;
333 uint32_t sec_off;
334 int rc;
335
336 pri_up_off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, idx);
337 pri_off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, idx - 1);
338 sec_off = boot_img_sector_off(state, BOOT_SECONDARY_SLOT, idx - 1);
339
340 if (bs->state == BOOT_STATUS_STATE_0) {
341 rc = boot_erase_region(fap_pri, pri_off, sz);
342 assert(rc == 0);
343
344 rc = boot_copy_region(state, fap_sec, fap_pri, sec_off, pri_off, sz);
345 assert(rc == 0);
346
347 rc = boot_write_status(state, bs);
348 bs->state = BOOT_STATUS_STATE_1;
349 BOOT_STATUS_ASSERT(rc == 0);
350 }
351
352 if (bs->state == BOOT_STATUS_STATE_1) {
353 rc = boot_erase_region(fap_sec, sec_off, sz);
354 assert(rc == 0);
355
356 rc = boot_copy_region(state, fap_pri, fap_sec, pri_up_off, sec_off, sz);
357 assert(rc == 0);
358
359 rc = boot_write_status(state, bs);
360 bs->idx++;
361 bs->state = BOOT_STATUS_STATE_0;
362 BOOT_STATUS_ASSERT(rc == 0);
363 }
364}
365
366/*
367 * When starting a revert the swap status exists in the primary slot, and
368 * the status in the secondary slot is erased. To start the swap, the status
369 * area in the primary slot must be re-initialized; if during the small
370 * window of time between re-initializing it and writing the first metadata
371 * a reset happens, the swap process is broken and cannot be resumed.
372 *
373 * This function handles the issue by making the revert look like a permanent
374 * upgrade (by initializing the secondary slot).
375 */
376void
377fixup_revert(const struct boot_loader_state *state, struct boot_status *bs,
378 const struct flash_area *fap_sec, uint8_t sec_id)
379{
380 struct boot_swap_state swap_state;
381 int rc;
382
383#if (BOOT_IMAGE_NUMBER == 1)
384 (void)state;
385#endif
386
387 /* No fixup required */
388 if (bs->swap_type != BOOT_SWAP_TYPE_REVERT ||
389 bs->op != BOOT_STATUS_OP_MOVE ||
390 bs->idx != BOOT_STATUS_IDX_0) {
391 return;
392 }
393
394 rc = boot_read_swap_state_by_id(sec_id, &swap_state);
395 assert(rc == 0);
396
397 BOOT_LOG_SWAP_STATE("Secondary image", &swap_state);
398
399 if (swap_state.magic == BOOT_MAGIC_UNSET) {
400 rc = swap_erase_trailer_sectors(state, fap_sec);
401 assert(rc == 0);
402
403 rc = boot_write_image_ok(fap_sec);
404 assert(rc == 0);
405
406 rc = boot_write_swap_size(fap_sec, bs->swap_size);
407 assert(rc == 0);
408
409#ifdef MCUBOOT_ENC_IMAGES
410 rc = boot_write_enc_key(fap_sec, 0, bs->enckey[0]);
411 assert(rc == 0);
412
413 rc = boot_write_enc_key(fap_sec, 1, bs->enckey[1]);
414 assert(rc == 0);
415#endif
416
417 rc = boot_write_magic(fap_sec);
418 assert(rc == 0);
419 }
420}
421
422void
423swap_run(struct boot_loader_state *state, struct boot_status *bs,
424 uint32_t copy_size)
425{
426 uint32_t sz;
427 uint32_t idx;
428 uint32_t slot_size;
429 uint8_t image_index;
430 const struct flash_area *fap_pri;
431 const struct flash_area *fap_sec;
432 int rc;
433
434 slot_size = 0;
435 g_last_idx = 0;
436
437 //FIXME: assume all sectors of same size and just do math here...
438 sz = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, 0);
439 while (1) {
440 slot_size += sz;
441 /* Skip to next sector because all sectors will be moved up. */
442 g_last_idx++;
443 if (slot_size >= copy_size) {
444 break;
445 }
446 }
447
448 image_index = BOOT_CURR_IMG(state);
449
450 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index), &fap_pri);
451 assert (rc == 0);
452
453 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index), &fap_sec);
454 assert (rc == 0);
455
456 fixup_revert(state, bs, fap_sec, FLASH_AREA_IMAGE_SECONDARY(image_index));
457
458 /* FIXME: assure last sector does not overrun trailer */
459
460 if (bs->op == BOOT_STATUS_OP_MOVE) {
461 idx = g_last_idx;
462 while (idx > 0) {
463 if (idx <= (g_last_idx - bs->idx + 1)) {
464 boot_move_sector_up(idx, sz, state, bs, fap_pri, fap_sec);
465 }
466 idx--;
467 }
468 bs->idx = BOOT_STATUS_IDX_0;
469 }
470
471 bs->op = BOOT_STATUS_OP_SWAP;
472
473 idx = 1;
474 while (idx <= g_last_idx) {
475 if (idx >= bs->idx) {
476 boot_swap_sectors(idx, sz, state, bs, fap_pri, fap_sec);
477 }
478 idx++;
479 }
480
481 flash_area_close(fap_pri);
482 flash_area_close(fap_sec);
483}
484
485#endif