main.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416
  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. #ifdef CONFIG_MODEM_SUPPORT
  32. #include <malloc.h> /* for free() prototype */
  33. #endif
  34. #ifdef CONFIG_SYS_HUSH_PARSER
  35. #include <hush.h>
  36. #endif
  37. #include <post.h>
  38. #if defined(CONFIG_SILENT_CONSOLE) || defined(CONFIG_POST) || defined(CONFIG_CMDLINE_EDITING)
  39. DECLARE_GLOBAL_DATA_PTR;
  40. #endif
  41. /*
  42. * Board-specific Platform code can reimplement show_boot_progress () if needed
  43. */
  44. void inline __show_boot_progress (int val) {}
  45. void show_boot_progress (int val) __attribute__((weak, alias("__show_boot_progress")));
  46. #if defined(CONFIG_UPDATE_TFTP)
  47. void update_tftp (void);
  48. #endif /* CONFIG_UPDATE_TFTP */
  49. #define MAX_DELAY_STOP_STR 32
  50. #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
  51. static int abortboot(int);
  52. #endif
  53. #undef DEBUG_PARSER
  54. char console_buffer[CONFIG_SYS_CBSIZE + 1]; /* console I/O buffer */
  55. static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen);
  56. static const char erase_seq[] = "\b \b"; /* erase sequence */
  57. static const char tab_seq[] = " "; /* used to expand TABs */
  58. #ifdef CONFIG_BOOT_RETRY_TIME
  59. static uint64_t endtime = 0; /* must be set, default is instant timeout */
  60. static int retry_time = -1; /* -1 so can call readline before main_loop */
  61. #endif
  62. #define endtick(seconds) (get_ticks() + (uint64_t)(seconds) * get_tbclk())
  63. #ifndef CONFIG_BOOT_RETRY_MIN
  64. #define CONFIG_BOOT_RETRY_MIN CONFIG_BOOT_RETRY_TIME
  65. #endif
  66. #ifdef CONFIG_MODEM_SUPPORT
  67. int do_mdm_init = 0;
  68. extern void mdm_init(void); /* defined in board.c */
  69. #endif
  70. /***************************************************************************
  71. * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
  72. * returns: 0 - no key string, allow autoboot
  73. * 1 - got key string, abort
  74. */
  75. #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
  76. # if defined(CONFIG_AUTOBOOT_KEYED)
  77. static __inline__ int abortboot(int bootdelay)
  78. {
  79. int abort = 0;
  80. uint64_t etime = endtick(bootdelay);
  81. struct {
  82. char* str;
  83. u_int len;
  84. int retry;
  85. }
  86. delaykey [] = {
  87. { str: getenv ("bootdelaykey"), retry: 1 },
  88. { str: getenv ("bootdelaykey2"), retry: 1 },
  89. { str: getenv ("bootstopkey"), retry: 0 },
  90. { str: getenv ("bootstopkey2"), retry: 0 },
  91. };
  92. char presskey [MAX_DELAY_STOP_STR];
  93. u_int presskey_len = 0;
  94. u_int presskey_max = 0;
  95. u_int i;
  96. # ifdef CONFIG_AUTOBOOT_PROMPT
  97. printf(CONFIG_AUTOBOOT_PROMPT);
  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. do {
  132. if (tstc()) {
  133. if (presskey_len < presskey_max) {
  134. presskey [presskey_len ++] = getc();
  135. }
  136. else {
  137. for (i = 0; i < presskey_max - 1; i ++)
  138. presskey [i] = presskey [i + 1];
  139. presskey [i] = getc();
  140. }
  141. }
  142. for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
  143. if (delaykey[i].len > 0 &&
  144. presskey_len >= delaykey[i].len &&
  145. memcmp (presskey + presskey_len - delaykey[i].len,
  146. delaykey[i].str,
  147. delaykey[i].len) == 0) {
  148. # if DEBUG_BOOTKEYS
  149. printf("got %skey\n",
  150. delaykey[i].retry ? "delay" : "stop");
  151. # endif
  152. # ifdef CONFIG_BOOT_RETRY_TIME
  153. /* don't retry auto boot */
  154. if (! delaykey[i].retry)
  155. retry_time = -1;
  156. # endif
  157. abort = 1;
  158. }
  159. }
  160. } while (!abort && get_ticks() <= etime);
  161. # if DEBUG_BOOTKEYS
  162. if (!abort)
  163. puts("key timeout\n");
  164. # endif
  165. #ifdef CONFIG_SILENT_CONSOLE
  166. if (abort)
  167. gd->flags &= ~GD_FLG_SILENT;
  168. #endif
  169. return abort;
  170. }
  171. # else /* !defined(CONFIG_AUTOBOOT_KEYED) */
  172. #ifdef CONFIG_MENUKEY
  173. static int menukey = 0;
  174. #endif
  175. static __inline__ int abortboot(int bootdelay)
  176. {
  177. int abort = 0;
  178. #ifdef CONFIG_MENUPROMPT
  179. printf(CONFIG_MENUPROMPT);
  180. #else
  181. printf("Hit any key to stop autoboot: %2d ", bootdelay);
  182. #endif
  183. #if defined CONFIG_ZERO_BOOTDELAY_CHECK
  184. /*
  185. * Check if key already pressed
  186. * Don't check if bootdelay < 0
  187. */
  188. if (bootdelay >= 0) {
  189. if (tstc()) { /* we got a key press */
  190. (void) getc(); /* consume input */
  191. puts ("\b\b\b 0");
  192. abort = 1; /* don't auto boot */
  193. }
  194. }
  195. #endif
  196. while ((bootdelay > 0) && (!abort)) {
  197. int i;
  198. --bootdelay;
  199. /* delay 100 * 10ms */
  200. for (i=0; !abort && i<100; ++i) {
  201. if (tstc()) { /* we got a key press */
  202. abort = 1; /* don't auto boot */
  203. bootdelay = 0; /* no more delay */
  204. # ifdef CONFIG_MENUKEY
  205. menukey = getc();
  206. # else
  207. (void) getc(); /* consume input */
  208. # endif
  209. break;
  210. }
  211. udelay(10000);
  212. }
  213. printf("\b\b\b%2d ", bootdelay);
  214. }
  215. putc('\n');
  216. #ifdef CONFIG_SILENT_CONSOLE
  217. if (abort)
  218. gd->flags &= ~GD_FLG_SILENT;
  219. #endif
  220. return abort;
  221. }
  222. # endif /* CONFIG_AUTOBOOT_KEYED */
  223. #endif /* CONFIG_BOOTDELAY >= 0 */
  224. /****************************************************************************/
  225. void main_loop (void)
  226. {
  227. #ifndef CONFIG_SYS_HUSH_PARSER
  228. static char lastcommand[CONFIG_SYS_CBSIZE] = { 0, };
  229. int len;
  230. int rc = 1;
  231. int flag;
  232. #endif
  233. #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
  234. char *s;
  235. int bootdelay;
  236. #endif
  237. #ifdef CONFIG_PREBOOT
  238. char *p;
  239. #endif
  240. #ifdef CONFIG_BOOTCOUNT_LIMIT
  241. unsigned long bootcount = 0;
  242. unsigned long bootlimit = 0;
  243. char *bcs;
  244. char bcs_set[16];
  245. #endif /* CONFIG_BOOTCOUNT_LIMIT */
  246. #ifdef CONFIG_BOOTCOUNT_LIMIT
  247. bootcount = bootcount_load();
  248. bootcount++;
  249. bootcount_store (bootcount);
  250. sprintf (bcs_set, "%lu", bootcount);
  251. setenv ("bootcount", bcs_set);
  252. bcs = getenv ("bootlimit");
  253. bootlimit = bcs ? simple_strtoul (bcs, NULL, 10) : 0;
  254. #endif /* CONFIG_BOOTCOUNT_LIMIT */
  255. #ifdef CONFIG_MODEM_SUPPORT
  256. debug ("DEBUG: main_loop: do_mdm_init=%d\n", do_mdm_init);
  257. if (do_mdm_init) {
  258. char *str = strdup(getenv("mdm_cmd"));
  259. setenv ("preboot", str); /* set or delete definition */
  260. if (str != NULL)
  261. free (str);
  262. mdm_init(); /* wait for modem connection */
  263. }
  264. #endif /* CONFIG_MODEM_SUPPORT */
  265. #ifdef CONFIG_VERSION_VARIABLE
  266. {
  267. extern char version_string[];
  268. setenv ("ver", version_string); /* set version variable */
  269. }
  270. #endif /* CONFIG_VERSION_VARIABLE */
  271. #ifdef CONFIG_SYS_HUSH_PARSER
  272. u_boot_hush_start ();
  273. #endif
  274. #if defined(CONFIG_HUSH_INIT_VAR)
  275. hush_init_var ();
  276. #endif
  277. #ifdef CONFIG_PREBOOT
  278. if ((p = getenv ("preboot")) != NULL) {
  279. # ifdef CONFIG_AUTOBOOT_KEYED
  280. int prev = disable_ctrlc(1); /* disable Control C checking */
  281. # endif
  282. # ifndef CONFIG_SYS_HUSH_PARSER
  283. run_command (p, 0);
  284. # else
  285. parse_string_outer(p, FLAG_PARSE_SEMICOLON |
  286. FLAG_EXIT_FROM_LOOP);
  287. # endif
  288. # ifdef CONFIG_AUTOBOOT_KEYED
  289. disable_ctrlc(prev); /* restore Control C checking */
  290. # endif
  291. }
  292. #endif /* CONFIG_PREBOOT */
  293. #if defined(CONFIG_UPDATE_TFTP)
  294. update_tftp ();
  295. #endif /* CONFIG_UPDATE_TFTP */
  296. #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
  297. s = getenv ("bootdelay");
  298. bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
  299. debug ("### main_loop entered: bootdelay=%d\n\n", bootdelay);
  300. # ifdef CONFIG_BOOT_RETRY_TIME
  301. init_cmd_timeout ();
  302. # endif /* CONFIG_BOOT_RETRY_TIME */
  303. #ifdef CONFIG_POST
  304. if (gd->flags & GD_FLG_POSTFAIL) {
  305. s = getenv("failbootcmd");
  306. }
  307. else
  308. #endif /* CONFIG_POST */
  309. #ifdef CONFIG_BOOTCOUNT_LIMIT
  310. if (bootlimit && (bootcount > bootlimit)) {
  311. printf ("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",
  312. (unsigned)bootlimit);
  313. s = getenv ("altbootcmd");
  314. }
  315. else
  316. #endif /* CONFIG_BOOTCOUNT_LIMIT */
  317. s = getenv ("bootcmd");
  318. debug ("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
  319. if (bootdelay >= 0 && s && !abortboot (bootdelay)) {
  320. # ifdef CONFIG_AUTOBOOT_KEYED
  321. int prev = disable_ctrlc(1); /* disable Control C checking */
  322. # endif
  323. # ifndef CONFIG_SYS_HUSH_PARSER
  324. run_command (s, 0);
  325. # else
  326. parse_string_outer(s, FLAG_PARSE_SEMICOLON |
  327. FLAG_EXIT_FROM_LOOP);
  328. # endif
  329. # ifdef CONFIG_AUTOBOOT_KEYED
  330. disable_ctrlc(prev); /* restore Control C checking */
  331. # endif
  332. }
  333. # ifdef CONFIG_MENUKEY
  334. if (menukey == CONFIG_MENUKEY) {
  335. s = getenv("menucmd");
  336. if (s) {
  337. # ifndef CONFIG_SYS_HUSH_PARSER
  338. run_command (s, 0);
  339. # else
  340. parse_string_outer(s, FLAG_PARSE_SEMICOLON |
  341. FLAG_EXIT_FROM_LOOP);
  342. # endif
  343. }
  344. }
  345. #endif /* CONFIG_MENUKEY */
  346. #endif /* CONFIG_BOOTDELAY */
  347. /*
  348. * Main Loop for Monitor Command Processing
  349. */
  350. #ifdef CONFIG_SYS_HUSH_PARSER
  351. parse_file_outer();
  352. /* This point is never reached */
  353. for (;;);
  354. #else
  355. for (;;) {
  356. #ifdef CONFIG_BOOT_RETRY_TIME
  357. if (rc >= 0) {
  358. /* Saw enough of a valid command to
  359. * restart the timeout.
  360. */
  361. reset_cmd_timeout();
  362. }
  363. #endif
  364. len = readline (CONFIG_SYS_PROMPT);
  365. flag = 0; /* assume no special flags for now */
  366. if (len > 0)
  367. strcpy (lastcommand, console_buffer);
  368. else if (len == 0)
  369. flag |= CMD_FLAG_REPEAT;
  370. #ifdef CONFIG_BOOT_RETRY_TIME
  371. else if (len == -2) {
  372. /* -2 means timed out, retry autoboot
  373. */
  374. puts ("\nTimed out waiting for command\n");
  375. # ifdef CONFIG_RESET_TO_RETRY
  376. /* Reinit board to run initialization code again */
  377. do_reset (NULL, 0, 0, NULL);
  378. # else
  379. return; /* retry autoboot */
  380. # endif
  381. }
  382. #endif
  383. if (len == -1)
  384. puts ("<INTERRUPT>\n");
  385. else
  386. rc = run_command (lastcommand, flag);
  387. if (rc <= 0) {
  388. /* invalid command or not repeatable, forget it */
  389. lastcommand[0] = 0;
  390. }
  391. }
  392. #endif /*CONFIG_SYS_HUSH_PARSER*/
  393. }
  394. #ifdef CONFIG_BOOT_RETRY_TIME
  395. /***************************************************************************
  396. * initialize command line timeout
  397. */
  398. void init_cmd_timeout(void)
  399. {
  400. char *s = getenv ("bootretry");
  401. if (s != NULL)
  402. retry_time = (int)simple_strtol(s, NULL, 10);
  403. else
  404. retry_time = CONFIG_BOOT_RETRY_TIME;
  405. if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN)
  406. retry_time = CONFIG_BOOT_RETRY_MIN;
  407. }
  408. /***************************************************************************
  409. * reset command line timeout to retry_time seconds
  410. */
  411. void reset_cmd_timeout(void)
  412. {
  413. endtime = endtick(retry_time);
  414. }
  415. #endif
  416. #ifdef CONFIG_CMDLINE_EDITING
  417. /*
  418. * cmdline-editing related codes from vivi.
  419. * Author: Janghoon Lyu <nandy@mizi.com>
  420. */
  421. #define putnstr(str,n) do { \
  422. printf ("%.*s", (int)n, str); \
  423. } while (0)
  424. #define CTL_CH(c) ((c) - 'a' + 1)
  425. #define CTL_BACKSPACE ('\b')
  426. #define DEL ((char)255)
  427. #define DEL7 ((char)127)
  428. #define CREAD_HIST_CHAR ('!')
  429. #define getcmd_putch(ch) putc(ch)
  430. #define getcmd_getch() getc()
  431. #define getcmd_cbeep() getcmd_putch('\a')
  432. #define HIST_MAX 20
  433. #define HIST_SIZE CONFIG_SYS_CBSIZE
  434. static int hist_max = 0;
  435. static int hist_add_idx = 0;
  436. static int hist_cur = -1;
  437. unsigned hist_num = 0;
  438. char* hist_list[HIST_MAX];
  439. char hist_lines[HIST_MAX][HIST_SIZE + 1]; /* Save room for NULL */
  440. #define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
  441. static void hist_init(void)
  442. {
  443. int i;
  444. hist_max = 0;
  445. hist_add_idx = 0;
  446. hist_cur = -1;
  447. hist_num = 0;
  448. for (i = 0; i < HIST_MAX; i++) {
  449. hist_list[i] = hist_lines[i];
  450. hist_list[i][0] = '\0';
  451. }
  452. }
  453. static void cread_add_to_hist(char *line)
  454. {
  455. strcpy(hist_list[hist_add_idx], line);
  456. if (++hist_add_idx >= HIST_MAX)
  457. hist_add_idx = 0;
  458. if (hist_add_idx > hist_max)
  459. hist_max = hist_add_idx;
  460. hist_num++;
  461. }
  462. static char* hist_prev(void)
  463. {
  464. char *ret;
  465. int old_cur;
  466. if (hist_cur < 0)
  467. return NULL;
  468. old_cur = hist_cur;
  469. if (--hist_cur < 0)
  470. hist_cur = hist_max;
  471. if (hist_cur == hist_add_idx) {
  472. hist_cur = old_cur;
  473. ret = NULL;
  474. } else
  475. ret = hist_list[hist_cur];
  476. return (ret);
  477. }
  478. static char* hist_next(void)
  479. {
  480. char *ret;
  481. if (hist_cur < 0)
  482. return NULL;
  483. if (hist_cur == hist_add_idx)
  484. return NULL;
  485. if (++hist_cur > hist_max)
  486. hist_cur = 0;
  487. if (hist_cur == hist_add_idx) {
  488. ret = "";
  489. } else
  490. ret = hist_list[hist_cur];
  491. return (ret);
  492. }
  493. #ifndef CONFIG_CMDLINE_EDITING
  494. static void cread_print_hist_list(void)
  495. {
  496. int i;
  497. unsigned long n;
  498. n = hist_num - hist_max;
  499. i = hist_add_idx + 1;
  500. while (1) {
  501. if (i > hist_max)
  502. i = 0;
  503. if (i == hist_add_idx)
  504. break;
  505. printf("%s\n", hist_list[i]);
  506. n++;
  507. i++;
  508. }
  509. }
  510. #endif /* CONFIG_CMDLINE_EDITING */
  511. #define BEGINNING_OF_LINE() { \
  512. while (num) { \
  513. getcmd_putch(CTL_BACKSPACE); \
  514. num--; \
  515. } \
  516. }
  517. #define ERASE_TO_EOL() { \
  518. if (num < eol_num) { \
  519. printf("%*s", (int)(eol_num - num), ""); \
  520. do { \
  521. getcmd_putch(CTL_BACKSPACE); \
  522. } while (--eol_num > num); \
  523. } \
  524. }
  525. #define REFRESH_TO_EOL() { \
  526. if (num < eol_num) { \
  527. wlen = eol_num - num; \
  528. putnstr(buf + num, wlen); \
  529. num = eol_num; \
  530. } \
  531. }
  532. static void cread_add_char(char ichar, int insert, unsigned long *num,
  533. unsigned long *eol_num, char *buf, unsigned long len)
  534. {
  535. unsigned long wlen;
  536. /* room ??? */
  537. if (insert || *num == *eol_num) {
  538. if (*eol_num > len - 1) {
  539. getcmd_cbeep();
  540. return;
  541. }
  542. (*eol_num)++;
  543. }
  544. if (insert) {
  545. wlen = *eol_num - *num;
  546. if (wlen > 1) {
  547. memmove(&buf[*num+1], &buf[*num], wlen-1);
  548. }
  549. buf[*num] = ichar;
  550. putnstr(buf + *num, wlen);
  551. (*num)++;
  552. while (--wlen) {
  553. getcmd_putch(CTL_BACKSPACE);
  554. }
  555. } else {
  556. /* echo the character */
  557. wlen = 1;
  558. buf[*num] = ichar;
  559. putnstr(buf + *num, wlen);
  560. (*num)++;
  561. }
  562. }
  563. static void cread_add_str(char *str, int strsize, int insert, unsigned long *num,
  564. unsigned long *eol_num, char *buf, unsigned long len)
  565. {
  566. while (strsize--) {
  567. cread_add_char(*str, insert, num, eol_num, buf, len);
  568. str++;
  569. }
  570. }
  571. static int cread_line(const char *const prompt, char *buf, unsigned int *len)
  572. {
  573. unsigned long num = 0;
  574. unsigned long eol_num = 0;
  575. unsigned long wlen;
  576. char ichar;
  577. int insert = 1;
  578. int esc_len = 0;
  579. char esc_save[8];
  580. int init_len = strlen(buf);
  581. if (init_len)
  582. cread_add_str(buf, init_len, 1, &num, &eol_num, buf, *len);
  583. while (1) {
  584. #ifdef CONFIG_BOOT_RETRY_TIME
  585. while (!tstc()) { /* while no incoming data */
  586. if (retry_time >= 0 && get_ticks() > endtime)
  587. return (-2); /* timed out */
  588. WATCHDOG_RESET();
  589. }
  590. #endif
  591. ichar = getcmd_getch();
  592. if ((ichar == '\n') || (ichar == '\r')) {
  593. putc('\n');
  594. break;
  595. }
  596. /*
  597. * handle standard linux xterm esc sequences for arrow key, etc.
  598. */
  599. if (esc_len != 0) {
  600. if (esc_len == 1) {
  601. if (ichar == '[') {
  602. esc_save[esc_len] = ichar;
  603. esc_len = 2;
  604. } else {
  605. cread_add_str(esc_save, esc_len, insert,
  606. &num, &eol_num, buf, *len);
  607. esc_len = 0;
  608. }
  609. continue;
  610. }
  611. switch (ichar) {
  612. case 'D': /* <- key */
  613. ichar = CTL_CH('b');
  614. esc_len = 0;
  615. break;
  616. case 'C': /* -> key */
  617. ichar = CTL_CH('f');
  618. esc_len = 0;
  619. break; /* pass off to ^F handler */
  620. case 'H': /* Home key */
  621. ichar = CTL_CH('a');
  622. esc_len = 0;
  623. break; /* pass off to ^A handler */
  624. case 'A': /* up arrow */
  625. ichar = CTL_CH('p');
  626. esc_len = 0;
  627. break; /* pass off to ^P handler */
  628. case 'B': /* down arrow */
  629. ichar = CTL_CH('n');
  630. esc_len = 0;
  631. break; /* pass off to ^N handler */
  632. default:
  633. esc_save[esc_len++] = ichar;
  634. cread_add_str(esc_save, esc_len, insert,
  635. &num, &eol_num, buf, *len);
  636. esc_len = 0;
  637. continue;
  638. }
  639. }
  640. switch (ichar) {
  641. case 0x1b:
  642. if (esc_len == 0) {
  643. esc_save[esc_len] = ichar;
  644. esc_len = 1;
  645. } else {
  646. puts("impossible condition #876\n");
  647. esc_len = 0;
  648. }
  649. break;
  650. case CTL_CH('a'):
  651. BEGINNING_OF_LINE();
  652. break;
  653. case CTL_CH('c'): /* ^C - break */
  654. *buf = '\0'; /* discard input */
  655. return (-1);
  656. case CTL_CH('f'):
  657. if (num < eol_num) {
  658. getcmd_putch(buf[num]);
  659. num++;
  660. }
  661. break;
  662. case CTL_CH('b'):
  663. if (num) {
  664. getcmd_putch(CTL_BACKSPACE);
  665. num--;
  666. }
  667. break;
  668. case CTL_CH('d'):
  669. if (num < eol_num) {
  670. wlen = eol_num - num - 1;
  671. if (wlen) {
  672. memmove(&buf[num], &buf[num+1], wlen);
  673. putnstr(buf + num, wlen);
  674. }
  675. getcmd_putch(' ');
  676. do {
  677. getcmd_putch(CTL_BACKSPACE);
  678. } while (wlen--);
  679. eol_num--;
  680. }
  681. break;
  682. case CTL_CH('k'):
  683. ERASE_TO_EOL();
  684. break;
  685. case CTL_CH('e'):
  686. REFRESH_TO_EOL();
  687. break;
  688. case CTL_CH('o'):
  689. insert = !insert;
  690. break;
  691. case CTL_CH('x'):
  692. case CTL_CH('u'):
  693. BEGINNING_OF_LINE();
  694. ERASE_TO_EOL();
  695. break;
  696. case DEL:
  697. case DEL7:
  698. case 8:
  699. if (num) {
  700. wlen = eol_num - num;
  701. num--;
  702. memmove(&buf[num], &buf[num+1], wlen);
  703. getcmd_putch(CTL_BACKSPACE);
  704. putnstr(buf + num, wlen);
  705. getcmd_putch(' ');
  706. do {
  707. getcmd_putch(CTL_BACKSPACE);
  708. } while (wlen--);
  709. eol_num--;
  710. }
  711. break;
  712. case CTL_CH('p'):
  713. case CTL_CH('n'):
  714. {
  715. char * hline;
  716. esc_len = 0;
  717. if (ichar == CTL_CH('p'))
  718. hline = hist_prev();
  719. else
  720. hline = hist_next();
  721. if (!hline) {
  722. getcmd_cbeep();
  723. continue;
  724. }
  725. /* nuke the current line */
  726. /* first, go home */
  727. BEGINNING_OF_LINE();
  728. /* erase to end of line */
  729. ERASE_TO_EOL();
  730. /* copy new line into place and display */
  731. strcpy(buf, hline);
  732. eol_num = strlen(buf);
  733. REFRESH_TO_EOL();
  734. continue;
  735. }
  736. #ifdef CONFIG_AUTO_COMPLETE
  737. case '\t': {
  738. int num2, col;
  739. /* do not autocomplete when in the middle */
  740. if (num < eol_num) {
  741. getcmd_cbeep();
  742. break;
  743. }
  744. buf[num] = '\0';
  745. col = strlen(prompt) + eol_num;
  746. num2 = num;
  747. if (cmd_auto_complete(prompt, buf, &num2, &col)) {
  748. col = num2 - num;
  749. num += col;
  750. eol_num += col;
  751. }
  752. break;
  753. }
  754. #endif
  755. default:
  756. cread_add_char(ichar, insert, &num, &eol_num, buf, *len);
  757. break;
  758. }
  759. }
  760. *len = eol_num;
  761. buf[eol_num] = '\0'; /* lose the newline */
  762. if (buf[0] && buf[0] != CREAD_HIST_CHAR)
  763. cread_add_to_hist(buf);
  764. hist_cur = hist_add_idx;
  765. return 0;
  766. }
  767. #endif /* CONFIG_CMDLINE_EDITING */
  768. /****************************************************************************/
  769. /*
  770. * Prompt for input and read a line.
  771. * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
  772. * time out when time goes past endtime (timebase time in ticks).
  773. * Return: number of read characters
  774. * -1 if break
  775. * -2 if timed out
  776. */
  777. int readline (const char *const prompt)
  778. {
  779. /*
  780. * If console_buffer isn't 0-length the user will be prompted to modify
  781. * it instead of entering it from scratch as desired.
  782. */
  783. console_buffer[0] = '\0';
  784. return readline_into_buffer(prompt, console_buffer);
  785. }
  786. int readline_into_buffer (const char *const prompt, char * buffer)
  787. {
  788. char *p = buffer;
  789. #ifdef CONFIG_CMDLINE_EDITING
  790. unsigned int len = CONFIG_SYS_CBSIZE;
  791. int rc;
  792. static int initted = 0;
  793. /*
  794. * History uses a global array which is not
  795. * writable until after relocation to RAM.
  796. * Revert to non-history version if still
  797. * running from flash.
  798. */
  799. if (gd->flags & GD_FLG_RELOC) {
  800. if (!initted) {
  801. hist_init();
  802. initted = 1;
  803. }
  804. if (prompt)
  805. puts (prompt);
  806. rc = cread_line(prompt, p, &len);
  807. return rc < 0 ? rc : len;
  808. } else {
  809. #endif /* CONFIG_CMDLINE_EDITING */
  810. char * p_buf = p;
  811. int n = 0; /* buffer index */
  812. int plen = 0; /* prompt length */
  813. int col; /* output column cnt */
  814. char c;
  815. /* print prompt */
  816. if (prompt) {
  817. plen = strlen (prompt);
  818. puts (prompt);
  819. }
  820. col = plen;
  821. for (;;) {
  822. #ifdef CONFIG_BOOT_RETRY_TIME
  823. while (!tstc()) { /* while no incoming data */
  824. if (retry_time >= 0 && get_ticks() > endtime)
  825. return (-2); /* timed out */
  826. WATCHDOG_RESET();
  827. }
  828. #endif
  829. WATCHDOG_RESET(); /* Trigger watchdog, if needed */
  830. #ifdef CONFIG_SHOW_ACTIVITY
  831. while (!tstc()) {
  832. extern void show_activity(int arg);
  833. show_activity(0);
  834. WATCHDOG_RESET();
  835. }
  836. #endif
  837. c = getc();
  838. /*
  839. * Special character handling
  840. */
  841. switch (c) {
  842. case '\r': /* Enter */
  843. case '\n':
  844. *p = '\0';
  845. puts ("\r\n");
  846. return (p - p_buf);
  847. case '\0': /* nul */
  848. continue;
  849. case 0x03: /* ^C - break */
  850. p_buf[0] = '\0'; /* discard input */
  851. return (-1);
  852. case 0x15: /* ^U - erase line */
  853. while (col > plen) {
  854. puts (erase_seq);
  855. --col;
  856. }
  857. p = p_buf;
  858. n = 0;
  859. continue;
  860. case 0x17: /* ^W - erase word */
  861. p=delete_char(p_buf, p, &col, &n, plen);
  862. while ((n > 0) && (*p != ' ')) {
  863. p=delete_char(p_buf, p, &col, &n, plen);
  864. }
  865. continue;
  866. case 0x08: /* ^H - backspace */
  867. case 0x7F: /* DEL - backspace */
  868. p=delete_char(p_buf, p, &col, &n, plen);
  869. continue;
  870. default:
  871. /*
  872. * Must be a normal character then
  873. */
  874. if (n < CONFIG_SYS_CBSIZE-2) {
  875. if (c == '\t') { /* expand TABs */
  876. #ifdef CONFIG_AUTO_COMPLETE
  877. /* if auto completion triggered just continue */
  878. *p = '\0';
  879. if (cmd_auto_complete(prompt, console_buffer, &n, &col)) {
  880. p = p_buf + n; /* reset */
  881. continue;
  882. }
  883. #endif
  884. puts (tab_seq+(col&07));
  885. col += 8 - (col&07);
  886. } else {
  887. ++col; /* echo input */
  888. putc (c);
  889. }
  890. *p++ = c;
  891. ++n;
  892. } else { /* Buffer full */
  893. putc ('\a');
  894. }
  895. }
  896. }
  897. #ifdef CONFIG_CMDLINE_EDITING
  898. }
  899. #endif
  900. }
  901. /****************************************************************************/
  902. static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen)
  903. {
  904. char *s;
  905. if (*np == 0) {
  906. return (p);
  907. }
  908. if (*(--p) == '\t') { /* will retype the whole line */
  909. while (*colp > plen) {
  910. puts (erase_seq);
  911. (*colp)--;
  912. }
  913. for (s=buffer; s<p; ++s) {
  914. if (*s == '\t') {
  915. puts (tab_seq+((*colp) & 07));
  916. *colp += 8 - ((*colp) & 07);
  917. } else {
  918. ++(*colp);
  919. putc (*s);
  920. }
  921. }
  922. } else {
  923. puts (erase_seq);
  924. (*colp)--;
  925. }
  926. (*np)--;
  927. return (p);
  928. }
  929. /****************************************************************************/
  930. int parse_line (char *line, char *argv[])
  931. {
  932. int nargs = 0;
  933. #ifdef DEBUG_PARSER
  934. printf ("parse_line: \"%s\"\n", line);
  935. #endif
  936. while (nargs < CONFIG_SYS_MAXARGS) {
  937. /* skip any white space */
  938. while ((*line == ' ') || (*line == '\t')) {
  939. ++line;
  940. }
  941. if (*line == '\0') { /* end of line, no more args */
  942. argv[nargs] = NULL;
  943. #ifdef DEBUG_PARSER
  944. printf ("parse_line: nargs=%d\n", nargs);
  945. #endif
  946. return (nargs);
  947. }
  948. argv[nargs++] = line; /* begin of argument string */
  949. /* find end of string */
  950. while (*line && (*line != ' ') && (*line != '\t')) {
  951. ++line;
  952. }
  953. if (*line == '\0') { /* end of line, no more args */
  954. argv[nargs] = NULL;
  955. #ifdef DEBUG_PARSER
  956. printf ("parse_line: nargs=%d\n", nargs);
  957. #endif
  958. return (nargs);
  959. }
  960. *line++ = '\0'; /* terminate current arg */
  961. }
  962. printf ("** Too many args (max. %d) **\n", CONFIG_SYS_MAXARGS);
  963. #ifdef DEBUG_PARSER
  964. printf ("parse_line: nargs=%d\n", nargs);
  965. #endif
  966. return (nargs);
  967. }
  968. /****************************************************************************/
  969. static void process_macros (const char *input, char *output)
  970. {
  971. char c, prev;
  972. const char *varname_start = NULL;
  973. int inputcnt = strlen (input);
  974. int outputcnt = CONFIG_SYS_CBSIZE;
  975. int state = 0; /* 0 = waiting for '$' */
  976. /* 1 = waiting for '(' or '{' */
  977. /* 2 = waiting for ')' or '}' */
  978. /* 3 = waiting for ''' */
  979. #ifdef DEBUG_PARSER
  980. char *output_start = output;
  981. printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen (input),
  982. input);
  983. #endif
  984. prev = '\0'; /* previous character */
  985. while (inputcnt && outputcnt) {
  986. c = *input++;
  987. inputcnt--;
  988. if (state != 3) {
  989. /* remove one level of escape characters */
  990. if ((c == '\\') && (prev != '\\')) {
  991. if (inputcnt-- == 0)
  992. break;
  993. prev = c;
  994. c = *input++;
  995. }
  996. }
  997. switch (state) {
  998. case 0: /* Waiting for (unescaped) $ */
  999. if ((c == '\'') && (prev != '\\')) {
  1000. state = 3;
  1001. break;
  1002. }
  1003. if ((c == '$') && (prev != '\\')) {
  1004. state++;
  1005. } else {
  1006. *(output++) = c;
  1007. outputcnt--;
  1008. }
  1009. break;
  1010. case 1: /* Waiting for ( */
  1011. if (c == '(' || c == '{') {
  1012. state++;
  1013. varname_start = input;
  1014. } else {
  1015. state = 0;
  1016. *(output++) = '$';
  1017. outputcnt--;
  1018. if (outputcnt) {
  1019. *(output++) = c;
  1020. outputcnt--;
  1021. }
  1022. }
  1023. break;
  1024. case 2: /* Waiting for ) */
  1025. if (c == ')' || c == '}') {
  1026. int i;
  1027. char envname[CONFIG_SYS_CBSIZE], *envval;
  1028. int envcnt = input - varname_start - 1; /* Varname # of chars */
  1029. /* Get the varname */
  1030. for (i = 0; i < envcnt; i++) {
  1031. envname[i] = varname_start[i];
  1032. }
  1033. envname[i] = 0;
  1034. /* Get its value */
  1035. envval = getenv (envname);
  1036. /* Copy into the line if it exists */
  1037. if (envval != NULL)
  1038. while ((*envval) && outputcnt) {
  1039. *(output++) = *(envval++);
  1040. outputcnt--;
  1041. }
  1042. /* Look for another '$' */
  1043. state = 0;
  1044. }
  1045. break;
  1046. case 3: /* Waiting for ' */
  1047. if ((c == '\'') && (prev != '\\')) {
  1048. state = 0;
  1049. } else {
  1050. *(output++) = c;
  1051. outputcnt--;
  1052. }
  1053. break;
  1054. }
  1055. prev = c;
  1056. }
  1057. if (outputcnt)
  1058. *output = 0;
  1059. else
  1060. *(output - 1) = 0;
  1061. #ifdef DEBUG_PARSER
  1062. printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
  1063. strlen (output_start), output_start);
  1064. #endif
  1065. }
  1066. /****************************************************************************
  1067. * returns:
  1068. * 1 - command executed, repeatable
  1069. * 0 - command executed but not repeatable, interrupted commands are
  1070. * always considered not repeatable
  1071. * -1 - not executed (unrecognized, bootd recursion or too many args)
  1072. * (If cmd is NULL or "" or longer than CONFIG_SYS_CBSIZE-1 it is
  1073. * considered unrecognized)
  1074. *
  1075. * WARNING:
  1076. *
  1077. * We must create a temporary copy of the command since the command we get
  1078. * may be the result from getenv(), which returns a pointer directly to
  1079. * the environment data, which may change magicly when the command we run
  1080. * creates or modifies environment variables (like "bootp" does).
  1081. */
  1082. int run_command (const char *cmd, int flag)
  1083. {
  1084. cmd_tbl_t *cmdtp;
  1085. char cmdbuf[CONFIG_SYS_CBSIZE]; /* working copy of cmd */
  1086. char *token; /* start of token in cmdbuf */
  1087. char *sep; /* end of token (separator) in cmdbuf */
  1088. char finaltoken[CONFIG_SYS_CBSIZE];
  1089. char *str = cmdbuf;
  1090. char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */
  1091. int argc, inquotes;
  1092. int repeatable = 1;
  1093. int rc = 0;
  1094. #ifdef DEBUG_PARSER
  1095. printf ("[RUN_COMMAND] cmd[%p]=\"", cmd);
  1096. puts (cmd ? cmd : "NULL"); /* use puts - string may be loooong */
  1097. puts ("\"\n");
  1098. #endif
  1099. clear_ctrlc(); /* forget any previous Control C */
  1100. if (!cmd || !*cmd) {
  1101. return -1; /* empty command */
  1102. }
  1103. if (strlen(cmd) >= CONFIG_SYS_CBSIZE) {
  1104. puts ("## Command too long!\n");
  1105. return -1;
  1106. }
  1107. strcpy (cmdbuf, cmd);
  1108. /* Process separators and check for invalid
  1109. * repeatable commands
  1110. */
  1111. #ifdef DEBUG_PARSER
  1112. printf ("[PROCESS_SEPARATORS] %s\n", cmd);
  1113. #endif
  1114. while (*str) {
  1115. /*
  1116. * Find separator, or string end
  1117. * Allow simple escape of ';' by writing "\;"
  1118. */
  1119. for (inquotes = 0, sep = str; *sep; sep++) {
  1120. if ((*sep=='\'') &&
  1121. (*(sep-1) != '\\'))
  1122. inquotes=!inquotes;
  1123. if (!inquotes &&
  1124. (*sep == ';') && /* separator */
  1125. ( sep != str) && /* past string start */
  1126. (*(sep-1) != '\\')) /* and NOT escaped */
  1127. break;
  1128. }
  1129. /*
  1130. * Limit the token to data between separators
  1131. */
  1132. token = str;
  1133. if (*sep) {
  1134. str = sep + 1; /* start of command for next pass */
  1135. *sep = '\0';
  1136. }
  1137. else
  1138. str = sep; /* no more commands for next pass */
  1139. #ifdef DEBUG_PARSER
  1140. printf ("token: \"%s\"\n", token);
  1141. #endif
  1142. /* find macros in this token and replace them */
  1143. process_macros (token, finaltoken);
  1144. /* Extract arguments */
  1145. if ((argc = parse_line (finaltoken, argv)) == 0) {
  1146. rc = -1; /* no command at all */
  1147. continue;
  1148. }
  1149. /* Look up command in command table */
  1150. if ((cmdtp = find_cmd(argv[0])) == NULL) {
  1151. printf ("Unknown command '%s' - try 'help'\n", argv[0]);
  1152. rc = -1; /* give up after bad command */
  1153. continue;
  1154. }
  1155. /* found - check max args */
  1156. if (argc > cmdtp->maxargs) {
  1157. cmd_usage(cmdtp);
  1158. rc = -1;
  1159. continue;
  1160. }
  1161. #if defined(CONFIG_CMD_BOOTD)
  1162. /* avoid "bootd" recursion */
  1163. if (cmdtp->cmd == do_bootd) {
  1164. #ifdef DEBUG_PARSER
  1165. printf ("[%s]\n", finaltoken);
  1166. #endif
  1167. if (flag & CMD_FLAG_BOOTD) {
  1168. puts ("'bootd' recursion detected\n");
  1169. rc = -1;
  1170. continue;
  1171. } else {
  1172. flag |= CMD_FLAG_BOOTD;
  1173. }
  1174. }
  1175. #endif
  1176. /* OK - call function to do the command */
  1177. if ((cmdtp->cmd) (cmdtp, flag, argc, argv) != 0) {
  1178. rc = -1;
  1179. }
  1180. repeatable &= cmdtp->repeatable;
  1181. /* Did the user stop this? */
  1182. if (had_ctrlc ())
  1183. return -1; /* if stopped then not repeatable */
  1184. }
  1185. return rc ? rc : repeatable;
  1186. }
  1187. /****************************************************************************/
  1188. #if defined(CONFIG_CMD_RUN)
  1189. int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
  1190. {
  1191. int i;
  1192. if (argc < 2)
  1193. return cmd_usage(cmdtp);
  1194. for (i=1; i<argc; ++i) {
  1195. char *arg;
  1196. if ((arg = getenv (argv[i])) == NULL) {
  1197. printf ("## Error: \"%s\" not defined\n", argv[i]);
  1198. return 1;
  1199. }
  1200. #ifndef CONFIG_SYS_HUSH_PARSER
  1201. if (run_command (arg, flag) == -1)
  1202. return 1;
  1203. #else
  1204. if (parse_string_outer(arg,
  1205. FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0)
  1206. return 1;
  1207. #endif
  1208. }
  1209. return 0;
  1210. }
  1211. #endif