main.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414
  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. #if defined(CONFIG_SILENT_CONSOLE) || defined(CONFIG_POST) || defined(CONFIG_CMDLINE_EDITING)
  41. DECLARE_GLOBAL_DATA_PTR;
  42. #endif
  43. /*
  44. * Board-specific Platform code can reimplement show_boot_progress () if needed
  45. */
  46. void inline __show_boot_progress (int val) {}
  47. void show_boot_progress (int val) __attribute__((weak, alias("__show_boot_progress")));
  48. #if defined(CONFIG_UPDATE_TFTP)
  49. int update_tftp (ulong addr);
  50. #endif /* CONFIG_UPDATE_TFTP */
  51. #define MAX_DELAY_STOP_STR 32
  52. #undef DEBUG_PARSER
  53. char console_buffer[CONFIG_SYS_CBSIZE + 1]; /* console I/O buffer */
  54. static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen);
  55. static const char erase_seq[] = "\b \b"; /* erase sequence */
  56. static const char tab_seq[] = " "; /* used to expand TABs */
  57. #ifdef CONFIG_BOOT_RETRY_TIME
  58. static uint64_t endtime = 0; /* must be set, default is instant timeout */
  59. static int retry_time = -1; /* -1 so can call readline before main_loop */
  60. #endif
  61. #define endtick(seconds) (get_ticks() + (uint64_t)(seconds) * get_tbclk())
  62. #ifndef CONFIG_BOOT_RETRY_MIN
  63. #define CONFIG_BOOT_RETRY_MIN CONFIG_BOOT_RETRY_TIME
  64. #endif
  65. #ifdef CONFIG_MODEM_SUPPORT
  66. int do_mdm_init = 0;
  67. extern void mdm_init(void); /* defined in board.c */
  68. #endif
  69. /***************************************************************************
  70. * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
  71. * returns: 0 - no key string, allow autoboot 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. * Return 0 on success, or != 0 on error.
  230. */
  231. static inline
  232. int run_command2(const char *cmd, int flag)
  233. {
  234. #ifndef CONFIG_SYS_HUSH_PARSER
  235. /*
  236. * run_command can return 0 or 1 for success, so clean up its result.
  237. */
  238. if (run_command(cmd, flag) == -1)
  239. return 1;
  240. return 0;
  241. #else
  242. return parse_string_outer(cmd,
  243. FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP);
  244. #endif
  245. }
  246. /****************************************************************************/
  247. void main_loop (void)
  248. {
  249. #ifndef CONFIG_SYS_HUSH_PARSER
  250. static char lastcommand[CONFIG_SYS_CBSIZE] = { 0, };
  251. int len;
  252. int rc = 1;
  253. int flag;
  254. #endif
  255. #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
  256. char *s;
  257. int bootdelay;
  258. #endif
  259. #ifdef CONFIG_PREBOOT
  260. char *p;
  261. #endif
  262. #ifdef CONFIG_BOOTCOUNT_LIMIT
  263. unsigned long bootcount = 0;
  264. unsigned long bootlimit = 0;
  265. char *bcs;
  266. char bcs_set[16];
  267. #endif /* CONFIG_BOOTCOUNT_LIMIT */
  268. #ifdef CONFIG_BOOTCOUNT_LIMIT
  269. bootcount = bootcount_load();
  270. bootcount++;
  271. bootcount_store (bootcount);
  272. sprintf (bcs_set, "%lu", bootcount);
  273. setenv ("bootcount", bcs_set);
  274. bcs = getenv ("bootlimit");
  275. bootlimit = bcs ? simple_strtoul (bcs, NULL, 10) : 0;
  276. #endif /* CONFIG_BOOTCOUNT_LIMIT */
  277. #ifdef CONFIG_MODEM_SUPPORT
  278. debug ("DEBUG: main_loop: do_mdm_init=%d\n", do_mdm_init);
  279. if (do_mdm_init) {
  280. char *str = strdup(getenv("mdm_cmd"));
  281. setenv ("preboot", str); /* set or delete definition */
  282. if (str != NULL)
  283. free (str);
  284. mdm_init(); /* wait for modem connection */
  285. }
  286. #endif /* CONFIG_MODEM_SUPPORT */
  287. #ifdef CONFIG_VERSION_VARIABLE
  288. {
  289. setenv ("ver", version_string); /* set version variable */
  290. }
  291. #endif /* CONFIG_VERSION_VARIABLE */
  292. #ifdef CONFIG_SYS_HUSH_PARSER
  293. u_boot_hush_start ();
  294. #endif
  295. #if defined(CONFIG_HUSH_INIT_VAR)
  296. hush_init_var ();
  297. #endif
  298. #ifdef CONFIG_PREBOOT
  299. if ((p = getenv ("preboot")) != NULL) {
  300. # ifdef CONFIG_AUTOBOOT_KEYED
  301. int prev = disable_ctrlc(1); /* disable Control C checking */
  302. # endif
  303. run_command2(p, 0);
  304. # ifdef CONFIG_AUTOBOOT_KEYED
  305. disable_ctrlc(prev); /* restore Control C checking */
  306. # endif
  307. }
  308. #endif /* CONFIG_PREBOOT */
  309. #if defined(CONFIG_UPDATE_TFTP)
  310. update_tftp (0UL);
  311. #endif /* CONFIG_UPDATE_TFTP */
  312. #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
  313. s = getenv ("bootdelay");
  314. bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
  315. debug ("### main_loop entered: bootdelay=%d\n\n", bootdelay);
  316. # ifdef CONFIG_BOOT_RETRY_TIME
  317. init_cmd_timeout ();
  318. # endif /* CONFIG_BOOT_RETRY_TIME */
  319. #ifdef CONFIG_POST
  320. if (gd->flags & GD_FLG_POSTFAIL) {
  321. s = getenv("failbootcmd");
  322. }
  323. else
  324. #endif /* CONFIG_POST */
  325. #ifdef CONFIG_BOOTCOUNT_LIMIT
  326. if (bootlimit && (bootcount > bootlimit)) {
  327. printf ("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",
  328. (unsigned)bootlimit);
  329. s = getenv ("altbootcmd");
  330. }
  331. else
  332. #endif /* CONFIG_BOOTCOUNT_LIMIT */
  333. s = getenv ("bootcmd");
  334. debug ("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
  335. if (bootdelay >= 0 && s && !abortboot (bootdelay)) {
  336. # ifdef CONFIG_AUTOBOOT_KEYED
  337. int prev = disable_ctrlc(1); /* disable Control C checking */
  338. # endif
  339. run_command2(s, 0);
  340. # ifdef CONFIG_AUTOBOOT_KEYED
  341. disable_ctrlc(prev); /* restore Control C checking */
  342. # endif
  343. }
  344. # ifdef CONFIG_MENUKEY
  345. if (menukey == CONFIG_MENUKEY) {
  346. s = getenv("menucmd");
  347. if (s)
  348. run_command2(s, 0);
  349. }
  350. #endif /* CONFIG_MENUKEY */
  351. #endif /* CONFIG_BOOTDELAY */
  352. /*
  353. * Main Loop for Monitor Command Processing
  354. */
  355. #ifdef CONFIG_SYS_HUSH_PARSER
  356. parse_file_outer();
  357. /* This point is never reached */
  358. for (;;);
  359. #else
  360. for (;;) {
  361. #ifdef CONFIG_BOOT_RETRY_TIME
  362. if (rc >= 0) {
  363. /* Saw enough of a valid command to
  364. * restart the timeout.
  365. */
  366. reset_cmd_timeout();
  367. }
  368. #endif
  369. len = readline (CONFIG_SYS_PROMPT);
  370. flag = 0; /* assume no special flags for now */
  371. if (len > 0)
  372. strcpy (lastcommand, console_buffer);
  373. else if (len == 0)
  374. flag |= CMD_FLAG_REPEAT;
  375. #ifdef CONFIG_BOOT_RETRY_TIME
  376. else if (len == -2) {
  377. /* -2 means timed out, retry autoboot
  378. */
  379. puts ("\nTimed out waiting for command\n");
  380. # ifdef CONFIG_RESET_TO_RETRY
  381. /* Reinit board to run initialization code again */
  382. do_reset (NULL, 0, 0, NULL);
  383. # else
  384. return; /* retry autoboot */
  385. # endif
  386. }
  387. #endif
  388. if (len == -1)
  389. puts ("<INTERRUPT>\n");
  390. else
  391. rc = run_command (lastcommand, flag);
  392. if (rc <= 0) {
  393. /* invalid command or not repeatable, forget it */
  394. lastcommand[0] = 0;
  395. }
  396. }
  397. #endif /*CONFIG_SYS_HUSH_PARSER*/
  398. }
  399. #ifdef CONFIG_BOOT_RETRY_TIME
  400. /***************************************************************************
  401. * initialize command line timeout
  402. */
  403. void init_cmd_timeout(void)
  404. {
  405. char *s = getenv ("bootretry");
  406. if (s != NULL)
  407. retry_time = (int)simple_strtol(s, NULL, 10);
  408. else
  409. retry_time = CONFIG_BOOT_RETRY_TIME;
  410. if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN)
  411. retry_time = CONFIG_BOOT_RETRY_MIN;
  412. }
  413. /***************************************************************************
  414. * reset command line timeout to retry_time seconds
  415. */
  416. void reset_cmd_timeout(void)
  417. {
  418. endtime = endtick(retry_time);
  419. }
  420. #endif
  421. #ifdef CONFIG_CMDLINE_EDITING
  422. /*
  423. * cmdline-editing related codes from vivi.
  424. * Author: Janghoon Lyu <nandy@mizi.com>
  425. */
  426. #define putnstr(str,n) do { \
  427. printf ("%.*s", (int)n, str); \
  428. } while (0)
  429. #define CTL_CH(c) ((c) - 'a' + 1)
  430. #define CTL_BACKSPACE ('\b')
  431. #define DEL ((char)255)
  432. #define DEL7 ((char)127)
  433. #define CREAD_HIST_CHAR ('!')
  434. #define getcmd_putch(ch) putc(ch)
  435. #define getcmd_getch() getc()
  436. #define getcmd_cbeep() getcmd_putch('\a')
  437. #define HIST_MAX 20
  438. #define HIST_SIZE CONFIG_SYS_CBSIZE
  439. static int hist_max = 0;
  440. static int hist_add_idx = 0;
  441. static int hist_cur = -1;
  442. unsigned hist_num = 0;
  443. char* hist_list[HIST_MAX];
  444. char hist_lines[HIST_MAX][HIST_SIZE + 1]; /* Save room for NULL */
  445. #define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
  446. static void hist_init(void)
  447. {
  448. int i;
  449. hist_max = 0;
  450. hist_add_idx = 0;
  451. hist_cur = -1;
  452. hist_num = 0;
  453. for (i = 0; i < HIST_MAX; i++) {
  454. hist_list[i] = hist_lines[i];
  455. hist_list[i][0] = '\0';
  456. }
  457. }
  458. static void cread_add_to_hist(char *line)
  459. {
  460. strcpy(hist_list[hist_add_idx], line);
  461. if (++hist_add_idx >= HIST_MAX)
  462. hist_add_idx = 0;
  463. if (hist_add_idx > hist_max)
  464. hist_max = hist_add_idx;
  465. hist_num++;
  466. }
  467. static char* hist_prev(void)
  468. {
  469. char *ret;
  470. int old_cur;
  471. if (hist_cur < 0)
  472. return NULL;
  473. old_cur = hist_cur;
  474. if (--hist_cur < 0)
  475. hist_cur = hist_max;
  476. if (hist_cur == hist_add_idx) {
  477. hist_cur = old_cur;
  478. ret = NULL;
  479. } else
  480. ret = hist_list[hist_cur];
  481. return (ret);
  482. }
  483. static char* hist_next(void)
  484. {
  485. char *ret;
  486. if (hist_cur < 0)
  487. return NULL;
  488. if (hist_cur == hist_add_idx)
  489. return NULL;
  490. if (++hist_cur > hist_max)
  491. hist_cur = 0;
  492. if (hist_cur == hist_add_idx) {
  493. ret = "";
  494. } else
  495. ret = hist_list[hist_cur];
  496. return (ret);
  497. }
  498. #ifndef CONFIG_CMDLINE_EDITING
  499. static void cread_print_hist_list(void)
  500. {
  501. int i;
  502. unsigned long n;
  503. n = hist_num - hist_max;
  504. i = hist_add_idx + 1;
  505. while (1) {
  506. if (i > hist_max)
  507. i = 0;
  508. if (i == hist_add_idx)
  509. break;
  510. printf("%s\n", hist_list[i]);
  511. n++;
  512. i++;
  513. }
  514. }
  515. #endif /* CONFIG_CMDLINE_EDITING */
  516. #define BEGINNING_OF_LINE() { \
  517. while (num) { \
  518. getcmd_putch(CTL_BACKSPACE); \
  519. num--; \
  520. } \
  521. }
  522. #define ERASE_TO_EOL() { \
  523. if (num < eol_num) { \
  524. printf("%*s", (int)(eol_num - num), ""); \
  525. do { \
  526. getcmd_putch(CTL_BACKSPACE); \
  527. } while (--eol_num > num); \
  528. } \
  529. }
  530. #define REFRESH_TO_EOL() { \
  531. if (num < eol_num) { \
  532. wlen = eol_num - num; \
  533. putnstr(buf + num, wlen); \
  534. num = eol_num; \
  535. } \
  536. }
  537. static void cread_add_char(char ichar, int insert, unsigned long *num,
  538. unsigned long *eol_num, char *buf, unsigned long len)
  539. {
  540. unsigned long wlen;
  541. /* room ??? */
  542. if (insert || *num == *eol_num) {
  543. if (*eol_num > len - 1) {
  544. getcmd_cbeep();
  545. return;
  546. }
  547. (*eol_num)++;
  548. }
  549. if (insert) {
  550. wlen = *eol_num - *num;
  551. if (wlen > 1) {
  552. memmove(&buf[*num+1], &buf[*num], wlen-1);
  553. }
  554. buf[*num] = ichar;
  555. putnstr(buf + *num, wlen);
  556. (*num)++;
  557. while (--wlen) {
  558. getcmd_putch(CTL_BACKSPACE);
  559. }
  560. } else {
  561. /* echo the character */
  562. wlen = 1;
  563. buf[*num] = ichar;
  564. putnstr(buf + *num, wlen);
  565. (*num)++;
  566. }
  567. }
  568. static void cread_add_str(char *str, int strsize, int insert, unsigned long *num,
  569. unsigned long *eol_num, char *buf, unsigned long len)
  570. {
  571. while (strsize--) {
  572. cread_add_char(*str, insert, num, eol_num, buf, len);
  573. str++;
  574. }
  575. }
  576. static int cread_line(const char *const prompt, char *buf, unsigned int *len)
  577. {
  578. unsigned long num = 0;
  579. unsigned long eol_num = 0;
  580. unsigned long wlen;
  581. char ichar;
  582. int insert = 1;
  583. int esc_len = 0;
  584. char esc_save[8];
  585. int init_len = strlen(buf);
  586. if (init_len)
  587. cread_add_str(buf, init_len, 1, &num, &eol_num, buf, *len);
  588. while (1) {
  589. #ifdef CONFIG_BOOT_RETRY_TIME
  590. while (!tstc()) { /* while no incoming data */
  591. if (retry_time >= 0 && get_ticks() > endtime)
  592. return (-2); /* timed out */
  593. WATCHDOG_RESET();
  594. }
  595. #endif
  596. ichar = getcmd_getch();
  597. if ((ichar == '\n') || (ichar == '\r')) {
  598. putc('\n');
  599. break;
  600. }
  601. /*
  602. * handle standard linux xterm esc sequences for arrow key, etc.
  603. */
  604. if (esc_len != 0) {
  605. if (esc_len == 1) {
  606. if (ichar == '[') {
  607. esc_save[esc_len] = ichar;
  608. esc_len = 2;
  609. } else {
  610. cread_add_str(esc_save, esc_len, insert,
  611. &num, &eol_num, buf, *len);
  612. esc_len = 0;
  613. }
  614. continue;
  615. }
  616. switch (ichar) {
  617. case 'D': /* <- key */
  618. ichar = CTL_CH('b');
  619. esc_len = 0;
  620. break;
  621. case 'C': /* -> key */
  622. ichar = CTL_CH('f');
  623. esc_len = 0;
  624. break; /* pass off to ^F handler */
  625. case 'H': /* Home key */
  626. ichar = CTL_CH('a');
  627. esc_len = 0;
  628. break; /* pass off to ^A handler */
  629. case 'A': /* up arrow */
  630. ichar = CTL_CH('p');
  631. esc_len = 0;
  632. break; /* pass off to ^P handler */
  633. case 'B': /* down arrow */
  634. ichar = CTL_CH('n');
  635. esc_len = 0;
  636. break; /* pass off to ^N handler */
  637. default:
  638. esc_save[esc_len++] = ichar;
  639. cread_add_str(esc_save, esc_len, insert,
  640. &num, &eol_num, buf, *len);
  641. esc_len = 0;
  642. continue;
  643. }
  644. }
  645. switch (ichar) {
  646. case 0x1b:
  647. if (esc_len == 0) {
  648. esc_save[esc_len] = ichar;
  649. esc_len = 1;
  650. } else {
  651. puts("impossible condition #876\n");
  652. esc_len = 0;
  653. }
  654. break;
  655. case CTL_CH('a'):
  656. BEGINNING_OF_LINE();
  657. break;
  658. case CTL_CH('c'): /* ^C - break */
  659. *buf = '\0'; /* discard input */
  660. return (-1);
  661. case CTL_CH('f'):
  662. if (num < eol_num) {
  663. getcmd_putch(buf[num]);
  664. num++;
  665. }
  666. break;
  667. case CTL_CH('b'):
  668. if (num) {
  669. getcmd_putch(CTL_BACKSPACE);
  670. num--;
  671. }
  672. break;
  673. case CTL_CH('d'):
  674. if (num < eol_num) {
  675. wlen = eol_num - num - 1;
  676. if (wlen) {
  677. memmove(&buf[num], &buf[num+1], wlen);
  678. putnstr(buf + num, wlen);
  679. }
  680. getcmd_putch(' ');
  681. do {
  682. getcmd_putch(CTL_BACKSPACE);
  683. } while (wlen--);
  684. eol_num--;
  685. }
  686. break;
  687. case CTL_CH('k'):
  688. ERASE_TO_EOL();
  689. break;
  690. case CTL_CH('e'):
  691. REFRESH_TO_EOL();
  692. break;
  693. case CTL_CH('o'):
  694. insert = !insert;
  695. break;
  696. case CTL_CH('x'):
  697. case CTL_CH('u'):
  698. BEGINNING_OF_LINE();
  699. ERASE_TO_EOL();
  700. break;
  701. case DEL:
  702. case DEL7:
  703. case 8:
  704. if (num) {
  705. wlen = eol_num - num;
  706. num--;
  707. memmove(&buf[num], &buf[num+1], wlen);
  708. getcmd_putch(CTL_BACKSPACE);
  709. putnstr(buf + num, wlen);
  710. getcmd_putch(' ');
  711. do {
  712. getcmd_putch(CTL_BACKSPACE);
  713. } while (wlen--);
  714. eol_num--;
  715. }
  716. break;
  717. case CTL_CH('p'):
  718. case CTL_CH('n'):
  719. {
  720. char * hline;
  721. esc_len = 0;
  722. if (ichar == CTL_CH('p'))
  723. hline = hist_prev();
  724. else
  725. hline = hist_next();
  726. if (!hline) {
  727. getcmd_cbeep();
  728. continue;
  729. }
  730. /* nuke the current line */
  731. /* first, go home */
  732. BEGINNING_OF_LINE();
  733. /* erase to end of line */
  734. ERASE_TO_EOL();
  735. /* copy new line into place and display */
  736. strcpy(buf, hline);
  737. eol_num = strlen(buf);
  738. REFRESH_TO_EOL();
  739. continue;
  740. }
  741. #ifdef CONFIG_AUTO_COMPLETE
  742. case '\t': {
  743. int num2, col;
  744. /* do not autocomplete when in the middle */
  745. if (num < eol_num) {
  746. getcmd_cbeep();
  747. break;
  748. }
  749. buf[num] = '\0';
  750. col = strlen(prompt) + eol_num;
  751. num2 = num;
  752. if (cmd_auto_complete(prompt, buf, &num2, &col)) {
  753. col = num2 - num;
  754. num += col;
  755. eol_num += col;
  756. }
  757. break;
  758. }
  759. #endif
  760. default:
  761. cread_add_char(ichar, insert, &num, &eol_num, buf, *len);
  762. break;
  763. }
  764. }
  765. *len = eol_num;
  766. buf[eol_num] = '\0'; /* lose the newline */
  767. if (buf[0] && buf[0] != CREAD_HIST_CHAR)
  768. cread_add_to_hist(buf);
  769. hist_cur = hist_add_idx;
  770. return 0;
  771. }
  772. #endif /* CONFIG_CMDLINE_EDITING */
  773. /****************************************************************************/
  774. /*
  775. * Prompt for input and read a line.
  776. * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
  777. * time out when time goes past endtime (timebase time in ticks).
  778. * Return: number of read characters
  779. * -1 if break
  780. * -2 if timed out
  781. */
  782. int readline (const char *const prompt)
  783. {
  784. /*
  785. * If console_buffer isn't 0-length the user will be prompted to modify
  786. * it instead of entering it from scratch as desired.
  787. */
  788. console_buffer[0] = '\0';
  789. return readline_into_buffer(prompt, console_buffer);
  790. }
  791. int readline_into_buffer (const char *const prompt, char * buffer)
  792. {
  793. char *p = buffer;
  794. #ifdef CONFIG_CMDLINE_EDITING
  795. unsigned int len = CONFIG_SYS_CBSIZE;
  796. int rc;
  797. static int initted = 0;
  798. /*
  799. * History uses a global array which is not
  800. * writable until after relocation to RAM.
  801. * Revert to non-history version if still
  802. * running from flash.
  803. */
  804. if (gd->flags & GD_FLG_RELOC) {
  805. if (!initted) {
  806. hist_init();
  807. initted = 1;
  808. }
  809. if (prompt)
  810. puts (prompt);
  811. rc = cread_line(prompt, p, &len);
  812. return rc < 0 ? rc : len;
  813. } else {
  814. #endif /* CONFIG_CMDLINE_EDITING */
  815. char * p_buf = p;
  816. int n = 0; /* buffer index */
  817. int plen = 0; /* prompt length */
  818. int col; /* output column cnt */
  819. char c;
  820. /* print prompt */
  821. if (prompt) {
  822. plen = strlen (prompt);
  823. puts (prompt);
  824. }
  825. col = plen;
  826. for (;;) {
  827. #ifdef CONFIG_BOOT_RETRY_TIME
  828. while (!tstc()) { /* while no incoming data */
  829. if (retry_time >= 0 && get_ticks() > endtime)
  830. return (-2); /* timed out */
  831. WATCHDOG_RESET();
  832. }
  833. #endif
  834. WATCHDOG_RESET(); /* Trigger watchdog, if needed */
  835. #ifdef CONFIG_SHOW_ACTIVITY
  836. while (!tstc()) {
  837. extern void show_activity(int arg);
  838. show_activity(0);
  839. WATCHDOG_RESET();
  840. }
  841. #endif
  842. c = getc();
  843. /*
  844. * Special character handling
  845. */
  846. switch (c) {
  847. case '\r': /* Enter */
  848. case '\n':
  849. *p = '\0';
  850. puts ("\r\n");
  851. return (p - p_buf);
  852. case '\0': /* nul */
  853. continue;
  854. case 0x03: /* ^C - break */
  855. p_buf[0] = '\0'; /* discard input */
  856. return (-1);
  857. case 0x15: /* ^U - erase line */
  858. while (col > plen) {
  859. puts (erase_seq);
  860. --col;
  861. }
  862. p = p_buf;
  863. n = 0;
  864. continue;
  865. case 0x17: /* ^W - erase word */
  866. p=delete_char(p_buf, p, &col, &n, plen);
  867. while ((n > 0) && (*p != ' ')) {
  868. p=delete_char(p_buf, p, &col, &n, plen);
  869. }
  870. continue;
  871. case 0x08: /* ^H - backspace */
  872. case 0x7F: /* DEL - backspace */
  873. p=delete_char(p_buf, p, &col, &n, plen);
  874. continue;
  875. default:
  876. /*
  877. * Must be a normal character then
  878. */
  879. if (n < CONFIG_SYS_CBSIZE-2) {
  880. if (c == '\t') { /* expand TABs */
  881. #ifdef CONFIG_AUTO_COMPLETE
  882. /* if auto completion triggered just continue */
  883. *p = '\0';
  884. if (cmd_auto_complete(prompt, console_buffer, &n, &col)) {
  885. p = p_buf + n; /* reset */
  886. continue;
  887. }
  888. #endif
  889. puts (tab_seq+(col&07));
  890. col += 8 - (col&07);
  891. } else {
  892. ++col; /* echo input */
  893. putc (c);
  894. }
  895. *p++ = c;
  896. ++n;
  897. } else { /* Buffer full */
  898. putc ('\a');
  899. }
  900. }
  901. }
  902. #ifdef CONFIG_CMDLINE_EDITING
  903. }
  904. #endif
  905. }
  906. /****************************************************************************/
  907. static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen)
  908. {
  909. char *s;
  910. if (*np == 0) {
  911. return (p);
  912. }
  913. if (*(--p) == '\t') { /* will retype the whole line */
  914. while (*colp > plen) {
  915. puts (erase_seq);
  916. (*colp)--;
  917. }
  918. for (s=buffer; s<p; ++s) {
  919. if (*s == '\t') {
  920. puts (tab_seq+((*colp) & 07));
  921. *colp += 8 - ((*colp) & 07);
  922. } else {
  923. ++(*colp);
  924. putc (*s);
  925. }
  926. }
  927. } else {
  928. puts (erase_seq);
  929. (*colp)--;
  930. }
  931. (*np)--;
  932. return (p);
  933. }
  934. /****************************************************************************/
  935. int parse_line (char *line, char *argv[])
  936. {
  937. int nargs = 0;
  938. #ifdef DEBUG_PARSER
  939. printf ("parse_line: \"%s\"\n", line);
  940. #endif
  941. while (nargs < CONFIG_SYS_MAXARGS) {
  942. /* skip any white space */
  943. while (isblank(*line))
  944. ++line;
  945. if (*line == '\0') { /* end of line, no more args */
  946. argv[nargs] = NULL;
  947. #ifdef DEBUG_PARSER
  948. printf ("parse_line: nargs=%d\n", nargs);
  949. #endif
  950. return (nargs);
  951. }
  952. argv[nargs++] = line; /* begin of argument string */
  953. /* find end of string */
  954. while (*line && !isblank(*line))
  955. ++line;
  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. if (run_command2(arg, flag) != 0)
  1204. return 1;
  1205. }
  1206. return 0;
  1207. }
  1208. #endif