Skip to content

Commit 25ddcc9

Browse files
committed
drivers: hwinfo: native: report reset cause
Support reporting the reset cause for native_sim. The default is to report POR (Power-On Reset). If the system is rebooted using sys_reboot(), the reset cause is set to SOFTWARE. Signed-off-by: Tim Pambor <[email protected]>
1 parent 1f3fb17 commit 25ddcc9

File tree

4 files changed

+61
-0
lines changed

4 files changed

+61
-0
lines changed

boards/native/native_sim/reboot_bottom.c

Lines changed: 5 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,10 @@ void maybe_reboot(void)
6364
nsi_exit(1);
6465
}
6566

67+
if (nsi_host_setenv("NATIVE_SIM_RESET_CAUSE", "SOFTWARE", 1) < 0) {
68+
nsi_exit(1);
69+
}
70+
6671
nsi_print_warning("%s: Restarting process.\n", module);
6772

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

drivers/hwinfo/hwinfo_native.c

Lines changed: 44 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,26 @@ 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+
const char *cause = nsi_host_getenv("NATIVE_SIM_RESET_CAUSE");
88+
89+
if (!cause) {
90+
/* Default to POR if not set */
91+
native_hwinfo_reset_cause = RESET_POR;
92+
return;
93+
}
94+
95+
if (strcmp(cause, "POR") == 0) {
96+
native_hwinfo_reset_cause = RESET_POR;
97+
} else if (strcmp(cause, "SOFTWARE") == 0) {
98+
native_hwinfo_reset_cause = RESET_SOFTWARE;
99+
} else {
100+
posix_print_warning("Unknown reset cause, defaulting to POR\n");
101+
native_hwinfo_reset_cause = RESET_POR;
102+
}
103+
}
104+
62105
NATIVE_TASK(native_hwinfo_add_options, PRE_BOOT_1, 10);
63106
NATIVE_TASK(native_hwinfo_gethostid, PRE_BOOT_2, 10);
107+
NATIVE_TASK(native_hwinfo_get_reset_cause, PRE_BOOT_2, 10);

scripts/native_simulator/common/src/include/nsi_host_trampolines.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ void *nsi_host_realloc(void *ptr, unsigned long size);
3737
void nsi_host_srandom(unsigned int seed);
3838
char *nsi_host_strdup(const char *s);
3939
long nsi_host_write(int fd, const void *buffer, unsigned long size);
40+
char *nsi_host_getenv(const char *name);
41+
int nsi_host_setenv(const char *name, const char *value, int overwrite);
4042

4143
#ifdef __cplusplus
4244
}

scripts/native_simulator/common/src/nsi_host_trampolines.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,13 @@ long nsi_host_write(int fd, const void *buffer, unsigned long size)
7575
{
7676
return write(fd, buffer, size);
7777
}
78+
79+
char *nsi_host_getenv(const char *name)
80+
{
81+
return getenv(name);
82+
}
83+
84+
int nsi_host_setenv(const char *name, const char *value, int overwrite)
85+
{
86+
return setenv(name, value, overwrite);
87+
}

0 commit comments

Comments
 (0)