mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH v2] commands: of_property: Extend command usage on external dtb
@ 2021-06-25 16:05 Yunus Bas
  2021-06-28 20:23 ` Sascha Hauer
  0 siblings, 1 reply; 6+ messages in thread
From: Yunus Bas @ 2021-06-25 16:05 UTC (permalink / raw)
  To: barebox

The scope of the of_property command is limited to the internal devicetree
only. There is the possibility to add fixtures to postpone the operation
until boot and after the kernel DTB is already loaded, but this does not
cover the case when the property value needs to be phandle adresses
defined in the external DTB, since assigned values are evaluated
immediatly.

This patch extends the of_property-command to use it on external DTB's as
well. In case fixup is not used, the changes will also affect the original
data.

Signed-off-by: Yunus Bas <y.bas@phytec.de>
---
Changes in v2:
- Use %m instead of "%s", strerror(errno)
- Not fixing devicetree anymore before save
---
 commands/of_property.c | 74 ++++++++++++++++++++++++++++++++----------
 1 file changed, 57 insertions(+), 17 deletions(-)

diff --git a/commands/of_property.c b/commands/of_property.c
index 3d5097165..41bb5f71b 100644
--- a/commands/of_property.c
+++ b/commands/of_property.c
@@ -4,6 +4,7 @@
 /* of_property.c - device tree property handling support */
 
 #include <common.h>
+#include <libfile.h>
 #include <environment.h>
 #include <fdt.h>
 #include <of.h>
@@ -18,7 +19,7 @@
 #include <init.h>
 #include <xfuncs.h>
 
-static int of_parse_prop_cells(char * const *newval, int count, char *data, int *len)
+static int of_parse_prop_cells(struct device_node *root, char * const *newval, int count, char *data, int *len)
 {
 	char *cp;
 	unsigned long tmp;	/* holds converted values */
@@ -60,7 +61,7 @@ static int of_parse_prop_cells(char * const *newval, int count, char *data, int
 			str = xzalloc(len + 1);
 			strncpy(str, cp, len);
 
-			n = of_find_node_by_path_or_alias(NULL, str);
+			n = of_find_node_by_path_or_alias(root, str);
 			if (!n)
 				printf("Cannot find node '%s'\n", str);
 
@@ -164,7 +165,7 @@ static int of_parse_prop_string(char * const *newval, int count, char *data, int
  * data: A bytestream to be placed in the property
  * len: The length of the resulting bytestream
  */
-static int of_parse_prop(char * const *newval, int count, char *data, int *len)
+static int of_parse_prop(struct device_node *root, char * const *newval, int count, char *data, int *len)
 {
 	char *newp;		/* temporary newval char pointer */
 
@@ -177,7 +178,7 @@ static int of_parse_prop(char * const *newval, int count, char *data, int *len)
 
 	switch (*newp) {
 	case '<':
-		return of_parse_prop_cells(newval, count, data, len);
+		return of_parse_prop_cells(root, newval, count, data, len);
 	case '[':
 		return of_parse_prop_stream(newval, count, data, len);
 	default:
@@ -302,8 +303,13 @@ static int do_of_property(int argc, char *argv[])
 	int set = 0;
 	int fixup = 0;
 	char *path, *propname;
+	char *dtbfile = NULL;
+	int ret = 0;
+	size_t size;
+	struct fdt_header *fdt = NULL;
+	struct device_node *root = NULL;
 
-	while ((opt = getopt(argc, argv, "dsf")) > 0) {
+	while ((opt = getopt(argc, argv, "dsfe:")) > 0) {
 		switch (opt) {
 		case 'd':
 			delete = 1;
@@ -314,6 +320,9 @@ static int do_of_property(int argc, char *argv[])
 		case 'f':
 			fixup = 1;
 			break;
+		case 'e':
+			dtbfile = optarg;
+			break;
 		default:
 			return COMMAND_ERROR_USAGE;
 		}
@@ -327,8 +336,22 @@ static int do_of_property(int argc, char *argv[])
 
 	debug("path: %s propname: %s\n", path, propname);
 
+	if ( !(set || delete))
+		return COMMAND_ERROR_USAGE;
+
+	if (dtbfile) {
+		fdt = read_file(dtbfile, &size);
+		if (!fdt) {
+			printf("unable to read %s: %m\n", dtbfile);
+			return -errno;
+		}
+
+		root = of_unflatten_dtb(fdt);
+
+		free(fdt);
+	}
+
 	if (set) {
-		int ret;
 		int len;
 		void *data;
 
@@ -340,10 +363,10 @@ static int do_of_property(int argc, char *argv[])
 		if (!data)
 			return -ENOMEM;
 
-		ret = of_parse_prop(&argv[optind + 2], argc - optind - 2, data, &len);
+		ret = of_parse_prop(root, &argv[optind + 2], argc - optind - 2, data, &len);
 		if (ret) {
 			free(data);
-			return ret;
+			goto out;
 		}
 
 		if (fixup) {
@@ -351,21 +374,37 @@ static int do_of_property(int argc, char *argv[])
 			if (ret)
 				free(data);
 		} else {
-			ret = do_of_property_set_now(NULL, path, propname, data, len);
+			ret = do_of_property_set_now(root, path, propname, data, len);
 			free(data);
 		}
 
-		return ret;
-	}
+		if (ret)
+			goto out;
 
-	if (delete) {
+	} else if (delete) {
 		if (fixup)
-			return do_of_property_delete_fixup(path, propname);
+			ret = do_of_property_delete_fixup(path, propname);
 		else
-			return do_of_property_delete_now(NULL, path, propname);
+			ret = do_of_property_delete_now(root, path, propname);
+
+		if (ret)
+			goto out;
+	}
+
+	if (root && !fixup) {
+		fdt = of_flatten_dtb(root);
+
+		ret = write_file(dtbfile, fdt, fdt32_to_cpu(fdt->totalsize));
+
+		free(fdt);
 	}
 
-	return COMMAND_ERROR_USAGE;
+out:
+
+	if (root)
+		of_delete_node(root);
+
+	return ret;
 }
 
 BAREBOX_CMD_HELP_START(of_property)
@@ -373,6 +412,7 @@ BAREBOX_CMD_HELP_TEXT("Options:")
 BAREBOX_CMD_HELP_OPT ("-s",  "set property to value")
 BAREBOX_CMD_HELP_OPT ("-d",  "delete property")
 BAREBOX_CMD_HELP_OPT ("-f",  "set/delete as a fixup (defer the action until booting)")
+BAREBOX_CMD_HELP_OPT ("-e dtb",  "set/delete/fixup from external dtb")
 BAREBOX_CMD_HELP_TEXT("")
 BAREBOX_CMD_HELP_TEXT("Valid formats for values:")
 BAREBOX_CMD_HELP_TEXT("<0x00112233 4 05> - an array of cells. cells not beginning with a digit are")
@@ -384,8 +424,8 @@ BAREBOX_CMD_HELP_END
 BAREBOX_CMD_START(of_property)
 	.cmd		= do_of_property,
 	BAREBOX_CMD_DESC("handle device tree properties")
-	BAREBOX_CMD_OPTS("[-sd] [-f] NODE [PROPERTY] [VALUES]")
+	BAREBOX_CMD_OPTS("[-sd] [-e] [-f] NODE [PROPERTY] [VALUES]")
 	BAREBOX_CMD_GROUP(CMD_GRP_MISC)
-	BAREBOX_CMD_COMPLETE(devicetree_complete)
+	BAREBOX_CMD_COMPLETE(devicetree_file_complete)
 	BAREBOX_CMD_HELP(cmd_of_property_help)
 BAREBOX_CMD_END
-- 
2.30.0


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2] commands: of_property: Extend command usage on external dtb
  2021-06-25 16:05 [PATCH v2] commands: of_property: Extend command usage on external dtb Yunus Bas
@ 2021-06-28 20:23 ` Sascha Hauer
  2021-06-29  9:25   ` Michael Riesch
  0 siblings, 1 reply; 6+ messages in thread
From: Sascha Hauer @ 2021-06-28 20:23 UTC (permalink / raw)
  To: Yunus Bas; +Cc: barebox

On Fri, Jun 25, 2021 at 06:05:55PM +0200, Yunus Bas wrote:
> The scope of the of_property command is limited to the internal devicetree
> only. There is the possibility to add fixtures to postpone the operation
> until boot and after the kernel DTB is already loaded, but this does not
> cover the case when the property value needs to be phandle adresses
> defined in the external DTB, since assigned values are evaluated
> immediatly.
> 
> This patch extends the of_property-command to use it on external DTB's as
> well. In case fixup is not used, the changes will also affect the original
> data.
> 
> Signed-off-by: Yunus Bas <y.bas@phytec.de>
> ---
> Changes in v2:
> - Use %m instead of "%s", strerror(errno)
> - Not fixing devicetree anymore before save
> ---
>  commands/of_property.c | 74 ++++++++++++++++++++++++++++++++----------
>  1 file changed, 57 insertions(+), 17 deletions(-)

Applied, thanks

Sascha

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2] commands: of_property: Extend command usage on external dtb
  2021-06-28 20:23 ` Sascha Hauer
