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