[PATCH v3 08/15] libfdt: overlay: Factor out fixup and fragment path parsing helpers

From: Douglas Anderson

Date: Sun Nov 01 2026 - 17:06:07 EST


In preparation for updating the base device tree's `/__local_fixups__`
node during `fdt_overlay_apply()`, factor out two helper functions in
`libfdt/fdt_overlay.c`:

- Factor out `overlay_parse_fixup_entry()` from
`overlay_fixup_phandle()` to parse `"path:name:offset"` entries in
`/__fixups__` properties.
- Factor out `overlay_split_fragment_path()` from
`overlay_symbol_update()` to validate and split
`"/<fragment>/__overlay__[/<rel>]"` paths into the fragment name and
relative subnode path.

This commit is pure code movement with no functional change.

Reviewed-by: Simon Glass <sjg@xxxxxxxxxxxx>
Assisted-by: Gemini:gemini-pro-prerelease
Signed-off-by: Douglas Anderson <dianders@xxxxxxxxxxxx>
---

Changes in v3:
- Document in overlay_parse_fixup_entry()'s comment that @valuep and
@lenp are advanced past the consumed entry (David Gibson)
- Return 1 (under __overlay__), 0 (not under __overlay__), or
-FDT_ERR_BADVALUE from overlay_split_fragment_path() instead of taking
a bool *is_overlayp out-parameter (David Gibson)

Changes in v2:
- New patch factoring out overlay_parse_fixup_entry() and
overlay_split_fragment_path() helpers in libfdt/fdt_overlay.c

libfdt/fdt_overlay.c | 230 ++++++++++++++++++++++++++++---------------
1 file changed, 153 insertions(+), 77 deletions(-)

diff --git a/libfdt/fdt_overlay.c b/libfdt/fdt_overlay.c
index 8d96428af04f..978c9c23bd56 100644
--- a/libfdt/fdt_overlay.c
+++ b/libfdt/fdt_overlay.c
@@ -361,6 +361,81 @@ static int overlay_fixup_one_phandle(void *fdto, int symbols_off,
return ret;
}

