main.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587
  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. /****************************************************************************/
  292. void main_loop (void)
  293. {
  294. #ifndef CONFIG_SYS_HUSH_PARSER
  295. static char lastcommand[CONFIG_SYS_CBSIZE] = { 0, };
  296. int len;
  297. int rc = 1;
  298. int flag;
  299. #endif
  300. #if defined(CONFIG_BOOTDELAY) && defined(CONFIG_OF_CONTROL)
  301. char *env;
  302. #endif
  303. #if defined(CONFIG_BOOTDELAY)
  304. char *s;
  305. int bootdelay;
  306. #endif
  307. #ifdef CONFIG_PREBOOT
  308. char *p;
  309. #endif
  310. #ifdef CONFIG_BOOTCOUNT_LIMIT
  311. unsigned long bootcount = 0;
  312. unsigned long bootlimit = 0;
  313. char *bcs;
  314. char bcs_set[16];
  315. #endif /* CONFIG_BOOTCOUNT_LIMIT */
  316. bootstage_mark_name(BOOTSTAGE_ID_MAIN_LOOP, "main_loop");
  317. #ifdef CONFIG_BOOTCOUNT_LIMIT
  318. bootcount = bootcount_load();
  319. bootcount++;
  320. bootcount_store (bootcount);
  321. sprintf (bcs_set, "%lu", bootcount);
  322. setenv ("bootcount", bcs_set);
  323. bcs = getenv ("bootlimit");
  324. bootlimit = bcs ? simple_strtoul (bcs, NULL, 10) : 0;
  325. #endif /* CONFIG_BOOTCOUNT_LIMIT */
  326. #ifdef CONFIG_MODEM_SUPPORT
  327. debug ("DEBUG: main_loop: do_mdm_init=%d\n", do_mdm_init);
  328. if (do_mdm_init) {
  329. char *str = strdup(getenv("mdm_cmd"));
  330. setenv ("preboot", str); /* set or delete definition */
  331. if (str != NULL)
  332. free (str);
  333. mdm_init(); /* wait for modem connection */
  334. }
  335. #endif /* CONFIG_MODEM_SUPPORT */
  336. #ifdef CONFIG_VERSION_VARIABLE
  337. {
  338. setenv ("ver", version_string); /* set version variable */
  339. }
  340. #endif /* CONFIG_VERSION_VARIABLE */
  341. #ifdef CONFIG_SYS_HUSH_PARSER
  342. u_boot_hush_start ();
  343. #endif
  344. #if defined(CONFIG_HUSH_INIT_VAR)
  345. hush_init_var ();
  346. #endif
  347. #ifdef CONFIG_PREBOOT
  348. if ((p = getenv ("preboot")) != NULL) {
  349. # ifdef CONFIG_AUTOBOOT_KEYED
  350. int prev = disable_ctrlc(1); /* disable Control C checking */
  351. # endif
  352. run_command_list(p, -1, 0);
  353. # ifdef CONFIG_AUTOBOOT_KEYED
  354. disable_ctrlc(prev); /* restore Control C checking */
  355. # endif
  356. }
  357. #endif /* CONFIG_PREBOOT */
  358. #if defined(CONFIG_UPDATE_TFTP)
  359. update_tftp (0UL);
  360. #endif /* CONFIG_UPDATE_TFTP */
  361. #if defined(CONFIG_BOOTDELAY)
  362. s = getenv ("bootdelay");
  363. bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
  364. debug ("### main_loop entered: bootdelay=%d\n\n", bootdelay);
  365. #if defined(CONFIG_MENU_SHOW)
  366. bootdelay = menu_show(bootdelay);
  367. #endif
  368. # ifdef CONFIG_BOOT_RETRY_TIME
  369. init_cmd_timeout ();
  370. # endif /* CONFIG_BOOT_RETRY_TIME */
  371. #ifdef CONFIG_POST
  372. if (gd->flags & GD_FLG_POSTFAIL) {
  373. s = getenv("failbootcmd");
  374. }
  375. else
  376. #endif /* CONFIG_POST */
  377. #ifdef CONFIG_BOOTCOUNT_LIMIT
  378. if (bootlimit && (bootcount > bootlimit)) {
  379. printf ("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",
  380. (unsigned)bootlimit);
  381. s = getenv ("altbootcmd");
  382. }
  383. else
  384. #endif /* CONFIG_BOOTCOUNT_LIMIT */
  385. s = getenv ("bootcmd");
  386. #ifdef CONFIG_OF_CONTROL
  387. /* Allow the fdt to override the boot command */
  388. env = fdtdec_get_config_string(gd->fdt_blob, "bootcmd");
  389. if (env)
  390. s = env;
  391. process_fdt_options(gd->fdt_blob);
  392. /*
  393. * If the bootsecure option was chosen, use secure_boot_cmd().
  394. * Always use 'env' in this case, since bootsecure requres that the
  395. * bootcmd was specified in the FDT too.
  396. */
  397. if (fdtdec_get_config_int(gd->fdt_blob, "bootsecure", 0))
  398. secure_boot_cmd(env);
  399. #endif /* CONFIG_OF_CONTROL */
  400. debug ("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
  401. if (bootdelay != -1 && s && !abortboot(bootdelay)) {
  402. # ifdef CONFIG_AUTOBOOT_KEYED
  403. int prev = disable_ctrlc(1); /* disable Control C checking */
  404. # endif
  405. run_command_list(s, -1, 0);
  406. # ifdef CONFIG_AUTOBOOT_KEYED
  407. disable_ctrlc(prev); /* restore Control C checking */
  408. # endif
  409. }
  410. # ifdef CONFIG_MENUKEY
  411. if (menukey == CONFIG_MENUKEY) {
  412. s = getenv("menucmd");
  413. if (s)
  414. run_command_list(s, -1, 0);
  415. }
  416. #endif /* CONFIG_MENUKEY */
  417. #endif /* CONFIG_BOOTDELAY */
  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