1 /* Copyright (C) 2011 2 * Corscience GmbH & Co. KG - Simon Schwarz <schwarz@corscience.de> 3 * - Added prep subcommand support 4 * - Reorganized source - modeled after powerpc version 5 * 6 * (C) Copyright 2002 7 * Sysgo Real-Time Solutions, GmbH <www.elinos.com> 8 * Marius Groeger <mgroeger@sysgo.de> 9 * 10 * Copyright (C) 2001 Erik Mouw (J.A.K.Mouw@its.tudelft.nl) 11 * 12 * This program is free software; you can redistribute it and/or modify 13 * it under the terms of the GNU General Public License as published by 14 * the Free Software Foundation; either version 2 of the License, or 15 * (at your option) any later version. 16 * 17 * This program is distributed in the hope that it will be useful, 18 * but WITHOUT ANY WARRANTY; without even the implied warranty of 19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20 * GNU General Public License for more details. 21 * 22 * You should have received a copy of the GNU General Public License 23 * along with this program; if not, write to the Free Software 24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 25 * 26 */ 27 28 #include <common.h> 29 #include <command.h> 30 #include <image.h> 31 #include <u-boot/zlib.h> 32 #include <asm/byteorder.h> 33 #include <fdt.h> 34 #include <libfdt.h> 35 #include <fdt_support.h> 36 #include <asm/bootm.h> 37 #include <linux/compiler.h> 38 39 DECLARE_GLOBAL_DATA_PTR; 40 41 #if defined(CONFIG_SETUP_MEMORY_TAGS) || \ 42 defined(CONFIG_CMDLINE_TAG) || \ 43 defined(CONFIG_INITRD_TAG) || \ 44 defined(CONFIG_SERIAL_TAG) || \ 45 defined(CONFIG_REVISION_TAG) 46 static struct tag *params; 47 #endif 48 49 static ulong get_sp(void) 50 { 51 ulong ret; 52 53 asm("mov %0, sp" : "=r"(ret) : ); 54 return ret; 55 } 56 57 void arch_lmb_reserve(struct lmb *lmb) 58 { 59 ulong sp; 60 61 /* 62 * Booting a (Linux) kernel image 63 * 64 * Allocate space for command line and board info - the 65 * address should be as high as possible within the reach of 66 * the kernel (see CONFIG_SYS_BOOTMAPSZ settings), but in unused 67 * memory, which means far enough below the current stack 68 * pointer. 69 */ 70 sp = get_sp(); 71 debug("## Current stack ends at 0x%08lx ", sp); 72 73 /* adjust sp by 4K to be safe */ 74 sp -= 4096; 75 lmb_reserve(lmb, sp, 76 gd->bd->bi_dram[0].start + gd->bd->bi_dram[0].size - sp); 77 } 78 79 #ifdef CONFIG_OF_LIBFDT 80 static int fixup_memory_node(void *blob) 81 { 82 bd_t *bd = gd->bd; 83 int bank; 84 u64 start[CONFIG_NR_DRAM_BANKS]; 85 u64 size[CONFIG_NR_DRAM_BANKS]; 86 87 for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) { 88 start[bank] = bd->bi_dram[bank].start; 89 size[bank] = bd->bi_dram[bank].size; 90 } 91 92 return fdt_fixup_memory_banks(blob, start, size, CONFIG_NR_DRAM_BANKS); 93 } 94 #endif 95 96 static void announce_and_cleanup(void) 97 { 98 printf("\nStarting kernel ...\n\n"); 99 bootstage_mark_name(BOOTSTAGE_ID_BOOTM_HANDOFF, "start_kernel"); 100 #ifdef CONFIG_BOOTSTAGE_FDT 101 bootstage_fdt_add_report(); 102 #endif 103 #ifdef CONFIG_BOOTSTAGE_REPORT 104 bootstage_report(); 105 #endif 106 107 #ifdef CONFIG_USB_DEVICE 108 udc_disconnect(); 109 #endif 110 cleanup_before_linux(); 111 } 112 113 #if defined(CONFIG_SETUP_MEMORY_TAGS) || \ 114 defined(CONFIG_CMDLINE_TAG) || \ 115 defined(CONFIG_INITRD_TAG) || \ 116 defined(CONFIG_SERIAL_TAG) || \ 117 defined(CONFIG_REVISION_TAG) 118 static void setup_start_tag (bd_t *bd) 119 { 120 params = (struct tag *)bd->bi_boot_params; 121 122 params->hdr.tag = ATAG_CORE; 123 params->hdr.size = tag_size (tag_core); 124 125 params->u.core.flags = 0; 126 params->u.core.pagesize = 0; 127 params->u.core.rootdev = 0; 128 129 params = tag_next (params); 130 } 131 #endif 132 133 #ifdef CONFIG_SETUP_MEMORY_TAGS 134 static void setup_memory_tags(bd_t *bd) 135 { 136 int i; 137 138 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) { 139 params->hdr.tag = ATAG_MEM; 140 params->hdr.size = tag_size (tag_mem32); 141 142 params->u.mem.start = bd->bi_dram[i].start; 143 params->u.mem.size = bd->bi_dram[i].size; 144 145 params = tag_next (params); 146 } 147 } 148 #endif 149 150 #ifdef CONFIG_CMDLINE_TAG 151 static void setup_commandline_tag(bd_t *bd, char *commandline) 152 { 153 char *p; 154 155 if (!commandline) 156 return; 157 158 /* eat leading white space */ 159 for (p = commandline; *p == ' '; p++); 160 161 /* skip non-existent command lines so the kernel will still 162 * use its default command line. 163 */ 164 if (*p == '\0') 165 return; 166 167 params->hdr.tag = ATAG_CMDLINE; 168 params->hdr.size = 169 (sizeof (struct tag_header) + strlen (p) + 1 + 4) >> 2; 170 171 strcpy (params->u.cmdline.cmdline, p); 172 173 params = tag_next (params); 174 } 175 #endif 176 177 #ifdef CONFIG_INITRD_TAG 178 static void setup_initrd_tag(bd_t *bd, ulong initrd_start, ulong initrd_end) 179 { 180 /* an ATAG_INITRD node tells the kernel where the compressed 181 * ramdisk can be found. ATAG_RDIMG is a better name, actually. 182 */ 183 params->hdr.tag = ATAG_INITRD2; 184 params->hdr.size = tag_size (tag_initrd); 185 186 params->u.initrd.start = initrd_start; 187 params->u.initrd.size = initrd_end - initrd_start; 188 189 params = tag_next (params); 190 } 191 #endif 192 193 #ifdef CONFIG_SERIAL_TAG 194 void setup_serial_tag(struct tag **tmp) 195 { 196 struct tag *params = *tmp; 197 struct tag_serialnr serialnr; 198 void get_board_serial(struct tag_serialnr *serialnr); 199 200 get_board_serial(&serialnr); 201 params->hdr.tag = ATAG_SERIAL; 202 params->hdr.size = tag_size (tag_serialnr); 203 params->u.serialnr.low = serialnr.low; 204 params->u.serialnr.high= serialnr.high; 205 params = tag_next (params); 206 *tmp = params; 207 } 208 #endif 209 210 #ifdef CONFIG_REVISION_TAG 211 void setup_revision_tag(struct tag **in_params) 212 { 213 u32 rev = 0; 214 u32 get_board_rev(void); 215 216 rev = get_board_rev(); 217 params->hdr.tag = ATAG_REVISION; 218 params->hdr.size = tag_size (tag_revision); 219 params->u.revision.rev = rev; 220 params = tag_next (params); 221 } 222 #endif 223 224 #if defined(CONFIG_SETUP_MEMORY_TAGS) || \ 225 defined(CONFIG_CMDLINE_TAG) || \ 226 defined(CONFIG_INITRD_TAG) || \ 227 defined(CONFIG_SERIAL_TAG) || \ 228 defined(CONFIG_REVISION_TAG) 229 static void setup_end_tag(bd_t *bd) 230 { 231 params->hdr.tag = ATAG_NONE; 232 params->hdr.size = 0; 233 } 234 #endif 235 236 #ifdef CONFIG_OF_LIBFDT 237 static int create_fdt(bootm_headers_t *images) 238 { 239 ulong of_size = images->ft_len; 240 char **of_flat_tree = &images->ft_addr; 241 ulong *initrd_start = &images->initrd_start; 242 ulong *initrd_end = &images->initrd_end; 243 struct lmb *lmb = &images->lmb; 244 ulong rd_len; 245 int ret; 246 247 debug("using: FDT\n"); 248 249 boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree); 250 251 rd_len = images->rd_end - images->rd_start; 252 ret = boot_ramdisk_high(lmb, images->rd_start, rd_len, 253 initrd_start, initrd_end); 254 if (ret) 255 return ret; 256 257 ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size); 258 if (ret) 259 return ret; 260 261 fdt_chosen(*of_flat_tree, 1); 262 fixup_memory_node(*of_flat_tree); 263 fdt_fixup_ethernet(*of_flat_tree); 264 fdt_initrd(*of_flat_tree, *initrd_start, *initrd_end, 1); 265 #ifdef CONFIG_OF_BOARD_SETUP 266 ft_board_setup(*of_flat_tree, gd->bd); 267 #endif 268 269 return 0; 270 } 271 #endif 272 273 __weak void setup_board_tags(struct tag **in_params) {} 274 275 /* Subcommand: PREP */ 276 static void boot_prep_linux(bootm_headers_t *images) 277 { 278 #ifdef CONFIG_CMDLINE_TAG 279 char *commandline = getenv("bootargs"); 280 #endif 281 282 #ifdef CONFIG_OF_LIBFDT 283 if (images->ft_len) { 284 debug("using: FDT\n"); 285 if (create_fdt(images)) { 286 printf("FDT creation failed! hanging..."); 287 hang(); 288 } 289 } else 290 #endif 291 { 292 #if defined(CONFIG_SETUP_MEMORY_TAGS) || \ 293 defined(CONFIG_CMDLINE_TAG) || \ 294 defined(CONFIG_INITRD_TAG) || \ 295 defined(CONFIG_SERIAL_TAG) || \ 296 defined(CONFIG_REVISION_TAG) 297 debug("using: ATAGS\n"); 298 setup_start_tag(gd->bd); 299 #ifdef CONFIG_SERIAL_TAG 300 setup_serial_tag(¶ms); 301 #endif 302 #ifdef CONFIG_CMDLINE_TAG 303 setup_commandline_tag(gd->bd, commandline); 304 #endif 305 #ifdef CONFIG_REVISION_TAG 306 setup_revision_tag(¶ms); 307 #endif 308 #ifdef CONFIG_SETUP_MEMORY_TAGS 309 setup_memory_tags(gd->bd); 310 #endif 311 #ifdef CONFIG_INITRD_TAG 312 if (images->rd_start && images->rd_end) 313 setup_initrd_tag(gd->bd, images->rd_start, 314 images->rd_end); 315 #endif 316 setup_board_tags(¶ms); 317 setup_end_tag(gd->bd); 318 #else /* all tags */ 319 printf("FDT and ATAGS support not compiled in - hanging\n"); 320 hang(); 321 #endif /* all tags */ 322 } 323 } 324 325 /* Subcommand: GO */ 326 static void boot_jump_linux(bootm_headers_t *images) 327 { 328 unsigned long machid = gd->bd->bi_arch_number; 329 char *s; 330 void (*kernel_entry)(int zero, int arch, uint params); 331 unsigned long r2; 332 333 kernel_entry = (void (*)(int, int, uint))images->ep; 334 335 s = getenv("machid"); 336 if (s) { 337 strict_strtoul(s, 16, &machid); 338 printf("Using machid 0x%lx from environment\n", machid); 339 } 340 341 debug("## Transferring control to Linux (at address %08lx)" \ 342 "...\n", (ulong) kernel_entry); 343 bootstage_mark(BOOTSTAGE_ID_RUN_OS); 344 announce_and_cleanup(); 345 346 #ifdef CONFIG_OF_LIBFDT 347 if (images->ft_len) 348 r2 = (unsigned long)images->ft_addr; 349 else 350 #endif 351 r2 = gd->bd->bi_boot_params; 352 353 kernel_entry(0, machid, r2); 354 } 355 356 /* Main Entry point for arm bootm implementation 357 * 358 * Modeled after the powerpc implementation 359 * DIFFERENCE: Instead of calling prep and go at the end 360 * they are called if subcommand is equal 0. 361 */ 362 int do_bootm_linux(int flag, int argc, char *argv[], bootm_headers_t *images) 363 { 364 /* No need for those on ARM */ 365 if (flag & BOOTM_STATE_OS_BD_T || flag & BOOTM_STATE_OS_CMDLINE) 366 return -1; 367 368 if (flag & BOOTM_STATE_OS_PREP) { 369 boot_prep_linux(images); 370 return 0; 371 } 372 373 if (flag & BOOTM_STATE_OS_GO) { 374 boot_jump_linux(images); 375 return 0; 376 } 377 378 boot_prep_linux(images); 379 boot_jump_linux(images); 380 return 0; 381 } 382 383 #ifdef CONFIG_CMD_BOOTZ 384 385 struct zimage_header { 386 uint32_t code[9]; 387 uint32_t zi_magic; 388 uint32_t zi_start; 389 uint32_t zi_end; 390 }; 391 392 #define LINUX_ARM_ZIMAGE_MAGIC 0x016f2818 393 394 int bootz_setup(void *image, void **start, void **end) 395 { 396 struct zimage_header *zi = (struct zimage_header *)image; 397 398 if (zi->zi_magic != LINUX_ARM_ZIMAGE_MAGIC) { 399 puts("Bad Linux ARM zImage magic!\n"); 400 return 1; 401 } 402 403 *start = (void *)zi->zi_start; 404 *end = (void *)zi->zi_end; 405 406 debug("Kernel image @ 0x%08x [ 0x%08x - 0x%08x ]\n", 407 (uint32_t)image, (uint32_t)*start, (uint32_t)*end); 408 409 return 0; 410 } 411 #endif /* CONFIG_CMD_BOOTZ */ 412