* [PATCH 1/3] libfdt: Add fdt_relative_path_offset
2025-03-12 19:41 [PATCH 0/3] Allow specifying target node in fdtoverlay Ayush Singh
@ 2025-03-12 19:42 ` Ayush Singh
2025-03-12 19:42 ` [PATCH 2/3] fdtoverlay: Add target option Ayush Singh
2025-03-12 19:42 ` [PATCH 3/3] tests: Add test for fdtoverlay target argument Ayush Singh
2 siblings, 0 replies; 4+ messages in thread
From: Ayush Singh @ 2025-03-12 19:42 UTC (permalink / raw)
To: David Gibson, Andreas Gnau, d-gole, lorforlinux, jkridner,
robertcnelson, Andrew Davis, Geert Uytterhoeven, Simon Glass,
Herve Codina, Luca Ceresoli, Thomas Petazzoni
Cc: devicetree-compiler, Ayush Singh
Add helper to allow searching for subnode path given a base node offset.
Behaves the same as fdt_path_offset when offset = 0.
Aliases are only allowed when offset == 0.
Signed-off-by: Ayush Singh <ayush@beagleboard.org>
---
libfdt/fdt_ro.c | 17 ++++++++++++++---
libfdt/libfdt.h | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 67 insertions(+), 3 deletions(-)
diff --git a/libfdt/fdt_ro.c b/libfdt/fdt_ro.c
index b78c4e48f1cb351504af4d861513b47f375262f5..f426183651cff746b67ebfe86e5809325cad4fec 100644
--- a/libfdt/fdt_ro.c
+++ b/libfdt/fdt_ro.c
@@ -247,11 +247,11 @@ int fdt_subnode_offset(const void *fdt, int parentoffset,
return fdt_subnode_offset_namelen(fdt, parentoffset, name, strlen(name));
}
-int fdt_path_offset_namelen(const void *fdt, const char *path, int namelen)
+static int fdt_relative_path_offset_namelen(const void *fdt, const char *path,
+ int namelen, size_t offset)
{
const char *end = path + namelen;
const char *p = path;
- int offset = 0;
FDT_RO_PROBE(fdt);
@@ -259,7 +259,7 @@ int fdt_path_offset_namelen(const void *fdt, const char *path, int namelen)
return -FDT_ERR_BADPATH;
/* see if we have an alias */
- if (*path != '/') {
+ if (*path != '/' && offset == 0) {
const char *q = memchr(path, '/', end - p);
if (!q)
@@ -295,11 +295,22 @@ int fdt_path_offset_namelen(const void *fdt, const char *path, int namelen)
return offset;
}
+int fdt_path_offset_namelen(const void *fdt, const char *path, int namelen)
+{
+ return fdt_relative_path_offset_namelen(fdt, path, namelen, 0);
+}
+
int fdt_path_offset(const void *fdt, const char *path)
{
return fdt_path_offset_namelen(fdt, path, strlen(path));
}
+int fdt_relative_path_offset(const void *fdt, const char *path, size_t offset)
+{
+ return fdt_relative_path_offset_namelen(fdt, path, strlen(path),
+ offset);
+}
+
const char *fdt_get_name(const void *fdt, int nodeoffset, int *len)
{
const struct fdt_node_header *nh = fdt_offset_ptr_(fdt, nodeoffset);
diff --git a/libfdt/libfdt.h b/libfdt/libfdt.h
index f2675b12f674b2221273d666d51b8e31f9b4dfbe..9b61215ecd11c8990128c0b523443b83f84b1013 100644
--- a/libfdt/libfdt.h
+++ b/libfdt/libfdt.h
@@ -562,6 +562,59 @@ int fdt_path_offset_namelen(const void *fdt, const char *path, int namelen);
*/
int fdt_path_offset(const void *fdt, const char *path);
+/**
+ * fdt_relative_path_offset - find a tree node by its relative path
+ * @fdt: pointer to the device tree blob
+ * @path: full path of the node to locate
+ * @offset: Offset of starting node
+ *
+ * fdt_relative_path_offset() finds a node of a given path in the device
+ * tree relative to a prior node.
+ *
+ * Each path component may omit the unit address portion, but the
+ * results of this are undefined if any such path component is
+ * ambiguous (that is if there are multiple nodes at the relevant
+ * level matching the given component, differentiated only by unit
+ * address).
+ *
+ * If the path is not absolute (i.e. does not begin with '/') and the
+ * starting offset is not 0, the first component is treated as an alias.
+ * That is, the property by that name is looked up in the /aliases node,
+ * and the value of that property used in place of that first component.
+ *
+ * For example, for this small fragment
+ *
+ * / {
+ * aliases {
+ * i2c2 = &foo; // RHS compiles to "/soc@0/i2c@30a40000/eeprom@52"
+ * };
+ * soc@0 {
+ * foo: i2c@30a40000 {
+ * bar: eeprom@52 {
+ * };
+ * };
+ * };
+ * };
+ *
+ * these would be equivalent:
+ *
+ * /soc@0/i2c@30a40000/eeprom@52
+ * i2c2/eeprom@52
+ *
+ * returns:
+ * structure block offset of the node with the requested path (>=0), on
+ * success
+ * -FDT_ERR_BADPATH, given path does not begin with '/' and the first
+ * component is not a valid alias
+ * -FDT_ERR_NOTFOUND, if the requested node does not exist
+ * -FDT_ERR_BADMAGIC,
+ * -FDT_ERR_BADVERSION,
+ * -FDT_ERR_BADSTATE,
+ * -FDT_ERR_BADSTRUCTURE,
+ * -FDT_ERR_TRUNCATED, standard meanings.
+ */
+int fdt_relative_path_offset(const void *fdt, const char *path, size_t offset);
+
/**
* fdt_get_name - retrieve the name of a given node
* @fdt: pointer to the device tree blob
--
2.48.1
^ permalink raw reply related [flat|nested] 4+ messages in thread* [PATCH 2/3] fdtoverlay: Add target option
2025-03-12 19:41 [PATCH 0/3] Allow specifying target node in fdtoverlay Ayush Singh
2025-03-12 19:42 ` [PATCH 1/3] libfdt: Add fdt_relative_path_offset Ayush Singh
@ 2025-03-12 19:42 ` Ayush Singh
2025-03-12 19:42 ` [PATCH 3/3] tests: Add test for fdtoverlay target argument Ayush Singh
2 siblings, 0 replies; 4+ messages in thread
From: Ayush Singh @ 2025-03-12 19:42 UTC (permalink / raw)
To: David Gibson, Andreas Gnau, d-gole, lorforlinux, jkridner,
robertcnelson, Andrew Davis, Geert Uytterhoeven, Simon Glass,
Herve Codina, Luca Ceresoli, Thomas Petazzoni
Cc: devicetree-compiler, Ayush Singh
Linux kernel supports applying overlays to a specific node. This is to
allow kernel drivers to use devicetree for device hotplugging usecases
such as connector + addon-board setups.
The overlays designed in such cases will not work with fdtoverlay
without modification since there is currently no way to specify target
nodes in fdtoverlay.
This functionality is required to allow using the same overlays that can
be used by drivers by providing the target node using program argument.
It is important to note that this only works with `target-path`
fragments. It does not have any effect on phandle target.
Signed-off-by: Ayush Singh <ayush@beagleboard.org>
---
fdtoverlay.c | 33 +++++++++++++++-------------
libfdt/fdt_overlay.c | 62 +++++++++++++++++++++++++++++++++++++++-------------
libfdt/libfdt.h | 52 +++++++++++++++++++++++++++++++++++++++++++
libfdt/version.lds | 2 ++
4 files changed, 119 insertions(+), 30 deletions(-)
diff --git a/fdtoverlay.c b/fdtoverlay.c
index ee1eb8f3ad28b14762aca9ab5c8a4e36aac967d8..94c6b089e7ab6054a5ed8794fb183eb69c9a6b89 100644
--- a/fdtoverlay.c
+++ b/fdtoverlay.c
@@ -24,24 +24,23 @@
static const char usage_synopsis[] =
"apply a number of overlays to a base blob\n"
" fdtoverlay <options> [<overlay.dtbo> [<overlay.dtbo>]]";
-static const char usage_short_opts[] = "i:o:v" USAGE_COMMON_SHORT_OPTS;
+static const char usage_short_opts[] = "i:o:t:v" USAGE_COMMON_SHORT_OPTS;
static struct option const usage_long_opts[] = {
- {"input", required_argument, NULL, 'i'},
- {"output", required_argument, NULL, 'o'},
- {"verbose", no_argument, NULL, 'v'},
+ { "input", required_argument, NULL, 'i' },
+ { "output", required_argument, NULL, 'o' },
+ { "target", optional_argument, NULL, 't' },
+ { "verbose", no_argument, NULL, 'v' },
USAGE_COMMON_LONG_OPTS,
};
-static const char * const usage_opts_help[] = {
- "Input base DT blob",
- "Output DT blob",
- "Verbose messages",
- USAGE_COMMON_OPTS_HELP
-};
+static const char *const usage_opts_help[] = { "Input base DT blob",
+ "Output DT blob", "Target node",
+ "Verbose messages",
+ USAGE_COMMON_OPTS_HELP };
int verbose = 0;
static void *apply_one(char *base, const char *overlay, size_t *buf_len,
- const char *name)
+ const char *name, const char *target)
{
char *tmp = NULL;
char *tmpo;
@@ -68,7 +67,7 @@ static void *apply_one(char *base, const char *overlay, size_t *buf_len,
memcpy(tmpo, overlay, fdt_totalsize(overlay));
- ret = fdt_overlay_apply(tmp, tmpo);
+ ret = fdt_overlay_apply_with_target(tmp, tmpo, target);
if (ret == -FDT_ERR_NOSPACE) {
*buf_len += BUF_INCREMENT;
}
@@ -97,7 +96,7 @@ fail:
return NULL;
}
static int do_fdtoverlay(const char *input_filename,
- const char *output_filename,
+ const char *output_filename, const char *target,
int argc, char *argv[])
{
char *blob = NULL;
@@ -142,7 +141,7 @@ static int do_fdtoverlay(const char *input_filename,
/* apply the overlays in sequence */
for (i = 0; i < argc; i++) {
- blob = apply_one(blob, ovblob[i], &buf_len, argv[i]);
+ blob = apply_one(blob, ovblob[i], &buf_len, argv[i], target);
if (!blob)
goto out_err;
}
@@ -171,6 +170,7 @@ int main(int argc, char *argv[])
int opt, i;
char *input_filename = NULL;
char *output_filename = NULL;
+ const char *target = NULL;
while ((opt = util_getopt_long()) != EOF) {
switch (opt) {
@@ -185,6 +185,9 @@ int main(int argc, char *argv[])
case 'v':
verbose = 1;
break;
+ case 't':
+ target = optarg;
+ break;
}
}
@@ -207,7 +210,7 @@ int main(int argc, char *argv[])
printf("overlay[%d] = %s\n", i, argv[i]);
}
- if (do_fdtoverlay(input_filename, output_filename, argc, argv))
+ if (do_fdtoverlay(input_filename, output_filename, target, argc, argv))
return 1;
return 0;
diff --git a/libfdt/fdt_overlay.c b/libfdt/fdt_overlay.c
index e6b9eb6439585364ff4e84eecacea7a61bcc67eb..b8df3a9d69949d73f61b4f8644e20cebe44c444b 100644
--- a/libfdt/fdt_overlay.c
+++ b/libfdt/fdt_overlay.c
@@ -40,8 +40,9 @@ static uint32_t overlay_get_target_phandle(const void *fdto, int fragment)
return fdt32_to_cpu(*val);
}
-int fdt_overlay_target_offset(const void *fdt, const void *fdto,
- int fragment_offset, char const **pathp)
+int fdt_overlay_target_offset_v2(const void *fdt, const void *fdto,
+ int fragment_offset, char const **pathp,
+ const char *target)
{
uint32_t phandle;
const char *path = NULL;
@@ -56,10 +57,24 @@ int fdt_overlay_target_offset(const void *fdt, const void *fdto,
if (!phandle) {
/* And then a path based lookup */
path = fdt_getprop(fdto, fragment_offset, "target-path", &path_len);
- if (path)
- ret = fdt_path_offset(fdt, path);
- else
+ if (path) {
+ /* Either target or path should be non-null */
+ if (target == NULL && strlen(path) == 0)
+ return -FDT_ERR_BADPATH;
+
+ ret = 0;
+ if (target) {
+ ret = fdt_path_offset(fdt, target);
+
+ if (ret < 0)
+ return ret;
+ }
+
+ if (strlen(path) > 0)
+ ret = fdt_relative_path_offset(fdt, path, ret);
+ } else {
ret = path_len;
+ }
} else
ret = fdt_node_offset_by_phandle(fdt, phandle);
@@ -84,6 +99,13 @@ int fdt_overlay_target_offset(const void *fdt, const void *fdto,
return ret;
}
+int fdt_overlay_target_offset(const void *fdt, const void *fdto,
+ int fragment_offset, char const **pathp)
+{
+ return fdt_overlay_target_offset_v2(fdt, fdto, fragment_offset, pathp,
+ NULL);
+}
+
/**
* overlay_phandle_add_offset - Increases a phandle by an offset
* @fdt: Base device tree blob
@@ -710,7 +732,8 @@ static int overlay_prevent_phandle_overwrite_node(void *fdt, int fdtnode,
* 0 on success
* Negative error code on failure
*/
-static int overlay_prevent_phandle_overwrite(void *fdt, void *fdto)
+static int overlay_prevent_phandle_overwrite(void *fdt, void *fdto,
+ const char *target_path)
{
int fragment;
@@ -726,7 +749,8 @@ static int overlay_prevent_phandle_overwrite(void *fdt, void *fdto)
if (overlay < 0)
return overlay;
- target = fdt_overlay_target_offset(fdt, fdto, fragment, NULL);
+ target = fdt_overlay_target_offset_v2(fdt, fdto, fragment, NULL,
+ target_path);
if (target == -FDT_ERR_NOTFOUND)
/*
* The subtree doesn't exist in the base, so nothing
@@ -826,7 +850,7 @@ static int overlay_apply_node(void *fdt, int target,
* 0 on success
* Negative error code on failure
*/
-static int overlay_merge(void *fdt, void *fdto)
+static int overlay_merge(void *fdt, void *fdto, const char *target_path)
{
int fragment;
@@ -846,7 +870,8 @@ static int overlay_merge(void *fdt, void *fdto)
if (overlay < 0)
return overlay;
- target = fdt_overlay_target_offset(fdt, fdto, fragment, NULL);
+ target = fdt_overlay_target_offset_v2(fdt, fdto, fragment, NULL,
+ target_path);
if (target < 0)
return target;
@@ -902,7 +927,7 @@ static int get_path_len(const void *fdt, int nodeoffset)
* 0 on success
* Negative error code on failure
*/
-static int overlay_symbol_update(void *fdt, void *fdto)
+static int overlay_symbol_update(void *fdt, void *fdto, const char *target_p)
{
int root_sym, ov_sym, prop, path_len, fragment, target;
int len, frag_name_len, ret, rel_path_len;
@@ -989,7 +1014,8 @@ static int overlay_symbol_update(void *fdt, void *fdto)
return -FDT_ERR_BADOVERLAY;
/* get the target of the fragment */
- ret = fdt_overlay_target_offset(fdt, fdto, fragment, &target_path);
+ ret = fdt_overlay_target_offset_v2(fdt, fdto, fragment,
+ &target_path, target_p);
if (ret < 0)
return ret;
target = ret;
@@ -1011,7 +1037,8 @@ static int overlay_symbol_update(void *fdt, void *fdto)
if (!target_path) {
/* again in case setprop_placeholder changed it */
- ret = fdt_overlay_target_offset(fdt, fdto, fragment, &target_path);
+ ret = fdt_overlay_target_offset_v2(fdt, fdto, fragment,
+ &target_path, NULL);
if (ret < 0)
return ret;
target = ret;
@@ -1038,6 +1065,11 @@ static int overlay_symbol_update(void *fdt, void *fdto)
}
int fdt_overlay_apply(void *fdt, void *fdto)
+{
+ return fdt_overlay_apply_with_target(fdt, fdto, NULL);
+}
+
+int fdt_overlay_apply_with_target(void *fdt, void *fdto, const char *target)
{
uint32_t delta;
int ret;
@@ -1065,15 +1097,15 @@ int fdt_overlay_apply(void *fdt, void *fdto)
goto err;
/* Don't overwrite phandles in fdt */
- ret = overlay_prevent_phandle_overwrite(fdt, fdto);
+ ret = overlay_prevent_phandle_overwrite(fdt, fdto, target);
if (ret)
goto err;
- ret = overlay_merge(fdt, fdto);
+ ret = overlay_merge(fdt, fdto, target);
if (ret)
goto err;
- ret = overlay_symbol_update(fdt, fdto);
+ ret = overlay_symbol_update(fdt, fdto, target);
if (ret)
goto err;
diff --git a/libfdt/libfdt.h b/libfdt/libfdt.h
index 9b61215ecd11c8990128c0b523443b83f84b1013..7a8d3903188487890a88920394e715bc23e11784 100644
--- a/libfdt/libfdt.h
+++ b/libfdt/libfdt.h
@@ -2317,6 +2317,38 @@ int fdt_add_subnode(void *fdt, int parentoffset, const char *name);
*/
int fdt_del_node(void *fdt, int nodeoffset);
+/**
+ * fdt_overlay_apply_with_target - Applies a DT overlay on a base DT
+ * @fdt: pointer to the base device tree blob
+ * @fdto: pointer to the device tree overlay blob
+ * @target: target node path
+ *
+ * fdt_overlay_apply_with_target() will apply the given device tree overlay
+ * on the given node in base device tree.
+ *
+ * Expect the base device tree to be modified, even if the function
+ * returns an error.
+ *
+ * returns:
+ * 0, on success
+ * -FDT_ERR_NOSPACE, there's not enough space in the base device tree
+ * -FDT_ERR_NOTFOUND, the overlay points to some nonexistent nodes or
+ * properties in the base DT
+ * -FDT_ERR_BADPHANDLE,
+ * -FDT_ERR_BADOVERLAY,
+ * -FDT_ERR_NOPHANDLES,
+ * -FDT_ERR_INTERNAL,
+ * -FDT_ERR_BADLAYOUT,
+ * -FDT_ERR_BADMAGIC,
+ * -FDT_ERR_BADOFFSET,
+ * -FDT_ERR_BADPATH,
+ * -FDT_ERR_BADVERSION,
+ * -FDT_ERR_BADSTRUCTURE,
+ * -FDT_ERR_BADSTATE,
+ * -FDT_ERR_TRUNCATED, standard meanings
+ */
+int fdt_overlay_apply_with_target(void *fdt, void *fdto, const char *target);
+
/**
* fdt_overlay_apply - Applies a DT overlay on a base DT
* @fdt: pointer to the base device tree blob
@@ -2348,6 +2380,26 @@ int fdt_del_node(void *fdt, int nodeoffset);
*/
int fdt_overlay_apply(void *fdt, void *fdto);
+/**
+ * fdt_overlay_target_offset_v2 - retrieves the offset of a fragment's target
+ * @fdt: Base device tree blob
+ * @fdto: Device tree overlay blob
+ * @fragment_offset: node offset of the fragment in the overlay
+ * @pathp: pointer which receives the path of the target (or NULL)
+ * @path_prefix: prefix added to any target-path property
+ *
+ * fdt_overlay_target_offset_v2() retrieves the target offset in the base
+ * device tree of a fragment, no matter how the actual targeting is
+ * done (through a phandle or a path)
+ *
+ * returns:
+ * the targeted node offset in the base device tree
+ * Negative error code on error
+ */
+int fdt_overlay_target_offset_v2(const void *fdt, const void *fdto,
+ int fragment_offset, char const **pathp,
+ const char *path_prefix);
+
/**
* fdt_overlay_target_offset - retrieves the offset of a fragment's target
* @fdt: Base device tree blob
diff --git a/libfdt/version.lds b/libfdt/version.lds
index cbfef546de6c7dd2b26a5298be46687be9779233..f9351042ec4ffcabbc14076ddf69115eccde5d9a 100644
--- a/libfdt/version.lds
+++ b/libfdt/version.lds
@@ -68,6 +68,7 @@ LIBFDT_1.2 {
fdt_stringlist_get;
fdt_resize;
fdt_overlay_apply;
+ fdt_overlay_apply_with_target;
fdt_get_string;
fdt_find_max_phandle;
fdt_generate_phandle;
@@ -80,6 +81,7 @@ LIBFDT_1.2 {
fdt_setprop_inplace_namelen_partial;
fdt_create_with_flags;
fdt_overlay_target_offset;
+ fdt_overlay_target_offset_v2;
fdt_get_symbol;
fdt_get_symbol_namelen;
local:
--
2.48.1
^ permalink raw reply related [flat|nested] 4+ messages in thread