main.c 34 KB

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