main.c 30 KB

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