git: b8b0c65e8a43 - main - Add serial-number to hw.fdt sysctl area if found in fdt.

From: Mike Karels <karels_at_FreeBSD.org>
Date: Thu, 03 Mar 2022 13:08:40 UTC
The branch main has been updated by karels:

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

commit b8b0c65e8a43cab95b97e28d3e3d73fc7d9c0a5e
Author:     Mike Karels <karels@FreeBSD.org>
AuthorDate: 2022-02-23 20:42:30 +0000
Commit:     Mike Karels <karels@FreeBSD.org>
CommitDate: 2022-03-03 13:08:32 +0000

    Add serial-number to hw.fdt sysctl area if found in fdt.
    
    Add serial-number sysctl if that fdt property exists and is a printable
    string.  While here, ensure that the hw.fdt sysctl values fit in the
    buffers provided so that they will be NUL-terminated.  Tested on
    Raspberry Pi 3B+ and 4.
    
    MFC after:      5 days
    Reviewed by:    manu imp
    Differential Revision: https://reviews.freebsd.org/D34356
---
 sys/dev/ofw/ofw_fdt.c | 49 ++++++++++++++++++++++++++++++++++++++++---------
 1 file changed, 40 insertions(+), 9 deletions(-)

diff --git a/sys/dev/ofw/ofw_fdt.c b/sys/dev/ofw/ofw_fdt.c
index a83f7cbdcb60..855bf6d5d14a 100644
--- a/sys/dev/ofw/ofw_fdt.c
+++ b/sys/dev/ofw/ofw_fdt.c
@@ -32,6 +32,7 @@
 __FBSDID("$FreeBSD$");
 
 #include <sys/param.h>
+#include <sys/ctype.h>
 #include <sys/kernel.h>
 #include <sys/malloc.h>
 #include <sys/systm.h>
@@ -107,11 +108,13 @@ OFW_DEF(ofw_fdt);
 #define	FDT_FBSDVER_LEN	16
 #define	FDT_MODEL_LEN	80
 #define	FDT_COMPAT_LEN	255
+#define	FDT_SERIAL_LEN	32
 
 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 char fdt_serial[FDT_SERIAL_LEN];
 
 static int
 sysctl_handle_dtb(SYSCTL_HANDLER_ARGS)
@@ -143,6 +146,10 @@ sysctl_register_fdt_oid(void *arg)
 		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");
+	if (fdt_serial[0] != '\0')
+		SYSCTL_ADD_STRING(NULL, SYSCTL_STATIC_CHILDREN(_hw_fdt),
+		    OID_AUTO, "serial-number", CTLFLAG_RD, fdt_serial,
+		    FDT_SERIAL_LEN, "Serial number");
 }
 SYSINIT(dtb_oid, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_register_fdt_oid, NULL);
 
@@ -161,20 +168,44 @@ ofw_fdt_init(ofw_t ofw, void *data)
 	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);
+	if (len > 0 && len <= FDT_MODEL_LEN) {
+		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) {
+	if (len > 0 && len <= FDT_COMPAT_LEN) {
+		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] = ' ';
+	}
+	len = ofw_fdt_getproplen(NULL, root, "freebsd,dts-version");
+	if (len > 0 && len <= FDT_FBSDVER_LEN) {
 		bzero(fdt_fbsd_version, FDT_FBSDVER_LEN);
 		ofw_fdt_getprop(NULL, root, "freebsd,dts-version",
 		  fdt_fbsd_version, FDT_FBSDVER_LEN);
 	}
+	len = ofw_fdt_getproplen(NULL, root, "serial-number");
+	if (len > 0 && len <= FDT_SERIAL_LEN) {
+		bzero(fdt_serial, FDT_SERIAL_LEN);
+		ofw_fdt_getprop(NULL, root, "serial-number",
+		    fdt_serial, FDT_SERIAL_LEN);
+		/*
+		 * Non-standard property; check for NUL-terminated
+		 * printable string.
+		 */
+		for (i = 0; i < len - 1; i++) {
+			if (!isprint(fdt_serial[i])) {
+				fdt_serial[0] = '\0';
+				break;
+			}
+		}
+		if (fdt_serial[len - 1] != '\0')
+			fdt_serial[0] = '\0';
+	}
 	return (0);
 }