common.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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. 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. return -ENOMSG; /* needed for env_load() */
  291. } else if (crc1_ok && !crc2_ok) {
  292. gd->env_valid = ENV_VALID;
  293. } else if (!crc1_ok && crc2_ok) {
  294. gd->env_valid = ENV_REDUND;
  295. } else {
  296. /* both ok - check serial */
  297. if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
  298. gd->env_valid = ENV_REDUND;
  299. else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
  300. gd->env_valid = ENV_VALID;
  301. else if (tmp_env1->flags > tmp_env2->flags)
  302. gd->env_valid = ENV_VALID;
  303. else if (tmp_env2->flags > tmp_env1->flags)
  304. gd->env_valid = ENV_REDUND;
  305. else /* flags are equal - almost impossible */
  306. gd->env_valid = ENV_VALID;
  307. }
  308. return 0;
  309. }
  310. int env_import_redund(const char *buf1, int buf1_read_fail,
  311. const char *buf2, int buf2_read_fail,
  312. int flags)
  313. {
  314. env_t *ep;
  315. int ret;
  316. ret = env_check_redund(buf1, buf1_read_fail, buf2, buf2_read_fail);
  317. if (ret == -EIO) {
  318. env_set_default("bad env area", 0);
  319. return -EIO;
  320. } else if (ret == -ENOMSG) {
  321. env_set_default("bad CRC", 0);
  322. return -ENOMSG;
  323. }
  324. if (gd->env_valid == ENV_VALID)
  325. ep = (env_t *)buf1;
  326. else
  327. ep = (env_t *)buf2;
  328. env_flags = ep->flags;
  329. return env_import((char *)ep, 0, flags);
  330. }
  331. #endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */
  332. /* Export the environment and generate CRC for it. */
  333. int env_export(env_t *env_out)
  334. {
  335. char *res;
  336. ssize_t len;
  337. res = (char *)env_out->data;
  338. len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
  339. if (len < 0) {
  340. pr_err("Cannot export environment: errno = %d\n", errno);
  341. return 1;
  342. }
  343. env_out->crc = crc32(0, env_out->data, ENV_SIZE);
  344. #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
  345. env_out->flags = ++env_flags; /* increase the serial */
  346. #endif
  347. return 0;
  348. }
  349. void env_relocate(void)
  350. {
  351. #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  352. env_reloc();
  353. env_fix_drivers();
  354. env_htab.change_ok += gd->reloc_off;
  355. #endif
  356. if (gd->env_valid == ENV_INVALID) {
  357. #if defined(CONFIG_ENV_IS_NOWHERE) || defined(CONFIG_SPL_BUILD)
  358. /* Environment not changable */
  359. env_set_default(NULL, 0);
  360. #else
  361. bootstage_error(BOOTSTAGE_ID_NET_CHECKSUM);
  362. env_set_default("bad CRC", 0);
  363. #endif
  364. } else {
  365. env_load();
  366. }
  367. }
  368. #ifdef CONFIG_AUTO_COMPLETE
  369. int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf,
  370. bool dollar_comp)
  371. {
  372. struct env_entry *match;
  373. int found, idx;
  374. if (dollar_comp) {
  375. /*
  376. * When doing $ completion, the first character should
  377. * obviously be a '$'.
  378. */
  379. if (var[0] != '$')
  380. return 0;
  381. var++;
  382. /*
  383. * The second one, if present, should be a '{', as some
  384. * configuration of the u-boot shell expand ${var} but not
  385. * $var.
  386. */
  387. if (var[0] == '{')
  388. var++;
  389. else if (var[0] != '\0')
  390. return 0;
  391. }
  392. idx = 0;
  393. found = 0;
  394. cmdv[0] = NULL;
  395. while ((idx = hmatch_r(var, idx, &match, &env_htab))) {
  396. int vallen = strlen(match->key) + 1;
  397. if (found >= maxv - 2 ||
  398. bufsz < vallen + (dollar_comp ? 3 : 0))
  399. break;
  400. cmdv[found++] = buf;
  401. /* Add the '${' prefix to each var when doing $ completion. */
  402. if (dollar_comp) {
  403. strcpy(buf, "${");
  404. buf += 2;
  405. bufsz -= 3;
  406. }
  407. memcpy(buf, match->key, vallen);
  408. buf += vallen;
  409. bufsz -= vallen;
  410. if (dollar_comp) {
  411. /*
  412. * This one is a bit odd: vallen already contains the
  413. * '\0' character but we need to add the '}' suffix,
  414. * hence the buf - 1 here. strcpy() will add the '\0'
  415. * character just after '}'. buf is then incremented
  416. * to account for the extra '}' we just added.
  417. */
  418. strcpy(buf - 1, "}");
  419. buf++;
  420. }
  421. }
  422. qsort(cmdv, found, sizeof(cmdv[0]), strcmp_compar);
  423. if (idx)
  424. cmdv[found++] = dollar_comp ? "${...}" : "...";
  425. cmdv[found] = NULL;
  426. return found;
  427. }
  428. #endif
  429. #ifdef CONFIG_ENV_IMPORT_FDT
  430. void env_import_fdt(void)
  431. {
  432. const char *path;
  433. struct ofprop prop;
  434. ofnode node;
  435. int res;
  436. path = env_get("env_fdt_path");
  437. if (!path || !path[0])
  438. return;
  439. node = ofnode_path(path);
  440. if (!ofnode_valid(node)) {
  441. printf("Warning: device tree node '%s' not found\n", path);
  442. return;
  443. }
  444. for (res = ofnode_get_first_property(node, &prop);
  445. !res;
  446. res = ofnode_get_next_property(&prop)) {
  447. const char *name, *val;
  448. val = ofnode_get_property_by_prop(&prop, &name, NULL);
  449. env_set(name, val);
  450. }
  451. }
  452. #endif