./
Coverage:
low: ≥ 0%
medium: ≥ 50.0%
high: ≥ 80.0%
Lines:
85 / 88
96.6%
Branches:
52 / 65
80.0%
Functions:
5 / 6
83.3%

kfsw-services/src/boot_diagnostics.c
Line Branch Exec Source
1 #include <errno.h>
2 #include <string.h>
3
4 #include <zephyr/fs/fs.h>
5 #include <zephyr/kernel.h>
6 #include <zephyr/sys/byteorder.h>
7 #include <zephyr/sys/crc.h>
8
9 #include <kfsw/services/boot.h>
10 #define KFSW_LOG_MODULE KFSW_LOG_MODULE_BOOT
11 #include <kfsw/services/log.h>
12 #if CONFIG_KFSW_STORAGE
13 #include <kfsw/platform/storage.h>
14 #include <kfsw/services/snapshot.h>
15 #endif
16 #if CONFIG_BOOTLOADER_MCUBOOT && CONFIG_MCUBOOT_IMG_MANAGER
17 #include <bootutil/image.h>
18 #include <zephyr/dfu/mcuboot.h>
19 #include <zephyr/storage/flash_map.h>
20 #endif
21
22 #define RECORD_MAGIC_SIZE 4U
23 #define RECORD_IMAGE_OFFSET 4U
24 #define RECORD_ATTEMPTS_OFFSET 36U
25 #define RECORD_TRIAL_OFFSET 40U
26 #define RECORD_REASON_OFFSET 41U
27 #define RECORD_REVERT_OFFSET 42U
28 #define RECORD_PREVIOUS_IMAGE_OFFSET 44U
29 #define RECORD_SIZE 80U
30 #define RECORD_CRC_OFFSET 76U
31 #if CONFIG_KFSW_STORAGE
32 #define RECORD_PATH KFSW_STORAGE_MOUNT_POINT "/boot-trial.dat"
33 #define RECORD_TEMP_PATH KFSW_STORAGE_MOUNT_POINT "/boot-trial.tmp"
34 #define RECORD_REJECTED_PATH KFSW_STORAGE_MOUNT_POINT "/boot-trial.bad"
35 #endif
36
37 K_MUTEX_DEFINE(boot_trial_lock);
38 static uint8_t trial_quarantined;
39 static struct kfsw_boot_diagnostics diagnostics = {
40 .attempts = UINT32_MAX,
41 .revert_reason = UINT8_MAX,
42 };
43
44 34 void kfsw_boot_get_diagnostics(struct kfsw_boot_diagnostics *value)
45 {
46 34 k_mutex_lock(&boot_trial_lock, K_FOREVER);
47 34 *value = diagnostics;
48 34 value->quarantined = trial_quarantined;
49 34 k_mutex_unlock(&boot_trial_lock);
50 34 }
51
52 #if CONFIG_KFSW_STORAGE
53 6 static int quarantine_record(int reason)
54 {
55 6 int result = fs_rename(RECORD_PATH, RECORD_REJECTED_PATH);
56
57
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
6 if (result != 0) {
58 2 kfsw_log_error("Boot trial record rejected: %d; quarantine failed: %d", reason,
59 result);
60 2 return result;
61 }
62 4 kfsw_log_error("Boot trial record rejected: %d; moved to boot-trial.bad", reason);
63
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (trial_quarantined < UINT8_MAX) {
64 4 trial_quarantined++;
65 }
66 4 return 0;
67 }
68
69 24 static int load_record(uint8_t *record)
70 {
71 struct fs_file_t file;
72 uint8_t extra;
73 24 size_t offset = 0U;
74 int result;
75
76 24 fs_file_t_init(&file);
77 24 result = fs_open(&file, RECORD_PATH, FS_O_READ);
78
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 20 times.
24 if (result != 0) {
79 4 return result;
80 }
81
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 16 times.
40 while (offset < RECORD_SIZE) {
82 24 ssize_t count = fs_read(&file, &record[offset], RECORD_SIZE - offset);
83
84
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 20 times.
24 if (count <= 0) {
85
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 result = count < 0 ? (int)count : -EBADMSG;
86 4 break;
87 }
88 20 offset += (size_t)count;
89 }
90
3/4
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 16 times.
20 if ((result == 0) && (fs_read(&file, &extra, 1U) != 0)) {
91 ✗ result = -EBADMSG;
92 }
93 20 int close_result = fs_close(&file);
94
95
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 4 times.
20 if (result == 0) {
96 16 result = close_result;
97 }
98
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 16 times.
20 if (result != 0) {
99 4 return result;
100 }
101
1/3
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
16 if ((memcmp(record, "KBT1", RECORD_MAGIC_SIZE) != 0) ||
102
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 (record[RECORD_TRIAL_OFFSET] > 1U) ||
103
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 (record[RECORD_REASON_OFFSET] > KFSW_BOOT_REVERT_REPLACED_UNKNOWN) ||
104
2/4
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
32 (record[RECORD_REVERT_OFFSET] > 1U) ||
105
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 14 times.
32 (sys_get_be32(&record[RECORD_ATTEMPTS_OFFSET]) == UINT32_MAX) ||
106 16 (sys_get_be32(&record[RECORD_CRC_OFFSET]) != crc32_ieee(record, RECORD_CRC_OFFSET))) {
107 2 return -EBADMSG;
108 }
109 14 return 0;
110 }
111 #endif
112
113 28 int kfsw_boot_record_image(const uint8_t image[KFSW_BOOT_IMAGE_ID_SIZE], bool confirmed,
114 bool revert_pending)
115 {
116 28 int result = -ENOTSUP;
117
118 28 k_mutex_lock(&boot_trial_lock, K_FOREVER);
119 28 diagnostics = (struct kfsw_boot_diagnostics){
120 .attempts = UINT32_MAX,
121 .revert_reason = UINT8_MAX,
122 };
123 #if CONFIG_KFSW_STORAGE
124 28 uint8_t record[RECORD_SIZE] = {0};
125
126
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 26 times.
28 if (image == NULL) {
127 2 result = -EINVAL;
128 2 goto out;
129 }
130
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 24 times.
26 if (!kfsw_storage_is_ready()) {
131 2 result = -ENODEV;
132 2 goto out;
133 }
134 24 result = load_record(record);
135 /* Only a record that fails its checks is moved aside. A read or open
136 * error may be transient, and the record behind it may be sound.
137 */
138
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 18 times.
24 if (result == -EBADMSG) {
139
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 4 times.
6 if (quarantine_record(result) != 0) {
140 2 goto out;
141 }
142 4 result = -ENOENT;
143 }
144
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 16 times.
22 if (result == -ENOENT) {
145 6 memset(record, 0, sizeof(record));
146 6 memcpy(record, "KBT1", RECORD_MAGIC_SIZE);
147 }
148
2/4
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
22 if ((record[RECORD_TRIAL_OFFSET] != 0U) &&
149
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 8 times.
12 (memcmp(&record[RECORD_IMAGE_OFFSET], image, KFSW_BOOT_IMAGE_ID_SIZE) != 0)) {
150
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 record[RECORD_REASON_OFFSET] = record[RECORD_REVERT_OFFSET] != 0U
151 ? KFSW_BOOT_REVERT_UNCONFIRMED_REPLACED
152 : KFSW_BOOT_REVERT_REPLACED_UNKNOWN;
153 4 memcpy(&record[RECORD_PREVIOUS_IMAGE_OFFSET], &record[RECORD_IMAGE_OFFSET],
154 KFSW_BOOT_IMAGE_ID_SIZE);
155 }
156 22 uint32_t attempts = sys_get_be32(&record[RECORD_ATTEMPTS_OFFSET]);
157
158
2/4
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
22 if (confirmed ||
159
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 6 times.
16 (memcmp(&record[RECORD_IMAGE_OFFSET], image, KFSW_BOOT_IMAGE_ID_SIZE) != 0)) {
160 16 attempts = 0U;
161 }
162
4/4
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 4 times.
22 if (!confirmed && (attempts < UINT32_MAX - 1U)) {
163 12 attempts++;
164 }
165 22 memcpy(&record[RECORD_IMAGE_OFFSET], image, KFSW_BOOT_IMAGE_ID_SIZE);
166 22 sys_put_be32(attempts, &record[RECORD_ATTEMPTS_OFFSET]);
167 22 record[RECORD_TRIAL_OFFSET] = confirmed ? 0U : 1U;
168 /* Revert pending is observed in the trial image, not retained by MCUboot. */
169
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 8 times.
22 record[RECORD_REVERT_OFFSET] = revert_pending ? 1U : 0U;
170 22 sys_put_be32(crc32_ieee(record, RECORD_CRC_OFFSET), &record[RECORD_CRC_OFFSET]);
171 22 result = kfsw_snapshot_write(RECORD_PATH, RECORD_TEMP_PATH, record, sizeof(record));
172
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 18 times.
22 if (result == 0) {
173 18 diagnostics.attempts = attempts;
174 18 diagnostics.revert_reason = record[RECORD_REASON_OFFSET];
175 18 diagnostics.valid = true;
176 }
177 4 out:
178 #else
179 ARG_UNUSED(image);
180 ARG_UNUSED(confirmed);
181 ARG_UNUSED(revert_pending);
182 #endif
183 28 k_mutex_unlock(&boot_trial_lock);
184 28 return result;
185 }
186
187 #if CONFIG_BOOTLOADER_MCUBOOT && CONFIG_MCUBOOT_IMG_MANAGER
188 /* The signed image's SHA256 TLV identifies content even when versions repeat. */
189 static int running_image_id(uint8_t *image)
190 {
191 const struct flash_area *area;
192 struct image_header header;
193 struct image_tlv_info info;
194 uint32_t offset;
195 uint32_t end;
196 int result = flash_area_open(boot_fetch_active_slot(), &area);
197
198 if (result != 0) {
199 return result;
200 }
201 result = flash_area_read(area, 0U, &header, sizeof(header));
202 if (result != 0) {
203 goto out;
204 }
205 uint32_t header_size = sys_le16_to_cpu(header.ih_hdr_size);
206 uint32_t body_size = sys_le32_to_cpu(header.ih_img_size);
207 uint32_t protected_size = sys_le16_to_cpu(header.ih_protect_tlv_size);
208
209 result = -EBADMSG;
210 if ((sys_le32_to_cpu(header.ih_magic) != IMAGE_MAGIC) || (header_size > area->fa_size) ||
211 (body_size > area->fa_size - header_size) ||
212 (protected_size > area->fa_size - header_size - body_size)) {
213 goto out;
214 }
215 offset = header_size + body_size + protected_size;
216 if (area->fa_size - offset < sizeof(info)) {
217 goto out;
218 }
219 result = flash_area_read(area, offset, &info, sizeof(info));
220 if (result != 0) {
221 goto out;
222 }
223 uint32_t length = sys_le16_to_cpu(info.it_tlv_tot);
224
225 result = -EBADMSG;
226 if ((sys_le16_to_cpu(info.it_magic) != IMAGE_TLV_INFO_MAGIC) || (length < sizeof(info)) ||
227 (length > area->fa_size - offset)) {
228 goto out;
229 }
230 end = offset + length;
231 offset += sizeof(info);
232 while (end - offset >= sizeof(struct image_tlv)) {
233 struct image_tlv tlv;
234
235 result = flash_area_read(area, offset, &tlv, sizeof(tlv));
236 if (result != 0) {
237 goto out;
238 }
239 offset += sizeof(tlv);
240 length = sys_le16_to_cpu(tlv.it_len);
241 result = -EBADMSG;
242 if (length > end - offset) {
243 goto out;
244 }
245 if ((tlv.it_type == IMAGE_TLV_SHA256) && (length == KFSW_BOOT_IMAGE_ID_SIZE)) {
246 result = flash_area_read(area, offset, image, length);
247 goto out;
248 }
249 offset += length;
250 }
251 result = -ENOENT;
252 out:
253 flash_area_close(area);
254 return result;
255 }
256 #endif
257
258 2 int kfsw_boot_diagnostics_start(void)
259 {
260 #if CONFIG_BOOTLOADER_MCUBOOT && CONFIG_MCUBOOT_IMG_MANAGER
261 uint8_t image[KFSW_BOOT_IMAGE_ID_SIZE];
262 int result = running_image_id(image);
263
264 if (result != 0) {
265 k_mutex_lock(&boot_trial_lock, K_FOREVER);
266 diagnostics = (struct kfsw_boot_diagnostics){
267 .attempts = UINT32_MAX,
268 .revert_reason = UINT8_MAX,
269 };
270 k_mutex_unlock(&boot_trial_lock);
271 return result;
272 }
273 return kfsw_boot_record_image(image, boot_is_img_confirmed(),
274 mcuboot_swap_type() == BOOT_SWAP_TYPE_REVERT);
275 #else
276 2 return -ENOTSUP;
277 #endif
278 }
279
280 ✗ int kfsw_boot_confirm_image(void)
281 {
282 #if CONFIG_BOOTLOADER_MCUBOOT && CONFIG_MCUBOOT_IMG_MANAGER
283 int result = boot_write_img_confirmed();
284
285 if (result != 0) {
286 kfsw_log_error("Image confirmation failed: %d", result);
287 return result;
288 }
289 /* Re-read confirmation. The snapshot is bookkeeping and is retried at the
290 next startup, so its failure is reported and not returned: the image is
291 confirmed either way, and an image with no storage behind it, such as a
292 recovery build, would otherwise always look like a failed confirmation. */
293 result = kfsw_boot_diagnostics_start();
294 if (result != 0) {
295 kfsw_log_error("Confirmed image but trial diagnostics save failed: %d", result);
296 }
297 return 0;
298 #else
299 ✗ return -ENOTSUP;
300 #endif
301 }
302