main.c 30 KB

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