Skip to contentSkip to conversation

kexec: pass initrd position in dtb

PATCH Applied Wang Nan 3 participants 6 messages Mar 25, 2014 – Mar 28, 2014

Subsystems (2)
lore
Share & tools
lore

Wang Nan

author
#0 lore raw

cover kexec: pass initrd position in dtb

The main goal of this patch series is to pass initrd position to secondary kernel. To makes code clear, patch 2/3 introduce a new function to handle fdt related operations.

Without these two patches, kexec failed to work on arm.

Change from v1:

- Remove #include "libfdt_internal.h" since it is for internal use for libfdt.

Change from v2:

- Split into two logically independent patches.
- Compute memory consumption more accurate.

Change from v3:

- Introduces helpers in libfdt for memory usage computation.

Wang Nan (3):
kexec: introduce helpers for computing dtb size
kexec: introduce setup_dtb_prop to make code clear
kexec: pass initrd position by dtb

 kexec/arch/arm/kexec-zImage-arm.c | 106 +++++++++++++++++++++++++++++---------
 kexec/libfdt/libfdt.h             |  24 +++++++++
 kexec/libfdt/libfdt_internal.h    |   3 --
 3 files changed, 105 insertions(+), 28 deletions(-)
Signature · 7 lines
1.8.4


_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
author
#1 lore raw

PATCH 1/3 kexec: introduce helpers for computing dtb size

This patch introduce fdt_node_len and fdt_prop_len to help for computing memory requirement when operating dtb.

Cc Simon Horman <[email protected]>
Cc Dave Young <[email protected]>
Cc Geng Hui <[email protected]>
---
 kexec/libfdt/libfdt.h          | 24 ++++++++++++++++++++++++
 kexec/libfdt/libfdt_internal.h |  3 ---
 2 files changed, 24 insertions(+), 3 deletions(-)
kexec/libfdt/libfdt.h +24 -0
index ce80e4f..87a24ab 100644
@@ -1073,4 +1073,28 @@ int fdt_del_node(void *fdt, int nodeoffset);
 
 const char *fdt_strerror(int errval);
 
+#define FDT_ALIGN(x, a)		(((x) + (a) - 1) & ~((a) - 1))
+#define FDT_TAGALIGN(x)		(FDT_ALIGN((x), FDT_TAGSIZE))
+
+/*
+ * if add a new subnode:
+ * see: fdt_add_subnode -> fdt_add_subnode_namelen
+ */
+static inline int fdt_node_len(const char* node_name)
+{
+	return sizeof(struct fdt_node_header) +
+		FDT_TAGALIGN(strlen(node_name) + 1) + FDT_TAGSIZE;
+}
+
+/*
+ * if add a new prop: (assume prop_name not exist in strtab)
+ * see: fdt_setprop -> _fdt_add_property
+ */
+static inline int fdt_prop_len(const char* prop_name, int len)
+{
+	return (strlen(prop_name) + 1) +
+		sizeof(struct fdt_property) +
+		FDT_TAGALIGN(len);
+}
+
 #endif /* _LIBFDT_H */
kexec/libfdt/libfdt_internal.h +0 -3
index 46eb93e..7e6c4c8 100644
@@ -52,9 +52,6 @@
  */
 #include <fdt.h>
 
