[Concept,14/25] chid: Provide a command to access chid features

Message ID 20250903133639.3235920-15-sjg@u-boot.org
State New
Headers
Series Selection of devicetree using CHIDs |

Commit Message

Simon Glass Sept. 3, 2025, 1:36 p.m. UTC
  From: Simon Glass <sjg@chromium.org>

Provide a simple command which supports showing the information which
goes into calculating a CHID. The information is obtained entirely from
SMBIOS tables at present.

Co-developed-by: Claude <noreply@anthropic.com>
Signed-off-by: Simon Glass <sjg@chromium.org>
---

 cmd/Kconfig            | 10 +++++++
 cmd/Makefile           |  1 +
 cmd/chid.c             | 43 +++++++++++++++++++++++++++
 doc/usage/cmd/chid.rst | 67 ++++++++++++++++++++++++++++++++++++++++++
 doc/usage/index.rst    |  1 +
 test/cmd/Makefile      |  1 +
 test/cmd/chid.c        | 45 ++++++++++++++++++++++++++++
 7 files changed, 168 insertions(+)
 create mode 100644 cmd/chid.c
 create mode 100644 doc/usage/cmd/chid.rst
 create mode 100644 test/cmd/chid.c
  

Patch

diff --git a/cmd/Kconfig b/cmd/Kconfig
index 8126821ffdc..8037827930b 100644
--- a/cmd/Kconfig
+++ b/cmd/Kconfig
@@ -280,6 +280,16 @@  config CMD_SMBIOS
 	help
 	  Display the SMBIOS information.
 
+config CMD_CHID
+	bool "chid"
+	depends on CHID
+	default y
+	help
+	  Computer Hardware ID (CHID) utilities. This provides commands to
+	  extract hardware identification data from SMBIOS tables according
+	  to the Microsoft CHID specification. This is used by Windows Update
+	  and fwupd for hardware identification and firmware updates.
+
 endmenu
 
 menu "Boot commands"
diff --git a/cmd/Makefile b/cmd/Makefile
index bc381905729..75ef9fab922 100644
--- a/cmd/Makefile
+++ b/cmd/Makefile
@@ -46,6 +46,7 @@  obj-$(CONFIG_CMD_CAT) += cat.o
 obj-$(CONFIG_CMD_CACHE) += cache.o
 obj-$(CONFIG_CMD_CBFS) += cbfs.o
 obj-$(CONFIG_CMD_CEDIT) += cedit.o
+obj-$(CONFIG_CMD_CHID) += chid.o
 obj-$(CONFIG_CMD_CLK) += clk.o
 obj-$(CONFIG_CMD_CLS) += cls.o
 obj-$(CONFIG_CMD_CONFIG) += config.o
diff --git a/cmd/chid.c b/cmd/chid.c
new file mode 100644
index 00000000000..096f88e3c44
--- /dev/null
+++ b/cmd/chid.c
@@ -0,0 +1,43 @@ 
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Command for Computer Hardware Identifiers (Windows CHID)
+ *
+ * Copyright 2025 Simon Glass <sjg@chromium.org>
+ */
+
+#include <chid.h>
+#include <command.h>
+#include <vsprintf.h>
+
+static int do_chid_show(struct cmd_tbl *cmdtp, int flag, int argc,
+			char *const argv[])
+{
+	struct chid_data chid;
+	int ret;
+
+	ret = chid_from_smbios(&chid);
+	if (ret) {
+		printf("Failed to get CHID data from SMBIOS (err=%dE)\n", ret);
+		return CMD_RET_FAILURE;
+	}
+
+	printf("Manufacturer:      %s\n", chid.manuf);
+	printf("Family:            %s\n", chid.family);
+	printf("Product Name:      %s\n", chid.product_name);
+	printf("Product SKU:       %s\n", chid.product_sku);
+	printf("Baseboard Manuf:   %s\n", chid.board_manuf);
+	printf("Baseboard Product: %s\n", chid.board_product);
+	printf("BIOS Vendor:       %s\n", chid.bios_vendor);
+	printf("BIOS Version:      %s\n", chid.bios_version);
+	printf("BIOS Major:        %u\n", chid.bios_major);
+	printf("BIOS Minor:        %u\n", chid.bios_minor);
+	printf("Enclosure Type:    %u\n", chid.enclosure_type);
+
+	return 0;
+}
+
+U_BOOT_LONGHELP(chid,
+	"show - Show CHID data extracted from SMBIOS");
+
+U_BOOT_CMD_WITH_SUBCMDS(chid, "Computer Hardware ID utilities", chid_help_text,
+	U_BOOT_SUBCMD_MKENT(show, 1, 1, do_chid_show));
diff --git a/doc/usage/cmd/chid.rst b/doc/usage/cmd/chid.rst
new file mode 100644
index 00000000000..d24f213df0f
--- /dev/null
+++ b/doc/usage/cmd/chid.rst
@@ -0,0 +1,67 @@ 
+.. SPDX-License-Identifier: GPL-2.0+
+
+chid command
+============
+
+Synopsis
+--------
+
+::
+
+    chid show
+
+Description
+-----------
+
+The chid command provides access to Computer Hardware IDs (CHIDs) generated from
+SMBIOS data. CHIDs are used by Windows Update and fwupd for hardware
+identification and firmware-update targeting.
+
+CHIDs are generated according to Microsoft's specification, which defines 15
+different hardware ID variants (HardwareID-00 through HardwareID-14), each
+using different combinations of SMBIOS fields. The variants range from most
+specific (including all identifying fields) to least specific (manufacturer
+only).
+
+Subcommands
+-----------
+
+show
+    Show the relevant SMBIOS values for the current board. These are used to
+    calculate CHIDs.
+
+
+Examples
+--------
+
+::
+
+    => chid show
+    Manufacturer:      Sandbox Corp
+    Family:            Sandbox_Family
+    Product Name:      Sandbox Computer
+    Product SKU:       SANDBOX-SKU
+    Baseboard Manuf:   Sandbox Boards
+    Baseboard Product: Sandbox Motherboard
+    BIOS Vendor:       U-Boot
+    BIOS Version:      2025.08-g167811e037b5-dirty
+    BIOS Major:        25
+    BIOS Minor:        8
+    Enclosure Type:    2
+    =>
+
+
+Configuration
+-------------
+
+The chid command is available when `CONFIG_CMD_CHID` is enabled.
+
+Return value
+------------
+
+The return value $? is 0 (true) on success, 1 (false) on failure.
+
+See also
+--------
+
+* :doc:`smbios <smbios>` - SMBIOS table information
diff --git a/doc/usage/index.rst b/doc/usage/index.rst
index 89341349d12..eeda632b1a0 100644
--- a/doc/usage/index.rst
+++ b/doc/usage/index.rst
@@ -49,6 +49,7 @@  Shell commands
    cmd/cbcmos
    cmd/cbsysinfo
    cmd/cedit
