Skip to contentSkip to conversation

kexec: pass initrd position in dtb

PATCH In review Wang Nan 2 participants 4 messages Mar 20, 2014 – Mar 24, 2014

Subsystems (2)
lore
Share & tools
lore

Wang Nan

author
#0 lore raw

v3 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 1/2 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.

Wang Nan (2):
kexec: introduce setup_dtb_prop to make code clear
kexec: pass initrd position by dtb

 kexec/arch/arm/kexec-zImage-arm.c | 109 +++++++++++++++++++++++++++++---------
 1 file changed, 83 insertions(+), 26 deletions(-)
Signature · 7 lines
1.8.3.4


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

PATCH v3 1/2 kexec: introduce setup_dtb_prop to make code clear

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 | 96 ++++++++++++++++++++++++++++-----------
 1 file changed, 70 insertions(+), 26 deletions(-)
kexec/arch/arm/kexec-zImage-arm.c +70 -26
index 8a35018..977254e 100644
@@ -216,6 +216,68 @@ 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;
+	int off;
+
+	if ((bufp == NULL) || (sizep == NULL) || (*bufp == NULL))
+		die("Internal error\n");
+
+	dtb_buf = *bufp;
+
+	/* 
+	 * Potential memory usage grown:
+	 *
+	 * 1. if fdt_add_subnode is called:
+	 *    sizeof(struct fdt_node_header) + FDT_TAGALIGN(nodename+1) + FDT_TAGSIZE
+	 *       // see fdt_add_subnode_namelen()
+	 *
+	 * 2. in fdt_setprop, if _fdt_add_property is called:
+	 *  a) strlen(prop_name) + 1  // see _fdt_find_add_string()
+	 *  b) sizeof(struct fdt_property) + FDT_TAGALIGN(len) 
+	 *        // see _fdt_add_property()
+	 *
+	 * FDT_TAGALIGN is (FDT_ALIGN((x), FDT_TAGSIZE)), which is same
+	 * to _ALIGN(x, FDT_TAGSIZE).
+	 */
+
+	*sizep = fdt_totalsize(*bufp) +
+		/* fdt_add_subnode -> fdt_add_subnode_namelen */
+		sizeof(struct fdt_node_header) +
+		_ALIGN(strlen(node_name) + 1, FDT_TAGSIZE) +
+		FDT_TAGSIZE +
+		/* fdt_setprop -> _fdt_add_property -> _fdt_find_add_string */
+		strlen(prop_name) + 1 +
+		/* fdt_setprop -> _fdt_add_property */
+		sizeof(struct fdt_property) + _ALIGN(len, FDT_TAGSIZE);
+
+	dtb_buf = xrealloc(dtb_buf, *sizep);
+	if (dtb_buf == NULL)
+		die("xrealloc failed\n");
+	*bufp = dtb_buf;
+
+	fdt_set_totalsize(dtb_buf, *sizep);
+
+	/* check if the 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,
+				val, len) != 0) {
+		fprintf(stderr, "FDT: Error setting %s/%s property.\n",
+				node_name, prop_name);
+		return -1;
+	}
+	return 0;
+}
+
 int zImage_arm_load(int argc, char **argv, const char *buf, off_t len,
 	struct kexec_info *info)
 {
@@ -375,32 +437,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);
-					return -1;
-				}
+				/*  
+				 *  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.3.4


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

Re: [PATCH v3 1/2] kexec: introduce setup_dtb_prop to make code clear

Hi, Wang Nan

Rethinking about this patch, I still have several comments.

Quoted 36 lines · 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 | 96 ++++++++++++++++++++++++++++-----------
 1 file changed, 70 insertions(+), 26 deletions(-)
kexec/arch/arm/kexec-zImage-arm.c +16 -0
index 8a35018..977254e 100644
@@ -216,6 +216,68 @@ 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;
+	int off;
+
+	if ((bufp == NULL) || (sizep == NULL) || (*bufp == NULL))
+		die("Internal error\n");
+
+	dtb_buf = *bufp;
+
+	/* 
+	 * Potential memory usage grown:
+	 *
+	 * 1. if fdt_add_subnode is called:
+	 *    sizeof(struct fdt_node_header) + FDT_TAGALIGN(nodename+1) + FDT_TAGSIZE

kexec/arch/arm/kexec-zImage-arm.c int atag_arm_load(struct kexec_info *info, unsigned long base, · line 244

Quoted 10 lines
kexec/arch/arm/kexec-zImage-arm.c +10 -0
+	 *       // see fdt_add_subnode_namelen()
+	 *
+	 * 2. in fdt_setprop, if _fdt_add_property is called:
+	 *  a) strlen(prop_name) + 1  // see _fdt_find_add_string()
+	 *  b) sizeof(struct fdt_property) + FDT_TAGALIGN(len) 
+	 *        // see _fdt_add_property()
+	 *
+	 * FDT_TAGALIGN is (FDT_ALIGN((x), FDT_TAGSIZE)), which is same
+	 * to _ALIGN(x, FDT_TAGSIZE).
+	 */

