main.c 30 KB

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