main.c 34 KB

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