cmd_bootm.c 41 KB

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