cmd_nvedit.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  1. /*
  2. * (C) Copyright 2000-2010
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  6. * Andreas Heppel <aheppel@sysgo.de>
  7. *
  8. * Copyright 2011 Freescale Semiconductor, Inc.
  9. *
  10. * See file CREDITS for list of people who contributed to this
  11. * project.
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License as
  15. * published by the Free Software Foundation; either version 2 of
  16. * the License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  26. * MA 02111-1307 USA
  27. */
  28. /*
  29. * Support for persistent environment data
  30. *
  31. * The "environment" is stored on external storage as a list of '\0'
  32. * terminated "name=value" strings. The end of the list is marked by
  33. * a double '\0'. The environment is preceeded by a 32 bit CRC over
  34. * the data part and, in case of redundant environment, a byte of
  35. * flags.
  36. *
  37. * This linearized representation will also be used before
  38. * relocation, i. e. as long as we don't have a full C runtime
  39. * environment. After that, we use a hash table.
  40. */
  41. #include <common.h>
  42. #include <command.h>
  43. #include <environment.h>
  44. #include <search.h>
  45. #include <errno.h>
  46. #include <malloc.h>
  47. #include <watchdog.h>
  48. #include <serial.h>
  49. #include <linux/stddef.h>
  50. #include <asm/byteorder.h>
  51. #if defined(CONFIG_CMD_NET)
  52. #include <net.h>
  53. #endif
  54. DECLARE_GLOBAL_DATA_PTR;
  55. #if !defined(CONFIG_ENV_IS_IN_EEPROM) && \
  56. !defined(CONFIG_ENV_IS_IN_FLASH) && \
  57. !defined(CONFIG_ENV_IS_IN_DATAFLASH) && \
  58. !defined(CONFIG_ENV_IS_IN_MG_DISK) && \
  59. !defined(CONFIG_ENV_IS_IN_MMC) && \
  60. !defined(CONFIG_ENV_IS_IN_FAT) && \
  61. !defined(CONFIG_ENV_IS_IN_NAND) && \
  62. !defined(CONFIG_ENV_IS_IN_NVRAM) && \
  63. !defined(CONFIG_ENV_IS_IN_ONENAND) && \
  64. !defined(CONFIG_ENV_IS_IN_SPI_FLASH) && \
  65. !defined(CONFIG_ENV_IS_NOWHERE)
  66. # error Define one of CONFIG_ENV_IS_IN_{EEPROM|FLASH|DATAFLASH|ONENAND|\
  67. SPI_FLASH|MG_DISK|NVRAM|MMC|FAT} or CONFIG_ENV_IS_NOWHERE
  68. #endif
  69. #define XMK_STR(x) #x
  70. #define MK_STR(x) XMK_STR(x)
  71. /*
  72. * Maximum expected input data size for import command
  73. */
  74. #define MAX_ENV_SIZE (1 << 20) /* 1 MiB */
  75. ulong load_addr = CONFIG_SYS_LOAD_ADDR; /* Default Load Address */
  76. ulong save_addr; /* Default Save Address */
  77. ulong save_size; /* Default Save Size (in bytes) */
  78. /*
  79. * Table with supported baudrates (defined in config_xyz.h)
  80. */
  81. static const unsigned long baudrate_table[] = CONFIG_SYS_BAUDRATE_TABLE;
  82. #define N_BAUDRATES (sizeof(baudrate_table) / sizeof(baudrate_table[0]))
  83. /*
  84. * This variable is incremented on each do_env_set(), so it can
  85. * be used via get_env_id() as an indication, if the environment
  86. * has changed or not. So it is possible to reread an environment
  87. * variable only if the environment was changed ... done so for
  88. * example in NetInitLoop()
  89. */
  90. static int env_id = 1;
  91. int get_env_id(void)
  92. {
  93. return env_id;
  94. }
  95. /*
  96. * Command interface: print one or all environment variables
  97. *
  98. * Returns 0 in case of error, or length of printed string
  99. */
  100. static int env_print(char *name)
  101. {
  102. char *res = NULL;
  103. size_t len;
  104. if (name) { /* print a single name */
  105. ENTRY e, *ep;
  106. e.key = name;
  107. e.data = NULL;
  108. hsearch_r(e, FIND, &ep, &env_htab);
  109. if (ep == NULL)
  110. return 0;
  111. len = printf("%s=%s\n", ep->key, ep->data);
  112. return len;
  113. }
  114. /* print whole list */
  115. len = hexport_r(&env_htab, '\n', &res, 0, 0, NULL);
  116. if (len > 0) {
  117. puts(res);
  118. free(res);
  119. return len;
  120. }
  121. /* should never happen */
  122. return 0;
  123. }
  124. int do_env_print (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  125. {
  126. int i;
  127. int rcode = 0;
  128. if (argc == 1) {
  129. /* print all env vars */
  130. rcode = env_print(NULL);
  131. if (!rcode)
  132. return 1;
  133. printf("\nEnvironment size: %d/%ld bytes\n",
  134. rcode, (ulong)ENV_SIZE);
  135. return 0;
  136. }
  137. /* print selected env vars */
  138. for (i = 1; i < argc; ++i) {
  139. int rc = env_print(argv[i]);
  140. if (!rc) {
  141. printf("## Error: \"%s\" not defined\n", argv[i]);
  142. ++rcode;
  143. }
  144. }
  145. return rcode;
  146. }
  147. #ifdef CONFIG_CMD_GREPENV
  148. static int do_env_grep(cmd_tbl_t *cmdtp, int flag,
  149. int argc, char * const argv[])
  150. {
  151. ENTRY *match;
  152. unsigned char matched[env_htab.size / 8];
  153. int rcode = 1, arg = 1, idx;
  154. if (argc < 2)
  155. return CMD_RET_USAGE;
  156. memset(matched, 0, env_htab.size / 8);
  157. while (arg <= argc) {
  158. idx = 0;
  159. while ((idx = hstrstr_r(argv[arg], idx, &match, &env_htab))) {
  160. if (!(matched[idx / 8] & (1 << (idx & 7)))) {
  161. puts(match->key);
  162. puts("=");
  163. puts(match->data);
  164. puts("\n");
  165. }
  166. matched[idx / 8] |= 1 << (idx & 7);
  167. rcode = 0;
  168. }
  169. arg++;
  170. }
  171. return rcode;
  172. }
  173. #endif
  174. /*
  175. * Set a new environment variable,
  176. * or replace or delete an existing one.
  177. */
  178. int _do_env_set(int flag, int argc, char * const argv[])
  179. {
  180. bd_t *bd = gd->bd;
  181. int i, len;
  182. int console = -1;
  183. char *name, *value, *s;
  184. ENTRY e, *ep;
  185. name = argv[1];
  186. if (strchr(name, '=')) {
  187. printf("## Error: illegal character '=' in variable name"
  188. "\"%s\"\n", name);
  189. return 1;
  190. }
  191. env_id++;
  192. /*
  193. * search if variable with this name already exists
  194. */
  195. e.key = name;
  196. e.data = NULL;
  197. hsearch_r(e, FIND, &ep, &env_htab);
  198. /* Check for console redirection */
  199. if (strcmp(name, "stdin") == 0)
  200. console = stdin;
  201. else if (strcmp(name, "stdout") == 0)
  202. console = stdout;
  203. else if (strcmp(name, "stderr") == 0)
  204. console = stderr;
  205. if (console != -1) {
  206. if (argc < 3) { /* Cannot delete it! */
  207. printf("Can't delete \"%s\"\n", name);
  208. return 1;
  209. }
  210. #ifdef CONFIG_CONSOLE_MUX
  211. i = iomux_doenv(console, argv[2]);
  212. if (i)
  213. return i;
  214. #else
  215. /* Try assigning specified device */
  216. if (console_assign(console, argv[2]) < 0)
  217. return 1;
  218. #ifdef CONFIG_SERIAL_MULTI
  219. if (serial_assign(argv[2]) < 0)
  220. return 1;
  221. #endif
  222. #endif /* CONFIG_CONSOLE_MUX */
  223. }
  224. /*
  225. * Some variables like "ethaddr" and "serial#" can be set only
  226. * once and cannot be deleted; also, "ver" is readonly.
  227. */
  228. if (ep) { /* variable exists */
  229. #ifndef CONFIG_ENV_OVERWRITE
  230. if (strcmp(name, "serial#") == 0 ||
  231. (strcmp(name, "ethaddr") == 0
  232. #if defined(CONFIG_OVERWRITE_ETHADDR_ONCE) && defined(CONFIG_ETHADDR)
  233. && strcmp(ep->data, MK_STR(CONFIG_ETHADDR)) != 0
  234. #endif /* CONFIG_OVERWRITE_ETHADDR_ONCE && CONFIG_ETHADDR */
  235. )) {
  236. printf("Can't overwrite \"%s\"\n", name);
  237. return 1;
  238. }
  239. #endif
  240. /*
  241. * Switch to new baudrate if new baudrate is supported
  242. */
  243. if (strcmp(name, "baudrate") == 0) {
  244. int baudrate = simple_strtoul(argv[2], NULL, 10);
  245. int i;
  246. for (i = 0; i < N_BAUDRATES; ++i) {
  247. if (baudrate == baudrate_table[i])
  248. break;
  249. }
  250. if (i == N_BAUDRATES) {
  251. printf("## Baudrate %d bps not supported\n",
  252. baudrate);
  253. return 1;
  254. }
  255. printf("## Switch baudrate to %d bps and"
  256. "press ENTER ...\n", baudrate);
  257. udelay(50000);
  258. gd->baudrate = baudrate;
  259. #if defined(CONFIG_PPC) || defined(CONFIG_MCF52x2)
  260. gd->bd->bi_baudrate = baudrate;
  261. #endif
  262. serial_setbrg();
  263. udelay(50000);
  264. while (getc() != '\r')
  265. ;
  266. }
  267. }
  268. /* Delete only ? */
  269. if (argc < 3 || argv[2] == NULL) {
  270. int rc = hdelete_r(name, &env_htab);
  271. return !rc;
  272. }
  273. /*
  274. * Insert / replace new value
  275. */
  276. for (i = 2, len = 0; i < argc; ++i)
  277. len += strlen(argv[i]) + 1;
  278. value = malloc(len);
  279. if (value == NULL) {
  280. printf("## Can't malloc %d bytes\n", len);
  281. return 1;
  282. }
  283. for (i = 2, s = value; i < argc; ++i) {
  284. char *v = argv[i];
  285. while ((*s++ = *v++) != '\0')
  286. ;
  287. *(s - 1) = ' ';
  288. }
  289. if (s != value)
  290. *--s = '\0';
  291. e.key = name;
  292. e.data = value;
  293. hsearch_r(e, ENTER, &ep, &env_htab);
  294. free(value);
  295. if (!ep) {
  296. printf("## Error inserting \"%s\" variable, errno=%d\n",
  297. name, errno);
  298. return 1;
  299. }
  300. /*
  301. * Some variables should be updated when the corresponding
  302. * entry in the environment is changed
  303. */
  304. if (strcmp(name, "ipaddr") == 0) {
  305. char *s = argv[2]; /* always use only one arg */
  306. char *e;
  307. unsigned long addr;
  308. bd->bi_ip_addr = 0;
  309. for (addr = 0, i = 0; i < 4; ++i) {
  310. ulong val = s ? simple_strtoul(s, &e, 10) : 0;
  311. addr <<= 8;
  312. addr |= val & 0xFF;
  313. if (s)
  314. s = *e ? e + 1 : e;
  315. }
  316. bd->bi_ip_addr = htonl(addr);
  317. return 0;
  318. } else if (strcmp(argv[1], "loadaddr") == 0) {
  319. load_addr = simple_strtoul(argv[2], NULL, 16);
  320. return 0;
  321. }
  322. #if defined(CONFIG_CMD_NET)
  323. else if (strcmp(argv[1], "bootfile") == 0) {
  324. copy_filename(BootFile, argv[2], sizeof(BootFile));
  325. return 0;
  326. }
  327. #endif
  328. return 0;
  329. }
  330. int setenv(const char *varname, const char *varvalue)
  331. {
  332. const char * const argv[4] = { "setenv", varname, varvalue, NULL };
  333. if (varvalue == NULL || varvalue[0] == '\0')
  334. return _do_env_set(0, 2, (char * const *)argv);
  335. else
  336. return _do_env_set(0, 3, (char * const *)argv);
  337. }
  338. /**
  339. * Set an environment variable to an integer value
  340. *
  341. * @param varname Environmet variable to set
  342. * @param value Value to set it to
  343. * @return 0 if ok, 1 on error
  344. */
  345. int setenv_ulong(const char *varname, ulong value)
  346. {
  347. /* TODO: this should be unsigned */
  348. char *str = simple_itoa(value);
  349. return setenv(varname, str);
  350. }
  351. /**
  352. * Set an environment variable to an address in hex
  353. *
  354. * @param varname Environmet variable to set
  355. * @param addr Value to set it to
  356. * @return 0 if ok, 1 on error
  357. */
  358. int setenv_addr(const char *varname, const void *addr)
  359. {
  360. char str[17];
  361. sprintf(str, "%lx", (uintptr_t)addr);
  362. return setenv(varname, str);
  363. }
  364. int do_env_set(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  365. {
  366. if (argc < 2)
  367. return CMD_RET_USAGE;
  368. return _do_env_set(flag, argc, argv);
  369. }
  370. /*
  371. * Prompt for environment variable
  372. */
  373. #if defined(CONFIG_CMD_ASKENV)
  374. int do_env_ask(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  375. {
  376. char message[CONFIG_SYS_CBSIZE];
  377. int size = CONFIG_SYS_CBSIZE - 1;
  378. int i, len, pos;
  379. char *local_args[4];
  380. local_args[0] = argv[0];
  381. local_args[1] = argv[1];
  382. local_args[2] = NULL;
  383. local_args[3] = NULL;
  384. /* Check the syntax */
  385. switch (argc) {
  386. case 1:
  387. return CMD_RET_USAGE;
  388. case 2: /* env_ask envname */
  389. sprintf(message, "Please enter '%s':", argv[1]);
  390. break;
  391. case 3: /* env_ask envname size */
  392. sprintf(message, "Please enter '%s':", argv[1]);
  393. size = simple_strtoul(argv[2], NULL, 10);
  394. break;
  395. default: /* env_ask envname message1 ... messagen size */
  396. for (i = 2, pos = 0; i < argc - 1; i++) {
  397. if (pos)
  398. message[pos++] = ' ';
  399. strcpy(message + pos, argv[i]);
  400. pos += strlen(argv[i]);
  401. }
  402. message[pos] = '\0';
  403. size = simple_strtoul(argv[argc - 1], NULL, 10);
  404. break;
  405. }
  406. if (size >= CONFIG_SYS_CBSIZE)
  407. size = CONFIG_SYS_CBSIZE - 1;
  408. if (size <= 0)
  409. return 1;
  410. /* prompt for input */
  411. len = readline(message);
  412. if (size < len)
  413. console_buffer[size] = '\0';
  414. len = 2;
  415. if (console_buffer[0] != '\0') {
  416. local_args[2] = console_buffer;
  417. len = 3;
  418. }
  419. /* Continue calling setenv code */
  420. return _do_env_set(flag, len, local_args);
  421. }
  422. #endif
  423. /*
  424. * Interactively edit an environment variable
  425. */
  426. #if defined(CONFIG_CMD_EDITENV)
  427. int do_env_edit(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  428. {
  429. char buffer[CONFIG_SYS_CBSIZE];
  430. char *init_val;
  431. if (argc < 2)
  432. return CMD_RET_USAGE;
  433. /* Set read buffer to initial value or empty sting */
  434. init_val = getenv(argv[1]);
  435. if (init_val)
  436. sprintf(buffer, "%s", init_val);
  437. else
  438. buffer[0] = '\0';
  439. readline_into_buffer("edit: ", buffer, 0);
  440. return setenv(argv[1], buffer);
  441. }
  442. #endif /* CONFIG_CMD_EDITENV */
  443. /*
  444. * Look up variable from environment,
  445. * return address of storage for that variable,
  446. * or NULL if not found
  447. */
  448. char *getenv(const char *name)
  449. {
  450. if (gd->flags & GD_FLG_ENV_READY) { /* after import into hashtable */
  451. ENTRY e, *ep;
  452. WATCHDOG_RESET();
  453. e.key = name;
  454. e.data = NULL;
  455. hsearch_r(e, FIND, &ep, &env_htab);
  456. return ep ? ep->data : NULL;
  457. }
  458. /* restricted capabilities before import */
  459. if (getenv_f(name, (char *)(gd->env_buf), sizeof(gd->env_buf)) > 0)
  460. return (char *)(gd->env_buf);
  461. return NULL;
  462. }
  463. /*
  464. * Look up variable from environment for restricted C runtime env.
  465. */
  466. int getenv_f(const char *name, char *buf, unsigned len)
  467. {
  468. int i, nxt;
  469. for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
  470. int val, n;
  471. for (nxt = i; env_get_char(nxt) != '\0'; ++nxt) {
  472. if (nxt >= CONFIG_ENV_SIZE)
  473. return -1;
  474. }
  475. val = envmatch((uchar *)name, i);
  476. if (val < 0)
  477. continue;
  478. /* found; copy out */
  479. for (n = 0; n < len; ++n, ++buf) {
  480. *buf = env_get_char(val++);
  481. if (*buf == '\0')
  482. return n;
  483. }
  484. if (n)
  485. *--buf = '\0';
  486. printf("env_buf [%d bytes] too small for value of \"%s\"\n",
  487. len, name);
  488. return n;
  489. }
  490. return -1;
  491. }
  492. /**
  493. * Decode the integer value of an environment variable and return it.
  494. *
  495. * @param name Name of environemnt variable
  496. * @param base Number base to use (normally 10, or 16 for hex)
  497. * @param default_val Default value to return if the variable is not
  498. * found
  499. * @return the decoded value, or default_val if not found
  500. */
  501. ulong getenv_ulong(const char *name, int base, ulong default_val)
  502. {
  503. /*
  504. * We can use getenv() here, even before relocation, since the
  505. * environment variable value is an integer and thus short.
  506. */
  507. const char *str = getenv(name);
  508. return str ? simple_strtoul(str, NULL, base) : default_val;
  509. }
  510. #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
  511. int do_env_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  512. {
  513. printf("Saving Environment to %s...\n", env_name_spec);
  514. return saveenv() ? 1 : 0;
  515. }
  516. U_BOOT_CMD(
  517. saveenv, 1, 0, do_env_save,
  518. "save environment variables to persistent storage",
  519. ""
  520. );
  521. #endif
  522. /*
  523. * Match a name / name=value pair
  524. *
  525. * s1 is either a simple 'name', or a 'name=value' pair.
  526. * i2 is the environment index for a 'name2=value2' pair.
  527. * If the names match, return the index for the value2, else -1.
  528. */
  529. int envmatch(uchar *s1, int i2)
  530. {
  531. while (*s1 == env_get_char(i2++))
  532. if (*s1++ == '=')
  533. return i2;
  534. if (*s1 == '\0' && env_get_char(i2-1) == '=')
  535. return i2;
  536. return -1;
  537. }
  538. static int do_env_default(cmd_tbl_t *cmdtp, int flag,
  539. int argc, char * const argv[])
  540. {
  541. if (argc != 2 || strcmp(argv[1], "-f") != 0)
  542. return CMD_RET_USAGE;
  543. set_default_env("## Resetting to default environment\n");
  544. return 0;
  545. }
  546. static int do_env_delete(cmd_tbl_t *cmdtp, int flag,
  547. int argc, char * const argv[])
  548. {
  549. printf("Not implemented yet\n");
  550. return 0;
  551. }
  552. #ifdef CONFIG_CMD_EXPORTENV
  553. /*
  554. * env export [-t | -b | -c] [-s size] addr [var ...]
  555. * -t: export as text format; if size is given, data will be
  556. * padded with '\0' bytes; if not, one terminating '\0'
  557. * will be added (which is included in the "filesize"
  558. * setting so you can for exmple copy this to flash and
  559. * keep the termination).
  560. * -b: export as binary format (name=value pairs separated by
  561. * '\0', list end marked by double "\0\0")
  562. * -c: export as checksum protected environment format as
  563. * used for example by "saveenv" command
  564. * -s size:
  565. * size of output buffer
  566. * addr: memory address where environment gets stored
  567. * var... List of variable names that get included into the
  568. * export. Without arguments, the whole environment gets
  569. * exported.
  570. *
  571. * With "-c" and size is NOT given, then the export command will
  572. * format the data as currently used for the persistent storage,
  573. * i. e. it will use CONFIG_ENV_SECT_SIZE as output block size and
  574. * prepend a valid CRC32 checksum and, in case of resundant
  575. * environment, a "current" redundancy flag. If size is given, this
  576. * value will be used instead of CONFIG_ENV_SECT_SIZE; again, CRC32
  577. * checksum and redundancy flag will be inserted.
  578. *
  579. * With "-b" and "-t", always only the real data (including a
  580. * terminating '\0' byte) will be written; here the optional size
  581. * argument will be used to make sure not to overflow the user
  582. * provided buffer; the command will abort if the size is not
  583. * sufficient. Any remainign space will be '\0' padded.
  584. *
  585. * On successful return, the variable "filesize" will be set.
  586. * Note that filesize includes the trailing/terminating '\0' byte(s).
  587. *
  588. * Usage szenario: create a text snapshot/backup of the current settings:
  589. *
  590. * => env export -t 100000
  591. * => era ${backup_addr} +${filesize}
  592. * => cp.b 100000 ${backup_addr} ${filesize}
  593. *
  594. * Re-import this snapshot, deleting all other settings:
  595. *
  596. * => env import -d -t ${backup_addr}
  597. */
  598. static int do_env_export(cmd_tbl_t *cmdtp, int flag,
  599. int argc, char * const argv[])
  600. {
  601. char buf[32];
  602. char *addr, *cmd, *res;
  603. size_t size = 0;
  604. ssize_t len;
  605. env_t *envp;
  606. char sep = '\n';
  607. int chk = 0;
  608. int fmt = 0;
  609. cmd = *argv;
  610. while (--argc > 0 && **++argv == '-') {
  611. char *arg = *argv;
  612. while (*++arg) {
  613. switch (*arg) {
  614. case 'b': /* raw binary format */
  615. if (fmt++)
  616. goto sep_err;
  617. sep = '\0';
  618. break;
  619. case 'c': /* external checksum format */
  620. if (fmt++)
  621. goto sep_err;
  622. sep = '\0';
  623. chk = 1;
  624. break;
  625. case 's': /* size given */
  626. if (--argc <= 0)
  627. return cmd_usage(cmdtp);
  628. size = simple_strtoul(*++argv, NULL, 16);
  629. goto NXTARG;
  630. case 't': /* text format */
  631. if (fmt++)
  632. goto sep_err;
  633. sep = '\n';
  634. break;
  635. default:
  636. return CMD_RET_USAGE;
  637. }
  638. }
  639. NXTARG: ;
  640. }
  641. if (argc < 1)
  642. return CMD_RET_USAGE;
  643. addr = (char *)simple_strtoul(argv[0], NULL, 16);
  644. if (size)
  645. memset(addr, '\0', size);
  646. argc--;
  647. argv++;
  648. if (sep) { /* export as text file */
  649. len = hexport_r(&env_htab, sep, &addr, size, argc, argv);
  650. if (len < 0) {
  651. error("Cannot export environment: errno = %d\n", errno);
  652. return 1;
  653. }
  654. sprintf(buf, "%zX", (size_t)len);
  655. setenv("filesize", buf);
  656. return 0;
  657. }
  658. envp = (env_t *)addr;
  659. if (chk) /* export as checksum protected block */
  660. res = (char *)envp->data;
  661. else /* export as raw binary data */
  662. res = addr;
  663. len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, argc, argv);
  664. if (len < 0) {
  665. error("Cannot export environment: errno = %d\n", errno);
  666. return 1;
  667. }
  668. if (chk) {
  669. envp->crc = crc32(0, envp->data, ENV_SIZE);
  670. #ifdef CONFIG_ENV_ADDR_REDUND
  671. envp->flags = ACTIVE_FLAG;
  672. #endif
  673. }
  674. sprintf(buf, "%zX", (size_t)(len + offsetof(env_t, data)));
  675. setenv("filesize", buf);
  676. return 0;
  677. sep_err:
  678. printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n", cmd);
  679. return 1;
  680. }
  681. #endif
  682. #ifdef CONFIG_CMD_IMPORTENV
  683. /*
  684. * env import [-d] [-t | -b | -c] addr [size]
  685. * -d: delete existing environment before importing;
  686. * otherwise overwrite / append to existion definitions
  687. * -t: assume text format; either "size" must be given or the
  688. * text data must be '\0' terminated
  689. * -b: assume binary format ('\0' separated, "\0\0" terminated)
  690. * -c: assume checksum protected environment format
  691. * addr: memory address to read from
  692. * size: length of input data; if missing, proper '\0'
  693. * termination is mandatory
  694. */
  695. static int do_env_import(cmd_tbl_t *cmdtp, int flag,
  696. int argc, char * const argv[])
  697. {
  698. char *cmd, *addr;
  699. char sep = '\n';
  700. int chk = 0;
  701. int fmt = 0;
  702. int del = 0;
  703. size_t size;
  704. cmd = *argv;
  705. while (--argc > 0 && **++argv == '-') {
  706. char *arg = *argv;
  707. while (*++arg) {
  708. switch (*arg) {
  709. case 'b': /* raw binary format */
  710. if (fmt++)
  711. goto sep_err;
  712. sep = '\0';
  713. break;
  714. case 'c': /* external checksum format */
  715. if (fmt++)
  716. goto sep_err;
  717. sep = '\0';
  718. chk = 1;
  719. break;
  720. case 't': /* text format */
  721. if (fmt++)
  722. goto sep_err;
  723. sep = '\n';
  724. break;
  725. case 'd':
  726. del = 1;
  727. break;
  728. default:
  729. return CMD_RET_USAGE;
  730. }
  731. }
  732. }
  733. if (argc < 1)
  734. return CMD_RET_USAGE;
  735. if (!fmt)
  736. printf("## Warning: defaulting to text format\n");
  737. addr = (char *)simple_strtoul(argv[0], NULL, 16);
  738. if (argc == 2) {
  739. size = simple_strtoul(argv[1], NULL, 16);
  740. } else {
  741. char *s = addr;
  742. size = 0;
  743. while (size < MAX_ENV_SIZE) {
  744. if ((*s == sep) && (*(s+1) == '\0'))
  745. break;
  746. ++s;
  747. ++size;
  748. }
  749. if (size == MAX_ENV_SIZE) {
  750. printf("## Warning: Input data exceeds %d bytes"
  751. " - truncated\n", MAX_ENV_SIZE);
  752. }
  753. size += 2;
  754. printf("## Info: input data size = %zu = 0x%zX\n", size, size);
  755. }
  756. if (chk) {
  757. uint32_t crc;
  758. env_t *ep = (env_t *)addr;
  759. size -= offsetof(env_t, data);
  760. memcpy(&crc, &ep->crc, sizeof(crc));
  761. if (crc32(0, ep->data, size) != crc) {
  762. puts("## Error: bad CRC, import failed\n");
  763. return 1;
  764. }
  765. addr = (char *)ep->data;
  766. }
  767. if (himport_r(&env_htab, addr, size, sep, del ? 0 : H_NOCLEAR) == 0) {
  768. error("Environment import failed: errno = %d\n", errno);
  769. return 1;
  770. }
  771. gd->flags |= GD_FLG_ENV_READY;
  772. return 0;
  773. sep_err:
  774. printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
  775. cmd);
  776. return 1;
  777. }
  778. #endif
  779. /*
  780. * New command line interface: "env" command with subcommands
  781. */
  782. static cmd_tbl_t cmd_env_sub[] = {
  783. #if defined(CONFIG_CMD_ASKENV)
  784. U_BOOT_CMD_MKENT(ask, CONFIG_SYS_MAXARGS, 1, do_env_ask, "", ""),
  785. #endif
  786. U_BOOT_CMD_MKENT(default, 1, 0, do_env_default, "", ""),
  787. U_BOOT_CMD_MKENT(delete, 2, 0, do_env_delete, "", ""),
  788. #if defined(CONFIG_CMD_EDITENV)
  789. U_BOOT_CMD_MKENT(edit, 2, 0, do_env_edit, "", ""),
  790. #endif
  791. #if defined(CONFIG_CMD_EXPORTENV)
  792. U_BOOT_CMD_MKENT(export, 4, 0, do_env_export, "", ""),
  793. #endif
  794. #if defined(CONFIG_CMD_GREPENV)
  795. U_BOOT_CMD_MKENT(grep, CONFIG_SYS_MAXARGS, 1, do_env_grep, "", ""),
  796. #endif
  797. #if defined(CONFIG_CMD_IMPORTENV)
  798. U_BOOT_CMD_MKENT(import, 5, 0, do_env_import, "", ""),
  799. #endif
  800. U_BOOT_CMD_MKENT(print, CONFIG_SYS_MAXARGS, 1, do_env_print, "", ""),
  801. #if defined(CONFIG_CMD_RUN)
  802. U_BOOT_CMD_MKENT(run, CONFIG_SYS_MAXARGS, 1, do_run, "", ""),
  803. #endif
  804. #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
  805. U_BOOT_CMD_MKENT(save, 1, 0, do_env_save, "", ""),
  806. #endif
  807. U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 0, do_env_set, "", ""),
  808. };
  809. #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  810. void env_reloc(void)
  811. {
  812. fixup_cmdtable(cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
  813. }
  814. #endif
  815. static int do_env(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  816. {
  817. cmd_tbl_t *cp;
  818. if (argc < 2)
  819. return CMD_RET_USAGE;
  820. /* drop initial "env" arg */
  821. argc--;
  822. argv++;
  823. cp = find_cmd_tbl(argv[0], cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
  824. if (cp)
  825. return cp->cmd(cmdtp, flag, argc, argv);
  826. return CMD_RET_USAGE;
  827. }
  828. U_BOOT_CMD(
  829. env, CONFIG_SYS_MAXARGS, 1, do_env,
  830. "environment handling commands",
  831. #if defined(CONFIG_CMD_ASKENV)
  832. "ask name [message] [size] - ask for environment variable\nenv "
  833. #endif
  834. "default -f - reset default environment\n"
  835. #if defined(CONFIG_CMD_EDITENV)
  836. "env edit name - edit environment variable\n"
  837. #endif
  838. "env export [-t | -b | -c] [-s size] addr [var ...] - export environment\n"
  839. #if defined(CONFIG_CMD_GREPENV)
  840. "env grep string [...] - search environment\n"
  841. #endif
  842. "env import [-d] [-t | -b | -c] addr [size] - import environment\n"
  843. "env print [name ...] - print environment\n"
  844. #if defined(CONFIG_CMD_RUN)
  845. "env run var [...] - run commands in an environment variable\n"
  846. #endif
  847. #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
  848. "env save - save environment\n"
  849. #endif
  850. "env set [-f] name [arg ...]\n"
  851. );
  852. /*
  853. * Old command line interface, kept for compatibility
  854. */
  855. #if defined(CONFIG_CMD_EDITENV)
  856. U_BOOT_CMD_COMPLETE(
  857. editenv, 2, 0, do_env_edit,
  858. "edit environment variable",
  859. "name\n"
  860. " - edit environment variable 'name'",
  861. var_complete
  862. );
  863. #endif
  864. U_BOOT_CMD_COMPLETE(
  865. printenv, CONFIG_SYS_MAXARGS, 1, do_env_print,
  866. "print environment variables",
  867. "\n - print values of all environment variables\n"
  868. "printenv name ...\n"
  869. " - print value of environment variable 'name'",
  870. var_complete
  871. );
  872. #ifdef CONFIG_CMD_GREPENV
  873. U_BOOT_CMD_COMPLETE(
  874. grepenv, CONFIG_SYS_MAXARGS, 0, do_env_grep,
  875. "search environment variables",
  876. "string ...\n"
  877. " - list environment name=value pairs matching 'string'",
  878. var_complete
  879. );
  880. #endif
  881. U_BOOT_CMD_COMPLETE(
  882. setenv, CONFIG_SYS_MAXARGS, 0, do_env_set,
  883. "set environment variables",
  884. "name value ...\n"
  885. " - set environment variable 'name' to 'value ...'\n"
  886. "setenv name\n"
  887. " - delete environment variable 'name'",
  888. var_complete
  889. );
  890. #if defined(CONFIG_CMD_ASKENV)
  891. U_BOOT_CMD(
  892. askenv, CONFIG_SYS_MAXARGS, 1, do_env_ask,
  893. "get environment variables from stdin",
  894. "name [message] [size]\n"
  895. " - get environment variable 'name' from stdin (max 'size' chars)\n"
  896. "askenv name\n"
  897. " - get environment variable 'name' from stdin\n"
  898. "askenv name size\n"
  899. " - get environment variable 'name' from stdin (max 'size' chars)\n"
  900. "askenv name [message] size\n"
  901. " - display 'message' string and get environment variable 'name'"
  902. "from stdin (max 'size' chars)"
  903. );
  904. #endif
  905. #if defined(CONFIG_CMD_RUN)
  906. U_BOOT_CMD_COMPLETE(
  907. run, CONFIG_SYS_MAXARGS, 1, do_run,
  908. "run commands in an environment variable",
  909. "var [...]\n"
  910. " - run the commands in the environment variable(s) 'var'",
  911. var_complete
  912. );
  913. #endif