blob: 64485db24c294b12bad2bdaf6aa62373b75ca2b5 [file] [log] [blame]
Fabio Utzig12d59162019-11-28 10:01:59 -03001/*
David Brownaac71112020-02-03 16:13:42 -07002 * SPDX-License-Identifier: Apache-2.0
3 *
Fabio Utzig12d59162019-11-28 10:01:59 -03004 * Copyright (c) 2019 JUUL Labs
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * 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, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
Fabio Utzig12d59162019-11-28 10:01:59 -030019#include <stddef.h>
20#include <stdbool.h>
21#include <inttypes.h>
22#include <stdlib.h>
23#include <string.h>
24#include "bootutil/bootutil.h"
25#include "bootutil_priv.h"
26#include "swap_priv.h"
27#include "bootutil/bootutil_log.h"
28
29#include "mcuboot_config/mcuboot_config.h"
30
Roman Okhrimenko977b3752022-03-31 14:40:48 +030031BOOT_LOG_MODULE_DECLARE(mcuboot);
Fabio Utzig12d59162019-11-28 10:01:59 -030032
33#if defined(MCUBOOT_SWAP_USING_SCRATCH) || defined(MCUBOOT_SWAP_USING_MOVE)
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +020034
35#ifndef MCUBOOT_SWAP_USING_STATUS
36
Fabio Utzig12d59162019-11-28 10:01:59 -030037int
38swap_erase_trailer_sectors(const struct boot_loader_state *state,
39 const struct flash_area *fap)
40{
41 uint8_t slot;
42 uint32_t sector;
43 uint32_t trailer_sz;
44 uint32_t total_sz;
45 uint32_t off;
46 uint32_t sz;
47 int fa_id_primary;
48 int fa_id_secondary;
49 uint8_t image_index;
50 int rc;
51
Roman Okhrimenko977b3752022-03-31 14:40:48 +030052 BOOT_LOG_DBG("erasing trailer; fa_id=%u", (unsigned)flash_area_get_id(fap));
Fabio Utzig12d59162019-11-28 10:01:59 -030053
54 image_index = BOOT_CURR_IMG(state);
55 fa_id_primary = flash_area_id_from_multi_image_slot(image_index,
56 BOOT_PRIMARY_SLOT);
57 fa_id_secondary = flash_area_id_from_multi_image_slot(image_index,
58 BOOT_SECONDARY_SLOT);
59
Roman Okhrimenko977b3752022-03-31 14:40:48 +030060 if (flash_area_get_id(fap) == fa_id_primary) {
Fabio Utzig12d59162019-11-28 10:01:59 -030061 slot = BOOT_PRIMARY_SLOT;
Roman Okhrimenko977b3752022-03-31 14:40:48 +030062 } else if (flash_area_get_id(fap) == fa_id_secondary) {
Fabio Utzig12d59162019-11-28 10:01:59 -030063 slot = BOOT_SECONDARY_SLOT;
64 } else {
65 return BOOT_EFLASH;
66 }
67
68 /* delete starting from last sector and moving to beginning */
69 sector = boot_img_num_sectors(state, slot) - 1;
70 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state));
71 total_sz = 0;
72 do {
73 sz = boot_img_sector_size(state, slot, sector);
74 off = boot_img_sector_off(state, slot, sector);
75 rc = boot_erase_region(fap, off, sz);
76 assert(rc == 0);
77
78 sector--;
79 total_sz += sz;
80 } while (total_sz < trailer_sz);
81
82 return rc;
83}
84
85int
86swap_status_init(const struct boot_loader_state *state,
87 const struct flash_area *fap,
88 const struct boot_status *bs)
89{
90 struct boot_swap_state swap_state;
91 uint8_t image_index;
92 int rc;
93
94#if (BOOT_IMAGE_NUMBER == 1)
95 (void)state;
96#endif
97
98 image_index = BOOT_CURR_IMG(state);
99
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300100 BOOT_LOG_DBG("initializing status; fa_id=%u", (unsigned)flash_area_get_id(fap));
Fabio Utzig12d59162019-11-28 10:01:59 -0300101
102 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(image_index),
103 &swap_state);
104 assert(rc == 0);
105
106 if (bs->swap_type != BOOT_SWAP_TYPE_NONE) {
107 rc = boot_write_swap_info(fap, bs->swap_type, image_index);
108 assert(rc == 0);
109 }
110
111 if (swap_state.image_ok == BOOT_FLAG_SET) {
112 rc = boot_write_image_ok(fap);
113 assert(rc == 0);
114 }
115
116 rc = boot_write_swap_size(fap, bs->swap_size);
117 assert(rc == 0);
118
119#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig4741c452019-12-19 15:32:41 -0300120 rc = boot_write_enc_key(fap, 0, bs);
Fabio Utzig12d59162019-11-28 10:01:59 -0300121 assert(rc == 0);
122
Fabio Utzig4741c452019-12-19 15:32:41 -0300123 rc = boot_write_enc_key(fap, 1, bs);
Fabio Utzig12d59162019-11-28 10:01:59 -0300124 assert(rc == 0);
125#endif
126
127 rc = boot_write_magic(fap);
128 assert(rc == 0);
129
130 return 0;
131}
132
133int
134swap_read_status(struct boot_loader_state *state, struct boot_status *bs)
135{
136 const struct flash_area *fap;
137 uint32_t off;
138 uint8_t swap_info;
Fabio Utzig12d59162019-11-28 10:01:59 -0300139 int area_id;
140 int rc;
141
Fabio Utzigb86e6882019-12-10 09:51:17 -0300142 bs->source = swap_status_source(state);
143 switch (bs->source) {
Fabio Utzig12d59162019-11-28 10:01:59 -0300144 case BOOT_STATUS_SOURCE_NONE:
145 return 0;
146
147#if MCUBOOT_SWAP_USING_SCRATCH
148 case BOOT_STATUS_SOURCE_SCRATCH:
149 area_id = FLASH_AREA_IMAGE_SCRATCH;
150 break;
151#endif
152
153 case BOOT_STATUS_SOURCE_PRIMARY_SLOT:
154 area_id = FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state));
155 break;
156
157 default:
158 assert(0);
159 return BOOT_EBADARGS;
160 }
161
162 rc = flash_area_open(area_id, &fap);
163 if (rc != 0) {
164 return BOOT_EFLASH;
165 }
166
167 rc = swap_read_status_bytes(fap, state, bs);
168 if (rc == 0) {
169 off = boot_swap_info_off(fap);
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300170 rc = flash_area_read(fap, off, &swap_info, sizeof swap_info);
Fabio Utzig4b2e55f2020-09-24 11:49:20 -0300171 if (rc != 0) {
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300172 rc = BOOT_EFLASH;
173 goto done;
Fabio Utzig4b2e55f2020-09-24 11:49:20 -0300174 }
175
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300176 if (swap_info == flash_area_erased_val(fap)) {
Fabio Utzig12d59162019-11-28 10:01:59 -0300177 BOOT_SET_SWAP_INFO(swap_info, 0, BOOT_SWAP_TYPE_NONE);
178 rc = 0;
179 }
180
181 /* Extract the swap type info */
182 bs->swap_type = BOOT_GET_SWAP_TYPE(swap_info);
183 }
184
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300185done:
Fabio Utzig12d59162019-11-28 10:01:59 -0300186 flash_area_close(fap);
187
188 return rc;
189}
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200190#endif /* !MCUBOOT_SWAP_USING_STATUS */
Fabio Utzig12d59162019-11-28 10:01:59 -0300191
192int
193swap_set_copy_done(uint8_t image_index)
194{
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300195 const struct flash_area *fap = NULL;
Fabio Utzig12d59162019-11-28 10:01:59 -0300196 int rc;
197
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300198 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index), &fap);
Fabio Utzig12d59162019-11-28 10:01:59 -0300199 if (rc != 0) {
200 return BOOT_EFLASH;
201 }
202
203 rc = boot_write_copy_done(fap);
204 flash_area_close(fap);
205 return rc;
206}
207
208int
209swap_set_image_ok(uint8_t image_index)
210{
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300211 const struct flash_area *fap = NULL;
212 struct boot_swap_state state = {0};
Fabio Utzig12d59162019-11-28 10:01:59 -0300213 int rc;
214
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300215 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index), &fap);
Fabio Utzig12d59162019-11-28 10:01:59 -0300216 if (rc != 0) {
217 return BOOT_EFLASH;
218 }
219
220 rc = boot_read_swap_state(fap, &state);
221 if (rc != 0) {
222 rc = BOOT_EFLASH;
223 goto out;
224 }
225
226 if (state.image_ok == BOOT_FLAG_UNSET) {
227 rc = boot_write_image_ok(fap);
228 }
229
230out:
231 flash_area_close(fap);
232 return rc;
233}
234
Fabio Utzig12d59162019-11-28 10:01:59 -0300235#endif /* defined(MCUBOOT_SWAP_USING_SCRATCH) || defined(MCUBOOT_SWAP_USING_MOVE) */