main.c 30 KB

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