ppc_linux.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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. #include <common.h>
  26. #include <watchdog.h>
  27. #include <command.h>
  28. #include <image.h>
  29. #include <malloc.h>
  30. #include <zlib.h>
  31. #include <bzlib.h>
  32. #include <environment.h>
  33. #include <asm/byteorder.h>
  34. #if defined(CONFIG_OF_LIBFDT)
  35. #include <fdt.h>
  36. #include <libfdt.h>
  37. #include <fdt_support.h>
  38. #endif
  39. #if defined(CONFIG_OF_FLAT_TREE)
  40. #include <ft_build.h>
  41. #endif
  42. #ifdef CONFIG_LOGBUFFER
  43. #include <logbuff.h>
  44. #endif
  45. #ifdef CFG_INIT_RAM_LOCK
  46. #include <asm/cache.h>
  47. #endif
  48. #define CHUNKSZ (64 * 1024)
  49. DECLARE_GLOBAL_DATA_PTR;
  50. extern image_header_t header;
  51. /*cmd_boot.c*/
  52. extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
  53. #if defined(CONFIG_CMD_BDI)
  54. extern int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
  55. #endif
  56. void __attribute__((noinline))
  57. do_bootm_linux (cmd_tbl_t *cmdtp, int flag,
  58. int argc, char *argv[],
  59. ulong addr,
  60. ulong *len_ptr,
  61. int verify)
  62. {
  63. ulong sp;
  64. ulong len;
  65. ulong initrd_start, initrd_end;
  66. ulong cmd_start, cmd_end;
  67. ulong initrd_high;
  68. ulong data;
  69. int initrd_copy_to_ram = 1;
  70. char *cmdline;
  71. char *s;
  72. bd_t *kbd;
  73. void (*kernel)(bd_t *, ulong, ulong, ulong, ulong);
  74. image_header_t *hdr = &header;
  75. #if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
  76. char *of_flat_tree = NULL;
  77. ulong of_data = 0;
  78. #endif
  79. if ((s = getenv ("initrd_high")) != NULL) {
  80. /* a value of "no" or a similar string will act like 0,
  81. * turning the "load high" feature off. This is intentional.
  82. */
  83. initrd_high = simple_strtoul(s, NULL, 16);
  84. if (initrd_high == ~0)
  85. initrd_copy_to_ram = 0;
  86. } else { /* not set, no restrictions to load high */
  87. initrd_high = ~0;
  88. }
  89. #ifdef CONFIG_LOGBUFFER
  90. kbd=gd->bd;
  91. /* Prevent initrd from overwriting logbuffer */
  92. if (initrd_high < (kbd->bi_memsize-LOGBUFF_LEN-LOGBUFF_OVERHEAD))
  93. initrd_high = kbd->bi_memsize-LOGBUFF_LEN-LOGBUFF_OVERHEAD;
  94. debug ("## Logbuffer at 0x%08lX ", kbd->bi_memsize-LOGBUFF_LEN);
  95. #endif
  96. /*
  97. * Booting a (Linux) kernel image
  98. *
  99. * Allocate space for command line and board info - the
  100. * address should be as high as possible within the reach of
  101. * the kernel (see CFG_BOOTMAPSZ settings), but in unused
  102. * memory, which means far enough below the current stack
  103. * pointer.
  104. */
  105. asm( "mr %0,1": "=r"(sp) : );
  106. debug ("## Current stack ends at 0x%08lX ", sp);
  107. sp -= 2048; /* just to be sure */
  108. if (sp > CFG_BOOTMAPSZ)
  109. sp = CFG_BOOTMAPSZ;
  110. sp &= ~0xF;
  111. debug ("=> set upper limit to 0x%08lX\n", sp);
  112. cmdline = (char *)((sp - CFG_BARGSIZE) & ~0xF);
  113. kbd = (bd_t *)(((ulong)cmdline - sizeof(bd_t)) & ~0xF);
  114. if ((s = getenv("bootargs")) == NULL)
  115. s = "";
  116. strcpy (cmdline, s);
  117. cmd_start = (ulong)&cmdline[0];
  118. cmd_end = cmd_start + strlen(cmdline);
  119. *kbd = *(gd->bd);
  120. #ifdef DEBUG
  121. printf ("## cmdline at 0x%08lX ... 0x%08lX\n", cmd_start, cmd_end);
  122. #if defined(CONFIG_CMD_BDI)
  123. do_bdinfo (NULL, 0, 0, NULL);
  124. #endif
  125. #endif
  126. if ((s = getenv ("clocks_in_mhz")) != NULL) {
  127. /* convert all clock information to MHz */
  128. kbd->bi_intfreq /= 1000000L;
  129. kbd->bi_busfreq /= 1000000L;
  130. #if defined(CONFIG_MPC8220)
  131. kbd->bi_inpfreq /= 1000000L;
  132. kbd->bi_pcifreq /= 1000000L;
  133. kbd->bi_pevfreq /= 1000000L;
  134. kbd->bi_flbfreq /= 1000000L;
  135. kbd->bi_vcofreq /= 1000000L;
  136. #endif
  137. #if defined(CONFIG_CPM2)
  138. kbd->bi_cpmfreq /= 1000000L;
  139. kbd->bi_brgfreq /= 1000000L;
  140. kbd->bi_sccfreq /= 1000000L;
  141. kbd->bi_vco /= 1000000L;
  142. #endif
  143. #if defined(CONFIG_MPC5xxx)
  144. kbd->bi_ipbfreq /= 1000000L;
  145. kbd->bi_pcifreq /= 1000000L;
  146. #endif /* CONFIG_MPC5xxx */
  147. }
  148. kernel = (void (*)(bd_t *, ulong, ulong, ulong, ulong))image_get_ep (hdr);
  149. /*
  150. * Check if there is an initrd image
  151. */
  152. #if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
  153. /* Look for a '-' which indicates to ignore the ramdisk argument */
  154. if (argc >= 3 && strcmp(argv[2], "-") == 0) {
  155. debug ("Skipping initrd\n");
  156. len = data = 0;
  157. }
  158. else
  159. #endif
  160. if (argc >= 3) {
  161. debug ("Not skipping initrd\n");
  162. show_boot_progress (9);
  163. addr = simple_strtoul(argv[2], NULL, 16);
  164. printf ("## Loading RAMDisk Image at %08lx ...\n", addr);
  165. hdr = (image_header_t *)addr;
  166. if (!image_check_magic (hdr)) {
  167. puts ("Bad Magic Number\n");
  168. show_boot_progress (-10);
  169. do_reset (cmdtp, flag, argc, argv);
  170. }
  171. if (!image_check_hcrc (hdr)) {
  172. puts ("Bad Header Checksum\n");
  173. show_boot_progress (-11);
  174. do_reset (cmdtp, flag, argc, argv);
  175. }
  176. show_boot_progress (10);
  177. print_image_hdr (hdr);
  178. if (verify) {
  179. puts (" Verifying Checksum ... ");
  180. if (!image_check_dcrc_wd (hdr, CHUNKSZ)) {
  181. puts ("Bad Data CRC\n");
  182. show_boot_progress (-12);
  183. do_reset (cmdtp, flag, argc, argv);
  184. }
  185. puts ("OK\n");
  186. }
  187. show_boot_progress (11);
  188. if (!image_check_os (hdr, IH_OS_LINUX) ||
  189. !image_check_arch (hdr, IH_ARCH_PPC) ||
  190. !image_check_type (hdr, IH_TYPE_RAMDISK)) {
  191. puts ("No Linux PPC Ramdisk Image\n");
  192. show_boot_progress (-13);
  193. do_reset (cmdtp, flag, argc, argv);
  194. }
  195. data = image_get_data (hdr);
  196. len = image_get_data_size (hdr);
  197. /*
  198. * Now check if we have a multifile image
  199. */
  200. } else if (image_check_type (hdr, IH_TYPE_MULTI) && (len_ptr[1])) {
  201. u_long tail = image_to_cpu (len_ptr[0]) % 4;
  202. int i;
  203. show_boot_progress (13);
  204. /* skip kernel length and terminator */
  205. data = (ulong)(&len_ptr[2]);
  206. /* skip any additional image length fields */
  207. for (i=1; len_ptr[i]; ++i)
  208. data += 4;
  209. /* add kernel length, and align */
  210. data += image_to_cpu (len_ptr[0]);
  211. if (tail) {
  212. data += 4 - tail;
  213. }
  214. len = image_to_cpu (len_ptr[1]);
  215. } else {
  216. /*
  217. * no initrd image
  218. */
  219. show_boot_progress (14);
  220. len = data = 0;
  221. }
  222. #if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
  223. if(argc > 3) {
  224. of_flat_tree = (char *) simple_strtoul(argv[3], NULL, 16);
  225. hdr = (image_header_t *)of_flat_tree;
  226. #if defined(CONFIG_OF_FLAT_TREE)
  227. if (*((ulong *)(of_flat_tree + image_get_header_size ())) != OF_DT_HEADER) {
  228. #else
  229. if (fdt_check_header (of_flat_tree + image_get_header_size ()) != 0) {
  230. #endif
  231. #ifndef CFG_NO_FLASH
  232. if (addr2info((ulong)of_flat_tree) != NULL)
  233. of_data = (ulong)of_flat_tree;
  234. #endif
  235. } else if (image_check_magic (hdr)) {
  236. printf("## Flat Device Tree at %08lX\n", hdr);
  237. print_image_hdr (hdr);
  238. if ((image_get_load (hdr) < ((unsigned long)hdr + image_get_image_size (hdr))) &&
  239. ((image_get_load (hdr) + image_get_data_size (hdr)) > (unsigned long)hdr)) {
  240. puts ("ERROR: fdt overwritten - "
  241. "must RESET the board to recover.\n");
  242. do_reset (cmdtp, flag, argc, argv);
  243. }
  244. puts (" Verifying Checksum ... ");
  245. if (!image_check_hcrc (hdr)) {
  246. puts ("ERROR: fdt header checksum invalid - "
  247. "must RESET the board to recover.\n");
  248. do_reset (cmdtp, flag, argc, argv);
  249. }
  250. if (!image_check_dcrc (hdr)) {
  251. puts ("ERROR: fdt checksum invalid - "
  252. "must RESET the board to recover.\n");
  253. do_reset (cmdtp, flag, argc, argv);
  254. }
  255. puts ("OK\n");
  256. if (!image_check_type (hdr, IH_TYPE_FLATDT)) {
  257. puts ("ERROR: uImage is not a fdt - "
  258. "must RESET the board to recover.\n");
  259. do_reset (cmdtp, flag, argc, argv);
  260. }
  261. if (image_get_comp (hdr) != IH_COMP_NONE) {
  262. puts ("ERROR: uImage is compressed - "
  263. "must RESET the board to recover.\n");
  264. do_reset (cmdtp, flag, argc, argv);
  265. }
  266. #if defined(CONFIG_OF_FLAT_TREE)
  267. if (*((ulong *)(of_flat_tree + image_get_header_size ())) != OF_DT_HEADER) {
  268. #else
  269. if (fdt_check_header (of_flat_tree + image_get_header_size ()) != 0) {
  270. #endif
  271. puts ("ERROR: uImage data is not a fdt - "
  272. "must RESET the board to recover.\n");
  273. do_reset (cmdtp, flag, argc, argv);
  274. }
  275. memmove ((void *)image_get_load (hdr),
  276. (void *)(of_flat_tree + image_get_header_size ()),
  277. image_get_data_size (hdr));
  278. of_flat_tree = (char *)image_get_load (hdr);
  279. } else {
  280. puts ("Did not find a flat Flat Device Tree.\n"
  281. "Must RESET the board to recover.\n");
  282. do_reset (cmdtp, flag, argc, argv);
  283. }
  284. printf (" Booting using the fdt at 0x%x\n",
  285. of_flat_tree);
  286. } else if (image_check_type (hdr, IH_TYPE_MULTI) && (len_ptr[1]) && (len_ptr[2])) {
  287. u_long tail = image_to_cpu (len_ptr[0]) % 4;
  288. int i;
  289. /* skip kernel length, initrd length, and terminator */
  290. of_flat_tree = (char *)(&len_ptr[3]);
  291. /* skip any additional image length fields */
  292. for (i=2; len_ptr[i]; ++i)
  293. of_flat_tree += 4;
  294. /* add kernel length, and align */
  295. of_flat_tree += image_to_cpu (len_ptr[0]);
  296. if (tail) {
  297. of_flat_tree += 4 - tail;
  298. }
  299. /* add initrd length, and align */
  300. tail = image_to_cpu (len_ptr[1]) % 4;
  301. of_flat_tree += image_to_cpu (len_ptr[1]);
  302. if (tail) {
  303. of_flat_tree += 4 - tail;
  304. }
  305. #ifndef CFG_NO_FLASH
  306. /* move the blob if it is in flash (set of_data to !null) */
  307. if (addr2info ((ulong)of_flat_tree) != NULL)
  308. of_data = (ulong)of_flat_tree;
  309. #endif
  310. #if defined(CONFIG_OF_FLAT_TREE)
  311. if (*((ulong *)(of_flat_tree)) != OF_DT_HEADER) {
  312. #else
  313. if (fdt_check_header (of_flat_tree) != 0) {
  314. #endif
  315. puts ("ERROR: image is not a fdt - "
  316. "must RESET the board to recover.\n");
  317. do_reset (cmdtp, flag, argc, argv);
  318. }
  319. #if defined(CONFIG_OF_FLAT_TREE)
  320. if (((struct boot_param_header *)of_flat_tree)->totalsize !=
  321. image_to_cpu (len_ptr[2])) {
  322. #else
  323. if (be32_to_cpu (fdt_totalsize (of_flat_tree)) !=
  324. image_to_cpu (len_ptr[2])) {
  325. #endif
  326. puts ("ERROR: fdt size != image size - "
  327. "must RESET the board to recover.\n");
  328. do_reset (cmdtp, flag, argc, argv);
  329. }
  330. }
  331. #endif
  332. if (!data) {
  333. debug ("No initrd\n");
  334. }
  335. if (data) {
  336. if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
  337. initrd_start = data;
  338. initrd_end = initrd_start + len;
  339. } else {
  340. initrd_start = (ulong)kbd - len;
  341. initrd_start &= ~(4096 - 1); /* align on page */
  342. if (initrd_high) {
  343. ulong nsp;
  344. /*
  345. * the inital ramdisk does not need to be within
  346. * CFG_BOOTMAPSZ as it is not accessed until after
  347. * the mm system is initialised.
  348. *
  349. * do the stack bottom calculation again and see if
  350. * the initrd will fit just below the monitor stack
  351. * bottom without overwriting the area allocated
  352. * above for command line args and board info.
  353. */
  354. asm( "mr %0,1": "=r"(nsp) : );
  355. nsp -= 2048; /* just to be sure */
  356. nsp &= ~0xF;
  357. if (nsp > initrd_high) /* limit as specified */
  358. nsp = initrd_high;
  359. nsp -= len;
  360. nsp &= ~(4096 - 1); /* align on page */
  361. if (nsp >= sp)
  362. initrd_start = nsp;
  363. }
  364. show_boot_progress (12);
  365. debug ("## initrd at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
  366. data, data + len - 1, len, len);
  367. initrd_end = initrd_start + len;
  368. printf (" Loading Ramdisk to %08lx, end %08lx ... ",
  369. initrd_start, initrd_end);
  370. #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
  371. {
  372. size_t l = len;
  373. void *to = (void *)initrd_start;
  374. void *from = (void *)data;
  375. while (l > 0) {
  376. size_t tail = (l > CHUNKSZ) ? CHUNKSZ : l;
  377. WATCHDOG_RESET();
  378. memmove (to, from, tail);
  379. to += tail;
  380. from += tail;
  381. l -= tail;
  382. }
  383. }
  384. #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
  385. memmove ((void *)initrd_start, (void *)data, len);
  386. #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
  387. puts ("OK\n");
  388. }
  389. } else {
  390. initrd_start = 0;
  391. initrd_end = 0;
  392. }
  393. #if defined(CONFIG_OF_LIBFDT)
  394. #ifdef CFG_BOOTMAPSZ
  395. /*
  396. * The blob must be within CFG_BOOTMAPSZ,
  397. * so we flag it to be copied if it is not.
  398. */
  399. if (of_flat_tree >= (char *)CFG_BOOTMAPSZ)
  400. of_data = (ulong)of_flat_tree;
  401. #endif
  402. /* move of_flat_tree if needed */
  403. if (of_data) {
  404. int err;
  405. ulong of_start, of_len;
  406. of_len = be32_to_cpu(fdt_totalsize(of_data));
  407. /* position on a 4K boundary before the kbd */
  408. of_start = (ulong)kbd - of_len;
  409. of_start &= ~(4096 - 1); /* align on page */
  410. debug ("## device tree at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
  411. of_data, of_data + of_len - 1, of_len, of_len);
  412. of_flat_tree = (char *)of_start;
  413. printf (" Loading Device Tree to %08lx, end %08lx ... ",
  414. of_start, of_start + of_len - 1);
  415. err = fdt_open_into((void *)of_data, (void *)of_start, of_len);
  416. if (err != 0) {
  417. puts ("ERROR: fdt move failed - "
  418. "must RESET the board to recover.\n");
  419. do_reset (cmdtp, flag, argc, argv);
  420. }
  421. puts ("OK\n");
  422. }
  423. /*
  424. * Add the chosen node if it doesn't exist, add the env and bd_t
  425. * if the user wants it (the logic is in the subroutines).
  426. */
  427. if (of_flat_tree) {
  428. if (fdt_chosen(of_flat_tree, initrd_start, initrd_end, 0) < 0) {
  429. puts ("ERROR: /chosen node create failed - "
  430. "must RESET the board to recover.\n");
  431. do_reset (cmdtp, flag, argc, argv);
  432. }
  433. #ifdef CONFIG_OF_HAS_UBOOT_ENV
  434. if (fdt_env(of_flat_tree) < 0) {
  435. puts ("ERROR: /u-boot-env node create failed - "
  436. "must RESET the board to recover.\n");
  437. do_reset (cmdtp, flag, argc, argv);
  438. }
  439. #endif
  440. #ifdef CONFIG_OF_HAS_BD_T
  441. if (fdt_bd_t(of_flat_tree) < 0) {
  442. puts ("ERROR: /bd_t node create failed - "
  443. "must RESET the board to recover.\n");
  444. do_reset (cmdtp, flag, argc, argv);
  445. }
  446. #endif
  447. #ifdef CONFIG_OF_BOARD_SETUP
  448. /* Call the board-specific fixup routine */
  449. ft_board_setup(of_flat_tree, gd->bd);
  450. #endif
  451. }
  452. #endif /* CONFIG_OF_LIBFDT */
  453. #if defined(CONFIG_OF_FLAT_TREE)
  454. #ifdef CFG_BOOTMAPSZ
  455. /*
  456. * The blob must be within CFG_BOOTMAPSZ,
  457. * so we flag it to be copied if it is not.
  458. */
  459. if (of_flat_tree >= (char *)CFG_BOOTMAPSZ)
  460. of_data = (ulong)of_flat_tree;
  461. #endif
  462. /* move of_flat_tree if needed */
  463. if (of_data) {
  464. ulong of_start, of_len;
  465. of_len = ((struct boot_param_header *)of_data)->totalsize;
  466. /* provide extra 8k pad */
  467. of_start = (ulong)kbd - of_len - 8192;
  468. of_start &= ~(4096 - 1); /* align on page */
  469. debug ("## device tree at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
  470. of_data, of_data + of_len - 1, of_len, of_len);
  471. of_flat_tree = (char *)of_start;
  472. printf (" Loading Device Tree to %08lx, end %08lx ... ",
  473. of_start, of_start + of_len - 1);
  474. memmove ((void *)of_start, (void *)of_data, of_len);
  475. puts ("OK\n");
  476. }
  477. /*
  478. * Create the /chosen node and modify the blob with board specific
  479. * values as needed.
  480. */
  481. ft_setup(of_flat_tree, kbd, initrd_start, initrd_end);
  482. /* ft_dump_blob(of_flat_tree); */
  483. #endif
  484. debug ("## Transferring control to Linux (at address %08lx) ...\n",
  485. (ulong)kernel);
  486. show_boot_progress (15);
  487. #if defined(CFG_INIT_RAM_LOCK) && !defined(CONFIG_E500)
  488. unlock_ram_in_cache();
  489. #endif
  490. #if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
  491. if (of_flat_tree) { /* device tree; boot new style */
  492. /*
  493. * Linux Kernel Parameters (passing device tree):
  494. * r3: pointer to the fdt, followed by the board info data
  495. * r4: physical pointer to the kernel itself
  496. * r5: NULL
  497. * r6: NULL
  498. * r7: NULL
  499. */
  500. (*kernel) ((bd_t *)of_flat_tree, (ulong)kernel, 0, 0, 0);
  501. /* does not return */
  502. }
  503. #endif
  504. /*
  505. * Linux Kernel Parameters (passing board info data):
  506. * r3: ptr to board info data
  507. * r4: initrd_start or 0 if no initrd
  508. * r5: initrd_end - unused if r4 is 0
  509. * r6: Start of command line string
  510. * r7: End of command line string
  511. */
  512. (*kernel) (kbd, initrd_start, initrd_end, cmd_start, cmd_end);
  513. /* does not return */
  514. }