main.c 30 KB

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