main.c 34 KB

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