main.c 34 KB

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