+/**
+ * overlay_parse_fixup_entry - Parse one "path:name:offset" entry from /__fixups__
+ * @valuep: In/out pointer to the current NUL-terminated fixup string;
+ * advanced past the terminating NUL byte to the next entry
+ * @lenp: In/out remaining length of the /__fixups__ property value;
+ * decremented by the consumed entry length (including its NUL byte)
+ * @pathp: Output pointer to the target node path
+ * @path_lenp: Output length of @pathp in bytes
+ * @namep: Output pointer to the target property name
+ * @name_lenp: Output length of @namep in bytes
+ * @poffsetp: Output byte offset within the target property
+ *
+ * returns:
+ * 0 on success
+ * -FDT_ERR_BADOVERLAY if the fixup entry is malformed
+ */
+static int overlay_parse_fixup_entry(const char **valuep, int *lenp,
+ const char **pathp, uint32_t *path_lenp,
+ const char **namep, uint32_t *name_lenp,
+ uint32_t *poffsetp)
+{
+ const char *path, *name, *fixup_end;
+ const char *fixup_str = *valuep;
+ uint32_t path_len, name_len;
+ uint32_t fixup_len;
+ const char *sep;
+ char *endptr;
+ unsigned long poffset;
+
+ fixup_end = memchr(*valuep, '\0', *lenp);
+ if (!fixup_end)
+ return -FDT_ERR_BADOVERLAY;
+ fixup_len = fixup_end - fixup_str;
+
+ *lenp -= fixup_len + 1;
+ *valuep += fixup_len + 1;
+
+ path = fixup_str;
+ sep = memchr(fixup_str, ':', fixup_len);
+ if (!sep)
+ return -FDT_ERR_BADOVERLAY;
+
+ path_len = sep - path;
+ if (path_len == (fixup_len - 1))
+ return -FDT_ERR_BADOVERLAY;
+
+ fixup_len -= path_len + 1;
+ name = sep + 1;
+ sep = memchr(name, ':', fixup_len);
+ if (!sep)
+ return -FDT_ERR_BADOVERLAY;
+
+ name_len = sep - name;
+ if (!name_len)
+ return -FDT_ERR_BADOVERLAY;
+
+ /*
+ * Require an initial ASCII digit because strtoul() accepts leading
+ * whitespace and '+'/'-' signs (wrapping negative values to unsigned
+ * long).
+ */
+ if ((*(sep + 1) < '0') || (*(sep + 1) > '9'))
+ return -FDT_ERR_BADOVERLAY;
+ poffset = strtoul(sep + 1, &endptr, 10);
+ if ((*endptr != '\0') || (poffset > UINT32_MAX))
+ return -FDT_ERR_BADOVERLAY;
+
+ *pathp = path;
+ *path_lenp = path_len;
+ *namep = name;
+ *name_lenp = name_len;
+ *poffsetp = poffset;
+ return 0;
+}
+
/**
* overlay_fixup_phandle - Set an overlay phandle to the base one
* @fdt: Base Device Tree blob
@@ -413,52 +488,14 @@ static int overlay_fixup_phandle(void *fdt, void *fdto, int symbols_off,
return -FDT_ERR_NOTFOUND;

do {
- const char *path, *name, *fixup_end;
- const char *fixup_str = value;
- uint32_t path_len, name_len;
- uint32_t fixup_len;
- const char *sep;
- char *endptr;
- unsigned long poffset;
+ const char *path, *name;
+ uint32_t path_len, name_len, poffset;
int ret;

- fixup_end = memchr(value, '\0', len);
- if (!fixup_end)
- return -FDT_ERR_BADOVERLAY;
- fixup_len = fixup_end - fixup_str;
-
- len -= fixup_len + 1;
- value += fixup_len + 1;
-
- path = fixup_str;
- sep = memchr(fixup_str, ':', fixup_len);
- if (!sep)
- return -FDT_ERR_BADOVERLAY;
-
- path_len = sep - path;
- if (path_len == (fixup_len - 1))
- return -FDT_ERR_BADOVERLAY;
-
- fixup_len -= path_len + 1;
- name = sep + 1;
- sep = memchr(name, ':', fixup_len);
- if (!sep)
- return -FDT_ERR_BADOVERLAY;
-
- name_len = sep - name;
- if (!name_len)
- return -FDT_ERR_BADOVERLAY;
-
- /*
- * Require an initial ASCII digit because strtoul() accepts
- * leading whitespace and '+'/'-' signs (wrapping negative
- * values to unsigned long).
- */
- if ((*(sep + 1) < '0') || (*(sep + 1) > '9'))
- return -FDT_ERR_BADOVERLAY;
- poffset = strtoul(sep + 1, &endptr, 10);
- if ((*endptr != '\0') || (poffset > UINT32_MAX))
- return -FDT_ERR_BADOVERLAY;
+ ret = overlay_parse_fixup_entry(&value, &len, &path, &path_len,
+ &name, &name_len, &poffset);
+ if (ret)
+ return ret;

ret = overlay_fixup_one_phandle(fdto, symbols_off,
path, path_len, name, name_len,
@@ -905,6 +942,72 @@ static int get_path_len(const void *fdt, int nodeoffset)
return len;
}

