dtoverlay: Change node address when "reg" is set
authorPhil Elwell <phil@raspberrypi.org>
Mon, 27 Mar 2017 13:50:25 +0000 (14:50 +0100)
committerpopcornmix <popcornmix@gmail.com>
Tue, 16 May 2017 12:49:16 +0000 (13:49 +0100)
The DT parameter mechanism has long needed the ability to change node
names to allow the same overlay to be used repeatedly. Many such use
cases can be handled by simply patching the address part of the node
name when the node's "reg" property is written.

See: https://www.raspberrypi.org/forums/viewtopic.php?f=107&t=178721

Signed-off-by: Phil Elwell <phil@raspberrypi.org>
helpers/dtoverlay/dtoverlay.c

index bf57cdf1b51ade62cd0971569acf58fbf47f12a1..a1d2d2352fbba53116488d27cea38838c053d3ea 100644 (file)
@@ -956,6 +956,22 @@ int dtoverlay_override_one_target(int override_type,
              err = fdt_setprop(dtb->fdt, node_off, prop_name, prop_buf, new_prop_len);
             free(prop_buf);
         }
+
+         if (strcmp(prop_name, "reg") == 0 && target_off == 0)
+         {
+            const char *old_name = fdt_get_name(dtb->fdt, node_off, NULL);
+            const char *atpos = strchr(old_name, '@');
+            if (atpos)
+            {
+               int name_len = (atpos - old_name);
+               char *new_name = malloc(name_len + 1 + 16 + 1);
+               if (!new_name)
+                  return -FDT_ERR_NOSPACE;
+               sprintf(new_name, "%.*s@%x", name_len, old_name, (uint32_t)override_int);
+               fdt_set_name(dtb->fdt, node_off, new_name);
+               free(new_name);
+            }
+         }
         break;
 
       case DTOVERRIDE_BOOLEAN: