main.c 30 KB

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