Skip to content

Commit 6e048bc

Browse files
tpamborcfriedt
authored andcommitted
drivers: hwinfo: native: report reset cause
Support reporting the reset cause for native_sim. The default is to report POR (Power-On Reset). If CONFIG_NATIVE_SIM_REBOOT was enabled and the system is rebooted using sys_reboot(), the reset cause is set to SOFTWARE. Signed-off-by: Tim Pambor <[email protected]>
1 parent 30e5ea0 commit 6e048bc

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

boards/native/native_sim/reboot_bottom.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <nsi_tasks.h>
1414
#include <nsi_tracing.h>
1515
#include <nsi_cmdline.h>
16+
#include <nsi_host_trampolines.h>
1617

1718
static const char module[] = "native_sim_reboot";
1819

@@ -63,6 +64,9 @@ void maybe_reboot(void)
6364
nsi_exit(1);
6465
}
6566

67+
/* Let's set an environment variable which the native_sim hw_info driver may check */
68+
(void)nsi_host_setenv("NATIVE_SIM_RESET_CAUSE", "SOFTWARE", 1);
69+
6670
nsi_print_warning("%s: Restarting process.\n", module);
6771

6872
(void)execv("/proc/self/exe", argv);

drivers/hwinfo/hwinfo_native.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55

66
#include <cmdline.h>
7+
#include <nsi_host_trampolines.h>
78
#include <posix_native_task.h>
89
#include <string.h>
910
#include <zephyr/drivers/hwinfo.h>
@@ -13,6 +14,7 @@
1314

1415
static uint32_t native_hwinfo_device_id;
1516
static bool native_hwinfo_device_id_set;
17+
static uint32_t native_hwinfo_reset_cause;
1618

1719
ssize_t z_impl_hwinfo_get_device_id(uint8_t *buffer, size_t length)
1820
{
@@ -26,6 +28,27 @@ ssize_t z_impl_hwinfo_get_device_id(uint8_t *buffer, size_t length)
2628
return length;
2729
}
2830

31+
int z_impl_hwinfo_get_reset_cause(uint32_t *cause)
32+
{
33+
*cause = native_hwinfo_reset_cause;
34+
35+
return 0;
36+
}
37+
38+
int z_impl_hwinfo_clear_reset_cause(void)
39+
{
40+
native_hwinfo_reset_cause = 0;
41+
42+
return 0;
43+
}
44+
45+
int z_impl_hwinfo_get_supported_reset_cause(uint32_t *supported)
46+
{
47+
*supported = RESET_POR | RESET_SOFTWARE;
48+
49+
return 0;
50+
}
51+
2952
static void native_hwinfo_gethostid(void)
3053
{
3154
if (!native_hwinfo_device_id_set) {
@@ -59,5 +82,30 @@ static void native_hwinfo_add_options(void)
5982
native_add_command_line_opts(native_hwinfo_options);
6083
}
6184

85+
static void native_hwinfo_get_reset_cause(void)
86+
{
87+
/* If CONFIG_NATIVE_SIM_REBOOT was set, and a reboot was triggered, this
88+
* environment variable would be set. Otherwise it is not expected to
89+
* exist. Note this environment variable is not an stable API of any kind
90+
*/
91+
const char *cause = nsi_host_getenv("NATIVE_SIM_RESET_CAUSE");
92+
93+
if (!cause) {
94+
/* Default to POR if not set */
95+
native_hwinfo_reset_cause = RESET_POR;
96+
return;
97+
}
98+
99+
if (strcmp(cause, "SOFTWARE") == 0) {
100+
native_hwinfo_reset_cause = RESET_SOFTWARE;
101+
} else {
102+
posix_print_warning("NATIVE_SIM_RESET_CAUSE (%s) set to an unknown reset cause, "
103+
"defaulting to POR\n",
104+
cause);
105+
native_hwinfo_reset_cause = RESET_POR;
106+
}
107+
}
108+
62109
NATIVE_TASK(native_hwinfo_add_options, PRE_BOOT_1, 10);
63110
NATIVE_TASK(native_hwinfo_gethostid, PRE_BOOT_2, 10);
111+
NATIVE_TASK(native_hwinfo_get_reset_cause, PRE_BOOT_2, 10);

0 commit comments

Comments
 (0)