cmd_bootm.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630
  1. /*
  2. * (C) Copyright 2000-2006
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. /*
  24. * Boot support
  25. */
  26. #include <common.h>
  27. #include <watchdog.h>
  28. #include <command.h>
  29. #include <image.h>
  30. #include <malloc.h>
  31. #include <zlib.h>
  32. #include <bzlib.h>
  33. #include <environment.h>
  34. #include <asm/byteorder.h>
  35. #if defined(CONFIG_OF_LIBFDT)
  36. #include <fdt.h>
  37. #include <libfdt.h>
  38. #include <fdt_support.h>
  39. #endif
  40. #if defined(CONFIG_OF_FLAT_TREE)
  41. #include <ft_build.h>
  42. #endif
  43. DECLARE_GLOBAL_DATA_PTR;
  44. /*cmd_boot.c*/
  45. extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
  46. #if (CONFIG_COMMANDS & CFG_CMD_DATE) || defined(CONFIG_TIMESTAMP)
  47. #include <rtc.h>
  48. #endif
  49. #ifdef CFG_HUSH_PARSER
  50. #include <hush.h>
  51. #endif
  52. #ifdef CONFIG_SHOW_BOOT_PROGRESS
  53. # include <status_led.h>
  54. # define SHOW_BOOT_PROGRESS(arg) show_boot_progress(arg)
  55. #else
  56. # define SHOW_BOOT_PROGRESS(arg)
  57. #endif
  58. #ifdef CFG_INIT_RAM_LOCK
  59. #include <asm/cache.h>
  60. #endif
  61. #ifdef CONFIG_LOGBUFFER
  62. #include <logbuff.h>
  63. #endif
  64. #ifdef CONFIG_HAS_DATAFLASH
  65. #include <dataflash.h>
  66. #endif
  67. /*
  68. * Some systems (for example LWMON) have very short watchdog periods;
  69. * we must make sure to split long operations like memmove() or
  70. * crc32() into reasonable chunks.
  71. */
  72. #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
  73. # define CHUNKSZ (64 * 1024)
  74. #endif
  75. int gunzip (void *, int, unsigned char *, unsigned long *);
  76. static void *zalloc(void *, unsigned, unsigned);
  77. static void zfree(void *, void *, unsigned);
  78. #if (CONFIG_COMMANDS & CFG_CMD_IMI)
  79. static int image_info (unsigned long addr);
  80. #endif
  81. #if (CONFIG_COMMANDS & CFG_CMD_IMLS)
  82. #include <flash.h>
  83. extern flash_info_t flash_info[]; /* info for FLASH chips */
  84. static int do_imls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
  85. #endif
  86. static void print_type (image_header_t *hdr);
  87. #ifdef __I386__
  88. image_header_t *fake_header(image_header_t *hdr, void *ptr, int size);
  89. #endif
  90. /*
  91. * Continue booting an OS image; caller already has:
  92. * - copied image header to global variable `header'
  93. * - checked header magic number, checksums (both header & image),
  94. * - verified image architecture (PPC) and type (KERNEL or MULTI),
  95. * - loaded (first part of) image to header load address,
  96. * - disabled interrupts.
  97. */
  98. typedef void boot_os_Fcn (cmd_tbl_t *cmdtp, int flag,
  99. int argc, char *argv[],
  100. ulong addr, /* of image to boot */
  101. ulong *len_ptr, /* multi-file image length table */
  102. int verify); /* getenv("verify")[0] != 'n' */
  103. #ifdef DEBUG
  104. extern int do_bdinfo ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
  105. #endif
  106. #ifdef CONFIG_PPC
  107. static boot_os_Fcn do_bootm_linux;
  108. #else
  109. extern boot_os_Fcn do_bootm_linux;
  110. #endif
  111. #ifdef CONFIG_SILENT_CONSOLE
  112. static void fixup_silent_linux (void);
  113. #endif
  114. static boot_os_Fcn do_bootm_netbsd;
  115. static boot_os_Fcn do_bootm_rtems;
  116. #if (CONFIG_COMMANDS & CFG_CMD_ELF)
  117. static boot_os_Fcn do_bootm_vxworks;
  118. static boot_os_Fcn do_bootm_qnxelf;
  119. int do_bootvx ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[] );
  120. int do_bootelf (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[] );
  121. #endif /* CFG_CMD_ELF */
  122. #if defined(CONFIG_ARTOS) && defined(CONFIG_PPC)
  123. static boot_os_Fcn do_bootm_artos;
  124. #endif
  125. #ifdef CONFIG_LYNXKDI
  126. static boot_os_Fcn do_bootm_lynxkdi;
  127. extern void lynxkdi_boot( image_header_t * );
  128. #endif
  129. #ifndef CFG_BOOTM_LEN
  130. #define CFG_BOOTM_LEN 0x800000 /* use 8MByte as default max gunzip size */
  131. #endif
  132. image_header_t header;
  133. ulong load_addr = CFG_LOAD_ADDR; /* Default Load Address */
  134. int do_bootm (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  135. {
  136. ulong iflag;
  137. ulong addr;
  138. ulong data, len, checksum;
  139. ulong *len_ptr;
  140. uint unc_len = CFG_BOOTM_LEN;
  141. int i, verify;
  142. char *name, *s;
  143. int (*appl)(int, char *[]);
  144. image_header_t *hdr = &header;
  145. s = getenv ("verify");
  146. verify = (s && (*s == 'n')) ? 0 : 1;
  147. if (argc < 2) {
  148. addr = load_addr;
  149. } else {
  150. addr = simple_strtoul(argv[1], NULL, 16);
  151. }
  152. SHOW_BOOT_PROGRESS (1);
  153. printf ("## Booting image at %08lx ...\n", addr);
  154. /* Copy header so we can blank CRC field for re-calculation */
  155. #ifdef CONFIG_HAS_DATAFLASH
  156. if (addr_dataflash(addr)){
  157. read_dataflash(addr, sizeof(image_header_t), (char *)&header);
  158. } else
  159. #endif
  160. memmove (&header, (char *)addr, sizeof(image_header_t));
  161. if (ntohl(hdr->ih_magic) != IH_MAGIC) {
  162. #ifdef __I386__ /* correct image format not implemented yet - fake it */
  163. if (fake_header(hdr, (void*)addr, -1) != NULL) {
  164. /* to compensate for the addition below */
  165. addr -= sizeof(image_header_t);
  166. /* turnof verify,
  167. * fake_header() does not fake the data crc
  168. */
  169. verify = 0;
  170. } else
  171. #endif /* __I386__ */
  172. {
  173. puts ("Bad Magic Number\n");
  174. SHOW_BOOT_PROGRESS (-1);
  175. return 1;
  176. }
  177. }
  178. SHOW_BOOT_PROGRESS (2);
  179. data = (ulong)&header;
  180. len = sizeof(image_header_t);
  181. checksum = ntohl(hdr->ih_hcrc);
  182. hdr->ih_hcrc = 0;
  183. if (crc32 (0, (uchar *)data, len) != checksum) {
  184. puts ("Bad Header Checksum\n");
  185. SHOW_BOOT_PROGRESS (-2);
  186. return 1;
  187. }
  188. SHOW_BOOT_PROGRESS (3);
  189. #ifdef CONFIG_HAS_DATAFLASH
  190. if (addr_dataflash(addr)){
  191. len = ntohl(hdr->ih_size) + sizeof(image_header_t);
  192. read_dataflash(addr, len, (char *)CFG_LOAD_ADDR);
  193. addr = CFG_LOAD_ADDR;
  194. }
  195. #endif
  196. /* for multi-file images we need the data part, too */
  197. print_image_hdr ((image_header_t *)addr);
  198. data = addr + sizeof(image_header_t);
  199. len = ntohl(hdr->ih_size);
  200. if (verify) {
  201. puts (" Verifying Checksum ... ");
  202. if (crc32 (0, (uchar *)data, len) != ntohl(hdr->ih_dcrc)) {
  203. printf ("Bad Data CRC\n");
  204. SHOW_BOOT_PROGRESS (-3);
  205. return 1;
  206. }
  207. puts ("OK\n");
  208. }
  209. SHOW_BOOT_PROGRESS (4);
  210. len_ptr = (ulong *)data;
  211. #if defined(__ARM__)
  212. if (hdr->ih_arch != IH_CPU_ARM)
  213. #elif defined(__avr32__)
  214. if (hdr->ih_arch != IH_CPU_AVR32)
  215. #elif defined(__bfin__)
  216. if (hdr->ih_arch != IH_CPU_BLACKFIN)
  217. #elif defined(__I386__)
  218. if (hdr->ih_arch != IH_CPU_I386)
  219. #elif defined(__M68K__)
  220. if (hdr->ih_arch != IH_CPU_M68K)
  221. #elif defined(__microblaze__)
  222. if (hdr->ih_arch != IH_CPU_MICROBLAZE)
  223. #elif defined(__mips__)
  224. if (hdr->ih_arch != IH_CPU_MIPS)
  225. #elif defined(__nios__)
  226. if (hdr->ih_arch != IH_CPU_NIOS)
  227. #elif defined(__nios2__)
  228. if (hdr->ih_arch != IH_CPU_NIOS2)
  229. #elif defined(__PPC__)
  230. if (hdr->ih_arch != IH_CPU_PPC)
  231. #else
  232. # error Unknown CPU type
  233. #endif
  234. {
  235. printf ("Unsupported Architecture 0x%x\n", hdr->ih_arch);
  236. SHOW_BOOT_PROGRESS (-4);
  237. return 1;
  238. }
  239. SHOW_BOOT_PROGRESS (5);
  240. switch (hdr->ih_type) {
  241. case IH_TYPE_STANDALONE:
  242. name = "Standalone Application";
  243. /* A second argument overwrites the load address */
  244. if (argc > 2) {
  245. hdr->ih_load = htonl(simple_strtoul(argv[2], NULL, 16));
  246. }
  247. break;
  248. case IH_TYPE_KERNEL:
  249. name = "Kernel Image";
  250. break;
  251. case IH_TYPE_MULTI:
  252. name = "Multi-File Image";
  253. len = ntohl(len_ptr[0]);
  254. /* OS kernel is always the first image */
  255. data += 8; /* kernel_len + terminator */
  256. for (i=1; len_ptr[i]; ++i)
  257. data += 4;
  258. break;
  259. default: printf ("Wrong Image Type for %s command\n", cmdtp->name);
  260. SHOW_BOOT_PROGRESS (-5);
  261. return 1;
  262. }
  263. SHOW_BOOT_PROGRESS (6);
  264. /*
  265. * We have reached the point of no return: we are going to
  266. * overwrite all exception vector code, so we cannot easily
  267. * recover from any failures any more...
  268. */
  269. iflag = disable_interrupts();
  270. #ifdef CONFIG_AMIGAONEG3SE
  271. /*
  272. * We've possible left the caches enabled during
  273. * bios emulation, so turn them off again
  274. */
  275. icache_disable();
  276. invalidate_l1_instruction_cache();
  277. flush_data_cache();
  278. dcache_disable();
  279. #endif
  280. switch (hdr->ih_comp) {
  281. case IH_COMP_NONE:
  282. if(ntohl(hdr->ih_load) == addr) {
  283. printf (" XIP %s ... ", name);
  284. } else {
  285. #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
  286. size_t l = len;
  287. void *to = (void *)ntohl(hdr->ih_load);
  288. void *from = (void *)data;
  289. printf (" Loading %s ... ", name);
  290. while (l > 0) {
  291. size_t tail = (l > CHUNKSZ) ? CHUNKSZ : l;
  292. WATCHDOG_RESET();
  293. memmove (to, from, tail);
  294. to += tail;
  295. from += tail;
  296. l -= tail;
  297. }
  298. #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
  299. memmove ((void *) ntohl(hdr->ih_load), (uchar *)data, len);
  300. #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
  301. }
  302. break;
  303. case IH_COMP_GZIP:
  304. printf (" Uncompressing %s ... ", name);
  305. if (gunzip ((void *)ntohl(hdr->ih_load), unc_len,
  306. (uchar *)data, &len) != 0) {
  307. puts ("GUNZIP ERROR - must RESET board to recover\n");
  308. SHOW_BOOT_PROGRESS (-6);
  309. do_reset (cmdtp, flag, argc, argv);
  310. }
  311. break;
  312. #ifdef CONFIG_BZIP2
  313. case IH_COMP_BZIP2:
  314. printf (" Uncompressing %s ... ", name);
  315. /*
  316. * If we've got less than 4 MB of malloc() space,
  317. * use slower decompression algorithm which requires
  318. * at most 2300 KB of memory.
  319. */
  320. i = BZ2_bzBuffToBuffDecompress ((char*)ntohl(hdr->ih_load),
  321. &unc_len, (char *)data, len,
  322. CFG_MALLOC_LEN < (4096 * 1024), 0);
  323. if (i != BZ_OK) {
  324. printf ("BUNZIP2 ERROR %d - must RESET board to recover\n", i);
  325. SHOW_BOOT_PROGRESS (-6);
  326. udelay(100000);
  327. do_reset (cmdtp, flag, argc, argv);
  328. }
  329. break;
  330. #endif /* CONFIG_BZIP2 */
  331. default:
  332. if (iflag)
  333. enable_interrupts();
  334. printf ("Unimplemented compression type %d\n", hdr->ih_comp);
  335. SHOW_BOOT_PROGRESS (-7);
  336. return 1;
  337. }
  338. puts ("OK\n");
  339. SHOW_BOOT_PROGRESS (7);
  340. switch (hdr->ih_type) {
  341. case IH_TYPE_STANDALONE:
  342. if (iflag)
  343. enable_interrupts();
  344. /* load (and uncompress), but don't start if "autostart"
  345. * is set to "no"
  346. */
  347. if (((s = getenv("autostart")) != NULL) && (strcmp(s,"no") == 0)) {
  348. char buf[32];
  349. sprintf(buf, "%lX", len);
  350. setenv("filesize", buf);
  351. return 0;
  352. }
  353. appl = (int (*)(int, char *[]))ntohl(hdr->ih_ep);
  354. (*appl)(argc-1, &argv[1]);
  355. return 0;
  356. case IH_TYPE_KERNEL:
  357. case IH_TYPE_MULTI:
  358. /* handled below */
  359. break;
  360. default:
  361. if (iflag)
  362. enable_interrupts();
  363. printf ("Can't boot image type %d\n", hdr->ih_type);
  364. SHOW_BOOT_PROGRESS (-8);
  365. return 1;
  366. }
  367. SHOW_BOOT_PROGRESS (8);
  368. switch (hdr->ih_os) {
  369. default: /* handled by (original) Linux case */
  370. case IH_OS_LINUX:
  371. #ifdef CONFIG_SILENT_CONSOLE
  372. fixup_silent_linux();
  373. #endif
  374. do_bootm_linux (cmdtp, flag, argc, argv,
  375. addr, len_ptr, verify);
  376. break;
  377. case IH_OS_NETBSD:
  378. do_bootm_netbsd (cmdtp, flag, argc, argv,
  379. addr, len_ptr, verify);
  380. break;
  381. #ifdef CONFIG_LYNXKDI
  382. case IH_OS_LYNXOS:
  383. do_bootm_lynxkdi (cmdtp, flag, argc, argv,
  384. addr, len_ptr, verify);
  385. break;
  386. #endif
  387. case IH_OS_RTEMS:
  388. do_bootm_rtems (cmdtp, flag, argc, argv,
  389. addr, len_ptr, verify);
  390. break;
  391. #if (CONFIG_COMMANDS & CFG_CMD_ELF)
  392. case IH_OS_VXWORKS:
  393. do_bootm_vxworks (cmdtp, flag, argc, argv,
  394. addr, len_ptr, verify);
  395. break;
  396. case IH_OS_QNX:
  397. do_bootm_qnxelf (cmdtp, flag, argc, argv,
  398. addr, len_ptr, verify);
  399. break;
  400. #endif /* CFG_CMD_ELF */
  401. #ifdef CONFIG_ARTOS
  402. case IH_OS_ARTOS:
  403. do_bootm_artos (cmdtp, flag, argc, argv,
  404. addr, len_ptr, verify);
  405. break;
  406. #endif
  407. }
  408. SHOW_BOOT_PROGRESS (-9);
  409. #ifdef DEBUG
  410. puts ("\n## Control returned to monitor - resetting...\n");
  411. do_reset (cmdtp, flag, argc, argv);
  412. #endif
  413. return 1;
  414. }
  415. U_BOOT_CMD(
  416. bootm, CFG_MAXARGS, 1, do_bootm,
  417. "bootm - boot application image from memory\n",
  418. "[addr [arg ...]]\n - boot application image stored in memory\n"
  419. "\tpassing arguments 'arg ...'; when booting a Linux kernel,\n"
  420. "\t'arg' can be the address of an initrd image\n"
  421. #if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
  422. "\tWhen booting a Linux kernel which requires a flat device-tree\n"
  423. "\ta third argument is required which is the address of the of the\n"
  424. "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
  425. "\tuse a '-' for the second argument. If you do not pass a third\n"
  426. "\ta bd_info struct will be passed instead\n"
  427. #endif
  428. );
  429. #ifdef CONFIG_SILENT_CONSOLE
  430. static void
  431. fixup_silent_linux ()
  432. {
  433. char buf[256], *start, *end;
  434. char *cmdline = getenv ("bootargs");
  435. /* Only fix cmdline when requested */
  436. if (!(gd->flags & GD_FLG_SILENT))
  437. return;
  438. debug ("before silent fix-up: %s\n", cmdline);
  439. if (cmdline) {
  440. if ((start = strstr (cmdline, "console=")) != NULL) {
  441. end = strchr (start, ' ');
  442. strncpy (buf, cmdline, (start - cmdline + 8));
  443. if (end)
  444. strcpy (buf + (start - cmdline + 8), end);
  445. else
  446. buf[start - cmdline + 8] = '\0';
  447. } else {
  448. strcpy (buf, cmdline);
  449. strcat (buf, " console=");
  450. }
  451. } else {
  452. strcpy (buf, "console=");
  453. }
  454. setenv ("bootargs", buf);
  455. debug ("after silent fix-up: %s\n", buf);
  456. }
  457. #endif /* CONFIG_SILENT_CONSOLE */
  458. #ifdef CONFIG_PPC
  459. static void __attribute__((noinline))
  460. do_bootm_linux (cmd_tbl_t *cmdtp, int flag,
  461. int argc, char *argv[],
  462. ulong addr,
  463. ulong *len_ptr,
  464. int verify)
  465. {
  466. ulong sp;
  467. ulong len, checksum;
  468. ulong initrd_start, initrd_end;
  469. ulong cmd_start, cmd_end;
  470. ulong initrd_high;
  471. ulong data;
  472. int initrd_copy_to_ram = 1;
  473. char *cmdline;
  474. char *s;
  475. bd_t *kbd;
  476. void (*kernel)(bd_t *, ulong, ulong, ulong, ulong);
  477. image_header_t *hdr = &header;
  478. #if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
  479. char *of_flat_tree = NULL;
  480. ulong of_data = 0;
  481. #endif
  482. if ((s = getenv ("initrd_high")) != NULL) {
  483. /* a value of "no" or a similar string will act like 0,
  484. * turning the "load high" feature off. This is intentional.
  485. */
  486. initrd_high = simple_strtoul(s, NULL, 16);
  487. if (initrd_high == ~0)
  488. initrd_copy_to_ram = 0;
  489. } else { /* not set, no restrictions to load high */
  490. initrd_high = ~0;
  491. }
  492. #ifdef CONFIG_LOGBUFFER
  493. kbd=gd->bd;
  494. /* Prevent initrd from overwriting logbuffer */
  495. if (initrd_high < (kbd->bi_memsize-LOGBUFF_LEN-LOGBUFF_OVERHEAD))
  496. initrd_high = kbd->bi_memsize-LOGBUFF_LEN-LOGBUFF_OVERHEAD;
  497. debug ("## Logbuffer at 0x%08lX ", kbd->bi_memsize-LOGBUFF_LEN);
  498. #endif
  499. /*
  500. * Booting a (Linux) kernel image
  501. *
  502. * Allocate space for command line and board info - the
  503. * address should be as high as possible within the reach of
  504. * the kernel (see CFG_BOOTMAPSZ settings), but in unused
  505. * memory, which means far enough below the current stack
  506. * pointer.
  507. */
  508. asm( "mr %0,1": "=r"(sp) : );
  509. debug ("## Current stack ends at 0x%08lX ", sp);
  510. sp -= 2048; /* just to be sure */
  511. if (sp > CFG_BOOTMAPSZ)
  512. sp = CFG_BOOTMAPSZ;
  513. sp &= ~0xF;
  514. debug ("=> set upper limit to 0x%08lX\n", sp);
  515. cmdline = (char *)((sp - CFG_BARGSIZE) & ~0xF);
  516. kbd = (bd_t *)(((ulong)cmdline - sizeof(bd_t)) & ~0xF);
  517. if ((s = getenv("bootargs")) == NULL)
  518. s = "";
  519. strcpy (cmdline, s);
  520. cmd_start = (ulong)&cmdline[0];
  521. cmd_end = cmd_start + strlen(cmdline);
  522. *kbd = *(gd->bd);
  523. #ifdef DEBUG
  524. printf ("## cmdline at 0x%08lX ... 0x%08lX\n", cmd_start, cmd_end);
  525. do_bdinfo (NULL, 0, 0, NULL);
  526. #endif
  527. if ((s = getenv ("clocks_in_mhz")) != NULL) {
  528. /* convert all clock information to MHz */
  529. kbd->bi_intfreq /= 1000000L;
  530. kbd->bi_busfreq /= 1000000L;
  531. #if defined(CONFIG_MPC8220)
  532. kbd->bi_inpfreq /= 1000000L;
  533. kbd->bi_pcifreq /= 1000000L;
  534. kbd->bi_pevfreq /= 1000000L;
  535. kbd->bi_flbfreq /= 1000000L;
  536. kbd->bi_vcofreq /= 1000000L;
  537. #endif
  538. #if defined(CONFIG_CPM2)
  539. kbd->bi_cpmfreq /= 1000000L;
  540. kbd->bi_brgfreq /= 1000000L;
  541. kbd->bi_sccfreq /= 1000000L;
  542. kbd->bi_vco /= 1000000L;
  543. #endif
  544. #if defined(CONFIG_MPC5xxx)
  545. kbd->bi_ipbfreq /= 1000000L;
  546. kbd->bi_pcifreq /= 1000000L;
  547. #endif /* CONFIG_MPC5xxx */
  548. }
  549. kernel = (void (*)(bd_t *, ulong, ulong, ulong, ulong)) ntohl(hdr->ih_ep);
  550. /*
  551. * Check if there is an initrd image
  552. */
  553. #if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
  554. /* Look for a '-' which indicates to ignore the ramdisk argument */
  555. if (argc >= 3 && strcmp(argv[2], "-") == 0) {
  556. debug ("Skipping initrd\n");
  557. len = data = 0;
  558. }
  559. else
  560. #endif
  561. if (argc >= 3) {
  562. debug ("Not skipping initrd\n");
  563. SHOW_BOOT_PROGRESS (9);
  564. addr = simple_strtoul(argv[2], NULL, 16);
  565. printf ("## Loading RAMDisk Image at %08lx ...\n", addr);
  566. /* Copy header so we can blank CRC field for re-calculation */
  567. memmove (&header, (char *)addr, sizeof(image_header_t));
  568. if (ntohl(hdr->ih_magic) != IH_MAGIC) {
  569. puts ("Bad Magic Number\n");
  570. SHOW_BOOT_PROGRESS (-10);
  571. do_reset (cmdtp, flag, argc, argv);
  572. }
  573. data = (ulong)&header;
  574. len = sizeof(image_header_t);
  575. checksum = ntohl(hdr->ih_hcrc);
  576. hdr->ih_hcrc = 0;
  577. if (crc32 (0, (uchar *)data, len) != checksum) {
  578. puts ("Bad Header Checksum\n");
  579. SHOW_BOOT_PROGRESS (-11);
  580. do_reset (cmdtp, flag, argc, argv);
  581. }
  582. SHOW_BOOT_PROGRESS (10);
  583. print_image_hdr (hdr);
  584. data = addr + sizeof(image_header_t);
  585. len = ntohl(hdr->ih_size);
  586. if (verify) {
  587. ulong csum = 0;
  588. #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
  589. ulong cdata = data, edata = cdata + len;
  590. #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
  591. puts (" Verifying Checksum ... ");
  592. #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
  593. while (cdata < edata) {
  594. ulong chunk = edata - cdata;
  595. if (chunk > CHUNKSZ)
  596. chunk = CHUNKSZ;
  597. csum = crc32 (csum, (uchar *)cdata, chunk);
  598. cdata += chunk;
  599. WATCHDOG_RESET();
  600. }
  601. #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
  602. csum = crc32 (0, (uchar *)data, len);
  603. #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
  604. if (csum != ntohl(hdr->ih_dcrc)) {
  605. puts ("Bad Data CRC\n");
  606. SHOW_BOOT_PROGRESS (-12);
  607. do_reset (cmdtp, flag, argc, argv);
  608. }
  609. puts ("OK\n");
  610. }
  611. SHOW_BOOT_PROGRESS (11);
  612. if ((hdr->ih_os != IH_OS_LINUX) ||
  613. (hdr->ih_arch != IH_CPU_PPC) ||
  614. (hdr->ih_type != IH_TYPE_RAMDISK) ) {
  615. puts ("No Linux PPC Ramdisk Image\n");
  616. SHOW_BOOT_PROGRESS (-13);
  617. do_reset (cmdtp, flag, argc, argv);
  618. }
  619. /*
  620. * Now check if we have a multifile image
  621. */
  622. } else if ((hdr->ih_type==IH_TYPE_MULTI) && (len_ptr[1])) {
  623. u_long tail = ntohl(len_ptr[0]) % 4;
  624. int i;
  625. SHOW_BOOT_PROGRESS (13);
  626. /* skip kernel length and terminator */
  627. data = (ulong)(&len_ptr[2]);
  628. /* skip any additional image length fields */
  629. for (i=1; len_ptr[i]; ++i)
  630. data += 4;
  631. /* add kernel length, and align */
  632. data += ntohl(len_ptr[0]);
  633. if (tail) {
  634. data += 4 - tail;
  635. }
  636. len = ntohl(len_ptr[1]);
  637. } else {
  638. /*
  639. * no initrd image
  640. */
  641. SHOW_BOOT_PROGRESS (14);
  642. len = data = 0;
  643. }
  644. #if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
  645. if(argc > 3) {
  646. of_flat_tree = (char *) simple_strtoul(argv[3], NULL, 16);
  647. hdr = (image_header_t *)of_flat_tree;
  648. #if defined(CONFIG_OF_LIBFDT)
  649. if (fdt_check_header(of_flat_tree) == 0) {
  650. #else
  651. if (*(ulong *)of_flat_tree == OF_DT_HEADER) {
  652. #endif
  653. #ifndef CFG_NO_FLASH
  654. if (addr2info((ulong)of_flat_tree) != NULL)
  655. of_data = (ulong)of_flat_tree;
  656. #endif
  657. } else if (ntohl(hdr->ih_magic) == IH_MAGIC) {
  658. printf("## Flat Device Tree Image at %08lX\n", hdr);
  659. print_image_hdr(hdr);
  660. if ((ntohl(hdr->ih_load) < ((unsigned long)hdr + ntohl(hdr->ih_size) + sizeof(hdr))) &&
  661. ((ntohl(hdr->ih_load) + ntohl(hdr->ih_size)) > (unsigned long)hdr)) {
  662. printf ("ERROR: Load address overwrites Flat Device Tree uImage\n");
  663. return;
  664. }
  665. printf(" Verifying Checksum ... ");
  666. memmove (&header, (char *)hdr, sizeof(image_header_t));
  667. checksum = ntohl(header.ih_hcrc);
  668. header.ih_hcrc = 0;
  669. if(checksum != crc32(0, (uchar *)&header, sizeof(image_header_t))) {
  670. printf("ERROR: Flat Device Tree header checksum is invalid\n");
  671. return;
  672. }
  673. checksum = ntohl(hdr->ih_dcrc);
  674. addr = (ulong)((uchar *)(hdr) + sizeof(image_header_t));
  675. if(checksum != crc32(0, (uchar *)addr, ntohl(hdr->ih_size))) {
  676. printf("ERROR: Flat Device Tree checksum is invalid\n");
  677. return;
  678. }
  679. printf("OK\n");
  680. if (ntohl(hdr->ih_type) != IH_TYPE_FLATDT) {
  681. printf ("ERROR: uImage not Flat Device Tree type\n");
  682. return;
  683. }
  684. if (ntohl(hdr->ih_comp) != IH_COMP_NONE) {
  685. printf("ERROR: uImage is not uncompressed\n");
  686. return;
  687. }
  688. #if defined(CONFIG_OF_LIBFDT)
  689. if (fdt_check_header(of_flat_tree + sizeof(image_header_t)) == 0) {
  690. #else
  691. if (*((ulong *)(of_flat_tree + sizeof(image_header_t))) != OF_DT_HEADER) {
  692. #endif
  693. printf ("ERROR: uImage data is not a flat device tree\n");
  694. return;
  695. }
  696. memmove((void *)ntohl(hdr->ih_load),
  697. (void *)(of_flat_tree + sizeof(image_header_t)),
  698. ntohl(hdr->ih_size));
  699. of_flat_tree = (char *)ntohl(hdr->ih_load);
  700. } else {
  701. printf ("Did not find a flat flat device tree at address %08lX\n", of_flat_tree);
  702. return;
  703. }
  704. printf (" Booting using flat device tree at 0x%x\n",
  705. of_flat_tree);
  706. } else if ((hdr->ih_type==IH_TYPE_MULTI) && (len_ptr[1]) && (len_ptr[2])) {
  707. u_long tail = ntohl(len_ptr[0]) % 4;
  708. int i;
  709. /* skip kernel length, initrd length, and terminator */
  710. of_data = (ulong)(&len_ptr[3]);
  711. /* skip any additional image length fields */
  712. for (i=2; len_ptr[i]; ++i)
  713. of_data += 4;
  714. /* add kernel length, and align */
  715. of_data += ntohl(len_ptr[0]);
  716. if (tail) {
  717. of_data += 4 - tail;
  718. }
  719. /* add initrd length, and align */
  720. tail = ntohl(len_ptr[1]) % 4;
  721. of_data += ntohl(len_ptr[1]);
  722. if (tail) {
  723. of_data += 4 - tail;
  724. }
  725. #if defined(CONFIG_OF_LIBFDT)
  726. if (fdt_check_header((void *)of_data) != 0) {
  727. #else
  728. if (((struct boot_param_header *)of_data)->magic != OF_DT_HEADER) {
  729. #endif
  730. printf ("ERROR: image is not a flat device tree\n");
  731. return;
  732. }
  733. #if defined(CONFIG_OF_LIBFDT)
  734. if (be32_to_cpu(fdt_totalsize(of_data)) != ntohl(len_ptr[2])) {
  735. #else
  736. if (((struct boot_param_header *)of_data)->totalsize != ntohl(len_ptr[2])) {
  737. #endif
  738. printf ("ERROR: flat device tree size does not agree with image\n");
  739. return;
  740. }
  741. }
  742. #endif
  743. if (!data) {
  744. debug ("No initrd\n");
  745. }
  746. if (data) {
  747. if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
  748. initrd_start = data;
  749. initrd_end = initrd_start + len;
  750. } else {
  751. initrd_start = (ulong)kbd - len;
  752. initrd_start &= ~(4096 - 1); /* align on page */
  753. if (initrd_high) {
  754. ulong nsp;
  755. /*
  756. * the inital ramdisk does not need to be within
  757. * CFG_BOOTMAPSZ as it is not accessed until after
  758. * the mm system is initialised.
  759. *
  760. * do the stack bottom calculation again and see if
  761. * the initrd will fit just below the monitor stack
  762. * bottom without overwriting the area allocated
  763. * above for command line args and board info.
  764. */
  765. asm( "mr %0,1": "=r"(nsp) : );
  766. nsp -= 2048; /* just to be sure */
  767. nsp &= ~0xF;
  768. if (nsp > initrd_high) /* limit as specified */
  769. nsp = initrd_high;
  770. nsp -= len;
  771. nsp &= ~(4096 - 1); /* align on page */
  772. if (nsp >= sp)
  773. initrd_start = nsp;
  774. }
  775. SHOW_BOOT_PROGRESS (12);
  776. debug ("## initrd at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
  777. data, data + len - 1, len, len);
  778. initrd_end = initrd_start + len;
  779. printf (" Loading Ramdisk to %08lx, end %08lx ... ",
  780. initrd_start, initrd_end);
  781. #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
  782. {
  783. size_t l = len;
  784. void *to = (void *)initrd_start;
  785. void *from = (void *)data;
  786. while (l > 0) {
  787. size_t tail = (l > CHUNKSZ) ? CHUNKSZ : l;
  788. WATCHDOG_RESET();
  789. memmove (to, from, tail);
  790. to += tail;
  791. from += tail;
  792. l -= tail;
  793. }
  794. }
  795. #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
  796. memmove ((void *)initrd_start, (void *)data, len);
  797. #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
  798. puts ("OK\n");
  799. }
  800. } else {
  801. initrd_start = 0;
  802. initrd_end = 0;
  803. }
  804. debug ("## Transferring control to Linux (at address %08lx) ...\n",
  805. (ulong)kernel);
  806. SHOW_BOOT_PROGRESS (15);
  807. #if defined(CFG_INIT_RAM_LOCK) && !defined(CONFIG_E500)
  808. unlock_ram_in_cache();
  809. #endif
  810. #if defined(CONFIG_OF_LIBFDT)
  811. /* move of_flat_tree if needed */
  812. if (of_data) {
  813. int err;
  814. ulong of_start, of_len;
  815. of_len = be32_to_cpu(fdt_totalsize(of_data));
  816. /* position on a 4K boundary before the initrd/kbd */
  817. if (initrd_start)
  818. of_start = initrd_start - of_len;
  819. else
  820. of_start = (ulong)kbd - of_len;
  821. of_start &= ~(4096 - 1); /* align on page */
  822. debug ("## device tree at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
  823. of_data, of_data + of_len - 1, of_len, of_len);
  824. of_flat_tree = (char *)of_start;
  825. printf (" Loading Device Tree to %08lx, end %08lx ... ",
  826. of_start, of_start + of_len - 1);
  827. err = fdt_open_into((void *)of_start, (void *)of_data, of_len);
  828. if (err != 0) {
  829. printf ("libfdt: %s " __FILE__ " %d\n", fdt_strerror(err), __LINE__);
  830. }
  831. /*
  832. * Add the chosen node if it doesn't exist, add the env and bd_t
  833. * if the user wants it (the logic is in the subroutines).
  834. */
  835. if (fdt_chosen(of_flat_tree, initrd_start, initrd_end, 0) < 0) {
  836. printf("Failed creating the /chosen node (0x%08X), aborting.\n", of_flat_tree);
  837. return;
  838. }
  839. #ifdef CONFIG_OF_HAS_UBOOT_ENV
  840. if (fdt_env(of_flat_tree) < 0) {
  841. printf("Failed creating the /u-boot-env node, aborting.\n");
  842. return;
  843. }
  844. #endif
  845. #ifdef CONFIG_OF_HAS_BD_T
  846. if (fdt_bd_t(of_flat_tree) < 0) {
  847. printf("Failed creating the /bd_t node, aborting.\n");
  848. return;
  849. }
  850. #endif
  851. }
  852. #endif
  853. #if defined(CONFIG_OF_FLAT_TREE)
  854. /* move of_flat_tree if needed */
  855. if (of_data) {
  856. ulong of_start, of_len;
  857. of_len = ((struct boot_param_header *)of_data)->totalsize;
  858. /* provide extra 8k pad */
  859. if (initrd_start)
  860. of_start = initrd_start - of_len - 8192;
  861. else
  862. of_start = (ulong)kbd - of_len - 8192;
  863. of_start &= ~(4096 - 1); /* align on page */
  864. debug ("## device tree at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
  865. of_data, of_data + of_len - 1, of_len, of_len);
  866. of_flat_tree = (char *)of_start;
  867. printf (" Loading Device Tree to %08lx, end %08lx ... ",
  868. of_start, of_start + of_len - 1);
  869. memmove ((void *)of_start, (void *)of_data, of_len);
  870. }
  871. #endif
  872. /*
  873. * Linux Kernel Parameters (passing board info data):
  874. * r3: ptr to board info data
  875. * r4: initrd_start or 0 if no initrd
  876. * r5: initrd_end - unused if r4 is 0
  877. * r6: Start of command line string
  878. * r7: End of command line string
  879. */
  880. #if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
  881. if (!of_flat_tree) /* no device tree; boot old style */
  882. #endif
  883. (*kernel) (kbd, initrd_start, initrd_end, cmd_start, cmd_end);
  884. /* does not return */
  885. #if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
  886. /*
  887. * Linux Kernel Parameters (passing device tree):
  888. * r3: ptr to OF flat tree, followed by the board info data
  889. * r4: physical pointer to the kernel itself
  890. * r5: NULL
  891. * r6: NULL
  892. * r7: NULL
  893. */
  894. #if defined(CONFIG_OF_FLAT_TREE)
  895. ft_setup(of_flat_tree, kbd, initrd_start, initrd_end);
  896. /* ft_dump_blob(of_flat_tree); */
  897. #endif
  898. #if defined(CONFIG_OF_LIBFDT)
  899. if (fdt_chosen(of_flat_tree, initrd_start, initrd_end, 0) < 0) {
  900. printf("Failed creating the /chosen node (0x%08X), aborting.\n", of_flat_tree);
  901. return;
  902. }
  903. #ifdef CONFIG_OF_HAS_UBOOT_ENV
  904. if (fdt_env(of_flat_tree) < 0) {
  905. printf("Failed creating the /u-boot-env node, aborting.\n");
  906. return;
  907. }
  908. #endif
  909. #ifdef CONFIG_OF_HAS_BD_T
  910. if (fdt_bd_t(of_flat_tree) < 0) {
  911. printf("Failed creating the /bd_t node, aborting.\n");
  912. return;
  913. }
  914. #endif
  915. #endif /* if defined(CONFIG_OF_LIBFDT) */
  916. (*kernel) ((bd_t *)of_flat_tree, (ulong)kernel, 0, 0, 0);
  917. #endif
  918. }
  919. #endif /* CONFIG_PPC */
  920. static void
  921. do_bootm_netbsd (cmd_tbl_t *cmdtp, int flag,
  922. int argc, char *argv[],
  923. ulong addr,
  924. ulong *len_ptr,
  925. int verify)
  926. {
  927. image_header_t *hdr = &header;
  928. void (*loader)(bd_t *, image_header_t *, char *, char *);
  929. image_header_t *img_addr;
  930. char *consdev;
  931. char *cmdline;
  932. /*
  933. * Booting a (NetBSD) kernel image
  934. *
  935. * This process is pretty similar to a standalone application:
  936. * The (first part of an multi-) image must be a stage-2 loader,
  937. * which in turn is responsible for loading & invoking the actual
  938. * kernel. The only differences are the parameters being passed:
  939. * besides the board info strucure, the loader expects a command
  940. * line, the name of the console device, and (optionally) the
  941. * address of the original image header.
  942. */
  943. img_addr = 0;
  944. if ((hdr->ih_type==IH_TYPE_MULTI) && (len_ptr[1]))
  945. img_addr = (image_header_t *) addr;
  946. consdev = "";
  947. #if defined (CONFIG_8xx_CONS_SMC1)
  948. consdev = "smc1";
  949. #elif defined (CONFIG_8xx_CONS_SMC2)
  950. consdev = "smc2";
  951. #elif defined (CONFIG_8xx_CONS_SCC2)
  952. consdev = "scc2";
  953. #elif defined (CONFIG_8xx_CONS_SCC3)
  954. consdev = "scc3";
  955. #endif
  956. if (argc > 2) {
  957. ulong len;
  958. int i;
  959. for (i=2, len=0 ; i<argc ; i+=1)
  960. len += strlen (argv[i]) + 1;
  961. cmdline = malloc (len);
  962. for (i=2, len=0 ; i<argc ; i+=1) {
  963. if (i > 2)
  964. cmdline[len++] = ' ';
  965. strcpy (&cmdline[len], argv[i]);
  966. len += strlen (argv[i]);
  967. }
  968. } else if ((cmdline = getenv("bootargs")) == NULL) {
  969. cmdline = "";
  970. }
  971. loader = (void (*)(bd_t *, image_header_t *, char *, char *)) ntohl(hdr->ih_ep);
  972. printf ("## Transferring control to NetBSD stage-2 loader (at address %08lx) ...\n",
  973. (ulong)loader);
  974. SHOW_BOOT_PROGRESS (15);
  975. /*
  976. * NetBSD Stage-2 Loader Parameters:
  977. * r3: ptr to board info data
  978. * r4: image address
  979. * r5: console device
  980. * r6: boot args string
  981. */
  982. (*loader) (gd->bd, img_addr, consdev, cmdline);
  983. }
  984. #if defined(CONFIG_ARTOS) && defined(CONFIG_PPC)
  985. /* Function that returns a character from the environment */
  986. extern uchar (*env_get_char)(int);
  987. static void
  988. do_bootm_artos (cmd_tbl_t *cmdtp, int flag,
  989. int argc, char *argv[],
  990. ulong addr,
  991. ulong *len_ptr,
  992. int verify)
  993. {
  994. ulong top;
  995. char *s, *cmdline;
  996. char **fwenv, **ss;
  997. int i, j, nxt, len, envno, envsz;
  998. bd_t *kbd;
  999. void (*entry)(bd_t *bd, char *cmdline, char **fwenv, ulong top);
  1000. image_header_t *hdr = &header;
  1001. /*
  1002. * Booting an ARTOS kernel image + application
  1003. */
  1004. /* this used to be the top of memory, but was wrong... */
  1005. #ifdef CONFIG_PPC
  1006. /* get stack pointer */
  1007. asm volatile ("mr %0,1" : "=r"(top) );
  1008. #endif
  1009. debug ("## Current stack ends at 0x%08lX ", top);
  1010. top -= 2048; /* just to be sure */
  1011. if (top > CFG_BOOTMAPSZ)
  1012. top = CFG_BOOTMAPSZ;
  1013. top &= ~0xF;
  1014. debug ("=> set upper limit to 0x%08lX\n", top);
  1015. /* first check the artos specific boot args, then the linux args*/
  1016. if ((s = getenv("abootargs")) == NULL && (s = getenv("bootargs")) == NULL)
  1017. s = "";
  1018. /* get length of cmdline, and place it */
  1019. len = strlen(s);
  1020. top = (top - (len + 1)) & ~0xF;
  1021. cmdline = (char *)top;
  1022. debug ("## cmdline at 0x%08lX ", top);
  1023. strcpy(cmdline, s);
  1024. /* copy bdinfo */
  1025. top = (top - sizeof(bd_t)) & ~0xF;
  1026. debug ("## bd at 0x%08lX ", top);
  1027. kbd = (bd_t *)top;
  1028. memcpy(kbd, gd->bd, sizeof(bd_t));
  1029. /* first find number of env entries, and their size */
  1030. envno = 0;
  1031. envsz = 0;
  1032. for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
  1033. for (nxt = i; env_get_char(nxt) != '\0'; ++nxt)
  1034. ;
  1035. envno++;
  1036. envsz += (nxt - i) + 1; /* plus trailing zero */
  1037. }
  1038. envno++; /* plus the terminating zero */
  1039. debug ("## %u envvars total size %u ", envno, envsz);
  1040. top = (top - sizeof(char **)*envno) & ~0xF;
  1041. fwenv = (char **)top;
  1042. debug ("## fwenv at 0x%08lX ", top);
  1043. top = (top - envsz) & ~0xF;
  1044. s = (char *)top;
  1045. ss = fwenv;
  1046. /* now copy them */
  1047. for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
  1048. for (nxt = i; env_get_char(nxt) != '\0'; ++nxt)
  1049. ;
  1050. *ss++ = s;
  1051. for (j = i; j < nxt; ++j)
  1052. *s++ = env_get_char(j);
  1053. *s++ = '\0';
  1054. }
  1055. *ss++ = NULL; /* terminate */
  1056. entry = (void (*)(bd_t *, char *, char **, ulong))ntohl(hdr->ih_ep);
  1057. (*entry)(kbd, cmdline, fwenv, top);
  1058. }
  1059. #endif
  1060. #if (CONFIG_COMMANDS & CFG_CMD_BOOTD)
  1061. int do_bootd (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  1062. {
  1063. int rcode = 0;
  1064. #ifndef CFG_HUSH_PARSER
  1065. if (run_command (getenv ("bootcmd"), flag) < 0) rcode = 1;
  1066. #else
  1067. if (parse_string_outer(getenv("bootcmd"),
  1068. FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0 ) rcode = 1;
  1069. #endif
  1070. return rcode;
  1071. }
  1072. U_BOOT_CMD(
  1073. boot, 1, 1, do_bootd,
  1074. "boot - boot default, i.e., run 'bootcmd'\n",
  1075. NULL
  1076. );
  1077. /* keep old command name "bootd" for backward compatibility */
  1078. U_BOOT_CMD(
  1079. bootd, 1, 1, do_bootd,
  1080. "bootd - boot default, i.e., run 'bootcmd'\n",
  1081. NULL
  1082. );
  1083. #endif
  1084. #if (CONFIG_COMMANDS & CFG_CMD_IMI)
  1085. int do_iminfo ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  1086. {
  1087. int arg;
  1088. ulong addr;
  1089. int rcode=0;
  1090. if (argc < 2) {
  1091. return image_info (load_addr);
  1092. }
  1093. for (arg=1; arg <argc; ++arg) {
  1094. addr = simple_strtoul(argv[arg], NULL, 16);
  1095. if (image_info (addr) != 0) rcode = 1;
  1096. }
  1097. return rcode;
  1098. }
  1099. static int image_info (ulong addr)
  1100. {
  1101. ulong data, len, checksum;
  1102. image_header_t *hdr = &header;
  1103. printf ("\n## Checking Image at %08lx ...\n", addr);
  1104. /* Copy header so we can blank CRC field for re-calculation */
  1105. memmove (&header, (char *)addr, sizeof(image_header_t));
  1106. if (ntohl(hdr->ih_magic) != IH_MAGIC) {
  1107. puts (" Bad Magic Number\n");
  1108. return 1;
  1109. }
  1110. data = (ulong)&header;
  1111. len = sizeof(image_header_t);
  1112. checksum = ntohl(hdr->ih_hcrc);
  1113. hdr->ih_hcrc = 0;
  1114. if (crc32 (0, (uchar *)data, len) != checksum) {
  1115. puts (" Bad Header Checksum\n");
  1116. return 1;
  1117. }
  1118. /* for multi-file images we need the data part, too */
  1119. print_image_hdr ((image_header_t *)addr);
  1120. data = addr + sizeof(image_header_t);
  1121. len = ntohl(hdr->ih_size);
  1122. puts (" Verifying Checksum ... ");
  1123. if (crc32 (0, (uchar *)data, len) != ntohl(hdr->ih_dcrc)) {
  1124. puts (" Bad Data CRC\n");
  1125. return 1;
  1126. }
  1127. puts ("OK\n");
  1128. return 0;
  1129. }
  1130. U_BOOT_CMD(
  1131. iminfo, CFG_MAXARGS, 1, do_iminfo,
  1132. "iminfo - print header information for application image\n",
  1133. "addr [addr ...]\n"
  1134. " - print header information for application image starting at\n"
  1135. " address 'addr' in memory; this includes verification of the\n"
  1136. " image contents (magic number, header and payload checksums)\n"
  1137. );
  1138. #endif /* CFG_CMD_IMI */
  1139. #if (CONFIG_COMMANDS & CFG_CMD_IMLS)
  1140. /*-----------------------------------------------------------------------
  1141. * List all images found in flash.
  1142. */
  1143. int do_imls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  1144. {
  1145. flash_info_t *info;
  1146. int i, j;
  1147. image_header_t *hdr;
  1148. ulong data, len, checksum;
  1149. for (i=0, info=&flash_info[0]; i<CFG_MAX_FLASH_BANKS; ++i, ++info) {
  1150. if (info->flash_id == FLASH_UNKNOWN)
  1151. goto next_bank;
  1152. for (j=0; j<info->sector_count; ++j) {
  1153. if (!(hdr=(image_header_t *)info->start[j]) ||
  1154. (ntohl(hdr->ih_magic) != IH_MAGIC))
  1155. goto next_sector;
  1156. /* Copy header so we can blank CRC field for re-calculation */
  1157. memmove (&header, (char *)hdr, sizeof(image_header_t));
  1158. checksum = ntohl(header.ih_hcrc);
  1159. header.ih_hcrc = 0;
  1160. if (crc32 (0, (uchar *)&header, sizeof(image_header_t))
  1161. != checksum)
  1162. goto next_sector;
  1163. printf ("Image at %08lX:\n", (ulong)hdr);
  1164. print_image_hdr( hdr );
  1165. data = (ulong)hdr + sizeof(image_header_t);
  1166. len = ntohl(hdr->ih_size);
  1167. puts (" Verifying Checksum ... ");
  1168. if (crc32 (0, (uchar *)data, len) != ntohl(hdr->ih_dcrc)) {
  1169. puts (" Bad Data CRC\n");
  1170. }
  1171. puts ("OK\n");
  1172. next_sector: ;
  1173. }
  1174. next_bank: ;
  1175. }
  1176. return (0);
  1177. }
  1178. U_BOOT_CMD(
  1179. imls, 1, 1, do_imls,
  1180. "imls - list all images found in flash\n",
  1181. "\n"
  1182. " - Prints information about all images found at sector\n"
  1183. " boundaries in flash.\n"
  1184. );
  1185. #endif /* CFG_CMD_IMLS */
  1186. void
  1187. print_image_hdr (image_header_t *hdr)
  1188. {
  1189. #if (CONFIG_COMMANDS & CFG_CMD_DATE) || defined(CONFIG_TIMESTAMP)
  1190. time_t timestamp = (time_t)ntohl(hdr->ih_time);
  1191. struct rtc_time tm;
  1192. #endif
  1193. printf (" Image Name: %.*s\n", IH_NMLEN, hdr->ih_name);
  1194. #if (CONFIG_COMMANDS & CFG_CMD_DATE) || defined(CONFIG_TIMESTAMP)
  1195. to_tm (timestamp, &tm);
  1196. printf (" Created: %4d-%02d-%02d %2d:%02d:%02d UTC\n",
  1197. tm.tm_year, tm.tm_mon, tm.tm_mday,
  1198. tm.tm_hour, tm.tm_min, tm.tm_sec);
  1199. #endif /* CFG_CMD_DATE, CONFIG_TIMESTAMP */
  1200. puts (" Image Type: "); print_type(hdr);
  1201. printf ("\n Data Size: %d Bytes = ", ntohl(hdr->ih_size));
  1202. print_size (ntohl(hdr->ih_size), "\n");
  1203. printf (" Load Address: %08x\n"
  1204. " Entry Point: %08x\n",
  1205. ntohl(hdr->ih_load), ntohl(hdr->ih_ep));
  1206. if (hdr->ih_type == IH_TYPE_MULTI) {
  1207. int i;
  1208. ulong len;
  1209. ulong *len_ptr = (ulong *)((ulong)hdr + sizeof(image_header_t));
  1210. puts (" Contents:\n");
  1211. for (i=0; (len = ntohl(*len_ptr)); ++i, ++len_ptr) {
  1212. printf (" Image %d: %8ld Bytes = ", i, len);
  1213. print_size (len, "\n");
  1214. }
  1215. }
  1216. }
  1217. static void
  1218. print_type (image_header_t *hdr)
  1219. {
  1220. char *os, *arch, *type, *comp;
  1221. switch (hdr->ih_os) {
  1222. case IH_OS_INVALID: os = "Invalid OS"; break;
  1223. case IH_OS_NETBSD: os = "NetBSD"; break;
  1224. case IH_OS_LINUX: os = "Linux"; break;
  1225. case IH_OS_VXWORKS: os = "VxWorks"; break;
  1226. case IH_OS_QNX: os = "QNX"; break;
  1227. case IH_OS_U_BOOT: os = "U-Boot"; break;
  1228. case IH_OS_RTEMS: os = "RTEMS"; break;
  1229. #ifdef CONFIG_ARTOS
  1230. case IH_OS_ARTOS: os = "ARTOS"; break;
  1231. #endif
  1232. #ifdef CONFIG_LYNXKDI
  1233. case IH_OS_LYNXOS: os = "LynxOS"; break;
  1234. #endif
  1235. default: os = "Unknown OS"; break;
  1236. }
  1237. switch (hdr->ih_arch) {
  1238. case IH_CPU_INVALID: arch = "Invalid CPU"; break;
  1239. case IH_CPU_ALPHA: arch = "Alpha"; break;
  1240. case IH_CPU_ARM: arch = "ARM"; break;
  1241. case IH_CPU_AVR32: arch = "AVR32"; break;
  1242. case IH_CPU_BLACKFIN: arch = "Blackfin"; break;
  1243. case IH_CPU_I386: arch = "Intel x86"; break;
  1244. case IH_CPU_IA64: arch = "IA64"; break;
  1245. case IH_CPU_M68K: arch = "M68K"; break;
  1246. case IH_CPU_MICROBLAZE: arch = "Microblaze"; break;
  1247. case IH_CPU_MIPS64: arch = "MIPS 64 Bit"; break;
  1248. case IH_CPU_MIPS: arch = "MIPS"; break;
  1249. case IH_CPU_NIOS2: arch = "Nios-II"; break;
  1250. case IH_CPU_NIOS: arch = "Nios"; break;
  1251. case IH_CPU_PPC: arch = "PowerPC"; break;
  1252. case IH_CPU_S390: arch = "IBM S390"; break;
  1253. case IH_CPU_SH: arch = "SuperH"; break;
  1254. case IH_CPU_SPARC64: arch = "SPARC 64 Bit"; break;
  1255. case IH_CPU_SPARC: arch = "SPARC"; break;
  1256. default: arch = "Unknown Architecture"; break;
  1257. }
  1258. switch (hdr->ih_type) {
  1259. case IH_TYPE_INVALID: type = "Invalid Image"; break;
  1260. case IH_TYPE_STANDALONE:type = "Standalone Program"; break;
  1261. case IH_TYPE_KERNEL: type = "Kernel Image"; break;
  1262. case IH_TYPE_RAMDISK: type = "RAMDisk Image"; break;
  1263. case IH_TYPE_MULTI: type = "Multi-File Image"; break;
  1264. case IH_TYPE_FIRMWARE: type = "Firmware"; break;
  1265. case IH_TYPE_SCRIPT: type = "Script"; break;
  1266. case IH_TYPE_FLATDT: type = "Flat Device Tree"; break;
  1267. default: type = "Unknown Image"; break;
  1268. }
  1269. switch (hdr->ih_comp) {
  1270. case IH_COMP_NONE: comp = "uncompressed"; break;
  1271. case IH_COMP_GZIP: comp = "gzip compressed"; break;
  1272. case IH_COMP_BZIP2: comp = "bzip2 compressed"; break;
  1273. default: comp = "unknown compression"; break;
  1274. }
  1275. printf ("%s %s %s (%s)", arch, os, type, comp);
  1276. }
  1277. #define ZALLOC_ALIGNMENT 16
  1278. static void *zalloc(void *x, unsigned items, unsigned size)
  1279. {
  1280. void *p;
  1281. size *= items;
  1282. size = (size + ZALLOC_ALIGNMENT - 1) & ~(ZALLOC_ALIGNMENT - 1);
  1283. p = malloc (size);
  1284. return (p);
  1285. }
  1286. static void zfree(void *x, void *addr, unsigned nb)
  1287. {
  1288. free (addr);
  1289. }
  1290. #define HEAD_CRC 2
  1291. #define EXTRA_FIELD 4
  1292. #define ORIG_NAME 8
  1293. #define COMMENT 0x10
  1294. #define RESERVED 0xe0
  1295. #define DEFLATED 8
  1296. int gunzip(void *dst, int dstlen, unsigned char *src, unsigned long *lenp)
  1297. {
  1298. z_stream s;
  1299. int r, i, flags;
  1300. /* skip header */
  1301. i = 10;
  1302. flags = src[3];
  1303. if (src[2] != DEFLATED || (flags & RESERVED) != 0) {
  1304. puts ("Error: Bad gzipped data\n");
  1305. return (-1);
  1306. }
  1307. if ((flags & EXTRA_FIELD) != 0)
  1308. i = 12 + src[10] + (src[11] << 8);
  1309. if ((flags & ORIG_NAME) != 0)
  1310. while (src[i++] != 0)
  1311. ;
  1312. if ((flags & COMMENT) != 0)
  1313. while (src[i++] != 0)
  1314. ;
  1315. if ((flags & HEAD_CRC) != 0)
  1316. i += 2;
  1317. if (i >= *lenp) {
  1318. puts ("Error: gunzip out of data in header\n");
  1319. return (-1);
  1320. }
  1321. s.zalloc = zalloc;
  1322. s.zfree = zfree;
  1323. #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
  1324. s.outcb = (cb_func)WATCHDOG_RESET;
  1325. #else
  1326. s.outcb = Z_NULL;
  1327. #endif /* CONFIG_HW_WATCHDOG */
  1328. r = inflateInit2(&s, -MAX_WBITS);
  1329. if (r != Z_OK) {
  1330. printf ("Error: inflateInit2() returned %d\n", r);
  1331. return (-1);
  1332. }
  1333. s.next_in = src + i;
  1334. s.avail_in = *lenp - i;
  1335. s.next_out = dst;
  1336. s.avail_out = dstlen;
  1337. r = inflate(&s, Z_FINISH);
  1338. if (r != Z_OK && r != Z_STREAM_END) {
  1339. printf ("Error: inflate() returned %d\n", r);
  1340. return (-1);
  1341. }
  1342. *lenp = s.next_out - (unsigned char *) dst;
  1343. inflateEnd(&s);
  1344. return (0);
  1345. }
  1346. #ifdef CONFIG_BZIP2
  1347. void bz_internal_error(int errcode)
  1348. {
  1349. printf ("BZIP2 internal error %d\n", errcode);
  1350. }
  1351. #endif /* CONFIG_BZIP2 */
  1352. static void
  1353. do_bootm_rtems (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
  1354. ulong addr, ulong *len_ptr, int verify)
  1355. {
  1356. image_header_t *hdr = &header;
  1357. void (*entry_point)(bd_t *);
  1358. entry_point = (void (*)(bd_t *)) ntohl(hdr->ih_ep);
  1359. printf ("## Transferring control to RTEMS (at address %08lx) ...\n",
  1360. (ulong)entry_point);
  1361. SHOW_BOOT_PROGRESS (15);
  1362. /*
  1363. * RTEMS Parameters:
  1364. * r3: ptr to board info data
  1365. */
  1366. (*entry_point ) ( gd->bd );
  1367. }
  1368. #if (CONFIG_COMMANDS & CFG_CMD_ELF)
  1369. static void
  1370. do_bootm_vxworks (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
  1371. ulong addr, ulong *len_ptr, int verify)
  1372. {
  1373. image_header_t *hdr = &header;
  1374. char str[80];
  1375. sprintf(str, "%x", ntohl(hdr->ih_ep)); /* write entry-point into string */
  1376. setenv("loadaddr", str);
  1377. do_bootvx(cmdtp, 0, 0, NULL);
  1378. }
  1379. static void
  1380. do_bootm_qnxelf (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
  1381. ulong addr, ulong *len_ptr, int verify)
  1382. {
  1383. image_header_t *hdr = &header;
  1384. char *local_args[2];
  1385. char str[16];
  1386. sprintf(str, "%x", ntohl(hdr->ih_ep)); /* write entry-point into string */
  1387. local_args[0] = argv[0];
  1388. local_args[1] = str; /* and provide it via the arguments */
  1389. do_bootelf(cmdtp, 0, 2, local_args);
  1390. }
  1391. #endif /* CFG_CMD_ELF */
  1392. #ifdef CONFIG_LYNXKDI
  1393. static void
  1394. do_bootm_lynxkdi (cmd_tbl_t *cmdtp, int flag,
  1395. int argc, char *argv[],
  1396. ulong addr,
  1397. ulong *len_ptr,
  1398. int verify)
  1399. {
  1400. lynxkdi_boot( &header );
  1401. }
  1402. #endif /* CONFIG_LYNXKDI */