cmd_bootm.c 42 KB

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