git: e436cb79666d - main - if_bnxt: Correcting the firmware package version parsing logic

From: Sumit Saxena <ssaxena_at_FreeBSD.org>
Date: Wed, 06 Mar 2024 12:17:46 UTC
The branch main has been updated by ssaxena:

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

commit e436cb79666db3c8bb167d47ca9803c36013e32c
Author:     Sumit Saxena <ssaxena@FreeBSD.org>
AuthorDate: 2024-02-23 08:20:26 +0000
Commit:     Sumit Saxena <ssaxena@FreeBSD.org>
CommitDate: 2024-03-06 12:17:29 +0000

    if_bnxt: Correcting the firmware package version parsing logic
    
    The firmware package version currently appears as "Unknown" through
    the sysctl interface. The parsing logic for extracting the firmware
    package version from the package log has been modified to ensure
    compatibility with all controllers.
    
    Reviewed by:            imp
    Approved by:            imp
    Differential revision:  https://reviews.freebsd.org/D42950
---
 sys/dev/bnxt/bnxt_sysctl.c | 58 +++++++++++++++++++++++++++++-----------------
 1 file changed, 37 insertions(+), 21 deletions(-)

diff --git a/sys/dev/bnxt/bnxt_sysctl.c b/sys/dev/bnxt/bnxt_sysctl.c
index 943e3019f579..971754029afd 100644
--- a/sys/dev/bnxt/bnxt_sysctl.c
+++ b/sys/dev/bnxt/bnxt_sysctl.c
@@ -28,6 +28,7 @@
 
 #include <sys/types.h>
 #include <sys/sysctl.h>
+#include <sys/ctype.h>
 
 #include "bnxt.h"
 #include "bnxt_hwrm.h"
@@ -719,6 +720,39 @@ static char *bnxt_chip_type[] = {
 };
 #define MAX_CHIP_TYPE 3
 
+static char *bnxt_parse_pkglog(int desired_field, uint8_t *data, size_t datalen)
+{
+	char    *retval = NULL;
+	char    *p;
+	char    *value;
+	int     field = 0;
+
+	if (datalen < 1)
+		return NULL;
+	/* null-terminate the log data (removing last '\n'): */
+	data[datalen - 1] = 0;
+	for (p = data; *p != 0; p++) {
+		field = 0;
+		retval = NULL;
+		while (*p != 0 && *p != '\n') {
+			value = p;
+			while (*p != 0 && *p != '\t' && *p != '\n')
+				p++;
+			if (field == desired_field)
+				retval = value;
+			if (*p != '\t')
+				break;
+			*p = 0;
+			field++;
+			p++;
+		}
+		if (*p == 0)
+			break;
+		*p = 0;
+	}
+	return retval;
+}
+
 static int
 bnxt_package_ver_sysctl(SYSCTL_HANDLER_ARGS)
 {
@@ -726,11 +760,9 @@ bnxt_package_ver_sysctl(SYSCTL_HANDLER_ARGS)
 	struct iflib_dma_info dma_data;
 	char *pkglog = NULL;
 	char *p;
-	char *next;
 	char unk[] = "<unknown>";
 	char *buf = unk;
 	int rc;
-	int field;
 	uint16_t ordinal = BNX_DIR_ORDINAL_FIRST;
 	uint16_t index;
 	uint32_t data_len;
@@ -748,27 +780,11 @@ bnxt_package_ver_sysctl(SYSCTL_HANDLER_ARGS)
 			    &dma_data);
 			if (rc == 0) {
 				pkglog = dma_data.idi_vaddr;
-				/* NULL terminate (removes last \n) */
-				pkglog[data_len-1] = 0;
-
-				/* Set p = start of last line */
-				p = strrchr(pkglog, '\n');
-				if (p == NULL)
-					p = pkglog;
-
-				/* Now find the correct tab delimited field */
-				for (field = 0, next = p,
-				    p = strsep(&next, "\t");
-				    field <
-				    BNX_PKG_LOG_FIELD_IDX_PKG_VERSION && p;
-				    p = strsep(&next, "\t")) {
-					field++;
-				}
-				if (field == BNX_PKG_LOG_FIELD_IDX_PKG_VERSION)
+				p = bnxt_parse_pkglog(BNX_PKG_LOG_FIELD_IDX_PKG_VERSION, pkglog, data_len);
+				if (p && *p != 0 && isdigit(*p))
 					buf = p;
 			}
-		}
-		else
+		} else
 			dma_data.idi_vaddr = NULL;
 	}