diff mbox series

[02/12] fdt_support: add fdt_copy_fixed_partitions function

Message ID 20230608171614.2.Idd521a274f1b0524963a501324a1702a5a8b52c1@changeid
State Accepted
Commit 163c5f60ebb492eb3bab75e299cb43e6d022653d
Delegated to: Patrice Chotard
Headers show
Series stm32mp1: use U-Boot device tree to configure MTD partitions | expand

Commit Message

Patrick DELAUNAY June 8, 2023, 3:16 p.m. UTC
Add a new function fdt_copy_fixed_partitions to copy the fixed
partition nodes from U-Boot device tree to Linux kernel
device tree and to dynamically configure the MTD partitions.

This function fdt_copy_fixed_partitions is only based on device tree
with livetree compatible function and replace the function
fdt_fixup_mtdparts based on mtdparts variable.

Signed-off-by: Patrick Delaunay <patrick.delaunay@foss.st.com>
---

 common/fdt_support.c  | 73 +++++++++++++++++++++++++++++++++++++++++++
 include/fdt_support.h |  8 +++++
 2 files changed, 81 insertions(+)

Comments

Patrice CHOTARD June 13, 2023, 6:26 a.m. UTC | #1
On 6/8/23 17:16, Patrick Delaunay wrote:
> Add a new function fdt_copy_fixed_partitions to copy the fixed
> partition nodes from U-Boot device tree to Linux kernel
> device tree and to dynamically configure the MTD partitions.
> 
> This function fdt_copy_fixed_partitions is only based on device tree
> with livetree compatible function and replace the function
> fdt_fixup_mtdparts based on mtdparts variable.
> 
> Signed-off-by: Patrick Delaunay <patrick.delaunay@foss.st.com>
> ---
> 
>  common/fdt_support.c  | 73 +++++++++++++++++++++++++++++++++++++++++++
>  include/fdt_support.h |  8 +++++
>  2 files changed, 81 insertions(+)
> 
> diff --git a/common/fdt_support.c b/common/fdt_support.c
> index ffc59fd8b36a..5e49078f8c35 100644
> --- a/common/fdt_support.c
> +++ b/common/fdt_support.c
> @@ -1051,6 +1051,79 @@ void fdt_fixup_mtdparts(void *blob, const struct node_info *node_info,
>  }
>  #endif
>  
> +int fdt_copy_fixed_partitions(void *blob)
> +{
> +	ofnode node, subnode;
> +	int off, suboff, res;
> +	char path[256];
> +	int address_cells, size_cells;
> +	u8 i, j, child_count;
> +
> +	node = ofnode_by_compatible(ofnode_null(), "fixed-partitions");
> +	while (ofnode_valid(node)) {
> +		/* copy the U-Boot fixed partition */
> +		address_cells = ofnode_read_simple_addr_cells(node);
> +		size_cells = ofnode_read_simple_size_cells(node);
> +
> +		res = ofnode_get_path(ofnode_get_parent(node), path, sizeof(path));
> +		if (res)
> +			return res;
> +
> +		off = fdt_path_offset(blob, path);
> +		if (off < 0)
> +			return -ENODEV;
> +
> +		off = fdt_find_or_add_subnode(blob, off, "partitions");
> +		res = fdt_setprop_string(blob, off, "compatible", "fixed-partitions");
> +		if (res)
> +			return res;
> +
> +		res = fdt_setprop_u32(blob, off, "#address-cells", address_cells);
> +		if (res)
> +			return res;
> +
> +		res = fdt_setprop_u32(blob, off, "#size-cells", size_cells);
> +		if (res)
> +			return res;
> +
> +		/*
> +		 * parse partition in reverse order as fdt_find_or_add_subnode() only
> +		 * insert the new node after the parent's properties
> +		 */
> +		child_count = ofnode_get_child_count(node);
> +		for (i = child_count; i > 0 ; i--) {
> +			subnode = ofnode_first_subnode(node);
> +			if (!ofnode_valid(subnode))
> +				break;
> +
> +			for (j = 0; (j < i - 1); j++)
> +				subnode = ofnode_next_subnode(subnode);
> +
> +			if (!ofnode_valid(subnode))
> +				break;
> +
> +			const u32 *reg;
> +			int len;
> +
> +			suboff = fdt_find_or_add_subnode(blob, off, ofnode_get_name(subnode));
> +			res = fdt_setprop_string(blob, suboff, "label",
> +						 ofnode_read_string(subnode, "label"));
> +			if (res)
> +				return res;
> +
> +			reg = ofnode_get_property(subnode, "reg", &len);
> +			res = fdt_setprop(blob, suboff, "reg", reg, len);
> +			if (res)
> +				return res;
> +		}
> +
> +		/* go to next fixed-partitions node */
> +		node = ofnode_by_compatible(node, "fixed-partitions");
> +	}
> +
> +	return 0;
> +}
> +
>  void fdt_del_node_and_alias(void *blob, const char *alias)
>  {
>  	int off = fdt_path_offset(blob, alias);
> diff --git a/include/fdt_support.h b/include/fdt_support.h
> index eeb83e6251d3..2cd836689821 100644
> --- a/include/fdt_support.h
> +++ b/include/fdt_support.h
> @@ -256,6 +256,14 @@ static inline void fdt_fixup_mtdparts(void *fdt,
>  }
>  #endif
>  
> +/**
> + * copy the fixed-partition nodes from U-Boot device tree to external blob
> + *
> + * @param blob		FDT blob to update
> + * Return: 0 if ok, or non-zero on error
> + */
> +int fdt_copy_fixed_partitions(void *blob);
> +
>  void fdt_del_node_and_alias(void *blob, const char *alias);
>  
>  /**
Reviewed-by: Patrice Chotard <patrice.chotard@foss.st.com>

Thanks
Patrice
Simon Glass June 15, 2023, 9:14 a.m. UTC | #2
Hi Patrick,

On Thu, 8 Jun 2023 at 16:16, Patrick Delaunay
<patrick.delaunay@foss.st.com> wrote:
>
> Add a new function fdt_copy_fixed_partitions to copy the fixed
> partition nodes from U-Boot device tree to Linux kernel
> device tree and to dynamically configure the MTD partitions.
>
> This function fdt_copy_fixed_partitions is only based on device tree
> with livetree compatible function and replace the function
> fdt_fixup_mtdparts based on mtdparts variable.
>
> Signed-off-by: Patrick Delaunay <patrick.delaunay@foss.st.com>
> ---
>
>  common/fdt_support.c  | 73 +++++++++++++++++++++++++++++++++++++++++++
>  include/fdt_support.h |  8 +++++
>  2 files changed, 81 insertions(+)
>
> diff --git a/common/fdt_support.c b/common/fdt_support.c
> index ffc59fd8b36a..5e49078f8c35 100644
> --- a/common/fdt_support.c
> +++ b/common/fdt_support.c
> @@ -1051,6 +1051,79 @@ void fdt_fixup_mtdparts(void *blob, const struct node_info *node_info,
>  }
>  #endif
>
> +int fdt_copy_fixed_partitions(void *blob)
> +{
> +       ofnode node, subnode;
> +       int off, suboff, res;
> +       char path[256];
> +       int address_cells, size_cells;
> +       u8 i, j, child_count;
> +
> +       node = ofnode_by_compatible(ofnode_null(), "fixed-partitions");
> +       while (ofnode_valid(node)) {
> +               /* copy the U-Boot fixed partition */
> +               address_cells = ofnode_read_simple_addr_cells(node);
> +               size_cells = ofnode_read_simple_size_cells(node);
> +
> +               res = ofnode_get_path(ofnode_get_parent(node), path, sizeof(path));
> +               if (res)
> +                       return res;
> +
> +               off = fdt_path_offset(blob, path);
> +               if (off < 0)
> +                       return -ENODEV;

It should be possible to use livetree to write to the blob. E.g.:

oftree tree = oftree_from_fdt(blob);
ofnode node = oftree_path(tree, "/...");

That would be more future-proof than using this API. I'd like to move
DT fixup to the ofnode API eventually.

> +
> +               off = fdt_find_or_add_subnode(blob, off, "partitions");
> +               res = fdt_setprop_string(blob, off, "compatible", "fixed-partitions");
> +               if (res)
> +                       return res;
> +
> +               res = fdt_setprop_u32(blob, off, "#address-cells", address_cells);
> +               if (res)
> +                       return res;
> +
> +               res = fdt_setprop_u32(blob, off, "#size-cells", size_cells);
> +               if (res)
> +                       return res;
> +
> +               /*
> +                * parse partition in reverse order as fdt_find_or_add_subnode() only
> +                * insert the new node after the parent's properties
> +                */
> +               child_count = ofnode_get_child_count(node);
> +               for (i = child_count; i > 0 ; i--) {
> +                       subnode = ofnode_first_subnode(node);
> +                       if (!ofnode_valid(subnode))
> +                               break;
> +
> +                       for (j = 0; (j < i - 1); j++)
> +                               subnode = ofnode_next_subnode(subnode);
> +
> +                       if (!ofnode_valid(subnode))
> +                               break;
> +
> +                       const u32 *reg;
> +                       int len;
> +
> +                       suboff = fdt_find_or_add_subnode(blob, off, ofnode_get_name(subnode));
> +                       res = fdt_setprop_string(blob, suboff, "label",
> +                                                ofnode_read_string(subnode, "label"));
> +                       if (res)
> +                               return res;
> +
> +                       reg = ofnode_get_property(subnode, "reg", &len);
> +                       res = fdt_setprop(blob, suboff, "reg", reg, len);
> +                       if (res)
> +                               return res;
> +               }
> +
> +               /* go to next fixed-partitions node */
> +               node = ofnode_by_compatible(node, "fixed-partitions");
> +       }
> +
> +       return 0;
> +}
> +
>  void fdt_del_node_and_alias(void *blob, const char *alias)
>  {
>         int off = fdt_path_offset(blob, alias);
> diff --git a/include/fdt_support.h b/include/fdt_support.h
> index eeb83e6251d3..2cd836689821 100644
> --- a/include/fdt_support.h
> +++ b/include/fdt_support.h
> @@ -256,6 +256,14 @@ static inline void fdt_fixup_mtdparts(void *fdt,
>  }
>  #endif
>
> +/**
> + * copy the fixed-partition nodes from U-Boot device tree to external blob
> + *
> + * @param blob         FDT blob to update
> + * Return: 0 if ok, or non-zero on error
> + */
> +int fdt_copy_fixed_partitions(void *blob);
> +
>  void fdt_del_node_and_alias(void *blob, const char *alias);
>
>  /**
> --
> 2.25.1
>

Regards,
Simon
diff mbox series

Patch

diff --git a/common/fdt_support.c b/common/fdt_support.c
index ffc59fd8b36a..5e49078f8c35 100644
--- a/common/fdt_support.c
+++ b/common/fdt_support.c
@@ -1051,6 +1051,79 @@  void fdt_fixup_mtdparts(void *blob, const struct node_info *node_info,
 }
 #endif
 
+int fdt_copy_fixed_partitions(void *blob)
+{
+	ofnode node, subnode;
+	int off, suboff, res;
+	char path[256];
+	int address_cells, size_cells;
+	u8 i, j, child_count;
+
+	node = ofnode_by_compatible(ofnode_null(), "fixed-partitions");
+	while (ofnode_valid(node)) {
+		/* copy the U-Boot fixed partition */
+		address_cells = ofnode_read_simple_addr_cells(node);
+		size_cells = ofnode_read_simple_size_cells(node);
+
+		res = ofnode_get_path(ofnode_get_parent(node), path, sizeof(path));
+		if (res)
+			return res;
+
+		off = fdt_path_offset(blob, path);
+		if (off < 0)
+			return -ENODEV;
+
+		off = fdt_find_or_add_subnode(blob, off, "partitions");
+		res = fdt_setprop_string(blob, off, "compatible", "fixed-partitions");
+		if (res)
+			return res;
+
+		res = fdt_setprop_u32(blob, off, "#address-cells", address_cells);
+		if (res)
+			return res;
+
+		res = fdt_setprop_u32(blob, off, "#size-cells", size_cells);
+		if (res)
+			return res;
+
+		/*
+		 * parse partition in reverse order as fdt_find_or_add_subnode() only
+		 * insert the new node after the parent's properties
+		 */
+		child_count = ofnode_get_child_count(node);
+		for (i = child_count; i > 0 ; i--) {
+			subnode = ofnode_first_subnode(node);
+			if (!ofnode_valid(subnode))
+				break;
+
+			for (j = 0; (j < i - 1); j++)
+				subnode = ofnode_next_subnode(subnode);
+
+			if (!ofnode_valid(subnode))
+				break;
+
+			const u32 *reg;
+			int len;
+
+			suboff = fdt_find_or_add_subnode(blob, off, ofnode_get_name(subnode));
+			res = fdt_setprop_string(blob, suboff, "label",
+						 ofnode_read_string(subnode, "label"));
+			if (res)
+				return res;
+
+			reg = ofnode_get_property(subnode, "reg", &len);
+			res = fdt_setprop(blob, suboff, "reg", reg, len);
+			if (res)
+				return res;
+		}
+
+		/* go to next fixed-partitions node */
+		node = ofnode_by_compatible(node, "fixed-partitions");
+	}
+
+	return 0;
+}
+
 void fdt_del_node_and_alias(void *blob, const char *alias)
 {
 	int off = fdt_path_offset(blob, alias);
diff --git a/include/fdt_support.h b/include/fdt_support.h
index eeb83e6251d3..2cd836689821 100644
--- a/include/fdt_support.h
+++ b/include/fdt_support.h
@@ -256,6 +256,14 @@  static inline void fdt_fixup_mtdparts(void *fdt,
 }
 #endif
 
+/**
+ * copy the fixed-partition nodes from U-Boot device tree to external blob
+ *
+ * @param blob		FDT blob to update
+ * Return: 0 if ok, or non-zero on error
+ */
+int fdt_copy_fixed_partitions(void *blob);
+
 void fdt_del_node_and_alias(void *blob, const char *alias);
 
 /**