@ 2021-06-29  9:25   ` Michael Riesch
  2021-06-29 11:33     ` Yunus Bas
  0 siblings, 1 reply; 6+ messages in thread
From: Michael Riesch @ 2021-06-29  9:25 UTC (permalink / raw)
  To: Sascha Hauer, Yunus Bas; +Cc: barebox

Hello Sascha,

On 6/28/21 10:23 PM, Sascha Hauer wrote:
> On Fri, Jun 25, 2021 at 06:05:55PM +0200, Yunus Bas wrote:
>> The scope of the of_property command is limited to the internal devicetree
>> only. There is the possibility to add fixtures to postpone the operation
>> until boot and after the kernel DTB is already loaded, but this does not
>> cover the case when the property value needs to be phandle adresses
>> defined in the external DTB, since assigned values are evaluated
>> immediatly.
>>
>> This patch extends the of_property-command to use it on external DTB's as
>> well. In case fixup is not used, the changes will also affect the original
>> data.
>>
>> Signed-off-by: Yunus Bas <y.bas@phytec.de>
>> ---
>> Changes in v2:
>> - Use %m instead of "%s", strerror(errno)
>> - Not fixing devicetree anymore before save
>> ---
>>  commands/of_property.c | 74 ++++++++++++++++++++++++++++++++----------
>>  1 file changed, 57 insertions(+), 17 deletions(-)
> 
> Applied, thanks
> 
> Sascha

