main.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588
  1. /*
  2. * (C) Copyright 2000
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * Add to readline cmdline-editing by
  6. * (C) Copyright 2005
  7. * JinHua Luo, GuangDong Linux Center, <luo.jinhua@gd-linux.com>
  8. *
  9. * See file CREDITS for list of people who contributed to this
  10. * project.
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License as
  14. * published by the Free Software Foundation; either version 2 of
  15. * the License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  25. * MA 02111-1307 USA
  26. */
  27. /* #define DEBUG */
  28. #include <common.h>
  29. #include <watchdog.h>
  30. #include <command.h>
  31. #include <fdtdec.h>
  32. #include <malloc.h>
  33. #include <version.h>
  34. #ifdef CONFIG_MODEM_SUPPORT
  35. #include <malloc.h> /* for free() prototype */
  36. #endif
  37. #ifdef CONFIG_SYS_HUSH_PARSER
  38. #include <hush.h>
  39. #endif
  40. #ifdef CONFIG_OF_CONTROL
  41. #include <fdtdec.h>
  42. #endif
  43. #ifdef CONFIG_OF_LIBFDT
  44. #include <fdt_support.h>
  45. #endif /* CONFIG_OF_LIBFDT */
  46. #include <post.h>
  47. #include <linux/ctype.h>
  48. #include <menu.h>
  49. DECLARE_GLOBAL_DATA_PTR;
  50. /*
  51. * Board-specific Platform code can reimplement show_boot_progress () if needed
  52. */
  53. void inline __show_boot_progress (int val) {}
  54. void show_boot_progress (int val) __attribute__((weak, alias("__show_boot_progress")));
  55. #if defined(CONFIG_UPDATE_TFTP)
  56. int update_tftp (ulong addr);
  57. #endif /* CONFIG_UPDATE_TFTP */
  58. #define MAX_DELAY_STOP_STR 32
  59. #undef DEBUG_PARSER
  60. char console_buffer[CONFIG_SYS_CBSIZE + 1]; /* console I/O buffer */
  61. static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen);
  62. static const char erase_seq[] = "\b \b"; /* erase sequence */
  63. static const char tab_seq[] = " "; /* used to expand TABs */
  64. #ifdef CONFIG_BOOT_RETRY_TIME
  65. static uint64_t endtime = 0; /* must be set, default is instant timeout */
  66. static int retry_time = -1; /* -1 so can call readline before main_loop */
  67. #endif
  68. #define endtick(seconds) (get_ticks() + (uint64_t)(seconds) * get_tbclk())
  69. #ifndef CONFIG_BOOT_RETRY_MIN
  70. #define CONFIG_BOOT_RETRY_MIN CONFIG_BOOT_RETRY_TIME
  71. #endif
  72. #ifdef CONFIG_MODEM_SUPPORT
  73. int do_mdm_init = 0;
  74. extern void mdm_init(void); /* defined in board.c */
  75. #endif
  76. /***************************************************************************
  77. * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
  78. * returns: 0 - no key string, allow autoboot 1 - got key string, abort
  79. */
  80. #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
  81. # if defined(CONFIG_AUTOBOOT_KEYED)
  82. #ifndef CONFIG_MENU
  83. static inline
  84. #endif
  85. int abortboot(int bootdelay)
  86. {
  87. int abort = 0;
  88. uint64_t etime = endtick(bootdelay);
  89. struct {
  90. char* str;
  91. u_int len;
  92. int retry;
  93. }
  94. delaykey [] = {
  95. { str: getenv ("bootdelaykey"), retry: 1 },
  96. { str: getenv ("bootdelaykey2"), retry: 1 },
  97. { str: getenv ("bootstopkey"), retry: 0 },
  98. { str: getenv ("bootstopkey2"), retry: 0 },
  99. };
  100. char presskey [MAX_DELAY_STOP_STR];
  101. u_int presskey_len = 0;
  102. u_int presskey_max = 0;
  103. u_int i;
  104. #ifndef CONFIG_ZERO_BOOTDELAY_CHECK
  105. if (bootdelay == 0)
  106. return 0;
  107. #endif
  108. # ifdef CONFIG_AUTOBOOT_PROMPT
  109. printf(CONFIG_AUTOBOOT_PROMPT);
  110. # endif
  111. # ifdef CONFIG_AUTOBOOT_DELAY_STR
  112. if (delaykey[0].str == NULL)
  113. delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
  114. # endif
  115. # ifdef CONFIG_AUTOBOOT_DELAY_STR2
  116. if (delaykey[1].str == NULL)
  117. delaykey[1].str = CONFIG_AUTOBOOT_DELAY_STR2;
  118. # endif
  119. # ifdef CONFIG_AUTOBOOT_STOP_STR
  120. if (delaykey[2].str == NULL)
  121. delaykey[2].str = CONFIG_AUTOBOOT_STOP_STR;
  122. # endif
  123. # ifdef CONFIG_AUTOBOOT_STOP_STR2
  124. if (delaykey[3].str == NULL)
  125. delaykey[3].str = CONFIG_AUTOBOOT_STOP_STR2;
  126. # endif
  127. for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
  128. delaykey[i].len = delaykey[i].str == NULL ?
  129. 0 : strlen (delaykey[i].str);
  130. delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
  131. MAX_DELAY_STOP_STR : delaykey[i].len;
  132. presskey_max = presskey_max > delaykey[i].len ?
  133. presskey_max : delaykey[i].len;
  134. # if DEBUG_BOOTKEYS
  135. printf("%s key:<%s>\n",
  136. delaykey[i].retry ? "delay" : "stop",
  137. delaykey[i].str ? delaykey[i].str : "NULL");
  138. # endif
  139. }
  140. /* In order to keep up with incoming data, check timeout only
  141. * when catch up.
  142. */
  143. do {
  144. if (tstc()) {
  145. if (presskey_len < presskey_max) {
  146. presskey [presskey_len ++] = getc();
  147. }
  148. else {
  149. for (i = 0; i < presskey_max - 1; i ++)
  150. presskey [i] = presskey [i + 1];
  151. presskey [i] = getc();
  152. }
  153. }
  154. for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
  155. if (delaykey[i].len > 0 &&
  156. presskey_len >= delaykey[i].len &&
  157. memcmp (presskey + presskey_len - delaykey[i].len,
  158. delaykey[i].str,
  159. delaykey[i].len) == 0) {
  160. # if DEBUG_BOOTKEYS
  161. printf("got %skey\n",
  162. delaykey[i].retry ? "delay" : "stop");
  163. # endif
  164. # ifdef CONFIG_BOOT_RETRY_TIME
  165. /* don't retry auto boot */
  166. if (! delaykey[i].retry)
  167. retry_time = -1;
  168. # endif
  169. abort = 1;
  170. }
  171. }
  172. } while (!abort && get_ticks() <= etime);
  173. # if DEBUG_BOOTKEYS
  174. if (!abort)
  175. puts("key timeout\n");
  176. # endif
  177. #ifdef CONFIG_SILENT_CONSOLE
  178. if (abort)
  179. gd->flags &= ~GD_FLG_SILENT;
  180. #endif
  181. return abort;
  182. }
  183. # else /* !defined(CONFIG_AUTOBOOT_KEYED) */
  184. #ifdef CONFIG_MENUKEY
  185. static int menukey = 0;
  186. #endif
  187. #ifndef CONFIG_MENU
  188. static inline
  189. #endif
  190. int abortboot(int bootdelay)
  191. {
  192. int abort = 0;
  193. #ifdef CONFIG_MENUPROMPT
  194. printf(CONFIG_MENUPROMPT);
  195. #else
  196. if (bootdelay >= 0)
  197. printf("Hit any key to stop autoboot: %2d ", bootdelay);
  198. #endif
  199. #if defined CONFIG_ZERO_BOOTDELAY_CHECK
  200. /*
  201. * Check if key already pressed
  202. * Don't check if bootdelay < 0
  203. */
  204. if (bootdelay >= 0) {
  205. if (tstc()) { /* we got a key press */
  206. (void) getc(); /* consume input */
  207. puts ("\b\b\b 0");
  208. abort = 1; /* don't auto boot */
  209. }
  210. }
  211. #endif
  212. while ((bootdelay > 0) && (!abort)) {
  213. int i;
  214. --bootdelay;
  215. /* delay 100 * 10ms */
  216. for (i=0; !abort && i<100; ++i) {
  217. if (tstc()) { /* we got a key press */
  218. abort = 1; /* don't auto boot */
  219. bootdelay = 0; /* no more delay */
  220. # ifdef CONFIG_MENUKEY
  221. menukey = getc();
  222. # else
  223. (void) getc(); /* consume input */
  224. # endif
  225. break;
  226. }
  227. udelay(10000);
  228. }
  229. printf("\b\b\b%2d ", bootdelay);
  230. }
  231. putc('\n');
  232. #ifdef CONFIG_SILENT_CONSOLE
  233. if (abort)
  234. gd->flags &= ~GD_FLG_SILENT;
  235. #endif
  236. return abort;
  237. }
  238. # endif /* CONFIG_AUTOBOOT_KEYED */
  239. #endif /* CONFIG_BOOTDELAY >= 0 */
  240. /*
  241. * Runs the given boot command securely. Specifically:
  242. * - Doesn't run the command with the shell (run_command or parse_string_outer),
  243. * since that's a lot of code surface that an attacker might exploit.
  244. * Because of this, we don't do any argument parsing--the secure boot command
  245. * has to be a full-fledged u-boot command.
  246. * - Doesn't check for keypresses before booting, since that could be a
  247. * security hole; also disables Ctrl-C.
  248. * - Doesn't allow the command to return.
  249. *
  250. * Upon any failures, this function will drop into an infinite loop after
  251. * printing the error message to console.
  252. */
  253. #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0) && \
  254. defined(CONFIG_OF_CONTROL)
  255. static void secure_boot_cmd(char *cmd)
  256. {
  257. cmd_tbl_t *cmdtp;
  258. int rc;
  259. if (!cmd) {
  260. printf("## Error: Secure boot command not specified\n");
  261. goto err;
  262. }
  263. /* Disable Ctrl-C just in case some command is used that checks it. */
  264. disable_ctrlc(1);
  265. /* Find the command directly. */
  266. cmdtp = find_cmd(cmd);
  267. if (!cmdtp) {
  268. printf("## Error: \"%s\" not defined\n", cmd);
  269. goto err;
  270. }
  271. /* Run the command, forcing no flags and faking argc and argv. */
  272. rc = (cmdtp->cmd)(cmdtp, 0, 1, &cmd);
  273. /* Shouldn't ever return from boot command. */
  274. printf("## Error: \"%s\" returned (code %d)\n", cmd, rc);
  275. err:
  276. /*
  277. * Not a whole lot to do here. Rebooting won't help much, since we'll
  278. * just end up right back here. Just loop.
  279. */
  280. hang();
  281. }
  282. static void process_fdt_options(const void *blob)
  283. {
  284. ulong addr;
  285. /* Add an env variable to point to a kernel payload, if available */
  286. addr = fdtdec_get_config_int(gd->fdt_blob, "kernel-offset", 0);
  287. if (addr)
  288. setenv_addr("kernaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
  289. /* Add an env variable to point to a root disk, if available */
  290. addr = fdtdec_get_config_int(gd->fdt_blob, "rootdisk-offset", 0);
  291. if (addr)
  292. setenv_addr("rootaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
  293. }
  294. #endif /* CONFIG_OF_CONTROL */
  295. /****************************************************************************/
  296. void main_loop (void)
  297. {
  298. #ifndef CONFIG_SYS_HUSH_PARSER
  299. static char lastcommand[CONFIG_SYS_CBSIZE] = { 0, };
  300. int len;
  301. int rc = 1;
  302. int flag;
  303. #endif
  304. #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0) && \
  305. defined(CONFIG_OF_CONTROL)
  306. char *env;
  307. #endif
  308. #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
  309. char *s;
  310. int bootdelay;
  311. #endif
  312. #ifdef CONFIG_PREBOOT
  313. char *p;
  314. #endif
  315. #ifdef CONFIG_BOOTCOUNT_LIMIT
  316. unsigned long bootcount = 0;
  317. unsigned long bootlimit = 0;
  318. char *bcs;
  319. char bcs_set[16];
  320. #endif /* CONFIG_BOOTCOUNT_LIMIT */
  321. #ifdef CONFIG_BOOTCOUNT_LIMIT
  322. bootcount = bootcount_load();
  323. bootcount++;
  324. bootcount_store (bootcount);
  325. sprintf (bcs_set, "%lu", bootcount);
  326. setenv ("bootcount", bcs_set);
  327. bcs = getenv ("bootlimit");
  328. bootlimit = bcs ? simple_strtoul (bcs, NULL, 10) : 0;
  329. #endif /* CONFIG_BOOTCOUNT_LIMIT */
  330. #ifdef CONFIG_MODEM_SUPPORT
  331. debug ("DEBUG: main_loop: do_mdm_init=%d\n", do_mdm_init);
  332. if (do_mdm_init) {
  333. char *str = strdup(getenv("mdm_cmd"));
  334. setenv ("preboot", str); /* set or delete definition */
  335. if (str != NULL)
  336. free (str);
  337. mdm_init(); /* wait for modem connection */
  338. }
  339. #endif /* CONFIG_MODEM_SUPPORT */
  340. #ifdef CONFIG_VERSION_VARIABLE
  341. {
  342. setenv ("ver", version_string); /* set version variable */
  343. }
  344. #endif /* CONFIG_VERSION_VARIABLE */
  345. #ifdef CONFIG_SYS_HUSH_PARSER
  346. u_boot_hush_start ();
  347. #endif
  348. #if defined(CONFIG_HUSH_INIT_VAR)
  349. hush_init_var ();
  350. #endif
  351. #ifdef CONFIG_PREBOOT
  352. if ((p = getenv ("preboot")) != NULL) {
  353. # ifdef CONFIG_AUTOBOOT_KEYED
  354. int prev = disable_ctrlc(1); /* disable Control C checking */
  355. # endif
  356. run_command_list(p, -1, 0);
  357. # ifdef CONFIG_AUTOBOOT_KEYED
  358. disable_ctrlc(prev); /* restore Control C checking */
  359. # endif
  360. }
  361. #endif /* CONFIG_PREBOOT */
  362. #if defined(CONFIG_UPDATE_TFTP)
  363. update_tftp (0UL);
  364. #endif /* CONFIG_UPDATE_TFTP */
  365. #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
  366. s = getenv ("bootdelay");
  367. bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
  368. debug ("### main_loop entered: bootdelay=%d\n\n", bootdelay);
  369. #if defined(CONFIG_MENU_SHOW)
  370. bootdelay = menu_show(bootdelay);
  371. #endif
  372. # ifdef CONFIG_BOOT_RETRY_TIME
  373. init_cmd_timeout ();
  374. # endif /* CONFIG_BOOT_RETRY_TIME */
  375. #ifdef CONFIG_POST
  376. if (gd->flags & GD_FLG_POSTFAIL) {
  377. s = getenv("failbootcmd");
  378. }
  379. else
  380. #endif /* CONFIG_POST */
  381. #ifdef CONFIG_BOOTCOUNT_LIMIT
  382. if (bootlimit && (bootcount > bootlimit)) {
  383. printf ("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",
  384. (unsigned)bootlimit);
  385. s = getenv ("altbootcmd");
  386. }
  387. else
  388. #endif /* CONFIG_BOOTCOUNT_LIMIT */
  389. s = getenv ("bootcmd");
  390. #ifdef CONFIG_OF_CONTROL
  391. /* Allow the fdt to override the boot command */
  392. env = fdtdec_get_config_string(gd->fdt_blob, "bootcmd");
  393. if (env)
  394. s = env;
  395. process_fdt_options(gd->fdt_blob);
  396. /*
  397. * If the bootsecure option was chosen, use secure_boot_cmd().
  398. * Always use 'env' in this case, since bootsecure requres that the
  399. * bootcmd was specified in the FDT too.
  400. */
  401. if (fdtdec_get_config_int(gd->fdt_blob, "bootsecure", 0))
  402. secure_boot_cmd(env);
  403. #endif /* CONFIG_OF_CONTROL */
  404. debug ("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
  405. if (bootdelay != -1 && s && !abortboot(bootdelay)) {
  406. # ifdef CONFIG_AUTOBOOT_KEYED
  407. int prev = disable_ctrlc(1); /* disable Control C checking */
  408. # endif
  409. run_command_list(s, -1, 0);
  410. # ifdef CONFIG_AUTOBOOT_KEYED
  411. disable_ctrlc(prev); /* restore Control C checking */
  412. # endif
  413. }
  414. # ifdef CONFIG_MENUKEY
  415. if (menukey == CONFIG_MENUKEY) {
  416. s = getenv("menucmd");
  417. if (s)
  418. run_command_list(s, -1, 0);
  419. }
  420. #endif /* CONFIG_MENUKEY */
  421. #endif /* CONFIG_BOOTDELAY */
  422. #if defined CONFIG_OF_CONTROL
  423. set_working_fdt_addr((void *)gd->fdt_blob);
  424. #endif /* CONFIG_OF_CONTROL */
  425. /*
  426. * Main Loop for Monitor Command Processing
  427. */
  428. #ifdef CONFIG_SYS_HUSH_PARSER
  429. parse_file_outer();
  430. /* This point is never reached */
  431. for (;;);
  432. #else
  433. for (;;) {
  434. #ifdef CONFIG_BOOT_RETRY_TIME
  435. if (rc >= 0) {
  436. /* Saw enough of a valid command to
  437. * restart the timeout.
  438. */
  439. reset_cmd_timeout();
  440. }
  441. #endif
  442. len = readline (CONFIG_SYS_PROMPT);
  443. flag = 0; /* assume no special flags for now */
  444. if (len > 0)
  445. strcpy (lastcommand, console_buffer);
  446. else if (len == 0)
  447. flag |= CMD_FLAG_REPEAT;
  448. #ifdef CONFIG_BOOT_RETRY_TIME
  449. else if (len == -2) {
  450. /* -2 means timed out, retry autoboot
  451. */
  452. puts ("\nTimed out waiting for command\n");
  453. # ifdef CONFIG_RESET_TO_RETRY
  454. /* Reinit board to run initialization code again */
  455. do_reset (NULL, 0, 0, NULL);
  456. # else
  457. return; /* retry autoboot */
  458. # endif
  459. }
  460. #endif
  461. if (len == -1)
  462. puts ("<INTERRUPT>\n");
  463. else
  464. rc = run_command(lastcommand, flag);
  465. if (rc <= 0) {
  466. /* invalid command or not repeatable, forget it */
  467. lastcommand[0] = 0;
  468. }
  469. }
  470. #endif /*CONFIG_SYS_HUSH_PARSER*/
  471. }
  472. #ifdef CONFIG_BOOT_RETRY_TIME
  473. /***************************************************************************
  474. * initialize command line timeout
  475. */
  476. void init_cmd_timeout(void)
  477. {
  478. char *s = getenv ("bootretry");
  479. if (s != NULL)
  480. retry_time = (int)simple_strtol(s, NULL, 10);
  481. else
  482. retry_time = CONFIG_BOOT_RETRY_TIME;
  483. if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN)
  484. retry_time = CONFIG_BOOT_RETRY_MIN;
  485. }
  486. /***************************************************************************
  487. * reset command line timeout to retry_time seconds
  488. */
  489. void reset_cmd_timeout(void)
  490. {
  491. endtime = endtick(retry_time);
  492. }
  493. #endif
  494. #ifdef CONFIG_CMDLINE_EDITING
  495. /*
  496. * cmdline-editing related codes from vivi.
  497. * Author: Janghoon Lyu <nandy@mizi.com>
  498. */
  499. #define putnstr(str,n) do { \
  500. printf ("%.*s", (int)n, str); \
  501. } while (0)
  502. #define CTL_CH(c) ((c) - 'a' + 1)
  503. #define CTL_BACKSPACE ('\b')
  504. #define DEL ((char)255)
  505. #define DEL7 ((char)127)
  506. #define CREAD_HIST_CHAR ('!')
  507. #define getcmd_putch(ch) putc(ch)
  508. #define getcmd_getch() getc()
  509. #define getcmd_cbeep() getcmd_putch('\a')
  510. #define HIST_MAX 20
  511. #define HIST_SIZE CONFIG_SYS_CBSIZE
  512. static int hist_max;
  513. static int hist_add_idx;
  514. static int hist_cur = -1;
  515. static unsigned hist_num;
  516. static char *hist_list[HIST_MAX];
  517. static char hist_lines[HIST_MAX][HIST_SIZE + 1]; /* Save room for NULL */
  518. #define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
  519. static void hist_init(void)
  520. {
  521. int i;
  522. hist_max = 0;
  523. hist_add_idx = 0;
  524. hist_cur = -1;
  525. hist_num = 0;
  526. for (i = 0; i < HIST_MAX; i++) {
  527. hist_list[i] = hist_lines[i];
  528. hist_list[i][0] = '\0';
  529. }
  530. }
  531. static void cread_add_to_hist(char *line)
  532. {
  533. strcpy(hist_list[hist_add_idx], line);
  534. if (++hist_add_idx >= HIST_MAX)
  535. hist_add_idx = 0;
  536. if (hist_add_idx > hist_max)
  537. hist_max = hist_add_idx;
  538. hist_num++;
  539. }
  540. static char* hist_prev(void)
  541. {
  542. char *ret;
  543. int old_cur;
  544. if (hist_cur < 0)
  545. return NULL;
  546. old_cur = hist_cur;
  547. if (--hist_cur < 0)
  548. hist_cur = hist_max;
  549. if (hist_cur == hist_add_idx) {
  550. hist_cur = old_cur;
  551. ret = NULL;
  552. } else
  553. ret = hist_list[hist_cur];
  554. return (ret);
  555. }
  556. static char* hist_next(void)
  557. {
  558. char *ret;
  559. if (hist_cur < 0)
  560. return NULL;
  561. if (hist_cur == hist_add_idx)
  562. return NULL;
  563. if (++hist_cur > hist_max)
  564. hist_cur = 0;
  565. if (hist_cur == hist_add_idx) {
  566. ret = "";
  567. } else
  568. ret = hist_list[hist_cur];
  569. return (ret);
  570. }
  571. #ifndef CONFIG_CMDLINE_EDITING
  572. static void cread_print_hist_list(void)
  573. {
  574. int i;
  575. unsigned long n;
  576. n = hist_num - hist_max;
  577. i = hist_add_idx + 1;
  578. while (1) {
  579. if (i > hist_max)
  580. i = 0;
  581. if (i == hist_add_idx)
  582. break;
  583. printf("%s\n", hist_list[i]);
  584. n++;
  585. i++;
  586. }
  587. }
  588. #endif /* CONFIG_CMDLINE_EDITING */
  589. #define BEGINNING_OF_LINE() { \
  590. while (num) { \
  591. getcmd_putch(CTL_BACKSPACE); \
  592. num--; \
  593. } \
  594. }
  595. #define ERASE_TO_EOL() { \
  596. if (num < eol_num) { \
  597. printf("%*s", (int)(eol_num - num), ""); \
  598. do { \
  599. getcmd_putch(CTL_BACKSPACE); \
  600. } while (--eol_num > num); \
  601. } \
  602. }
  603. #define REFRESH_TO_EOL() { \
  604. if (num < eol_num) { \
  605. wlen = eol_num - num; \
  606. putnstr(buf + num, wlen); \
  607. num = eol_num; \
  608. } \
  609. }
  610. static void cread_add_char(char ichar, int insert, unsigned long *num,
  611. unsigned long *eol_num, char *buf, unsigned long len)
  612. {
  613. unsigned long wlen;
  614. /* room ??? */
  615. if (insert || *num == *eol_num) {
  616. if (*eol_num > len - 1) {
  617. getcmd_cbeep();
  618. return;
  619. }
  620. (*eol_num)++;
  621. }
  622. if (insert) {
  623. wlen = *eol_num - *num;
  624. if (wlen > 1) {
  625. memmove(&buf[*num+1], &buf[*num], wlen-1);
  626. }
  627. buf[*num] = ichar;
  628. putnstr(buf + *num, wlen);
  629. (*num)++;
  630. while (--wlen) {
  631. getcmd_putch(CTL_BACKSPACE);
  632. }
  633. } else {
  634. /* echo the character */
  635. wlen = 1;
  636. buf[*num] = ichar;
  637. putnstr(buf + *num, wlen);
  638. (*num)++;
  639. }
  640. }
  641. static void cread_add_str(char *str, int strsize, int insert, unsigned long *num,
  642. unsigned long *eol_num, char *buf, unsigned long len)
  643. {
  644. while (strsize--) {
  645. cread_add_char(*str, insert, num, eol_num, buf, len);
  646. str++;
  647. }
  648. }
  649. static int cread_line(const char *const prompt, char *buf, unsigned int *len,
  650. int timeout)
  651. {
  652. unsigned long num = 0;
  653. unsigned long eol_num = 0;
  654. unsigned long wlen;
  655. char ichar;
  656. int insert = 1;
  657. int esc_len = 0;
  658. char esc_save[8];
  659. int init_len = strlen(buf);
  660. int first = 1;
  661. if (init_len)
  662. cread_add_str(buf, init_len, 1, &num, &eol_num, buf, *len);
  663. while (1) {
  664. #ifdef CONFIG_BOOT_RETRY_TIME
  665. while (!tstc()) { /* while no incoming data */
  666. if (retry_time >= 0 && get_ticks() > endtime)
  667. return (-2); /* timed out */
  668. WATCHDOG_RESET();
  669. }
  670. #endif
  671. if (first && timeout) {
  672. uint64_t etime = endtick(timeout);
  673. while (!tstc()) { /* while no incoming data */
  674. if (get_ticks() >= etime)
  675. return -2; /* timed out */
  676. WATCHDOG_RESET();
  677. }
  678. first = 0;
  679. }
  680. ichar = getcmd_getch();
  681. if ((ichar == '\n') || (ichar == '\r')) {
  682. putc('\n');
  683. break;
  684. }
  685. /*
  686. * handle standard linux xterm esc sequences for arrow key, etc.
  687. */
  688. if (esc_len != 0) {
  689. if (esc_len == 1) {
  690. if (ichar == '[') {
  691. esc_save[esc_len] = ichar;
  692. esc_len = 2;
  693. } else {
  694. cread_add_str(esc_save, esc_len, insert,
  695. &num, &eol_num, buf, *len);
  696. esc_len = 0;
  697. }
  698. continue;
  699. }
  700. switch (ichar) {
  701. case 'D': /* <- key */
  702. ichar = CTL_CH('b');
  703. esc_len = 0;
  704. break;
  705. case 'C': /* -> key */
  706. ichar = CTL_CH('f');
  707. esc_len = 0;
  708. break; /* pass off to ^F handler */
  709. case 'H': /* Home key */
  710. ichar = CTL_CH('a');
  711. esc_len = 0;
  712. break; /* pass off to ^A handler */
  713. case 'A': /* up arrow */
  714. ichar = CTL_CH('p');
  715. esc_len = 0;
  716. break; /* pass off to ^P handler */
  717. case 'B': /* down arrow */
  718. ichar = CTL_CH('n');
  719. esc_len = 0;
  720. break; /* pass off to ^N handler */
  721. default:
  722. esc_save[esc_len++] = ichar;
  723. cread_add_str(esc_save, esc_len, insert,
  724. &num, &eol_num, buf, *len);
  725. esc_len = 0;
  726. continue;
  727. }
  728. }
  729. switch (ichar) {
  730. case 0x1b:
  731. if (esc_len == 0) {
  732. esc_save[esc_len] = ichar;
  733. esc_len = 1;
  734. } else {
  735. puts("impossible condition #876\n");
  736. esc_len = 0;
  737. }
  738. break;
  739. case CTL_CH('a'):
  740. BEGINNING_OF_LINE();
  741. break;
  742. case CTL_CH('c'): /* ^C - break */
  743. *buf = '\0'; /* discard input */
  744. return (-1);
  745. case CTL_CH('f'):
  746. if (num < eol_num) {
  747. getcmd_putch(buf[num]);
  748. num++;
  749. }
  750. break;
  751. case CTL_CH('b'):
  752. if (num) {
  753. getcmd_putch(CTL_BACKSPACE);
  754. num--;
  755. }
  756. break;
  757. case CTL_CH('d'):
  758. if (num < eol_num) {
  759. wlen = eol_num - num - 1;
  760. if (wlen) {
  761. memmove(&buf[num], &buf[num+1], wlen);
  762. putnstr(buf + num, wlen);
  763. }
  764. getcmd_putch(' ');
  765. do {
  766. getcmd_putch(CTL_BACKSPACE);
  767. } while (wlen--);
  768. eol_num--;
  769. }
  770. break;
  771. case CTL_CH('k'):
  772. ERASE_TO_EOL();
  773. break;
  774. case CTL_CH('e'):
  775. REFRESH_TO_EOL();
  776. break;
  777. case CTL_CH('o'):
  778. insert = !insert;
  779. break;
  780. case CTL_CH('x'):
  781. case CTL_CH('u'):
  782. BEGINNING_OF_LINE();
  783. ERASE_TO_EOL();
  784. break;
  785. case DEL:
  786. case DEL7:
  787. case 8:
  788. if (num) {
  789. wlen = eol_num - num;
  790. num--;
  791. memmove(&buf[num], &buf[num+1], wlen);
  792. getcmd_putch(CTL_BACKSPACE);
  793. putnstr(buf + num, wlen);
  794. getcmd_putch(' ');
  795. do {
  796. getcmd_putch(CTL_BACKSPACE);
  797. } while (wlen--);
  798. eol_num--;
  799. }
  800. break;
  801. case CTL_CH('p'):
  802. case CTL_CH('n'):
  803. {
  804. char * hline;
  805. esc_len = 0;
  806. if (ichar == CTL_CH('p'))
  807. hline = hist_prev();
  808. else
  809. hline = hist_next();
  810. if (!hline) {
  811. getcmd_cbeep();
  812. continue;
  813. }
  814. /* nuke the current line */
  815. /* first, go home */
  816. BEGINNING_OF_LINE();
  817. /* erase to end of line */
  818. ERASE_TO_EOL();
  819. /* copy new line into place and display */
  820. strcpy(buf, hline);
  821. eol_num = strlen(buf);
  822. REFRESH_TO_EOL();
  823. continue;
  824. }
  825. #ifdef CONFIG_AUTO_COMPLETE
  826. case '\t': {
  827. int num2, col;
  828. /* do not autocomplete when in the middle */
  829. if (num < eol_num) {
  830. getcmd_cbeep();
  831. break;
  832. }
  833. buf[num] = '\0';
  834. col = strlen(prompt) + eol_num;
  835. num2 = num;
  836. if (cmd_auto_complete(prompt, buf, &num2, &col)) {
  837. col = num2 - num;
  838. num += col;
  839. eol_num += col;
  840. }
  841. break;
  842. }
  843. #endif
  844. default:
  845. cread_add_char(ichar, insert, &num, &eol_num, buf, *len);
  846. break;
  847. }
  848. }
  849. *len = eol_num;
  850. buf[eol_num] = '\0'; /* lose the newline */
  851. if (buf[0] && buf[0] != CREAD_HIST_CHAR)
  852. cread_add_to_hist(buf);
  853. hist_cur = hist_add_idx;
  854. return 0;
  855. }
  856. #endif /* CONFIG_CMDLINE_EDITING */
  857. /****************************************************************************/
  858. /*
  859. * Prompt for input and read a line.
  860. * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
  861. * time out when time goes past endtime (timebase time in ticks).
  862. * Return: number of read characters
  863. * -1 if break
  864. * -2 if timed out
  865. */
  866. int readline (const char *const prompt)
  867. {
  868. /*
  869. * If console_buffer isn't 0-length the user will be prompted to modify
  870. * it instead of entering it from scratch as desired.
  871. */
  872. console_buffer[0] = '\0';
  873. return readline_into_buffer(prompt, console_buffer, 0);
  874. }
  875. int readline_into_buffer(const char *const prompt, char *buffer, int timeout)
  876. {
  877. char *p = buffer;
  878. #ifdef CONFIG_CMDLINE_EDITING
  879. unsigned int len = CONFIG_SYS_CBSIZE;
  880. int rc;
  881. static int initted = 0;
  882. /*
  883. * History uses a global array which is not
  884. * writable until after relocation to RAM.
  885. * Revert to non-history version if still
  886. * running from flash.
  887. */
  888. if (gd->flags & GD_FLG_RELOC) {
  889. if (!initted) {
  890. hist_init();
  891. initted = 1;
  892. }
  893. if (prompt)
  894. puts (prompt);
  895. rc = cread_line(prompt, p, &len, timeout);
  896. return rc < 0 ? rc : len;
  897. } else {
  898. #endif /* CONFIG_CMDLINE_EDITING */
  899. char * p_buf = p;
  900. int n = 0; /* buffer index */
  901. int plen = 0; /* prompt length */
  902. int col; /* output column cnt */
  903. char c;
  904. /* print prompt */
  905. if (prompt) {
  906. plen = strlen (prompt);
  907. puts (prompt);
  908. }
  909. col = plen;
  910. for (;;) {
  911. #ifdef CONFIG_BOOT_RETRY_TIME
  912. while (!tstc()) { /* while no incoming data */
  913. if (retry_time >= 0 && get_ticks() > endtime)
  914. return (-2); /* timed out */
  915. WATCHDOG_RESET();
  916. }
  917. #endif
  918. WATCHDOG_RESET(); /* Trigger watchdog, if needed */
  919. #ifdef CONFIG_SHOW_ACTIVITY
  920. while (!tstc()) {
  921. show_activity(0);
  922. WATCHDOG_RESET();
  923. }
  924. #endif
  925. c = getc();
  926. /*
  927. * Special character handling
  928. */
  929. switch (c) {
  930. case '\r': /* Enter */
  931. case '\n':
  932. *p = '\0';
  933. puts ("\r\n");
  934. return (p - p_buf);
  935. case '\0': /* nul */
  936. continue;
  937. case 0x03: /* ^C - break */
  938. p_buf[0] = '\0'; /* discard input */
  939. return (-1);
  940. case 0x15: /* ^U - erase line */
  941. while (col > plen) {
  942. puts (erase_seq);
  943. --col;
  944. }
  945. p = p_buf;
  946. n = 0;
  947. continue;
  948. case 0x17: /* ^W - erase word */
  949. p=delete_char(p_buf, p, &col, &n, plen);
  950. while ((n > 0) && (*p != ' ')) {
  951. p=delete_char(p_buf, p, &col, &n, plen);
  952. }
  953. continue;
  954. case 0x08: /* ^H - backspace */
  955. case 0x7F: /* DEL - backspace */
  956. p=delete_char(p_buf, p, &col, &n, plen);
  957. continue;
  958. default:
  959. /*
  960. * Must be a normal character then
  961. */
  962. if (n < CONFIG_SYS_CBSIZE-2) {
  963. if (c == '\t') { /* expand TABs */
  964. #ifdef CONFIG_AUTO_COMPLETE
  965. /* if auto completion triggered just continue */
  966. *p = '\0';
  967. if (cmd_auto_complete(prompt, console_buffer, &n, &col)) {
  968. p = p_buf + n; /* reset */
  969. continue;
  970. }
  971. #endif
  972. puts (tab_seq+(col&07));
  973. col += 8 - (col&07);
  974. } else {
  975. ++col; /* echo input */
  976. putc (c);
  977. }
  978. *p++ = c;
  979. ++n;
  980. } else { /* Buffer full */
  981. putc ('\a');
  982. }
  983. }
  984. }
  985. #ifdef CONFIG_CMDLINE_EDITING
  986. }
  987. #endif
  988. }
  989. /****************************************************************************/
  990. static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen)
  991. {
  992. char *s;
  993. if (*np == 0) {
  994. return (p);
  995. }
  996. if (*(--p) == '\t') { /* will retype the whole line */
  997. while (*colp > plen) {
  998. puts (erase_seq);
  999. (*colp)--;
  1000. }
  1001. for (s=buffer; s<p; ++s) {
  1002. if (*s == '\t') {
  1003. puts (tab_seq+((*colp) & 07));
  1004. *colp += 8 - ((*colp) & 07);
  1005. } else {
  1006. ++(*colp);
  1007. putc (*s);
  1008. }
  1009. }
  1010. } else {
  1011. puts (erase_seq);
  1012. (*colp)--;
  1013. }
  1014. (*np)--;
  1015. return (p);
  1016. }
  1017. /****************************************************************************/
  1018. int parse_line (char *line, char *argv[])
  1019. {
  1020. int nargs = 0;
  1021. #ifdef DEBUG_PARSER
  1022. printf ("parse_line: \"%s\"\n", line);
  1023. #endif
  1024. while (nargs < CONFIG_SYS_MAXARGS) {
  1025. /* skip any white space */
  1026. while (isblank(*line))
  1027. ++line;
  1028. if (*line == '\0') { /* end of line, no more args */
  1029. argv[nargs] = NULL;
  1030. #ifdef DEBUG_PARSER
  1031. printf ("parse_line: nargs=%d\n", nargs);
  1032. #endif
  1033. return (nargs);
  1034. }
  1035. argv[nargs++] = line; /* begin of argument string */
  1036. /* find end of string */
  1037. while (*line && !isblank(*line))
  1038. ++line;
  1039. if (*line == '\0') { /* end of line, no more args */
  1040. argv[nargs] = NULL;
  1041. #ifdef DEBUG_PARSER
  1042. printf ("parse_line: nargs=%d\n", nargs);
  1043. #endif
  1044. return (nargs);
  1045. }
  1046. *line++ = '\0'; /* terminate current arg */
  1047. }
  1048. printf ("** Too many args (max. %d) **\n", CONFIG_SYS_MAXARGS);
  1049. #ifdef DEBUG_PARSER
  1050. printf ("parse_line: nargs=%d\n", nargs);
  1051. #endif
  1052. return (nargs);
  1053. }
  1054. /****************************************************************************/
  1055. #ifndef CONFIG_SYS_HUSH_PARSER
  1056. static void process_macros (const char *input, char *output)
  1057. {
  1058. char c, prev;
  1059. const char *varname_start = NULL;
  1060. int inputcnt = strlen (input);
  1061. int outputcnt = CONFIG_SYS_CBSIZE;
  1062. int state = 0; /* 0 = waiting for '$' */
  1063. /* 1 = waiting for '(' or '{' */
  1064. /* 2 = waiting for ')' or '}' */
  1065. /* 3 = waiting for ''' */
  1066. #ifdef DEBUG_PARSER
  1067. char *output_start = output;
  1068. printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen (input),
  1069. input);
  1070. #endif
  1071. prev = '\0'; /* previous character */
  1072. while (inputcnt && outputcnt) {
  1073. c = *input++;
  1074. inputcnt--;
  1075. if (state != 3) {
  1076. /* remove one level of escape characters */
  1077. if ((c == '\\') && (prev != '\\')) {
  1078. if (inputcnt-- == 0)
  1079. break;
  1080. prev = c;
  1081. c = *input++;
  1082. }
  1083. }
  1084. switch (state) {
  1085. case 0: /* Waiting for (unescaped) $ */
  1086. if ((c == '\'') && (prev != '\\')) {
  1087. state = 3;
  1088. break;
  1089. }
  1090. if ((c == '$') && (prev != '\\')) {
  1091. state++;
  1092. } else {
  1093. *(output++) = c;
  1094. outputcnt--;
  1095. }
  1096. break;
  1097. case 1: /* Waiting for ( */
  1098. if (c == '(' || c == '{') {
  1099. state++;
  1100. varname_start = input;
  1101. } else {
  1102. state = 0;
  1103. *(output++) = '$';
  1104. outputcnt--;
  1105. if (outputcnt) {
  1106. *(output++) = c;
  1107. outputcnt--;
  1108. }
  1109. }
  1110. break;
  1111. case 2: /* Waiting for ) */
  1112. if (c == ')' || c == '}') {
  1113. int i;
  1114. char envname[CONFIG_SYS_CBSIZE], *envval;
  1115. int envcnt = input - varname_start - 1; /* Varname # of chars */
  1116. /* Get the varname */
  1117. for (i = 0; i < envcnt; i++) {
  1118. envname[i] = varname_start[i];
  1119. }
  1120. envname[i] = 0;
  1121. /* Get its value */
  1122. envval = getenv (envname);
  1123. /* Copy into the line if it exists */
  1124. if (envval != NULL)
  1125. while ((*envval) && outputcnt) {
  1126. *(output++) = *(envval++);
  1127. outputcnt--;
  1128. }
  1129. /* Look for another '$' */
  1130. state = 0;
  1131. }
  1132. break;
  1133. case 3: /* Waiting for ' */
  1134. if ((c == '\'') && (prev != '\\')) {
  1135. state = 0;
  1136. } else {
  1137. *(output++) = c;
  1138. outputcnt--;
  1139. }
  1140. break;
  1141. }
  1142. prev = c;
  1143. }
  1144. if (outputcnt)
  1145. *output = 0;
  1146. else
  1147. *(output - 1) = 0;
  1148. #ifdef DEBUG_PARSER
  1149. printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
  1150. strlen (output_start), output_start);
  1151. #endif
  1152. }
  1153. /****************************************************************************
  1154. * returns:
  1155. * 1 - command executed, repeatable
  1156. * 0 - command executed but not repeatable, interrupted commands are
  1157. * always considered not repeatable
  1158. * -1 - not executed (unrecognized, bootd recursion or too many args)
  1159. * (If cmd is NULL or "" or longer than CONFIG_SYS_CBSIZE-1 it is
  1160. * considered unrecognized)
  1161. *
  1162. * WARNING:
  1163. *
  1164. * We must create a temporary copy of the command since the command we get
  1165. * may be the result from getenv(), which returns a pointer directly to
  1166. * the environment data, which may change magicly when the command we run
  1167. * creates or modifies environment variables (like "bootp" does).
  1168. */
  1169. static int builtin_run_command(const char *cmd, int flag)
  1170. {
  1171. char cmdbuf[CONFIG_SYS_CBSIZE]; /* working copy of cmd */
  1172. char *token; /* start of token in cmdbuf */
  1173. char *sep; /* end of token (separator) in cmdbuf */
  1174. char finaltoken[CONFIG_SYS_CBSIZE];
  1175. char *str = cmdbuf;
  1176. char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */
  1177. int argc, inquotes;
  1178. int repeatable = 1;
  1179. int rc = 0;
  1180. #ifdef DEBUG_PARSER
  1181. printf ("[RUN_COMMAND] cmd[%p]=\"", cmd);
  1182. puts (cmd ? cmd : "NULL"); /* use puts - string may be loooong */
  1183. puts ("\"\n");
  1184. #endif
  1185. clear_ctrlc(); /* forget any previous Control C */
  1186. if (!cmd || !*cmd) {
  1187. return -1; /* empty command */
  1188. }
  1189. if (strlen(cmd) >= CONFIG_SYS_CBSIZE) {
  1190. puts ("## Command too long!\n");
  1191. return -1;
  1192. }
  1193. strcpy (cmdbuf, cmd);
  1194. /* Process separators and check for invalid
  1195. * repeatable commands
  1196. */
  1197. #ifdef DEBUG_PARSER
  1198. printf ("[PROCESS_SEPARATORS] %s\n", cmd);
  1199. #endif
  1200. while (*str) {
  1201. /*
  1202. * Find separator, or string end
  1203. * Allow simple escape of ';' by writing "\;"
  1204. */
  1205. for (inquotes = 0, sep = str; *sep; sep++) {
  1206. if ((*sep=='\'') &&
  1207. (*(sep-1) != '\\'))
  1208. inquotes=!inquotes;
  1209. if (!inquotes &&
  1210. (*sep == ';') && /* separator */
  1211. ( sep != str) && /* past string start */
  1212. (*(sep-1) != '\\')) /* and NOT escaped */
  1213. break;
  1214. }
  1215. /*
  1216. * Limit the token to data between separators
  1217. */
  1218. token = str;
  1219. if (*sep) {
  1220. str = sep + 1; /* start of command for next pass */
  1221. *sep = '\0';
  1222. }
  1223. else
  1224. str = sep; /* no more commands for next pass */
  1225. #ifdef DEBUG_PARSER
  1226. printf ("token: \"%s\"\n", token);
  1227. #endif
  1228. /* find macros in this token and replace them */
  1229. process_macros (token, finaltoken);
  1230. /* Extract arguments */
  1231. if ((argc = parse_line (finaltoken, argv)) == 0) {
  1232. rc = -1; /* no command at all */
  1233. continue;
  1234. }
  1235. if (cmd_process(flag, argc, argv, &repeatable))
  1236. rc = -1;
  1237. /* Did the user stop this? */
  1238. if (had_ctrlc ())
  1239. return -1; /* if stopped then not repeatable */
  1240. }
  1241. return rc ? rc : repeatable;
  1242. }
  1243. #endif
  1244. /*
  1245. * Run a command using the selected parser.
  1246. *
  1247. * @param cmd Command to run
  1248. * @param flag Execution flags (CMD_FLAG_...)
  1249. * @return 0 on success, or != 0 on error.
  1250. */
  1251. int run_command(const char *cmd, int flag)
  1252. {
  1253. #ifndef CONFIG_SYS_HUSH_PARSER
  1254. /*
  1255. * builtin_run_command can return 0 or 1 for success, so clean up
  1256. * its result.
  1257. */
  1258. if (builtin_run_command(cmd, flag) == -1)
  1259. return 1;
  1260. return 0;
  1261. #else
  1262. return parse_string_outer(cmd,
  1263. FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP);
  1264. #endif
  1265. }
  1266. #ifndef CONFIG_SYS_HUSH_PARSER
  1267. /**
  1268. * Execute a list of command separated by ; or \n using the built-in parser.
  1269. *
  1270. * This function cannot take a const char * for the command, since if it
  1271. * finds newlines in the string, it replaces them with \0.
  1272. *
  1273. * @param cmd String containing list of commands
  1274. * @param flag Execution flags (CMD_FLAG_...)
  1275. * @return 0 on success, or != 0 on error.
  1276. */
  1277. static int builtin_run_command_list(char *cmd, int flag)
  1278. {
  1279. char *line, *next;
  1280. int rcode = 0;
  1281. /*
  1282. * Break into individual lines, and execute each line; terminate on
  1283. * error.
  1284. */
  1285. line = next = cmd;
  1286. while (*next) {
  1287. if (*next == '\n') {
  1288. *next = '\0';
  1289. /* run only non-empty commands */
  1290. if (*line) {
  1291. debug("** exec: \"%s\"\n", line);
  1292. if (builtin_run_command(line, 0) < 0) {
  1293. rcode = 1;
  1294. break;
  1295. }
  1296. }
  1297. line = next + 1;
  1298. }
  1299. ++next;
  1300. }
  1301. if (rcode == 0 && *line)
  1302. rcode = (builtin_run_command(line, 0) >= 0);
  1303. return rcode;
  1304. }
  1305. #endif
  1306. int run_command_list(const char *cmd, int len, int flag)
  1307. {
  1308. int need_buff = 1;
  1309. char *buff = (char *)cmd; /* cast away const */
  1310. int rcode = 0;
  1311. if (len == -1) {
  1312. len = strlen(cmd);
  1313. #ifdef CONFIG_SYS_HUSH_PARSER
  1314. /* hush will never change our string */
  1315. need_buff = 0;
  1316. #else
  1317. /* the built-in parser will change our string if it sees \n */
  1318. need_buff = strchr(cmd, '\n') != NULL;
  1319. #endif
  1320. }
  1321. if (need_buff) {
  1322. buff = malloc(len + 1);
  1323. if (!buff)
  1324. return 1;
  1325. memcpy(buff, cmd, len);
  1326. buff[len] = '\0';
  1327. }
  1328. #ifdef CONFIG_SYS_HUSH_PARSER
  1329. rcode = parse_string_outer(buff, FLAG_PARSE_SEMICOLON);
  1330. #else
  1331. /*
  1332. * This function will overwrite any \n it sees with a \0, which
  1333. * is why it can't work with a const char *. Here we are making
  1334. * using of internal knowledge of this function, to avoid always
  1335. * doing a malloc() which is actually required only in a case that
  1336. * is pretty rare.
  1337. */
  1338. rcode = builtin_run_command_list(buff, flag);
  1339. if (need_buff)
  1340. free(buff);
  1341. #endif
  1342. return rcode;
  1343. }
  1344. /****************************************************************************/
  1345. #if defined(CONFIG_CMD_RUN)
  1346. int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
  1347. {
  1348. int i;
  1349. if (argc < 2)
  1350. return CMD_RET_USAGE;
  1351. for (i=1; i<argc; ++i) {
  1352. char *arg;
  1353. if ((arg = getenv (argv[i])) == NULL) {
  1354. printf ("## Error: \"%s\" not defined\n", argv[i]);
  1355. return 1;
  1356. }
  1357. if (run_command(arg, flag) != 0)
  1358. return 1;
  1359. }
  1360. return 0;
  1361. }
  1362. #endif