cmd_bootm.c 42 KB

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