Marian Balakowicz | 2437cf2 | 2008-01-08 18:11:43 +0100 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2008 Semihalf |
| 3 | * |
| 4 | * (C) Copyright 2000-2006 |
| 5 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 6 | * |
| 7 | * See file CREDITS for list of people who contributed to this |
| 8 | * project. |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or |
| 11 | * modify it under the terms of the GNU General Public License as |
| 12 | * published by the Free Software Foundation; either version 2 of |
| 13 | * the License, or (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program; if not, write to the Free Software |
| 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 23 | * MA 02111-1307 USA |
| 24 | */ |
| 25 | |
Marian Balakowicz | f92332b | 2008-01-31 13:20:06 +0100 | [diff] [blame] | 26 | #define DEBUG |
| 27 | |
Marian Balakowicz | 2437cf2 | 2008-01-08 18:11:43 +0100 | [diff] [blame] | 28 | #include <common.h> |
| 29 | #include <watchdog.h> |
| 30 | #include <command.h> |
| 31 | #include <image.h> |
| 32 | #include <malloc.h> |
| 33 | #include <zlib.h> |
| 34 | #include <bzlib.h> |
| 35 | #include <environment.h> |
| 36 | #include <asm/byteorder.h> |
| 37 | |
| 38 | #if defined(CONFIG_OF_LIBFDT) |
| 39 | #include <fdt.h> |
| 40 | #include <libfdt.h> |
| 41 | #include <fdt_support.h> |
Marian Balakowicz | 28fb615 | 2008-01-31 13:20:08 +0100 | [diff] [blame] | 42 | |
| 43 | static void fdt_error (const char *msg); |
Marian Balakowicz | 6881dae | 2008-02-04 08:28:17 +0100 | [diff] [blame] | 44 | static void get_fdt (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[], |
| 45 | bootm_headers_t *images, char **of_flat_tree, ulong *of_size); |
| 46 | static ulong fdt_relocate (ulong alloc_current, |
| 47 | cmd_tbl_t *cmdtp, int flag, int argc, char *argv[], |
| 48 | char **of_flat_tree, ulong *of_size); |
Marian Balakowicz | 2437cf2 | 2008-01-08 18:11:43 +0100 | [diff] [blame] | 49 | #endif |
| 50 | |
Marian Balakowicz | 2437cf2 | 2008-01-08 18:11:43 +0100 | [diff] [blame] | 51 | #ifdef CFG_INIT_RAM_LOCK |
| 52 | #include <asm/cache.h> |
| 53 | #endif |
| 54 | |
Marian Balakowicz | 2437cf2 | 2008-01-08 18:11:43 +0100 | [diff] [blame] | 55 | DECLARE_GLOBAL_DATA_PTR; |
Marian Balakowicz | 2437cf2 | 2008-01-08 18:11:43 +0100 | [diff] [blame] | 56 | |
Marian Balakowicz | 2437cf2 | 2008-01-08 18:11:43 +0100 | [diff] [blame] | 57 | extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]); |
Marian Balakowicz | 9c701e8 | 2008-01-31 13:57:17 +0100 | [diff] [blame] | 58 | static ulong get_sp (void); |
Marian Balakowicz | 2efc264 | 2008-01-31 13:58:13 +0100 | [diff] [blame] | 59 | static void set_clocks_in_mhz (bd_t *kbd); |
Marian Balakowicz | 2437cf2 | 2008-01-08 18:11:43 +0100 | [diff] [blame] | 60 | |
| 61 | void __attribute__((noinline)) |
Marian Balakowicz | 61fde55 | 2008-02-27 11:01:04 +0100 | [diff] [blame] | 62 | do_bootm_linux(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[], |
| 63 | bootm_headers_t *images) |
Marian Balakowicz | 2437cf2 | 2008-01-08 18:11:43 +0100 | [diff] [blame] | 64 | { |
Marian Balakowicz | 2efc264 | 2008-01-31 13:58:13 +0100 | [diff] [blame] | 65 | ulong sp, sp_limit, alloc_current; |
| 66 | |
Marian Balakowicz | b4a12a9 | 2008-01-08 18:12:17 +0100 | [diff] [blame] | 67 | ulong initrd_start, initrd_end; |
Marian Balakowicz | 9541850 | 2008-01-31 13:55:39 +0100 | [diff] [blame] | 68 | ulong rd_data_start, rd_data_end, rd_len; |
Marian Balakowicz | b4a12a9 | 2008-01-08 18:12:17 +0100 | [diff] [blame] | 69 | |
| 70 | ulong cmd_start, cmd_end; |
Marian Balakowicz | 2437cf2 | 2008-01-08 18:11:43 +0100 | [diff] [blame] | 71 | bd_t *kbd; |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 72 | ulong ep = 0; |
Marian Balakowicz | 2437cf2 | 2008-01-08 18:11:43 +0100 | [diff] [blame] | 73 | void (*kernel)(bd_t *, ulong, ulong, ulong, ulong); |
Marian Balakowicz | b4a12a9 | 2008-01-08 18:12:17 +0100 | [diff] [blame] | 74 | |
Marian Balakowicz | d216085 | 2008-01-31 13:20:07 +0100 | [diff] [blame] | 75 | #if defined(CONFIG_OF_LIBFDT) |
Marian Balakowicz | 6881dae | 2008-02-04 08:28:17 +0100 | [diff] [blame] | 76 | char *of_flat_tree = NULL; |
| 77 | ulong of_size = 0; |
Marian Balakowicz | 2437cf2 | 2008-01-08 18:11:43 +0100 | [diff] [blame] | 78 | #endif |
| 79 | |
Marian Balakowicz | 2437cf2 | 2008-01-08 18:11:43 +0100 | [diff] [blame] | 80 | /* |
| 81 | * Booting a (Linux) kernel image |
| 82 | * |
| 83 | * Allocate space for command line and board info - the |
| 84 | * address should be as high as possible within the reach of |
| 85 | * the kernel (see CFG_BOOTMAPSZ settings), but in unused |
| 86 | * memory, which means far enough below the current stack |
| 87 | * pointer. |
| 88 | */ |
Marian Balakowicz | 2efc264 | 2008-01-31 13:58:13 +0100 | [diff] [blame] | 89 | sp = get_sp(); |
| 90 | debug ("## Current stack ends at 0x%08lx ", sp); |
Marian Balakowicz | 2437cf2 | 2008-01-08 18:11:43 +0100 | [diff] [blame] | 91 | |
Marian Balakowicz | 2efc264 | 2008-01-31 13:58:13 +0100 | [diff] [blame] | 92 | alloc_current = sp_limit = get_boot_sp_limit(sp); |
| 93 | debug ("=> set upper limit to 0x%08lx\n", sp_limit); |
Marian Balakowicz | 2437cf2 | 2008-01-08 18:11:43 +0100 | [diff] [blame] | 94 | |
Marian Balakowicz | 2efc264 | 2008-01-31 13:58:13 +0100 | [diff] [blame] | 95 | /* allocate space and init command line */ |
| 96 | alloc_current = get_boot_cmdline (alloc_current, &cmd_start, &cmd_end); |
Marian Balakowicz | 2437cf2 | 2008-01-08 18:11:43 +0100 | [diff] [blame] | 97 | |
Marian Balakowicz | 2efc264 | 2008-01-31 13:58:13 +0100 | [diff] [blame] | 98 | /* allocate space for kernel copy of board info */ |
| 99 | alloc_current = get_boot_kbd (alloc_current, &kbd); |
| 100 | set_clocks_in_mhz(kbd); |
Marian Balakowicz | 2437cf2 | 2008-01-08 18:11:43 +0100 | [diff] [blame] | 101 | |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 102 | /* find kernel entry point */ |
| 103 | if (images->legacy_hdr_valid) { |
| 104 | ep = image_get_ep (images->legacy_hdr_os); |
| 105 | #if defined(CONFIG_FIT) |
| 106 | } else if (images->fit_uname_os) { |
| 107 | fit_unsupported_reset ("PPC linux bootm"); |
| 108 | do_reset (cmdtp, flag, argc, argv); |
| 109 | #endif |
| 110 | } else { |
| 111 | puts ("Could not find kernel entry point!\n"); |
| 112 | do_reset (cmdtp, flag, argc, argv); |
| 113 | } |
| 114 | kernel = (void (*)(bd_t *, ulong, ulong, ulong, ulong))ep; |
Marian Balakowicz | 2437cf2 | 2008-01-08 18:11:43 +0100 | [diff] [blame] | 115 | |
Marian Balakowicz | 9c701e8 | 2008-01-31 13:57:17 +0100 | [diff] [blame] | 116 | /* find ramdisk */ |
Marian Balakowicz | 61fde55 | 2008-02-27 11:01:04 +0100 | [diff] [blame] | 117 | get_ramdisk (cmdtp, flag, argc, argv, images, |
Marian Balakowicz | 9541850 | 2008-01-31 13:55:39 +0100 | [diff] [blame] | 118 | IH_ARCH_PPC, &rd_data_start, &rd_data_end); |
Marian Balakowicz | 9c701e8 | 2008-01-31 13:57:17 +0100 | [diff] [blame] | 119 | |
Marian Balakowicz | 9541850 | 2008-01-31 13:55:39 +0100 | [diff] [blame] | 120 | rd_len = rd_data_end - rd_data_start; |
Marian Balakowicz | 2437cf2 | 2008-01-08 18:11:43 +0100 | [diff] [blame] | 121 | |
Marian Balakowicz | 2efc264 | 2008-01-31 13:58:13 +0100 | [diff] [blame] | 122 | alloc_current = ramdisk_high (alloc_current, rd_data_start, rd_len, |
| 123 | kbd, sp_limit, get_sp (), |
Marian Balakowicz | 9c701e8 | 2008-01-31 13:57:17 +0100 | [diff] [blame] | 124 | &initrd_start, &initrd_end); |
| 125 | |
Marian Balakowicz | d216085 | 2008-01-31 13:20:07 +0100 | [diff] [blame] | 126 | #if defined(CONFIG_OF_LIBFDT) |
Marian Balakowicz | 9c701e8 | 2008-01-31 13:57:17 +0100 | [diff] [blame] | 127 | /* find flattened device tree */ |
Marian Balakowicz | 6881dae | 2008-02-04 08:28:17 +0100 | [diff] [blame] | 128 | get_fdt (cmdtp, flag, argc, argv, images, &of_flat_tree, &of_size); |
| 129 | |
| 130 | alloc_current = fdt_relocate (alloc_current, |
| 131 | cmdtp, flag, argc, argv, &of_flat_tree, &of_size); |
Marian Balakowicz | 2437cf2 | 2008-01-08 18:11:43 +0100 | [diff] [blame] | 132 | |
Marian Balakowicz | 2437cf2 | 2008-01-08 18:11:43 +0100 | [diff] [blame] | 133 | /* |
| 134 | * Add the chosen node if it doesn't exist, add the env and bd_t |
| 135 | * if the user wants it (the logic is in the subroutines). |
| 136 | */ |
Marian Balakowicz | 6881dae | 2008-02-04 08:28:17 +0100 | [diff] [blame] | 137 | if (of_size) { |
Marian Balakowicz | 2437cf2 | 2008-01-08 18:11:43 +0100 | [diff] [blame] | 138 | if (fdt_chosen(of_flat_tree, initrd_start, initrd_end, 0) < 0) { |
Marian Balakowicz | 28fb615 | 2008-01-31 13:20:08 +0100 | [diff] [blame] | 139 | fdt_error ("/chosen node create failed"); |
Marian Balakowicz | 2437cf2 | 2008-01-08 18:11:43 +0100 | [diff] [blame] | 140 | do_reset (cmdtp, flag, argc, argv); |
| 141 | } |
| 142 | #ifdef CONFIG_OF_HAS_UBOOT_ENV |
| 143 | if (fdt_env(of_flat_tree) < 0) { |
Marian Balakowicz | 28fb615 | 2008-01-31 13:20:08 +0100 | [diff] [blame] | 144 | fdt_error ("/u-boot-env node create failed"); |
Marian Balakowicz | 2437cf2 | 2008-01-08 18:11:43 +0100 | [diff] [blame] | 145 | do_reset (cmdtp, flag, argc, argv); |
| 146 | } |
| 147 | #endif |
| 148 | #ifdef CONFIG_OF_HAS_BD_T |
| 149 | if (fdt_bd_t(of_flat_tree) < 0) { |
Marian Balakowicz | 28fb615 | 2008-01-31 13:20:08 +0100 | [diff] [blame] | 150 | fdt_error ("/bd_t node create failed"); |
Marian Balakowicz | 2437cf2 | 2008-01-08 18:11:43 +0100 | [diff] [blame] | 151 | do_reset (cmdtp, flag, argc, argv); |
| 152 | } |
| 153 | #endif |
| 154 | #ifdef CONFIG_OF_BOARD_SETUP |
| 155 | /* Call the board-specific fixup routine */ |
| 156 | ft_board_setup(of_flat_tree, gd->bd); |
| 157 | #endif |
Marian Balakowicz | 2437cf2 | 2008-01-08 18:11:43 +0100 | [diff] [blame] | 158 | } |
Marian Balakowicz | d216085 | 2008-01-31 13:20:07 +0100 | [diff] [blame] | 159 | #endif /* CONFIG_OF_LIBFDT */ |
Marian Balakowicz | 751b2b8 | 2008-01-08 18:11:43 +0100 | [diff] [blame] | 160 | |
Marian Balakowicz | 2437cf2 | 2008-01-08 18:11:43 +0100 | [diff] [blame] | 161 | debug ("## Transferring control to Linux (at address %08lx) ...\n", |
| 162 | (ulong)kernel); |
| 163 | |
| 164 | show_boot_progress (15); |
| 165 | |
| 166 | #if defined(CFG_INIT_RAM_LOCK) && !defined(CONFIG_E500) |
| 167 | unlock_ram_in_cache(); |
| 168 | #endif |
| 169 | |
Marian Balakowicz | d216085 | 2008-01-31 13:20:07 +0100 | [diff] [blame] | 170 | #if defined(CONFIG_OF_LIBFDT) |
Marian Balakowicz | 2437cf2 | 2008-01-08 18:11:43 +0100 | [diff] [blame] | 171 | if (of_flat_tree) { /* device tree; boot new style */ |
| 172 | /* |
| 173 | * Linux Kernel Parameters (passing device tree): |
| 174 | * r3: pointer to the fdt, followed by the board info data |
| 175 | * r4: physical pointer to the kernel itself |
| 176 | * r5: NULL |
| 177 | * r6: NULL |
| 178 | * r7: NULL |
| 179 | */ |
| 180 | (*kernel) ((bd_t *)of_flat_tree, (ulong)kernel, 0, 0, 0); |
| 181 | /* does not return */ |
| 182 | } |
| 183 | #endif |
| 184 | /* |
| 185 | * Linux Kernel Parameters (passing board info data): |
| 186 | * r3: ptr to board info data |
| 187 | * r4: initrd_start or 0 if no initrd |
| 188 | * r5: initrd_end - unused if r4 is 0 |
| 189 | * r6: Start of command line string |
| 190 | * r7: End of command line string |
| 191 | */ |
| 192 | (*kernel) (kbd, initrd_start, initrd_end, cmd_start, cmd_end); |
| 193 | /* does not return */ |
| 194 | } |
Marian Balakowicz | 28fb615 | 2008-01-31 13:20:08 +0100 | [diff] [blame] | 195 | |
Marian Balakowicz | 9c701e8 | 2008-01-31 13:57:17 +0100 | [diff] [blame] | 196 | static ulong get_sp (void) |
| 197 | { |
| 198 | ulong sp; |
| 199 | |
| 200 | asm( "mr %0,1": "=r"(sp) : ); |
| 201 | return sp; |
| 202 | } |
| 203 | |
Marian Balakowicz | 2efc264 | 2008-01-31 13:58:13 +0100 | [diff] [blame] | 204 | static void set_clocks_in_mhz (bd_t *kbd) |
| 205 | { |
| 206 | char *s; |
| 207 | |
| 208 | if ((s = getenv ("clocks_in_mhz")) != NULL) { |
| 209 | /* convert all clock information to MHz */ |
| 210 | kbd->bi_intfreq /= 1000000L; |
| 211 | kbd->bi_busfreq /= 1000000L; |
| 212 | #if defined(CONFIG_MPC8220) |
| 213 | kbd->bi_inpfreq /= 1000000L; |
| 214 | kbd->bi_pcifreq /= 1000000L; |
| 215 | kbd->bi_pevfreq /= 1000000L; |
| 216 | kbd->bi_flbfreq /= 1000000L; |
| 217 | kbd->bi_vcofreq /= 1000000L; |
| 218 | #endif |
| 219 | #if defined(CONFIG_CPM2) |
| 220 | kbd->bi_cpmfreq /= 1000000L; |
| 221 | kbd->bi_brgfreq /= 1000000L; |
| 222 | kbd->bi_sccfreq /= 1000000L; |
| 223 | kbd->bi_vco /= 1000000L; |
| 224 | #endif |
| 225 | #if defined(CONFIG_MPC5xxx) |
| 226 | kbd->bi_ipbfreq /= 1000000L; |
| 227 | kbd->bi_pcifreq /= 1000000L; |
| 228 | #endif /* CONFIG_MPC5xxx */ |
| 229 | } |
| 230 | } |
| 231 | |
Marian Balakowicz | 28fb615 | 2008-01-31 13:20:08 +0100 | [diff] [blame] | 232 | #if defined(CONFIG_OF_LIBFDT) |
| 233 | static void fdt_error (const char *msg) |
| 234 | { |
| 235 | puts ("ERROR: "); |
| 236 | puts (msg); |
| 237 | puts (" - must RESET the board to recover.\n"); |
| 238 | } |
Marian Balakowicz | 25154b6 | 2008-01-31 13:58:20 +0100 | [diff] [blame] | 239 | |
Marian Balakowicz | 351d3e3 | 2008-02-27 11:02:26 +0100 | [diff] [blame^] | 240 | static image_header_t *image_get_fdt (ulong fdt_addr) |
| 241 | { |
| 242 | image_header_t *fdt_hdr = (image_header_t *)fdt_addr; |
| 243 | |
| 244 | image_print_contents (fdt_hdr); |
| 245 | |
| 246 | puts (" Verifying Checksum ... "); |
| 247 | if (!image_check_hcrc (fdt_hdr)) { |
| 248 | fdt_error ("fdt header checksum invalid"); |
| 249 | return NULL; |
| 250 | } |
| 251 | |
| 252 | if (!image_check_dcrc (fdt_hdr)) { |
| 253 | fdt_error ("fdt checksum invalid"); |
| 254 | return NULL; |
| 255 | } |
| 256 | puts ("OK\n"); |
| 257 | |
| 258 | if (!image_check_type (fdt_hdr, IH_TYPE_FLATDT)) { |
| 259 | fdt_error ("uImage is not a fdt"); |
| 260 | return NULL; |
| 261 | } |
| 262 | if (image_get_comp (fdt_hdr) != IH_COMP_NONE) { |
| 263 | fdt_error ("uImage is compressed"); |
| 264 | return NULL; |
| 265 | } |
| 266 | if (fdt_check_header ((char *)image_get_data (fdt_hdr)) != 0) { |
| 267 | fdt_error ("uImage data is not a fdt"); |
| 268 | return NULL; |
| 269 | } |
| 270 | return fdt_hdr; |
| 271 | } |
| 272 | |
Marian Balakowicz | 6881dae | 2008-02-04 08:28:17 +0100 | [diff] [blame] | 273 | static void get_fdt (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[], |
| 274 | bootm_headers_t *images, char **of_flat_tree, ulong *of_size) |
Marian Balakowicz | 25154b6 | 2008-01-31 13:58:20 +0100 | [diff] [blame] | 275 | { |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 276 | ulong fdt_addr; |
Marian Balakowicz | 25154b6 | 2008-01-31 13:58:20 +0100 | [diff] [blame] | 277 | image_header_t *fdt_hdr; |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 278 | char *fdt_blob = NULL; |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 279 | ulong image_start, image_end; |
| 280 | ulong load_start, load_end; |
| 281 | #if defined(CONFIG_FIT) |
| 282 | void *fit_hdr; |
| 283 | const char *fit_uname_config = NULL; |
| 284 | const char *fit_uname_fdt = NULL; |
| 285 | ulong default_addr; |
| 286 | #endif |
Marian Balakowicz | 25154b6 | 2008-01-31 13:58:20 +0100 | [diff] [blame] | 287 | |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 288 | if (argc > 3) { |
| 289 | #if defined(CONFIG_FIT) |
| 290 | /* |
| 291 | * If the FDT blob comes from the FIT image and the FIT image |
| 292 | * address is omitted in the command line argument, try to use |
| 293 | * ramdisk or os FIT image address or default load address. |
| 294 | */ |
| 295 | if (images->fit_uname_rd) |
| 296 | default_addr = (ulong)images->fit_hdr_rd; |
| 297 | else if (images->fit_uname_os) |
| 298 | default_addr = (ulong)images->fit_hdr_os; |
| 299 | else |
| 300 | default_addr = load_addr; |
| 301 | |
| 302 | if (fit_parse_conf (argv[3], default_addr, |
| 303 | &fdt_addr, &fit_uname_config)) { |
| 304 | debug ("* fdt: config '%s' from image at 0x%08lx\n", |
| 305 | fit_uname_config, fdt_addr); |
| 306 | } else if (fit_parse_subimage (argv[3], default_addr, |
| 307 | &fdt_addr, &fit_uname_fdt)) { |
| 308 | debug ("* fdt: subimage '%s' from image at 0x%08lx\n", |
| 309 | fit_uname_fdt, fdt_addr); |
| 310 | } else |
| 311 | #endif |
| 312 | { |
| 313 | fdt_addr = simple_strtoul(argv[3], NULL, 16); |
| 314 | debug ("* fdt: cmdline image address = 0x%08lx\n", |
| 315 | fdt_addr); |
| 316 | } |
Marian Balakowicz | c536d6f | 2008-02-21 17:20:19 +0100 | [diff] [blame] | 317 | |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 318 | debug ("## Checking for 'FDT'/'FDT image' at %08lx\n", |
| 319 | fdt_addr); |
Marian Balakowicz | c536d6f | 2008-02-21 17:20:19 +0100 | [diff] [blame] | 320 | |
| 321 | /* copy from dataflash if needed */ |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 322 | fdt_addr = gen_get_image (fdt_addr); |
Marian Balakowicz | 25154b6 | 2008-01-31 13:58:20 +0100 | [diff] [blame] | 323 | |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 324 | /* |
| 325 | * Check if there is an FDT image at the |
| 326 | * address provided in the second bootm argument |
| 327 | * check image type, for FIT images get a FIT node. |
| 328 | */ |
| 329 | switch (gen_image_get_format ((void *)fdt_addr)) { |
| 330 | case IMAGE_FORMAT_LEGACY: |
| 331 | debug ("* fdt: legacy format image\n"); |
Marian Balakowicz | 25154b6 | 2008-01-31 13:58:20 +0100 | [diff] [blame] | 332 | |
Marian Balakowicz | 351d3e3 | 2008-02-27 11:02:26 +0100 | [diff] [blame^] | 333 | /* verify fdt_addr points to a valid image header */ |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 334 | printf ("## Flattened Device Tree Legacy Image at %08lx\n", |
Marian Balakowicz | 351d3e3 | 2008-02-27 11:02:26 +0100 | [diff] [blame^] | 335 | fdt_addr); |
| 336 | fdt_hdr = image_get_fdt (fdt_addr); |
| 337 | if (!fdt_hdr) |
| 338 | do_reset (cmdtp, flag, argc, argv); |
Marian Balakowicz | 25154b6 | 2008-01-31 13:58:20 +0100 | [diff] [blame] | 339 | |
Marian Balakowicz | 351d3e3 | 2008-02-27 11:02:26 +0100 | [diff] [blame^] | 340 | /* |
| 341 | * move image data to the load address, |
| 342 | * make sure we don't overwrite initial image |
| 343 | */ |
Marian Balakowicz | 25154b6 | 2008-01-31 13:58:20 +0100 | [diff] [blame] | 344 | image_start = (ulong)fdt_hdr; |
| 345 | image_end = image_get_image_end (fdt_hdr); |
| 346 | |
| 347 | load_start = image_get_load (fdt_hdr); |
| 348 | load_end = load_start + image_get_data_size (fdt_hdr); |
| 349 | |
| 350 | if ((load_start < image_end) && (load_end > image_start)) { |
| 351 | fdt_error ("fdt overwritten"); |
| 352 | do_reset (cmdtp, flag, argc, argv); |
| 353 | } |
Marian Balakowicz | 25154b6 | 2008-01-31 13:58:20 +0100 | [diff] [blame] | 354 | memmove ((void *)image_get_load (fdt_hdr), |
Marian Balakowicz | 351d3e3 | 2008-02-27 11:02:26 +0100 | [diff] [blame^] | 355 | (void *)image_get_data (fdt_hdr), |
| 356 | image_get_data_size (fdt_hdr)); |
Marian Balakowicz | 25154b6 | 2008-01-31 13:58:20 +0100 | [diff] [blame] | 357 | |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 358 | fdt_blob = (char *)image_get_load (fdt_hdr); |
| 359 | break; |
| 360 | #if defined(CONFIG_FIT) |
| 361 | case IMAGE_FORMAT_FIT: |
| 362 | |
| 363 | /* check FDT blob vs FIT hdr */ |
| 364 | if (fit_uname_config || fit_uname_fdt) { |
| 365 | /* |
| 366 | * FIT image |
| 367 | */ |
| 368 | fit_hdr = (void *)fdt_addr; |
| 369 | debug ("* fdt: FIT format image\n"); |
| 370 | fit_unsupported_reset ("PPC fdt"); |
| 371 | do_reset (cmdtp, flag, argc, argv); |
| 372 | } else { |
| 373 | /* |
| 374 | * FDT blob |
| 375 | */ |
| 376 | printf ("## Flattened Device Tree blob at %08lx\n", fdt_blob); |
| 377 | fdt_blob = (char *)fdt_addr; |
| 378 | } |
| 379 | break; |
| 380 | #endif |
| 381 | default: |
| 382 | fdt_error ("Did not find a cmdline Flattened Device Tree"); |
Marian Balakowicz | 25154b6 | 2008-01-31 13:58:20 +0100 | [diff] [blame] | 383 | do_reset (cmdtp, flag, argc, argv); |
| 384 | } |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 385 | |
| 386 | printf (" Booting using the fdt blob at 0x%x\n", fdt_blob); |
| 387 | |
| 388 | } else if (images->legacy_hdr_valid && |
| 389 | image_check_type (images->legacy_hdr_os, IH_TYPE_MULTI)) { |
| 390 | |
Marian Balakowicz | 25154b6 | 2008-01-31 13:58:20 +0100 | [diff] [blame] | 391 | ulong fdt_data, fdt_len; |
| 392 | |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 393 | /* |
| 394 | * Now check if we have a legacy multi-component image, |
| 395 | * get second entry data start address and len. |
| 396 | */ |
Marian Balakowicz | 25154b6 | 2008-01-31 13:58:20 +0100 | [diff] [blame] | 397 | printf ("## Flattened Device Tree from multi " |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 398 | "component Image at %08lX\n", |
| 399 | (ulong)images->legacy_hdr_os); |
Marian Balakowicz | 25154b6 | 2008-01-31 13:58:20 +0100 | [diff] [blame] | 400 | |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 401 | image_multi_getimg (images->legacy_hdr_os, 2, &fdt_data, &fdt_len); |
Marian Balakowicz | 25154b6 | 2008-01-31 13:58:20 +0100 | [diff] [blame] | 402 | if (fdt_len) { |
| 403 | |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 404 | fdt_blob = (char *)fdt_data; |
| 405 | printf (" Booting using the fdt at 0x%x\n", fdt_blob); |
Marian Balakowicz | 25154b6 | 2008-01-31 13:58:20 +0100 | [diff] [blame] | 406 | |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 407 | if (fdt_check_header (fdt_blob) != 0) { |
Marian Balakowicz | 25154b6 | 2008-01-31 13:58:20 +0100 | [diff] [blame] | 408 | fdt_error ("image is not a fdt"); |
| 409 | do_reset (cmdtp, flag, argc, argv); |
| 410 | } |
| 411 | |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 412 | if (be32_to_cpu (fdt_totalsize (fdt_blob)) != fdt_len) { |
Marian Balakowicz | 25154b6 | 2008-01-31 13:58:20 +0100 | [diff] [blame] | 413 | fdt_error ("fdt size != image size"); |
| 414 | do_reset (cmdtp, flag, argc, argv); |
| 415 | } |
| 416 | } else { |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 417 | fdt_error ("Did not find a Flattened Device Tree " |
| 418 | "in a legacy multi-component image"); |
| 419 | do_reset (cmdtp, flag, argc, argv); |
Marian Balakowicz | 25154b6 | 2008-01-31 13:58:20 +0100 | [diff] [blame] | 420 | } |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 421 | } else { |
| 422 | debug ("## No Flattened Device Tree\n"); |
| 423 | *of_flat_tree = NULL; |
Marian Balakowicz | 6881dae | 2008-02-04 08:28:17 +0100 | [diff] [blame] | 424 | *of_size = 0; |
| 425 | return; |
| 426 | } |
| 427 | |
| 428 | *of_flat_tree = fdt_blob; |
| 429 | *of_size = be32_to_cpu (fdt_totalsize (fdt_blob)); |
| 430 | debug (" of_flat_tree at 0x%08lx size 0x%08lx\n", |
| 431 | *of_flat_tree, *of_size); |
| 432 | } |
| 433 | |
| 434 | static ulong fdt_relocate (ulong alloc_current, |
| 435 | cmd_tbl_t *cmdtp, int flag, int argc, char *argv[], |
| 436 | char **of_flat_tree, ulong *of_size) |
| 437 | { |
| 438 | char *fdt_blob = *of_flat_tree; |
| 439 | ulong relocate = 0; |
| 440 | ulong new_alloc_current; |
| 441 | |
| 442 | /* nothing to do */ |
| 443 | if (*of_size == 0) |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 444 | return alloc_current; |
Marian Balakowicz | 6881dae | 2008-02-04 08:28:17 +0100 | [diff] [blame] | 445 | |
| 446 | if (fdt_check_header (fdt_blob) != 0) { |
| 447 | fdt_error ("image is not a fdt"); |
| 448 | do_reset (cmdtp, flag, argc, argv); |
Marian Balakowicz | 25154b6 | 2008-01-31 13:58:20 +0100 | [diff] [blame] | 449 | } |
| 450 | |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 451 | #ifndef CFG_NO_FLASH |
Marian Balakowicz | 6881dae | 2008-02-04 08:28:17 +0100 | [diff] [blame] | 452 | /* move the blob if it is in flash (set relocate) */ |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 453 | if (addr2info ((ulong)fdt_blob) != NULL) |
Marian Balakowicz | 6881dae | 2008-02-04 08:28:17 +0100 | [diff] [blame] | 454 | relocate = 1; |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 455 | #endif |
| 456 | |
Marian Balakowicz | 25154b6 | 2008-01-31 13:58:20 +0100 | [diff] [blame] | 457 | #ifdef CFG_BOOTMAPSZ |
| 458 | /* |
| 459 | * The blob must be within CFG_BOOTMAPSZ, |
| 460 | * so we flag it to be copied if it is not. |
| 461 | */ |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 462 | if (fdt_blob >= (char *)CFG_BOOTMAPSZ) |
Marian Balakowicz | 6881dae | 2008-02-04 08:28:17 +0100 | [diff] [blame] | 463 | relocate = 1; |
Marian Balakowicz | 25154b6 | 2008-01-31 13:58:20 +0100 | [diff] [blame] | 464 | #endif |
| 465 | |
| 466 | /* move flattend device tree if needed */ |
Marian Balakowicz | 6881dae | 2008-02-04 08:28:17 +0100 | [diff] [blame] | 467 | if (relocate) { |
Marian Balakowicz | 25154b6 | 2008-01-31 13:58:20 +0100 | [diff] [blame] | 468 | int err; |
| 469 | ulong of_start, of_len; |
| 470 | |
Marian Balakowicz | 6881dae | 2008-02-04 08:28:17 +0100 | [diff] [blame] | 471 | of_len = *of_size; |
Marian Balakowicz | 25154b6 | 2008-01-31 13:58:20 +0100 | [diff] [blame] | 472 | |
| 473 | /* position on a 4K boundary before the alloc_current */ |
| 474 | of_start = alloc_current - of_len; |
| 475 | of_start &= ~(4096 - 1); /* align on page */ |
| 476 | |
| 477 | debug ("## device tree at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n", |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 478 | (ulong)fdt_blob, (ulong)fdt_blob + of_len - 1, |
Marian Balakowicz | 25154b6 | 2008-01-31 13:58:20 +0100 | [diff] [blame] | 479 | of_len, of_len); |
| 480 | |
| 481 | printf (" Loading Device Tree to %08lx, end %08lx ... ", |
| 482 | of_start, of_start + of_len - 1); |
| 483 | |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 484 | err = fdt_open_into (fdt_blob, (void *)of_start, of_len); |
Marian Balakowicz | 25154b6 | 2008-01-31 13:58:20 +0100 | [diff] [blame] | 485 | if (err != 0) { |
| 486 | fdt_error ("fdt move failed"); |
| 487 | do_reset (cmdtp, flag, argc, argv); |
| 488 | } |
| 489 | puts ("OK\n"); |
| 490 | |
| 491 | *of_flat_tree = (char *)of_start; |
| 492 | new_alloc_current = of_start; |
| 493 | } else { |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 494 | *of_flat_tree = fdt_blob; |
Marian Balakowicz | 25154b6 | 2008-01-31 13:58:20 +0100 | [diff] [blame] | 495 | new_alloc_current = alloc_current; |
| 496 | } |
| 497 | |
| 498 | return new_alloc_current; |
| 499 | } |
Marian Balakowicz | 28fb615 | 2008-01-31 13:20:08 +0100 | [diff] [blame] | 500 | #endif |