Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions boards/native/native_sim/reboot_bottom.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <nsi_tasks.h>
#include <nsi_tracing.h>
#include <nsi_cmdline.h>
#include <nsi_host_trampolines.h>

static const char module[] = "native_sim_reboot";

Expand Down Expand Up @@ -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);
Expand Down
48 changes: 48 additions & 0 deletions drivers/hwinfo/hwinfo_native.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

#include <cmdline.h>
#include <nsi_host_trampolines.h>
#include <posix_native_task.h>
#include <string.h>
#include <zephyr/drivers/hwinfo.h>
Expand All @@ -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)
{
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ 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);
/* int nsi_host_printf (const char *fmt, ...); Use the nsi_tracing.h equivalents */
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);
Expand Down
10 changes: 10 additions & 0 deletions scripts/native_simulator/common/src/nsi_host_trampolines.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
9 changes: 9 additions & 0 deletions tests/boards/native_sim/reset_hw_info/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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})
6 changes: 6 additions & 0 deletions tests/boards/native_sim/reset_hw_info/prj.conf
Original file line number Diff line number Diff line change
@@ -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
29 changes: 29 additions & 0 deletions tests/boards/native_sim/reset_hw_info/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2025 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <stdio.h>
#include <zephyr/drivers/hwinfo.h>
#include <zephyr/sys/reboot.h>
#include <nsi_main.h>

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);
}
16 changes: 16 additions & 0 deletions tests/boards/native_sim/reset_hw_info/testcase.yaml
Original file line number Diff line number Diff line change
@@ -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"
Loading