From e1302b3e305890cbf1c3bdf13321d1a3476dfae7 Mon Sep 17 00:00:00 2001
From: Simon Glass <sjg@chromium.org>
Date: Sun, 12 Nov 2023 19:58:29 -0700
Subject: [PATCH] x86: coreboot: Add a sysinfo driver

Create a sysinfo driver to avoid needing a custom checkboard()
function. With this the following information is printed when booting
from coreboot under QEMU:

   Model: Standard PC (i440FX + PIIX, 1996)
   Manufacturer: QEMU
   Prior-stage version: 4.21-885-g2a87ef1eca56
   Prior-stage date: 11/11/2023

Signed-off-by: Simon Glass <sjg@chromium.org>
---
 arch/x86/cpu/coreboot/Kconfig      |  2 +
 arch/x86/dts/coreboot.dts          |  4 ++
 board/coreboot/coreboot/Makefile   |  1 +
 board/coreboot/coreboot/coreboot.c | 44 ---------------
 board/coreboot/coreboot/sysinfo.c  | 89 ++++++++++++++++++++++++++++++
 5 files changed, 96 insertions(+), 44 deletions(-)
 create mode 100644 board/coreboot/coreboot/sysinfo.c

diff --git a/arch/x86/cpu/coreboot/Kconfig b/arch/x86/cpu/coreboot/Kconfig
index 178f8ad181..085302c048 100644
--- a/arch/x86/cpu/coreboot/Kconfig
+++ b/arch/x86/cpu/coreboot/Kconfig
@@ -27,5 +27,7 @@ config SYS_COREBOOT
 	imply X86_TSC_READ_BASE
 	imply USE_PREBOOT
 	select BINMAN if X86_64
+	select SYSINFO
+	imply SYSINFO_EXTRA
 
 endif
diff --git a/arch/x86/dts/coreboot.dts b/arch/x86/dts/coreboot.dts
index 0eb31cae42..dfce7c2d59 100644
--- a/arch/x86/dts/coreboot.dts
+++ b/arch/x86/dts/coreboot.dts
@@ -45,4 +45,8 @@
 		bootph-some-ram;
 		compatible = "coreboot-fb";
 	};
+
+	sysinfo {
+		compatible = "coreboot,sysinfo";
+	};
 };
diff --git a/board/coreboot/coreboot/Makefile b/board/coreboot/coreboot/Makefile
index d292b7032c..75bfbd1894 100644
--- a/board/coreboot/coreboot/Makefile
+++ b/board/coreboot/coreboot/Makefile
@@ -11,3 +11,4 @@
 # Daniel Engström, Omicron Ceti AB, daniel@omicron.se.
 
 obj-y	+= coreboot.o
+obj-$(CONFIG_$(SPL_TPL_)SMBIOS_PARSER)	+= sysinfo.o
diff --git a/board/coreboot/coreboot/coreboot.c b/board/coreboot/coreboot/coreboot.c
index db855c11ae..e58dce3747 100644
--- a/board/coreboot/coreboot/coreboot.c
+++ b/board/coreboot/coreboot/coreboot.c
@@ -23,50 +23,6 @@ int board_early_init_r(void)
 	return 0;
 }
 
