bootm: refactor ramdisk locating code
[oweals/u-boot.git] / lib_ppc / bootm.c
index 89463e342f0f4accb4175ed1f67961986b397205..e6a6e4461d4b28cfb801bc5bf63f75a436266713 100644 (file)
@@ -51,6 +51,10 @@ static int boot_relocate_fdt (struct lmb *lmb, ulong bootmap_base,
 #include <asm/cache.h>
 #endif
 
+#ifndef CFG_FDT_PAD
+#define CFG_FDT_PAD 0x3000
+#endif
+
 DECLARE_GLOBAL_DATA_PTR;
 
 extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
@@ -69,13 +73,14 @@ do_bootm_linux(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
        ulong   sp;
 
        ulong   initrd_start, initrd_end;
-       ulong   rd_data_start, rd_data_end, rd_len;
+       ulong   rd_len;
        ulong   size;
+       phys_size_t bootm_size;
 
        ulong   cmd_start, cmd_end, bootmap_base;
        bd_t    *kbd;
-       ulong   ep = 0;
-       void    (*kernel)(bd_t *, ulong, ulong, ulong, ulong);
+       void    (*kernel)(bd_t *, ulong r4, ulong r5, ulong r6,
+                         ulong r7, ulong r8, ulong r9);
        int     ret;
        ulong   of_size = 0;
        struct lmb *lmb = images->lmb;
@@ -84,23 +89,27 @@ do_bootm_linux(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
        char    *of_flat_tree = NULL;
 #endif
 
+       kernel = (void (*)(bd_t *, ulong, ulong, ulong,
+                          ulong, ulong, ulong))images->ep;
+
        bootmap_base = getenv_bootm_low();
-       size = getenv_bootm_size();
+       bootm_size = getenv_bootm_size();
 
 #ifdef DEBUG
-       if (((u64)bootmap_base + size) > (CFG_SDRAM_BASE + (u64)gd->ram_size))
+       if (((u64)bootmap_base + bootm_size) >
+           (CFG_SDRAM_BASE + (u64)gd->ram_size))
                puts("WARNING: bootm_low + bootm_size exceed total memory\n");
-       if ((bootmap_base + size) > get_effective_memsize())
+       if ((bootmap_base + bootm_size) > get_effective_memsize())
                puts("WARNING: bootm_low + bootm_size exceed eff. memory\n");
 #endif
 
-       size = min(size, get_effective_memsize());
+       size = min(bootm_size, get_effective_memsize());
        size = min(size, CFG_LINUX_LOWMEM_MAX_SIZE);
 
-       if (size < getenv_bootm_size()) {
+       if (size < bootm_size) {
                ulong base = bootmap_base + size;
-               printf("WARNING: adjusting available memory to %x\n", size);
-               lmb_reserve(lmb, base, getenv_bootm_size() - size);
+               printf("WARNING: adjusting available memory to %lx\n", size);
+               lmb_reserve(lmb, base, bootm_size - size);
        }
 
        /*
@@ -144,31 +153,7 @@ do_bootm_linux(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
                set_clocks_in_mhz(kbd);
        }
 
-       /* find kernel entry point */
-       if (images->legacy_hdr_valid) {
-               ep = image_get_ep (images->legacy_hdr_os);
-#if defined(CONFIG_FIT)
-       } else if (images->fit_uname_os) {
-               ret = fit_image_get_entry (images->fit_hdr_os,
-                               images->fit_noffset_os, &ep);
-               if (ret) {
-                       puts ("Can't get entry point property!\n");
-                       goto error;
-               }
-#endif
-       } else {
-               puts ("Could not find kernel entry point!\n");
-               goto error;
-       }
-       kernel = (void (*)(bd_t *, ulong, ulong, ulong, ulong))ep;
-
-       /* find ramdisk */
-       ret = boot_get_ramdisk (argc, argv, images, IH_ARCH_PPC,
-                       &rd_data_start, &rd_data_end);
-       if (ret)
-               goto error;
-
-       rd_len = rd_data_end - rd_data_start;
+       rd_len = images->rd_end - images->rd_start;
 
 #if defined(CONFIG_OF_LIBFDT)
        ret = boot_relocate_fdt (lmb, bootmap_base,
@@ -180,30 +165,57 @@ do_bootm_linux(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
         */
        if (of_size) {
                /* pass in dummy initrd info, we'll fix up later */
-               if (fdt_chosen(of_flat_tree, rd_data_start, rd_data_end, 0) < 0) {
+               if (fdt_chosen(of_flat_tree, images->rd_start, images->rd_end, 0) < 0) {
                        fdt_error ("/chosen node create failed");
                        goto error;
                }
-#ifdef CONFIG_OF_HAS_UBOOT_ENV
-               if (fdt_env(of_flat_tree) < 0) {
-                       fdt_error ("/u-boot-env node create failed");
-                       goto error;
-               }
-#endif
-#ifdef CONFIG_OF_HAS_BD_T
-               if (fdt_bd_t(of_flat_tree) < 0) {
-                       fdt_error ("/bd_t node create failed");
-                       goto error;
-               }
-#endif
 #ifdef CONFIG_OF_BOARD_SETUP
                /* Call the board-specific fixup routine */
                ft_board_setup(of_flat_tree, gd->bd);
 #endif
        }
+
+       /* Fixup the fdt memreserve now that we know how big it is */
+       if (of_flat_tree) {
+               int j;
+               uint64_t addr, size;
+               int total = fdt_num_mem_rsv(of_flat_tree);
+               uint actualsize;
+
+               for (j = 0; j < total; j++) {
+                       fdt_get_mem_rsv(of_flat_tree, j, &addr, &size);
+                       if (addr == (uint64_t)(u32)of_flat_tree) {
+                               fdt_del_mem_rsv(of_flat_tree, j);
+                               break;
+                       }
+               }
+
+               /* Delete the old LMB reservation */
+               lmb_free(lmb, (phys_addr_t)(u32)of_flat_tree,
+                               (phys_size_t)fdt_totalsize(of_flat_tree));
+
+               /* Calculate the actual size of the fdt */
+               actualsize = fdt_off_dt_strings(of_flat_tree) +
+                       fdt_size_dt_strings(of_flat_tree);
+
+               /* Make it so the fdt ends on a page boundary */
+               actualsize = ALIGN(actualsize, 0x1000);
+               actualsize = actualsize - ((uint)of_flat_tree & 0xfff);
+
+               /* Change the fdt header to reflect the correct size */
+               fdt_set_totalsize(of_flat_tree, actualsize);
+               of_size = actualsize;
+
+               /* Add the new reservation */
+               ret = fdt_add_mem_rsv(of_flat_tree, (uint)of_flat_tree,
+                               of_size);
+
+               /* Create a new LMB reservation */
+               lmb_reserve(lmb, (ulong)of_flat_tree, of_size);
+       }
 #endif /* CONFIG_OF_LIBFDT */
 
-       ret = boot_ramdisk_high (lmb, rd_data_start, rd_len, &initrd_start, &initrd_end);
+       ret = boot_ramdisk_high (lmb, images->rd_start, rd_len, &initrd_start, &initrd_end);
        if (ret)
                goto error;
 
@@ -217,7 +229,7 @@ do_bootm_linux(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
                /* Look for the dummy entry and delete it */
                for (j = 0; j < total; j++) {
                        fdt_get_mem_rsv(of_flat_tree, j, &addr, &size);
-                       if (addr == rd_data_start) {
+                       if (addr == images->rd_start) {
                                fdt_del_mem_rsv(of_flat_tree, j);
                                break;
                        }
@@ -244,21 +256,28 @@ do_bootm_linux(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
 #if defined(CFG_INIT_RAM_LOCK) && !defined(CONFIG_E500)
        unlock_ram_in_cache();
 #endif
-       if (!images->autostart)
-               return ;
 
 #if defined(CONFIG_OF_LIBFDT)
        if (of_flat_tree) {     /* device tree; boot new style */
                /*
                 * Linux Kernel Parameters (passing device tree):
-                *   r3: pointer to the fdt, followed by the board info data
-                *   r4: physical pointer to the kernel itself
-                *   r5: NULL
-                *   r6: NULL
-                *   r7: NULL
+                *   r3: pointer to the fdt
+                *   r4: 0
+                *   r5: 0
+                *   r6: epapr magic
+                *   r7: size of IMA in bytes
+                *   r8: 0
+                *   r9: 0
                 */
+#if defined(CONFIG_85xx) || defined(CONFIG_440)
+ #define EPAPR_MAGIC   (0x45504150)
+#else
+ #define EPAPR_MAGIC   (0x65504150)
+#endif
+
                debug ("   Booting using OF flat tree...\n");
-               (*kernel) ((bd_t *)of_flat_tree, (ulong)kernel, 0, 0, 0);
+               (*kernel) ((bd_t *)of_flat_tree, 0, 0, EPAPR_MAGIC,
+                          CFG_BOOTMAPSZ, 0, 0);
                /* does not return */
        } else
 #endif
@@ -270,16 +289,18 @@ do_bootm_linux(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
                 *   r5: initrd_end - unused if r4 is 0
                 *   r6: Start of command line string
                 *   r7: End   of command line string
+                *   r8: 0
+                *   r9: 0
                 */
                debug ("   Booting using board info...\n");
-               (*kernel) (kbd, initrd_start, initrd_end, cmd_start, cmd_end);
+               (*kernel) (kbd, initrd_start, initrd_end,
+                          cmd_start, cmd_end, 0, 0);
                /* does not return */
        }
        return ;
 
 error:
-       if (images->autostart)
-               do_reset (cmdtp, flag, argc, argv);
+       do_reset (cmdtp, flag, argc, argv);
        return ;
 }
 
@@ -310,7 +331,7 @@ static void set_clocks_in_mhz (bd_t *kbd)
                kbd->bi_cpmfreq /= 1000000L;
                kbd->bi_brgfreq /= 1000000L;
                kbd->bi_sccfreq /= 1000000L;
-               kbd->bi_vco     /= 1000000L;
+               kbd->bi_vco     /= 1000000L;
 #endif
 #if defined(CONFIG_MPC5xxx)
                kbd->bi_ipbfreq /= 1000000L;
@@ -410,9 +431,9 @@ static int boot_get_fdt (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
        ulong           image_start, image_end;
        ulong           load_start, load_end;
 #if defined(CONFIG_FIT)
-        void            *fit_hdr;
-        const char      *fit_uname_config = NULL;
-        const char      *fit_uname_fdt = NULL;
+       void            *fit_hdr;
+       const char      *fit_uname_config = NULL;
+       const char      *fit_uname_fdt = NULL;
        ulong           default_addr;
        int             cfg_noffset;
        int             fdt_noffset;
@@ -638,9 +659,9 @@ static int boot_get_fdt (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
                                /*
                                 * FDT blob
                                 */
-                               debug ("*  fdt: raw FDT blob\n");
-                               printf ("## Flattened Device Tree blob at %08lx\n", fdt_blob);
                                fdt_blob = (char *)fdt_addr;
+                               debug ("*  fdt: raw FDT blob\n");
+                               printf ("## Flattened Device Tree blob at %08lx\n", (long)fdt_blob);
                        }
                        break;
                default:
@@ -648,10 +669,10 @@ static int boot_get_fdt (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
                        goto error;
                }
 
-               printf ("   Booting using the fdt blob at 0x%x\n", fdt_blob);
+               printf ("   Booting using the fdt blob at 0x%x\n", (int)fdt_blob);
 
        } else if (images->legacy_hdr_valid &&
-                       image_check_type (images->legacy_hdr_os, IH_TYPE_MULTI)) {
+                       image_check_type (&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
 
                ulong fdt_data, fdt_len;
 
@@ -667,7 +688,7 @@ static int boot_get_fdt (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
                if (fdt_len) {
 
                        fdt_blob = (char *)fdt_data;
-                       printf ("   Booting using the fdt at 0x%x\n", fdt_blob);
+                       printf ("   Booting using the fdt at 0x%x\n", (int)fdt_blob);
 
                        if (fdt_check_header (fdt_blob) != 0) {
                                fdt_error ("image is not a fdt");
@@ -679,9 +700,8 @@ static int boot_get_fdt (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
                                goto error;
                        }
                } else {
-                       fdt_error ("Did not find a Flattened Device Tree "
-                               "in a legacy multi-component image");
-                       goto error;
+                       debug ("## No Flattened Device Tree\n");
+                       return 0;
                }
        } else {
                debug ("## No Flattened Device Tree\n");
@@ -724,22 +744,25 @@ static int boot_relocate_fdt (struct lmb *lmb, ulong bootmap_base,
 #endif
 
        /*
-        * The blob must be within CFG_BOOTMAPSZ,
-        * so we flag it to be copied if it is not.
+        * The blob needs to be inside the boot mapping.
         */
-       if (fdt_blob >= (char *)CFG_BOOTMAPSZ)
+       if (fdt_blob < (char *)bootmap_base)
                relocate = 1;
 
-       of_len = be32_to_cpu (fdt_totalsize (fdt_blob));
+       if ((fdt_blob + *of_size + CFG_FDT_PAD) >=
+                       ((char *)CFG_BOOTMAPSZ + bootmap_base))
+               relocate = 1;
 
        /* move flattend device tree if needed */
        if (relocate) {
                int err;
-               ulong of_start;
+               ulong of_start = 0;
 
                /* position on a 4K boundary before the alloc_current */
-               of_start = lmb_alloc_base(lmb, of_len, 0x1000,
-                                        (CFG_BOOTMAPSZ + bootmap_base));
+               /* Pad the FDT by a specified amount */
+               of_len = *of_size + CFG_FDT_PAD;
+               of_start = (unsigned long)lmb_alloc_base(lmb, of_len, 0x1000,
+                               (CFG_BOOTMAPSZ + bootmap_base));
 
                if (of_start == 0) {
                        puts("device tree - allocation error\n");
@@ -747,7 +770,7 @@ static int boot_relocate_fdt (struct lmb *lmb, ulong bootmap_base,
                }
 
                debug ("## device tree at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
-                       (ulong)fdt_blob, (ulong)fdt_blob + of_len - 1,
+                       (ulong)fdt_blob, (ulong)fdt_blob + *of_size - 1,
                        of_len, of_len);
 
                printf ("   Loading Device Tree to %08lx, end %08lx ... ",
@@ -761,9 +784,14 @@ static int boot_relocate_fdt (struct lmb *lmb, ulong bootmap_base,
                puts ("OK\n");
 
                *of_flat_tree = (char *)of_start;
+               *of_size = of_len;
        } else {
                *of_flat_tree = fdt_blob;
-               lmb_reserve(lmb, (ulong)fdt, of_len);
+               of_len = (CFG_BOOTMAPSZ + bootmap_base) - (ulong)fdt_blob;
+               lmb_reserve(lmb, (ulong)fdt_blob, of_len);
+               fdt_set_totalsize(*of_flat_tree, of_len);
+
+               *of_size = of_len;
        }
 
        return 0;