cmd_bootm.c 39 KB

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