Marian Balakowicz | 5d3cc55 | 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 | 7582438 | 2008-01-31 13:20:06 +0100 | [diff] [blame] | 26 | #define DEBUG |
| 27 | |
Marian Balakowicz | 5d3cc55 | 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 | d3c5eb6 | 2008-01-31 13:20:08 +0100 | [diff] [blame] | 42 | |
| 43 | static void fdt_error (const char *msg); |
Kumar Gala | 274cea2 | 2008-02-27 21:51:46 -0600 | [diff] [blame^] | 44 | static int get_fdt (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[], |
Marian Balakowicz | d2ced9e | 2008-02-04 08:28:17 +0100 | [diff] [blame] | 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 | 5d3cc55 | 2008-01-08 18:11:43 +0100 | [diff] [blame] | 49 | #endif |
| 50 | |
Marian Balakowicz | 5d3cc55 | 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 | 5d3cc55 | 2008-01-08 18:11:43 +0100 | [diff] [blame] | 55 | DECLARE_GLOBAL_DATA_PTR; |
Marian Balakowicz | 5d3cc55 | 2008-01-08 18:11:43 +0100 | [diff] [blame] | 56 | |
Marian Balakowicz | 5d3cc55 | 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 | ceaed2b | 2008-01-31 13:57:17 +0100 | [diff] [blame] | 58 | static ulong get_sp (void); |
Marian Balakowicz | b6b0fe6 | 2008-01-31 13:58:13 +0100 | [diff] [blame] | 59 | static void set_clocks_in_mhz (bd_t *kbd); |
Marian Balakowicz | 5d3cc55 | 2008-01-08 18:11:43 +0100 | [diff] [blame] | 60 | |
| 61 | void __attribute__((noinline)) |
Marian Balakowicz | 8a5ea3e | 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 | 5d3cc55 | 2008-01-08 18:11:43 +0100 | [diff] [blame] | 64 | { |
Marian Balakowicz | b6b0fe6 | 2008-01-31 13:58:13 +0100 | [diff] [blame] | 65 | ulong sp, sp_limit, alloc_current; |
| 66 | |
Marian Balakowicz | f13e7b2 | 2008-01-08 18:12:17 +0100 | [diff] [blame] | 67 | ulong initrd_start, initrd_end; |
Marian Balakowicz | 5ad03eb | 2008-01-31 13:55:39 +0100 | [diff] [blame] | 68 | ulong rd_data_start, rd_data_end, rd_len; |
Marian Balakowicz | f13e7b2 | 2008-01-08 18:12:17 +0100 | [diff] [blame] | 69 | |
| 70 | ulong cmd_start, cmd_end; |
Marian Balakowicz | 5d3cc55 | 2008-01-08 18:11:43 +0100 | [diff] [blame] | 71 | bd_t *kbd; |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 72 | ulong ep = 0; |
Marian Balakowicz | 5d3cc55 | 2008-01-08 18:11:43 +0100 | [diff] [blame] | 73 | void (*kernel)(bd_t *, ulong, ulong, ulong, ulong); |
Kumar Gala | d2bc095 | 2008-02-27 21:51:45 -0600 | [diff] [blame] | 74 | int ret; |
Kumar Gala | 2795349 | 2008-02-27 21:51:44 -0600 | [diff] [blame] | 75 | ulong of_size = 0; |
Marian Balakowicz | f13e7b2 | 2008-01-08 18:12:17 +0100 | [diff] [blame] | 76 | |
Marian Balakowicz | 4a2ad5f | 2008-01-31 13:20:07 +0100 | [diff] [blame] | 77 | #if defined(CONFIG_OF_LIBFDT) |
Marian Balakowicz | d2ced9e | 2008-02-04 08:28:17 +0100 | [diff] [blame] | 78 | char *of_flat_tree = NULL; |
Marian Balakowicz | 5d3cc55 | 2008-01-08 18:11:43 +0100 | [diff] [blame] | 79 | #endif |
| 80 | |
Marian Balakowicz | 5d3cc55 | 2008-01-08 18:11:43 +0100 | [diff] [blame] | 81 | /* |
| 82 | * Booting a (Linux) kernel image |
| 83 | * |
| 84 | * Allocate space for command line and board info - the |
| 85 | * address should be as high as possible within the reach of |
| 86 | * the kernel (see CFG_BOOTMAPSZ settings), but in unused |
| 87 | * memory, which means far enough below the current stack |
| 88 | * pointer. |
| 89 | */ |
Marian Balakowicz | b6b0fe6 | 2008-01-31 13:58:13 +0100 | [diff] [blame] | 90 | sp = get_sp(); |
| 91 | debug ("## Current stack ends at 0x%08lx ", sp); |
Marian Balakowicz | 5d3cc55 | 2008-01-08 18:11:43 +0100 | [diff] [blame] | 92 | |
Marian Balakowicz | b6b0fe6 | 2008-01-31 13:58:13 +0100 | [diff] [blame] | 93 | alloc_current = sp_limit = get_boot_sp_limit(sp); |
| 94 | debug ("=> set upper limit to 0x%08lx\n", sp_limit); |
Marian Balakowicz | 5d3cc55 | 2008-01-08 18:11:43 +0100 | [diff] [blame] | 95 | |
Kumar Gala | 2795349 | 2008-02-27 21:51:44 -0600 | [diff] [blame] | 96 | #if defined(CONFIG_OF_LIBFDT) |
| 97 | /* find flattened device tree */ |
Kumar Gala | 274cea2 | 2008-02-27 21:51:46 -0600 | [diff] [blame^] | 98 | ret = get_fdt (cmdtp, flag, argc, argv, images, &of_flat_tree, &of_size); |
| 99 | |
| 100 | if (ret) |
| 101 | goto error; |
Kumar Gala | 2795349 | 2008-02-27 21:51:44 -0600 | [diff] [blame] | 102 | #endif |
Marian Balakowicz | 5d3cc55 | 2008-01-08 18:11:43 +0100 | [diff] [blame] | 103 | |
Kumar Gala | 2795349 | 2008-02-27 21:51:44 -0600 | [diff] [blame] | 104 | if (!of_size) { |
| 105 | /* allocate space and init command line */ |
| 106 | alloc_current = get_boot_cmdline (alloc_current, &cmd_start, &cmd_end); |
| 107 | |
| 108 | /* allocate space for kernel copy of board info */ |
| 109 | alloc_current = get_boot_kbd (alloc_current, &kbd); |
| 110 | set_clocks_in_mhz(kbd); |
| 111 | } |
Marian Balakowicz | 5d3cc55 | 2008-01-08 18:11:43 +0100 | [diff] [blame] | 112 | |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 113 | /* find kernel entry point */ |
| 114 | if (images->legacy_hdr_valid) { |
| 115 | ep = image_get_ep (images->legacy_hdr_os); |
| 116 | #if defined(CONFIG_FIT) |
| 117 | } else if (images->fit_uname_os) { |
| 118 | fit_unsupported_reset ("PPC linux bootm"); |
Kumar Gala | 274cea2 | 2008-02-27 21:51:46 -0600 | [diff] [blame^] | 119 | goto error; |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 120 | #endif |
| 121 | } else { |
| 122 | puts ("Could not find kernel entry point!\n"); |
Kumar Gala | 274cea2 | 2008-02-27 21:51:46 -0600 | [diff] [blame^] | 123 | goto error; |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 124 | } |
| 125 | kernel = (void (*)(bd_t *, ulong, ulong, ulong, ulong))ep; |
Marian Balakowicz | 5d3cc55 | 2008-01-08 18:11:43 +0100 | [diff] [blame] | 126 | |
Marian Balakowicz | ceaed2b | 2008-01-31 13:57:17 +0100 | [diff] [blame] | 127 | /* find ramdisk */ |
Kumar Gala | 274cea2 | 2008-02-27 21:51:46 -0600 | [diff] [blame^] | 128 | ret = get_ramdisk (cmdtp, flag, argc, argv, images, |
Marian Balakowicz | 5ad03eb | 2008-01-31 13:55:39 +0100 | [diff] [blame] | 129 | IH_ARCH_PPC, &rd_data_start, &rd_data_end); |
Marian Balakowicz | ceaed2b | 2008-01-31 13:57:17 +0100 | [diff] [blame] | 130 | |
Kumar Gala | 274cea2 | 2008-02-27 21:51:46 -0600 | [diff] [blame^] | 131 | if (ret) |
| 132 | goto error; |
| 133 | |
Marian Balakowicz | 5ad03eb | 2008-01-31 13:55:39 +0100 | [diff] [blame] | 134 | rd_len = rd_data_end - rd_data_start; |
Marian Balakowicz | 5d3cc55 | 2008-01-08 18:11:43 +0100 | [diff] [blame] | 135 | |
Marian Balakowicz | 4a2ad5f | 2008-01-31 13:20:07 +0100 | [diff] [blame] | 136 | #if defined(CONFIG_OF_LIBFDT) |
Marian Balakowicz | d2ced9e | 2008-02-04 08:28:17 +0100 | [diff] [blame] | 137 | alloc_current = fdt_relocate (alloc_current, |
| 138 | cmdtp, flag, argc, argv, &of_flat_tree, &of_size); |
Marian Balakowicz | 4a2ad5f | 2008-01-31 13:20:07 +0100 | [diff] [blame] | 139 | |
Marian Balakowicz | 5d3cc55 | 2008-01-08 18:11:43 +0100 | [diff] [blame] | 140 | /* |
| 141 | * Add the chosen node if it doesn't exist, add the env and bd_t |
| 142 | * if the user wants it (the logic is in the subroutines). |
| 143 | */ |
Marian Balakowicz | d2ced9e | 2008-02-04 08:28:17 +0100 | [diff] [blame] | 144 | if (of_size) { |
Kumar Gala | d2bc095 | 2008-02-27 21:51:45 -0600 | [diff] [blame] | 145 | /* pass in dummy initrd info, we'll fix up later */ |
| 146 | if (fdt_chosen(of_flat_tree, rd_data_start, rd_data_end, 0) < 0) { |
Marian Balakowicz | d3c5eb6 | 2008-01-31 13:20:08 +0100 | [diff] [blame] | 147 | fdt_error ("/chosen node create failed"); |
Kumar Gala | 274cea2 | 2008-02-27 21:51:46 -0600 | [diff] [blame^] | 148 | goto error; |
Marian Balakowicz | 5d3cc55 | 2008-01-08 18:11:43 +0100 | [diff] [blame] | 149 | } |
| 150 | #ifdef CONFIG_OF_HAS_UBOOT_ENV |
| 151 | if (fdt_env(of_flat_tree) < 0) { |
Marian Balakowicz | d3c5eb6 | 2008-01-31 13:20:08 +0100 | [diff] [blame] | 152 | fdt_error ("/u-boot-env node create failed"); |
Kumar Gala | 274cea2 | 2008-02-27 21:51:46 -0600 | [diff] [blame^] | 153 | goto error; |
Marian Balakowicz | 5d3cc55 | 2008-01-08 18:11:43 +0100 | [diff] [blame] | 154 | } |
| 155 | #endif |
| 156 | #ifdef CONFIG_OF_HAS_BD_T |
| 157 | if (fdt_bd_t(of_flat_tree) < 0) { |
Marian Balakowicz | d3c5eb6 | 2008-01-31 13:20:08 +0100 | [diff] [blame] | 158 | fdt_error ("/bd_t node create failed"); |
Kumar Gala | 274cea2 | 2008-02-27 21:51:46 -0600 | [diff] [blame^] | 159 | goto error; |
Marian Balakowicz | 5d3cc55 | 2008-01-08 18:11:43 +0100 | [diff] [blame] | 160 | } |
| 161 | #endif |
| 162 | #ifdef CONFIG_OF_BOARD_SETUP |
| 163 | /* Call the board-specific fixup routine */ |
| 164 | ft_board_setup(of_flat_tree, gd->bd); |
| 165 | #endif |
| 166 | } |
Marian Balakowicz | 4a2ad5f | 2008-01-31 13:20:07 +0100 | [diff] [blame] | 167 | #endif /* CONFIG_OF_LIBFDT */ |
Marian Balakowicz | d45d5a1 | 2008-01-08 18:11:43 +0100 | [diff] [blame] | 168 | |
Kumar Gala | d2bc095 | 2008-02-27 21:51:45 -0600 | [diff] [blame] | 169 | alloc_current = ramdisk_high (alloc_current, rd_data_start, rd_len, |
| 170 | sp_limit, get_sp (), &initrd_start, &initrd_end); |
| 171 | |
| 172 | #if defined(CONFIG_OF_LIBFDT) |
| 173 | /* fixup the initrd now that we know where it should be */ |
| 174 | if ((of_flat_tree) && (initrd_start && initrd_end)) { |
| 175 | uint64_t addr, size; |
| 176 | int total = fdt_num_mem_rsv(of_flat_tree); |
| 177 | int j; |
| 178 | |
| 179 | /* Look for the dummy entry and delete it */ |
| 180 | for (j = 0; j < total; j++) { |
| 181 | fdt_get_mem_rsv(of_flat_tree, j, &addr, &size); |
| 182 | if (addr == rd_data_start) { |
| 183 | fdt_del_mem_rsv(of_flat_tree, j); |
| 184 | break; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | ret = fdt_add_mem_rsv(of_flat_tree, initrd_start, |
| 189 | initrd_end - initrd_start + 1); |
| 190 | if (ret < 0) { |
| 191 | printf("fdt_chosen: %s\n", fdt_strerror(ret)); |
Kumar Gala | 274cea2 | 2008-02-27 21:51:46 -0600 | [diff] [blame^] | 192 | goto error; |
Kumar Gala | d2bc095 | 2008-02-27 21:51:45 -0600 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | do_fixup_by_path_u32(of_flat_tree, "/chosen", |
| 196 | "linux,initrd-start", initrd_start, 0); |
| 197 | do_fixup_by_path_u32(of_flat_tree, "/chosen", |
| 198 | "linux,initrd-end", initrd_end, 0); |
| 199 | } |
| 200 | #endif |
Marian Balakowicz | 5d3cc55 | 2008-01-08 18:11:43 +0100 | [diff] [blame] | 201 | debug ("## Transferring control to Linux (at address %08lx) ...\n", |
| 202 | (ulong)kernel); |
| 203 | |
| 204 | show_boot_progress (15); |
| 205 | |
| 206 | #if defined(CFG_INIT_RAM_LOCK) && !defined(CONFIG_E500) |
| 207 | unlock_ram_in_cache(); |
| 208 | #endif |
| 209 | |
Marian Balakowicz | 4a2ad5f | 2008-01-31 13:20:07 +0100 | [diff] [blame] | 210 | #if defined(CONFIG_OF_LIBFDT) |
Marian Balakowicz | 5d3cc55 | 2008-01-08 18:11:43 +0100 | [diff] [blame] | 211 | if (of_flat_tree) { /* device tree; boot new style */ |
| 212 | /* |
| 213 | * Linux Kernel Parameters (passing device tree): |
| 214 | * r3: pointer to the fdt, followed by the board info data |
| 215 | * r4: physical pointer to the kernel itself |
| 216 | * r5: NULL |
| 217 | * r6: NULL |
| 218 | * r7: NULL |
| 219 | */ |
| 220 | (*kernel) ((bd_t *)of_flat_tree, (ulong)kernel, 0, 0, 0); |
| 221 | /* does not return */ |
| 222 | } |
| 223 | #endif |
| 224 | /* |
| 225 | * Linux Kernel Parameters (passing board info data): |
| 226 | * r3: ptr to board info data |
| 227 | * r4: initrd_start or 0 if no initrd |
| 228 | * r5: initrd_end - unused if r4 is 0 |
| 229 | * r6: Start of command line string |
| 230 | * r7: End of command line string |
| 231 | */ |
| 232 | (*kernel) (kbd, initrd_start, initrd_end, cmd_start, cmd_end); |
| 233 | /* does not return */ |
Kumar Gala | 274cea2 | 2008-02-27 21:51:46 -0600 | [diff] [blame^] | 234 | return ; |
| 235 | |
| 236 | error: |
| 237 | do_reset (cmdtp, flag, argc, argv); |
| 238 | return ; |
Marian Balakowicz | 5d3cc55 | 2008-01-08 18:11:43 +0100 | [diff] [blame] | 239 | } |
Marian Balakowicz | d3c5eb6 | 2008-01-31 13:20:08 +0100 | [diff] [blame] | 240 | |
Marian Balakowicz | ceaed2b | 2008-01-31 13:57:17 +0100 | [diff] [blame] | 241 | static ulong get_sp (void) |
| 242 | { |
| 243 | ulong sp; |
| 244 | |
| 245 | asm( "mr %0,1": "=r"(sp) : ); |
| 246 | return sp; |
| 247 | } |
| 248 | |
Marian Balakowicz | b6b0fe6 | 2008-01-31 13:58:13 +0100 | [diff] [blame] | 249 | static void set_clocks_in_mhz (bd_t *kbd) |
| 250 | { |
| 251 | char *s; |
| 252 | |
| 253 | if ((s = getenv ("clocks_in_mhz")) != NULL) { |
| 254 | /* convert all clock information to MHz */ |
| 255 | kbd->bi_intfreq /= 1000000L; |
| 256 | kbd->bi_busfreq /= 1000000L; |
| 257 | #if defined(CONFIG_MPC8220) |
| 258 | kbd->bi_inpfreq /= 1000000L; |
| 259 | kbd->bi_pcifreq /= 1000000L; |
| 260 | kbd->bi_pevfreq /= 1000000L; |
| 261 | kbd->bi_flbfreq /= 1000000L; |
| 262 | kbd->bi_vcofreq /= 1000000L; |
| 263 | #endif |
| 264 | #if defined(CONFIG_CPM2) |
| 265 | kbd->bi_cpmfreq /= 1000000L; |
| 266 | kbd->bi_brgfreq /= 1000000L; |
| 267 | kbd->bi_sccfreq /= 1000000L; |
| 268 | kbd->bi_vco /= 1000000L; |
| 269 | #endif |
| 270 | #if defined(CONFIG_MPC5xxx) |
| 271 | kbd->bi_ipbfreq /= 1000000L; |
| 272 | kbd->bi_pcifreq /= 1000000L; |
| 273 | #endif /* CONFIG_MPC5xxx */ |
| 274 | } |
| 275 | } |
| 276 | |
Marian Balakowicz | d3c5eb6 | 2008-01-31 13:20:08 +0100 | [diff] [blame] | 277 | #if defined(CONFIG_OF_LIBFDT) |
| 278 | static void fdt_error (const char *msg) |
| 279 | { |
| 280 | puts ("ERROR: "); |
| 281 | puts (msg); |
| 282 | puts (" - must RESET the board to recover.\n"); |
| 283 | } |
Marian Balakowicz | 7b32545 | 2008-01-31 13:58:20 +0100 | [diff] [blame] | 284 | |
Marian Balakowicz | ff0734c | 2008-02-27 11:02:26 +0100 | [diff] [blame] | 285 | static image_header_t *image_get_fdt (ulong fdt_addr) |
| 286 | { |
| 287 | image_header_t *fdt_hdr = (image_header_t *)fdt_addr; |
| 288 | |
| 289 | image_print_contents (fdt_hdr); |
| 290 | |
| 291 | puts (" Verifying Checksum ... "); |
| 292 | if (!image_check_hcrc (fdt_hdr)) { |
| 293 | fdt_error ("fdt header checksum invalid"); |
| 294 | return NULL; |
| 295 | } |
| 296 | |
| 297 | if (!image_check_dcrc (fdt_hdr)) { |
| 298 | fdt_error ("fdt checksum invalid"); |
| 299 | return NULL; |
| 300 | } |
| 301 | puts ("OK\n"); |
| 302 | |
| 303 | if (!image_check_type (fdt_hdr, IH_TYPE_FLATDT)) { |
| 304 | fdt_error ("uImage is not a fdt"); |
| 305 | return NULL; |
| 306 | } |
| 307 | if (image_get_comp (fdt_hdr) != IH_COMP_NONE) { |
| 308 | fdt_error ("uImage is compressed"); |
| 309 | return NULL; |
| 310 | } |
| 311 | if (fdt_check_header ((char *)image_get_data (fdt_hdr)) != 0) { |
| 312 | fdt_error ("uImage data is not a fdt"); |
| 313 | return NULL; |
| 314 | } |
| 315 | return fdt_hdr; |
| 316 | } |
| 317 | |
Kumar Gala | 274cea2 | 2008-02-27 21:51:46 -0600 | [diff] [blame^] | 318 | static int get_fdt (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[], |
Marian Balakowicz | d2ced9e | 2008-02-04 08:28:17 +0100 | [diff] [blame] | 319 | bootm_headers_t *images, char **of_flat_tree, ulong *of_size) |
Marian Balakowicz | 7b32545 | 2008-01-31 13:58:20 +0100 | [diff] [blame] | 320 | { |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 321 | ulong fdt_addr; |
Marian Balakowicz | 7b32545 | 2008-01-31 13:58:20 +0100 | [diff] [blame] | 322 | image_header_t *fdt_hdr; |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 323 | char *fdt_blob = NULL; |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 324 | ulong image_start, image_end; |
| 325 | ulong load_start, load_end; |
| 326 | #if defined(CONFIG_FIT) |
| 327 | void *fit_hdr; |
| 328 | const char *fit_uname_config = NULL; |
| 329 | const char *fit_uname_fdt = NULL; |
| 330 | ulong default_addr; |
| 331 | #endif |
Marian Balakowicz | 7b32545 | 2008-01-31 13:58:20 +0100 | [diff] [blame] | 332 | |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 333 | if (argc > 3) { |
| 334 | #if defined(CONFIG_FIT) |
| 335 | /* |
| 336 | * If the FDT blob comes from the FIT image and the FIT image |
| 337 | * address is omitted in the command line argument, try to use |
| 338 | * ramdisk or os FIT image address or default load address. |
| 339 | */ |
| 340 | if (images->fit_uname_rd) |
| 341 | default_addr = (ulong)images->fit_hdr_rd; |
| 342 | else if (images->fit_uname_os) |
| 343 | default_addr = (ulong)images->fit_hdr_os; |
| 344 | else |
| 345 | default_addr = load_addr; |
Marian Balakowicz | fff888a1 | 2008-02-21 17:20:19 +0100 | [diff] [blame] | 346 | |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 347 | if (fit_parse_conf (argv[3], default_addr, |
| 348 | &fdt_addr, &fit_uname_config)) { |
| 349 | debug ("* fdt: config '%s' from image at 0x%08lx\n", |
| 350 | fit_uname_config, fdt_addr); |
| 351 | } else if (fit_parse_subimage (argv[3], default_addr, |
| 352 | &fdt_addr, &fit_uname_fdt)) { |
| 353 | debug ("* fdt: subimage '%s' from image at 0x%08lx\n", |
| 354 | fit_uname_fdt, fdt_addr); |
| 355 | } else |
| 356 | #endif |
| 357 | { |
| 358 | fdt_addr = simple_strtoul(argv[3], NULL, 16); |
| 359 | debug ("* fdt: cmdline image address = 0x%08lx\n", |
| 360 | fdt_addr); |
| 361 | } |
| 362 | |
| 363 | debug ("## Checking for 'FDT'/'FDT image' at %08lx\n", |
| 364 | fdt_addr); |
Marian Balakowicz | fff888a1 | 2008-02-21 17:20:19 +0100 | [diff] [blame] | 365 | |
| 366 | /* copy from dataflash if needed */ |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 367 | fdt_addr = gen_get_image (fdt_addr); |
Marian Balakowicz | 7b32545 | 2008-01-31 13:58:20 +0100 | [diff] [blame] | 368 | |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 369 | /* |
| 370 | * Check if there is an FDT image at the |
| 371 | * address provided in the second bootm argument |
| 372 | * check image type, for FIT images get a FIT node. |
| 373 | */ |
| 374 | switch (gen_image_get_format ((void *)fdt_addr)) { |
| 375 | case IMAGE_FORMAT_LEGACY: |
| 376 | debug ("* fdt: legacy format image\n"); |
Marian Balakowicz | 7b32545 | 2008-01-31 13:58:20 +0100 | [diff] [blame] | 377 | |
Marian Balakowicz | ff0734c | 2008-02-27 11:02:26 +0100 | [diff] [blame] | 378 | /* verify fdt_addr points to a valid image header */ |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 379 | printf ("## Flattened Device Tree Legacy Image at %08lx\n", |
Marian Balakowicz | ff0734c | 2008-02-27 11:02:26 +0100 | [diff] [blame] | 380 | fdt_addr); |
| 381 | fdt_hdr = image_get_fdt (fdt_addr); |
| 382 | if (!fdt_hdr) |
Kumar Gala | 274cea2 | 2008-02-27 21:51:46 -0600 | [diff] [blame^] | 383 | goto error; |
Marian Balakowicz | 7b32545 | 2008-01-31 13:58:20 +0100 | [diff] [blame] | 384 | |
Marian Balakowicz | ff0734c | 2008-02-27 11:02:26 +0100 | [diff] [blame] | 385 | /* |
| 386 | * move image data to the load address, |
| 387 | * make sure we don't overwrite initial image |
| 388 | */ |
Marian Balakowicz | 7b32545 | 2008-01-31 13:58:20 +0100 | [diff] [blame] | 389 | image_start = (ulong)fdt_hdr; |
| 390 | image_end = image_get_image_end (fdt_hdr); |
| 391 | |
| 392 | load_start = image_get_load (fdt_hdr); |
| 393 | load_end = load_start + image_get_data_size (fdt_hdr); |
| 394 | |
| 395 | if ((load_start < image_end) && (load_end > image_start)) { |
| 396 | fdt_error ("fdt overwritten"); |
Kumar Gala | 274cea2 | 2008-02-27 21:51:46 -0600 | [diff] [blame^] | 397 | goto error; |
Marian Balakowicz | 7b32545 | 2008-01-31 13:58:20 +0100 | [diff] [blame] | 398 | } |
Marian Balakowicz | 7b32545 | 2008-01-31 13:58:20 +0100 | [diff] [blame] | 399 | memmove ((void *)image_get_load (fdt_hdr), |
Marian Balakowicz | ff0734c | 2008-02-27 11:02:26 +0100 | [diff] [blame] | 400 | (void *)image_get_data (fdt_hdr), |
| 401 | image_get_data_size (fdt_hdr)); |
Marian Balakowicz | 7b32545 | 2008-01-31 13:58:20 +0100 | [diff] [blame] | 402 | |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 403 | fdt_blob = (char *)image_get_load (fdt_hdr); |
| 404 | break; |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 405 | case IMAGE_FORMAT_FIT: |
Marian Balakowicz | 4efbe9d | 2008-02-27 11:02:26 +0100 | [diff] [blame] | 406 | /* |
| 407 | * This case will catch both: new uImage format |
| 408 | * (libfdt based) and raw FDT blob (also libfdt |
| 409 | * based). |
| 410 | */ |
| 411 | #if defined(CONFIG_FIT) |
| 412 | /* check FDT blob vs FIT blob */ |
| 413 | if (0) { /* FIXME: call FIT format verification */ |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 414 | /* |
| 415 | * FIT image |
| 416 | */ |
| 417 | fit_hdr = (void *)fdt_addr; |
| 418 | debug ("* fdt: FIT format image\n"); |
| 419 | fit_unsupported_reset ("PPC fdt"); |
Kumar Gala | 274cea2 | 2008-02-27 21:51:46 -0600 | [diff] [blame^] | 420 | goto error; |
Marian Balakowicz | 4efbe9d | 2008-02-27 11:02:26 +0100 | [diff] [blame] | 421 | } else |
| 422 | #endif |
| 423 | { |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 424 | /* |
| 425 | * FDT blob |
| 426 | */ |
Marian Balakowicz | 4efbe9d | 2008-02-27 11:02:26 +0100 | [diff] [blame] | 427 | debug ("* fdt: raw FDT blob\n"); |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 428 | printf ("## Flattened Device Tree blob at %08lx\n", fdt_blob); |
| 429 | fdt_blob = (char *)fdt_addr; |
| 430 | } |
| 431 | break; |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 432 | default: |
| 433 | fdt_error ("Did not find a cmdline Flattened Device Tree"); |
Kumar Gala | 274cea2 | 2008-02-27 21:51:46 -0600 | [diff] [blame^] | 434 | goto error; |
Marian Balakowicz | 7b32545 | 2008-01-31 13:58:20 +0100 | [diff] [blame] | 435 | } |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 436 | |
| 437 | printf (" Booting using the fdt blob at 0x%x\n", fdt_blob); |
| 438 | |
| 439 | } else if (images->legacy_hdr_valid && |
| 440 | image_check_type (images->legacy_hdr_os, IH_TYPE_MULTI)) { |
| 441 | |
Marian Balakowicz | 7b32545 | 2008-01-31 13:58:20 +0100 | [diff] [blame] | 442 | ulong fdt_data, fdt_len; |
| 443 | |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 444 | /* |
| 445 | * Now check if we have a legacy multi-component image, |
| 446 | * get second entry data start address and len. |
| 447 | */ |
Marian Balakowicz | 7b32545 | 2008-01-31 13:58:20 +0100 | [diff] [blame] | 448 | printf ("## Flattened Device Tree from multi " |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 449 | "component Image at %08lX\n", |
| 450 | (ulong)images->legacy_hdr_os); |
Marian Balakowicz | 7b32545 | 2008-01-31 13:58:20 +0100 | [diff] [blame] | 451 | |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 452 | image_multi_getimg (images->legacy_hdr_os, 2, &fdt_data, &fdt_len); |
Marian Balakowicz | 7b32545 | 2008-01-31 13:58:20 +0100 | [diff] [blame] | 453 | if (fdt_len) { |
| 454 | |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 455 | fdt_blob = (char *)fdt_data; |
| 456 | printf (" Booting using the fdt at 0x%x\n", fdt_blob); |
Marian Balakowicz | 7b32545 | 2008-01-31 13:58:20 +0100 | [diff] [blame] | 457 | |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 458 | if (fdt_check_header (fdt_blob) != 0) { |
Marian Balakowicz | 7b32545 | 2008-01-31 13:58:20 +0100 | [diff] [blame] | 459 | fdt_error ("image is not a fdt"); |
Kumar Gala | 274cea2 | 2008-02-27 21:51:46 -0600 | [diff] [blame^] | 460 | goto error; |
Marian Balakowicz | 7b32545 | 2008-01-31 13:58:20 +0100 | [diff] [blame] | 461 | } |
| 462 | |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 463 | if (be32_to_cpu (fdt_totalsize (fdt_blob)) != fdt_len) { |
Marian Balakowicz | 7b32545 | 2008-01-31 13:58:20 +0100 | [diff] [blame] | 464 | fdt_error ("fdt size != image size"); |
Kumar Gala | 274cea2 | 2008-02-27 21:51:46 -0600 | [diff] [blame^] | 465 | goto error; |
Marian Balakowicz | 7b32545 | 2008-01-31 13:58:20 +0100 | [diff] [blame] | 466 | } |
| 467 | } else { |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 468 | fdt_error ("Did not find a Flattened Device Tree " |
| 469 | "in a legacy multi-component image"); |
Kumar Gala | 274cea2 | 2008-02-27 21:51:46 -0600 | [diff] [blame^] | 470 | goto error; |
Marian Balakowicz | 7b32545 | 2008-01-31 13:58:20 +0100 | [diff] [blame] | 471 | } |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 472 | } else { |
| 473 | debug ("## No Flattened Device Tree\n"); |
| 474 | *of_flat_tree = NULL; |
Marian Balakowicz | d2ced9e | 2008-02-04 08:28:17 +0100 | [diff] [blame] | 475 | *of_size = 0; |
Kumar Gala | 274cea2 | 2008-02-27 21:51:46 -0600 | [diff] [blame^] | 476 | return 0; |
Marian Balakowicz | d2ced9e | 2008-02-04 08:28:17 +0100 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | *of_flat_tree = fdt_blob; |
| 480 | *of_size = be32_to_cpu (fdt_totalsize (fdt_blob)); |
| 481 | debug (" of_flat_tree at 0x%08lx size 0x%08lx\n", |
| 482 | *of_flat_tree, *of_size); |
Kumar Gala | 274cea2 | 2008-02-27 21:51:46 -0600 | [diff] [blame^] | 483 | |
| 484 | return 0; |
| 485 | |
| 486 | error: |
| 487 | do_reset (cmdtp, flag, argc, argv); |
| 488 | return 1; |
Marian Balakowicz | d2ced9e | 2008-02-04 08:28:17 +0100 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | static ulong fdt_relocate (ulong alloc_current, |
| 492 | cmd_tbl_t *cmdtp, int flag, int argc, char *argv[], |
| 493 | char **of_flat_tree, ulong *of_size) |
| 494 | { |
| 495 | char *fdt_blob = *of_flat_tree; |
| 496 | ulong relocate = 0; |
| 497 | ulong new_alloc_current; |
| 498 | |
| 499 | /* nothing to do */ |
| 500 | if (*of_size == 0) |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 501 | return alloc_current; |
Marian Balakowicz | d2ced9e | 2008-02-04 08:28:17 +0100 | [diff] [blame] | 502 | |
| 503 | if (fdt_check_header (fdt_blob) != 0) { |
| 504 | fdt_error ("image is not a fdt"); |
Kumar Gala | 274cea2 | 2008-02-27 21:51:46 -0600 | [diff] [blame^] | 505 | goto error; |
Marian Balakowicz | 7b32545 | 2008-01-31 13:58:20 +0100 | [diff] [blame] | 506 | } |
| 507 | |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 508 | #ifndef CFG_NO_FLASH |
Marian Balakowicz | d2ced9e | 2008-02-04 08:28:17 +0100 | [diff] [blame] | 509 | /* move the blob if it is in flash (set relocate) */ |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 510 | if (addr2info ((ulong)fdt_blob) != NULL) |
Marian Balakowicz | d2ced9e | 2008-02-04 08:28:17 +0100 | [diff] [blame] | 511 | relocate = 1; |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 512 | #endif |
| 513 | |
Marian Balakowicz | 7b32545 | 2008-01-31 13:58:20 +0100 | [diff] [blame] | 514 | #ifdef CFG_BOOTMAPSZ |
| 515 | /* |
| 516 | * The blob must be within CFG_BOOTMAPSZ, |
| 517 | * so we flag it to be copied if it is not. |
| 518 | */ |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 519 | if (fdt_blob >= (char *)CFG_BOOTMAPSZ) |
Marian Balakowicz | d2ced9e | 2008-02-04 08:28:17 +0100 | [diff] [blame] | 520 | relocate = 1; |
Marian Balakowicz | 7b32545 | 2008-01-31 13:58:20 +0100 | [diff] [blame] | 521 | #endif |
| 522 | |
| 523 | /* move flattend device tree if needed */ |
Marian Balakowicz | d2ced9e | 2008-02-04 08:28:17 +0100 | [diff] [blame] | 524 | if (relocate) { |
Marian Balakowicz | 7b32545 | 2008-01-31 13:58:20 +0100 | [diff] [blame] | 525 | int err; |
| 526 | ulong of_start, of_len; |
| 527 | |
Marian Balakowicz | d2ced9e | 2008-02-04 08:28:17 +0100 | [diff] [blame] | 528 | of_len = *of_size; |
Marian Balakowicz | 7b32545 | 2008-01-31 13:58:20 +0100 | [diff] [blame] | 529 | |
| 530 | /* position on a 4K boundary before the alloc_current */ |
| 531 | of_start = alloc_current - of_len; |
| 532 | of_start &= ~(4096 - 1); /* align on page */ |
| 533 | |
| 534 | debug ("## device tree at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n", |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 535 | (ulong)fdt_blob, (ulong)fdt_blob + of_len - 1, |
Marian Balakowicz | 7b32545 | 2008-01-31 13:58:20 +0100 | [diff] [blame] | 536 | of_len, of_len); |
| 537 | |
| 538 | printf (" Loading Device Tree to %08lx, end %08lx ... ", |
| 539 | of_start, of_start + of_len - 1); |
| 540 | |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 541 | err = fdt_open_into (fdt_blob, (void *)of_start, of_len); |
Marian Balakowicz | 7b32545 | 2008-01-31 13:58:20 +0100 | [diff] [blame] | 542 | if (err != 0) { |
| 543 | fdt_error ("fdt move failed"); |
Kumar Gala | 274cea2 | 2008-02-27 21:51:46 -0600 | [diff] [blame^] | 544 | goto error; |
Marian Balakowicz | 7b32545 | 2008-01-31 13:58:20 +0100 | [diff] [blame] | 545 | } |
| 546 | puts ("OK\n"); |
| 547 | |
| 548 | *of_flat_tree = (char *)of_start; |
| 549 | new_alloc_current = of_start; |
| 550 | } else { |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 551 | *of_flat_tree = fdt_blob; |
Marian Balakowicz | 7b32545 | 2008-01-31 13:58:20 +0100 | [diff] [blame] | 552 | new_alloc_current = alloc_current; |
| 553 | } |
| 554 | |
| 555 | return new_alloc_current; |
Kumar Gala | 274cea2 | 2008-02-27 21:51:46 -0600 | [diff] [blame^] | 556 | |
| 557 | error: |
| 558 | do_reset (cmdtp, flag, argc, argv); |
| 559 | return 1; |
Marian Balakowicz | 7b32545 | 2008-01-31 13:58:20 +0100 | [diff] [blame] | 560 | } |
Marian Balakowicz | d3c5eb6 | 2008-01-31 13:20:08 +0100 | [diff] [blame] | 561 | #endif |