cmd_bootm.c 42 KB

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