cmd_nvedit.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204
  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. DECLARE_GLOBAL_DATA_PTR;
  52. #if !defined(CONFIG_ENV_IS_IN_EEPROM) && \
  53. !defined(CONFIG_ENV_IS_IN_FLASH) && \
  54. !defined(CONFIG_ENV_IS_IN_DATAFLASH) && \
  55. !defined(CONFIG_ENV_IS_IN_MMC) && \
  56. !defined(CONFIG_ENV_IS_IN_FAT) && \
  57. !defined(CONFIG_ENV_IS_IN_NAND) && \
  58. !defined(CONFIG_ENV_IS_IN_NVRAM) && \
  59. !defined(CONFIG_ENV_IS_IN_ONENAND) && \
  60. !defined(CONFIG_ENV_IS_IN_SPI_FLASH) && \
  61. !defined(CONFIG_ENV_IS_IN_REMOTE) && \
  62. !defined(CONFIG_ENV_IS_NOWHERE)
  63. # error Define one of CONFIG_ENV_IS_IN_{EEPROM|FLASH|DATAFLASH|ONENAND|\
  64. SPI_FLASH|NVRAM|MMC|FAT|REMOTE} or CONFIG_ENV_IS_NOWHERE
  65. #endif
  66. /*
  67. * Maximum expected input data size for import command
  68. */
  69. #define MAX_ENV_SIZE (1 << 20) /* 1 MiB */
  70. ulong load_addr = CONFIG_SYS_LOAD_ADDR; /* Default Load Address */
  71. ulong save_addr; /* Default Save Address */
  72. ulong save_size; /* Default Save Size (in bytes) */
  73. /*
  74. * Table with supported baudrates (defined in config_xyz.h)
  75. */
  76. static const unsigned long baudrate_table[] = CONFIG_SYS_BAUDRATE_TABLE;
  77. #define N_BAUDRATES (sizeof(baudrate_table) / sizeof(baudrate_table[0]))
  78. /*
  79. * This variable is incremented on each do_env_set(), so it can
  80. * be used via get_env_id() as an indication, if the environment
  81. * has changed or not. So it is possible to reread an environment
  82. * variable only if the environment was changed ... done so for
  83. * example in NetInitLoop()
  84. */
  85. static int env_id = 1;
  86. int get_env_id(void)
  87. {
  88. return env_id;
  89. }
  90. #ifndef CONFIG_SPL_BUILD
  91. /*
  92. * Command interface: print one or all environment variables
  93. *
  94. * Returns 0 in case of error, or length of printed string
  95. */
  96. static int env_print(char *name, int flag)
  97. {
  98. char *res = NULL;
  99. size_t len;
  100. if (name) { /* print a single name */
  101. ENTRY e, *ep;
  102. e.key = name;
  103. e.data = NULL;
  104. hsearch_r(e, FIND, &ep, &env_htab, flag);
  105. if (ep == NULL)
  106. return 0;
  107. len = printf("%s=%s\n", ep->key, ep->data);
  108. return len;
  109. }
  110. /* print whole list */
  111. len = hexport_r(&env_htab, '\n', flag, &res, 0, 0, NULL);
  112. if (len > 0) {
  113. puts(res);
  114. free(res);
  115. return len;
  116. }
  117. /* should never happen */
  118. return 0;
  119. }
  120. static int do_env_print(cmd_tbl_t *cmdtp, int flag, int argc,
  121. char * const argv[])
  122. {
  123. int i;
  124. int rcode = 0;
  125. int env_flag = H_HIDE_DOT;
  126. if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'a') {
  127. argc--;
  128. argv++;
  129. env_flag &= ~H_HIDE_DOT;
  130. }
  131. if (argc == 1) {
  132. /* print all env vars */
  133. rcode = env_print(NULL, env_flag);
  134. if (!rcode)
  135. return 1;
  136. printf("\nEnvironment size: %d/%ld bytes\n",
  137. rcode, (ulong)ENV_SIZE);
  138. return 0;
  139. }
  140. /* print selected env vars */
  141. env_flag &= ~H_HIDE_DOT;
  142. for (i = 1; i < argc; ++i) {
  143. int rc = env_print(argv[i], env_flag);
  144. if (!rc) {
  145. printf("## Error: \"%s\" not defined\n", argv[i]);
  146. ++rcode;
  147. }
  148. }
  149. return rcode;
  150. }
  151. #ifdef CONFIG_CMD_GREPENV
  152. static int do_env_grep(cmd_tbl_t *cmdtp, int flag,
  153. int argc, char * const argv[])
  154. {
  155. ENTRY *match;
  156. unsigned char matched[env_htab.size / 8];
  157. int rcode = 1, arg = 1, idx;
  158. if (argc < 2)
  159. return CMD_RET_USAGE;
  160. memset(matched, 0, env_htab.size / 8);
  161. while (arg <= argc) {
  162. idx = 0;
  163. while ((idx = hstrstr_r(argv[arg], idx, &match, &env_htab))) {
  164. if (!(matched[idx / 8] & (1 << (idx & 7)))) {
  165. puts(match->key);
  166. puts("=");
  167. puts(match->data);
  168. puts("\n");
  169. }
  170. matched[idx / 8] |= 1 << (idx & 7);
  171. rcode = 0;
  172. }
  173. arg++;
  174. }
  175. return rcode;
  176. }
  177. #endif
  178. #endif /* CONFIG_SPL_BUILD */
  179. /*
  180. * Perform consistency checking before setting, replacing, or deleting an
  181. * environment variable, then (if successful) apply the changes to internals so
  182. * to make them effective. Code for this function was taken out of
  183. * _do_env_set(), which now calls it instead.
  184. * Also called as a callback function by himport_r().
  185. * Returns 0 in case of success, 1 in case of failure.
  186. * When (flag & H_FORCE) is set, do not print out any error message and force
  187. * overwriting of write-once variables.
  188. */
  189. int env_change_ok(const ENTRY *item, const char *newval, enum env_op op,
  190. int flag)
  191. {
  192. int console = -1;
  193. const char *name;
  194. #if !defined(CONFIG_ENV_OVERWRITE) && defined(CONFIG_OVERWRITE_ETHADDR_ONCE) \
  195. && defined(CONFIG_ETHADDR)
  196. const char *oldval = NULL;
  197. if (op != env_op_create)
  198. oldval = item->data;
  199. #endif
  200. name = item->key;
  201. /* Default value for NULL to protect string-manipulating functions */
  202. newval = newval ? : "";
  203. /* Check for console redirection */
  204. if (strcmp(name, "stdin") == 0)
  205. console = stdin;
  206. else if (strcmp(name, "stdout") == 0)
  207. console = stdout;
  208. else if (strcmp(name, "stderr") == 0)
  209. console = stderr;
  210. if (console != -1 && (gd->flags & GD_FLG_DEVINIT) != 0) {
  211. if ((newval == NULL) || (*newval == '\0')) {
  212. /* We cannot delete stdin/stdout/stderr */
  213. if ((flag & H_FORCE) == 0)
  214. printf("Can't delete \"%s\"\n", name);
  215. return 1;
  216. }
  217. #ifdef CONFIG_CONSOLE_MUX
  218. if (iomux_doenv(console, newval))
  219. return 1;
  220. #else
  221. /* Try assigning specified device */
  222. if (console_assign(console, newval) < 0)
  223. return 1;
  224. #endif /* CONFIG_CONSOLE_MUX */
  225. }
  226. #ifndef CONFIG_ENV_OVERWRITE
  227. /*
  228. * Some variables like "ethaddr" and "serial#" can be set only once and
  229. * cannot be deleted, unless CONFIG_ENV_OVERWRITE is defined.
  230. */
  231. if (op != env_op_create && /* variable exists */
  232. (flag & H_FORCE) == 0) { /* and we are not forced */
  233. if (strcmp(name, "serial#") == 0 ||
  234. (strcmp(name, "ethaddr") == 0
  235. #if defined(CONFIG_OVERWRITE_ETHADDR_ONCE) && defined(CONFIG_ETHADDR)
  236. && strcmp(oldval, __stringify(CONFIG_ETHADDR)) != 0
  237. #endif /* CONFIG_OVERWRITE_ETHADDR_ONCE && CONFIG_ETHADDR */
  238. )) {
  239. printf("Can't overwrite \"%s\"\n", name);
  240. return 1;
  241. }
  242. }
  243. #endif
  244. /*
  245. * When we change baudrate, or we are doing an env default -a
  246. * (which will erase all variables prior to calling this),
  247. * we want the baudrate to actually change - for real.
  248. */
  249. if (op != env_op_create || /* variable exists */
  250. (flag & H_NOCLEAR) == 0) { /* or env is clear */
  251. /*
  252. * Switch to new baudrate if new baudrate is supported
  253. */
  254. if (strcmp(name, "baudrate") == 0) {
  255. int baudrate = simple_strtoul(newval, NULL, 10);
  256. int i;
  257. for (i = 0; i < N_BAUDRATES; ++i) {
  258. if (baudrate == baudrate_table[i])
  259. break;
  260. }
  261. if (i == N_BAUDRATES) {
  262. if ((flag & H_FORCE) == 0)
  263. printf("## Baudrate %d bps not "
  264. "supported\n", baudrate);
  265. return 1;
  266. }
  267. if (gd->baudrate == baudrate) {
  268. /* If unchanged, we just say it's OK */
  269. return 0;
  270. }
  271. printf("## Switch baudrate to %d bps and"
  272. "press ENTER ...\n", baudrate);
  273. udelay(50000);
  274. gd->baudrate = baudrate;
  275. #if defined(CONFIG_PPC) || defined(CONFIG_MCF52x2)
  276. gd->bd->bi_baudrate = baudrate;
  277. #endif
  278. serial_setbrg();
  279. udelay(50000);
  280. while (getc() != '\r')
  281. ;
  282. }
  283. }
  284. /*
  285. * Some variables should be updated when the corresponding
  286. * entry in the environment is changed
  287. */
  288. if (strcmp(name, "loadaddr") == 0) {
  289. load_addr = simple_strtoul(newval, NULL, 16);
  290. return 0;
  291. }
  292. return 0;
  293. }
  294. /*
  295. * Set a new environment variable,
  296. * or replace or delete an existing one.
  297. */
  298. static int _do_env_set(int flag, int argc, char * const argv[])
  299. {
  300. int i, len;
  301. char *name, *value, *s;
  302. ENTRY e, *ep;
  303. name = argv[1];
  304. value = argv[2];
  305. if (strchr(name, '=')) {
  306. printf("## Error: illegal character '='"
  307. "in variable name \"%s\"\n", name);
  308. return 1;
  309. }
  310. env_id++;
  311. /* Delete only ? */
  312. if (argc < 3 || argv[2] == NULL) {
  313. int rc = hdelete_r(name, &env_htab, H_INTERACTIVE);
  314. return !rc;
  315. }
  316. /*
  317. * Insert / replace new value
  318. */
  319. for (i = 2, len = 0; i < argc; ++i)
  320. len += strlen(argv[i]) + 1;
  321. value = malloc(len);
  322. if (value == NULL) {
  323. printf("## Can't malloc %d bytes\n", len);
  324. return 1;
  325. }
  326. for (i = 2, s = value; i < argc; ++i) {
  327. char *v = argv[i];
  328. while ((*s++ = *v++) != '\0')
  329. ;
  330. *(s - 1) = ' ';
  331. }
  332. if (s != value)
  333. *--s = '\0';
  334. e.key = name;
  335. e.data = value;
  336. hsearch_r(e, ENTER, &ep, &env_htab, H_INTERACTIVE);
  337. free(value);
  338. if (!ep) {
  339. printf("## Error inserting \"%s\" variable, errno=%d\n",
  340. name, errno);
  341. return 1;
  342. }
  343. return 0;
  344. }
  345. int setenv(const char *varname, const char *varvalue)
  346. {
  347. const char * const argv[4] = { "setenv", varname, varvalue, NULL };
  348. if (varvalue == NULL || varvalue[0] == '\0')
  349. return _do_env_set(0, 2, (char * const *)argv);
  350. else
  351. return _do_env_set(0, 3, (char * const *)argv);
  352. }
  353. /**
  354. * Set an environment variable to an integer value
  355. *
  356. * @param varname Environmet variable to set
  357. * @param value Value to set it to
  358. * @return 0 if ok, 1 on error
  359. */
  360. int setenv_ulong(const char *varname, ulong value)
  361. {
  362. /* TODO: this should be unsigned */
  363. char *str = simple_itoa(value);
  364. return setenv(varname, str);
  365. }
  366. /**
  367. * Set an environment variable to an address in hex
  368. *
  369. * @param varname Environmet variable to set
  370. * @param addr Value to set it to
  371. * @return 0 if ok, 1 on error
  372. */
  373. int setenv_addr(const char *varname, const void *addr)
  374. {
  375. char str[17];
  376. sprintf(str, "%lx", (uintptr_t)addr);
  377. return setenv(varname, str);
  378. }
  379. #ifndef CONFIG_SPL_BUILD
  380. static int do_env_set(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  381. {
  382. if (argc < 2)
  383. return CMD_RET_USAGE;
  384. return _do_env_set(flag, argc, argv);
  385. }
  386. /*
  387. * Prompt for environment variable
  388. */
  389. #if defined(CONFIG_CMD_ASKENV)
  390. int do_env_ask(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  391. {
  392. char message[CONFIG_SYS_CBSIZE];
  393. int size = CONFIG_SYS_CBSIZE - 1;
  394. int i, len, pos;
  395. char *local_args[4];
  396. local_args[0] = argv[0];
  397. local_args[1] = argv[1];
  398. local_args[2] = NULL;
  399. local_args[3] = NULL;
  400. /* Check the syntax */
  401. switch (argc) {
  402. case 1:
  403. return CMD_RET_USAGE;
  404. case 2: /* env_ask envname */
  405. sprintf(message, "Please enter '%s':", argv[1]);
  406. break;
  407. case 3: /* env_ask envname size */
  408. sprintf(message, "Please enter '%s':", argv[1]);
  409. size = simple_strtoul(argv[2], NULL, 10);
  410. break;
  411. default: /* env_ask envname message1 ... messagen size */
  412. for (i = 2, pos = 0; i < argc - 1; i++) {
  413. if (pos)
  414. message[pos++] = ' ';
  415. strcpy(message + pos, argv[i]);
  416. pos += strlen(argv[i]);
  417. }
  418. message[pos] = '\0';
  419. size = simple_strtoul(argv[argc - 1], NULL, 10);
  420. break;
  421. }
  422. if (size >= CONFIG_SYS_CBSIZE)
  423. size = CONFIG_SYS_CBSIZE - 1;
  424. if (size <= 0)
  425. return 1;
  426. /* prompt for input */
  427. len = readline(message);
  428. if (size < len)
  429. console_buffer[size] = '\0';
  430. len = 2;
  431. if (console_buffer[0] != '\0') {
  432. local_args[2] = console_buffer;
  433. len = 3;
  434. }
  435. /* Continue calling setenv code */
  436. return _do_env_set(flag, len, local_args);
  437. }
  438. #endif
  439. #if defined(CONFIG_CMD_ENV_CALLBACK)
  440. static int print_static_binding(const char *var_name, const char *callback_name)
  441. {
  442. printf("\t%-20s %-20s\n", var_name, callback_name);
  443. return 0;
  444. }
  445. static int print_active_callback(ENTRY *entry)
  446. {
  447. struct env_clbk_tbl *clbkp;
  448. int i;
  449. int num_callbacks;
  450. if (entry->callback == NULL)
  451. return 0;
  452. /* look up the callback in the linker-list */
  453. num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
  454. for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
  455. i < num_callbacks;
  456. i++, clbkp++) {
  457. #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  458. if (entry->callback == clbkp->callback + gd->reloc_off)
  459. #else
  460. if (entry->callback == clbkp->callback)
  461. #endif
  462. break;
  463. }
  464. if (i == num_callbacks)
  465. /* this should probably never happen, but just in case... */
  466. printf("\t%-20s %p\n", entry->key, entry->callback);
  467. else
  468. printf("\t%-20s %-20s\n", entry->key, clbkp->name);
  469. return 0;
  470. }
  471. /*
  472. * Print the callbacks available and what they are bound to
  473. */
  474. int do_env_callback(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  475. {
  476. struct env_clbk_tbl *clbkp;
  477. int i;
  478. int num_callbacks;
  479. /* Print the available callbacks */
  480. puts("Available callbacks:\n");
  481. puts("\tCallback Name\n");
  482. puts("\t-------------\n");
  483. num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
  484. for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
  485. i < num_callbacks;
  486. i++, clbkp++)
  487. printf("\t%s\n", clbkp->name);
  488. puts("\n");
  489. /* Print the static bindings that may exist */
  490. puts("Static callback bindings:\n");
  491. printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
  492. printf("\t%-20s %-20s\n", "-------------", "-------------");
  493. env_attr_walk(ENV_CALLBACK_LIST_STATIC, print_static_binding);
  494. puts("\n");
  495. /* walk through each variable and print the callback if it has one */
  496. puts("Active callback bindings:\n");
  497. printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
  498. printf("\t%-20s %-20s\n", "-------------", "-------------");
  499. hwalk_r(&env_htab, print_active_callback);
  500. return 0;
  501. }
  502. #endif
  503. /*
  504. * Interactively edit an environment variable
  505. */
  506. #if defined(CONFIG_CMD_EDITENV)
  507. static int do_env_edit(cmd_tbl_t *cmdtp, int flag, int argc,
  508. char * const argv[])
  509. {
  510. char buffer[CONFIG_SYS_CBSIZE];
  511. char *init_val;
  512. if (argc < 2)
  513. return CMD_RET_USAGE;
  514. /* Set read buffer to initial value or empty sting */
  515. init_val = getenv(argv[1]);
  516. if (init_val)
  517. sprintf(buffer, "%s", init_val);
  518. else
  519. buffer[0] = '\0';
  520. readline_into_buffer("edit: ", buffer, 0);
  521. return setenv(argv[1], buffer);
  522. }
  523. #endif /* CONFIG_CMD_EDITENV */
  524. #endif /* CONFIG_SPL_BUILD */
  525. /*
  526. * Look up variable from environment,
  527. * return address of storage for that variable,
  528. * or NULL if not found
  529. */
  530. char *getenv(const char *name)
  531. {
  532. if (gd->flags & GD_FLG_ENV_READY) { /* after import into hashtable */
  533. ENTRY e, *ep;
  534. WATCHDOG_RESET();
  535. e.key = name;
  536. e.data = NULL;
  537. hsearch_r(e, FIND, &ep, &env_htab, 0);
  538. return ep ? ep->data : NULL;
  539. }
  540. /* restricted capabilities before import */
  541. if (getenv_f(name, (char *)(gd->env_buf), sizeof(gd->env_buf)) > 0)
  542. return (char *)(gd->env_buf);
  543. return NULL;
  544. }
  545. /*
  546. * Look up variable from environment for restricted C runtime env.
  547. */
  548. int getenv_f(const char *name, char *buf, unsigned len)
  549. {
  550. int i, nxt;
  551. for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
  552. int val, n;
  553. for (nxt = i; env_get_char(nxt) != '\0'; ++nxt) {
  554. if (nxt >= CONFIG_ENV_SIZE)
  555. return -1;
  556. }
  557. val = envmatch((uchar *)name, i);
  558. if (val < 0)
  559. continue;
  560. /* found; copy out */
  561. for (n = 0; n < len; ++n, ++buf) {
  562. *buf = env_get_char(val++);
  563. if (*buf == '\0')
  564. return n;
  565. }
  566. if (n)
  567. *--buf = '\0';
  568. printf("env_buf [%d bytes] too small for value of \"%s\"\n",
  569. len, name);
  570. return n;
  571. }
  572. return -1;
  573. }
  574. /**
  575. * Decode the integer value of an environment variable and return it.
  576. *
  577. * @param name Name of environemnt variable
  578. * @param base Number base to use (normally 10, or 16 for hex)
  579. * @param default_val Default value to return if the variable is not
  580. * found
  581. * @return the decoded value, or default_val if not found
  582. */
  583. ulong getenv_ulong(const char *name, int base, ulong default_val)
  584. {
  585. /*
  586. * We can use getenv() here, even before relocation, since the
  587. * environment variable value is an integer and thus short.
  588. */
  589. const char *str = getenv(name);
  590. return str ? simple_strtoul(str, NULL, base) : default_val;
  591. }
  592. #ifndef CONFIG_SPL_BUILD
  593. #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
  594. static int do_env_save(cmd_tbl_t *cmdtp, int flag, int argc,
  595. char * const argv[])
  596. {
  597. printf("Saving Environment to %s...\n", env_name_spec);
  598. return saveenv() ? 1 : 0;
  599. }
  600. U_BOOT_CMD(
  601. saveenv, 1, 0, do_env_save,
  602. "save environment variables to persistent storage",
  603. ""
  604. );
  605. #endif
  606. #endif /* CONFIG_SPL_BUILD */
  607. /*
  608. * Match a name / name=value pair
  609. *
  610. * s1 is either a simple 'name', or a 'name=value' pair.
  611. * i2 is the environment index for a 'name2=value2' pair.
  612. * If the names match, return the index for the value2, else -1.
  613. */
  614. int envmatch(uchar *s1, int i2)
  615. {
  616. if (s1 == NULL)
  617. return -1;
  618. while (*s1 == env_get_char(i2++))
  619. if (*s1++ == '=')
  620. return i2;
  621. if (*s1 == '\0' && env_get_char(i2-1) == '=')
  622. return i2;
  623. return -1;
  624. }
  625. #ifndef CONFIG_SPL_BUILD
  626. static int do_env_default(cmd_tbl_t *cmdtp, int __flag,
  627. int argc, char * const argv[])
  628. {
  629. int all = 0, flag = 0;
  630. debug("Initial value for argc=%d\n", argc);
  631. while (--argc > 0 && **++argv == '-') {
  632. char *arg = *argv;
  633. while (*++arg) {
  634. switch (*arg) {
  635. case 'a': /* default all */
  636. all = 1;
  637. break;
  638. case 'f': /* force */
  639. flag |= H_FORCE;
  640. break;
  641. default:
  642. return cmd_usage(cmdtp);
  643. }
  644. }
  645. }
  646. debug("Final value for argc=%d\n", argc);
  647. if (all && (argc == 0)) {
  648. /* Reset the whole environment */
  649. set_default_env("## Resetting to default environment\n");
  650. return 0;
  651. }
  652. if (!all && (argc > 0)) {
  653. /* Reset individual variables */
  654. set_default_vars(argc, argv);
  655. return 0;
  656. }
  657. return cmd_usage(cmdtp);
  658. }
  659. static int do_env_delete(cmd_tbl_t *cmdtp, int flag,
  660. int argc, char * const argv[])
  661. {
  662. printf("Not implemented yet\n");
  663. return 0;
  664. }
  665. #ifdef CONFIG_CMD_EXPORTENV
  666. /*
  667. * env export [-t | -b | -c] [-s size] addr [var ...]
  668. * -t: export as text format; if size is given, data will be
  669. * padded with '\0' bytes; if not, one terminating '\0'
  670. * will be added (which is included in the "filesize"
  671. * setting so you can for exmple copy this to flash and
  672. * keep the termination).
  673. * -b: export as binary format (name=value pairs separated by
  674. * '\0', list end marked by double "\0\0")
  675. * -c: export as checksum protected environment format as
  676. * used for example by "saveenv" command
  677. * -s size:
  678. * size of output buffer
  679. * addr: memory address where environment gets stored
  680. * var... List of variable names that get included into the
  681. * export. Without arguments, the whole environment gets
  682. * exported.
  683. *
  684. * With "-c" and size is NOT given, then the export command will
  685. * format the data as currently used for the persistent storage,
  686. * i. e. it will use CONFIG_ENV_SECT_SIZE as output block size and
  687. * prepend a valid CRC32 checksum and, in case of resundant
  688. * environment, a "current" redundancy flag. If size is given, this
  689. * value will be used instead of CONFIG_ENV_SECT_SIZE; again, CRC32
  690. * checksum and redundancy flag will be inserted.
  691. *
  692. * With "-b" and "-t", always only the real data (including a
  693. * terminating '\0' byte) will be written; here the optional size
  694. * argument will be used to make sure not to overflow the user
  695. * provided buffer; the command will abort if the size is not
  696. * sufficient. Any remainign space will be '\0' padded.
  697. *
  698. * On successful return, the variable "filesize" will be set.
  699. * Note that filesize includes the trailing/terminating '\0' byte(s).
  700. *
  701. * Usage szenario: create a text snapshot/backup of the current settings:
  702. *
  703. * => env export -t 100000
  704. * => era ${backup_addr} +${filesize}
  705. * => cp.b 100000 ${backup_addr} ${filesize}
  706. *
  707. * Re-import this snapshot, deleting all other settings:
  708. *
  709. * => env import -d -t ${backup_addr}
  710. */
  711. static int do_env_export(cmd_tbl_t *cmdtp, int flag,
  712. int argc, char * const argv[])
  713. {
  714. char buf[32];
  715. char *addr, *cmd, *res;
  716. size_t size = 0;
  717. ssize_t len;
  718. env_t *envp;
  719. char sep = '\n';
  720. int chk = 0;
  721. int fmt = 0;
  722. cmd = *argv;
  723. while (--argc > 0 && **++argv == '-') {
  724. char *arg = *argv;
  725. while (*++arg) {
  726. switch (*arg) {
  727. case 'b': /* raw binary format */
  728. if (fmt++)
  729. goto sep_err;
  730. sep = '\0';
  731. break;
  732. case 'c': /* external checksum format */
  733. if (fmt++)
  734. goto sep_err;
  735. sep = '\0';
  736. chk = 1;
  737. break;
  738. case 's': /* size given */
  739. if (--argc <= 0)
  740. return cmd_usage(cmdtp);
  741. size = simple_strtoul(*++argv, NULL, 16);
  742. goto NXTARG;
  743. case 't': /* text format */
  744. if (fmt++)
  745. goto sep_err;
  746. sep = '\n';
  747. break;
  748. default:
  749. return CMD_RET_USAGE;
  750. }
  751. }
  752. NXTARG: ;
  753. }
  754. if (argc < 1)
  755. return CMD_RET_USAGE;
  756. addr = (char *)simple_strtoul(argv[0], NULL, 16);
  757. if (size)
  758. memset(addr, '\0', size);
  759. argc--;
  760. argv++;
  761. if (sep) { /* export as text file */
  762. len = hexport_r(&env_htab, sep, 0, &addr, size, argc, argv);
  763. if (len < 0) {
  764. error("Cannot export environment: errno = %d\n", errno);
  765. return 1;
  766. }
  767. sprintf(buf, "%zX", (size_t)len);
  768. setenv("filesize", buf);
  769. return 0;
  770. }
  771. envp = (env_t *)addr;
  772. if (chk) /* export as checksum protected block */
  773. res = (char *)envp->data;
  774. else /* export as raw binary data */
  775. res = addr;
  776. len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, argc, argv);
  777. if (len < 0) {
  778. error("Cannot export environment: errno = %d\n", errno);
  779. return 1;
  780. }
  781. if (chk) {
  782. envp->crc = crc32(0, envp->data, ENV_SIZE);
  783. #ifdef CONFIG_ENV_ADDR_REDUND
  784. envp->flags = ACTIVE_FLAG;
  785. #endif
  786. }
  787. sprintf(buf, "%zX", (size_t)(len + offsetof(env_t, data)));
  788. setenv("filesize", buf);
  789. return 0;
  790. sep_err:
  791. printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n", cmd);
  792. return 1;
  793. }
  794. #endif
  795. #ifdef CONFIG_CMD_IMPORTENV
  796. /*
  797. * env import [-d] [-t | -b | -c] addr [size]
  798. * -d: delete existing environment before importing;
  799. * otherwise overwrite / append to existion definitions
  800. * -t: assume text format; either "size" must be given or the
  801. * text data must be '\0' terminated
  802. * -b: assume binary format ('\0' separated, "\0\0" terminated)
  803. * -c: assume checksum protected environment format
  804. * addr: memory address to read from
  805. * size: length of input data; if missing, proper '\0'
  806. * termination is mandatory
  807. */
  808. static int do_env_import(cmd_tbl_t *cmdtp, int flag,
  809. int argc, char * const argv[])
  810. {
  811. char *cmd, *addr;
  812. char sep = '\n';
  813. int chk = 0;
  814. int fmt = 0;
  815. int del = 0;
  816. size_t size;
  817. cmd = *argv;
  818. while (--argc > 0 && **++argv == '-') {
  819. char *arg = *argv;
  820. while (*++arg) {
  821. switch (*arg) {
  822. case 'b': /* raw binary format */
  823. if (fmt++)
  824. goto sep_err;
  825. sep = '\0';
  826. break;
  827. case 'c': /* external checksum format */
  828. if (fmt++)
  829. goto sep_err;
  830. sep = '\0';
  831. chk = 1;
  832. break;
  833. case 't': /* text format */
  834. if (fmt++)
  835. goto sep_err;
  836. sep = '\n';
  837. break;
  838. case 'd':
  839. del = 1;
  840. break;
  841. default:
  842. return CMD_RET_USAGE;
  843. }
  844. }
  845. }
  846. if (argc < 1)
  847. return CMD_RET_USAGE;
  848. if (!fmt)
  849. printf("## Warning: defaulting to text format\n");
  850. addr = (char *)simple_strtoul(argv[0], NULL, 16);
  851. if (argc == 2) {
  852. size = simple_strtoul(argv[1], NULL, 16);
  853. } else {
  854. char *s = addr;
  855. size = 0;
  856. while (size < MAX_ENV_SIZE) {
  857. if ((*s == sep) && (*(s+1) == '\0'))
  858. break;
  859. ++s;
  860. ++size;
  861. }
  862. if (size == MAX_ENV_SIZE) {
  863. printf("## Warning: Input data exceeds %d bytes"
  864. " - truncated\n", MAX_ENV_SIZE);
  865. }
  866. size += 2;
  867. printf("## Info: input data size = %zu = 0x%zX\n", size, size);
  868. }
  869. if (chk) {
  870. uint32_t crc;
  871. env_t *ep = (env_t *)addr;
  872. size -= offsetof(env_t, data);
  873. memcpy(&crc, &ep->crc, sizeof(crc));
  874. if (crc32(0, ep->data, size) != crc) {
  875. puts("## Error: bad CRC, import failed\n");
  876. return 1;
  877. }
  878. addr = (char *)ep->data;
  879. }
  880. if (himport_r(&env_htab, addr, size, sep, del ? 0 : H_NOCLEAR,
  881. 0, NULL) == 0) {
  882. error("Environment import failed: errno = %d\n", errno);
  883. return 1;
  884. }
  885. gd->flags |= GD_FLG_ENV_READY;
  886. return 0;
  887. sep_err:
  888. printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
  889. cmd);
  890. return 1;
  891. }
  892. #endif
  893. /*
  894. * New command line interface: "env" command with subcommands
  895. */
  896. static cmd_tbl_t cmd_env_sub[] = {
  897. #if defined(CONFIG_CMD_ASKENV)
  898. U_BOOT_CMD_MKENT(ask, CONFIG_SYS_MAXARGS, 1, do_env_ask, "", ""),
  899. #endif
  900. U_BOOT_CMD_MKENT(default, 1, 0, do_env_default, "", ""),
  901. U_BOOT_CMD_MKENT(delete, 2, 0, do_env_delete, "", ""),
  902. #if defined(CONFIG_CMD_EDITENV)
  903. U_BOOT_CMD_MKENT(edit, 2, 0, do_env_edit, "", ""),
  904. #endif
  905. #if defined(CONFIG_CMD_ENV_CALLBACK)
  906. U_BOOT_CMD_MKENT(callbacks, 1, 0, do_env_callback, "", ""),
  907. #endif
  908. #if defined(CONFIG_CMD_EXPORTENV)
  909. U_BOOT_CMD_MKENT(export, 4, 0, do_env_export, "", ""),
  910. #endif
  911. #if defined(CONFIG_CMD_GREPENV)
  912. U_BOOT_CMD_MKENT(grep, CONFIG_SYS_MAXARGS, 1, do_env_grep, "", ""),
  913. #endif
  914. #if defined(CONFIG_CMD_IMPORTENV)
  915. U_BOOT_CMD_MKENT(import, 5, 0, do_env_import, "", ""),
  916. #endif
  917. U_BOOT_CMD_MKENT(print, CONFIG_SYS_MAXARGS, 1, do_env_print, "", ""),
  918. #if defined(CONFIG_CMD_RUN)
  919. U_BOOT_CMD_MKENT(run, CONFIG_SYS_MAXARGS, 1, do_run, "", ""),
  920. #endif
  921. #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
  922. U_BOOT_CMD_MKENT(save, 1, 0, do_env_save, "", ""),
  923. #endif
  924. U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 0, do_env_set, "", ""),
  925. };
  926. #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  927. void env_reloc(void)
  928. {
  929. fixup_cmdtable(cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
  930. }
  931. #endif
  932. static int do_env(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  933. {
  934. cmd_tbl_t *cp;
  935. if (argc < 2)
  936. return CMD_RET_USAGE;
  937. /* drop initial "env" arg */
  938. argc--;
  939. argv++;
  940. cp = find_cmd_tbl(argv[0], cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
  941. if (cp)
  942. return cp->cmd(cmdtp, flag, argc, argv);
  943. return CMD_RET_USAGE;
  944. }
  945. #ifdef CONFIG_SYS_LONGHELP
  946. static char env_help_text[] =
  947. #if defined(CONFIG_CMD_ASKENV)
  948. "ask name [message] [size] - ask for environment variable\nenv "
  949. #endif
  950. #if defined(CONFIG_CMD_ENV_CALLBACK)
  951. "callbacks - print callbacks and their associated variables\nenv "
  952. #endif
  953. "default [-f] -a - [forcibly] reset default environment\n"
  954. "env default [-f] var [...] - [forcibly] reset variable(s) to their default values\n"
  955. #if defined(CONFIG_CMD_EDITENV)
  956. "env edit name - edit environment variable\n"
  957. #endif
  958. #if defined(CONFIG_CMD_EXPORTENV)
  959. "env export [-t | -b | -c] [-s size] addr [var ...] - export environment\n"
  960. #endif
  961. #if defined(CONFIG_CMD_GREPENV)
  962. "env grep string [...] - search environment\n"
  963. #endif
  964. #if defined(CONFIG_CMD_IMPORTENV)
  965. "env import [-d] [-t | -b | -c] addr [size] - import environment\n"
  966. #endif
  967. "env print [-a | name ...] - print environment\n"
  968. #if defined(CONFIG_CMD_RUN)
  969. "env run var [...] - run commands in an environment variable\n"
  970. #endif
  971. #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
  972. "env save - save environment\n"
  973. #endif
  974. "env set [-f] name [arg ...]\n";
  975. #endif
  976. U_BOOT_CMD(
  977. env, CONFIG_SYS_MAXARGS, 1, do_env,
  978. "environment handling commands", env_help_text
  979. );
  980. /*
  981. * Old command line interface, kept for compatibility
  982. */
  983. #if defined(CONFIG_CMD_EDITENV)
  984. U_BOOT_CMD_COMPLETE(
  985. editenv, 2, 0, do_env_edit,
  986. "edit environment variable",
  987. "name\n"
  988. " - edit environment variable 'name'",
  989. var_complete
  990. );
  991. #endif
  992. U_BOOT_CMD_COMPLETE(
  993. printenv, CONFIG_SYS_MAXARGS, 1, do_env_print,
  994. "print environment variables",
  995. "[-a]\n - print [all] values of all environment variables\n"
  996. "printenv name ...\n"
  997. " - print value of environment variable 'name'",
  998. var_complete
  999. );
  1000. #ifdef CONFIG_CMD_GREPENV
  1001. U_BOOT_CMD_COMPLETE(
  1002. grepenv, CONFIG_SYS_MAXARGS, 0, do_env_grep,
  1003. "search environment variables",
  1004. "string ...\n"
  1005. " - list environment name=value pairs matching 'string'",
  1006. var_complete
  1007. );
  1008. #endif
  1009. U_BOOT_CMD_COMPLETE(
  1010. setenv, CONFIG_SYS_MAXARGS, 0, do_env_set,
  1011. "set environment variables",
  1012. "name value ...\n"
  1013. " - set environment variable 'name' to 'value ...'\n"
  1014. "setenv name\n"
  1015. " - delete environment variable 'name'",
  1016. var_complete
  1017. );
  1018. #if defined(CONFIG_CMD_ASKENV)
  1019. U_BOOT_CMD(
  1020. askenv, CONFIG_SYS_MAXARGS, 1, do_env_ask,
  1021. "get environment variables from stdin",
  1022. "name [message] [size]\n"
  1023. " - get environment variable 'name' from stdin (max 'size' chars)\n"
  1024. "askenv name\n"
  1025. " - get environment variable 'name' from stdin\n"
  1026. "askenv name size\n"
  1027. " - get environment variable 'name' from stdin (max 'size' chars)\n"
  1028. "askenv name [message] size\n"
  1029. " - display 'message' string and get environment variable 'name'"
  1030. "from stdin (max 'size' chars)"
  1031. );
  1032. #endif
  1033. #if defined(CONFIG_CMD_RUN)
  1034. U_BOOT_CMD_COMPLETE(
  1035. run, CONFIG_SYS_MAXARGS, 1, do_run,
  1036. "run commands in an environment variable",
  1037. "var [...]\n"
  1038. " - run the commands in the environment variable(s) 'var'",
  1039. var_complete
  1040. );
  1041. #endif
  1042. #endif /* CONFIG_SPL_BUILD */