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