cmd_bootm.c 41 KB

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