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. #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. {
  580. unsigned long num = 0;
  581. unsigned long eol_num = 0;
  582. unsigned long wlen;
  583. char ichar;
  584. int insert = 1;
  585. int esc_len = 0;
  586. char esc_save[8];
  587. int init_len = strlen(buf);
  588. if (init_len)
  589. cread_add_str(buf, init_len, 1, &num, &eol_num, buf, *len);
  590. while (1) {
  591. #ifdef CONFIG_BOOT_RETRY_TIME
  592. while (!tstc()) { /* while no incoming data */
  593. if (retry_time >= 0 && get_ticks() > endtime)
  594. return (-2); /* timed out */
  595. WATCHDOG_RESET();
  596. }
  597. #endif
  598. ichar = getcmd_getch();
  599. if ((ichar == '\n') || (ichar == '\r')) {
  600. putc('\n');
  601. break;
  602. }
  603. /*
  604. * handle standard linux xterm esc sequences for arrow key, etc.
  605. */
  606. if (esc_len != 0) {
  607. if (esc_len == 1) {
  608. if (ichar == '[') {
  609. esc_save[esc_len] = ichar;
  610. esc_len = 2;
  611. } else {
  612. cread_add_str(esc_save, esc_len, insert,
  613. &num, &eol_num, buf, *len);
  614. esc_len = 0;
  615. }
  616. continue;
  617. }
  618. switch (ichar) {
  619. case 'D': /* <- key */
  620. ichar = CTL_CH('b');
  621. esc_len = 0;
  622. break;
  623. case 'C': /* -> key */
  624. ichar = CTL_CH('f');
  625. esc_len = 0;
  626. break; /* pass off to ^F handler */
  627. case 'H': /* Home key */
  628. ichar = CTL_CH('a');
  629. esc_len = 0;
  630. break; /* pass off to ^A handler */
  631. case 'A': /* up arrow */
  632. ichar = CTL_CH('p');
  633. esc_len = 0;
  634. break; /* pass off to ^P handler */
  635. case 'B': /* down arrow */
  636. ichar = CTL_CH('n');
  637. esc_len = 0;
  638. break; /* pass off to ^N handler */
  639. default:
  640. esc_save[esc_len++] = ichar;
  641. cread_add_str(esc_save, esc_len, insert,
  642. &num, &eol_num, buf, *len);
  643. esc_len = 0;
  644. continue;
  645. }
  646. }
  647. switch (ichar) {
  648. case 0x1b:
  649. if (esc_len == 0) {
  650. esc_save[esc_len] = ichar;
  651. esc_len = 1;
  652. } else {
  653. puts("impossible condition #876\n");
  654. esc_len = 0;
  655. }
  656. break;
  657. case CTL_CH('a'):
  658. BEGINNING_OF_LINE();
  659. break;
  660. case CTL_CH('c'): /* ^C - break */
  661. *buf = '\0'; /* discard input */
  662. return (-1);
  663. case CTL_CH('f'):
  664. if (num < eol_num) {
  665. getcmd_putch(buf[num]);
  666. num++;
  667. }
  668. break;
  669. case CTL_CH('b'):
  670. if (num) {
  671. getcmd_putch(CTL_BACKSPACE);
  672. num--;
  673. }
  674. break;
  675. case CTL_CH('d'):
  676. if (num < eol_num) {
  677. wlen = eol_num - num - 1;
  678. if (wlen) {
  679. memmove(&buf[num], &buf[num+1], wlen);
  680. putnstr(buf + num, wlen);
  681. }
  682. getcmd_putch(' ');
  683. do {
  684. getcmd_putch(CTL_BACKSPACE);
  685. } while (wlen--);
  686. eol_num--;
  687. }
  688. break;
  689. case CTL_CH('k'):
  690. ERASE_TO_EOL();
  691. break;
  692. case CTL_CH('e'):
  693. REFRESH_TO_EOL();
  694. break;
  695. case CTL_CH('o'):
  696. insert = !insert;
  697. break;
  698. case CTL_CH('x'):
  699. case CTL_CH('u'):
  700. BEGINNING_OF_LINE();
  701. ERASE_TO_EOL();
  702. break;
  703. case DEL:
  704. case DEL7:
  705. case 8:
  706. if (num) {
  707. wlen = eol_num - num;
  708. num--;
  709. memmove(&buf[num], &buf[num+1], wlen);
  710. getcmd_putch(CTL_BACKSPACE);
  711. putnstr(buf + num, wlen);
  712. getcmd_putch(' ');
  713. do {
  714. getcmd_putch(CTL_BACKSPACE);
  715. } while (wlen--);
  716. eol_num--;
  717. }
  718. break;
  719. case CTL_CH('p'):
  720. case CTL_CH('n'):
  721. {
  722. char * hline;
  723. esc_len = 0;
  724. if (ichar == CTL_CH('p'))
  725. hline = hist_prev();
  726. else
  727. hline = hist_next();
  728. if (!hline) {
  729. getcmd_cbeep();
  730. continue;
  731. }
  732. /* nuke the current line */
  733. /* first, go home */
  734. BEGINNING_OF_LINE();
  735. /* erase to end of line */
  736. ERASE_TO_EOL();
  737. /* copy new line into place and display */
  738. strcpy(buf, hline);
  739. eol_num = strlen(buf);
  740. REFRESH_TO_EOL();
  741. continue;
  742. }
  743. #ifdef CONFIG_AUTO_COMPLETE
  744. case '\t': {
  745. int num2, col;
  746. /* do not autocomplete when in the middle */
  747. if (num < eol_num) {
  748. getcmd_cbeep();
  749. break;
  750. }
  751. buf[num] = '\0';
  752. col = strlen(prompt) + eol_num;
  753. num2 = num;
  754. if (cmd_auto_complete(prompt, buf, &num2, &col)) {
  755. col = num2 - num;
  756. num += col;
  757. eol_num += col;
  758. }
  759. break;
  760. }
  761. #endif
  762. default:
  763. cread_add_char(ichar, insert, &num, &eol_num, buf, *len);
  764. break;
  765. }
  766. }
  767. *len = eol_num;
  768. buf[eol_num] = '\0'; /* lose the newline */
  769. if (buf[0] && buf[0] != CREAD_HIST_CHAR)
  770. cread_add_to_hist(buf);
  771. hist_cur = hist_add_idx;
  772. return 0;
  773. }
  774. #endif /* CONFIG_CMDLINE_EDITING */
  775. /****************************************************************************/
  776. /*
  777. * Prompt for input and read a line.
  778. * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
  779. * time out when time goes past endtime (timebase time in ticks).
  780. * Return: number of read characters
  781. * -1 if break
  782. * -2 if timed out
  783. */
  784. int readline (const char *const prompt)
  785. {
  786. /*
  787. * If console_buffer isn't 0-length the user will be prompted to modify
  788. * it instead of entering it from scratch as desired.
  789. */
  790. console_buffer[0] = '\0';
  791. return readline_into_buffer(prompt, console_buffer);
  792. }
  793. int readline_into_buffer (const char *const prompt, char * buffer)
  794. {
  795. char *p = buffer;
  796. #ifdef CONFIG_CMDLINE_EDITING
  797. unsigned int len = CONFIG_SYS_CBSIZE;
  798. int rc;
  799. static int initted = 0;
  800. /*
  801. * History uses a global array which is not
  802. * writable until after relocation to RAM.
  803. * Revert to non-history version if still
  804. * running from flash.
  805. */
  806. if (gd->flags & GD_FLG_RELOC) {
  807. if (!initted) {
  808. hist_init();
  809. initted = 1;
  810. }
  811. if (prompt)
  812. puts (prompt);
  813. rc = cread_line(prompt, p, &len);
  814. return rc < 0 ? rc : len;
  815. } else {
  816. #endif /* CONFIG_CMDLINE_EDITING */
  817. char * p_buf = p;
  818. int n = 0; /* buffer index */
  819. int plen = 0; /* prompt length */
  820. int col; /* output column cnt */
  821. char c;
  822. /* print prompt */
  823. if (prompt) {
  824. plen = strlen (prompt);
  825. puts (prompt);
  826. }
  827. col = plen;
  828. for (;;) {
  829. #ifdef CONFIG_BOOT_RETRY_TIME
  830. while (!tstc()) { /* while no incoming data */
  831. if (retry_time >= 0 && get_ticks() > endtime)
  832. return (-2); /* timed out */
  833. WATCHDOG_RESET();
  834. }
  835. #endif
  836. WATCHDOG_RESET(); /* Trigger watchdog, if needed */
  837. #ifdef CONFIG_SHOW_ACTIVITY
  838. while (!tstc()) {
  839. extern void show_activity(int arg);
  840. show_activity(0);
  841. WATCHDOG_RESET();
  842. }
  843. #endif
  844. c = getc();
  845. /*
  846. * Special character handling
  847. */
  848. switch (c) {
  849. case '\r': /* Enter */
  850. case '\n':
  851. *p = '\0';
  852. puts ("\r\n");
  853. return (p - p_buf);
  854. case '\0': /* nul */
  855. continue;
  856. case 0x03: /* ^C - break */
  857. p_buf[0] = '\0'; /* discard input */
  858. return (-1);
  859. case 0x15: /* ^U - erase line */
  860. while (col > plen) {
  861. puts (erase_seq);
  862. --col;
  863. }
  864. p = p_buf;
  865. n = 0;
  866. continue;
  867. case 0x17: /* ^W - erase word */
  868. p=delete_char(p_buf, p, &col, &n, plen);
  869. while ((n > 0) && (*p != ' ')) {
  870. p=delete_char(p_buf, p, &col, &n, plen);
  871. }
  872. continue;
  873. case 0x08: /* ^H - backspace */
  874. case 0x7F: /* DEL - backspace */
  875. p=delete_char(p_buf, p, &col, &n, plen);
  876. continue;
  877. default:
  878. /*
  879. * Must be a normal character then
  880. */
  881. if (n < CONFIG_SYS_CBSIZE-2) {
  882. if (c == '\t') { /* expand TABs */
  883. #ifdef CONFIG_AUTO_COMPLETE
  884. /* if auto completion triggered just continue */
  885. *p = '\0';
  886. if (cmd_auto_complete(prompt, console_buffer, &n, &col)) {
  887. p = p_buf + n; /* reset */
  888. continue;
  889. }
  890. #endif
  891. puts (tab_seq+(col&07));
  892. col += 8 - (col&07);
  893. } else {
  894. ++col; /* echo input */
  895. putc (c);
  896. }
  897. *p++ = c;
  898. ++n;
  899. } else { /* Buffer full */
  900. putc ('\a');
  901. }
  902. }
  903. }
  904. #ifdef CONFIG_CMDLINE_EDITING
  905. }
  906. #endif
  907. }
  908. /****************************************************************************/
  909. static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen)
  910. {
  911. char *s;
  912. if (*np == 0) {
  913. return (p);
  914. }
  915. if (*(--p) == '\t') { /* will retype the whole line */
  916. while (*colp > plen) {
  917. puts (erase_seq);
  918. (*colp)--;
  919. }
  920. for (s=buffer; s<p; ++s) {
  921. if (*s == '\t') {
  922. puts (tab_seq+((*colp) & 07));
  923. *colp += 8 - ((*colp) & 07);
  924. } else {
  925. ++(*colp);
  926. putc (*s);
  927. }
  928. }
  929. } else {
  930. puts (erase_seq);
  931. (*colp)--;
  932. }
  933. (*np)--;
  934. return (p);
  935. }
  936. /****************************************************************************/
  937. int parse_line (char *line, char *argv[])
  938. {
  939. int nargs = 0;
  940. #ifdef DEBUG_PARSER
  941. printf ("parse_line: \"%s\"\n", line);
  942. #endif
  943. while (nargs < CONFIG_SYS_MAXARGS) {
  944. /* skip any white space */
  945. while (isblank(*line))
  946. ++line;
  947. if (*line == '\0') { /* end of line, no more args */
  948. argv[nargs] = NULL;
  949. #ifdef DEBUG_PARSER
  950. printf ("parse_line: nargs=%d\n", nargs);
  951. #endif
  952. return (nargs);
  953. }
  954. argv[nargs++] = line; /* begin of argument string */
  955. /* find end of string */
  956. while (*line && !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. *line++ = '\0'; /* terminate current arg */
  966. }
  967. printf ("** Too many args (max. %d) **\n", CONFIG_SYS_MAXARGS);
  968. #ifdef DEBUG_PARSER
  969. printf ("parse_line: nargs=%d\n", nargs);
  970. #endif
  971. return (nargs);
  972. }
  973. /****************************************************************************/
  974. static void process_macros (const char *input, char *output)
  975. {
  976. char c, prev;
  977. const char *varname_start = NULL;
  978. int inputcnt = strlen (input);
  979. int outputcnt = CONFIG_SYS_CBSIZE;
  980. int state = 0; /* 0 = waiting for '$' */
  981. /* 1 = waiting for '(' or '{' */
  982. /* 2 = waiting for ')' or '}' */
  983. /* 3 = waiting for ''' */
  984. #ifdef DEBUG_PARSER
  985. char *output_start = output;
  986. printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen (input),
  987. input);
  988. #endif
  989. prev = '\0'; /* previous character */
  990. while (inputcnt && outputcnt) {
  991. c = *input++;
  992. inputcnt--;
  993. if (state != 3) {
  994. /* remove one level of escape characters */
  995. if ((c == '\\') && (prev != '\\')) {
  996. if (inputcnt-- == 0)
  997. break;
  998. prev = c;
  999. c = *input++;
  1000. }
  1001. }
  1002. switch (state) {
  1003. case 0: /* Waiting for (unescaped) $ */
  1004. if ((c == '\'') && (prev != '\\')) {
  1005. state = 3;
  1006. break;
  1007. }
  1008. if ((c == '$') && (prev != '\\')) {
  1009. state++;
  1010. } else {
  1011. *(output++) = c;
  1012. outputcnt--;
  1013. }
  1014. break;
  1015. case 1: /* Waiting for ( */
  1016. if (c == '(' || c == '{') {
  1017. state++;
  1018. varname_start = input;
  1019. } else {
  1020. state = 0;
  1021. *(output++) = '$';
  1022. outputcnt--;
  1023. if (outputcnt) {
  1024. *(output++) = c;
  1025. outputcnt--;
  1026. }
  1027. }
  1028. break;
  1029. case 2: /* Waiting for ) */
  1030. if (c == ')' || c == '}') {
  1031. int i;
  1032. char envname[CONFIG_SYS_CBSIZE], *envval;
  1033. int envcnt = input - varname_start - 1; /* Varname # of chars */
  1034. /* Get the varname */
  1035. for (i = 0; i < envcnt; i++) {
  1036. envname[i] = varname_start[i];
  1037. }
  1038. envname[i] = 0;
  1039. /* Get its value */
  1040. envval = getenv (envname);
  1041. /* Copy into the line if it exists */
  1042. if (envval != NULL)
  1043. while ((*envval) && outputcnt) {
  1044. *(output++) = *(envval++);
  1045. outputcnt--;
  1046. }
  1047. /* Look for another '$' */
  1048. state = 0;
  1049. }
  1050. break;
  1051. case 3: /* Waiting for ' */
  1052. if ((c == '\'') && (prev != '\\')) {
  1053. state = 0;
  1054. } else {
  1055. *(output++) = c;
  1056. outputcnt--;
  1057. }
  1058. break;
  1059. }
  1060. prev = c;
  1061. }
  1062. if (outputcnt)
  1063. *output = 0;
  1064. else
  1065. *(output - 1) = 0;
  1066. #ifdef DEBUG_PARSER
  1067. printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
  1068. strlen (output_start), output_start);
  1069. #endif
  1070. }
  1071. /****************************************************************************
  1072. * returns:
  1073. * 1 - command executed, repeatable
  1074. * 0 - command executed but not repeatable, interrupted commands are
  1075. * always considered not repeatable
  1076. * -1 - not executed (unrecognized, bootd recursion or too many args)
  1077. * (If cmd is NULL or "" or longer than CONFIG_SYS_CBSIZE-1 it is
  1078. * considered unrecognized)
  1079. *
  1080. * WARNING:
  1081. *
  1082. * We must create a temporary copy of the command since the command we get
  1083. * may be the result from getenv(), which returns a pointer directly to
  1084. * the environment data, which may change magicly when the command we run
  1085. * creates or modifies environment variables (like "bootp" does).
  1086. */
  1087. int run_command (const char *cmd, int flag)
  1088. {
  1089. cmd_tbl_t *cmdtp;
  1090. char cmdbuf[CONFIG_SYS_CBSIZE]; /* working copy of cmd */
  1091. char *token; /* start of token in cmdbuf */
  1092. char *sep; /* end of token (separator) in cmdbuf */
  1093. char finaltoken[CONFIG_SYS_CBSIZE];
  1094. char *str = cmdbuf;
  1095. char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */
  1096. int argc, inquotes;
  1097. int repeatable = 1;
  1098. int rc = 0;
  1099. #ifdef DEBUG_PARSER
  1100. printf ("[RUN_COMMAND] cmd[%p]=\"", cmd);
  1101. puts (cmd ? cmd : "NULL"); /* use puts - string may be loooong */
  1102. puts ("\"\n");
  1103. #endif
  1104. clear_ctrlc(); /* forget any previous Control C */
  1105. if (!cmd || !*cmd) {
  1106. return -1; /* empty command */
  1107. }
  1108. if (strlen(cmd) >= CONFIG_SYS_CBSIZE) {
  1109. puts ("## Command too long!\n");
  1110. return -1;
  1111. }
  1112. strcpy (cmdbuf, cmd);
  1113. /* Process separators and check for invalid
  1114. * repeatable commands
  1115. */
  1116. #ifdef DEBUG_PARSER
  1117. printf ("[PROCESS_SEPARATORS] %s\n", cmd);
  1118. #endif
  1119. while (*str) {
  1120. /*
  1121. * Find separator, or string end
  1122. * Allow simple escape of ';' by writing "\;"
  1123. */
  1124. for (inquotes = 0, sep = str; *sep; sep++) {
  1125. if ((*sep=='\'') &&
  1126. (*(sep-1) != '\\'))
  1127. inquotes=!inquotes;
  1128. if (!inquotes &&
  1129. (*sep == ';') && /* separator */
  1130. ( sep != str) && /* past string start */
  1131. (*(sep-1) != '\\')) /* and NOT escaped */
  1132. break;
  1133. }
  1134. /*
  1135. * Limit the token to data between separators
  1136. */
  1137. token = str;
  1138. if (*sep) {
  1139. str = sep + 1; /* start of command for next pass */
  1140. *sep = '\0';
  1141. }
  1142. else
  1143. str = sep; /* no more commands for next pass */
  1144. #ifdef DEBUG_PARSER
  1145. printf ("token: \"%s\"\n", token);
  1146. #endif
  1147. /* find macros in this token and replace them */
  1148. process_macros (token, finaltoken);
  1149. /* Extract arguments */
  1150. if ((argc = parse_line (finaltoken, argv)) == 0) {
  1151. rc = -1; /* no command at all */
  1152. continue;
  1153. }
  1154. /* Look up command in command table */
  1155. if ((cmdtp = find_cmd(argv[0])) == NULL) {
  1156. printf ("Unknown command '%s' - try 'help'\n", argv[0]);
  1157. rc = -1; /* give up after bad command */
  1158. continue;
  1159. }
  1160. /* found - check max args */
  1161. if (argc > cmdtp->maxargs) {
  1162. cmd_usage(cmdtp);
  1163. rc = -1;
  1164. continue;
  1165. }
  1166. #if defined(CONFIG_CMD_BOOTD)
  1167. /* avoid "bootd" recursion */
  1168. if (cmdtp->cmd == do_bootd) {
  1169. #ifdef DEBUG_PARSER
  1170. printf ("[%s]\n", finaltoken);
  1171. #endif
  1172. if (flag & CMD_FLAG_BOOTD) {
  1173. puts ("'bootd' recursion detected\n");
  1174. rc = -1;
  1175. continue;
  1176. } else {
  1177. flag |= CMD_FLAG_BOOTD;
  1178. }
  1179. }
  1180. #endif
  1181. /* OK - call function to do the command */
  1182. if ((cmdtp->cmd) (cmdtp, flag, argc, argv) != 0) {
  1183. rc = -1;
  1184. }
  1185. repeatable &= cmdtp->repeatable;
  1186. /* Did the user stop this? */
  1187. if (had_ctrlc ())
  1188. return -1; /* if stopped then not repeatable */
  1189. }
  1190. return rc ? rc : repeatable;
  1191. }
  1192. /****************************************************************************/
  1193. #if defined(CONFIG_CMD_RUN)
  1194. int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
  1195. {
  1196. int i;
  1197. if (argc < 2)
  1198. return cmd_usage(cmdtp);
  1199. for (i=1; i<argc; ++i) {
  1200. char *arg;
  1201. if ((arg = getenv (argv[i])) == NULL) {
  1202. printf ("## Error: \"%s\" not defined\n", argv[i]);
  1203. return 1;
  1204. }
  1205. if (run_command2(arg, flag) != 0)
  1206. return 1;
  1207. }
  1208. return 0;
  1209. }
  1210. #endif