Date: Wed, 9 Mar 2022 15:35:48 GMT From: Mike Karels <karels@FreeBSD.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org Subject: git: c0d9004101db - stable/13 - Add serial-number to hw.fdt sysctl area if found in fdt. Message-ID: <202203091535.229FZmPS031969@gitrepo.freebsd.org>
next in thread | raw e-mail | index | archive | help
The branch stable/13 has been updated by karels: URL: https://cgit.FreeBSD.org/src/commit/?id=c0d9004101dbd9637a6b7e1b876f8eece489a5cf commit c0d9004101dbd9637a6b7e1b876f8eece489a5cf Author: Mike Karels <karels@FreeBSD.org> AuthorDate: 2022-02-23 20:42:30 +0000 Commit: Mike Karels <karels@FreeBSD.org> CommitDate: 2022-03-09 15:28:28 +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. Reviewed by: manu imp Differential Revision: https://reviews.freebsd.org/D34356 (cherry picked from commit b8b0c65e8a43cab95b97e28d3e3d73fc7d9c0a5e) --- 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); }
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202203091535.229FZmPS031969>