on the current next this patch collides with "fdt: Check blob size
during unflattening" as the call

>> +		root = of_unflatten_dtb(fdt);

requires now an extra size argument.

There is a variable "size" before the call. Is it safe to use it?

Regards, Michael

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2] commands: of_property: Extend command usage on external dtb
  2021-06-29  9:25   ` Michael Riesch
@ 2021-06-29 11:33     ` Yunus Bas
  2021-06-29 12:04       ` [PATCH] fixup! " Michael Riesch
  0 siblings, 1 reply; 6+ messages in thread
From: Yunus Bas @ 2021-06-29 11:33 UTC (permalink / raw)
  To: michael.riesch, sha; +Cc: barebox

Hi Michael,

Am Dienstag, dem 29.06.2021 um 11:25 +0200 schrieb Michael Riesch:
> Hello Sascha,
> 
> On 6/28/21 10:23 PM, Sascha Hauer wrote:
> > On Fri, Jun 25, 2021 at 06:05:55PM +0200, Yunus Bas wrote:
> > > The scope of the of_property command is limited to the internal
> > > devicetree
> > > only. There is the possibility to add fixtures to postpone the
> > > operation
> > > until boot and after the kernel DTB is already loaded, but this
> > > does not
> > > cover the case when the property value needs to be phandle
> > > adresses
> > > defined in the external DTB, since assigned values are evaluated
> > > immediatly.
> > > 
> > > This patch extends the of_property-command to use it on external
> > > DTB's as
> > > well. In case fixup is not used, the changes will also affect the
> > > original
> > > data.
> > > 
> > > Signed-off-by: Yunus Bas <y.bas@phytec.de>
> > > ---
> > > Changes in v2:
> > > - Use %m instead of "%s", strerror(errno)
> > > - Not fixing devicetree anymore before save
> > > ---
> > >  commands/of_property.c | 74 ++++++++++++++++++++++++++++++++----
> > > ------
> > >  1 file changed, 57 insertions(+), 17 deletions(-)
> > 
> > Applied, thanks
> > 
> > Sascha
> 
> on the current next this patch collides with "fdt: Check blob size
> during unflattening" as the call
> 
> > > +               root = of_unflatten_dtb(fdt);
> 
> requires now an extra size argument.
> 
> There is a variable "size" before the call. Is it safe to use it?

The size variable is read from the dtb, so I think it's also intended
to use it. Shall I send a v3 or can you apply the change?

Regards, Yunus

> 
> Regards, Michael

-- 
Mit freundlichen Grüßen
Yunus Bas

-Software Engineer-
PHYTEC Messtechnik GmbH
Robert-Koch-Str. 39
55129 Mainz
Germany
Tel.: +49 (0)6131 9221- 466
Web: www.phytec.de

Sie finden uns auch auf: Facebook, LinkedIn, Xing, YouTube

PHYTEC Messtechnik GmbH | Robert-Koch-Str. 39 | 55129 Mainz, Germany
Geschäftsführer: Dipl.-Ing. Michael Mitezki, Dipl.-Ing. Bodo Huber |
Handelsregister Mainz HRB 4656 | Finanzamt Mainz | St.Nr. 266500608, DE
149059855
This E-Mail may contain confidential or privileged information. If you
are not the intended recipient (or have received this E-Mail in error)
please notify the sender immediately and destroy this E-Mail. Any
unauthorized copying, disclosure or distribution of the material in
this E-Mail is strictly forbidden.

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH] fixup! commands: of_property: Extend command usage on external dtb
  2021-06-29 11:33     ` Yunus Bas
