lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20240521-board-ids-v3-1-e6c71d05f4d2@quicinc.com>
Date: Tue, 21 May 2024 11:37:58 -0700
From: Elliot Berman <quic_eberman@...cinc.com>
To: Rob Herring <robh+dt@...nel.org>, Frank Rowand <frowand.list@...il.com>,
        Krzysztof Kozlowski <krzysztof.kozlowski+dt@...aro.org>,
        Conor Dooley
	<conor+dt@...nel.org>,
        Bjorn Andersson <andersson@...nel.org>,
        Konrad Dybcio
	<konrad.dybcio@...aro.org>
CC: Amrit Anand <quic_amrianan@...cinc.com>,
        Peter Griffin
	<peter.griffin@...aro.org>,
        Caleb Connolly <caleb.connolly@...aro.org>,
        "Andy
 Gross" <agross@...nel.org>,
        Konrad Dybcio <konrad.dybcio@...aro.org>,
        "Doug
 Anderson" <dianders@...omium.org>,
        Simon Glass <sjg@...omium.org>, "Chen-Yu
 Tsai" <wenst@...omium.org>,
        Julius Werner <jwerner@...omium.org>,
        "Humphreys,
 Jonathan" <j-humphreys@...com>,
        Sumit Garg <sumit.garg@...aro.org>,
        "Jon
 Hunter" <jonathanh@...dia.org>,
        Michal Simek <michal.simek@....com>,
        <boot-architecture@...ts.linaro.org>, <devicetree@...r.kernel.org>,
        <linux-kernel@...r.kernel.org>, <linux-arm-kernel@...ts.infradead.org>,
        <linux-arm-msm@...r.kernel.org>,
        Elliot Berman <quic_eberman@...cinc.com>
Subject: [PATCH RFC v3 1/9] libfdt: board-id: Implement board-id scoring

The devicetree spec introduced a mechanism to match devicetree blobs to
boards using firmware-provided identifiers. Although the matching can be
implemented by DTB loaders, having a canonical implementation makes it
easier to integrate and ensure consistent behavior across ecosystems.

I've not yet investigated swig/python support for the new functions; I
would work on that before submitting the patch to libfdt.

Signed-off-by: Elliot Berman <quic_eberman@...cinc.com>
---
 scripts/dtc/libfdt/fdt_ro.c | 76 +++++++++++++++++++++++++++++++++++++++++++++
 scripts/dtc/libfdt/libfdt.h | 54 ++++++++++++++++++++++++++++++++
 2 files changed, 130 insertions(+)

diff --git a/scripts/dtc/libfdt/fdt_ro.c b/scripts/dtc/libfdt/fdt_ro.c
index 9f6c551a22c2..b19b17127399 100644
--- a/scripts/dtc/libfdt/fdt_ro.c
+++ b/scripts/dtc/libfdt/fdt_ro.c
@@ -857,3 +857,79 @@ int fdt_node_offset_by_compatible(const void *fdt, int startoffset,
 
 	return offset; /* error from fdt_next_node() */
 }
