main.c 30 KB

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