./
Coverage:
low: ≥ 0%
medium: ≥ 50.0%
high: ≥ 80.0%
Lines:
32 / 41
78.0%
Branches:
7 / 16
43.8%
Functions:
7 / 7
100.0%

kfsw-services/src/boot.c
Line Branch Exec Source
1 #include <errno.h>
2 #include <stdint.h>
3
4 #include <zephyr/sys/byteorder.h>
5 #include <zephyr/init.h>
6
7 #include <kfsw/platform/hardware.h>
8 #include <kfsw/platform/reset.h>
9 #include <kfsw/platform/time.h>
10 #include <kfsw/services/boot.h>
11 #define KFSW_LOG_MODULE KFSW_LOG_MODULE_BOOT
12 #include <kfsw/services/log.h>
13 #if CONFIG_KFSW_EVENT
14 #include <kfsw/services/event.h>
15 #endif
16
17 /* Read once at boot: reading the reset cause clears it. */
18 static uint32_t boot_reset_cause;
19 #if CONFIG_KFSW_LASTWORDS
20 static struct kfsw_lastwords boot_lastwords;
21 #endif
22 static int boot_reset_rc = -EAGAIN;
23 /* Read once and shared by the boot marker, the shell and the board table. */
24 static char boot_hardware_id[KFSW_HARDWARE_ID_TEXT_SIZE];
25
26 8 static int capture_reset(void)
27 {
28 #if CONFIG_KFSW_LASTWORDS
29 4 (void)kfsw_lastwords_take(&boot_lastwords);
30 #endif
31 #if CONFIG_HWINFO
32 4 boot_reset_rc = kfsw_platform_get_reset_cause(&boot_reset_cause);
33 #else
34 4 boot_reset_rc = -ENOTSUP;
35 #endif
36 8 return 0;
37 }
38
39 SYS_INIT(capture_reset, POST_KERNEL, 51);
40
41 4 const char *kfsw_boot_get_hardware_id(void)
42 {
43 4 return boot_hardware_id;
44 }
45
46 6 uint32_t kfsw_boot_get_reset_cause(void)
47 {
48 6 return boot_reset_cause;
49 }
50
51 6 const char *kfsw_boot_get_image_version(void)
52 {
53 6 return KFSW_IMAGE_VERSION;
54 }
55
56 2 const char *kfsw_boot_get_revisions(void)
57 {
58 2 return KFSW_BUILD_REVISIONS;
59 }
60
61 6 void kfsw_boot_service_start(void)
62 {
63 6 uint32_t reset_cause = boot_reset_cause;
64 6 int reset_rc = boot_reset_rc;
65
66
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 if (kfsw_platform_get_hardware_id(boot_hardware_id, sizeof(boot_hardware_id)) != 0) {
67 ✗ boot_hardware_id[0] = '\0';
68 }
69
70
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (reset_rc != 0) {
71 ✗ kfsw_log_warning("Reset cause unavailable: %d", reset_rc);
72 }
73
74 /* CI and HIL tests parse these markers. The raw mask is kept because several
75 * causes can be latched at once.
76 */
77
1/2
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
6 kfsw_log_marker("@BOOT sw=%s board=%s image=%s unit=%s reset=0x%08x reset_rc=%d "
78 "reset_cause=%s",
79 KFSW_IMAGE_VERSION, CONFIG_BOARD_TARGET,
80 IS_ENABLED(CONFIG_KFSW_GOLDEN_IMAGE) ? "golden" : "application",
81 (boot_hardware_id[0] != '\0') ? boot_hardware_id : "unknown",
82 (unsigned int)reset_cause, reset_rc,
83 kfsw_platform_reset_cause_name(reset_cause));
84
85
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 if (kfsw_platform_reset_cause_is_watchdog(reset_cause)) {
86 ✗ kfsw_log_warning("Previous run was ended by the watchdog");
87 }
88
89 6 kfsw_log_info("Boot checks complete at %llu us",
90 (unsigned long long)kfsw_time_monotonic_us());
91
92 #if CONFIG_KFSW_EVENT
93 /* Record the reset cause as an event too. */
94 {
95 uint8_t payload[5];
96
97 6 sys_put_be32(reset_cause, &payload[0]);
98 6 payload[4] = (reset_rc == 0) ? 0U : 1U;
99 /* A watchdog reset is logged as a warning. */
100 6 kfsw_event_emit(
101 KFSW_EVENT_SOURCE_BOOT, KFSW_EVENT_BOOT_READY,
102 6 ((reset_rc != 0) || kfsw_platform_reset_cause_is_watchdog(reset_cause))
103 ? KFSW_EVENT_WARNING
104
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
12 : KFSW_EVENT_INFO,
105 payload, sizeof(payload));
106 }
107 #endif
108
109 #if CONFIG_KFSW_LASTWORDS
110
1/4
✗ Branch 0 not taken.
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 1 taken 6 times.
6 if (boot_lastwords.reason != KFSW_LASTWORDS_NONE) {
111 ✗ kfsw_log_warning("Previous run said: %s (detail %u) after %u ms",
112 kfsw_lastwords_reason_name(boot_lastwords.reason),
113 boot_lastwords.detail, boot_lastwords.uptime_ms);
114 #if CONFIG_KFSW_EVENT
115 {
116 uint8_t payload[13];
117
118 ✗ payload[0] = (uint8_t)boot_lastwords.reason;
119 ✗ sys_put_be32(boot_lastwords.detail, &payload[1]);
120 ✗ sys_put_be32(boot_lastwords.uptime_ms, &payload[5]);
121 ✗ sys_put_be32(boot_lastwords.boot_count, &payload[9]);
122 /* A note from the previous run is logged as a warning. */
123 ✗ kfsw_event_emit(KFSW_EVENT_SOURCE_BOOT, KFSW_EVENT_BOOT_LASTWORDS,
124 KFSW_EVENT_WARNING, payload, sizeof(payload));
125 }
126 #endif
127 }
128 #endif
129
130 6 kfsw_log_marker("@SOURCE %s", KFSW_SOURCE_REVISION);
131 6 kfsw_log_marker("@READY uptime_ms=%lld", (long long)kfsw_time_monotonic_ms());
132 6 }
133
134 #if CONFIG_KFSW_LASTWORDS
135 2 const struct kfsw_lastwords *kfsw_boot_get_lastwords(void)
136 {
137 2 return &boot_lastwords;
138 }
139 #endif
140