+   cmd/chid
    cmd/cli
    cmd/cls
    cmd/cmp
diff --git a/test/cmd/Makefile b/test/cmd/Makefile
index ec39bd46e76..5fe6ac7bb3e 100644
--- a/test/cmd/Makefile
+++ b/test/cmd/Makefile
@@ -14,6 +14,7 @@  obj-y += exit.o
 obj-$(CONFIG_X86) += cpuid.o msr.o
 obj-$(CONFIG_CMD_ADDRMAP) += addrmap.o
 obj-$(CONFIG_CMD_BDI) += bdinfo.o
+obj-$(CONFIG_CMD_CHID) += chid.o
 obj-$(CONFIG_COREBOOT_SYSINFO) += coreboot.o
 obj-$(CONFIG_CMD_FDT) += fdt.o
 ifdef CONFIG_SANDBOX
diff --git a/test/cmd/chid.c b/test/cmd/chid.c
new file mode 100644
index 00000000000..4b28db2abde
--- /dev/null
+++ b/test/cmd/chid.c
@@ -0,0 +1,45 @@ 
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Test for chid command
+ *
+ * Copyright 2025 Simon Glass <sjg@chromium.org>
+ */
+
+#include <command.h>
+#include <console.h>
+#include <test/cmd.h>
+#include <test/ut.h>
+#include <version.h>
+
+/* Test the 'chid show' command */
+static int cmd_chid_show_test(struct unit_test_state *uts)
+{
+	/* Test chid show command and verify expected output */
+	ut_assertok(run_command("chid show", 0));
+
+	ut_assert_nextline("Manufacturer:      Sandbox Corp");
+	ut_assert_nextline("Family:            Sandbox_Family");
+	ut_assert_nextline("Product Name:      Sandbox Computer");
+	ut_assert_nextline("Product SKU:       SANDBOX-SKU");
+	ut_assert_nextline("Baseboard Manuf:   Sandbox Boards");
+	ut_assert_nextline("Baseboard Product: Sandbox Motherboard");
+	ut_assert_nextline("BIOS Vendor:       U-Boot");
+	ut_assert_nextlinen("BIOS Version:      " PLAIN_VERSION);
+	ut_assert_nextline("BIOS Major:        %u", U_BOOT_VERSION_NUM % 100);
+	ut_assert_nextline("BIOS Minor:        %u", U_BOOT_VERSION_NUM_PATCH);
+	ut_assert_nextline("Enclosure Type:    2");
+	ut_assert_console_end();
+
+	return 0;
+}
+CMD_TEST(cmd_chid_show_test, UTF_CONSOLE);
+
+/* Test invalid chid subcommand */
+static int cmd_chid_invalid_test(struct unit_test_state *uts)
+{
+	/* Test chid command with invalid arguments */
+	ut_asserteq(1, run_command("chid invalid", 0));
+
+	return 0;
+}
+CMD_TEST(cmd_chid_invalid_test, UTF_CONSOLE);