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