common.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2000-2010
  4. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  5. *
  6. * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  7. * Andreas Heppel <aheppel@sysgo.de>
  8. */
  9. #include <common.h>
  10. #include <bootstage.h>
  11. #include <command.h>
  12. #include <env.h>
  13. #include <env_internal.h>
  14. #include <log.h>
  15. #include <sort.h>
  16. #include <asm/global_data.h>
  17. #include <linux/printk.h>
  18. #include <linux/stddef.h>
  19. #include <search.h>
  20. #include <errno.h>
  21. #include <malloc.h>
  22. #include <u-boot/crc.h>
  23. #include <dm/ofnode.h>
  24. #include <net.h>
  25. #include <watchdog.h>
  26. DECLARE_GLOBAL_DATA_PTR;
  27. /************************************************************************
  28. * Default settings to be used when no valid environment is found
  29. */
  30. #include <env_default.h>
  31. struct hsearch_data env_htab = {
  32. .change_ok = env_flags_validate,
  33. };
  34. /*
  35. * This variable is incremented each time we set an environment variable so we
  36. * can be check via env_get_id() to see if the environment has changed or not.
  37. * This makes it possible to reread an environment variable only if the
  38. * environment was changed, typically used by networking code.
  39. */
  40. static int env_id = 1;
  41. int env_get_id(void)
  42. {
  43. return env_id;
  44. }
  45. void env_inc_id(void)
  46. {
  47. env_id++;
  48. }
  49. int env_do_env_set(int flag, int argc, char *const argv[], int env_flag)
  50. {
  51. int i, len;
  52. char *name, *value, *s;
  53. struct env_entry e, *ep;
  54. debug("Initial value for argc=%d\n", argc);
  55. #if !IS_ENABLED(CONFIG_SPL_BUILD) && IS_ENABLED(CONFIG_CMD_NVEDIT_EFI)
  56. if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'e')
  57. return do_env_set_efi(NULL, flag, --argc, ++argv);
  58. #endif
  59. while (argc > 1 && **(argv + 1) == '-') {
  60. char *arg = *++argv;
  61. --argc;
  62. while (*++arg) {
  63. switch (*arg) {
  64. case 'f': /* force */
  65. env_flag |= H_FORCE;
  66. break;
  67. default:
  68. return CMD_RET_USAGE;
  69. }
  70. }
  71. }
  72. debug("Final value for argc=%d\n", argc);
  73. name = argv[1];
  74. if (strchr(name, '=')) {
  75. printf("## Error: illegal character '=' "
  76. "in variable name \"%s\"\n", name);
  77. return 1;
  78. }
  79. env_inc_id();
  80. /* Delete only ? */
  81. if (argc < 3 || argv[2] == NULL) {
  82. int rc = hdelete_r(name, &env_htab, env_flag);
  83. /* If the variable didn't exist, don't report an error */
  84. return rc && rc != -ENOENT ? 1 : 0;
  85. }
  86. /*
  87. * Insert / replace new value
  88. */
  89. for (i = 2, len = 0; i < argc; ++i)
  90. len += strlen(argv[i]) + 1;
  91. value = malloc(len);
  92. if (value == NULL) {
  93. printf("## Can't malloc %d bytes\n", len);
  94. return 1;
  95. }
  96. for (i = 2, s = value; i < argc; ++i) {
  97. char *v = argv[i];
  98. while ((*s++ = *v++) != '\0')
  99. ;
  100. *(s - 1) = ' ';
  101. }
  102. if (s != value)
  103. *--s = '\0';
  104. e.key = name;
  105. e.data = value;
  106. hsearch_r(e, ENV_ENTER, &ep, &env_htab, env_flag);
  107. free(value);
  108. if (!ep) {
  109. printf("## Error inserting \"%s\" variable, errno=%d\n",
  110. name, errno);
  111. return 1;
  112. }
  113. return 0;
  114. }
  115. int env_set(const char *varname, const char *varvalue)
  116. {
  117. const char * const argv[4] = { "setenv", varname, varvalue, NULL };
  118. /* before import into hashtable */
  119. if (!(gd->flags & GD_FLG_ENV_READY))
  120. return 1;
  121. if (varvalue == NULL || varvalue[0] == '\0')
  122. return env_do_env_set(0, 2, (char * const *)argv, H_PROGRAMMATIC);
  123. else
  124. return env_do_env_set(0, 3, (char * const *)argv, H_PROGRAMMATIC);
  125. }
  126. /**
  127. * Set an environment variable to an integer value
  128. *
  129. * @param varname Environment variable to set
  130. * @param value Value to set it to
  131. * Return: 0 if ok, 1 on error
  132. */
  133. int env_set_ulong(const char *varname, ulong value)
  134. {
  135. /* TODO: this should be unsigned */
  136. char *str = simple_itoa(value);
  137. return env_set(varname, str);
  138. }
  139. /**
  140. * Set an environment variable to an value in hex
  141. *
  142. * @param varname Environment variable to set
  143. * @param value Value to set it to
  144. * Return: 0 if ok, 1 on error
  145. */
  146. int env_set_hex(const char *varname, ulong value)
  147. {
  148. char str[17];
  149. sprintf(str, "%lx", value);
  150. return env_set(varname, str);
  151. }
  152. ulong env_get_hex(const char *varname, ulong default_val)
  153. {
  154. const char *s;
  155. ulong value;
  156. char *endp;
  157. s = env_get(varname);
  158. if (s)
  159. value = hextoul(s, &endp);
  160. if (!s || endp == s)
  161. return default_val;
  162. return value;
  163. }
  164. int eth_env_get_enetaddr(const char *name, uint8_t *enetaddr)
  165. {
  166. string_to_enetaddr(env_get(name), enetaddr);
  167. return is_valid_ethaddr(enetaddr);
  168. }
  169. int eth_env_set_enetaddr(const char *name, const uint8_t *enetaddr)
  170. {
  171. char buf[ARP_HLEN_ASCII + 1];
  172. if (eth_env_get_enetaddr(name, (uint8_t *)buf))
  173. return -EEXIST;
  174. sprintf(buf, "%pM", enetaddr);
  175. return env_set(name, buf);
  176. }
  177. /*
  178. * Look up variable from environment,
  179. * return address of storage for that variable,
  180. * or NULL if not found
  181. */
  182. char *env_get(const char *name)
  183. {
  184. if (gd->flags & GD_FLG_ENV_READY) { /* after import into hashtable */
  185. struct env_entry e, *ep;
  186. schedule();
  187. e.key = name;
  188. e.data = NULL;
  189. hsearch_r(e, ENV_FIND, &ep, &env_htab, 0);
  190. return ep ? ep->data : NULL;
  191. }
  192. /* restricted capabilities before import */
  193. if (env_get_f(name, (char *)(gd->env_buf), sizeof(gd->env_buf)) >= 0)
  194. return (char *)(gd->env_buf);
  195. return NULL;
  196. }
  197. /*
  198. * Like env_get, but prints an error if envvar isn't defined in the
  199. * environment. It always returns what env_get does, so it can be used in
  200. * place of env_get without changing error handling otherwise.
  201. */
  202. char *from_env(const char *envvar)
  203. {
  204. char *ret;
  205. ret = env_get(envvar);
  206. if (!ret)
  207. printf("missing environment variable: %s\n", envvar);
  208. return ret;
  209. }
  210. static int env_get_from_linear(const char *env, const char *name, char *buf,
  211. unsigned len)
  212. {
  213. const char *p, *end;
  214. size_t name_len;
  215. if (name == NULL || *name == '\0')
  216. return -1;
  217. name_len = strlen(name);
  218. for (p = env; *p != '\0'; p = end + 1) {
  219. const char *value;
  220. unsigned res;
  221. for (end = p; *end != '\0'; ++end)
  222. if (end - env >= CONFIG_ENV_SIZE)
  223. return -1;
  224. if (strncmp(name, p, name_len) || p[name_len] != '=')
  225. continue;
  226. value = &p[name_len + 1];
  227. res = end - value;
  228. memcpy(buf, value, min(len, res + 1));
  229. if (len <= res) {
  230. buf[len - 1] = '\0';
  231. printf("env_buf [%u bytes] too small for value of \"%s\"\n",
  232. len, name);
  233. }
  234. return res;
  235. }
  236. return -1;
  237. }
  238. /*
  239. * Look up variable from environment for restricted C runtime env.
  240. */
  241. int env_get_f(const char *name, char *buf, unsigned len)
  242. {
  243. const char *env;
  244. if (gd->env_valid == ENV_INVALID)
  245. env = default_environment;
  246. else
  247. env = (const char *)gd->env_addr;
  248. return env_get_from_linear(env, name, buf, len);
  249. }
  250. /**
  251. * Decode the integer value of an environment variable and return it.
  252. *
  253. * @param name Name of environment variable
  254. * @param base Number base to use (normally 10, or 16 for hex)
  255. * @param default_val Default value to return if the variable is not
  256. * found
  257. * Return: the decoded value, or default_val if not found
  258. */
  259. ulong env_get_ulong(const char *name, int base, ulong default_val)
  260. {
  261. /*
  262. * We can use env_get() here, even before relocation, since the
  263. * environment variable value is an integer and thus short.
  264. */
  265. const char *str = env_get(name);
  266. return str ? simple_strtoul(str, NULL, base) : default_val;
  267. }
  268. /*
  269. * Read an environment variable as a boolean
  270. * Return -1 if variable does not exist (default to true)
  271. */
  272. int env_get_yesno(const char *var)
  273. {
  274. char *s = env_get(var);
  275. if (s == NULL)
  276. return -1;
  277. return (*s == '1' || *s == 'y' || *s == 'Y' || *s == 't' || *s == 'T') ?
  278. 1 : 0;
  279. }
  280. bool env_get_autostart(void)
  281. {
  282. return env_get_yesno("autostart") == 1;
  283. }
  284. /*
  285. * Look up the variable from the default environment
  286. */
  287. char *env_get_default(const char *name)
  288. {
  289. int ret;
  290. ret = env_get_default_into(name, (char *)(gd->env_buf),
  291. sizeof(gd->env_buf));
  292. if (ret >= 0)
  293. return (char *)(gd->env_buf);
  294. return NULL;
  295. }
  296. /*
  297. * Look up the variable from the default environment and store its value in buf
  298. */
  299. int env_get_default_into(const char *name, char *buf, unsigned int len)
  300. {
  301. return env_get_from_linear(default_environment, name, buf, len);
  302. }
  303. void env_set_default(const char *s, int flags)
  304. {
  305. if (s) {
  306. if ((flags & H_INTERACTIVE) == 0) {
  307. printf("*** Warning - %s, "
  308. "using default environment\n\n", s);
  309. } else {
  310. puts(s);
  311. }
  312. } else {
  313. debug("Using default environment\n");
  314. }
  315. flags |= H_DEFAULT;
  316. if (himport_r(&env_htab, default_environment,
  317. sizeof(default_environment), '\0', flags, 0,
  318. 0, NULL) == 0) {
  319. pr_err("## Error: Environment import failed: errno = %d\n",
  320. errno);
  321. return;
  322. }
  323. gd->flags |= GD_FLG_ENV_READY;
  324. gd->flags |= GD_FLG_ENV_DEFAULT;
  325. }
  326. /* [re]set individual variables to their value in the default environment */
  327. int env_set_default_vars(int nvars, char * const vars[], int flags)
  328. {
  329. /*
  330. * Special use-case: import from default environment
  331. * (and use \0 as a separator)
  332. */
  333. flags |= H_NOCLEAR | H_DEFAULT;
  334. return himport_r(&env_htab, default_environment,
  335. sizeof(default_environment), '\0',
  336. flags, 0, nvars, vars);
  337. }
  338. /*
  339. * Check if CRC is valid and (if yes) import the environment.
  340. * Note that "buf" may or may not be aligned.
  341. */
  342. int env_import(const char *buf, int check, int flags)
  343. {
  344. env_t *ep = (env_t *)buf;
  345. if (check) {
  346. uint32_t crc;
  347. memcpy(&crc, &ep->crc, sizeof(crc));
  348. if (crc32(0, ep->data, ENV_SIZE) != crc) {
  349. env_set_default("bad CRC", 0);
  350. return -ENOMSG; /* needed for env_load() */
  351. }
  352. }
  353. if (himport_r(&env_htab, (char *)ep->data, ENV_SIZE, '\0', flags, 0,
  354. 0, NULL)) {
  355. gd->flags |= GD_FLG_ENV_READY;
  356. return 0;
  357. }
  358. pr_err("Cannot import environment: errno = %d\n", errno);
  359. env_set_default("import failed", 0);
  360. return -EIO;
  361. }
  362. #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
  363. static unsigned char env_flags;
  364. int env_check_redund(const char *buf1, int buf1_read_fail,
  365. const char *buf2, int buf2_read_fail)
  366. {
  367. int crc1_ok = 0, crc2_ok = 0;
  368. env_t *tmp_env1, *tmp_env2;
  369. tmp_env1 = (env_t *)buf1;
  370. tmp_env2 = (env_t *)buf2;
  371. if (buf1_read_fail && buf2_read_fail) {
  372. puts("*** Error - No Valid Environment Area found\n");
  373. return -EIO;
  374. } else if (buf1_read_fail || buf2_read_fail) {
  375. puts("*** Warning - some problems detected ");
  376. puts("reading environment; recovered successfully\n");
  377. }
  378. if (!buf1_read_fail)
  379. crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) ==
  380. tmp_env1->crc;
  381. if (!buf2_read_fail)
  382. crc2_ok = crc32(0, tmp_env2->data, ENV_SIZE) ==
  383. tmp_env2->crc;
  384. if (!crc1_ok && !crc2_ok) {
  385. gd->env_valid = ENV_INVALID;
  386. return -ENOMSG; /* needed for env_load() */
  387. } else if (crc1_ok && !crc2_ok) {
  388. gd->env_valid = ENV_VALID;
  389. } else if (!crc1_ok && crc2_ok) {
  390. gd->env_valid = ENV_REDUND;
  391. } else {
  392. /* both ok - check serial */
  393. if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
  394. gd->env_valid = ENV_REDUND;
  395. else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
  396. gd->env_valid = ENV_VALID;
  397. else if (tmp_env1->flags > tmp_env2->flags)
  398. gd->env_valid = ENV_VALID;
  399. else if (tmp_env2->flags > tmp_env1->flags)
  400. gd->env_valid = ENV_REDUND;
  401. else /* flags are equal - almost impossible */
  402. gd->env_valid = ENV_VALID;
  403. }
  404. return 0;
  405. }
  406. int env_import_redund(const char *buf1, int buf1_read_fail,
  407. const char *buf2, int buf2_read_fail,
  408. int flags)
  409. {
  410. env_t *ep;
  411. int ret;
  412. ret = env_check_redund(buf1, buf1_read_fail, buf2, buf2_read_fail);
  413. if (ret == -EIO) {
  414. env_set_default("bad env area", 0);
  415. return -EIO;
  416. } else if (ret == -ENOMSG) {
  417. env_set_default("bad CRC", 0);
  418. return -ENOMSG;
  419. }
  420. if (gd->env_valid == ENV_VALID)
  421. ep = (env_t *)buf1;
  422. else
  423. ep = (env_t *)buf2;
  424. env_flags = ep->flags;
  425. return env_import((char *)ep, 0, flags);
  426. }
  427. #endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */
  428. /* Export the environment and generate CRC for it. */
  429. int env_export(env_t *env_out)
  430. {
  431. char *res;
  432. ssize_t len;
  433. res = (char *)env_out->data;
  434. len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
  435. if (len < 0) {
  436. pr_err("Cannot export environment: errno = %d\n", errno);
  437. return 1;
  438. }
  439. env_out->crc = crc32(0, env_out->data, ENV_SIZE);
  440. #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
  441. env_out->flags = ++env_flags; /* increase the serial */
  442. #endif
  443. return 0;
  444. }
  445. void env_relocate(void)
  446. {
  447. if (gd->env_valid == ENV_INVALID) {
  448. #if defined(CONFIG_ENV_IS_NOWHERE) || defined(CONFIG_SPL_BUILD)
  449. /* Environment not changable */
  450. env_set_default(NULL, 0);
  451. #else
  452. bootstage_error(BOOTSTAGE_ID_NET_CHECKSUM);
  453. env_set_default("bad CRC", 0);
  454. #endif
  455. } else {
  456. env_load();
  457. }
  458. }
  459. #ifdef CONFIG_AUTO_COMPLETE
  460. int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf,
  461. bool dollar_comp)
  462. {
  463. struct env_entry *match;
  464. int found, idx;
  465. if (dollar_comp) {
  466. /*
  467. * When doing $ completion, the first character should
  468. * obviously be a '$'.
  469. */
  470. if (var[0] != '$')
  471. return 0;
  472. var++;
  473. /*
  474. * The second one, if present, should be a '{', as some
  475. * configuration of the u-boot shell expand ${var} but not
  476. * $var.
  477. */
  478. if (var[0] == '{')
  479. var++;
  480. else if (var[0] != '\0')
  481. return 0;
  482. }
  483. idx = 0;
  484. found = 0;
  485. cmdv[0] = NULL;
  486. while ((idx = hmatch_r(var, idx, &match, &env_htab))) {
  487. int vallen = strlen(match->key) + 1;
  488. if (found >= maxv - 2 ||
  489. bufsz < vallen + (dollar_comp ? 3 : 0))
  490. break;
  491. cmdv[found++] = buf;
  492. /* Add the '${' prefix to each var when doing $ completion. */
  493. if (dollar_comp) {
  494. strcpy(buf, "${");
  495. buf += 2;
  496. bufsz -= 3;
  497. }
  498. memcpy(buf, match->key, vallen);
  499. buf += vallen;
  500. bufsz -= vallen;
  501. if (dollar_comp) {
  502. /*
  503. * This one is a bit odd: vallen already contains the
  504. * '\0' character but we need to add the '}' suffix,
  505. * hence the buf - 1 here. strcpy() will add the '\0'
  506. * character just after '}'. buf is then incremented
  507. * to account for the extra '}' we just added.
  508. */
  509. strcpy(buf - 1, "}");
  510. buf++;
  511. }
  512. }
  513. qsort(cmdv, found, sizeof(cmdv[0]), strcmp_compar);
  514. if (idx)
  515. cmdv[found++] = dollar_comp ? "${...}" : "...";
  516. cmdv[found] = NULL;
  517. return found;
  518. }
  519. #endif
  520. #ifdef CONFIG_ENV_IMPORT_FDT
  521. void env_import_fdt(void)
  522. {
  523. const char *path;
  524. struct ofprop prop;
  525. ofnode node;
  526. int res;
  527. path = env_get("env_fdt_path");
  528. if (!path || !path[0])
  529. return;
  530. node = ofnode_path(path);
  531. if (!ofnode_valid(node)) {
  532. printf("Warning: device tree node '%s' not found\n", path);
  533. return;
  534. }
  535. for (res = ofnode_first_property(node, &prop);
  536. !res;
  537. res = ofnode_next_property(&prop)) {
  538. const char *name, *val;
  539. val = ofprop_get_property(&prop, &name, NULL);
  540. env_set(name, val);
  541. }
  542. }
  543. #endif