main.c 33 KB

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