@ 2021-06-29 12:04       ` Michael Riesch
  2021-07-03 20:42         ` Sascha Hauer
  0 siblings, 1 reply; 6+ messages in thread
From: Michael Riesch @ 2021-06-29 12:04 UTC (permalink / raw)
  To: barebox; +Cc: Michael Riesch

---
 commands/of_property.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/commands/of_property.c b/commands/of_property.c
index 41bb5f71b..063e97775 100644
--- a/commands/of_property.c
+++ b/commands/of_property.c
@@ -346,7 +346,7 @@ static int do_of_property(int argc, char *argv[])
 			return -errno;
 		}
 
-		root = of_unflatten_dtb(fdt);
+		root = of_unflatten_dtb(fdt, size);
 
 		free(fdt);
 	}
-- 
2.20.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] fixup! commands: of_property: Extend command usage on external dtb
  2021-06-29 12:04       ` [PATCH] fixup! " Michael Riesch
@ 2021-07-03 20:42         ` Sascha Hauer
  0 siblings, 0 replies; 6+ messages in thread
From: Sascha Hauer @ 2021-07-03 20:42 UTC (permalink / raw)
  To: Michael Riesch; +Cc: barebox

On Tue, Jun 29, 2021 at 02:04:45PM +0200, Michael Riesch wrote:
> ---
>  commands/of_property.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)

Thanks for catching this. I had already fixed that up.

Sascha

> 
> diff --git a/commands/of_property.c b/commands/of_property.c
> index 41bb5f71b..063e97775 100644
> --- a/commands/of_property.c
> +++ b/commands/of_property.c
> @@ -346,7 +346,7 @@ static int do_of_property(int argc, char *argv[])
>  			return -errno;
>  		}
>  
> -		root = of_unflatten_dtb(fdt);
> +		root = of_unflatten_dtb(fdt, size);
>  
>  		free(fdt);
>  	}
> -- 
> 2.20.1
> 
> 
> _______________________________________________
> barebox mailing list
> barebox@lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/barebox
> 

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2021-07-03 20:43 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-25 16:05 [PATCH v2] commands: of_property: Extend command usage on external dtb Yunus Bas
2021-06-28 20:23 ` Sascha Hauer
2021-06-29  9:25   ` Michael Riesch
2021-06-29 11:33     ` Yunus Bas
2021-06-29 12:04       ` [PATCH] fixup! " Michael Riesch
2021-07-03 20:42         ` Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox