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_eber...@quicinc.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

_______________________________________________
boot-architecture mailing list -- boot-architecture@lists.linaro.org
To unsubscribe send an email to boot-architecture-le...@lists.linaro.org

Reply via email to