The comments are not necessary because it's obvious in the code itself. Maybe add two functions in libfdt is better, such as node_len() and prop_len()

kexec/arch/arm/kexec-zImage-arm.c int atag_arm_load(struct kexec_info *info, unsigned long base, · line 247

Quoted 3 lines
kexec/arch/arm/kexec-zImage-arm.c +3 -0
+
+	*sizep = fdt_totalsize(*bufp) +
+		/* fdt_add_subnode -> fdt_add_subnode_namelen */

ditto.

kexec/arch/arm/kexec-zImage-arm.c int atag_arm_load(struct kexec_info *info, unsigned long base, · line 251

Quoted 4 lines
kexec/arch/arm/kexec-zImage-arm.c +4 -0
+		sizeof(struct fdt_node_header) +
+		_ALIGN(strlen(node_name) + 1, FDT_TAGSIZE) +
+		FDT_TAGSIZE +
+		/* fdt_setprop -> _fdt_add_property -> _fdt_find_add_string */

ditto.

kexec/arch/arm/kexec-zImage-arm.c int atag_arm_load(struct kexec_info *info, unsigned long base, · line 253

Quoted 2 lines
kexec/arch/arm/kexec-zImage-arm.c +2 -0
+		strlen(prop_name) + 1 +
+		/* fdt_setprop -> _fdt_add_property */

ditto.

OTOH, should not extend the dtb_buf with extra node size if the node exist. same for the property

kexec/arch/arm/kexec-zImage-arm.c int atag_arm_load(struct kexec_info *info, unsigned long base, · line 263

Quoted 10 lines
kexec/arch/arm/kexec-zImage-arm.c +10 -0
+		sizeof(struct fdt_property) + _ALIGN(len, FDT_TAGSIZE);
+
+	dtb_buf = xrealloc(dtb_buf, *sizep);
+	if (dtb_buf == NULL)
+		die("xrealloc failed\n");
+	*bufp = dtb_buf;
+
+	fdt_set_totalsize(dtb_buf, *sizep);
+
+	/* check if the subnode already exists */

The comment is not necessary.

Quoted 69 lines
kexec/arch/arm/kexec-zImage-arm.c +25 -26
+	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,
+				val, len) != 0) {
+		fprintf(stderr, "FDT: Error setting %s/%s property.\n",
+				node_name, prop_name);
+		return -1;
+	}
+	return 0;
+}
+
 int zImage_arm_load(int argc, char **argv, const char *buf, off_t len,
 	struct kexec_info *info)
 {
@@ -375,32 +437,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);
-					return -1;
-				}
+				/*  
+				 *  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.3.4


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

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

author
#3 lore raw

PATCH v3 2/2 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 977254e..886a718 100644
@@ -461,6 +461,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.3.4


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