main.c 29 KB

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