main.c 34 KB

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