git: caa14c7b600d - stable/13 - fdt: Expose the model, compatible and freebsd dts brandind as sysctl

From: Mike Karels <karels_at_FreeBSD.org>
Date: Wed, 09 Mar 2022 15:35:47 UTC
The branch stable/13 has been updated by karels:

URL: https://cgit.FreeBSD.org/src/commit/?id=caa14c7b600d3d15042ada340665f7b0aed4b415

commit caa14c7b600d3d15042ada340665f7b0aed4b415
Author:     Emmanuel Vadot <manu@FreeBSD.org>
AuthorDate: 2021-07-17 15:19:22 +0000
Commit:     Mike Karels <karels@FreeBSD.org>
CommitDate: 2022-03-09 15:27:28 +0000

    fdt: Expose the model, compatible and freebsd dts brandind as sysctl
    
    This make it easier for script to get the hardware on which they are running.
    
    Sponsored by:   Diablotin Systems
    Differential Revision:    https://reviews.freebsd.org/D31205
    Reviewed by:              imp
    Should be ok on powerpc:  jhibbits (over irc)
    
    (cherry picked from commit 50e0dc0c4b46ee62b898ce2d92e52be4f77383d9)
---
 sys/dev/ofw/ofw_fdt.c | 39 ++++++++++++++++++++++++++++++++++++++-
 1 file changed, 38 insertions(+), 1 deletion(-)

diff --git a/sys/dev/ofw/ofw_fdt.c b/sys/dev/ofw/ofw_fdt.c
index 8e5840424860..a83f7cbdcb60 100644
--- a/sys/dev/ofw/ofw_fdt.c
+++ b/sys/dev/ofw/ofw_fdt.c
@@ -104,7 +104,14 @@ static ofw_def_t ofw_fdt = {
 };
 OFW_DEF(ofw_fdt);
 
+#define	FDT_FBSDVER_LEN	16
+#define	FDT_MODEL_LEN	80
+#define	FDT_COMPAT_LEN	255
+
 static void *fdtp = NULL;
+static char fdt_model[FDT_MODEL_LEN];
+static char fdt_compatible[FDT_COMPAT_LEN];
+static char fdt_fbsd_version[FDT_FBSDVER_LEN];
 
 static int
 sysctl_handle_dtb(SYSCTL_HANDLER_ARGS)
@@ -124,19 +131,50 @@ sysctl_register_fdt_oid(void *arg)
 	SYSCTL_ADD_PROC(NULL, SYSCTL_STATIC_CHILDREN(_hw_fdt), OID_AUTO, "dtb",
 	    CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
 	    sysctl_handle_dtb, "", "Device Tree Blob");
+	if (fdt_model[0] != '\0')
+		SYSCTL_ADD_STRING(NULL, SYSCTL_STATIC_CHILDREN(_hw_fdt),
+		    OID_AUTO, "model", CTLFLAG_RD, fdt_model,
+		    FDT_MODEL_LEN, "System board model");
+	if (fdt_compatible[0] != '\0')
+		SYSCTL_ADD_STRING(NULL, SYSCTL_STATIC_CHILDREN(_hw_fdt),
+		    OID_AUTO, "compatible", CTLFLAG_RD, fdt_compatible,
+		    FDT_COMPAT_LEN, "Compatible platforms");
+	if (fdt_fbsd_version[0] != '\0')
+		SYSCTL_ADD_STRING(NULL, SYSCTL_STATIC_CHILDREN(_hw_fdt),
+		    OID_AUTO, "freebsd-version", CTLFLAG_RD, fdt_fbsd_version,
+		    FDT_FBSDVER_LEN, "FreeBSD DTS branding version");
 }
 SYSINIT(dtb_oid, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_register_fdt_oid, NULL);
 
 static int
 ofw_fdt_init(ofw_t ofw, void *data)
 {
+	phandle_t root;
+	ssize_t len;
 	int err;
+	int i;
 
 	/* Check FDT blob integrity */
 	if ((err = fdt_check_header(data)) != 0)
 		return (err);
 
 	fdtp = data;
+	root = ofw_fdt_finddevice(NULL, "/");
+	len = ofw_fdt_getproplen(NULL, root, "model");
+	bzero(fdt_model, FDT_MODEL_LEN);
+	ofw_fdt_getprop(NULL, root, "model", fdt_model, FDT_MODEL_LEN);
+	len = ofw_fdt_getproplen(NULL, root, "compatible");
+	bzero(fdt_compatible, FDT_COMPAT_LEN);
+	ofw_fdt_getprop(NULL, root, "compatible", fdt_compatible, FDT_COMPAT_LEN);
+	/* Replace the middle '\0' with ' ' */
+	for (i = 0; i < len - 1; i++)
+		if (fdt_compatible[i] == '\0')
+			fdt_compatible[i] = ' ';
+	if ((len = ofw_fdt_getproplen(NULL, root, "freebsd,dts-version")) > 0) {
+		bzero(fdt_fbsd_version, FDT_FBSDVER_LEN);
+		ofw_fdt_getprop(NULL, root, "freebsd,dts-version",
+		  fdt_fbsd_version, FDT_FBSDVER_LEN);
+	}
 	return (0);
 }
 
@@ -410,7 +448,6 @@ ofw_fdt_package_to_path(ofw_t ofw, phandle_t package, char *buf, size_t len)
 static int
 ofw_fdt_fixup(ofw_t ofw)
 {
-#define FDT_MODEL_LEN	80
 	char model[FDT_MODEL_LEN];
 	phandle_t root;
 	ssize_t len;