main.c 34 KB

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