common.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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. schedule();
  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. bool env_get_autostart(void)
  194. {
  195. return env_get_yesno("autostart") == 1;
  196. }
  197. /*
  198. * Look up the variable from the default environment
  199. */
  200. char *env_get_default(const char *name)
  201. {
  202. if (env_get_from_linear(default_environment, name,
  203. (char *)(gd->env_buf),
  204. sizeof(gd->env_buf)) >= 0)
  205. return (char *)(gd->env_buf);
  206. return NULL;
  207. }
  208. void env_set_default(const char *s, int flags)
  209. {
  210. if (s) {
  211. if ((flags & H_INTERACTIVE) == 0) {
  212. printf("*** Warning - %s, "
  213. "using default environment\n\n", s);
  214. } else {
  215. puts(s);
  216. }
  217. } else {
  218. debug("Using default environment\n");
  219. }
  220. flags |= H_DEFAULT;
  221. if (himport_r(&env_htab, default_environment,
  222. sizeof(default_environment), '\0', flags, 0,
  223. 0, NULL) == 0) {
  224. pr_err("## Error: Environment import failed: errno = %d\n",
  225. errno);
  226. return;
  227. }
  228. gd->flags |= GD_FLG_ENV_READY;
  229. gd->flags |= GD_FLG_ENV_DEFAULT;
  230. }
  231. /* [re]set individual variables to their value in the default environment */
  232. int env_set_default_vars(int nvars, char * const vars[], int flags)
  233. {
  234. /*
  235. * Special use-case: import from default environment
  236. * (and use \0 as a separator)
  237. */
  238. flags |= H_NOCLEAR | H_DEFAULT;
  239. return himport_r(&env_htab, default_environment,
  240. sizeof(default_environment), '\0',
  241. flags, 0, nvars, vars);
  242. }
  243. /*
  244. * Check if CRC is valid and (if yes) import the environment.
  245. * Note that "buf" may or may not be aligned.
  246. */
  247. int env_import(const char *buf, int check, int flags)
  248. {
  249. env_t *ep = (env_t *)buf;
  250. if (check) {
  251. uint32_t crc;
  252. memcpy(&crc, &ep->crc, sizeof(crc));
  253. if (crc32(0, ep->data, ENV_SIZE) != crc) {
  254. env_set_default("bad CRC", 0);
  255. return -ENOMSG; /* needed for env_load() */
  256. }
  257. }
  258. if (himport_r(&env_htab, (char *)ep->data, ENV_SIZE, '\0', flags, 0,
  259. 0, NULL)) {
  260. gd->flags |= GD_FLG_ENV_READY;
  261. return 0;
  262. }
  263. pr_err("Cannot import environment: errno = %d\n", errno);
  264. env_set_default("import failed", 0);
  265. return -EIO;
  266. }
  267. #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
  268. static unsigned char env_flags;
  269. int env_check_redund(const char *buf1, int buf1_read_fail,
  270. const char *buf2, int buf2_read_fail)
  271. {
  272. int crc1_ok = 0, crc2_ok = 0;
  273. env_t *tmp_env1, *tmp_env2;
  274. tmp_env1 = (env_t *)buf1;
  275. tmp_env2 = (env_t *)buf2;
  276. if (buf1_read_fail && buf2_read_fail) {
  277. puts("*** Error - No Valid Environment Area found\n");
  278. return -EIO;
  279. } else if (buf1_read_fail || buf2_read_fail) {
  280. puts("*** Warning - some problems detected ");
  281. puts("reading environment; recovered successfully\n");
  282. }
  283. if (!buf1_read_fail)
  284. crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) ==
  285. tmp_env1->crc;
  286. if (!buf2_read_fail)
  287. crc2_ok = crc32(0, tmp_env2->data, ENV_SIZE) ==
  288. tmp_env2->crc;
  289. if (!crc1_ok && !crc2_ok) {
  290. gd->env_valid = ENV_INVALID;
  291. return -ENOMSG; /* needed for env_load() */
  292. } else if (crc1_ok && !crc2_ok) {
  293. gd->env_valid = ENV_VALID;
  294. } else if (!crc1_ok && crc2_ok) {
  295. gd->env_valid = ENV_REDUND;
  296. } else {
  297. /* both ok - check serial */
  298. if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
  299. gd->env_valid = ENV_REDUND;
  300. else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
  301. gd->env_valid = ENV_VALID;
  302. else if (tmp_env1->flags > tmp_env2->flags)
  303. gd->env_valid = ENV_VALID;
  304. else if (tmp_env2->flags > tmp_env1->flags)
  305. gd->env_valid = ENV_REDUND;
  306. else /* flags are equal - almost impossible */
  307. gd->env_valid = ENV_VALID;
  308. }
  309. return 0;
  310. }
  311. int env_import_redund(const char *buf1, int buf1_read_fail,
  312. const char *buf2, int buf2_read_fail,
  313. int flags)
  314. {
  315. env_t *ep;
  316. int ret;
  317. ret = env_check_redund(buf1, buf1_read_fail, buf2, buf2_read_fail);
  318. if (ret == -EIO) {
  319. env_set_default("bad env area", 0);
  320. return -EIO;
  321. } else if (ret == -ENOMSG) {
  322. env_set_default("bad CRC", 0);
  323. return -ENOMSG;
  324. }
  325. if (gd->env_valid == ENV_VALID)
  326. ep = (env_t *)buf1;
  327. else
  328. ep = (env_t *)buf2;
  329. env_flags = ep->flags;
  330. return env_import((char *)ep, 0, flags);
  331. }
  332. #endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */
  333. /* Export the environment and generate CRC for it. */
  334. int env_export(env_t *env_out)
  335. {
  336. char *res;
  337. ssize_t len;
  338. res = (char *)env_out->data;
  339. len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
  340. if (len < 0) {
  341. pr_err("Cannot export environment: errno = %d\n", errno);
  342. return 1;
  343. }
  344. env_out->crc = crc32(0, env_out->data, ENV_SIZE);
  345. #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
  346. env_out->flags = ++env_flags; /* increase the serial */
  347. #endif
  348. return 0;
  349. }
  350. void env_relocate(void)
  351. {
  352. #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  353. env_reloc();
  354. env_fix_drivers();
  355. env_htab.change_ok += gd->reloc_off;
  356. #endif
  357. if (gd->env_valid == ENV_INVALID) {
  358. #if defined(CONFIG_ENV_IS_NOWHERE) || defined(CONFIG_SPL_BUILD)
  359. /* Environment not changable */
  360. env_set_default(NULL, 0);
  361. #else
  362. bootstage_error(BOOTSTAGE_ID_NET_CHECKSUM);
  363. env_set_default("bad CRC", 0);
  364. #endif
  365. } else {
  366. env_load();
  367. }
  368. }
  369. #ifdef CONFIG_AUTO_COMPLETE
  370. int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf,
  371. bool dollar_comp)
  372. {
  373. struct env_entry *match;
  374. int found, idx;
  375. if (dollar_comp) {
  376. /*
  377. * When doing $ completion, the first character should
  378. * obviously be a '$'.
  379. */
  380. if (var[0] != '$')
  381. return 0;
  382. var++;
  383. /*
  384. * The second one, if present, should be a '{', as some
  385. * configuration of the u-boot shell expand ${var} but not
  386. * $var.
  387. */
  388. if (var[0] == '{')
  389. var++;
  390. else if (var[0] != '\0')
  391. return 0;
  392. }
  393. idx = 0;
  394. found = 0;
  395. cmdv[0] = NULL;
  396. while ((idx = hmatch_r(var, idx, &match, &env_htab))) {
  397. int vallen = strlen(match->key) + 1;
  398. if (found >= maxv - 2 ||
  399. bufsz < vallen + (dollar_comp ? 3 : 0))
  400. break;
  401. cmdv[found++] = buf;
  402. /* Add the '${' prefix to each var when doing $ completion. */
  403. if (dollar_comp) {
  404. strcpy(buf, "${");
  405. buf += 2;
  406. bufsz -= 3;
  407. }
  408. memcpy(buf, match->key, vallen);
  409. buf += vallen;
  410. bufsz -= vallen;
  411. if (dollar_comp) {
  412. /*
  413. * This one is a bit odd: vallen already contains the
  414. * '\0' character but we need to add the '}' suffix,
  415. * hence the buf - 1 here. strcpy() will add the '\0'
  416. * character just after '}'. buf is then incremented
  417. * to account for the extra '}' we just added.
  418. */
  419. strcpy(buf - 1, "}");
  420. buf++;
  421. }
  422. }
  423. qsort(cmdv, found, sizeof(cmdv[0]), strcmp_compar);
  424. if (idx)
  425. cmdv[found++] = dollar_comp ? "${...}" : "...";
  426. cmdv[found] = NULL;
  427. return found;
  428. }
  429. #endif
  430. #ifdef CONFIG_ENV_IMPORT_FDT
  431. void env_import_fdt(void)
  432. {
  433. const char *path;
  434. struct ofprop prop;
  435. ofnode node;
  436. int res;
  437. path = env_get("env_fdt_path");
  438. if (!path || !path[0])
  439. return;
  440. node = ofnode_path(path);
  441. if (!ofnode_valid(node)) {
  442. printf("Warning: device tree node '%s' not found\n", path);
  443. return;
  444. }
  445. for (res = ofnode_first_property(node, &prop);
  446. !res;
  447. res = ofnode_next_property(&prop)) {
  448. const char *name, *val;
  449. val = ofprop_get_property(&prop, &name, NULL);
  450. env_set(name, val);
  451. }
  452. }
  453. #endif