+/**
+ * overlay_split_fragment_path - Split "/<frag>/__overlay__[/<rel>]" into parts
+ * @path: NUL-terminated input path string
+ * @path_len: Length of @path in bytes (including terminating NUL)
+ * @frag_namep: Output pointer to the fragment name
+ * @frag_name_lenp: Output length of @frag_namep in bytes
+ * @rel_pathp: Output pointer to the relative subnode path after __overlay__
+ * @rel_path_lenp: Output length of @rel_pathp in bytes
+ *
+ * returns:
+ * 1 if @path is under an __overlay__ subnode
+ * 0 if @path is a valid path not under an __overlay__ subnode
+ * -FDT_ERR_BADVALUE if @path is malformed
+ */
+static int overlay_split_fragment_path(const char *path, int path_len,
+ const char **frag_namep,
+ int *frag_name_lenp,
+ const char **rel_pathp,
+ int *rel_path_lenp)
+{
+ int len, frag_name_len, rel_path_len;
+ const char *s, *e;
+ const char *frag_name;
+ const char *rel_path;
+
+ /* keep end marker to avoid strlen() */
+ e = path + path_len;
+
+ if (*path != '/')
+ return -FDT_ERR_BADVALUE;
+
+ /* get fragment name first */
+ s = strchr(path + 1, '/');
+ if (!s) {
+ /* Symbol refers to something that won't end
+ * up in the target tree */
+ return 0;
+ }
+
+ frag_name = path + 1;
+ frag_name_len = s - path - 1;
+
+ /* verify format; safe since "s" lies in \0 terminated prop */
+ len = sizeof("/__overlay__/") - 1;
+ if ((e - s) > len && (memcmp(s, "/__overlay__/", len) == 0)) {
+ /* /<fragment-name>/__overlay__/<relative-subnode-path> */
+ rel_path = s + len;
+ rel_path_len = e - rel_path - 1;
+ } else if ((e - s) == len
+ && (memcmp(s, "/__overlay__", len - 1) == 0)) {
+ /* /<fragment-name>/__overlay__ */
+ rel_path = "";
+ rel_path_len = 0;
+ } else {
+ /* Symbol refers to something that won't end
+ * up in the target tree */
+ return 0;
+ }
+
+ *frag_namep = frag_name;
+ *frag_name_lenp = frag_name_len;
+ *rel_pathp = rel_path;
+ *rel_path_lenp = rel_path_len;
+ return 1;
+}
+
/**
* overlay_symbol_update - Update the symbols of base tree after a merge
* @fdt: Base Device Tree blob
@@ -925,7 +1028,6 @@ static int overlay_symbol_update(void *fdt, void *fdto)
{
int root_sym, ov_sym, prop, path_len, fragment, target;
int len, frag_name_len, ret, rel_path_len;
- const char *s, *e;
const char *path;
const char *name;
const char *frag_name;
@@ -960,39 +1062,13 @@ static int overlay_symbol_update(void *fdt, void *fdto)
if (path_len < 1 || memchr(path, '\0', path_len) != &path[path_len - 1])
return -FDT_ERR_BADVALUE;

- /* keep end marker to avoid strlen() */
- e = path + path_len;
-
- if (*path != '/')
- return -FDT_ERR_BADVALUE;
-
- /* get fragment name first */
- s = strchr(path + 1, '/');
- if (!s) {
- /* Symbol refers to something that won't end
- * up in the target tree */
- continue;
- }
-
- frag_name = path + 1;
- frag_name_len = s - path - 1;
-
- /* verify format; safe since "s" lies in \0 terminated prop */
- len = sizeof("/__overlay__/") - 1;
- if ((e - s) > len && (memcmp(s, "/__overlay__/", len) == 0)) {
- /* /<fragment-name>/__overlay__/<relative-subnode-path> */
- rel_path = s + len;
- rel_path_len = e - rel_path - 1;
- } else if ((e - s) == len
- && (memcmp(s, "/__overlay__", len - 1) == 0)) {
- /* /<fragment-name>/__overlay__ */
- rel_path = "";
- rel_path_len = 0;
- } else {
- /* Symbol refers to something that won't end
- * up in the target tree */
+ ret = overlay_split_fragment_path(path, path_len,
+ &frag_name, &frag_name_len,
+ &rel_path, &rel_path_len);
+ if (ret < 0)
+ return ret;
+ if (!ret)
continue;
- }

/* find the fragment index in which the symbol lies */
ret = fdt_subnode_offset_namelen(fdto, 0, frag_name,
--
2.56.0.317.g454f8302e1-goog