diff --git a/boards/native/native_sim/doc/index.rst b/boards/native/native_sim/doc/index.rst index d7b6cea697a14..076cc1fd7cf46 100644 --- a/boards/native/native_sim/doc/index.rst +++ b/boards/native/native_sim/doc/index.rst @@ -721,6 +721,7 @@ host libC (:kconfig:option:`CONFIG_EXTERNAL_LIBC`): FUSE, :ref:`Host based filesystem access `, :kconfig:option:`CONFIG_FUSE_FS_ACCESS`, All GPIO, GPIO emulator, :kconfig:option:`CONFIG_GPIO_EMUL`, All GPIO, SDL GPIO emulator, :kconfig:option:`CONFIG_GPIO_EMUL_SDL`, All + HWINFO, :kconfig:option:`CONFIG_HWINFO_NATIVE`, All I2C, I2C emulator, :kconfig:option:`CONFIG_I2C_EMUL`, All Input, Input SDL touch, :kconfig:option:`CONFIG_INPUT_SDL_TOUCH`, All Input, Linux evdev, :kconfig:option:`CONFIG_NATIVE_LINUX_EVDEV`, All diff --git a/boards/native/native_sim/native_sim.yaml b/boards/native/native_sim/native_sim.yaml index 8a476ba2771a2..2e16e91981bd8 100644 --- a/boards/native/native_sim/native_sim.yaml +++ b/boards/native/native_sim/native_sim.yaml @@ -23,6 +23,7 @@ supported: - spi - gpio - rtc + - hwinfo testing: default: true vendor: zephyr diff --git a/boards/native/native_sim/native_sim_native_64.yaml b/boards/native/native_sim/native_sim_native_64.yaml index 24a4bb082ea8a..4656497412ff3 100644 --- a/boards/native/native_sim/native_sim_native_64.yaml +++ b/boards/native/native_sim/native_sim_native_64.yaml @@ -20,4 +20,5 @@ supported: - adc - gpio - rtc + - hwinfo vendor: zephyr diff --git a/drivers/hwinfo/CMakeLists.txt b/drivers/hwinfo/CMakeLists.txt index 85fb6b8eba5b1..f1dcc284eb01c 100644 --- a/drivers/hwinfo/CMakeLists.txt +++ b/drivers/hwinfo/CMakeLists.txt @@ -40,3 +40,13 @@ zephyr_library_sources_ifdef(CONFIG_HWINFO_SILABS_S2 hwinfo_silabs_series2.c) zephyr_library_sources_ifdef(CONFIG_HWINFO_SMARTBOND hwinfo_smartbond.c) zephyr_library_sources_ifdef(CONFIG_HWINFO_STM32 hwinfo_stm32.c) # zephyr-keep-sorted-stop + +if(CONFIG_HWINFO_NATIVE) + if(${CMAKE_HOST_SYSTEM_NAME} STREQUAL Linux) + zephyr_library_sources(hwinfo_native.c) + + target_sources(native_simulator INTERFACE hwinfo_native_bottom.c) + else() + message(FATAL_ERROR "CONFIG_HWINFO_NATIVE is only available on Linux") + endif() +endif() diff --git a/drivers/hwinfo/Kconfig b/drivers/hwinfo/Kconfig index 89e47925773c8..86d52152e64bd 100644 --- a/drivers/hwinfo/Kconfig +++ b/drivers/hwinfo/Kconfig @@ -43,6 +43,7 @@ rsource "Kconfig.mcux_src" rsource "Kconfig.mcux_src_rev2" rsource "Kconfig.mcux_syscon" rsource "Kconfig.mspm0" +rsource "Kconfig.native" rsource "Kconfig.nrf" rsource "Kconfig.numaker" rsource "Kconfig.numaker_rmc" diff --git a/drivers/hwinfo/Kconfig.native b/drivers/hwinfo/Kconfig.native new file mode 100644 index 0000000000000..9ff5b3695c27a --- /dev/null +++ b/drivers/hwinfo/Kconfig.native @@ -0,0 +1,10 @@ +# Copyright (c) 2025 Henrik Brix Andersen +# SPDX-License-Identifier: Apache-2.0 + +config HWINFO_NATIVE + bool "HWINFO driver for native_sim" + default y + depends on ARCH_POSIX + select HWINFO_HAS_DRIVER + help + Enable native_sim HWINFO driver. diff --git a/drivers/hwinfo/hwinfo_native.c b/drivers/hwinfo/hwinfo_native.c new file mode 100644 index 0000000000000..2e02c9cceb7ee --- /dev/null +++ b/drivers/hwinfo/hwinfo_native.c @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2025 Henrik Brix Andersen + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include +#include +#include + +#include "hwinfo_native_bottom.h" + +static uint32_t native_hwinfo_device_id; +static bool native_hwinfo_device_id_set; + +ssize_t z_impl_hwinfo_get_device_id(uint8_t *buffer, size_t length) +{ + + if (length > sizeof(native_hwinfo_device_id)) { + length = sizeof(native_hwinfo_device_id); + } + + sys_put_be(buffer, &native_hwinfo_device_id, length); + + return length; +} + +static void native_hwinfo_gethostid(void) +{ + if (!native_hwinfo_device_id_set) { + native_hwinfo_device_id = native_hwinfo_gethostid_bottom(); + } +} + +static void native_hwinfo_device_id_was_set(char *argv, int offset) +{ + ARG_UNUSED(argv); + ARG_UNUSED(offset); + + native_hwinfo_device_id_set = true; +} + +static void native_hwinfo_add_options(void) +{ + static struct args_struct_t native_hwinfo_options[] = { + { + .option = "device_id", + .name = "id", + .type = 'u', + .dest = (void *)&native_hwinfo_device_id, + .call_when_found = native_hwinfo_device_id_was_set, + .descript = "A 32-bit integer value to use as HWINFO device ID. " + "If not set, the host gethostid() output will be used.", + }, + ARG_TABLE_ENDMARKER, + }; + + native_add_command_line_opts(native_hwinfo_options); +} + +NATIVE_TASK(native_hwinfo_add_options, PRE_BOOT_1, 10); +NATIVE_TASK(native_hwinfo_gethostid, PRE_BOOT_2, 10); diff --git a/drivers/hwinfo/hwinfo_native_bottom.c b/drivers/hwinfo/hwinfo_native_bottom.c new file mode 100644 index 0000000000000..a3401f4607824 --- /dev/null +++ b/drivers/hwinfo/hwinfo_native_bottom.c @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Henrik Brix Andersen + * SPDX-License-Identifier: Apache-2.0 + */ + +#undef _XOPEN_SOURCE +#define _XOPEN_SOURCE 500 + +#include + +#include "hwinfo_native_bottom.h" + +long native_hwinfo_gethostid_bottom(void) +{ + return gethostid(); +} diff --git a/drivers/hwinfo/hwinfo_native_bottom.h b/drivers/hwinfo/hwinfo_native_bottom.h new file mode 100644 index 0000000000000..8d09e8418a91a --- /dev/null +++ b/drivers/hwinfo/hwinfo_native_bottom.h @@ -0,0 +1,11 @@ +/* + * Copyright (c) 2025 Henrik Brix Andersen + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef DRIVERS_HWINFO_NATIVE_BOTTOM_H +#define DRIVERS_HWINFO_NATIVE_BOTTOM_H + +long native_hwinfo_gethostid_bottom(void); + +#endif /* DRIVERS_HWINFO_NATIVE_BOTTOM_H */