From 212ecc00c46acceb7a00d669e9cf939b9dab7bdf Mon Sep 17 00:00:00 2001 From: Tim Pambor Date: Sun, 28 Sep 2025 07:34:20 +0200 Subject: [PATCH 1/3] native_simulator: Get latest from upstream Align with native_simulator's upstream main 4eab13716376e63236d77013f996a897d24dd780 Which includes: 4eab137 Host trampolines: Add getenv/setenv Signed-off-by: Tim Pambor --- .../common/src/include/nsi_host_trampolines.h | 2 ++ .../native_simulator/common/src/nsi_host_trampolines.c | 10 ++++++++++ 2 files changed, 12 insertions(+) diff --git a/scripts/native_simulator/common/src/include/nsi_host_trampolines.h b/scripts/native_simulator/common/src/include/nsi_host_trampolines.h index 31f954ee0cc43..efb108613f0fe 100644 --- a/scripts/native_simulator/common/src/include/nsi_host_trampolines.h +++ b/scripts/native_simulator/common/src/include/nsi_host_trampolines.h @@ -27,6 +27,7 @@ int nsi_host_close(int fd); /* void nsi_host_exit (int status); Use nsi_exit() instead */ void nsi_host_free(void *ptr); char *nsi_host_getcwd(char *buf, unsigned long size); +char *nsi_host_getenv(const char *name); int nsi_host_isatty(int fd); void *nsi_host_malloc(unsigned long size); int nsi_host_open(const char *pathname, int flags); @@ -34,6 +35,7 @@ int nsi_host_open(const char *pathname, int flags); long nsi_host_random(void); long nsi_host_read(int fd, void *buffer, unsigned long size); void *nsi_host_realloc(void *ptr, unsigned long size); +int nsi_host_setenv(const char *name, const char *value, int overwrite); void nsi_host_srandom(unsigned int seed); char *nsi_host_strdup(const char *s); long nsi_host_write(int fd, const void *buffer, unsigned long size); diff --git a/scripts/native_simulator/common/src/nsi_host_trampolines.c b/scripts/native_simulator/common/src/nsi_host_trampolines.c index eb378b7533f96..e34c59c0e5513 100644 --- a/scripts/native_simulator/common/src/nsi_host_trampolines.c +++ b/scripts/native_simulator/common/src/nsi_host_trampolines.c @@ -31,6 +31,11 @@ char *nsi_host_getcwd(char *buf, unsigned long size) return getcwd(buf, size); } +char *nsi_host_getenv(const char *name) +{ + return getenv(name); +} + int nsi_host_isatty(int fd) { return isatty(fd); @@ -61,6 +66,11 @@ void *nsi_host_realloc(void *ptr, unsigned long size) return realloc(ptr, size); } +int nsi_host_setenv(const char *name, const char *value, int overwrite) +{ + return setenv(name, value, overwrite); +} + void nsi_host_srandom(unsigned int seed) { srandom(seed); From 503bdf5402345b3da8fdf393e1089f6b1acb369b Mon Sep 17 00:00:00 2001 From: Tim Pambor Date: Sun, 28 Sep 2025 07:38:38 +0200 Subject: [PATCH 2/3] 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 --- boards/native/native_sim/reboot_bottom.c | 4 ++ drivers/hwinfo/hwinfo_native.c | 48 ++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/boards/native/native_sim/reboot_bottom.c b/boards/native/native_sim/reboot_bottom.c index 21ec7a2634f25..fafadd16948fe 100644 --- a/boards/native/native_sim/reboot_bottom.c +++ b/boards/native/native_sim/reboot_bottom.c @@ -13,6 +13,7 @@ #include #include #include +#include static const char module[] = "native_sim_reboot"; @@ -63,6 +64,9 @@ void maybe_reboot(void) nsi_exit(1); } + /* Let's set an environment variable which the native_sim hw_info driver may check */ + (void)nsi_host_setenv("NATIVE_SIM_RESET_CAUSE", "SOFTWARE", 1); + nsi_print_warning("%s: Restarting process.\n", module); (void)execv("/proc/self/exe", argv); diff --git a/drivers/hwinfo/hwinfo_native.c b/drivers/hwinfo/hwinfo_native.c index 2e02c9cceb7ee..28d62c904fa89 100644 --- a/drivers/hwinfo/hwinfo_native.c +++ b/drivers/hwinfo/hwinfo_native.c @@ -4,6 +4,7 @@ */ #include +#include #include #include #include @@ -13,6 +14,7 @@ static uint32_t native_hwinfo_device_id; static bool native_hwinfo_device_id_set; +static uint32_t native_hwinfo_reset_cause; ssize_t z_impl_hwinfo_get_device_id(uint8_t *buffer, size_t length) { @@ -26,6 +28,27 @@ ssize_t z_impl_hwinfo_get_device_id(uint8_t *buffer, size_t length) return length; } +int z_impl_hwinfo_get_reset_cause(uint32_t *cause) +{ + *cause = native_hwinfo_reset_cause; + + return 0; +} + +int z_impl_hwinfo_clear_reset_cause(void) +{ + native_hwinfo_reset_cause = 0; + + return 0; +} + +int z_impl_hwinfo_get_supported_reset_cause(uint32_t *supported) +{ + *supported = RESET_POR | RESET_SOFTWARE; + + return 0; +} + static void native_hwinfo_gethostid(void) { if (!native_hwinfo_device_id_set) { @@ -59,5 +82,30 @@ static void native_hwinfo_add_options(void) native_add_command_line_opts(native_hwinfo_options); } +static void native_hwinfo_get_reset_cause(void) +{ + /* If CONFIG_NATIVE_SIM_REBOOT was set, and a reboot was triggered, this + * environment variable would be set. Otherwise it is not expected to + * exist. Note this environment variable is not an stable API of any kind + */ + const char *cause = nsi_host_getenv("NATIVE_SIM_RESET_CAUSE"); + + if (!cause) { + /* Default to POR if not set */ + native_hwinfo_reset_cause = RESET_POR; + return; + } + + if (strcmp(cause, "SOFTWARE") == 0) { + native_hwinfo_reset_cause = RESET_SOFTWARE; + } else { + posix_print_warning("NATIVE_SIM_RESET_CAUSE (%s) set to an unknown reset cause, " + "defaulting to POR\n", + cause); + native_hwinfo_reset_cause = RESET_POR; + } +} + NATIVE_TASK(native_hwinfo_add_options, PRE_BOOT_1, 10); NATIVE_TASK(native_hwinfo_gethostid, PRE_BOOT_2, 10); +NATIVE_TASK(native_hwinfo_get_reset_cause, PRE_BOOT_2, 10); From ce0356325af7e2e2d6cb8299e986c322992eca84 Mon Sep 17 00:00:00 2001 From: Alberto Escolar Piedras Date: Mon, 8 Sep 2025 12:07:36 +0200 Subject: [PATCH 3/3] board native_sim: Test sys_reboot and hw_info reporting the reset cause Add a test of the CONFIG_NATIVE_SIM_REBOOT functionality together with the hw_info get_reset_cause() logic. Signed-off-by: Alberto Escolar Piedras --- .../native_sim/reset_hw_info/CMakeLists.txt | 9 ++++++ .../boards/native_sim/reset_hw_info/prj.conf | 6 ++++ .../native_sim/reset_hw_info/src/main.c | 29 +++++++++++++++++++ .../native_sim/reset_hw_info/testcase.yaml | 16 ++++++++++ 4 files changed, 60 insertions(+) create mode 100644 tests/boards/native_sim/reset_hw_info/CMakeLists.txt create mode 100644 tests/boards/native_sim/reset_hw_info/prj.conf create mode 100644 tests/boards/native_sim/reset_hw_info/src/main.c create mode 100644 tests/boards/native_sim/reset_hw_info/testcase.yaml diff --git a/tests/boards/native_sim/reset_hw_info/CMakeLists.txt b/tests/boards/native_sim/reset_hw_info/CMakeLists.txt new file mode 100644 index 0000000000000..f2456fc9cdf3e --- /dev/null +++ b/tests/boards/native_sim/reset_hw_info/CMakeLists.txt @@ -0,0 +1,9 @@ +# Copyright (c) 2025 Nordic Semiconductor ASA +# SPDX-License-Identifier: Apache-2.0 + +cmake_minimum_required(VERSION 3.20.0) +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(native_reset_hw_info) + +FILE(GLOB app_sources src/*.c) +target_sources(app PRIVATE ${app_sources}) diff --git a/tests/boards/native_sim/reset_hw_info/prj.conf b/tests/boards/native_sim/reset_hw_info/prj.conf new file mode 100644 index 0000000000000..beb465042537d --- /dev/null +++ b/tests/boards/native_sim/reset_hw_info/prj.conf @@ -0,0 +1,6 @@ +# Copyright (c) 2025 Nordic Semiconductor ASA +# SPDX-License-Identifier: Apache-2.0 + +CONFIG_REBOOT=y +CONFIG_NATIVE_SIM_REBOOT=y +CONFIG_HWINFO=y diff --git a/tests/boards/native_sim/reset_hw_info/src/main.c b/tests/boards/native_sim/reset_hw_info/src/main.c new file mode 100644 index 0000000000000..7a8d5bf7c49a5 --- /dev/null +++ b/tests/boards/native_sim/reset_hw_info/src/main.c @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2025 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include +#include + +int main(void) +{ + uint32_t cause; + int err; + + err = hwinfo_get_reset_cause(&cause); + if (err != 0) { + posix_print_error_and_exit("hwinfo_get_reset_cause() failed %i\n", err); + } + + if (cause == RESET_POR) { + printf("This seems like the first start => Resetting\n"); + sys_reboot(SYS_REBOOT_WARM); + } else if (cause == RESET_SOFTWARE) { + printf("Booted after SOFTWARE reset => we are done\n"); + } + nsi_exit(0); +} diff --git a/tests/boards/native_sim/reset_hw_info/testcase.yaml b/tests/boards/native_sim/reset_hw_info/testcase.yaml new file mode 100644 index 0000000000000..00d5601483e16 --- /dev/null +++ b/tests/boards/native_sim/reset_hw_info/testcase.yaml @@ -0,0 +1,16 @@ +tests: + boards.native_sim.reset_hw_info: + platform_allow: + - native_sim + - native_sim/native/64 + integration_platforms: + - native_sim + harness: console + harness_config: + type: multi_line + ordered: true + regex: + - "(.*)Booting Zephyr OS build(.*)" + - "This seems like the first start => Resetting" + - "(.*)Booting Zephyr OS build(.*)" + - "Booted after SOFTWARE reset => we are done"