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 <[email protected]>
---
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
On Tue, May 21, 2024 at 11:37:58AM -0700, Elliot Berman wrote:
> The devicetree spec introduced a mechanism to match devicetree blobs to
> boards using firmware-provided identifiers.
Can you share a link to where the devicetree spec introduced this
mechanism? I don't recall seeing a PR to dt-schema for it nor did a
quick check of the devicetree specification repo show a PR adding it.
What am I missing?
Thanks,
Conor.
> 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.
On Tue, May 21, 2024 at 08:28:01PM +0100, Conor Dooley wrote:
> On Tue, May 21, 2024 at 11:37:58AM -0700, Elliot Berman wrote:
> > The devicetree spec introduced a mechanism to match devicetree blobs to
> > boards using firmware-provided identifiers.
>
> Can you share a link to where the devicetree spec introduced this
> mechanism? I don't recall seeing a PR to dt-schema for it nor did a
> quick check of the devicetree specification repo show a PR adding it.
>
> What am I missing?
My thinking is that the next patch would probably go to dt-schema or
devicetree specification repo.
Thanks,
Elliot