of_property: resolve phandles against the booted device tree
cover of_property: resolve phandles against the booted device tree
Hi,
I ran into this on a phyBOARD-Mira (i.MX6) secure boot setup. Two board expansions configure a UART pin group via a fixup along these lines:
of_property -s -f -e $global.bootm.oftree /soc/.../serial@21ec000 \
pinctrl-0 </soc/.../pinctrl@20e0000/uart3grp>The node path is resolved to a phandle immediately, against the external dtb named by -e. With secure boot the kernel device tree is part of the FIT image, so that file never exists on disk: of_property fails, the fixup is never registered, and the UART keeps its default pin group, which conflicts with other pins on the board (user LEDs, in this case).
The patch defers phandle resolution to the point the fixup is applied, against the tree that is actually being booted, so -e is no longer needed for this and is now ignored (rather than required) when combined with -f. Existing scripts that still pass -e keep working.
With this applied, setups like the one above can drop the -e $global.bootm.oftree workaround entirely.
Primoz Fiser (1):
commands: of_property: resolve phandles of fixups in the booted tree
commands/of_property.c | 75 ++++++++++++++++++++++++++++-------------- 1 file changed, 51 insertions(+), 24 deletions(-)
Signature · 1 line
2.34.1
PATCH 1/1 commands: of_property: resolve phandles of fixups in the booted tree
Node paths in a cell value are converted to phandles when the command
runs: in the barebox live tree, or in the tree read with -e. For a fixup (-f), the phandle is then written to the tree the kernel is booted with. Phandles are assigned by dtc and differ between the barebox and the kernel tree, so without -e the fixup points to a wrong node.
With -e, the kernel dtb must be available as a separate file, and its phandles must match the tree that is actually booted. This is not the case when the device tree comes from a FIT image: the command fails because the file does not exist, and the fixup is never registered.
Only check the syntax of the value when the command runs, and parse it when the fixup is applied, so node paths are resolved in the tree that is booted. The -e option is not needed for fixups anymore and is ignored with -f, so existing scripts keep working.
--- commands/of_property.c | 75 ++++++++++++++++++++++++++++-------------- 1 file changed, 51 insertions(+), 24 deletions(-)
commands/of_property.c +51 -24
@@ -19,7 +19,12 @@#include <init.h> #include <xfuncs.h> -static int of_parse_prop_cells (struct device_node *root,char *const *newval,int count,char *data,int *len) +/* +* With @resolve false, node paths are only checked for syntax and +* converted to 0 instead of a phandle. +*/ +static int of_parse_prop_cells (struct device_node *root,bool resolve, +char *const *newval,int count,char *data,int *len) {char *cp;unsigned long tmp;/* holds converted values */
@@ -58,6 +63,12 @@ static int of_parse_prop_cells(struct device_node *root, char * const *newval, i len++; } +if (!resolve) { + tmp =0 ; + newp += len; + goto next; + } + str =xzalloc (len +1 );strncpy (str, cp, len);
@@ -73,7 +84,7 @@ static int of_parse_prop_cells(struct device_node *root, char * const *newval, i tmp =of_node_create_phandle (n); newp += len; } - +next: *(__be32 *)data =__cpu_to_be32 (tmp); data +=4 ; *len +=4 ;
@@ -165,7 +176,8 @@ 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 (struct device_node *root,char *const *newval,int count,char *data,int *len) +static int of_parse_prop (struct device_node *root,bool resolve, +char *const *newval,int count,char *data,int *len) {char *newp;/* temporary newval char pointer */
@@ -178,7 +190,7 @@ static int of_parse_prop(struct device_node *root, char * const *newval, int couswitch (*newp) {case '<' : -return of_parse_prop_cells (root, newval, count, data, len); +return of_parse_prop_cells (root, resolve, newval, count, data, len);case '[' :return of_parse_prop_stream (newval, count, data, len);default :
@@ -189,8 +201,8 @@ static int of_parse_prop(struct device_node *root, char * const *newval, int coustruct of_fixup_property_data {const char *path;const char *propname; -void *data; -int len; +char **values; +int count; }; static int do_of_property_delete_now(struct device_node *root, const char *path,
@@ -201,9 +213,20 @@ static int do_of_property_set_now(struct device_node *root, const char *path,static int of_fixup_property_set (struct device_node *root,void *context) {struct of_fixup_property_data *fixup = context; +void *data; +int len, ret; + +/* parse now, so node paths resolve against the tree being booted */ + data =xmalloc (1024 ); + + ret =of_parse_prop (root, true, fixup->values, fixup->count, data, &len); +if (!ret) + ret =do_of_property_set_now (root, fixup->path, fixup->propname, + data, len); -return do_of_property_set_now (root, fixup->path, fixup->propname, - fixup->data, fixup->len); +free (data); + +return ret; }static int of_fixup_property_delete (struct device_node *root,void *context)
@@ -214,15 +237,19 @@ static int of_fixup_property_delete(struct device_node *root, void *context) }static int do_of_property_set_fixup (const char *path,const char *propname, -void *data,int len) +char *const *values,int count) {struct of_fixup_property_data *fixup; +int i; fixup =xzalloc (sizeof (*fixup)); fixup->path =xstrdup_const (path); fixup->propname =xstrdup_const (propname); - fixup->data = data; - fixup->len = len; + fixup->values =xzalloc (count *sizeof (*fixup->values)); + fixup->count = count; + +for (i =0 ; i < count; i++) + fixup->values[i] =xstrdup (values[i]);return of_register_fixup (of_fixup_property_set, (void *)fixup); }
@@ -234,8 +261,6 @@ static int do_of_property_delete_fixup(const char *path, const char *propname) fixup =xzalloc (sizeof (*fixup)); fixup->path =xstrdup_const (path); fixup->propname =xstrdup_const (propname); - fixup->data =NULL ; - fixup->len =0 ;return of_register_fixup (of_fixup_property_delete, (void *)fixup); }
@@ -338,7 +363,8 @@ static int do_of_property(int argc, char *argv[])if ( !(set || delete))return COMMAND_ERROR_USAGE ; -if (dtbfile) { +/* fixups apply to the booted tree; -e is accepted but ignored */ +if (dtbfile && !fixup) { root =of_read_file (dtbfile);if (IS_ERR (root))return PTR_ERR (root);
@@ -356,20 +382,20 @@ static int do_of_property(int argc, char *argv[])if (!data)return -ENOMEM ; - ret =of_parse_prop (root, &argv[optind +2 ], argc - optind -2 , data, &len); + ret =of_parse_prop (root, !fixup, &argv[optind +2 ], + argc - optind -2 , data, &len);if (ret) {free (data); goto out; } -if (fixup) { - ret =do_of_property_set_fixup (path, propname, data, len); -if (ret) -free (data); - }else { +if (fixup) + ret =do_of_property_set_fixup (path, propname, + &argv[optind +2 ], argc - optind -2 ); +else ret =do_of_property_set_now (root, path, propname, data, len); -free (data); - } + +free (data);if (ret) goto out;
@@ -404,11 +430,12 @@ 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_OPT ("-e dtb", "set/delete in external dtb (ignored with -f)") 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") -BAREBOX_CMD_HELP_TEXT(" interpreted as node paths and converted to phandles") +BAREBOX_CMD_HELP_TEXT(" interpreted as node paths and converted to phandles.") +BAREBOX_CMD_HELP_TEXT(" With -f, the paths are looked up in the booted tree") BAREBOX_CMD_HELP_TEXT("[00 11 22 .. nn] - byte stream") BAREBOX_CMD_HELP_TEXT("If the value does not start with '<' or '[' it is interpreted as string")BAREBOX_CMD_HELP_END
Signature · 1 line
2.34.1
Sascha Hauer
AppliedRe: [PATCH 1/1] commands: of_property: resolve phandles of fixups in the booted tree
Quoted 6 lines · Primoz Fiser
Node paths in a cell value are converted to phandles when the command
runs: in the barebox live tree, or in the tree read with -e. For a fixup (-f), the phandle is then written to the tree the kernel is booted with. Phandles are assigned by dtc and differ between the barebox and the kernel tree, so without -e the fixup points to a wrong node.
Quoted 6 lines
With -e, the kernel dtb must be available as a separate file, and its phandles must match the tree that is actually booted. This is not the case when the device tree comes from a FIT image: the command fails because the file does not exist, and the fixup is never registered.
[...]
Applied, thanks!
[1/1] commands: of_property: resolve phandles of fixups in the booted tree
https://git.pengutronix.de/cgit/barebox/commit/?id=a02195e65cc9 (link may not be stable)
Best regards,
Signature · 1 line
Sascha Hauer <[email protected]>
Hi Primoz,
Quoted 7 lines · Primoz Fiser
Hi,
I ran into this on a phyBOARD-Mira (i.MX6) secure boot setup. Two board expansions configure a UART pin group via a fixup along these lines:
of_property -s -f -e $global.bootm.oftree /soc/.../serial@21ec000 \ pinctrl-0 </soc/.../pinctrl@20e0000/uart3grp>
So you are using -e to pass the same device tree that you are booting with later to make the phandles fit together. Took me a while to figure that out ;)
I think -f and -e should be mutually exclusive as a combination of both doesn't make sense, but now as you are apparently using exactly this combination to work around a bug I think it's fine to just ignore -e when -f is given.
BTW while looking at the patch I stumbled upon:
/* * standard console buffer size. The result won't be bigger than the * string input. */ data = malloc(1024);
This is actually wrong. A property given as <1 2 3 4> will expand to 4*32bit which is already longer than the input string, so putting more than 256 numbers like this into a property will overflow the buffer.
Sascha
Signature · 4 lines
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 |