+
+int fdt_board_id_prop_matches(const void *fdt,
+			      const struct fdt_property *prop,
+			      fdt_get_board_id_t get_board_id_cb,
+			      void *ctx)
+{
+	int data_len;
+	const void *data;
+	const char *name;
+	int name_len;
+	int string = 0;
+
+	name = fdt_get_string(fdt, fdt32_to_cpu(prop->nameoff), &name_len);
+	if (!name)
+		return -FDT_ERR_BADOFFSET;
+
+	if (name_len > 4 && !strcmp(name + name_len - 4, "_str"))
+		string = 1;
+
+	data = get_board_id_cb(ctx, name, &data_len);
+	if (!data)
+		return -FDT_ERR_NOTFOUND;
+
+	if (string) {
+		return fdt_stringlist_contains(prop->data,
+					       fdt32_to_cpu(prop->len),
+					       data);
+	} else {
+		// exact data comparison. data_len is the size of each entry
+		if (fdt32_to_cpu(prop->len) % data_len || data_len % 4)
+			return -FDT_ERR_BADVALUE;
+
+		for (int i = 0; i < fdt32_to_cpu(prop->len); i += data_len) {
+			if (!memcmp(&prop->data[i], data, data_len))
+				return 1;
+		}
+
+		return 0;
+	}
+
+	return 0;
+}
+
+int fdt_board_id_score(const void *fdt, fdt_get_board_id_t get_board_id_cb,
+		       void *ctx)
+{
+	const struct fdt_property *prop;
+	int node, property, ret, score = 0;
+	const char *name;
+
+	node = fdt_path_offset(fdt, "/board-id");
+	if (node < 0)
+		return node;
+
+	fdt_for_each_property_offset(property, fdt, node) {
+		prop = fdt_get_property_by_offset(fdt, property, NULL);
+		if (!prop)
+			return -FDT_ERR_BADOFFSET;
+
+		name = fdt_get_string(fdt, fdt32_to_cpu(prop->nameoff), NULL);
+		if (!name)
+			return -FDT_ERR_BADOFFSET;
+
+		if (!strcmp(name, "phandle") || !strcmp(name, "linux,phandle"))
+			continue;
+
+		ret = fdt_board_id_prop_matches(fdt, prop, get_board_id_cb,
+						ctx);
+		if (ret == 1)
+			score++;
+		else
+			return ret;
+	}
+
+	return score;
+}
diff --git a/scripts/dtc/libfdt/libfdt.h b/scripts/dtc/libfdt/libfdt.h
index 77ccff19911e..de1cbc339315 100644
--- a/scripts/dtc/libfdt/libfdt.h
+++ b/scripts/dtc/libfdt/libfdt.h
@@ -1230,6 +1230,60 @@ int fdt_address_cells(const void *fdt, int nodeoffset);
  */
 int fdt_size_cells(const void *fdt, int nodeoffset);
 
+/**********************************************************************/
+/* Read-only functions (board-id related)                             */
+/**********************************************************************/
+
+/**
+ * fdt_get_board_id_t - callback to retrieve the board id for the platform
+ * @ctx: Context data passed from fdt_board_id_prop_matches()
+ * @name: board id name
+ * @data: Callback sets pointer to the board id data
+ * @datalen: Callback sets length of the board id data
+ *
+ * returns:
+ *	Pointer to the board id data, NULL if the data doesn't exist
+ */
+typedef const void *(*fdt_get_board_id_t)(void *ctx, const char *name,
+					   int *datalen);
+
+/**
+ * fdt_board_id_prop_matches - check if the property matches the platform's board id
+ * @fdt: pointer to the device tree blob
+ * @prop_offset: Offset of the property to match
+ * @get_board_id_cb: Function pointer to retrieve the platform's board id
+ *
+ * returns:
+ *	1: the property matches
+ *	0: the property doesn't match
+ *	-FDT_ERR_BADMAGIC,
+ *	-FDT_ERR_BADVERSION,
+ *	-FDT_ERR_BADSTATE,
+ *	-FDT_ERR_BADSTRUCTURE,
+ *	-FDT_ERR_TRUNCATED, standard meanings
+ */
+int fdt_board_id_prop_matches(const void *fdt,
+			      const struct fdt_property *prop,
+			      fdt_get_board_id_t get_board_id_cb,
+			      void *ctx);
+
+/**
+ * fdt_board_id_score - calculate score of the fdt's board_id
+ * @fdt: pointer to the device tree blob
+ * @get_board_id_cb: Function pointer to retrieve the platform's board id
+ *
+ * returns:
+ *	>0 for the number of board-id properties that were matched
+ *	0 if there was a mismatch in any of the board-id properties
+ *	-FDT_ERR_BADMAGIC,
+ *	-FDT_ERR_BADVERSION,
+ *	-FDT_ERR_BADSTATE,
+ *	-FDT_ERR_BADSTRUCTURE,
+ *	-FDT_ERR_TRUNCATED, standard meanings
+ */
+int fdt_board_id_score(const void *fdt, fdt_get_board_id_t get_board_id_cb,
+		       void *ctx);
+
 
 /**********************************************************************/
 /* Write-in-place functions                                           */

-- 
2.34.1


Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