cmd_nvedit.c 29 KB

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