[U-Boot-Users] [PATCH] [new uImage] Use lmb for bootm allocations

Kumar Gala galak at kernel.crashing.org
Tue Feb 19 00:26:39 CET 2008


Convert generic ramdisk_high(), get_boot_cmdline(), get_boot_kbd()
functions over to using lmb for allocation of the ramdisk, command line
and kernel bd info.

Convert PPC specific get_fdt() to use lmb for allocation of the device
tree.

Also introduce the concept of bootmap_base to specify the offset in
physical memory that the bootmap is located at.  This is used for
allocations of the cmdline, kernel bd, and device tree as they should
be contained within bootmap_base and bootmap_base + CFG_BOOTMAPSZ.

Signed-off-by: Kumar Gala <galak at kernel.crashing.org>
---

This requires the 'ppc: Allow boards to specify effective amount of
memory'.  Also, this is in the series of patches against
u-boot-testing/new-image branch.

- k

 common/image.c   |  134 +++++++++++++++++++-----------------------------------
 include/image.h  |   13 ++---
 lib_m68k/bootm.c |   29 ++++++++----
 lib_ppc/bootm.c  |   78 ++++++++++++++++++-------------
 4 files changed, 119 insertions(+), 135 deletions(-)

diff --git a/common/image.c b/common/image.c
index dfe2004..b5f7a7a 100644
--- a/common/image.c
+++ b/common/image.c
@@ -506,10 +506,9 @@ int get_ramdisk (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
 #if defined(CONFIG_PPC) || defined(CONFIG_M68K)
 /**
  * ramdisk_high - relocate init ramdisk
+ * @lmb: pointer to lmb handle, will be used for memory mgmt
  * @rd_data: ramdisk data start address
  * @rd_len: ramdisk data length
- * @sp_limit: stack pointer limit (including BOOTMAPSZ)
- * @sp: current stack pointer
  * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
  *      start address (after possible relocation)
  * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
@@ -522,16 +521,16 @@ int get_ramdisk (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
  *     - initrd_start and initrd_end are set to final (after relocation) ramdisk
  *     start/end addresses if ramdisk image start and len were provided
  *     otherwise set initrd_start and initrd_end set to zeros
- *     - returns new allc_current, next free address below BOOTMAPSZ
+ *     - returns:
+ *        0 - success
+ *       -1 - failure
  */
-ulong ramdisk_high (ulong alloc_current, ulong rd_data, ulong rd_len,
-		ulong sp_limit, ulong sp,
-		ulong *initrd_start, ulong *initrd_end)
+int ramdisk_high (struct lmb *lmb, ulong rd_data, ulong rd_len,
+		  ulong *initrd_start, ulong *initrd_end)
 {
 	char	*s;
 	ulong	initrd_high;
 	int	initrd_copy_to_ram = 1;
-	ulong	new_alloc_current = alloc_current;

 	if ((s = getenv ("initrd_high")) != NULL) {
 		/* a value of "no" or a similar string will act like 0,
@@ -545,12 +544,6 @@ ulong ramdisk_high (ulong alloc_current, ulong rd_data, ulong rd_len,
 		initrd_high = ~0;
 	}

-#ifdef CONFIG_LOGBUFFER
-	/* Prevent initrd from overwriting logbuffer */
-	if (initrd_high < (gd->bd->bi_memsize - LOGBUFF_LEN - LOGBUFF_OVERHEAD))
-	    initrd_high = gd->bd->bi_memsize - LOGBUFF_LEN - LOGBUFF_OVERHEAD;
-	debug ("## Logbuffer at 0x%08lx ", gd->bd->bi_memsize - LOGBUFF_LEN);
-#endif
 	debug ("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
 			initrd_high, initrd_copy_to_ram);

@@ -559,40 +552,17 @@ ulong ramdisk_high (ulong alloc_current, ulong rd_data, ulong rd_len,
 			debug ("   in-place initrd\n");
 			*initrd_start = rd_data;
 			*initrd_end = rd_data + rd_len;
+			lmb_reserve(lmb, rd_data, rd_len);
 		} else {
-			new_alloc_current = alloc_current - rd_len;
-			*initrd_start  = new_alloc_current;
-			*initrd_start &= ~(4096 - 1);	/* align on page */
-
-			if (initrd_high) {
-				ulong nsp;
-
-				/*
-				 * the inital ramdisk does not need to be within
-				 * CFG_BOOTMAPSZ as it is not accessed until after
-				 * the mm system is initialised.
-				 *
-				 * do the stack bottom calculation again and see if
-				 * the initrd will fit just below the monitor stack
-				 * bottom without overwriting the area allocated
-				 * for command line args and board info.
-				 */
-				nsp = sp;
-				nsp -= 2048;		/* just to be sure */
-				nsp &= ~0xF;
-
-				if (nsp > initrd_high)	/* limit as specified */
-					nsp = initrd_high;
-
-				nsp -= rd_len;
-				nsp &= ~(4096 - 1);	/* align on page */
-
-				if (nsp >= sp_limit) {
-					*initrd_start = nsp;
-					new_alloc_current = alloc_current;
-				}
+			if (initrd_high)
+				*initrd_start = lmb_alloc_base(lmb, rd_len, 0x1000, initrd_high);
+			else
+				*initrd_start = lmb_alloc(lmb, rd_len, 0x1000);
+
+			if (*initrd_start == 0) {
+				puts("ramdisk - allocation error\n");
+				goto error;
 			}
-
 			show_boot_progress (12);

 			*initrd_end = *initrd_start + rd_len;
@@ -610,56 +580,40 @@ ulong ramdisk_high (ulong alloc_current, ulong rd_data, ulong rd_len,
 	}
 	debug ("   ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
 			*initrd_start, *initrd_end);
+	return 0;

-	return new_alloc_current;
-}
-
-/**
- * get_boot_sp_limit - calculate stack pointer limit
- * @sp: current stack pointer
- *
- * get_boot_sp_limit() takes current stack pointer adrress and calculates
- * stack pointer limit, below which kernel boot data (cmdline, board info,
- * etc.) will be allocated.
- *
- * returns:
- *     stack pointer limit
- */
-ulong get_boot_sp_limit(ulong sp)
-{
-	ulong sp_limit = sp;
-
-	sp_limit -= 2048;	/* just to be sure */
-
-	/* make sure sp_limit is within kernel mapped space */
-	if (sp_limit > CFG_BOOTMAPSZ)
-		sp_limit = CFG_BOOTMAPSZ;
-	sp_limit &= ~0xF;
-
-	return sp_limit;
+error:
+	return -1;
 }

 /**
  * get_boot_cmdline - allocate and initialize kernel cmdline
- * @alloc_current: current boot allocation address (counting down
- *      from sp_limit)
+ * @lmb: pointer to lmb handle, will be used for memory mgmt
  * @cmd_start: pointer to a ulong variable, will hold cmdline start
  * @cmd_end: pointer to a ulong variable, will hold cmdline end
+ * @bootmap_base: ulong variable, holds offset in physical memory to
+ * base of bootmap
  *
  * get_boot_cmdline() allocates space for kernel command line below
- * provided alloc_current address. If "bootargs" U-boot environemnt
+ * BOOTMAPSZ + bootmap_base address. If "bootargs" U-boot environemnt
  * variable is present its contents is copied to allocated kernel
  * command line.
  *
  * returns:
- *     alloc_current after cmdline allocation
+ *      0 - success
+ *     -1 - failure
  */
-ulong get_boot_cmdline (ulong alloc_current, ulong *cmd_start, ulong *cmd_end)
+int get_boot_cmdline (struct lmb *lmb, ulong *cmd_start, ulong *cmd_end,
+			ulong bootmap_base)
 {
 	char *cmdline;
 	char *s;

-	cmdline = (char *)((alloc_current - CFG_BARGSIZE) & ~0xF);
+	cmdline = (char *)lmb_alloc_base(lmb, CFG_BARGSIZE, 0xf,
+					 CFG_BOOTMAPSZ + bootmap_base);
+
+	if (cmdline == NULL)
+		return -1;

 	if ((s = getenv("bootargs")) == NULL)
 		s = "";
@@ -671,25 +625,31 @@ ulong get_boot_cmdline (ulong alloc_current, ulong *cmd_start, ulong *cmd_end)

 	debug ("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);

-	return (ulong)cmdline;
+	return 0;
 }

 /**
  * get_boot_kbd - allocate and initialize kernel copy of board info
- * @alloc_current: current boot allocation address (counting down
- *      from sp_limit)
+ * @lmb: pointer to lmb handle, will be used for memory mgmt
  * @kbd: double pointer to board info data
+ * @bootmap_base: ulong variable, holds offset in physical memory to
+ * base of bootmap
  *
- * get_boot_kbd() - allocates space for kernel copy of board info data.
- * Space is allocated below provided alloc_current address and kernel
- * board info is initialized with the current u-boot board info data.
+ * get_boot_kbd() allocates space for kernel copy of board info data below
+ * BOOTMAPSZ + bootmap_base address and kernel board info is initialized with
+ * the current u-boot board info data.
  *
  * returns:
- *     alloc_current after kbd allocation
+ *      0 - success
+ *     -1 - failure
  */
-ulong get_boot_kbd (ulong alloc_current, bd_t **kbd)
+int get_boot_kbd (struct lmb *lmb, bd_t **kbd, ulong bootmap_base)
 {
-	*kbd = (bd_t *) (((ulong)alloc_current - sizeof(bd_t)) & ~0xF);
+	*kbd = (bd_t *)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
+				      CFG_BOOTMAPSZ + bootmap_base);
+	if (*kbd == NULL)
+		return -1;
+
 	**kbd = *(gd->bd);

 	debug ("## kernel board info at 0x%08lx\n", (ulong)*kbd);
@@ -698,7 +658,7 @@ ulong get_boot_kbd (ulong alloc_current, bd_t **kbd)
 	do_bdinfo(NULL, 0, 0, NULL);
 #endif

-	return (ulong)*kbd;
+	return 0;
 }
 #endif /* CONFIG_PPC || CONFIG_M68K */

diff --git a/include/image.h b/include/image.h
index 4cf9567..f26b7e6 100644
--- a/include/image.h
+++ b/include/image.h
@@ -36,6 +36,7 @@
 #include <asm/byteorder.h>
 #include <command.h>
 #ifndef USE_HOSTCC
+#include <lmb.h>
 #include <linux/string.h>
 #include <asm/u-boot.h>
 #endif
@@ -344,13 +345,11 @@ int get_ramdisk (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
 		ulong *rd_start, ulong *rd_end);

 #if defined(CONFIG_PPC) || defined(CONFIG_M68K)
-ulong ramdisk_high (ulong alloc_current, ulong rd_data, ulong rd_len,
-		ulong sp_limit, ulong sp,
-		ulong *initrd_start, ulong *initrd_end);
-
-ulong get_boot_sp_limit (ulong sp);
-ulong get_boot_cmdline (ulong alloc_current, ulong *cmd_start, ulong *cmd_end);
-ulong get_boot_kbd (ulong alloc_current, bd_t **kbd);
+int ramdisk_high (struct lmb *lmb, ulong rd_data, ulong rd_len,
+		  ulong *initrd_start, ulong *initrd_end);
+int get_boot_cmdline (struct lmb *lmb, ulong *cmd_start, ulong *cmd_end,
+			ulong bootmap_base);
+int get_boot_kbd (struct lmb *lmb, bd_t **kbd, ulong bootmap_base);
 #endif /* CONFIG_PPC || CONFIG_M68K */

 #endif /* USE_HOSTCC */
diff --git a/lib_m68k/bootm.c b/lib_m68k/bootm.c
index c614e74..5aedb99 100644
--- a/lib_m68k/bootm.c
+++ b/lib_m68k/bootm.c
@@ -50,16 +50,18 @@ void do_bootm_linux(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
 		    image_header_t *hdr, int verify, int autostart,
 		    struct lmb *lmb)
 {
-	ulong sp, sp_limit, alloc_current;
+	ulong sp;

 	ulong rd_data_start, rd_data_end, rd_len;
 	ulong initrd_start, initrd_end;
 	int ret;

-	ulong cmd_start, cmd_end;
+	ulong cmd_start, cmd_end, bootmap_base;
 	bd_t *kbd;
 	void (*kernel) (bd_t *, ulong, ulong, ulong, ulong);

+	bootmap_base = 0;
+
 	/*
 	 * Booting a (Linux) kernel image
 	 *
@@ -72,14 +74,23 @@ void do_bootm_linux(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
 	sp = get_sp();
 	debug ("## Current stack ends at 0x%08lx ", sp);

-	alloc_current = sp_limit = get_boot_sp_limit(sp);
-	debug ("=> set upper limit to 0x%08lx\n", sp_limit);
+	/* adjust sp by 1K to be safe */
+	sp -= 1024;
+	lmb_reserve(lmb, sp, (CFG_SDRAM_BASE + gd->ram_size - sp));

 	/* allocate space and init command line */
-	alloc_current = get_boot_cmdline (alloc_current, &cmd_start, &cmd_end);
+	ret = get_boot_cmdline (lmb, &cmd_start, &cmd_end, bootmap_base);
+	if (ret) {
+		puts("ERROR with allocation of cmdline\n");
+		goto error;
+	}

 	/* allocate space for kernel copy of board info */
-	alloc_current = get_boot_kbd (alloc_current, &kbd);
+	ret = get_boot_kbd (lmb, &kbd, bootmap_base);
+	if (ret) {
+		puts("ERROR with allocation of kernel bd\n");
+		goto error;
+	}
 	set_clocks_in_mhz(kbd);

 	/* find kernel */
@@ -89,13 +100,13 @@ void do_bootm_linux(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
 	/* find ramdisk */
 	ret = get_ramdisk (cmdtp, flag, argc, argv, hdr, verify,
 				IH_ARCH_M68K, &rd_data_start, &rd_data_end);
-
 	if (ret)
 		goto error;

 	rd_len = rd_data_end - rd_data_start;
-	alloc_current = ramdisk_high (alloc_current, rd_data_start, rd_len,
-			sp_limit, get_sp (), &initrd_start, &initrd_end);
+	ret = ramdisk_high (lmb, rd_data_start, rd_len, &initrd_start, &initrd_end);
+	if (ret)
+		goto error;

 	debug("## Transferring control to Linux (at address %08lx) ...\n",
 	      (ulong) kernel);
diff --git a/lib_ppc/bootm.c b/lib_ppc/bootm.c
index 666f909..c8d6935 100644
--- a/lib_ppc/bootm.c
+++ b/lib_ppc/bootm.c
@@ -42,9 +42,9 @@
 #include <fdt_support.h>

 static void fdt_error (const char *msg);
-static ulong get_fdt (ulong alloc_current, cmd_tbl_t *cmdtp, int flag,
-		int argc, char *argv[],
-		image_header_t *hdr, char **of_flat_tree);
+static int get_fdt (struct lmb *lmb, cmd_tbl_t *cmdtp, int flag,
+			int argc, char *argv[], ulong bootmap_base,
+			image_header_t *hdr, char **of_flat_tree);
 #endif

 #ifdef CFG_INIT_RAM_LOCK
@@ -54,6 +54,7 @@ static ulong get_fdt (ulong alloc_current, cmd_tbl_t *cmdtp, int flag,
 DECLARE_GLOBAL_DATA_PTR;

 extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
+extern ulong get_effective_memsize(void);
 static ulong get_sp (void);
 static void set_clocks_in_mhz (bd_t *kbd);

@@ -62,12 +63,12 @@ do_bootm_linux(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
 		image_header_t *hdr, int verify, int autostart,
 		struct lmb *lmb)
 {
-	ulong	sp, sp_limit, alloc_current;
+	ulong	sp;

 	ulong	initrd_start, initrd_end;
 	ulong	rd_data_start, rd_data_end, rd_len;

-	ulong	cmd_start, cmd_end;
+	ulong	cmd_start, cmd_end, bootmap_base;
 	bd_t	*kbd;
 	void	(*kernel)(bd_t *, ulong, ulong, ulong, ulong);

@@ -89,6 +90,8 @@ do_bootm_linux(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
 	}
 #endif

+	bootmap_base = 0;
+
 	/*
 	 * Booting a (Linux) kernel image
 	 *
@@ -101,15 +104,24 @@ do_bootm_linux(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
 	sp = get_sp();
 	debug ("## Current stack ends at 0x%08lx ", sp);

-	alloc_current = sp_limit = get_boot_sp_limit(sp);
-	debug ("=> set upper limit to 0x%08lx\n", sp_limit);
+	/* adjust sp by 1K to be safe */
+	sp -= 1024;
+	lmb_reserve(lmb, sp, (CFG_SDRAM_BASE + get_effective_memsize() - sp));

 	if (!has_of) {
 		/* allocate space and init command line */
-		alloc_current = get_boot_cmdline (alloc_current, &cmd_start, &cmd_end);
+		ret = get_boot_cmdline (lmb, &cmd_start, &cmd_end, bootmap_base);
+		if (ret) {
+			puts("ERROR with allocation of cmdline\n");
+			goto error;
+		}

 		/* allocate space for kernel copy of board info */
-		alloc_current = get_boot_kbd (alloc_current, &kbd);
+		ret = get_boot_kbd (lmb, &kbd, bootmap_base);
+		if (ret) {
+			puts("ERROR with allocation of kernel bd\n");
+			goto error;
+		}
 		set_clocks_in_mhz(kbd);
 	}

@@ -119,7 +131,6 @@ do_bootm_linux(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
 	/* find ramdisk */
 	ret = get_ramdisk (cmdtp, flag, argc, argv, hdr, verify,
 				IH_ARCH_PPC, &rd_data_start, &rd_data_end);
-
 	if (ret)
 		goto error;

@@ -127,8 +138,11 @@ do_bootm_linux(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],

 #if defined(CONFIG_OF_LIBFDT)
 	/* find flattened device tree */
-	alloc_current = get_fdt (alloc_current,
-			cmdtp, flag, argc, argv, hdr, &of_flat_tree);
+	ret = get_fdt (lmb, cmdtp, flag, argc, argv, bootmap_base, hdr,
+			&of_flat_tree);
+
+	if (ret)
+		goto error;

 	/*
 	 * Add the chosen node if it doesn't exist, add the env and bd_t
@@ -159,8 +173,9 @@ do_bootm_linux(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
 	}
 #endif	/* CONFIG_OF_LIBFDT */

-	alloc_current = ramdisk_high (alloc_current, rd_data_start, rd_len,
-			sp_limit, get_sp (), &initrd_start, &initrd_end);
+	ret = ramdisk_high (lmb, rd_data_start, rd_len, &initrd_start, &initrd_end);
+	if (ret)
+		goto error;

 #if defined(CONFIG_OF_LIBFDT)
 	/* fixup the initrd now that we know where it should be */
@@ -275,15 +290,14 @@ static void fdt_error (const char *msg)
 	puts (" - must RESET the board to recover.\n");
 }

-static ulong get_fdt (ulong alloc_current,
-		cmd_tbl_t *cmdtp, int flag,
-		int argc, char *argv[],
-		image_header_t *hdr, char **of_flat_tree)
+static int get_fdt (struct lmb *lmb, cmd_tbl_t *cmdtp, int flag,
+			int argc, char *argv[], ulong bootmap_base,
+			image_header_t *hdr, char **of_flat_tree)
 {
 	image_header_t	*fdt_hdr;
 	ulong		fdt_relocate = 0;
 	char		*fdt = NULL;
-	ulong		new_alloc_current;
+	ulong		of_len;

 	if(argc > 3) {
 		fdt = (char *)simple_strtoul (argv[3], NULL, 16);
@@ -349,8 +363,7 @@ static ulong get_fdt (ulong alloc_current,
 			fdt_error ("Did not find a Flattened Device Tree");
 			goto error;
 		}
-		printf ("   Booting using the fdt at 0x%x\n",
-				fdt);
+		printf ("   Booting using the fdt at 0x%x\n", fdt);
 	} else if (image_check_type (hdr, IH_TYPE_MULTI)) {
 		ulong fdt_data, fdt_len;

@@ -383,25 +396,28 @@ static ulong get_fdt (ulong alloc_current,
 		}
 	}

-#ifdef CFG_BOOTMAPSZ
 	/*
 	 * The blob must be within CFG_BOOTMAPSZ,
 	 * so we flag it to be copied if it is not.
 	 */
 	if (fdt >= (char *)CFG_BOOTMAPSZ)
 		fdt_relocate = 1;
-#endif
+
+	of_len = be32_to_cpu (fdt_totalsize (fdt));

 	/* move flattend device tree if needed */
 	if (fdt_relocate) {
 		int err;
-		ulong of_start, of_len;
-
-		of_len = be32_to_cpu (fdt_totalsize (fdt));
+		ulong of_start;

 		/* position on a 4K boundary before the alloc_current */
-		of_start  = alloc_current - of_len;
-		of_start &= ~(4096 - 1);	/* align on page */
+		of_start = lmb_alloc_base(lmb, of_len, 0x1000,
+					 (CFG_BOOTMAPSZ + bootmap_base));
+
+		if (of_start == 0) {
+			puts("device tree - allocation error\n");
+			goto error;
+		}

 		debug ("## device tree at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
 			(ulong)fdt, (ulong)fdt + of_len - 1,
@@ -418,16 +434,14 @@ static ulong get_fdt (ulong alloc_current,
 		puts ("OK\n");

 		*of_flat_tree = (char *)of_start;
-		new_alloc_current = of_start;
 	} else {
 		*of_flat_tree = fdt;
-		new_alloc_current = alloc_current;
+		lmb_reserve(lmb, (ulong)fdt, of_len);
 	}

-	return new_alloc_current;
+	return 0;

 error:
-	do_reset (cmdtp, flag, argc, argv);
 	return 1;
 }
 #endif
-- 
1.5.3.8





More information about the U-Boot mailing list