main.c 30 KB

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