Skip to content

dtoverlay: allow top-level phandles to optionally be preserved#198

Merged
pelwell merged 1 commit into
raspberrypi:masterfrom
compholio:master
Jun 12, 2026
Merged

dtoverlay: allow top-level phandles to optionally be preserved#198
pelwell merged 1 commit into
raspberrypi:masterfrom
compholio:master

Conversation

@compholio

Copy link
Copy Markdown
Contributor

Look for the dtoverlay,preserve-phandle flag in the overlay and, if it's present, keep and merge the phandle.

This is useful with ethernet PHYs, like so:

/ {
	enet: fragment@1 {
		overlay: __overlay__ {
			status = "okay";
		};
	};
	phy: fragment@3 {
		phy_handle: __dormant__ {
			dtoverlay,preserve-phandle;
		};
	};
};

/ {
	__overrides__ {
		enet =
			<&enet>,"target-path"
		;
		phy =
			<0>,"+3"
		,
			<&overlay>,"phy-handle:0=",<&phy_handle>
		,
			<&phy>,"target-path"
		;
	};
};

Without this patch the top-level phandle is not preserved, so the phy-handle does not match the node for the PHY.

@pelwell

pelwell commented Jun 11, 2026

Copy link
Copy Markdown
Collaborator

I think we can make it shorter (and more efficient):

@@ -564,6 +564,7 @@ int dtoverlay_merge_fragment(DTBLOB_T *base_dtb, int target_off,
                                     int overlay_off, int depth)
 {
     int prop_off, subnode_off;
+    int preserve_phandles;
     int err = 0;
 
     if (dtoverlay_debug_enabled)
@@ -578,6 +579,10 @@ int dtoverlay_merge_fragment(DTBLOB_T *base_dtb, int target_off,
                         overlay_path);
     }
 
+    // Check for a property flag to indicate preserving top-level phandles
+    preserve_phandles = depth > 0 ||
+        fdt_getprop(overlay_dtb->fdt, overlay_off, "dtoverlay,preserve-phandle", NULL);
+
     // Merge each property of the node
     for (prop_off = fdt_first_property_offset(overlay_dtb->fdt, overlay_off);
          (prop_off >= 0) && (err == 0);
@@ -592,10 +597,11 @@ int dtoverlay_merge_fragment(DTBLOB_T *base_dtb, int target_off,
         prop_val = fdt_getprop_by_offset(overlay_dtb->fdt, prop_off,
                                          &prop_name, &prop_len);
 
-        /* Skip these system properties (only phandles in the first level) */
+        // Skip system properties and top-level phandles unless preserved
         if ((strcmp(prop_name, "name") == 0) ||
-            ((depth == 0) && ((strcmp(prop_name, "phandle") == 0) ||
-                              (strcmp(prop_name, "linux,phandle") == 0))))
+            (strcmp(prop_name, "dtoverlay,preserve-phandle") == 0) ||
+            (!preserve_phandles && ((strcmp(prop_name, "phandle") == 0) ||
+                                    (strcmp(prop_name, "linux,phandle") == 0))))
             continue;
 
         dtoverlay_debug("  +prop(%s)", prop_name);

Look for the 'dtoverlay,preserve-phandle' flag in the overlay and,
if it's present, keep and merge the phandle.
@compholio

Copy link
Copy Markdown
Contributor Author

Updated with your suggestions (I like your way a lot better).

@pelwell pelwell merged commit c6e0e3a into raspberrypi:master Jun 12, 2026
4 checks passed
pelwell added a commit that referenced this pull request Jun 12, 2026
Look for the 'dtoverlay,preserve-phandle' flag in the overlay payloads,
preserving the label on any payloads where it is present.

This is the ovmerge equivalent of
#198.

Signed-off-by: Phil Elwell <phil@raspberrypi.com>
@pelwell

pelwell commented Jun 12, 2026

Copy link
Copy Markdown
Collaborator

Thanks!

BTW, #199 is the equivalent change to ovmerge.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants