mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH] ARM: rpi: cleanup parsing of vc fdt
@ 2022-06-21 20:02 Daniel Brát
  2022-06-23 12:10 ` Ahmad Fatoum
  2022-06-27  6:54 ` Sascha Hauer
  0 siblings, 2 replies; 3+ messages in thread
From: Daniel Brát @ 2022-06-21 20:02 UTC (permalink / raw)
  To: barebox; +Cc: Daniel Brát

Refactoring and cleanup of vc fdt parsing based on advice
from Ahmad Fatoum.

Signed-off-by: Daniel Brát <danek.brat@gmail.com>
---
I originally wanted to send these as part of v5 of
'ARM: rpi: parse useful data from vc fdt' patch, but you
were quicker with accepting the v4 :)
Hope that's not a problem.

 arch/arm/boards/raspberry-pi/rpi-common.c | 114 +++++++++++-----------
 1 file changed, 58 insertions(+), 56 deletions(-)

diff --git a/arch/arm/boards/raspberry-pi/rpi-common.c b/arch/arm/boards/raspberry-pi/rpi-common.c
index eaca4edef..77935e5c8 100644
--- a/arch/arm/boards/raspberry-pi/rpi-common.c
+++ b/arch/arm/boards/raspberry-pi/rpi-common.c
@@ -202,22 +202,52 @@ static int rpi_env_init(void)
 }
 
 /* Some string properties in fdt passed to us from vc may be
- * malformed by not being null terminated. In that case, create and
- * return a null terminated copy.
+ * malformed by not being null terminated, so just create and
+ * return a fixed copy.
  */
 static char *of_read_vc_string(struct device_node *node,
-				     const char *prop_name,
-				     bool *is_dyn)
+			       const char *prop_name)
 {
 	int len;
-	char *str;
+	const char *str;
 
-	str = (char *)of_get_property(node, prop_name, &len);
-	if (!str || len <= 0)
+	str = of_get_property(node, prop_name, &len);
+	if (!str) {
+		pr_warn("no property '%s' found in vc fdt's '%s' node\n",
+			prop_name, node->full_name);
 		return NULL;
-	if (is_dyn)
-		*is_dyn = str[len - 1] != '\0';
-	return str[len - 1] == '\0' ? str : xstrndup(str, len);
+	}
+	return xstrndup(str, len);
+}
+
+static enum reset_src_type rpi_decode_pm_rsts(struct device_node *chosen,
+					      struct device_node *bootloader)
+{
+	u32 pm_rsts;
+	int ret;
+
+	ret = of_property_read_u32(chosen, "pm_rsts", &pm_rsts);
+	if (ret && bootloader)
+		 ret = of_property_read_u32(bootloader, "rsts", &pm_rsts);
+
+	if (ret) {
+		pr_warn("'pm_rsts' value not found in vc fdt\n");
+		return RESET_UKWN;
+	}
+	/*
+	 * https://github.com/raspberrypi/linux/issues/932#issuecomment-93989581
+	 */
+
+	if (pm_rsts & PM_RSTS_HADPOR_SET)
+		return RESET_POR;
+	if (pm_rsts & PM_RSTS_HADDR_SET)
+		return RESET_JTAG;
+	if (pm_rsts & PM_RSTS_HADWR_SET)
+		return RESET_WDG;
+	if (pm_rsts & PM_RSTS_HADSR_SET)
+		return RESET_RST;
+
+	return RESET_UKWN;
 }
 
 static u32 rpi_boot_mode, rpi_boot_part;
@@ -229,31 +259,22 @@ static void rpi_vc_fdt_parse(void *fdt)
 {
 	int ret;
 	struct device_node *root, *chosen, *bootloader;
-	enum reset_src_type rst_src = RESET_UKWN;
 	char *str;
-	bool is_dyn;
-	u32 pm_rsts;
 
 	root = of_unflatten_dtb(fdt, INT_MAX);
 	if (IS_ERR(root))
 		return;
 
-	str = of_read_vc_string(root, "serial-number", &is_dyn);
+	str = of_read_vc_string(root, "serial-number");
 	if (str) {
 		barebox_set_serial_number(str);
-		if (is_dyn)
-			free(str);
-	} else {
-		pr_warn("no 'serial-number' property found in vc fdt root\n");
+		free(str);
 	}
 
-	str = of_read_vc_string(root, "model", &is_dyn);
+	str = of_read_vc_string(root, "model");
 	if (str) {
 		barebox_set_model(str);
-		if (is_dyn)
-			free(str);
-	} else {
-		pr_warn("no 'model' property found in vc fdt root\n");
+		free(str);
 	}
 
 	chosen = of_find_node_by_path_from(root, "/chosen");
@@ -264,23 +285,23 @@ static void rpi_vc_fdt_parse(void *fdt)
 
 	bootloader = of_find_node_by_name(chosen, "bootloader");
 
-	str = of_read_vc_string(chosen, "bootargs", NULL);
-	if (str)
+	str = of_read_vc_string(chosen, "bootargs");
+	if (str) {
 		globalvar_add_simple("vc.bootargs", str);
-	else
-		pr_warn("no 'bootargs' property found in vc fdt '/chosen'\n");
+		free(str);
+	}
 
-	str = of_read_vc_string(chosen, "overlay_prefix", NULL);
-	if (str)
+	str = of_read_vc_string(chosen, "overlay_prefix");
+	if (str) {
 		globalvar_add_simple("vc.overlay_prefix", str);
-	else
-		pr_warn("no 'overlay_prefix' property found in vc fdt '/chosen'\n");
+		free(str);
+	}
 
-	str = of_read_vc_string(chosen, "os_prefix", NULL);
-	if (str)
+	str = of_read_vc_string(chosen, "os_prefix");
+	if (str) {
 		globalvar_add_simple("vc.os_prefix", str);
-	else
-		pr_warn("no 'os_prefix' property found in vc fdt '/chosen'\n");
+		free(str);
+	}
 
 	ret = of_property_read_u32(chosen, "boot-mode", &rpi_boot_mode);
 	if (ret && bootloader)
@@ -300,28 +321,9 @@ static void rpi_vc_fdt_parse(void *fdt)
 	else
 		globalvar_add_simple_int("vc.boot_partition", &rpi_boot_part, "%u");
 
-	if (IS_ENABLED(CONFIG_RESET_SOURCE)) {
-		ret = of_property_read_u32(chosen, "pm_rsts", &pm_rsts);
-		if (ret && bootloader)
-			 ret = of_property_read_u32(bootloader, "rsts", &pm_rsts);
+	if (IS_ENABLED(CONFIG_RESET_SOURCE))
+		reset_source_set(rpi_decode_pm_rsts(chosen, bootloader));
 
-		if (ret) {
-			pr_warn("pm_rsts could not be get from vc fdt\n");
-		} else {
-/*
- * https://github.com/raspberrypi/linux/issues/932#issuecomment-93989581
- */
-			if (pm_rsts & PM_RSTS_HADPOR_SET)
-				rst_src = RESET_POR;
-			else if (pm_rsts & PM_RSTS_HADDR_SET)
-				rst_src = RESET_JTAG;
-			else if (pm_rsts & PM_RSTS_HADWR_SET)
-				rst_src = RESET_WDG;
-			else if (pm_rsts & PM_RSTS_HADSR_SET)
-				rst_src = RESET_RST;
-			reset_source_set(rst_src);
-		}
-	}
 out:
 	if (root)
 		of_delete_node(root);
-- 
2.17.1




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

* Re: [PATCH] ARM: rpi: cleanup parsing of vc fdt
  2022-06-21 20:02 [PATCH] ARM: rpi: cleanup parsing of vc fdt Daniel Brát
@ 2022-06-23 12:10 ` Ahmad Fatoum
  2022-06-27  6:54 ` Sascha Hauer
  1 sibling, 0 replies; 3+ messages in thread
From: Ahmad Fatoum @ 2022-06-23 12:10 UTC (permalink / raw)
  To: Daniel Brát, barebox

On 21.06.22 22:02, Daniel Brát wrote:
> Refactoring and cleanup of vc fdt parsing based on advice
> from Ahmad Fatoum.
> 
> Signed-off-by: Daniel Brát <danek.brat@gmail.com>

Reviewed-by: Ahmad Fatoum <a.fatoum@pengutronix.de>

> ---
> I originally wanted to send these as part of v5 of
> 'ARM: rpi: parse useful data from vc fdt' patch, but you
> were quicker with accepting the v4 :)
> Hope that's not a problem.

Original patch is in next, so this should be squashable.

> 
>  arch/arm/boards/raspberry-pi/rpi-common.c | 114 +++++++++++-----------
>  1 file changed, 58 insertions(+), 56 deletions(-)
> 
> diff --git a/arch/arm/boards/raspberry-pi/rpi-common.c b/arch/arm/boards/raspberry-pi/rpi-common.c
> index eaca4edef..77935e5c8 100644
> --- a/arch/arm/boards/raspberry-pi/rpi-common.c
> +++ b/arch/arm/boards/raspberry-pi/rpi-common.c
> @@ -202,22 +202,52 @@ static int rpi_env_init(void)
>  }
>  
>  /* Some string properties in fdt passed to us from vc may be
> - * malformed by not being null terminated. In that case, create and
> - * return a null terminated copy.
> + * malformed by not being null terminated, so just create and
> + * return a fixed copy.
>   */
>  static char *of_read_vc_string(struct device_node *node,
> -				     const char *prop_name,
> -				     bool *is_dyn)
> +			       const char *prop_name)
>  {
>  	int len;
> -	char *str;
> +	const char *str;
>  
> -	str = (char *)of_get_property(node, prop_name, &len);
> -	if (!str || len <= 0)
> +	str = of_get_property(node, prop_name, &len);
> +	if (!str) {
> +		pr_warn("no property '%s' found in vc fdt's '%s' node\n",
> +			prop_name, node->full_name);
>  		return NULL;
> -	if (is_dyn)
> -		*is_dyn = str[len - 1] != '\0';
> -	return str[len - 1] == '\0' ? str : xstrndup(str, len);
> +	}
> +	return xstrndup(str, len);
> +}
> +
> +static enum reset_src_type rpi_decode_pm_rsts(struct device_node *chosen,
> +					      struct device_node *bootloader)
> +{
> +	u32 pm_rsts;
> +	int ret;
> +
> +	ret = of_property_read_u32(chosen, "pm_rsts", &pm_rsts);
> +	if (ret && bootloader)
> +		 ret = of_property_read_u32(bootloader, "rsts", &pm_rsts);
> +
> +	if (ret) {
> +		pr_warn("'pm_rsts' value not found in vc fdt\n");
> +		return RESET_UKWN;
> +	}
> +	/*
> +	 * https://github.com/raspberrypi/linux/issues/932#issuecomment-93989581
> +	 */
> +
> +	if (pm_rsts & PM_RSTS_HADPOR_SET)
> +		return RESET_POR;
> +	if (pm_rsts & PM_RSTS_HADDR_SET)
> +		return RESET_JTAG;
> +	if (pm_rsts & PM_RSTS_HADWR_SET)
> +		return RESET_WDG;
> +	if (pm_rsts & PM_RSTS_HADSR_SET)
> +		return RESET_RST;
> +
> +	return RESET_UKWN;
>  }
>  
>  static u32 rpi_boot_mode, rpi_boot_part;
> @@ -229,31 +259,22 @@ static void rpi_vc_fdt_parse(void *fdt)
>  {
>  	int ret;
>  	struct device_node *root, *chosen, *bootloader;
> -	enum reset_src_type rst_src = RESET_UKWN;
>  	char *str;
> -	bool is_dyn;
> -	u32 pm_rsts;
>  
>  	root = of_unflatten_dtb(fdt, INT_MAX);
>  	if (IS_ERR(root))
>  		return;
>  
> -	str = of_read_vc_string(root, "serial-number", &is_dyn);
> +	str = of_read_vc_string(root, "serial-number");
>  	if (str) {
>  		barebox_set_serial_number(str);
> -		if (is_dyn)
> -			free(str);
> -	} else {
> -		pr_warn("no 'serial-number' property found in vc fdt root\n");
> +		free(str);
>  	}
>  
> -	str = of_read_vc_string(root, "model", &is_dyn);
> +	str = of_read_vc_string(root, "model");
>  	if (str) {
>  		barebox_set_model(str);
> -		if (is_dyn)
> -			free(str);
> -	} else {
> -		pr_warn("no 'model' property found in vc fdt root\n");
> +		free(str);
>  	}
>  
>  	chosen = of_find_node_by_path_from(root, "/chosen");
> @@ -264,23 +285,23 @@ static void rpi_vc_fdt_parse(void *fdt)
>  
>  	bootloader = of_find_node_by_name(chosen, "bootloader");
>  
> -	str = of_read_vc_string(chosen, "bootargs", NULL);
> -	if (str)
> +	str = of_read_vc_string(chosen, "bootargs");
> +	if (str) {
>  		globalvar_add_simple("vc.bootargs", str);
> -	else
> -		pr_warn("no 'bootargs' property found in vc fdt '/chosen'\n");
> +		free(str);
> +	}
>  
> -	str = of_read_vc_string(chosen, "overlay_prefix", NULL);
> -	if (str)
> +	str = of_read_vc_string(chosen, "overlay_prefix");
> +	if (str) {
>  		globalvar_add_simple("vc.overlay_prefix", str);
> -	else
> -		pr_warn("no 'overlay_prefix' property found in vc fdt '/chosen'\n");
> +		free(str);
> +	}
>  
> -	str = of_read_vc_string(chosen, "os_prefix", NULL);
> -	if (str)
> +	str = of_read_vc_string(chosen, "os_prefix");
> +	if (str) {
>  		globalvar_add_simple("vc.os_prefix", str);
> -	else
> -		pr_warn("no 'os_prefix' property found in vc fdt '/chosen'\n");
> +		free(str);
> +	}
>  
>  	ret = of_property_read_u32(chosen, "boot-mode", &rpi_boot_mode);
>  	if (ret && bootloader)
> @@ -300,28 +321,9 @@ static void rpi_vc_fdt_parse(void *fdt)
>  	else
>  		globalvar_add_simple_int("vc.boot_partition", &rpi_boot_part, "%u");
>  
> -	if (IS_ENABLED(CONFIG_RESET_SOURCE)) {
> -		ret = of_property_read_u32(chosen, "pm_rsts", &pm_rsts);
> -		if (ret && bootloader)
> -			 ret = of_property_read_u32(bootloader, "rsts", &pm_rsts);
> +	if (IS_ENABLED(CONFIG_RESET_SOURCE))
> +		reset_source_set(rpi_decode_pm_rsts(chosen, bootloader));
>  
> -		if (ret) {
> -			pr_warn("pm_rsts could not be get from vc fdt\n");
> -		} else {
> -/*
> - * https://github.com/raspberrypi/linux/issues/932#issuecomment-93989581
> - */
> -			if (pm_rsts & PM_RSTS_HADPOR_SET)
> -				rst_src = RESET_POR;
> -			else if (pm_rsts & PM_RSTS_HADDR_SET)
> -				rst_src = RESET_JTAG;
> -			else if (pm_rsts & PM_RSTS_HADWR_SET)
> -				rst_src = RESET_WDG;
> -			else if (pm_rsts & PM_RSTS_HADSR_SET)
> -				rst_src = RESET_RST;
> -			reset_source_set(rst_src);
> -		}
> -	}
>  out:
>  	if (root)
>  		of_delete_node(root);


-- 
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 |



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

* Re: [PATCH] ARM: rpi: cleanup parsing of vc fdt
  2022-06-21 20:02 [PATCH] ARM: rpi: cleanup parsing of vc fdt Daniel Brát
  2022-06-23 12:10 ` Ahmad Fatoum
@ 2022-06-27  6:54 ` Sascha Hauer
  1 sibling, 0 replies; 3+ messages in thread
From: Sascha Hauer @ 2022-06-27  6:54 UTC (permalink / raw)
  To: Daniel Brát; +Cc: barebox

On Tue, Jun 21, 2022 at 10:02:01PM +0200, Daniel Brát wrote:
> Refactoring and cleanup of vc fdt parsing based on advice
> from Ahmad Fatoum.
> 
> Signed-off-by: Daniel Brát <danek.brat@gmail.com>
> ---
> I originally wanted to send these as part of v5 of
> 'ARM: rpi: parse useful data from vc fdt' patch, but you
> were quicker with accepting the v4 :)
> Hope that's not a problem.

No problem. Squashed into that patch, thanks

Sascha

> 
>  arch/arm/boards/raspberry-pi/rpi-common.c | 114 +++++++++++-----------
>  1 file changed, 58 insertions(+), 56 deletions(-)
> 
> diff --git a/arch/arm/boards/raspberry-pi/rpi-common.c b/arch/arm/boards/raspberry-pi/rpi-common.c
> index eaca4edef..77935e5c8 100644
> --- a/arch/arm/boards/raspberry-pi/rpi-common.c
> +++ b/arch/arm/boards/raspberry-pi/rpi-common.c
> @@ -202,22 +202,52 @@ static int rpi_env_init(void)
>  }
>  
>  /* Some string properties in fdt passed to us from vc may be
> - * malformed by not being null terminated. In that case, create and
> - * return a null terminated copy.
> + * malformed by not being null terminated, so just create and
> + * return a fixed copy.
>   */
>  static char *of_read_vc_string(struct device_node *node,
> -				     const char *prop_name,
> -				     bool *is_dyn)
> +			       const char *prop_name)
>  {
>  	int len;
> -	char *str;
> +	const char *str;
>  
> -	str = (char *)of_get_property(node, prop_name, &len);
> -	if (!str || len <= 0)
> +	str = of_get_property(node, prop_name, &len);
> +	if (!str) {
> +		pr_warn("no property '%s' found in vc fdt's '%s' node\n",
> +			prop_name, node->full_name);
>  		return NULL;
> -	if (is_dyn)
> -		*is_dyn = str[len - 1] != '\0';
> -	return str[len - 1] == '\0' ? str : xstrndup(str, len);
> +	}
> +	return xstrndup(str, len);
> +}
> +
> +static enum reset_src_type rpi_decode_pm_rsts(struct device_node *chosen,
> +					      struct device_node *bootloader)
> +{
> +	u32 pm_rsts;
> +	int ret;
> +
> +	ret = of_property_read_u32(chosen, "pm_rsts", &pm_rsts);
> +	if (ret && bootloader)
> +		 ret = of_property_read_u32(bootloader, "rsts", &pm_rsts);
> +
> +	if (ret) {
> +		pr_warn("'pm_rsts' value not found in vc fdt\n");
> +		return RESET_UKWN;
> +	}
> +	/*
> +	 * https://github.com/raspberrypi/linux/issues/932#issuecomment-93989581
> +	 */
> +
> +	if (pm_rsts & PM_RSTS_HADPOR_SET)
> +		return RESET_POR;
> +	if (pm_rsts & PM_RSTS_HADDR_SET)
> +		return RESET_JTAG;
> +	if (pm_rsts & PM_RSTS_HADWR_SET)
> +		return RESET_WDG;
> +	if (pm_rsts & PM_RSTS_HADSR_SET)
> +		return RESET_RST;
> +
> +	return RESET_UKWN;
>  }
>  
>  static u32 rpi_boot_mode, rpi_boot_part;
> @@ -229,31 +259,22 @@ static void rpi_vc_fdt_parse(void *fdt)
>  {
>  	int ret;
>  	struct device_node *root, *chosen, *bootloader;
> -	enum reset_src_type rst_src = RESET_UKWN;
>  	char *str;
> -	bool is_dyn;
> -	u32 pm_rsts;
>  
>  	root = of_unflatten_dtb(fdt, INT_MAX);
>  	if (IS_ERR(root))
>  		return;
>  
> -	str = of_read_vc_string(root, "serial-number", &is_dyn);
> +	str = of_read_vc_string(root, "serial-number");
>  	if (str) {
>  		barebox_set_serial_number(str);
> -		if (is_dyn)
> -			free(str);
> -	} else {
> -		pr_warn("no 'serial-number' property found in vc fdt root\n");
> +		free(str);
>  	}
>  
> -	str = of_read_vc_string(root, "model", &is_dyn);
> +	str = of_read_vc_string(root, "model");
>  	if (str) {
>  		barebox_set_model(str);
> -		if (is_dyn)
> -			free(str);
> -	} else {
> -		pr_warn("no 'model' property found in vc fdt root\n");
> +		free(str);
>  	}
>  
>  	chosen = of_find_node_by_path_from(root, "/chosen");
> @@ -264,23 +285,23 @@ static void rpi_vc_fdt_parse(void *fdt)
>  
>  	bootloader = of_find_node_by_name(chosen, "bootloader");
>  
> -	str = of_read_vc_string(chosen, "bootargs", NULL);
> -	if (str)
> +	str = of_read_vc_string(chosen, "bootargs");
> +	if (str) {
>  		globalvar_add_simple("vc.bootargs", str);
> -	else
> -		pr_warn("no 'bootargs' property found in vc fdt '/chosen'\n");
> +		free(str);
> +	}
>  
> -	str = of_read_vc_string(chosen, "overlay_prefix", NULL);
> -	if (str)
> +	str = of_read_vc_string(chosen, "overlay_prefix");
> +	if (str) {
>  		globalvar_add_simple("vc.overlay_prefix", str);
> -	else
> -		pr_warn("no 'overlay_prefix' property found in vc fdt '/chosen'\n");
> +		free(str);
> +	}
>  
> -	str = of_read_vc_string(chosen, "os_prefix", NULL);
> -	if (str)
> +	str = of_read_vc_string(chosen, "os_prefix");
> +	if (str) {
>  		globalvar_add_simple("vc.os_prefix", str);
> -	else
> -		pr_warn("no 'os_prefix' property found in vc fdt '/chosen'\n");
> +		free(str);
> +	}
>  
>  	ret = of_property_read_u32(chosen, "boot-mode", &rpi_boot_mode);
>  	if (ret && bootloader)
> @@ -300,28 +321,9 @@ static void rpi_vc_fdt_parse(void *fdt)
>  	else
>  		globalvar_add_simple_int("vc.boot_partition", &rpi_boot_part, "%u");
>  
> -	if (IS_ENABLED(CONFIG_RESET_SOURCE)) {
> -		ret = of_property_read_u32(chosen, "pm_rsts", &pm_rsts);
> -		if (ret && bootloader)
> -			 ret = of_property_read_u32(bootloader, "rsts", &pm_rsts);
> +	if (IS_ENABLED(CONFIG_RESET_SOURCE))
> +		reset_source_set(rpi_decode_pm_rsts(chosen, bootloader));
>  
> -		if (ret) {
> -			pr_warn("pm_rsts could not be get from vc fdt\n");
> -		} else {
> -/*
> - * https://github.com/raspberrypi/linux/issues/932#issuecomment-93989581
> - */
> -			if (pm_rsts & PM_RSTS_HADPOR_SET)
> -				rst_src = RESET_POR;
> -			else if (pm_rsts & PM_RSTS_HADDR_SET)
> -				rst_src = RESET_JTAG;
> -			else if (pm_rsts & PM_RSTS_HADWR_SET)
> -				rst_src = RESET_WDG;
> -			else if (pm_rsts & PM_RSTS_HADSR_SET)
> -				rst_src = RESET_RST;
> -			reset_source_set(rst_src);
> -		}
> -	}
>  out:
>  	if (root)
>  		of_delete_node(root);
> -- 
> 2.17.1
> 
> 
> 

-- 
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 |



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

end of thread, other threads:[~2022-06-27  6:57 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-21 20:02 [PATCH] ARM: rpi: cleanup parsing of vc fdt Daniel Brát
2022-06-23 12:10 ` Ahmad Fatoum
2022-06-27  6:54 ` Sascha Hauer

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