Skip to content

Commit ec1e05c

Browse files
drivers: hwinfo: add HWINFO driver for native_sim
Add a HWINFO driver for native_sim with support for getting the device ID. Default device ID will be that returned from gethostid(), but the ID can be overriden via the -hostid command line option. Signed-off-by: Henrik Brix Andersen <[email protected]>
1 parent c53d1fa commit ec1e05c

File tree

9 files changed

+114
-0
lines changed

9 files changed

+114
-0
lines changed

boards/native/native_sim/doc/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,7 @@ host libC (:kconfig:option:`CONFIG_EXTERNAL_LIBC`):
721721
FUSE, :ref:`Host based filesystem access <native_fuse_flash>`, :kconfig:option:`CONFIG_FUSE_FS_ACCESS`, All
722722
GPIO, GPIO emulator, :kconfig:option:`CONFIG_GPIO_EMUL`, All
723723
GPIO, SDL GPIO emulator, :kconfig:option:`CONFIG_GPIO_EMUL_SDL`, All
724+
HWINFO, :kconfig:option:`CONFIG_HWINFO_NATIVE`, All
724725
I2C, I2C emulator, :kconfig:option:`CONFIG_I2C_EMUL`, All
725726
Input, Input SDL touch, :kconfig:option:`CONFIG_INPUT_SDL_TOUCH`, All
726727
Input, Linux evdev, :kconfig:option:`CONFIG_NATIVE_LINUX_EVDEV`, All

boards/native/native_sim/native_sim.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ supported:
2323
- spi
2424
- gpio
2525
- rtc
26+
- hwinfo
2627
testing:
2728
default: true
2829
vendor: zephyr

boards/native/native_sim/native_sim_native_64.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ supported:
2020
- adc
2121
- gpio
2222
- rtc
23+
- hwinfo
2324
vendor: zephyr

drivers/hwinfo/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,13 @@ zephyr_library_sources_ifdef(CONFIG_HWINFO_SILABS_S2 hwinfo_silabs_series2.c)
4040
zephyr_library_sources_ifdef(CONFIG_HWINFO_SMARTBOND hwinfo_smartbond.c)
4141
zephyr_library_sources_ifdef(CONFIG_HWINFO_STM32 hwinfo_stm32.c)
4242
# zephyr-keep-sorted-stop
43+
44+
if(CONFIG_HWINFO_NATIVE)
45+
if(${CMAKE_HOST_SYSTEM_NAME} STREQUAL Linux)
46+
zephyr_library_sources(hwinfo_native.c)
47+
48+
target_sources(native_simulator INTERFACE hwinfo_native_bottom.c)
49+
else()
50+
message(FATAL_ERROR "CONFIG_HWINFO_NATIVE is only available on Linux")
51+
endif()
52+
endif()

drivers/hwinfo/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ rsource "Kconfig.mcux_src"
4343
rsource "Kconfig.mcux_src_rev2"
4444
rsource "Kconfig.mcux_syscon"
4545
rsource "Kconfig.mspm0"
46+
rsource "Kconfig.native"
4647
rsource "Kconfig.nrf"
4748
rsource "Kconfig.numaker"
4849
rsource "Kconfig.numaker_rmc"

drivers/hwinfo/Kconfig.native

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Copyright (c) 2025 Henrik Brix Andersen <[email protected]>
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
config HWINFO_NATIVE
5+
bool "HWINFO driver for native_sim"
6+
default y
7+
depends on ARCH_POSIX
8+
select HWINFO_HAS_DRIVER
9+
help
10+
Enable native_sim HWINFO driver.

drivers/hwinfo/hwinfo_native.c

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright (c) 2025 Henrik Brix Andersen <[email protected]>
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include <cmdline.h>
7+
#include <posix_native_task.h>
8+
#include <string.h>
9+
#include <zephyr/drivers/hwinfo.h>
10+
#include <zephyr/sys/byteorder.h>
11+
12+
#include "hwinfo_native_bottom.h"
13+
14+
static uint32_t native_hwinfo_device_id;
15+
static bool native_hwinfo_device_id_set;
16+
17+
ssize_t z_impl_hwinfo_get_device_id(uint8_t *buffer, size_t length)
18+
{
19+
20+
if (length > sizeof(native_hwinfo_device_id)) {
21+
length = sizeof(native_hwinfo_device_id);
22+
}
23+
24+
sys_put_be(buffer, &native_hwinfo_device_id, length);
25+
26+
return length;
27+
}
28+
29+
static void native_hwinfo_gethostid(void)
30+
{
31+
if (!native_hwinfo_device_id_set) {
32+
native_hwinfo_device_id = native_hwinfo_gethostid_bottom();
33+
}
34+
}
35+
36+
static void native_hwinfo_device_id_was_set(char *argv, int offset)
37+
{
38+
ARG_UNUSED(argv);
39+
ARG_UNUSED(offset);
40+
41+
native_hwinfo_device_id_set = true;
42+
}
43+
44+
static void native_hwinfo_add_options(void)
45+
{
46+
static struct args_struct_t native_hwinfo_options[] = {
47+
{
48+
.option = "device_id",
49+
.name = "id",
50+
.type = 'u',
51+
.dest = (void *)&native_hwinfo_device_id,
52+
.call_when_found = native_hwinfo_device_id_was_set,
53+
.descript = "A 32-bit integer value to use as HWINFO device ID. "
54+
"If not set, the host gethostid() output will be used.",
55+
},
56+
ARG_TABLE_ENDMARKER,
57+
};
58+
59+
native_add_command_line_opts(native_hwinfo_options);
60+
}
61+
62+
NATIVE_TASK(native_hwinfo_add_options, PRE_BOOT_1, 10);
63+
NATIVE_TASK(native_hwinfo_gethostid, PRE_BOOT_2, 10);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* Copyright (c) 2025 Henrik Brix Andersen <[email protected]>
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#undef _XOPEN_SOURCE
7+
#define _XOPEN_SOURCE 500
8+
9+
#include <unistd.h>
10+
11+
#include "hwinfo_native_bottom.h"
12+
13+
long native_hwinfo_gethostid_bottom(void)
14+
{
15+
return gethostid();
16+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
* Copyright (c) 2025 Henrik Brix Andersen <[email protected]>
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#ifndef DRIVERS_HWINFO_NATIVE_BOTTOM_H
7+
#define DRIVERS_HWINFO_NATIVE_BOTTOM_H
8+
9+
long native_hwinfo_gethostid_bottom(void);
10+
11+
#endif /* DRIVERS_HWINFO_NATIVE_BOTTOM_H */

0 commit comments

Comments
 (0)