main.c 34 KB

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