cmd_bootm.c 35 KB

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