cmd_bootm.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641
  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_BOARD_SETUP
  860. /* Call the board-specific fixup routine */
  861. ft_board_setup(of_flat_tree, gd->bd);
  862. #endif
  863. }
  864. #endif /* CONFIG_OF_LIBFDT */
  865. #if defined(CONFIG_OF_FLAT_TREE)
  866. #ifdef CFG_BOOTMAPSZ
  867. /*
  868. * The blob must be within CFG_BOOTMAPSZ,
  869. * so we flag it to be copied if it is not.
  870. */
  871. if (of_flat_tree >= (char *)CFG_BOOTMAPSZ)
  872. of_data = (ulong)of_flat_tree;
  873. #endif
  874. /* move of_flat_tree if needed */
  875. if (of_data) {
  876. ulong of_start, of_len;
  877. of_len = ((struct boot_param_header *)of_data)->totalsize;
  878. /* provide extra 8k pad */
  879. of_start = (ulong)kbd - of_len - 8192;
  880. of_start &= ~(4096 - 1); /* align on page */
  881. debug ("## device tree at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
  882. of_data, of_data + of_len - 1, of_len, of_len);
  883. of_flat_tree = (char *)of_start;
  884. printf (" Loading Device Tree to %08lx, end %08lx ... ",
  885. of_start, of_start + of_len - 1);
  886. memmove ((void *)of_start, (void *)of_data, of_len);
  887. puts ("OK\n");
  888. }
  889. /*
  890. * Create the /chosen node and modify the blob with board specific
  891. * values as needed.
  892. */
  893. ft_setup(of_flat_tree, kbd, initrd_start, initrd_end);
  894. /* ft_dump_blob(of_flat_tree); */
  895. #endif
  896. debug ("## Transferring control to Linux (at address %08lx) ...\n",
  897. (ulong)kernel);
  898. show_boot_progress (15);
  899. #if defined(CFG_INIT_RAM_LOCK) && !defined(CONFIG_E500)
  900. unlock_ram_in_cache();
  901. #endif
  902. #if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
  903. if (of_flat_tree) { /* device tree; boot new style */
  904. /*
  905. * Linux Kernel Parameters (passing device tree):
  906. * r3: pointer to the fdt, followed by the board info data
  907. * r4: physical pointer to the kernel itself
  908. * r5: NULL
  909. * r6: NULL
  910. * r7: NULL
  911. */
  912. (*kernel) ((bd_t *)of_flat_tree, (ulong)kernel, 0, 0, 0);
  913. /* does not return */
  914. }
  915. #endif
  916. /*
  917. * Linux Kernel Parameters (passing board info data):
  918. * r3: ptr to board info data
  919. * r4: initrd_start or 0 if no initrd
  920. * r5: initrd_end - unused if r4 is 0
  921. * r6: Start of command line string
  922. * r7: End of command line string
  923. */
  924. (*kernel) (kbd, initrd_start, initrd_end, cmd_start, cmd_end);
  925. /* does not return */
  926. }
  927. #endif /* CONFIG_PPC */
  928. static void
  929. do_bootm_netbsd (cmd_tbl_t *cmdtp, int flag,
  930. int argc, char *argv[],
  931. ulong addr,
  932. ulong *len_ptr,
  933. int verify)
  934. {
  935. image_header_t *hdr = &header;
  936. void (*loader)(bd_t *, image_header_t *, char *, char *);
  937. image_header_t *img_addr;
  938. char *consdev;
  939. char *cmdline;
  940. /*
  941. * Booting a (NetBSD) kernel image
  942. *
  943. * This process is pretty similar to a standalone application:
  944. * The (first part of an multi-) image must be a stage-2 loader,
  945. * which in turn is responsible for loading & invoking the actual
  946. * kernel. The only differences are the parameters being passed:
  947. * besides the board info strucure, the loader expects a command
  948. * line, the name of the console device, and (optionally) the
  949. * address of the original image header.
  950. */
  951. img_addr = 0;
  952. if ((hdr->ih_type==IH_TYPE_MULTI) && (len_ptr[1]))
  953. img_addr = (image_header_t *) addr;
  954. consdev = "";
  955. #if defined (CONFIG_8xx_CONS_SMC1)
  956. consdev = "smc1";
  957. #elif defined (CONFIG_8xx_CONS_SMC2)
  958. consdev = "smc2";
  959. #elif defined (CONFIG_8xx_CONS_SCC2)
  960. consdev = "scc2";
  961. #elif defined (CONFIG_8xx_CONS_SCC3)
  962. consdev = "scc3";
  963. #endif
  964. if (argc > 2) {
  965. ulong len;
  966. int i;
  967. for (i=2, len=0 ; i<argc ; i+=1)
  968. len += strlen (argv[i]) + 1;
  969. cmdline = malloc (len);
  970. for (i=2, len=0 ; i<argc ; i+=1) {
  971. if (i > 2)
  972. cmdline[len++] = ' ';
  973. strcpy (&cmdline[len], argv[i]);
  974. len += strlen (argv[i]);
  975. }
  976. } else if ((cmdline = getenv("bootargs")) == NULL) {
  977. cmdline = "";
  978. }
  979. loader = (void (*)(bd_t *, image_header_t *, char *, char *)) ntohl(hdr->ih_ep);
  980. printf ("## Transferring control to NetBSD stage-2 loader (at address %08lx) ...\n",
  981. (ulong)loader);
  982. show_boot_progress (15);
  983. /*
  984. * NetBSD Stage-2 Loader Parameters:
  985. * r3: ptr to board info data
  986. * r4: image address
  987. * r5: console device
  988. * r6: boot args string
  989. */
  990. (*loader) (gd->bd, img_addr, consdev, cmdline);
  991. }
  992. #if defined(CONFIG_ARTOS) && defined(CONFIG_PPC)
  993. /* Function that returns a character from the environment */
  994. extern uchar (*env_get_char)(int);
  995. static void
  996. do_bootm_artos (cmd_tbl_t *cmdtp, int flag,
  997. int argc, char *argv[],
  998. ulong addr,
  999. ulong *len_ptr,
  1000. int verify)
  1001. {
  1002. ulong top;
  1003. char *s, *cmdline;
  1004. char **fwenv, **ss;
  1005. int i, j, nxt, len, envno, envsz;
  1006. bd_t *kbd;
  1007. void (*entry)(bd_t *bd, char *cmdline, char **fwenv, ulong top);
  1008. image_header_t *hdr = &header;
  1009. /*
  1010. * Booting an ARTOS kernel image + application
  1011. */
  1012. /* this used to be the top of memory, but was wrong... */
  1013. #ifdef CONFIG_PPC
  1014. /* get stack pointer */
  1015. asm volatile ("mr %0,1" : "=r"(top) );
  1016. #endif
  1017. debug ("## Current stack ends at 0x%08lX ", top);
  1018. top -= 2048; /* just to be sure */
  1019. if (top > CFG_BOOTMAPSZ)
  1020. top = CFG_BOOTMAPSZ;
  1021. top &= ~0xF;
  1022. debug ("=> set upper limit to 0x%08lX\n", top);
  1023. /* first check the artos specific boot args, then the linux args*/
  1024. if ((s = getenv("abootargs")) == NULL && (s = getenv("bootargs")) == NULL)
  1025. s = "";
  1026. /* get length of cmdline, and place it */
  1027. len = strlen(s);
  1028. top = (top - (len + 1)) & ~0xF;
  1029. cmdline = (char *)top;
  1030. debug ("## cmdline at 0x%08lX ", top);
  1031. strcpy(cmdline, s);
  1032. /* copy bdinfo */
  1033. top = (top - sizeof(bd_t)) & ~0xF;
  1034. debug ("## bd at 0x%08lX ", top);
  1035. kbd = (bd_t *)top;
  1036. memcpy(kbd, gd->bd, sizeof(bd_t));
  1037. /* first find number of env entries, and their size */
  1038. envno = 0;
  1039. envsz = 0;
  1040. for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
  1041. for (nxt = i; env_get_char(nxt) != '\0'; ++nxt)
  1042. ;
  1043. envno++;
  1044. envsz += (nxt - i) + 1; /* plus trailing zero */
  1045. }
  1046. envno++; /* plus the terminating zero */
  1047. debug ("## %u envvars total size %u ", envno, envsz);
  1048. top = (top - sizeof(char **)*envno) & ~0xF;
  1049. fwenv = (char **)top;
  1050. debug ("## fwenv at 0x%08lX ", top);
  1051. top = (top - envsz) & ~0xF;
  1052. s = (char *)top;
  1053. ss = fwenv;
  1054. /* now copy them */
  1055. for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
  1056. for (nxt = i; env_get_char(nxt) != '\0'; ++nxt)
  1057. ;
  1058. *ss++ = s;
  1059. for (j = i; j < nxt; ++j)
  1060. *s++ = env_get_char(j);
  1061. *s++ = '\0';
  1062. }
  1063. *ss++ = NULL; /* terminate */
  1064. entry = (void (*)(bd_t *, char *, char **, ulong))ntohl(hdr->ih_ep);
  1065. (*entry)(kbd, cmdline, fwenv, top);
  1066. }
  1067. #endif
  1068. #if defined(CONFIG_CMD_BOOTD)
  1069. int do_bootd (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  1070. {
  1071. int rcode = 0;
  1072. #ifndef CFG_HUSH_PARSER
  1073. if (run_command (getenv ("bootcmd"), flag) < 0) rcode = 1;
  1074. #else
  1075. if (parse_string_outer(getenv("bootcmd"),
  1076. FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0 ) rcode = 1;
  1077. #endif
  1078. return rcode;
  1079. }
  1080. U_BOOT_CMD(
  1081. boot, 1, 1, do_bootd,
  1082. "boot - boot default, i.e., run 'bootcmd'\n",
  1083. NULL
  1084. );
  1085. /* keep old command name "bootd" for backward compatibility */
  1086. U_BOOT_CMD(
  1087. bootd, 1, 1, do_bootd,
  1088. "bootd - boot default, i.e., run 'bootcmd'\n",
  1089. NULL
  1090. );
  1091. #endif
  1092. #if defined(CONFIG_CMD_IMI)
  1093. int do_iminfo ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  1094. {
  1095. int arg;
  1096. ulong addr;
  1097. int rcode=0;
  1098. if (argc < 2) {
  1099. return image_info (load_addr);
  1100. }
  1101. for (arg=1; arg <argc; ++arg) {
  1102. addr = simple_strtoul(argv[arg], NULL, 16);
  1103. if (image_info (addr) != 0) rcode = 1;
  1104. }
  1105. return rcode;
  1106. }
  1107. static int image_info (ulong addr)
  1108. {
  1109. ulong data, len, checksum;
  1110. image_header_t *hdr = &header;
  1111. printf ("\n## Checking Image at %08lx ...\n", addr);
  1112. /* Copy header so we can blank CRC field for re-calculation */
  1113. memmove (&header, (char *)addr, sizeof(image_header_t));
  1114. if (ntohl(hdr->ih_magic) != IH_MAGIC) {
  1115. puts (" Bad Magic Number\n");
  1116. return 1;
  1117. }
  1118. data = (ulong)&header;
  1119. len = sizeof(image_header_t);
  1120. checksum = ntohl(hdr->ih_hcrc);
  1121. hdr->ih_hcrc = 0;
  1122. if (crc32 (0, (uchar *)data, len) != checksum) {
  1123. puts (" Bad Header Checksum\n");
  1124. return 1;
  1125. }
  1126. /* for multi-file images we need the data part, too */
  1127. print_image_hdr ((image_header_t *)addr);
  1128. data = addr + sizeof(image_header_t);
  1129. len = ntohl(hdr->ih_size);
  1130. puts (" Verifying Checksum ... ");
  1131. if (crc32 (0, (uchar *)data, len) != ntohl(hdr->ih_dcrc)) {
  1132. puts (" Bad Data CRC\n");
  1133. return 1;
  1134. }
  1135. puts ("OK\n");
  1136. return 0;
  1137. }
  1138. U_BOOT_CMD(
  1139. iminfo, CFG_MAXARGS, 1, do_iminfo,
  1140. "iminfo - print header information for application image\n",
  1141. "addr [addr ...]\n"
  1142. " - print header information for application image starting at\n"
  1143. " address 'addr' in memory; this includes verification of the\n"
  1144. " image contents (magic number, header and payload checksums)\n"
  1145. );
  1146. #endif
  1147. #if defined(CONFIG_CMD_IMLS)
  1148. /*-----------------------------------------------------------------------
  1149. * List all images found in flash.
  1150. */
  1151. int do_imls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  1152. {
  1153. flash_info_t *info;
  1154. int i, j;
  1155. image_header_t *hdr;
  1156. ulong data, len, checksum;
  1157. for (i=0, info=&flash_info[0]; i<CFG_MAX_FLASH_BANKS; ++i, ++info) {
  1158. if (info->flash_id == FLASH_UNKNOWN)
  1159. goto next_bank;
  1160. for (j=0; j<info->sector_count; ++j) {
  1161. if (!(hdr=(image_header_t *)info->start[j]) ||
  1162. (ntohl(hdr->ih_magic) != IH_MAGIC))
  1163. goto next_sector;
  1164. /* Copy header so we can blank CRC field for re-calculation */
  1165. memmove (&header, (char *)hdr, sizeof(image_header_t));
  1166. checksum = ntohl(header.ih_hcrc);
  1167. header.ih_hcrc = 0;
  1168. if (crc32 (0, (uchar *)&header, sizeof(image_header_t))
  1169. != checksum)
  1170. goto next_sector;
  1171. printf ("Image at %08lX:\n", (ulong)hdr);
  1172. print_image_hdr( hdr );
  1173. data = (ulong)hdr + sizeof(image_header_t);
  1174. len = ntohl(hdr->ih_size);
  1175. puts (" Verifying Checksum ... ");
  1176. if (crc32 (0, (uchar *)data, len) != ntohl(hdr->ih_dcrc)) {
  1177. puts (" Bad Data CRC\n");
  1178. }
  1179. puts ("OK\n");
  1180. next_sector: ;
  1181. }
  1182. next_bank: ;
  1183. }
  1184. return (0);
  1185. }
  1186. U_BOOT_CMD(
  1187. imls, 1, 1, do_imls,
  1188. "imls - list all images found in flash\n",
  1189. "\n"
  1190. " - Prints information about all images found at sector\n"
  1191. " boundaries in flash.\n"
  1192. );
  1193. #endif
  1194. void
  1195. print_image_hdr (image_header_t *hdr)
  1196. {
  1197. #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE)
  1198. time_t timestamp = (time_t)ntohl(hdr->ih_time);
  1199. struct rtc_time tm;
  1200. #endif
  1201. printf (" Image Name: %.*s\n", IH_NMLEN, hdr->ih_name);
  1202. #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE)
  1203. to_tm (timestamp, &tm);
  1204. printf (" Created: %4d-%02d-%02d %2d:%02d:%02d UTC\n",
  1205. tm.tm_year, tm.tm_mon, tm.tm_mday,
  1206. tm.tm_hour, tm.tm_min, tm.tm_sec);
  1207. #endif
  1208. puts (" Image Type: "); print_type(hdr);
  1209. printf ("\n Data Size: %d Bytes = ", ntohl(hdr->ih_size));
  1210. print_size (ntohl(hdr->ih_size), "\n");
  1211. printf (" Load Address: %08x\n"
  1212. " Entry Point: %08x\n",
  1213. ntohl(hdr->ih_load), ntohl(hdr->ih_ep));
  1214. if (hdr->ih_type == IH_TYPE_MULTI) {
  1215. int i;
  1216. ulong len;
  1217. ulong *len_ptr = (ulong *)((ulong)hdr + sizeof(image_header_t));
  1218. puts (" Contents:\n");
  1219. for (i=0; (len = ntohl(*len_ptr)); ++i, ++len_ptr) {
  1220. printf (" Image %d: %8ld Bytes = ", i, len);
  1221. print_size (len, "\n");
  1222. }
  1223. }
  1224. }
  1225. static void
  1226. print_type (image_header_t *hdr)
  1227. {
  1228. char *os, *arch, *type, *comp;
  1229. switch (hdr->ih_os) {
  1230. case IH_OS_INVALID: os = "Invalid OS"; break;
  1231. case IH_OS_NETBSD: os = "NetBSD"; break;
  1232. case IH_OS_LINUX: os = "Linux"; break;
  1233. case IH_OS_VXWORKS: os = "VxWorks"; break;
  1234. case IH_OS_QNX: os = "QNX"; break;
  1235. case IH_OS_U_BOOT: os = "U-Boot"; break;
  1236. case IH_OS_RTEMS: os = "RTEMS"; break;
  1237. #ifdef CONFIG_ARTOS
  1238. case IH_OS_ARTOS: os = "ARTOS"; break;
  1239. #endif
  1240. #ifdef CONFIG_LYNXKDI
  1241. case IH_OS_LYNXOS: os = "LynxOS"; break;
  1242. #endif
  1243. default: os = "Unknown OS"; break;
  1244. }
  1245. switch (hdr->ih_arch) {
  1246. case IH_CPU_INVALID: arch = "Invalid CPU"; break;
  1247. case IH_CPU_ALPHA: arch = "Alpha"; break;
  1248. case IH_CPU_ARM: arch = "ARM"; break;
  1249. case IH_CPU_AVR32: arch = "AVR32"; break;
  1250. case IH_CPU_BLACKFIN: arch = "Blackfin"; break;
  1251. case IH_CPU_I386: arch = "Intel x86"; break;
  1252. case IH_CPU_IA64: arch = "IA64"; break;
  1253. case IH_CPU_M68K: arch = "M68K"; break;
  1254. case IH_CPU_MICROBLAZE: arch = "Microblaze"; break;
  1255. case IH_CPU_MIPS64: arch = "MIPS 64 Bit"; break;
  1256. case IH_CPU_MIPS: arch = "MIPS"; break;
  1257. case IH_CPU_NIOS2: arch = "Nios-II"; break;
  1258. case IH_CPU_NIOS: arch = "Nios"; break;
  1259. case IH_CPU_PPC: arch = "PowerPC"; break;
  1260. case IH_CPU_S390: arch = "IBM S390"; break;
  1261. case IH_CPU_SH: arch = "SuperH"; break;
  1262. case IH_CPU_SPARC64: arch = "SPARC 64 Bit"; break;
  1263. case IH_CPU_SPARC: arch = "SPARC"; break;
  1264. default: arch = "Unknown Architecture"; break;
  1265. }
  1266. switch (hdr->ih_type) {
  1267. case IH_TYPE_INVALID: type = "Invalid Image"; break;
  1268. case IH_TYPE_STANDALONE:type = "Standalone Program"; break;
  1269. case IH_TYPE_KERNEL: type = "Kernel Image"; break;
  1270. case IH_TYPE_RAMDISK: type = "RAMDisk Image"; break;
  1271. case IH_TYPE_MULTI: type = "Multi-File Image"; break;
  1272. case IH_TYPE_FIRMWARE: type = "Firmware"; break;
  1273. case IH_TYPE_SCRIPT: type = "Script"; break;
  1274. case IH_TYPE_FLATDT: type = "Flat Device Tree"; break;
  1275. default: type = "Unknown Image"; break;
  1276. }
  1277. switch (hdr->ih_comp) {
  1278. case IH_COMP_NONE: comp = "uncompressed"; break;
  1279. case IH_COMP_GZIP: comp = "gzip compressed"; break;
  1280. case IH_COMP_BZIP2: comp = "bzip2 compressed"; break;
  1281. default: comp = "unknown compression"; break;
  1282. }
  1283. printf ("%s %s %s (%s)", arch, os, type, comp);
  1284. }
  1285. #define ZALLOC_ALIGNMENT 16
  1286. static void *zalloc(void *x, unsigned items, unsigned size)
  1287. {
  1288. void *p;
  1289. size *= items;
  1290. size = (size + ZALLOC_ALIGNMENT - 1) & ~(ZALLOC_ALIGNMENT - 1);
  1291. p = malloc (size);
  1292. return (p);
  1293. }
  1294. static void zfree(void *x, void *addr, unsigned nb)
  1295. {
  1296. free (addr);
  1297. }
  1298. #define HEAD_CRC 2
  1299. #define EXTRA_FIELD 4
  1300. #define ORIG_NAME 8
  1301. #define COMMENT 0x10
  1302. #define RESERVED 0xe0
  1303. #define DEFLATED 8
  1304. int gunzip(void *dst, int dstlen, unsigned char *src, unsigned long *lenp)
  1305. {
  1306. z_stream s;
  1307. int r, i, flags;
  1308. /* skip header */
  1309. i = 10;
  1310. flags = src[3];
  1311. if (src[2] != DEFLATED || (flags & RESERVED) != 0) {
  1312. puts ("Error: Bad gzipped data\n");
  1313. return (-1);
  1314. }
  1315. if ((flags & EXTRA_FIELD) != 0)
  1316. i = 12 + src[10] + (src[11] << 8);
  1317. if ((flags & ORIG_NAME) != 0)
  1318. while (src[i++] != 0)
  1319. ;
  1320. if ((flags & COMMENT) != 0)
  1321. while (src[i++] != 0)
  1322. ;
  1323. if ((flags & HEAD_CRC) != 0)
  1324. i += 2;
  1325. if (i >= *lenp) {
  1326. puts ("Error: gunzip out of data in header\n");
  1327. return (-1);
  1328. }
  1329. s.zalloc = zalloc;
  1330. s.zfree = zfree;
  1331. #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
  1332. s.outcb = (cb_func)WATCHDOG_RESET;
  1333. #else
  1334. s.outcb = Z_NULL;
  1335. #endif /* CONFIG_HW_WATCHDOG */
  1336. r = inflateInit2(&s, -MAX_WBITS);
  1337. if (r != Z_OK) {
  1338. printf ("Error: inflateInit2() returned %d\n", r);
  1339. return (-1);
  1340. }
  1341. s.next_in = src + i;
  1342. s.avail_in = *lenp - i;
  1343. s.next_out = dst;
  1344. s.avail_out = dstlen;
  1345. r = inflate(&s, Z_FINISH);
  1346. if (r != Z_OK && r != Z_STREAM_END) {
  1347. printf ("Error: inflate() returned %d\n", r);
  1348. return (-1);
  1349. }
  1350. *lenp = s.next_out - (unsigned char *) dst;
  1351. inflateEnd(&s);
  1352. return (0);
  1353. }
  1354. #ifdef CONFIG_BZIP2
  1355. void bz_internal_error(int errcode)
  1356. {
  1357. printf ("BZIP2 internal error %d\n", errcode);
  1358. }
  1359. #endif /* CONFIG_BZIP2 */
  1360. static void
  1361. do_bootm_rtems (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
  1362. ulong addr, ulong *len_ptr, int verify)
  1363. {
  1364. image_header_t *hdr = &header;
  1365. void (*entry_point)(bd_t *);
  1366. entry_point = (void (*)(bd_t *)) ntohl(hdr->ih_ep);
  1367. printf ("## Transferring control to RTEMS (at address %08lx) ...\n",
  1368. (ulong)entry_point);
  1369. show_boot_progress (15);
  1370. /*
  1371. * RTEMS Parameters:
  1372. * r3: ptr to board info data
  1373. */
  1374. (*entry_point ) ( gd->bd );
  1375. }
  1376. #if defined(CONFIG_CMD_ELF)
  1377. static void
  1378. do_bootm_vxworks (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
  1379. ulong addr, ulong *len_ptr, int verify)
  1380. {
  1381. image_header_t *hdr = &header;
  1382. char str[80];
  1383. sprintf(str, "%x", ntohl(hdr->ih_ep)); /* write entry-point into string */
  1384. setenv("loadaddr", str);
  1385. do_bootvx(cmdtp, 0, 0, NULL);
  1386. }
  1387. static void
  1388. do_bootm_qnxelf (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
  1389. ulong addr, ulong *len_ptr, int verify)
  1390. {
  1391. image_header_t *hdr = &header;
  1392. char *local_args[2];
  1393. char str[16];
  1394. sprintf(str, "%x", ntohl(hdr->ih_ep)); /* write entry-point into string */
  1395. local_args[0] = argv[0];
  1396. local_args[1] = str; /* and provide it via the arguments */
  1397. do_bootelf(cmdtp, 0, 2, local_args);
  1398. }
  1399. #endif
  1400. #ifdef CONFIG_LYNXKDI
  1401. static void
  1402. do_bootm_lynxkdi (cmd_tbl_t *cmdtp, int flag,
  1403. int argc, char *argv[],
  1404. ulong addr,
  1405. ulong *len_ptr,
  1406. int verify)
  1407. {
  1408. lynxkdi_boot( &header );
  1409. }
  1410. #endif /* CONFIG_LYNXKDI */