main.c 30 KB

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