blob: 31995b6a25cfcc755be36e15a7ae2483c3b4feda [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
Fabio Utzig4741c452019-12-19 15:32:41 -0300117 rc = boot_write_enc_key(fap, 0, bs);
Fabio Utzig12d59162019-11-28 10:01:59 -0300118 assert(rc == 0);
119
Fabio Utzig4741c452019-12-19 15:32:41 -0300120 rc = boot_write_enc_key(fap, 1, bs);
Fabio Utzig12d59162019-11-28 10:01:59 -0300121 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;
Fabio Utzig12d59162019-11-28 10:01:59 -0300136 int area_id;
137 int rc;
138
Fabio Utzigb86e6882019-12-10 09:51:17 -0300139 bs->source = swap_status_source(state);
140 switch (bs->source) {
Fabio Utzig12d59162019-11-28 10:01:59 -0300141 case BOOT_STATUS_SOURCE_NONE:
142 return 0;
143
144#if MCUBOOT_SWAP_USING_SCRATCH
145 case BOOT_STATUS_SOURCE_SCRATCH:
146 area_id = FLASH_AREA_IMAGE_SCRATCH;
147 break;
148#endif
149
150 case BOOT_STATUS_SOURCE_PRIMARY_SLOT:
151 area_id = FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state));
152 break;
153
154 default:
155 assert(0);
156 return BOOT_EBADARGS;
157 }
158
159 rc = flash_area_open(area_id, &fap);
160 if (rc != 0) {
161 return BOOT_EFLASH;
162 }
163
164 rc = swap_read_status_bytes(fap, state, bs);
165 if (rc == 0) {
166 off = boot_swap_info_off(fap);
167 rc = flash_area_read_is_empty(fap, off, &swap_info, sizeof swap_info);
168 if (rc == 1) {
169 BOOT_SET_SWAP_INFO(swap_info, 0, BOOT_SWAP_TYPE_NONE);
170 rc = 0;
171 }
172
173 /* Extract the swap type info */
174 bs->swap_type = BOOT_GET_SWAP_TYPE(swap_info);
175 }
176
177 flash_area_close(fap);
178
179 return rc;
180}
181
182int
183swap_set_copy_done(uint8_t image_index)
184{
185 const struct flash_area *fap;
186 int rc;
187
188 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
189 &fap);
190 if (rc != 0) {
191 return BOOT_EFLASH;
192 }
193
194 rc = boot_write_copy_done(fap);
195 flash_area_close(fap);
196 return rc;
197}
198
199int
200swap_set_image_ok(uint8_t image_index)
201{
202 const struct flash_area *fap;
203 struct boot_swap_state state;
204 int rc;
205
206 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
207 &fap);
208 if (rc != 0) {
209 return BOOT_EFLASH;
210 }
211
212 rc = boot_read_swap_state(fap, &state);
213 if (rc != 0) {
214 rc = BOOT_EFLASH;
215 goto out;
216 }
217
218 if (state.image_ok == BOOT_FLAG_UNSET) {
219 rc = boot_write_image_ok(fap);
220 }
221
222out:
223 flash_area_close(fap);
224 return rc;
225}
226
227
228#endif /* defined(MCUBOOT_SWAP_USING_SCRATCH) || defined(MCUBOOT_SWAP_USING_MOVE) */