./
Coverage:
low: ≥ 0%
medium: ≥ 50.0%
high: ≥ 80.0%
Lines:
108 / 156
69.2%
Branches:
42 / 68
61.8%
Functions:
11 / 15
73.3%

kfsw-services/src/scrub/scrub.c
Line Branch Exec Source
1 #define KFSW_LOG_MODULE KFSW_LOG_MODULE_SCRUB
2
3 #include <errno.h>
4 #include <string.h>
5
6 #include <zephyr/devicetree.h>
7 #include <zephyr/kernel.h>
8 #include <zephyr/storage/flash_map.h>
9 #include <zephyr/sys/byteorder.h>
10 #include <zephyr/sys/crc.h>
11
12 #include <kfsw/services/event.h>
13 #include <kfsw/services/fwu.h>
14 #include <kfsw/services/log.h>
15 #include <kfsw/services/scrub.h>
16 #if CONFIG_KFSW_PARAM_PERSISTENCE
17 #include <kfsw/services/parameter.h>
18 #endif
19
20 #define SLOT0_PRESENT DT_NODE_EXISTS(DT_NODELABEL(slot0_partition))
21 #define SLOT1_PRESENT DT_NODE_EXISTS(DT_NODELABEL(slot1_partition))
22 #define GOLDEN_NODE DT_CHOSEN(kfsw_golden_partition)
23 #define GOLDEN_PRESENT DT_NODE_EXISTS(GOLDEN_NODE)
24 #define PRESENT_REGIONS (SLOT0_PRESENT + SLOT1_PRESENT + GOLDEN_PRESENT)
25
26 /* Read in pieces: a slot is larger than anything worth putting on a stack. */
27 #define SCRUB_CHUNK 256U
28
29 /*
30 * The bootloader's image header as it sits on flash, little endian: the magic
31 * at 0, the header size at 8 and the image size at 12. Read from the bytes
32 * rather than through bootutil/image.h, which brings MCUboot's own
33 * configuration headers into a build that is not the bootloader.
34 */
35 #define IMAGE_HEADER_MAGIC 0x96f3b83dUL
36 #define IMAGE_HEADER_SIZE_AT 8U
37 #define IMAGE_SIZE_AT 12U
38 #define IMAGE_HEADER_READ 16U
39
40 struct region {
41 const char *name;
42 /* Flash area id, or -1 when the build has no such partition. */
43 int area;
44 /* Where the image starts inside the partition. */
45 uint32_t (*image_offset)(void);
46 };
47
48 32 static uint32_t at_partition_start(void)
49 {
50 32 return 0U;
51 }
52
53 /*
54 * MCUboot's swap-using-offset mode expects the secondary image one sector in,
55 * which is where FWU writes it. Reading slot1 from the start would find erased
56 * flash and report the region empty.
57 */
58 22 static uint32_t at_fwu_write_offset(void)
59 {
60 22 return kfsw_fwu_slot_write_offset();
61 }
62
63 static const struct region regions[KFSW_SCRUB_REGIONS] = {
64 [KFSW_SCRUB_SLOT0] =
65 {
66 .name = "slot0",
67 #if SLOT0_PRESENT
68 .area = DT_FIXED_PARTITION_ID(DT_NODELABEL(slot0_partition)),
69 #else
70 .area = -1,
71 #endif
72 .image_offset = at_partition_start,
73 },
74 [KFSW_SCRUB_SLOT1] =
75 {
76 .name = "slot1",
77 #if SLOT1_PRESENT
78 .area = DT_FIXED_PARTITION_ID(DT_NODELABEL(slot1_partition)),
79 #else
80 .area = -1,
81 #endif
82 .image_offset = at_fwu_write_offset,
83 },
84 [KFSW_SCRUB_GOLDEN] =
85 {
86 .name = "golden",
87 #if GOLDEN_PRESENT
88 .area = DT_FIXED_PARTITION_ID(GOLDEN_NODE),
89 #else
90 .area = -1,
91 #endif
92 .image_offset = at_partition_start,
93 },
94 };
95
96 static K_MUTEX_DEFINE(scrub_lock);
97 static struct kfsw_scrub_status scrub_state = {
98 .regions = PRESENT_REGIONS,
99 };
100 /* A region absent from this build says so, instead of reporting a state it
101 * never measured.
102 */
103 static struct kfsw_scrub_result results[KFSW_SCRUB_REGIONS] = {
104 [KFSW_SCRUB_SLOT0] = {.id = KFSW_SCRUB_SLOT0,
105 .state = SLOT0_PRESENT ? KFSW_SCRUB_UNKNOWN : KFSW_SCRUB_ABSENT},
106 [KFSW_SCRUB_SLOT1] = {.id = KFSW_SCRUB_SLOT1,
107 .state = SLOT1_PRESENT ? KFSW_SCRUB_UNKNOWN : KFSW_SCRUB_ABSENT},
108 [KFSW_SCRUB_GOLDEN] = {.id = KFSW_SCRUB_GOLDEN,
109 .state = GOLDEN_PRESENT ? KFSW_SCRUB_UNKNOWN : KFSW_SCRUB_ABSENT},
110 };
111 /* Persisted through the parameter table; the service reads and writes them. */
112 uint32_t kfsw_scrub_references[KFSW_SCRUB_REGIONS];
113 /* Latch a mismatch so one decayed image is counted once, not every sweep. */
114 static bool mismatching[KFSW_SCRUB_REGIONS];
115
116 static void scrub_work_handler(struct k_work *work);
117 K_WORK_DELAYABLE_DEFINE(scrub_work, scrub_work_handler);
118
119 /*
120 * The length to sweep comes from the image header, so an erased tail does not
121 * change the result and a larger partition does not slow the sweep.
122 */
123 54 static int image_length(const struct flash_area *area, uint32_t offset, uint32_t *length)
124 {
125 uint8_t header[IMAGE_HEADER_READ];
126 uint32_t header_size;
127 uint32_t image_size;
128
129
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 54 times.
54 if (flash_area_read(area, offset, header, sizeof(header)) != 0) {
130 ✗ return -EIO;
131 }
132
2/2
✓ Branch 1 taken 40 times.
✓ Branch 2 taken 14 times.
54 if (sys_get_le32(header) != IMAGE_HEADER_MAGIC) {
133 40 return -ENOENT;
134 }
135 14 header_size = sys_get_le16(&header[IMAGE_HEADER_SIZE_AT]);
136 14 image_size = sys_get_le32(&header[IMAGE_SIZE_AT]);
137
2/4
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 14 times.
14 if (header_size == 0U || image_size == 0U) {
138 ✗ return -ENOENT;
139 }
140 14 *length = header_size + image_size;
141
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (*length > (area->fa_size - offset)) {
142 ✗ return -ENOENT;
143 }
144 14 return 0;
145 }
146
147 54 static int measure(const struct region *region, uint32_t *bytes, uint32_t *crc)
148 {
149 const struct flash_area *area;
150 uint8_t chunk[SCRUB_CHUNK];
151 54 uint32_t offset = region->image_offset();
152 uint32_t length;
153 54 uint32_t done = 0U;
154 int result;
155
156
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 54 times.
54 if (flash_area_open((uint8_t)region->area, &area) != 0) {
157 ✗ return -EIO;
158 }
159 54 result = image_length(area, offset, &length);
160
2/2
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 14 times.
54 if (result != 0) {
161 40 flash_area_close(area);
162 40 return result;
163 }
164
165 14 *crc = 0U;
166
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 14 times.
56 while (done < length) {
167 42 uint32_t span = MIN(sizeof(chunk), length - done);
168
169
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 42 times.
42 if (flash_area_read(area, offset + done, chunk, span) != 0) {
170 ✗ flash_area_close(area);
171 ✗ return -EIO;
172 }
173 42 *crc = crc32_ieee_update(*crc, chunk, span);
174 42 done += span;
175 /* A sweep of a few hundred kilobytes must not hold the CPU
176 * against the watchdog feed.
177 */
178 42 k_yield();
179 }
180 14 flash_area_close(area);
181 14 *bytes = length;
182 14 return 0;
183 }
184
185 76 static void record(enum kfsw_scrub_region id, enum kfsw_scrub_state state, uint32_t bytes,
186 uint32_t measured)
187 {
188 76 results[id].state = (uint8_t)state;
189 76 results[id].bytes = bytes;
190 76 results[id].measured = measured;
191 76 results[id].reference = kfsw_scrub_references[id];
192 76 }
193
194 76 static int sweep_one(enum kfsw_scrub_region id)
195 {
196 76 const struct region *region = &regions[id];
197 76 uint32_t bytes = 0U;
198 76 uint32_t measured = 0U;
199 enum kfsw_scrub_state state;
200 int result;
201
202
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 54 times.
76 if (region->area < 0) {
203 22 record(id, KFSW_SCRUB_ABSENT, 0U, 0U);
204 22 return -ENODEV;
205 }
206
207 54 result = measure(region, &bytes, &measured);
208
2/2
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 14 times.
54 if (result == -ENOENT) {
209 40 state = KFSW_SCRUB_EMPTY;
210
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 } else if (result != 0) {
211 ✗ state = KFSW_SCRUB_UNREADABLE;
212
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 6 times.
14 } else if (kfsw_scrub_references[id] == 0U) {
213 8 state = KFSW_SCRUB_UNKNOWN;
214
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
6 } else if (kfsw_scrub_references[id] == measured) {
215 2 state = KFSW_SCRUB_OK;
216 } else {
217 4 state = KFSW_SCRUB_MISMATCH;
218 }
219
220 54 k_mutex_lock(&scrub_lock, K_FOREVER);
221 54 record(id, state, bytes, measured);
222
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 50 times.
54 if (state == KFSW_SCRUB_MISMATCH) {
223
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 if (!mismatching[id]) {
224 2 mismatching[id] = true;
225
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (scrub_state.mismatches < UINT32_MAX) {
226 2 scrub_state.mismatches++;
227 }
228 2 kfsw_log_error("Scrub: %s no longer matches its reference (%08x)",
229 region->name, measured);
230 #if CONFIG_KFSW_EVENT
231 {
232 uint8_t payload[5];
233
234 payload[0] = (uint8_t)id;
235 sys_put_be32(measured, &payload[1]);
236 kfsw_event_emit(KFSW_EVENT_SOURCE_SCRUB, KFSW_EVENT_SCRUB_MISMATCH,
237 KFSW_EVENT_ERROR, payload, sizeof(payload));
238 }
239 #endif
240 }
241 } else {
242 50 mismatching[id] = false;
243 }
244 54 k_mutex_unlock(&scrub_lock);
245 54 return 0;
246 }
247
248 18 int kfsw_scrub_sweep_region(enum kfsw_scrub_region region)
249 {
250
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 16 times.
18 if (region >= KFSW_SCRUB_REGIONS) {
251 2 return -EINVAL;
252 }
253 16 return sweep_one(region);
254 }
255
256 18 int kfsw_scrub_sweep(void)
257 {
258 18 int swept = 0;
259
260
2/2
✓ Branch 0 taken 54 times.
✓ Branch 1 taken 18 times.
72 for (unsigned int id = 0U; id < KFSW_SCRUB_REGIONS; id++) {
261
2/2
✓ Branch 1 taken 36 times.
✓ Branch 2 taken 18 times.
54 if (sweep_one((enum kfsw_scrub_region)id) == 0) {
262 36 swept++;
263 }
264 }
265
266 18 k_mutex_lock(&scrub_lock, K_FOREVER);
267
1/2
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
18 if (scrub_state.sweeps < UINT32_MAX) {
268 18 scrub_state.sweeps++;
269 }
270 18 scrub_state.last_sweep_ms = (uint32_t)k_uptime_get_32();
271 18 k_mutex_unlock(&scrub_lock);
272 18 return swept;
273 }
274
275 6 int kfsw_scrub_learn(enum kfsw_scrub_region region)
276 {
277 int result;
278
279
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (region >= KFSW_SCRUB_REGIONS) {
280 ✗ return -EINVAL;
281 }
282 6 result = sweep_one(region);
283
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
6 if (result != 0) {
284 2 return result;
285 }
286
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (results[region].state == KFSW_SCRUB_EMPTY) {
287 ✗ return -ENOENT;
288 }
289
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (results[region].state == KFSW_SCRUB_UNREADABLE) {
290 ✗ return -EIO;
291 }
292
293 4 k_mutex_lock(&scrub_lock, K_FOREVER);
294 4 kfsw_scrub_references[region] = results[region].measured;
295 4 results[region].reference = results[region].measured;
296 4 results[region].state = (uint8_t)KFSW_SCRUB_OK;
297 4 mismatching[region] = false;
298 4 k_mutex_unlock(&scrub_lock);
299
300 #if CONFIG_KFSW_PARAM_PERSISTENCE
301 /* A reference held only in RAM is relearned at every boot, and a
302 * reference that is always fresh can never report a mismatch.
303 */
304 result = kfsw_param_persist_save();
305 if (result != 0) {
306 kfsw_log_error("Scrub: could not persist the %s reference (%d)",
307 regions[region].name, result);
308 return -EIO;
309 }
310 #endif
311 4 kfsw_log_info("Scrub: %s reference is %08x over %u bytes", regions[region].name,
312 results[region].measured, results[region].bytes);
313 4 return 0;
314 }
315
316 ✗ static void scrub_work_handler(struct k_work *work)
317 {
318 ARG_UNUSED(work);
319
320 ✗ (void)kfsw_scrub_sweep();
321
322 ✗ k_mutex_lock(&scrub_lock, K_FOREVER);
323
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (scrub_state.running) {
324 ✗ (void)k_work_reschedule(&scrub_work, K_MSEC(CONFIG_KFSW_SCRUB_PERIOD_MS));
325 }
326 ✗ k_mutex_unlock(&scrub_lock);
327 ✗ }
328
329 ✗ int kfsw_scrub_start(void)
330 {
331 ✗ k_mutex_lock(&scrub_lock, K_FOREVER);
332
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (scrub_state.running) {
333 ✗ k_mutex_unlock(&scrub_lock);
334 ✗ return -EALREADY;
335 }
336 ✗ scrub_state.running = true;
337 ✗ k_mutex_unlock(&scrub_lock);
338
339 /* Sweep once now, so the regions are readable without waiting a period. */
340 ✗ (void)kfsw_scrub_sweep();
341 ✗ (void)k_work_reschedule(&scrub_work, K_MSEC(CONFIG_KFSW_SCRUB_PERIOD_MS));
342 ✗ kfsw_log_info("Image scrub started, every %u ms, %u region(s)", CONFIG_KFSW_SCRUB_PERIOD_MS,
343 scrub_state.regions);
344 ✗ return 0;
345 }
346
347 ✗ int kfsw_scrub_stop(void)
348 {
349 ✗ k_mutex_lock(&scrub_lock, K_FOREVER);
350
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (!scrub_state.running) {
351 ✗ k_mutex_unlock(&scrub_lock);
352 ✗ return -EALREADY;
353 }
354 ✗ scrub_state.running = false;
355 ✗ k_mutex_unlock(&scrub_lock);
356
357 ✗ (void)k_work_cancel_delayable(&scrub_work);
358 ✗ return 0;
359 }
360
361 8 void kfsw_scrub_get_status(struct kfsw_scrub_status *status)
362 {
363
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (status == NULL) {
364 ✗ return;
365 }
366 8 k_mutex_lock(&scrub_lock, K_FOREVER);
367 8 *status = scrub_state;
368 8 k_mutex_unlock(&scrub_lock);
369 }
370
371 18 int kfsw_scrub_get_result(enum kfsw_scrub_region region, struct kfsw_scrub_result *result)
372 {
373
3/4
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 16 times.
18 if (region >= KFSW_SCRUB_REGIONS || result == NULL) {
374 2 return -EINVAL;
375 }
376 16 k_mutex_lock(&scrub_lock, K_FOREVER);
377 16 *result = results[region];
378 16 k_mutex_unlock(&scrub_lock);
379 16 result->name = regions[region].name;
380 16 return 0;
381 }
382
383 ✗ void kfsw_scrub_visit_regions(kfsw_scrub_visitor_t visitor, void *context)
384 {
385
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (visitor == NULL) {
386 ✗ return;
387 }
388
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ for (unsigned int id = 0U; id < KFSW_SCRUB_REGIONS; id++) {
389 struct kfsw_scrub_result copy;
390
391 ✗ k_mutex_lock(&scrub_lock, K_FOREVER);
392 ✗ copy = results[id];
393 ✗ k_mutex_unlock(&scrub_lock);
394 ✗ copy.name = regions[id].name;
395
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ if (!visitor(&copy, context)) {
396 ✗ return;
397 }
398 }
399 }
400