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