main.c 34 KB

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