-#ifdef CONFIG_SMBIOS_PARSER
-int show_board_info(void)
-{
-	const struct smbios_entry *smbios = smbios_entry(lib_sysinfo.smbios_start, lib_sysinfo.smbios_size);
-
-	if (!smbios)
-		goto fallback;
-
-	const struct smbios_header *bios = smbios_header(smbios, SMBIOS_BIOS_INFORMATION);
-	const struct smbios_header *system = smbios_header(smbios, SMBIOS_SYSTEM_INFORMATION);
-	const struct smbios_type0 *t0 = (struct smbios_type0 *)bios;
-	const struct smbios_type1 *t1 = (struct smbios_type1 *)system;
-
-	if (!t0 || !t1)
-		goto fallback;
-
-	const char *bios_ver = smbios_string(bios, t0->bios_ver);
-	const char *bios_date = smbios_string(bios, t0->bios_release_date);
-	const char *model = smbios_string(system, t1->product_name);
-	const char *manufacturer = smbios_string(system, t1->manufacturer);
-
-	if (!model || !manufacturer || !bios_ver)
-		goto fallback;
-
-	printf("Vendor: %s\n", manufacturer);
-	printf("Model: %s\n", model);
-	printf("BIOS Version: %s\n", bios_ver);
-	if (bios_date)
-		printf("BIOS date: %s\n", bios_date);
-
-	return 0;
-
-fallback:
-	if (IS_ENABLED(CONFIG_OF_CONTROL)) {
-		model = fdt_getprop(gd->fdt_blob, 0, "model", NULL);
-
-		if (model)
-			printf("Model: %s\n", model);
-	}
-
-	return checkboard();
-}
-#endif
-
 static struct splash_location coreboot_splash_locations[] = {
 	{
 		.name = "virtio_fs",
diff --git a/board/coreboot/coreboot/sysinfo.c b/board/coreboot/coreboot/sysinfo.c
new file mode 100644
index 0000000000..e0bdc7a5a8
--- /dev/null
+++ b/board/coreboot/coreboot/sysinfo.c
@@ -0,0 +1,89 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * coreboot sysinfo driver
+ *
+ * Copyright 2023 Google LLC
+ * Written by Simon Glass <sjg@chromium.org>
+ */
+
+#include <dm.h>
+#include <smbios.h>
+#include <sysinfo.h>
+#include <asm/cb_sysinfo.h>
+
+struct cb_sysinfo_priv {
+	const struct smbios_header *bios;
+	const struct smbios_header *system;
+	const struct smbios_type0 *t0;
+	const struct smbios_type1 *t1;
+};
+
+static int cb_get_str(struct udevice *dev, int id, size_t size, char *val)
+{
+	struct cb_sysinfo_priv *priv = dev_get_priv(dev);
+	const char *str = NULL;
+
+	switch (id) {
+	case SYSINFO_ID_BOARD_MODEL:
+		if (priv->t1)
+			str = smbios_string(priv->system,
+					    priv->t1->product_name);
+		break;
+	case SYSINFO_ID_BOARD_MANUFACTURER:
+		if (priv->t1)
+			str = smbios_string(priv->system,
+					    priv->t1->manufacturer);
+		break;
+	case SYSINFO_ID_PRIOR_STAGE_VERSION:
+		if (priv->t0)
+			str = smbios_string(priv->bios, priv->t0->bios_ver);
+		break;
+	case SYSINFO_ID_PRIOR_STAGE_DATE:
+		if (priv->t0)
+			str = smbios_string(priv->bios,
+					    priv->t0->bios_release_date);
+		break;
+	}
+	if (!str)
+		return -ENOTSUPP;
+
+	strlcpy(val, str, size);
+
+	return  0;
+}
+
+static int cb_detect(struct udevice *dev)
+{
+	struct cb_sysinfo_priv *priv = dev_get_priv(dev);
+	const struct smbios_entry *smbios;
+
+	smbios = smbios_entry(lib_sysinfo.smbios_start,
+			      lib_sysinfo.smbios_size);
+	if (!smbios)
+		return 0;
+
+	priv->bios = smbios_header(smbios, SMBIOS_BIOS_INFORMATION);
+	priv->system = smbios_header(smbios, SMBIOS_SYSTEM_INFORMATION);
+	priv->t0 = (struct smbios_type0 *)priv->bios;
+	priv->t1 = (struct smbios_type1 *)priv->system;
+
+	return 0;
+}
+
+static const struct udevice_id sysinfo_coreboot_ids[] = {
+	{ .compatible = "coreboot,sysinfo" },
+	{ /* sentinel */ }
+};
+
+static const struct sysinfo_ops sysinfo_coreboot_ops = {
+	.detect		= cb_detect,
+	.get_str	= cb_get_str,
+};
+
+U_BOOT_DRIVER(sysinfo_coreboot) = {
+	.name           = "sysinfo_coreboot",
+	.id             = UCLASS_SYSINFO,
+	.of_match       = sysinfo_coreboot_ids,
+	.ops		= &sysinfo_coreboot_ops,
+	.priv_auto	= sizeof(struct cb_sysinfo_priv),
+};
-- 
2.39.5