-#define FDT_ALIGN(x, a)		(((x) + (a) - 1) & ~((a) - 1))
-#define FDT_TAGALIGN(x)		(FDT_ALIGN((x), FDT_TAGSIZE))
-
 #define FDT_CHECK_HEADER(fdt) \
 	{ \
 		int err; \
Signature · 7 lines
1.8.4


_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
author
#2 lore raw

PATCH 2/3 kexec: introduce setup_dtb_prop to make code clear

In reply to Wang Nan

This patch introduces setup_dtb_prop(), which is used for dtb operations. The code is extracted from zImage_arm_load, and this patch makes memory grown computation more accurate.

Cc Simon Horman <[email protected]>
Cc Dave Young <[email protected]>
Cc Geng Hui <[email protected]>
---
 kexec/arch/arm/kexec-zImage-arm.c | 93 ++++++++++++++++++++++++++++-----------
 1 file changed, 68 insertions(+), 25 deletions(-)
kexec/arch/arm/kexec-zImage-arm.c +68 -25
index 8a35018..aea1278 100644
@@ -216,6 +216,67 @@ int atag_arm_load(struct kexec_info *info, unsigned long base,
 	return 0;
 }
 
+static int setup_dtb_prop(char **bufp, off_t *sizep, const char *node_name,
+		const char *prop_name, const void *val, int len)
+{
+	char *dtb_buf;
+	off_t dtb_size;
+	int off;
+	int prop_len = 0;
+	const struct fdt_property *prop;
+
+	if ((bufp == NULL) || (sizep == NULL) || (*bufp == NULL))
+		die("Internal error\n");
+
+	dtb_buf = *bufp;
+	dtb_size = *sizep;
+
+	/* check if the subnode has already exist */
+	off = fdt_path_offset(dtb_buf, node_name);
+	if (off == -FDT_ERR_NOTFOUND) {
+		dtb_size += fdt_node_len(node_name);
+		fdt_set_totalsize(dtb_buf, dtb_size);
+		dtb_buf = xrealloc(dtb_buf, dtb_size);
+		if (dtb_buf == NULL)
+			die("xrealloc failed\n");
+		off = fdt_add_subnode(dtb_buf, off, node_name);
+	}
+
+	if (off < 0) {
+		fprintf(stderr, "FDT: Error adding %s node.\n", node_name);
+		return -1;
+	}
+
+	prop = fdt_get_property(dtb_buf, off, prop_name, &prop_len);
+	if ((prop == NULL) && (prop_len != -FDT_ERR_NOTFOUND)) {
+		die("FDT: fdt_get_property");
+	} else if (prop == NULL) {
+		/* prop_len == -FDT_ERR_NOTFOUND */
+		/* prop doesn't exist */
+		dtb_size += fdt_prop_len(prop_name, len);
+	} else {
+		if (prop_len < len)
+			dtb_size += len - prop_len;
+	}
+
+	if (fdt_totalsize(dtb_buf) < dtb_size) {
+		fdt_set_totalsize(dtb_buf, dtb_size);
+		dtb_buf = xrealloc(dtb_buf, dtb_size);
+		if (dtb_buf == NULL)
+			die("xrealloc failed\n");
+	}
+
+	if (fdt_setprop(dtb_buf, off, prop_name,
+				val, len) != 0) {
+		fprintf(stderr, "FDT: Error setting %s/%s property.\n",
+				node_name, prop_name);
+		return -1;
+	}
+	*bufp = dtb_buf;
+	*sizep = dtb_size;
+	return 0;
+}
+
 int zImage_arm_load(int argc, char **argv, const char *buf, off_t len,
 	struct kexec_info *info)
 {
@@ -375,32 +436,14 @@ int zImage_arm_load(int argc, char **argv, const char *buf, off_t len,
 			}
 
 			if (command_line) {
-				const char *node_name = "/chosen";
-				const char *prop_name = "bootargs";
-				int off;
-
-				dtb_length = fdt_totalsize(dtb_buf) + 1024 +
-					strlen(command_line);
-				dtb_buf = xrealloc(dtb_buf, dtb_length);
-				fdt_set_totalsize(dtb_buf, dtb_length);
-
-				/* check if a /choosen subnode already exists */
-				off = fdt_path_offset(dtb_buf, node_name);
-
-				if (off == -FDT_ERR_NOTFOUND)
-					off = fdt_add_subnode(dtb_buf, off, node_name);
-
-				if (off < 0) {
-					fprintf(stderr, "FDT: Error adding %s node.\n", node_name);
-					return -1;
-				}
-
-				if (fdt_setprop(dtb_buf, off, prop_name,
-						command_line, strlen(command_line) + 1) != 0) {
-					fprintf(stderr, "FDT: Error setting %s/%s property.\n",
-						node_name, prop_name);
+				/*
+				 *  Error should have been reported so
+				 *  directly return -1
+				 */
+				if (setup_dtb_prop(&dtb_buf, &dtb_length, "/chosen",
+						"bootargs", command_line,
+						strlen(command_line) + 1))
 					return -1;
-				}
 			}
 		} else {
 			/*
Signature · 7 lines
1.8.4


_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
author
#3 lore raw

PATCH 3/3 kexec: pass initrd position by dtb

In reply to Wang Nan

This patch append the position of initrd to dtb when loading arm kernel and initrd without using atag.

Cc Simon Horman <[email protected]>
Cc Dave Young <[email protected]>
Cc Geng Hui <[email protected]>
---
 kexec/arch/arm/kexec-zImage-arm.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
kexec/arch/arm/kexec-zImage-arm.c +13 -0
index aea1278..bfbf290 100644
@@ -460,6 +460,19 @@ int zImage_arm_load(int argc, char **argv, const char *buf, off_t len,
 		if (ramdisk) {
 			add_segment(info, ramdisk_buf, initrd_size,
 			            initrd_base, initrd_size);
+
+			unsigned long start, end;
+			start = cpu_to_be32((unsigned long)(initrd_base));
+			end = cpu_to_be32((unsigned long)(initrd_base + initrd_size));
+
+			if (setup_dtb_prop(&dtb_buf, &dtb_length, "/chosen",
+					"linux,initrd-start", &start,
+					sizeof(start)))
+				return -1;
+			if (setup_dtb_prop(&dtb_buf, &dtb_length, "/chosen",
+					"linux,initrd-end", &end,
+					sizeof(end)))
+				return -1;
 		}
 
 		/* Stick the dtb at the end of the initrd and page
Signature · 7 lines
1.8.4


_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
Acked-by on the v1 series
#4 lore raw
In reply to Wang Nan
Quoted 27 lines · Wang Nan

The main goal of this patch series is to pass initrd position to secondary kernel. To makes code clear, patch 2/3 introduce a new function to handle fdt related operations.

Without these two patches, kexec failed to work on arm.

Change from v1:

- Remove #include "libfdt_internal.h" since it is for internal use for libfdt.

Change from v2:

- Split into two logically independent patches.
- Compute memory consumption more accurate.

Change from v3:

- Introduces helpers in libfdt for memory usage computation.

Wang Nan (3):
kexec: introduce helpers for computing dtb size
kexec: introduce setup_dtb_prop to make code clear
kexec: pass initrd position by dtb

Quoted 5 lines
 kexec/arch/arm/kexec-zImage-arm.c | 106 +++++++++++++++++++++++++++++---------
 kexec/libfdt/libfdt.h             |  24 +++++++++
 kexec/libfdt/libfdt_internal.h    |   3 --
 3 files changed, 105 insertions(+), 28 deletions(-)

Thanks for the update.

Thanks
Dave

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

Applied
#5 lore raw
Quoted 34 lines · Dave Young
Quoted 27 lines · Wang Nan

The main goal of this patch series is to pass initrd position to secondary kernel. To makes code clear, patch 2/3 introduce a new function to handle fdt related operations.

Without these two patches, kexec failed to work on arm.

Change from v1:

- Remove #include "libfdt_internal.h" since it is for internal use for libfdt.

Change from v2:

- Split into two logically independent patches.
- Compute memory consumption more accurate.

Change from v3:

- Introduces helpers in libfdt for memory usage computation.

Wang Nan (3):
kexec: introduce helpers for computing dtb size
kexec: introduce setup_dtb_prop to make code clear
kexec: pass initrd position by dtb

Quoted 5 lines
 kexec/arch/arm/kexec-zImage-arm.c | 106 +++++++++++++++++++++++++++++---------
 kexec/libfdt/libfdt.h             |  24 +++++++++
 kexec/libfdt/libfdt_internal.h    |   3 --
 3 files changed, 105 insertions(+), 28 deletions(-)
Quoted 3 lines

Thanks for the update.

Thanks, applied.

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec