blob: f1945386e0c8f08e558eee444c8a84bb1d19708c [file] [log] [blame]
Fabio Utzig12d59162019-11-28 10:01:59 -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#if defined(MCUBOOT_SWAP_USING_SCRATCH) || defined(MCUBOOT_SWAP_USING_MOVE)
33
34int
35swap_erase_trailer_sectors(const struct boot_loader_state *state,
36 const struct flash_area *fap)
37{
38 uint8_t slot;
39 uint32_t sector;
40 uint32_t trailer_sz;
41 uint32_t total_sz;
42 uint32_t off;
43 uint32_t sz;
44 int fa_id_primary;
45 int fa_id_secondary;
46 uint8_t image_index;
47 int rc;
48
49 BOOT_LOG_DBG("erasing trailer; fa_id=%d", fap->fa_id);
50
51 image_index = BOOT_CURR_IMG(state);
52 fa_id_primary = flash_area_id_from_multi_image_slot(image_index,
53 BOOT_PRIMARY_SLOT);
54 fa_id_secondary = flash_area_id_from_multi_image_slot(image_index,
55 BOOT_SECONDARY_SLOT);
56
57 if (fap->fa_id == fa_id_primary) {
58 slot = BOOT_PRIMARY_SLOT;
59 } else if (fap->fa_id == fa_id_secondary) {
60 slot = BOOT_SECONDARY_SLOT;
61 } else {
62 return BOOT_EFLASH;
63 }
64
65 /* delete starting from last sector and moving to beginning */
66 sector = boot_img_num_sectors(state, slot) - 1;
67 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state));
68 total_sz = 0;
69 do {
70 sz = boot_img_sector_size(state, slot, sector);
71 off = boot_img_sector_off(state, slot, sector);
72 rc = boot_erase_region(fap, off, sz);
73 assert(rc == 0);
74
75 sector--;
76 total_sz += sz;
77 } while (total_sz < trailer_sz);
78
79 return rc;
80}
81
82int
83swap_status_init(const struct boot_loader_state *state,
84 const struct flash_area *fap,
85 const struct boot_status *bs)
86{
87 struct boot_swap_state swap_state;
88 uint8_t image_index;
89 int rc;
90
91#if (BOOT_IMAGE_NUMBER == 1)
92 (void)state;
93#endif
94
95 image_index = BOOT_CURR_IMG(state);
96
97 BOOT_LOG_DBG("initializing status; fa_id=%d", fap->fa_id);
98
99 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(image_index),
100 &swap_state);
101 assert(rc == 0);
102
103 if (bs->swap_type != BOOT_SWAP_TYPE_NONE) {
104 rc = boot_write_swap_info(fap, bs->swap_type, image_index);
105 assert(rc == 0);
106 }
107
108 if (swap_state.image_ok == BOOT_FLAG_SET) {
109 rc = boot_write_image_ok(fap);
110 assert(rc == 0);
111 }
112
113 rc = boot_write_swap_size(fap, bs->swap_size);
114 assert(rc == 0);
115
116#ifdef MCUBOOT_ENC_IMAGES
117 rc = boot_write_enc_key(fap, 0, bs->enckey[0]);
118 assert(rc == 0);
119
120 rc = boot_write_enc_key(fap, 1, bs->enckey[1]);
121 assert(rc == 0);
122#endif
123
124 rc = boot_write_magic(fap);
125 assert(rc == 0);
126
127 return 0;
128}
129
130int
131swap_read_status(struct boot_loader_state *state, struct boot_status *bs)
132{
133 const struct flash_area *fap;
134 uint32_t off;
135 uint8_t swap_info;
136 int status_loc;
137 int area_id;
138 int rc;
139
140 status_loc = swap_status_source(state);
141 switch (status_loc) {
142 case BOOT_STATUS_SOURCE_NONE:
143 return 0;
144
145#if MCUBOOT_SWAP_USING_SCRATCH
146 case BOOT_STATUS_SOURCE_SCRATCH:
147 area_id = FLASH_AREA_IMAGE_SCRATCH;
148 break;
149#endif
150
151 case BOOT_STATUS_SOURCE_PRIMARY_SLOT:
152 area_id = FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state));
153 break;
154
155 default:
156 assert(0);
157 return BOOT_EBADARGS;
158 }
159
160 rc = flash_area_open(area_id, &fap);
161 if (rc != 0) {
162 return BOOT_EFLASH;
163 }
164
165 rc = swap_read_status_bytes(fap, state, bs);
166 if (rc == 0) {
167 off = boot_swap_info_off(fap);
168 rc = flash_area_read_is_empty(fap, off, &swap_info, sizeof swap_info);
169 if (rc == 1) {
170 BOOT_SET_SWAP_INFO(swap_info, 0, BOOT_SWAP_TYPE_NONE);
171 rc = 0;
172 }
173
174 /* Extract the swap type info */
175 bs->swap_type = BOOT_GET_SWAP_TYPE(swap_info);
176 }
177
178 flash_area_close(fap);
179
180 return rc;
181}
182
183int
184swap_set_copy_done(uint8_t image_index)
185{
186 const struct flash_area *fap;
187 int rc;
188
189 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
190 &fap);
191 if (rc != 0) {
192 return BOOT_EFLASH;
193 }
194
195 rc = boot_write_copy_done(fap);
196 flash_area_close(fap);
197 return rc;
198}
199
200int
201swap_set_image_ok(uint8_t image_index)
202{
203 const struct flash_area *fap;
204 struct boot_swap_state state;
205 int rc;
206
207 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
208 &fap);
209 if (rc != 0) {
210 return BOOT_EFLASH;
211 }
212
213 rc = boot_read_swap_state(fap, &state);
214 if (rc != 0) {
215 rc = BOOT_EFLASH;
216 goto out;
217 }
218
219 if (state.image_ok == BOOT_FLAG_UNSET) {
220 rc = boot_write_image_ok(fap);
221 }
222
223out:
224 flash_area_close(fap);
225 return rc;
226}
227
228
229#endif /* defined(MCUBOOT_SWAP_USING_SCRATCH) || defined(MCUBOOT_SWAP_USING_MOVE) */