common.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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/stddef.h>
  18. #include <search.h>
  19. #include <errno.h>
  20. #include <malloc.h>
  21. #include <u-boot/crc.h>
  22. #include <dm/ofnode.h>
  23. #include <net.h>
  24. #include <watchdog.h>
  25. DECLARE_GLOBAL_DATA_PTR;
  26. /************************************************************************
  27. * Default settings to be used when no valid environment is found
  28. */
  29. #include <env_default.h>
  30. struct hsearch_data env_htab = {
  31. .change_ok = env_flags_validate,
  32. };
  33. /*
  34. * This env_set() function is defined in cmd/nvedit.c, since it calls
  35. * _do_env_set(), whis is a static function in that file.
  36. *
  37. * int env_set(const char *varname, const char *varvalue);
  38. */
  39. /**
  40. * Set an environment variable to an integer value
  41. *
  42. * @param varname Environment variable to set
  43. * @param value Value to set it to
  44. * @return 0 if ok, 1 on error
  45. */
  46. int env_set_ulong(const char *varname, ulong value)
  47. {
  48. /* TODO: this should be unsigned */
  49. char *str = simple_itoa(value);
  50. return env_set(varname, str);
  51. }
  52. /**
  53. * Set an environment variable to an value in hex
  54. *
  55. * @param varname Environment variable to set
  56. * @param value Value to set it to
  57. * @return 0 if ok, 1 on error
  58. */
  59. int env_set_hex(const char *varname, ulong value)
  60. {
  61. char str[17];
  62. sprintf(str, "%lx", value);
  63. return env_set(varname, str);
  64. }
  65. ulong env_get_hex(const char *varname, ulong default_val)
  66. {
  67. const char *s;
  68. ulong value;
  69. char *endp;
  70. s = env_get(varname);
  71. if (s)
  72. value = hextoul(s, &endp);
  73. if (!s || endp == s)
  74. return default_val;
  75. return value;
  76. }
  77. int eth_env_get_enetaddr(const char *name, uint8_t *enetaddr)
  78. {
  79. string_to_enetaddr(env_get(name), enetaddr);
  80. return is_valid_ethaddr(enetaddr);
  81. }
  82. int eth_env_set_enetaddr(const char *name, const uint8_t *enetaddr)
  83. {
  84. char buf[ARP_HLEN_ASCII + 1];
  85. if (eth_env_get_enetaddr(name, (uint8_t *)buf))
  86. return -EEXIST;
  87. sprintf(buf, "%pM", enetaddr);
  88. return env_set(name, buf);
  89. }
  90. /*
  91. * Look up variable from environment,
  92. * return address of storage for that variable,
  93. * or NULL if not found
  94. */
  95. char *env_get(const char *name)
  96. {
  97. if (gd->flags & GD_FLG_ENV_READY) { /* after import into hashtable */
  98. struct env_entry e, *ep;
  99. WATCHDOG_RESET();
  100. e.key = name;
  101. e.data = NULL;
  102. hsearch_r(e, ENV_FIND, &ep, &env_htab, 0);
  103. return ep ? ep->data : NULL;
  104. }
  105. /* restricted capabilities before import */
  106. if (env_get_f(name, (char *)(gd->env_buf), sizeof(gd->env_buf)) >= 0)
  107. return (char *)(gd->env_buf);
  108. return NULL;
  109. }
  110. /*
  111. * Like env_get, but prints an error if envvar isn't defined in the
  112. * environment. It always returns what env_get does, so it can be used in
  113. * place of env_get without changing error handling otherwise.
  114. */
  115. char *from_env(const char *envvar)
  116. {
  117. char *ret;
  118. ret = env_get(envvar);
  119. if (!ret)
  120. printf("missing environment variable: %s\n", envvar);
  121. return ret;
  122. }
  123. static int env_get_from_linear(const char *env, const char *name, char *buf,
  124. unsigned len)
  125. {
  126. const char *p, *end;
  127. size_t name_len;
  128. if (name == NULL || *name == '\0')
  129. return -1;
  130. name_len = strlen(name);
  131. for (p = env; *p != '\0'; p = end + 1) {
  132. const char *value;
  133. unsigned res;
  134. for (end = p; *end != '\0'; ++end)
  135. if (end - env >= CONFIG_ENV_SIZE)
  136. return -1;
  137. if (strncmp(name, p, name_len) || p[name_len] != '=')
  138. continue;
  139. value = &p[name_len + 1];
  140. res = end - value;
  141. memcpy(buf, value, min(len, res + 1));
  142. if (len <= res) {
  143. buf[len - 1] = '\0';
  144. printf("env_buf [%u bytes] too small for value of \"%s\"\n",
  145. len, name);
  146. }
  147. return res;
  148. }
  149. return -1;
  150. }
  151. /*
  152. * Look up variable from environment for restricted C runtime env.
  153. */
  154. int env_get_f(const char *name, char *buf, unsigned len)
  155. {
  156. const char *env;
  157. if (gd->env_valid == ENV_INVALID)
  158. env = default_environment;
  159. else
  160. env = (const char *)gd->env_addr;
  161. return env_get_from_linear(env, name, buf, len);
  162. }
  163. /**
  164. * Decode the integer value of an environment variable and return it.
  165. *
  166. * @param name Name of environment variable
  167. * @param base Number base to use (normally 10, or 16 for hex)
  168. * @param default_val Default value to return if the variable is not
  169. * found
  170. * @return the decoded value, or default_val if not found
  171. */
  172. ulong env_get_ulong(const char *name, int base, ulong default_val)
  173. {
  174. /*
  175. * We can use env_get() here, even before relocation, since the
  176. * environment variable value is an integer and thus short.
  177. */
  178. const char *str = env_get(name);
  179. return str ? simple_strtoul(str, NULL, base) : default_val;
  180. }
  181. /*
  182. * Read an environment variable as a boolean
  183. * Return -1 if variable does not exist (default to true)
  184. */
  185. int env_get_yesno(const char *var)
  186. {
  187. char *s = env_get(var);
  188. if (s == NULL)
  189. return -1;
  190. return (*s == '1' || *s == 'y' || *s == 'Y' || *s == 't' || *s == 'T') ?
  191. 1 : 0;
  192. }
  193. /*
  194. * Look up the variable from the default environment
  195. */
  196. char *env_get_default(const char *name)
  197. {
  198. if (env_get_from_linear(default_environment, name,
  199. (char *)(gd->env_buf),
  200. sizeof(gd->env_buf)) >= 0)
  201. return (char *)(gd->env_buf);
  202. return NULL;
  203. }
  204. void env_set_default(const char *s, int flags)
  205. {
  206. if (s) {
  207. if ((flags & H_INTERACTIVE) == 0) {
  208. printf("*** Warning - %s, "
  209. "using default environment\n\n", s);
  210. } else {
  211. puts(s);
  212. }
  213. } else {
  214. debug("Using default environment\n");
  215. }
  216. flags |= H_DEFAULT;
  217. if (himport_r(&env_htab, default_environment,
  218. sizeof(default_environment), '\0', flags, 0,
  219. 0, NULL) == 0) {
  220. pr_err("## Error: Environment import failed: errno = %d\n",
  221. errno);
  222. return;
  223. }
  224. gd->flags |= GD_FLG_ENV_READY;
  225. gd->flags |= GD_FLG_ENV_DEFAULT;
  226. }
  227. /* [re]set individual variables to their value in the default environment */
  228. int env_set_default_vars(int nvars, char * const vars[], int flags)
  229. {
  230. /*
  231. * Special use-case: import from default environment
  232. * (and use \0 as a separator)
  233. */
  234. flags |= H_NOCLEAR | H_DEFAULT;
  235. return himport_r(&env_htab, default_environment,
  236. sizeof(default_environment), '\0',
  237. flags, 0, nvars, vars);
  238. }
  239. /*
  240. * Check if CRC is valid and (if yes) import the environment.
  241. * Note that "buf" may or may not be aligned.
  242. */
  243. int env_import(const char *buf, int check, int flags)
  244. {
  245. env_t *ep = (env_t *)buf;
  246. if (check) {
  247. uint32_t crc;
  248. memcpy(&crc, &ep->crc, sizeof(crc));
  249. if (crc32(0, ep->data, ENV_SIZE) != crc) {
  250. env_set_default("bad CRC", 0);
  251. return -ENOMSG; /* needed for env_load() */
  252. }
  253. }
  254. if (himport_r(&env_htab, (char *)ep->data, ENV_SIZE, '\0', flags, 0,
  255. 0, NULL)) {
  256. gd->flags |= GD_FLG_ENV_READY;
  257. return 0;
  258. }
  259. pr_err("Cannot import environment: errno = %d\n", errno);
  260. env_set_default("import failed", 0);
  261. return -EIO;
  262. }
  263. #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
  264. static unsigned char env_flags;
  265. int env_check_redund(const char *buf1, int buf1_read_fail,
  266. const char *buf2, int buf2_read_fail)
  267. {
  268. int crc1_ok = 0, crc2_ok = 0;
  269. env_t *tmp_env1, *tmp_env2;
  270. tmp_env1 = (env_t *)buf1;
  271. tmp_env2 = (env_t *)buf2;
  272. if (buf1_read_fail && buf2_read_fail) {
  273. puts("*** Error - No Valid Environment Area found\n");
  274. return -EIO;
  275. } else if (buf1_read_fail || buf2_read_fail) {
  276. puts("*** Warning - some problems detected ");
  277. puts("reading environment; recovered successfully\n");
  278. }
  279. if (!buf1_read_fail)
  280. crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) ==
  281. tmp_env1->crc;
  282. if (!buf2_read_fail)
  283. crc2_ok = crc32(0, tmp_env2->data, ENV_SIZE) ==
  284. tmp_env2->crc;
  285. if (!crc1_ok && !crc2_ok) {
  286. return -ENOMSG; /* needed for env_load() */
  287. } else if (crc1_ok && !crc2_ok) {
  288. gd->env_valid = ENV_VALID;
  289. } else if (!crc1_ok && crc2_ok) {
  290. gd->env_valid = ENV_REDUND;
  291. } else {
  292. /* both ok - check serial */
  293. if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
  294. gd->env_valid = ENV_REDUND;
  295. else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
  296. gd->env_valid = ENV_VALID;
  297. else if (tmp_env1->flags > tmp_env2->flags)
  298. gd->env_valid = ENV_VALID;
  299. else if (tmp_env2->flags > tmp_env1->flags)
  300. gd->env_valid = ENV_REDUND;
  301. else /* flags are equal - almost impossible */
  302. gd->env_valid = ENV_VALID;
  303. }
  304. return 0;
  305. }
  306. int env_import_redund(const char *buf1, int buf1_read_fail,
  307. const char *buf2, int buf2_read_fail,
  308. int flags)
  309. {
  310. env_t *ep;
  311. int ret;
  312. ret = env_check_redund(buf1, buf1_read_fail, buf2, buf2_read_fail);
  313. if (ret == -EIO) {
  314. env_set_default("bad env area", 0);
  315. return -EIO;
  316. } else if (ret == -ENOMSG) {
  317. env_set_default("bad CRC", 0);
  318. return -ENOMSG;
  319. }
  320. if (gd->env_valid == ENV_VALID)
  321. ep = (env_t *)buf1;
  322. else
  323. ep = (env_t *)buf2;
  324. env_flags = ep->flags;
  325. return env_import((char *)ep, 0, flags);
  326. }
  327. #endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */
  328. /* Export the environment and generate CRC for it. */
  329. int env_export(env_t *env_out)
  330. {
  331. char *res;
  332. ssize_t len;
  333. res = (char *)env_out->data;
  334. len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
  335. if (len < 0) {
  336. pr_err("Cannot export environment: errno = %d\n", errno);
  337. return 1;
  338. }
  339. env_out->crc = crc32(0, env_out->data, ENV_SIZE);
  340. #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
  341. env_out->flags = ++env_flags; /* increase the serial */
  342. #endif
  343. return 0;
  344. }
  345. void env_relocate(void)
  346. {
  347. #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  348. env_reloc();
  349. env_fix_drivers();
  350. env_htab.change_ok += gd->reloc_off;
  351. #endif
  352. if (gd->env_valid == ENV_INVALID) {
  353. #if defined(CONFIG_ENV_IS_NOWHERE) || defined(CONFIG_SPL_BUILD)
  354. /* Environment not changable */
  355. env_set_default(NULL, 0);
  356. #else
  357. bootstage_error(BOOTSTAGE_ID_NET_CHECKSUM);
  358. env_set_default("bad CRC", 0);
  359. #endif
  360. } else {
  361. env_load();
  362. }
  363. }
  364. #ifdef CONFIG_AUTO_COMPLETE
  365. int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf,
  366. bool dollar_comp)
  367. {
  368. struct env_entry *match;
  369. int found, idx;
  370. if (dollar_comp) {
  371. /*
  372. * When doing $ completion, the first character should
  373. * obviously be a '$'.
  374. */
  375. if (var[0] != '$')
  376. return 0;
  377. var++;
  378. /*
  379. * The second one, if present, should be a '{', as some
  380. * configuration of the u-boot shell expand ${var} but not
  381. * $var.
  382. */
  383. if (var[0] == '{')
  384. var++;
  385. else if (var[0] != '\0')
  386. return 0;
  387. }
  388. idx = 0;
  389. found = 0;
  390. cmdv[0] = NULL;
  391. while ((idx = hmatch_r(var, idx, &match, &env_htab))) {
  392. int vallen = strlen(match->key) + 1;
  393. if (found >= maxv - 2 ||
  394. bufsz < vallen + (dollar_comp ? 3 : 0))
  395. break;
  396. cmdv[found++] = buf;
  397. /* Add the '${' prefix to each var when doing $ completion. */
  398. if (dollar_comp) {
  399. strcpy(buf, "${");
  400. buf += 2;
  401. bufsz -= 3;
  402. }
  403. memcpy(buf, match->key, vallen);
  404. buf += vallen;
  405. bufsz -= vallen;
  406. if (dollar_comp) {
  407. /*
  408. * This one is a bit odd: vallen already contains the
  409. * '\0' character but we need to add the '}' suffix,
  410. * hence the buf - 1 here. strcpy() will add the '\0'
  411. * character just after '}'. buf is then incremented
  412. * to account for the extra '}' we just added.
  413. */
  414. strcpy(buf - 1, "}");
  415. buf++;
  416. }
  417. }
  418. qsort(cmdv, found, sizeof(cmdv[0]), strcmp_compar);
  419. if (idx)
  420. cmdv[found++] = dollar_comp ? "${...}" : "...";
  421. cmdv[found] = NULL;
  422. return found;
  423. }
  424. #endif
  425. #ifdef CONFIG_ENV_IMPORT_FDT
  426. void env_import_fdt(void)
  427. {
  428. const char *path;
  429. struct ofprop prop;
  430. ofnode node;
  431. int res;
  432. path = env_get("env_fdt_path");
  433. if (!path || !path[0])
  434. return;
  435. node = ofnode_path(path);
  436. if (!ofnode_valid(node)) {
  437. printf("Warning: device tree node '%s' not found\n", path);
  438. return;
  439. }
  440. for (res = ofnode_get_first_property(node, &prop);
  441. !res;
  442. res = ofnode_get_next_property(&prop)) {
  443. const char *name, *val;
  444. val = ofnode_get_property_by_prop(&prop, &name, NULL);
  445. env_set(name, val);
  446. }
  447. }
  448. #endif