main.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
  1. /*
  2. * (C) Copyright 2000
  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. /* #define DEBUG */
  24. #include <common.h>
  25. #include <watchdog.h>
  26. #include <command.h>
  27. #include <malloc.h>
  28. #ifdef CFG_HUSH_PARSER
  29. #include <hush.h>
  30. #endif
  31. #include <post.h>
  32. #if defined(CONFIG_BOOT_RETRY_TIME) && defined(CONFIG_RESET_TO_RETRY)
  33. extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]); /* for do_reset() prototype */
  34. #endif
  35. extern int do_bootd (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
  36. #define MAX_DELAY_STOP_STR 32
  37. static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen);
  38. static int parse_line (char *, char *[]);
  39. #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
  40. static int abortboot(int);
  41. #endif
  42. #undef DEBUG_PARSER
  43. char console_buffer[CFG_CBSIZE]; /* console I/O buffer */
  44. static char erase_seq[] = "\b \b"; /* erase sequence */
  45. static char tab_seq[] = " "; /* used to expand TABs */
  46. #ifdef CONFIG_BOOT_RETRY_TIME
  47. static uint64_t endtime = 0; /* must be set, default is instant timeout */
  48. static int retry_time = -1; /* -1 so can call readline before main_loop */
  49. #endif
  50. #define endtick(seconds) (get_ticks() + (uint64_t)(seconds) * get_tbclk())
  51. #ifndef CONFIG_BOOT_RETRY_MIN
  52. #define CONFIG_BOOT_RETRY_MIN CONFIG_BOOT_RETRY_TIME
  53. #endif
  54. #ifdef CONFIG_MODEM_SUPPORT
  55. int do_mdm_init = 0;
  56. extern void mdm_init(void); /* defined in board.c */
  57. #endif
  58. /***************************************************************************
  59. * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
  60. * returns: 0 - no key string, allow autoboot
  61. * 1 - got key string, abort
  62. */
  63. #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
  64. # if defined(CONFIG_AUTOBOOT_KEYED)
  65. static __inline__ int abortboot(int bootdelay)
  66. {
  67. int abort = 0;
  68. uint64_t etime = endtick(bootdelay);
  69. struct
  70. {
  71. char* str;
  72. u_int len;
  73. int retry;
  74. }
  75. delaykey [] =
  76. {
  77. { str: getenv ("bootdelaykey"), retry: 1 },
  78. { str: getenv ("bootdelaykey2"), retry: 1 },
  79. { str: getenv ("bootstopkey"), retry: 0 },
  80. { str: getenv ("bootstopkey2"), retry: 0 },
  81. };
  82. char presskey [MAX_DELAY_STOP_STR];
  83. u_int presskey_len = 0;
  84. u_int presskey_max = 0;
  85. u_int i;
  86. #ifdef CONFIG_SILENT_CONSOLE
  87. {
  88. DECLARE_GLOBAL_DATA_PTR;
  89. if (gd->flags & GD_FLG_SILENT) {
  90. /* Restore serial console */
  91. console_assign (stdout, "serial");
  92. console_assign (stderr, "serial");
  93. }
  94. }
  95. #endif
  96. # ifdef CONFIG_AUTOBOOT_PROMPT
  97. printf (CONFIG_AUTOBOOT_PROMPT, bootdelay);
  98. # endif
  99. # ifdef CONFIG_AUTOBOOT_DELAY_STR
  100. if (delaykey[0].str == NULL)
  101. delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
  102. # endif
  103. # ifdef CONFIG_AUTOBOOT_DELAY_STR2
  104. if (delaykey[1].str == NULL)
  105. delaykey[1].str = CONFIG_AUTOBOOT_DELAY_STR2;
  106. # endif
  107. # ifdef CONFIG_AUTOBOOT_STOP_STR
  108. if (delaykey[2].str == NULL)
  109. delaykey[2].str = CONFIG_AUTOBOOT_STOP_STR;
  110. # endif
  111. # ifdef CONFIG_AUTOBOOT_STOP_STR2
  112. if (delaykey[3].str == NULL)
  113. delaykey[3].str = CONFIG_AUTOBOOT_STOP_STR2;
  114. # endif
  115. for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
  116. delaykey[i].len = delaykey[i].str == NULL ?
  117. 0 : strlen (delaykey[i].str);
  118. delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
  119. MAX_DELAY_STOP_STR : delaykey[i].len;
  120. presskey_max = presskey_max > delaykey[i].len ?
  121. presskey_max : delaykey[i].len;
  122. # if DEBUG_BOOTKEYS
  123. printf("%s key:<%s>\n",
  124. delaykey[i].retry ? "delay" : "stop",
  125. delaykey[i].str ? delaykey[i].str : "NULL");
  126. # endif
  127. }
  128. /* In order to keep up with incoming data, check timeout only
  129. * when catch up.
  130. */
  131. while (!abort && get_ticks() <= etime) {
  132. for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
  133. if (delaykey[i].len > 0 &&
  134. presskey_len >= delaykey[i].len &&
  135. memcmp (presskey + presskey_len - delaykey[i].len,
  136. delaykey[i].str,
  137. delaykey[i].len) == 0) {
  138. # if DEBUG_BOOTKEYS
  139. printf("got %skey\n",
  140. delaykey[i].retry ? "delay" : "stop");
  141. # endif
  142. # ifdef CONFIG_BOOT_RETRY_TIME
  143. /* don't retry auto boot */
  144. if (! delaykey[i].retry)
  145. retry_time = -1;
  146. # endif
  147. abort = 1;
  148. }
  149. }
  150. if (tstc()) {
  151. if (presskey_len < presskey_max) {
  152. presskey [presskey_len ++] = getc();
  153. }
  154. else {
  155. for (i = 0; i < presskey_max - 1; i ++)
  156. presskey [i] = presskey [i + 1];
  157. presskey [i] = getc();
  158. }
  159. }
  160. }
  161. # if DEBUG_BOOTKEYS
  162. if (!abort)
  163. printf("key timeout\n");
  164. # endif
  165. #ifdef CONFIG_SILENT_CONSOLE
  166. {
  167. DECLARE_GLOBAL_DATA_PTR;
  168. if (abort) {
  169. /* permanently enable normal console output */
  170. gd->flags &= ~(GD_FLG_SILENT);
  171. } else if (gd->flags & GD_FLG_SILENT) {
  172. /* Restore silent console */
  173. console_assign (stdout, "nulldev");
  174. console_assign (stderr, "nulldev");
  175. }
  176. }
  177. #endif
  178. return abort;
  179. }
  180. # else /* !defined(CONFIG_AUTOBOOT_KEYED) */
  181. #ifdef CONFIG_MENUKEY
  182. static int menukey = 0;
  183. #endif
  184. static __inline__ int abortboot(int bootdelay)
  185. {
  186. int abort = 0;
  187. #ifdef CONFIG_SILENT_CONSOLE
  188. {
  189. DECLARE_GLOBAL_DATA_PTR;
  190. if (gd->flags & GD_FLG_SILENT) {
  191. /* Restore serial console */
  192. console_assign (stdout, "serial");
  193. console_assign (stderr, "serial");
  194. }
  195. }
  196. #endif
  197. #ifdef CONFIG_MENUPROMPT
  198. printf(CONFIG_MENUPROMPT, bootdelay);
  199. #else
  200. printf("Hit any key to stop autoboot: %2d ", bootdelay);
  201. #endif
  202. #if defined CONFIG_ZERO_BOOTDELAY_CHECK
  203. /*
  204. * Check if key already pressed
  205. * Don't check if bootdelay < 0
  206. */
  207. if (bootdelay >= 0) {
  208. if (tstc()) { /* we got a key press */
  209. (void) getc(); /* consume input */
  210. printf ("\b\b\b 0");
  211. abort = 1; /* don't auto boot */
  212. }
  213. }
  214. #endif
  215. while ((bootdelay > 0) && (!abort)) {
  216. int i;
  217. --bootdelay;
  218. /* delay 100 * 10ms */
  219. for (i=0; !abort && i<100; ++i) {
  220. if (tstc()) { /* we got a key press */
  221. abort = 1; /* don't auto boot */
  222. bootdelay = 0; /* no more delay */
  223. # ifdef CONFIG_MENUKEY
  224. menukey = getc();
  225. # else
  226. (void) getc(); /* consume input */
  227. # endif
  228. break;
  229. }
  230. udelay (10000);
  231. }
  232. printf ("\b\b\b%2d ", bootdelay);
  233. }
  234. putc ('\n');
  235. #ifdef CONFIG_SILENT_CONSOLE
  236. {
  237. DECLARE_GLOBAL_DATA_PTR;
  238. if (abort) {
  239. /* permanently enable normal console output */
  240. gd->flags &= ~(GD_FLG_SILENT);
  241. } else if (gd->flags & GD_FLG_SILENT) {
  242. /* Restore silent console */
  243. console_assign (stdout, "nulldev");
  244. console_assign (stderr, "nulldev");
  245. }
  246. }
  247. #endif
  248. return abort;
  249. }
  250. # endif /* CONFIG_AUTOBOOT_KEYED */
  251. #endif /* CONFIG_BOOTDELAY >= 0 */
  252. /****************************************************************************/
  253. void main_loop (void)
  254. {
  255. #ifndef CFG_HUSH_PARSER
  256. static char lastcommand[CFG_CBSIZE] = { 0, };
  257. int len;
  258. int rc = 1;
  259. int flag;
  260. #endif
  261. #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
  262. char *s;
  263. int bootdelay;
  264. #endif
  265. #ifdef CONFIG_PREBOOT
  266. char *p;
  267. #endif
  268. #ifdef CONFIG_BOOTCOUNT_LIMIT
  269. unsigned long bootcount = 0;
  270. unsigned long bootlimit = 0;
  271. char *bcs;
  272. char bcs_set[16];
  273. #endif /* CONFIG_BOOTCOUNT_LIMIT */
  274. #if defined(CONFIG_VFD) && defined(VFD_TEST_LOGO)
  275. ulong bmp = 0; /* default bitmap */
  276. extern int trab_vfd (ulong bitmap);
  277. #ifdef CONFIG_MODEM_SUPPORT
  278. if (do_mdm_init)
  279. bmp = 1; /* alternate bitmap */
  280. #endif
  281. trab_vfd (bmp);
  282. #endif /* CONFIG_VFD && VFD_TEST_LOGO */
  283. #ifdef CONFIG_BOOTCOUNT_LIMIT
  284. bootcount = bootcount_load();
  285. bootcount++;
  286. bootcount_store (bootcount);
  287. sprintf (bcs_set, "%lu", bootcount);
  288. setenv ("bootcount", bcs_set);
  289. bcs = getenv ("bootlimit");
  290. bootlimit = bcs ? simple_strtoul (bcs, NULL, 10) : 0;
  291. #endif /* CONFIG_BOOTCOUNT_LIMIT */
  292. #ifdef CONFIG_MODEM_SUPPORT
  293. debug ("DEBUG: main_loop: do_mdm_init=%d\n", do_mdm_init);
  294. if (do_mdm_init) {
  295. uchar *str = strdup(getenv("mdm_cmd"));
  296. setenv ("preboot", str); /* set or delete definition */
  297. if (str != NULL)
  298. free (str);
  299. mdm_init(); /* wait for modem connection */
  300. }
  301. #endif /* CONFIG_MODEM_SUPPORT */
  302. #ifdef CONFIG_VERSION_VARIABLE
  303. {
  304. extern char version_string[];
  305. setenv ("ver", version_string); /* set version variable */
  306. }
  307. #endif /* CONFIG_VERSION_VARIABLE */
  308. #ifdef CFG_HUSH_PARSER
  309. u_boot_hush_start ();
  310. #endif
  311. #ifdef CONFIG_PREBOOT
  312. if ((p = getenv ("preboot")) != NULL) {
  313. # ifdef CONFIG_AUTOBOOT_KEYED
  314. int prev = disable_ctrlc(1); /* disable Control C checking */
  315. # endif
  316. # ifndef CFG_HUSH_PARSER
  317. run_command (p, 0);
  318. # else
  319. parse_string_outer(p, FLAG_PARSE_SEMICOLON |
  320. FLAG_EXIT_FROM_LOOP);
  321. # endif
  322. # ifdef CONFIG_AUTOBOOT_KEYED
  323. disable_ctrlc(prev); /* restore Control C checking */
  324. # endif
  325. }
  326. #endif /* CONFIG_PREBOOT */
  327. #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
  328. s = getenv ("bootdelay");
  329. bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
  330. debug ("### main_loop entered: bootdelay=%d\n\n", bootdelay);
  331. # ifdef CONFIG_BOOT_RETRY_TIME
  332. init_cmd_timeout ();
  333. # endif /* CONFIG_BOOT_RETRY_TIME */
  334. #ifdef CONFIG_BOOTCOUNT_LIMIT
  335. if (bootlimit && (bootcount > bootlimit)) {
  336. printf ("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",
  337. (unsigned)bootlimit);
  338. s = getenv ("altbootcmd");
  339. }
  340. else
  341. #endif /* CONFIG_BOOTCOUNT_LIMIT */
  342. s = getenv ("bootcmd");
  343. debug ("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
  344. if (bootdelay >= 0 && s && !abortboot (bootdelay)) {
  345. # ifdef CONFIG_AUTOBOOT_KEYED
  346. int prev = disable_ctrlc(1); /* disable Control C checking */
  347. # endif
  348. # ifndef CFG_HUSH_PARSER
  349. run_command (s, 0);
  350. # else
  351. parse_string_outer(s, FLAG_PARSE_SEMICOLON |
  352. FLAG_EXIT_FROM_LOOP);
  353. # endif
  354. # ifdef CONFIG_AUTOBOOT_KEYED
  355. disable_ctrlc(prev); /* restore Control C checking */
  356. # endif
  357. }
  358. # ifdef CONFIG_MENUKEY
  359. if (menukey == CONFIG_MENUKEY) {
  360. s = getenv("menucmd");
  361. if (s) {
  362. # ifndef CFG_HUSH_PARSER
  363. run_command (s, bd, 0);
  364. # else
  365. parse_string_outer(s, FLAG_PARSE_SEMICOLON |
  366. FLAG_EXIT_FROM_LOOP);
  367. # endif
  368. }
  369. }
  370. #endif /* CONFIG_MENUKEY */
  371. #endif /* CONFIG_BOOTDELAY */
  372. #ifdef CONFIG_AMIGAONEG3SE
  373. {
  374. extern void video_banner(void);
  375. video_banner();
  376. }
  377. #endif
  378. /*
  379. * Main Loop for Monitor Command Processing
  380. */
  381. #ifdef CFG_HUSH_PARSER
  382. parse_file_outer();
  383. /* This point is never reached */
  384. for (;;);
  385. #else
  386. for (;;) {
  387. #ifdef CONFIG_BOOT_RETRY_TIME
  388. if (rc >= 0) {
  389. /* Saw enough of a valid command to
  390. * restart the timeout.
  391. */
  392. reset_cmd_timeout();
  393. }
  394. #endif
  395. len = readline (CFG_PROMPT);
  396. flag = 0; /* assume no special flags for now */
  397. if (len > 0)
  398. strcpy (lastcommand, console_buffer);
  399. else if (len == 0)
  400. flag |= CMD_FLAG_REPEAT;
  401. #ifdef CONFIG_BOOT_RETRY_TIME
  402. else if (len == -2) {
  403. /* -2 means timed out, retry autoboot
  404. */
  405. printf("\nTimed out waiting for command\n");
  406. # ifdef CONFIG_RESET_TO_RETRY
  407. /* Reinit board to run initialization code again */
  408. do_reset (NULL, 0, 0, NULL);
  409. # else
  410. return; /* retry autoboot */
  411. # endif
  412. }
  413. #endif
  414. if (len == -1)
  415. printf ("<INTERRUPT>\n");
  416. else
  417. rc = run_command (lastcommand, flag);
  418. if (rc <= 0) {
  419. /* invalid command or not repeatable, forget it */
  420. lastcommand[0] = 0;
  421. }
  422. }
  423. #endif /*CFG_HUSH_PARSER*/
  424. }
  425. #ifdef CONFIG_BOOT_RETRY_TIME
  426. /***************************************************************************
  427. * initialise command line timeout
  428. */
  429. void init_cmd_timeout(void)
  430. {
  431. char *s = getenv ("bootretry");
  432. if (s != NULL)
  433. retry_time = (int)simple_strtol(s, NULL, 10);
  434. else
  435. retry_time = CONFIG_BOOT_RETRY_TIME;
  436. if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN)
  437. retry_time = CONFIG_BOOT_RETRY_MIN;
  438. }
  439. /***************************************************************************
  440. * reset command line timeout to retry_time seconds
  441. */
  442. void reset_cmd_timeout(void)
  443. {
  444. endtime = endtick(retry_time);
  445. }
  446. #endif
  447. /****************************************************************************/
  448. /*
  449. * Prompt for input and read a line.
  450. * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
  451. * time out when time goes past endtime (timebase time in ticks).
  452. * Return: number of read characters
  453. * -1 if break
  454. * -2 if timed out
  455. */
  456. int readline (const char *const prompt)
  457. {
  458. char *p = console_buffer;
  459. int n = 0; /* buffer index */
  460. int plen = 0; /* prompt length */
  461. int col; /* output column cnt */
  462. char c;
  463. /* print prompt */
  464. if (prompt) {
  465. plen = strlen (prompt);
  466. puts (prompt);
  467. }
  468. col = plen;
  469. for (;;) {
  470. #ifdef CONFIG_BOOT_RETRY_TIME
  471. while (!tstc()) { /* while no incoming data */
  472. if (retry_time >= 0 && get_ticks() > endtime)
  473. return (-2); /* timed out */
  474. }
  475. #endif
  476. WATCHDOG_RESET(); /* Trigger watchdog, if needed */
  477. #ifdef CONFIG_SHOW_ACTIVITY
  478. while (!tstc()) {
  479. extern void show_activity(int arg);
  480. show_activity(0);
  481. }
  482. #endif
  483. c = getc();
  484. /*
  485. * Special character handling
  486. */
  487. switch (c) {
  488. case '\r': /* Enter */
  489. case '\n':
  490. *p = '\0';
  491. puts ("\r\n");
  492. return (p - console_buffer);
  493. case 0x03: /* ^C - break */
  494. console_buffer[0] = '\0'; /* discard input */
  495. return (-1);
  496. case 0x15: /* ^U - erase line */
  497. while (col > plen) {
  498. puts (erase_seq);
  499. --col;
  500. }
  501. p = console_buffer;
  502. n = 0;
  503. continue;
  504. case 0x17: /* ^W - erase word */
  505. p=delete_char(console_buffer, p, &col, &n, plen);
  506. while ((n > 0) && (*p != ' ')) {
  507. p=delete_char(console_buffer, p, &col, &n, plen);
  508. }
  509. continue;
  510. case 0x08: /* ^H - backspace */
  511. case 0x7F: /* DEL - backspace */
  512. p=delete_char(console_buffer, p, &col, &n, plen);
  513. continue;
  514. default:
  515. /*
  516. * Must be a normal character then
  517. */
  518. if (n < CFG_CBSIZE-2) {
  519. if (c == '\t') { /* expand TABs */
  520. puts (tab_seq+(col&07));
  521. col += 8 - (col&07);
  522. } else {
  523. ++col; /* echo input */
  524. putc (c);
  525. }
  526. *p++ = c;
  527. ++n;
  528. } else { /* Buffer full */
  529. putc ('\a');
  530. }
  531. }
  532. }
  533. }
  534. /****************************************************************************/
  535. static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen)
  536. {
  537. char *s;
  538. if (*np == 0) {
  539. return (p);
  540. }
  541. if (*(--p) == '\t') { /* will retype the whole line */
  542. while (*colp > plen) {
  543. puts (erase_seq);
  544. (*colp)--;
  545. }
  546. for (s=buffer; s<p; ++s) {
  547. if (*s == '\t') {
  548. puts (tab_seq+((*colp) & 07));
  549. *colp += 8 - ((*colp) & 07);
  550. } else {
  551. ++(*colp);
  552. putc (*s);
  553. }
  554. }
  555. } else {
  556. puts (erase_seq);
  557. (*colp)--;
  558. }
  559. (*np)--;
  560. return (p);
  561. }
  562. /****************************************************************************/
  563. int parse_line (char *line, char *argv[])
  564. {
  565. int nargs = 0;
  566. #ifdef DEBUG_PARSER
  567. printf ("parse_line: \"%s\"\n", line);
  568. #endif
  569. while (nargs < CFG_MAXARGS) {
  570. /* skip any white space */
  571. while ((*line == ' ') || (*line == '\t')) {
  572. ++line;
  573. }
  574. if (*line == '\0') { /* end of line, no more args */
  575. argv[nargs] = NULL;
  576. #ifdef DEBUG_PARSER
  577. printf ("parse_line: nargs=%d\n", nargs);
  578. #endif
  579. return (nargs);
  580. }
  581. argv[nargs++] = line; /* begin of argument string */
  582. /* find end of string */
  583. while (*line && (*line != ' ') && (*line != '\t')) {
  584. ++line;
  585. }
  586. if (*line == '\0') { /* end of line, no more args */
  587. argv[nargs] = NULL;
  588. #ifdef DEBUG_PARSER
  589. printf ("parse_line: nargs=%d\n", nargs);
  590. #endif
  591. return (nargs);
  592. }
  593. *line++ = '\0'; /* terminate current arg */
  594. }
  595. printf ("** Too many args (max. %d) **\n", CFG_MAXARGS);
  596. #ifdef DEBUG_PARSER
  597. printf ("parse_line: nargs=%d\n", nargs);
  598. #endif
  599. return (nargs);
  600. }
  601. /****************************************************************************/
  602. static void process_macros (const char *input, char *output)
  603. {
  604. char c, prev;
  605. const char *varname_start = NULL;
  606. int inputcnt = strlen (input);
  607. int outputcnt = CFG_CBSIZE;
  608. int state = 0; /* 0 = waiting for '$' */
  609. /* 1 = waiting for '(' */
  610. /* 2 = waiting for ')' */
  611. /* 3 = waiting for ''' */
  612. #ifdef DEBUG_PARSER
  613. char *output_start = output;
  614. printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen(input), input);
  615. #endif
  616. prev = '\0'; /* previous character */
  617. while (inputcnt && outputcnt) {
  618. c = *input++;
  619. inputcnt--;
  620. if (state!=3) {
  621. /* remove one level of escape characters */
  622. if ((c == '\\') && (prev != '\\')) {
  623. if (inputcnt-- == 0)
  624. break;
  625. prev = c;
  626. c = *input++;
  627. }
  628. }
  629. switch (state) {
  630. case 0: /* Waiting for (unescaped) $ */
  631. if ((c == '\'') && (prev != '\\')) {
  632. state = 3;
  633. break;
  634. }
  635. if ((c == '$') && (prev != '\\')) {
  636. state++;
  637. } else {
  638. *(output++) = c;
  639. outputcnt--;
  640. }
  641. break;
  642. case 1: /* Waiting for ( */
  643. if (c == '(') {
  644. state++;
  645. varname_start = input;
  646. } else {
  647. state = 0;
  648. *(output++) = '$';
  649. outputcnt--;
  650. if (outputcnt) {
  651. *(output++) = c;
  652. outputcnt--;
  653. }
  654. }
  655. break;
  656. case 2: /* Waiting for ) */
  657. if (c == ')') {
  658. int i;
  659. char envname[CFG_CBSIZE], *envval;
  660. int envcnt = input-varname_start-1; /* Varname # of chars */
  661. /* Get the varname */
  662. for (i = 0; i < envcnt; i++) {
  663. envname[i] = varname_start[i];
  664. }
  665. envname[i] = 0;
  666. /* Get its value */
  667. envval = getenv (envname);
  668. /* Copy into the line if it exists */
  669. if (envval != NULL)
  670. while ((*envval) && outputcnt) {
  671. *(output++) = *(envval++);
  672. outputcnt--;
  673. }
  674. /* Look for another '$' */
  675. state = 0;
  676. }
  677. break;
  678. case 3: /* Waiting for ' */
  679. if ((c == '\'') && (prev != '\\')) {
  680. state = 0;
  681. } else {
  682. *(output++) = c;
  683. outputcnt--;
  684. }
  685. break;
  686. }
  687. prev = c;
  688. }
  689. if (outputcnt)
  690. *output = 0;
  691. #ifdef DEBUG_PARSER
  692. printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
  693. strlen(output_start), output_start);
  694. #endif
  695. }
  696. /****************************************************************************
  697. * returns:
  698. * 1 - command executed, repeatable
  699. * 0 - command executed but not repeatable, interrupted commands are
  700. * always considered not repeatable
  701. * -1 - not executed (unrecognized, bootd recursion or too many args)
  702. * (If cmd is NULL or "" or longer than CFG_CBSIZE-1 it is
  703. * considered unrecognized)
  704. *
  705. * WARNING:
  706. *
  707. * We must create a temporary copy of the command since the command we get
  708. * may be the result from getenv(), which returns a pointer directly to
  709. * the environment data, which may change magicly when the command we run
  710. * creates or modifies environment variables (like "bootp" does).
  711. */
  712. int run_command (const char *cmd, int flag)
  713. {
  714. cmd_tbl_t *cmdtp;
  715. char cmdbuf[CFG_CBSIZE]; /* working copy of cmd */
  716. char *token; /* start of token in cmdbuf */
  717. char *sep; /* end of token (separator) in cmdbuf */
  718. char finaltoken[CFG_CBSIZE];
  719. char *str = cmdbuf;
  720. char *argv[CFG_MAXARGS + 1]; /* NULL terminated */
  721. int argc, inquotes;
  722. int repeatable = 1;
  723. int rc = 0;
  724. #ifdef DEBUG_PARSER
  725. printf ("[RUN_COMMAND] cmd[%p]=\"", cmd);
  726. puts (cmd ? cmd : "NULL"); /* use puts - string may be loooong */
  727. puts ("\"\n");
  728. #endif
  729. clear_ctrlc(); /* forget any previous Control C */
  730. if (!cmd || !*cmd) {
  731. return -1; /* empty command */
  732. }
  733. if (strlen(cmd) >= CFG_CBSIZE) {
  734. puts ("## Command too long!\n");
  735. return -1;
  736. }
  737. strcpy (cmdbuf, cmd);
  738. /* Process separators and check for invalid
  739. * repeatable commands
  740. */
  741. #ifdef DEBUG_PARSER
  742. printf ("[PROCESS_SEPARATORS] %s\n", cmd);
  743. #endif
  744. while (*str) {
  745. /*
  746. * Find separator, or string end
  747. * Allow simple escape of ';' by writing "\;"
  748. */
  749. for (inquotes = 0, sep = str; *sep; sep++) {
  750. if ((*sep=='\'') &&
  751. (*(sep-1) != '\\'))
  752. inquotes=!inquotes;
  753. if (!inquotes &&
  754. (*sep == ';') && /* separator */
  755. ( sep != str) && /* past string start */
  756. (*(sep-1) != '\\')) /* and NOT escaped */
  757. break;
  758. }
  759. /*
  760. * Limit the token to data between separators
  761. */
  762. token = str;
  763. if (*sep) {
  764. str = sep + 1; /* start of command for next pass */
  765. *sep = '\0';
  766. }
  767. else
  768. str = sep; /* no more commands for next pass */
  769. #ifdef DEBUG_PARSER
  770. printf ("token: \"%s\"\n", token);
  771. #endif
  772. /* find macros in this token and replace them */
  773. process_macros (token, finaltoken);
  774. /* Extract arguments */
  775. argc = parse_line (finaltoken, argv);
  776. /* Look up command in command table */
  777. if ((cmdtp = find_cmd(argv[0])) == NULL) {
  778. printf ("Unknown command '%s' - try 'help'\n", argv[0]);
  779. rc = -1; /* give up after bad command */
  780. continue;
  781. }
  782. /* found - check max args */
  783. if (argc > cmdtp->maxargs) {
  784. printf ("Usage:\n%s\n", cmdtp->usage);
  785. rc = -1;
  786. continue;
  787. }
  788. #if (CONFIG_COMMANDS & CFG_CMD_BOOTD)
  789. /* avoid "bootd" recursion */
  790. if (cmdtp->cmd == do_bootd) {
  791. #ifdef DEBUG_PARSER
  792. printf ("[%s]\n", finaltoken);
  793. #endif
  794. if (flag & CMD_FLAG_BOOTD) {
  795. printf ("'bootd' recursion detected\n");
  796. rc = -1;
  797. continue;
  798. }
  799. else
  800. flag |= CMD_FLAG_BOOTD;
  801. }
  802. #endif /* CFG_CMD_BOOTD */
  803. /* OK - call function to do the command */
  804. if ((cmdtp->cmd) (cmdtp, flag, argc, argv) != 0) {
  805. rc = -1;
  806. }
  807. repeatable &= cmdtp->repeatable;
  808. /* Did the user stop this? */
  809. if (had_ctrlc ())
  810. return 0; /* if stopped then not repeatable */
  811. }
  812. return rc ? rc : repeatable;
  813. }
  814. /****************************************************************************/
  815. #if (CONFIG_COMMANDS & CFG_CMD_RUN)
  816. int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
  817. {
  818. int i;
  819. if (argc < 2) {
  820. printf ("Usage:\n%s\n", cmdtp->usage);
  821. return 1;
  822. }
  823. for (i=1; i<argc; ++i) {
  824. char *arg;
  825. if ((arg = getenv (argv[i])) == NULL) {
  826. printf ("## Error: \"%s\" not defined\n", argv[i]);
  827. return 1;
  828. }
  829. #ifndef CFG_HUSH_PARSER
  830. if (run_command (arg, flag) == -1)
  831. return 1;
  832. #else
  833. if (parse_string_outer(arg,
  834. FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0)
  835. return 1;
  836. #endif
  837. }
  838. return 0;
  839. }
  840. #endif /* CFG_CMD_RUN */