main.c 34 KB

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