1 /*
2  * SPDX-License-Identifier: Apache-2.0
3  *
4  * 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 
19 #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 
31 BOOT_LOG_MODULE_DECLARE(mcuboot);
32 
33 #if defined(MCUBOOT_SWAP_USING_SCRATCH) || defined(MCUBOOT_SWAP_USING_MOVE)
34 int
swap_erase_trailer_sectors(const struct boot_loader_state * state,const struct flash_area * fap)35 swap_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", flash_area_get_id(fap));
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 (flash_area_get_id(fap) == fa_id_primary) {
58         slot = BOOT_PRIMARY_SLOT;
59     } else if (flash_area_get_id(fap) == 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 
82 int
swap_status_init(const struct boot_loader_state * state,const struct flash_area * fap,const struct boot_status * bs)83 swap_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", flash_area_get_id(fap));
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);
118     assert(rc == 0);
119 
120     rc = boot_write_enc_key(fap, 1, bs);
121     assert(rc == 0);
122 #endif
123 
124     rc = boot_write_magic(fap);
125     assert(rc == 0);
126 
127     return 0;
128 }
129 
130 int
swap_read_status(struct boot_loader_state * state,struct boot_status * bs)131 swap_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 area_id;
137     int rc;
138 
139     bs->source = swap_status_source(state);
140     switch (bs->source) {
141     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(fap, off, &swap_info, sizeof swap_info);
168         if (rc != 0) {
169             rc = BOOT_EFLASH;
170             goto done;
171         }
172 
173         if (bootutil_buffer_is_erased(fap, &swap_info, sizeof swap_info)) {
174             BOOT_SET_SWAP_INFO(swap_info, 0, BOOT_SWAP_TYPE_NONE);
175             rc = 0;
176         }
177 
178         /* Extract the swap type info */
179         bs->swap_type = BOOT_GET_SWAP_TYPE(swap_info);
180     }
181 
182 done:
183     flash_area_close(fap);
184 
185     return rc;
186 }
187 
188 int
swap_set_copy_done(uint8_t image_index)189 swap_set_copy_done(uint8_t image_index)
190 {
191     const struct flash_area *fap;
192     int rc;
193 
194     rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
195             &fap);
196     if (rc != 0) {
197         return BOOT_EFLASH;
198     }
199 
200     rc = boot_write_copy_done(fap);
201     flash_area_close(fap);
202     return rc;
203 }
204 
205 int
swap_set_image_ok(uint8_t image_index)206 swap_set_image_ok(uint8_t image_index)
207 {
208     const struct flash_area *fap;
209     struct boot_swap_state state;
210     int rc;
211 
212     rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
213             &fap);
214     if (rc != 0) {
215         return BOOT_EFLASH;
216     }
217 
218     rc = boot_read_swap_state(fap, &state);
219     if (rc != 0) {
220         rc = BOOT_EFLASH;
221         goto out;
222     }
223 
224     if (state.image_ok == BOOT_FLAG_UNSET) {
225         rc = boot_write_image_ok(fap);
226     }
227 
228 out:
229     flash_area_close(fap);
230     return rc;
231 }
232 
233 
234 #endif /* defined(MCUBOOT_SWAP_USING_SCRATCH) || defined(MCUBOOT_SWAP_USING_MOVE) */
235