fw_env.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489
  1. /*
  2. * (C) Copyright 2000-2010
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * (C) Copyright 2008
  6. * Guennadi Liakhovetski, DENX Software Engineering, lg@denx.de.
  7. *
  8. * SPDX-License-Identifier: GPL-2.0+
  9. */
  10. #define _GNU_SOURCE
  11. #include <compiler.h>
  12. #include <errno.h>
  13. #include <env_flags.h>
  14. #include <fcntl.h>
  15. #include <linux/fs.h>
  16. #include <linux/stringify.h>
  17. #include <ctype.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <stddef.h>
  21. #include <string.h>
  22. #include <sys/types.h>
  23. #include <sys/ioctl.h>
  24. #include <sys/stat.h>
  25. #include <unistd.h>
  26. #ifdef MTD_OLD
  27. # include <stdint.h>
  28. # include <linux/mtd/mtd.h>
  29. #else
  30. # define __user /* nothing */
  31. # include <mtd/mtd-user.h>
  32. #endif
  33. #include "fw_env.h"
  34. struct env_opts default_opts = {
  35. #ifdef CONFIG_FILE
  36. .config_file = CONFIG_FILE
  37. #endif
  38. };
  39. #define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
  40. #define min(x, y) ({ \
  41. typeof(x) _min1 = (x); \
  42. typeof(y) _min2 = (y); \
  43. (void) (&_min1 == &_min2); \
  44. _min1 < _min2 ? _min1 : _min2; })
  45. struct envdev_s {
  46. const char *devname; /* Device name */
  47. long long devoff; /* Device offset */
  48. ulong env_size; /* environment size */
  49. ulong erase_size; /* device erase size */
  50. ulong env_sectors; /* number of environment sectors */
  51. uint8_t mtd_type; /* type of the MTD device */
  52. };
  53. static struct envdev_s envdevices[2] =
  54. {
  55. {
  56. .mtd_type = MTD_ABSENT,
  57. }, {
  58. .mtd_type = MTD_ABSENT,
  59. },
  60. };
  61. static int dev_current;
  62. #define DEVNAME(i) envdevices[(i)].devname
  63. #define DEVOFFSET(i) envdevices[(i)].devoff
  64. #define ENVSIZE(i) envdevices[(i)].env_size
  65. #define DEVESIZE(i) envdevices[(i)].erase_size
  66. #define ENVSECTORS(i) envdevices[(i)].env_sectors
  67. #define DEVTYPE(i) envdevices[(i)].mtd_type
  68. #define CUR_ENVSIZE ENVSIZE(dev_current)
  69. static unsigned long usable_envsize;
  70. #define ENV_SIZE usable_envsize
  71. struct env_image_single {
  72. uint32_t crc; /* CRC32 over data bytes */
  73. char data[];
  74. };
  75. struct env_image_redundant {
  76. uint32_t crc; /* CRC32 over data bytes */
  77. unsigned char flags; /* active or obsolete */
  78. char data[];
  79. };
  80. enum flag_scheme {
  81. FLAG_NONE,
  82. FLAG_BOOLEAN,
  83. FLAG_INCREMENTAL,
  84. };
  85. struct environment {
  86. void *image;
  87. uint32_t *crc;
  88. unsigned char *flags;
  89. char *data;
  90. enum flag_scheme flag_scheme;
  91. };
  92. static struct environment environment = {
  93. .flag_scheme = FLAG_NONE,
  94. };
  95. static int env_aes_cbc_crypt(char *data, const int enc, uint8_t *key);
  96. static int HaveRedundEnv = 0;
  97. static unsigned char active_flag = 1;
  98. /* obsolete_flag must be 0 to efficiently set it on NOR flash without erasing */
  99. static unsigned char obsolete_flag = 0;
  100. #define DEFAULT_ENV_INSTANCE_STATIC
  101. #include <env_default.h>
  102. static int flash_io (int mode);
  103. static int parse_config(struct env_opts *opts);
  104. #if defined(CONFIG_FILE)
  105. static int get_config (char *);
  106. #endif
  107. static char *skip_chars(char *s)
  108. {
  109. for (; *s != '\0'; s++) {
  110. if (isblank(*s))
  111. return s;
  112. }
  113. return NULL;
  114. }
  115. static char *skip_blanks(char *s)
  116. {
  117. for (; *s != '\0'; s++) {
  118. if (!isblank(*s))
  119. return s;
  120. }
  121. return NULL;
  122. }
  123. /*
  124. * s1 is either a simple 'name', or a 'name=value' pair.
  125. * s2 is a 'name=value' pair.
  126. * If the names match, return the value of s2, else NULL.
  127. */
  128. static char *envmatch(char *s1, char *s2)
  129. {
  130. if (s1 == NULL || s2 == NULL)
  131. return NULL;
  132. while (*s1 == *s2++)
  133. if (*s1++ == '=')
  134. return s2;
  135. if (*s1 == '\0' && *(s2 - 1) == '=')
  136. return s2;
  137. return NULL;
  138. }
  139. /**
  140. * Search the environment for a variable.
  141. * Return the value, if found, or NULL, if not found.
  142. */
  143. char *fw_getenv (char *name)
  144. {
  145. char *env, *nxt;
  146. for (env = environment.data; *env; env = nxt + 1) {
  147. char *val;
  148. for (nxt = env; *nxt; ++nxt) {
  149. if (nxt >= &environment.data[ENV_SIZE]) {
  150. fprintf (stderr, "## Error: "
  151. "environment not terminated\n");
  152. return NULL;
  153. }
  154. }
  155. val = envmatch (name, env);
  156. if (!val)
  157. continue;
  158. return val;
  159. }
  160. return NULL;
  161. }
  162. /*
  163. * Search the default environment for a variable.
  164. * Return the value, if found, or NULL, if not found.
  165. */
  166. char *fw_getdefenv(char *name)
  167. {
  168. char *env, *nxt;
  169. for (env = default_environment; *env; env = nxt + 1) {
  170. char *val;
  171. for (nxt = env; *nxt; ++nxt) {
  172. if (nxt >= &default_environment[ENV_SIZE]) {
  173. fprintf(stderr, "## Error: "
  174. "default environment not terminated\n");
  175. return NULL;
  176. }
  177. }
  178. val = envmatch(name, env);
  179. if (!val)
  180. continue;
  181. return val;
  182. }
  183. return NULL;
  184. }
  185. int parse_aes_key(char *key, uint8_t *bin_key)
  186. {
  187. char tmp[5] = { '0', 'x', 0, 0, 0 };
  188. unsigned long ul;
  189. int i;
  190. if (strnlen(key, 64) != 32) {
  191. fprintf(stderr,
  192. "## Error: '-a' option requires 16-byte AES key\n");
  193. return -1;
  194. }
  195. for (i = 0; i < 16; i++) {
  196. tmp[2] = key[0];
  197. tmp[3] = key[1];
  198. errno = 0;
  199. ul = strtoul(tmp, NULL, 16);
  200. if (errno) {
  201. fprintf(stderr,
  202. "## Error: '-a' option requires valid AES key\n");
  203. return -1;
  204. }
  205. bin_key[i] = ul & 0xff;
  206. key += 2;
  207. }
  208. return 0;
  209. }
  210. /*
  211. * Print the current definition of one, or more, or all
  212. * environment variables
  213. */
  214. int fw_printenv(int argc, char *argv[], int value_only, struct env_opts *opts)
  215. {
  216. int i, rc = 0;
  217. if (value_only && argc != 1) {
  218. fprintf(stderr,
  219. "## Error: `-n' option requires exactly one argument\n");
  220. return -1;
  221. }
  222. if (!opts)
  223. opts = &default_opts;
  224. if (fw_env_open(opts))
  225. return -1;
  226. if (argc == 0) { /* Print all env variables */
  227. char *env, *nxt;
  228. for (env = environment.data; *env; env = nxt + 1) {
  229. for (nxt = env; *nxt; ++nxt) {
  230. if (nxt >= &environment.data[ENV_SIZE]) {
  231. fprintf (stderr, "## Error: "
  232. "environment not terminated\n");
  233. return -1;
  234. }
  235. }
  236. printf ("%s\n", env);
  237. }
  238. return 0;
  239. }
  240. for (i = 0; i < argc; ++i) { /* print a subset of env variables */
  241. char *name = argv[i];
  242. char *val = NULL;
  243. val = fw_getenv(name);
  244. if (!val) {
  245. fprintf (stderr, "## Error: \"%s\" not defined\n", name);
  246. rc = -1;
  247. continue;
  248. }
  249. if (value_only) {
  250. puts(val);
  251. break;
  252. }
  253. printf("%s=%s\n", name, val);
  254. }
  255. return rc;
  256. }
  257. int fw_env_close(struct env_opts *opts)
  258. {
  259. int ret;
  260. if (!opts)
  261. opts = &default_opts;
  262. if (opts->aes_flag) {
  263. ret = env_aes_cbc_crypt(environment.data, 1,
  264. opts->aes_key);
  265. if (ret) {
  266. fprintf(stderr,
  267. "Error: can't encrypt env for flash\n");
  268. return ret;
  269. }
  270. }
  271. /*
  272. * Update CRC
  273. */
  274. *environment.crc = crc32(0, (uint8_t *) environment.data, ENV_SIZE);
  275. /* write environment back to flash */
  276. if (flash_io(O_RDWR)) {
  277. fprintf(stderr,
  278. "Error: can't write fw_env to flash\n");
  279. return -1;
  280. }
  281. return 0;
  282. }
  283. /*
  284. * Set/Clear a single variable in the environment.
  285. * This is called in sequence to update the environment
  286. * in RAM without updating the copy in flash after each set
  287. */
  288. int fw_env_write(char *name, char *value)
  289. {
  290. int len;
  291. char *env, *nxt;
  292. char *oldval = NULL;
  293. int deleting, creating, overwriting;
  294. /*
  295. * search if variable with this name already exists
  296. */
  297. for (nxt = env = environment.data; *env; env = nxt + 1) {
  298. for (nxt = env; *nxt; ++nxt) {
  299. if (nxt >= &environment.data[ENV_SIZE]) {
  300. fprintf(stderr, "## Error: "
  301. "environment not terminated\n");
  302. errno = EINVAL;
  303. return -1;
  304. }
  305. }
  306. if ((oldval = envmatch (name, env)) != NULL)
  307. break;
  308. }
  309. deleting = (oldval && !(value && strlen(value)));
  310. creating = (!oldval && (value && strlen(value)));
  311. overwriting = (oldval && (value && strlen(value)));
  312. /* check for permission */
  313. if (deleting) {
  314. if (env_flags_validate_varaccess(name,
  315. ENV_FLAGS_VARACCESS_PREVENT_DELETE)) {
  316. printf("Can't delete \"%s\"\n", name);
  317. errno = EROFS;
  318. return -1;
  319. }
  320. } else if (overwriting) {
  321. if (env_flags_validate_varaccess(name,
  322. ENV_FLAGS_VARACCESS_PREVENT_OVERWR)) {
  323. printf("Can't overwrite \"%s\"\n", name);
  324. errno = EROFS;
  325. return -1;
  326. } else if (env_flags_validate_varaccess(name,
  327. ENV_FLAGS_VARACCESS_PREVENT_NONDEF_OVERWR)) {
  328. const char *defval = fw_getdefenv(name);
  329. if (defval == NULL)
  330. defval = "";
  331. if (strcmp(oldval, defval)
  332. != 0) {
  333. printf("Can't overwrite \"%s\"\n", name);
  334. errno = EROFS;
  335. return -1;
  336. }
  337. }
  338. } else if (creating) {
  339. if (env_flags_validate_varaccess(name,
  340. ENV_FLAGS_VARACCESS_PREVENT_CREATE)) {
  341. printf("Can't create \"%s\"\n", name);
  342. errno = EROFS;
  343. return -1;
  344. }
  345. } else
  346. /* Nothing to do */
  347. return 0;
  348. if (deleting || overwriting) {
  349. if (*++nxt == '\0') {
  350. *env = '\0';
  351. } else {
  352. for (;;) {
  353. *env = *nxt++;
  354. if ((*env == '\0') && (*nxt == '\0'))
  355. break;
  356. ++env;
  357. }
  358. }
  359. *++env = '\0';
  360. }
  361. /* Delete only ? */
  362. if (!value || !strlen(value))
  363. return 0;
  364. /*
  365. * Append new definition at the end
  366. */
  367. for (env = environment.data; *env || *(env + 1); ++env);
  368. if (env > environment.data)
  369. ++env;
  370. /*
  371. * Overflow when:
  372. * "name" + "=" + "val" +"\0\0" > CUR_ENVSIZE - (env-environment)
  373. */
  374. len = strlen (name) + 2;
  375. /* add '=' for first arg, ' ' for all others */
  376. len += strlen(value) + 1;
  377. if (len > (&environment.data[ENV_SIZE] - env)) {
  378. fprintf (stderr,
  379. "Error: environment overflow, \"%s\" deleted\n",
  380. name);
  381. return -1;
  382. }
  383. while ((*env = *name++) != '\0')
  384. env++;
  385. *env = '=';
  386. while ((*++env = *value++) != '\0')
  387. ;
  388. /* end is marked with double '\0' */
  389. *++env = '\0';
  390. return 0;
  391. }
  392. /*
  393. * Deletes or sets environment variables. Returns -1 and sets errno error codes:
  394. * 0 - OK
  395. * EINVAL - need at least 1 argument
  396. * EROFS - certain variables ("ethaddr", "serial#") cannot be
  397. * modified or deleted
  398. *
  399. */
  400. int fw_setenv(int argc, char *argv[], struct env_opts *opts)
  401. {
  402. int i;
  403. size_t len;
  404. char *name, **valv;
  405. char *value = NULL;
  406. int valc;
  407. if (!opts)
  408. opts = &default_opts;
  409. if (argc < 1) {
  410. fprintf(stderr, "## Error: variable name missing\n");
  411. errno = EINVAL;
  412. return -1;
  413. }
  414. if (fw_env_open(opts)) {
  415. fprintf(stderr, "Error: environment not initialized\n");
  416. return -1;
  417. }
  418. name = argv[0];
  419. valv = argv + 1;
  420. valc = argc - 1;
  421. if (env_flags_validate_env_set_params(name, valv, valc) < 0)
  422. return -1;
  423. len = 0;
  424. for (i = 0; i < valc; ++i) {
  425. char *val = valv[i];
  426. size_t val_len = strlen(val);
  427. if (value)
  428. value[len - 1] = ' ';
  429. value = realloc(value, len + val_len + 1);
  430. if (!value) {
  431. fprintf(stderr,
  432. "Cannot malloc %zu bytes: %s\n",
  433. len, strerror(errno));
  434. return -1;
  435. }
  436. memcpy(value + len, val, val_len);
  437. len += val_len;
  438. value[len++] = '\0';
  439. }
  440. fw_env_write(name, value);
  441. free(value);
  442. return fw_env_close(opts);
  443. }
  444. /*
  445. * Parse a file and configure the u-boot variables.
  446. * The script file has a very simple format, as follows:
  447. *
  448. * Each line has a couple with name, value:
  449. * <white spaces>variable_name<white spaces>variable_value
  450. *
  451. * Both variable_name and variable_value are interpreted as strings.
  452. * Any character after <white spaces> and before ending \r\n is interpreted
  453. * as variable's value (no comment allowed on these lines !)
  454. *
  455. * Comments are allowed if the first character in the line is #
  456. *
  457. * Returns -1 and sets errno error codes:
  458. * 0 - OK
  459. * -1 - Error
  460. */
  461. int fw_parse_script(char *fname, struct env_opts *opts)
  462. {
  463. FILE *fp;
  464. char dump[1024]; /* Maximum line length in the file */
  465. char *name;
  466. char *val;
  467. int lineno = 0;
  468. int len;
  469. int ret = 0;
  470. if (!opts)
  471. opts = &default_opts;
  472. if (fw_env_open(opts)) {
  473. fprintf(stderr, "Error: environment not initialized\n");
  474. return -1;
  475. }
  476. if (strcmp(fname, "-") == 0)
  477. fp = stdin;
  478. else {
  479. fp = fopen(fname, "r");
  480. if (fp == NULL) {
  481. fprintf(stderr, "I cannot open %s for reading\n",
  482. fname);
  483. return -1;
  484. }
  485. }
  486. while (fgets(dump, sizeof(dump), fp)) {
  487. lineno++;
  488. len = strlen(dump);
  489. /*
  490. * Read a whole line from the file. If the line is too long
  491. * or is not terminated, reports an error and exit.
  492. */
  493. if (dump[len - 1] != '\n') {
  494. fprintf(stderr,
  495. "Line %d not corrected terminated or too long\n",
  496. lineno);
  497. ret = -1;
  498. break;
  499. }
  500. /* Drop ending line feed / carriage return */
  501. dump[--len] = '\0';
  502. if (len && dump[len - 1] == '\r')
  503. dump[--len] = '\0';
  504. /* Skip comment or empty lines */
  505. if (len == 0 || dump[0] == '#')
  506. continue;
  507. /*
  508. * Search for variable's name,
  509. * remove leading whitespaces
  510. */
  511. name = skip_blanks(dump);
  512. if (!name)
  513. continue;
  514. /* The first white space is the end of variable name */
  515. val = skip_chars(name);
  516. len = strlen(name);
  517. if (val) {
  518. *val++ = '\0';
  519. if ((val - name) < len)
  520. val = skip_blanks(val);
  521. else
  522. val = NULL;
  523. }
  524. #ifdef DEBUG
  525. fprintf(stderr, "Setting %s : %s\n",
  526. name, val ? val : " removed");
  527. #endif
  528. if (env_flags_validate_type(name, val) < 0) {
  529. ret = -1;
  530. break;
  531. }
  532. /*
  533. * If there is an error setting a variable,
  534. * try to save the environment and returns an error
  535. */
  536. if (fw_env_write(name, val)) {
  537. fprintf(stderr,
  538. "fw_env_write returns with error : %s\n",
  539. strerror(errno));
  540. ret = -1;
  541. break;
  542. }
  543. }
  544. /* Close file if not stdin */
  545. if (strcmp(fname, "-") != 0)
  546. fclose(fp);
  547. ret |= fw_env_close(opts);
  548. return ret;
  549. }
  550. /**
  551. * environment_end() - compute offset of first byte right after environemnt
  552. * @dev - index of enviroment buffer
  553. * Return:
  554. * device offset of first byte right after environemnt
  555. */
  556. off_t environment_end(int dev)
  557. {
  558. /* environment is block aligned */
  559. return DEVOFFSET(dev) + ENVSECTORS(dev) * DEVESIZE(dev);
  560. }
  561. /*
  562. * Test for bad block on NAND, just returns 0 on NOR, on NAND:
  563. * 0 - block is good
  564. * > 0 - block is bad
  565. * < 0 - failed to test
  566. */
  567. static int flash_bad_block(int fd, uint8_t mtd_type, loff_t blockstart)
  568. {
  569. if (mtd_type == MTD_NANDFLASH) {
  570. int badblock = ioctl(fd, MEMGETBADBLOCK, &blockstart);
  571. if (badblock < 0) {
  572. perror ("Cannot read bad block mark");
  573. return badblock;
  574. }
  575. if (badblock) {
  576. #ifdef DEBUG
  577. fprintf (stderr, "Bad block at 0x%llx, skipping\n",
  578. (unsigned long long)blockstart);
  579. #endif
  580. return badblock;
  581. }
  582. }
  583. return 0;
  584. }
  585. /*
  586. * Read data from flash at an offset into a provided buffer. On NAND it skips
  587. * bad blocks but makes sure it stays within ENVSECTORS (dev) starting from
  588. * the DEVOFFSET (dev) block. On NOR the loop is only run once.
  589. */
  590. static int flash_read_buf (int dev, int fd, void *buf, size_t count,
  591. off_t offset, uint8_t mtd_type)
  592. {
  593. size_t blocklen; /* erase / write length - one block on NAND,
  594. 0 on NOR */
  595. size_t processed = 0; /* progress counter */
  596. size_t readlen = count; /* current read length */
  597. off_t block_seek; /* offset inside the current block to the start
  598. of the data */
  599. loff_t blockstart; /* running start of the current block -
  600. MEMGETBADBLOCK needs 64 bits */
  601. int rc;
  602. blockstart = (offset / DEVESIZE (dev)) * DEVESIZE (dev);
  603. /* Offset inside a block */
  604. block_seek = offset - blockstart;
  605. if (mtd_type == MTD_NANDFLASH) {
  606. /*
  607. * NAND: calculate which blocks we are reading. We have
  608. * to read one block at a time to skip bad blocks.
  609. */
  610. blocklen = DEVESIZE (dev);
  611. /* Limit to one block for the first read */
  612. if (readlen > blocklen - block_seek)
  613. readlen = blocklen - block_seek;
  614. } else {
  615. blocklen = 0;
  616. }
  617. /* This only runs once on NOR flash */
  618. while (processed < count) {
  619. rc = flash_bad_block(fd, mtd_type, blockstart);
  620. if (rc < 0) /* block test failed */
  621. return -1;
  622. if (blockstart + block_seek + readlen > environment_end(dev)) {
  623. /* End of range is reached */
  624. fprintf (stderr,
  625. "Too few good blocks within range\n");
  626. return -1;
  627. }
  628. if (rc) { /* block is bad */
  629. blockstart += blocklen;
  630. continue;
  631. }
  632. /*
  633. * If a block is bad, we retry in the next block at the same
  634. * offset - see common/env_nand.c::writeenv()
  635. */
  636. lseek (fd, blockstart + block_seek, SEEK_SET);
  637. rc = read (fd, buf + processed, readlen);
  638. if (rc != readlen) {
  639. fprintf (stderr, "Read error on %s: %s\n",
  640. DEVNAME (dev), strerror (errno));
  641. return -1;
  642. }
  643. #ifdef DEBUG
  644. fprintf(stderr, "Read 0x%x bytes at 0x%llx on %s\n",
  645. rc, (unsigned long long) blockstart + block_seek,
  646. DEVNAME(dev));
  647. #endif
  648. processed += readlen;
  649. readlen = min (blocklen, count - processed);
  650. block_seek = 0;
  651. blockstart += blocklen;
  652. }
  653. return processed;
  654. }
  655. /*
  656. * Write count bytes at offset, but stay within ENVSECTORS (dev) sectors of
  657. * DEVOFFSET (dev). Similar to the read case above, on NOR and dataflash we
  658. * erase and write the whole data at once.
  659. */
  660. static int flash_write_buf (int dev, int fd, void *buf, size_t count,
  661. off_t offset, uint8_t mtd_type)
  662. {
  663. void *data;
  664. struct erase_info_user erase;
  665. size_t blocklen; /* length of NAND block / NOR erase sector */
  666. size_t erase_len; /* whole area that can be erased - may include
  667. bad blocks */
  668. size_t erasesize; /* erase / write length - one block on NAND,
  669. whole area on NOR */
  670. size_t processed = 0; /* progress counter */
  671. size_t write_total; /* total size to actually write - excluding
  672. bad blocks */
  673. off_t erase_offset; /* offset to the first erase block (aligned)
  674. below offset */
  675. off_t block_seek; /* offset inside the erase block to the start
  676. of the data */
  677. loff_t blockstart; /* running start of the current block -
  678. MEMGETBADBLOCK needs 64 bits */
  679. int rc;
  680. /*
  681. * For mtd devices only offset and size of the environment do matter
  682. */
  683. if (mtd_type == MTD_ABSENT) {
  684. blocklen = count;
  685. erase_len = blocklen;
  686. blockstart = offset;
  687. block_seek = 0;
  688. write_total = blocklen;
  689. } else {
  690. blocklen = DEVESIZE(dev);
  691. erase_offset = (offset / blocklen) * blocklen;
  692. /* Maximum area we may use */
  693. erase_len = environment_end(dev) - erase_offset;
  694. blockstart = erase_offset;
  695. /* Offset inside a block */
  696. block_seek = offset - erase_offset;
  697. /*
  698. * Data size we actually write: from the start of the block
  699. * to the start of the data, then count bytes of data, and
  700. * to the end of the block
  701. */
  702. write_total = ((block_seek + count + blocklen - 1) /
  703. blocklen) * blocklen;
  704. }
  705. /*
  706. * Support data anywhere within erase sectors: read out the complete
  707. * area to be erased, replace the environment image, write the whole
  708. * block back again.
  709. */
  710. if (write_total > count) {
  711. data = malloc (erase_len);
  712. if (!data) {
  713. fprintf (stderr,
  714. "Cannot malloc %zu bytes: %s\n",
  715. erase_len, strerror (errno));
  716. return -1;
  717. }
  718. rc = flash_read_buf (dev, fd, data, write_total, erase_offset,
  719. mtd_type);
  720. if (write_total != rc)
  721. return -1;
  722. #ifdef DEBUG
  723. fprintf(stderr, "Preserving data ");
  724. if (block_seek != 0)
  725. fprintf(stderr, "0x%x - 0x%lx", 0, block_seek - 1);
  726. if (block_seek + count != write_total) {
  727. if (block_seek != 0)
  728. fprintf(stderr, " and ");
  729. fprintf(stderr, "0x%lx - 0x%lx",
  730. (unsigned long) block_seek + count,
  731. (unsigned long) write_total - 1);
  732. }
  733. fprintf(stderr, "\n");
  734. #endif
  735. /* Overwrite the old environment */
  736. memcpy (data + block_seek, buf, count);
  737. } else {
  738. /*
  739. * We get here, iff offset is block-aligned and count is a
  740. * multiple of blocklen - see write_total calculation above
  741. */
  742. data = buf;
  743. }
  744. if (mtd_type == MTD_NANDFLASH) {
  745. /*
  746. * NAND: calculate which blocks we are writing. We have
  747. * to write one block at a time to skip bad blocks.
  748. */
  749. erasesize = blocklen;
  750. } else {
  751. erasesize = erase_len;
  752. }
  753. erase.length = erasesize;
  754. /* This only runs once on NOR flash and SPI-dataflash */
  755. while (processed < write_total) {
  756. rc = flash_bad_block(fd, mtd_type, blockstart);
  757. if (rc < 0) /* block test failed */
  758. return rc;
  759. if (blockstart + erasesize > environment_end(dev)) {
  760. fprintf (stderr, "End of range reached, aborting\n");
  761. return -1;
  762. }
  763. if (rc) { /* block is bad */
  764. blockstart += blocklen;
  765. continue;
  766. }
  767. if (mtd_type != MTD_ABSENT) {
  768. erase.start = blockstart;
  769. ioctl(fd, MEMUNLOCK, &erase);
  770. /* These do not need an explicit erase cycle */
  771. if (mtd_type != MTD_DATAFLASH)
  772. if (ioctl(fd, MEMERASE, &erase) != 0) {
  773. fprintf(stderr,
  774. "MTD erase error on %s: %s\n",
  775. DEVNAME(dev), strerror(errno));
  776. return -1;
  777. }
  778. }
  779. if (lseek (fd, blockstart, SEEK_SET) == -1) {
  780. fprintf (stderr,
  781. "Seek error on %s: %s\n",
  782. DEVNAME (dev), strerror (errno));
  783. return -1;
  784. }
  785. #ifdef DEBUG
  786. fprintf(stderr, "Write 0x%llx bytes at 0x%llx\n",
  787. (unsigned long long) erasesize,
  788. (unsigned long long) blockstart);
  789. #endif
  790. if (write (fd, data + processed, erasesize) != erasesize) {
  791. fprintf (stderr, "Write error on %s: %s\n",
  792. DEVNAME (dev), strerror (errno));
  793. return -1;
  794. }
  795. if (mtd_type != MTD_ABSENT)
  796. ioctl(fd, MEMLOCK, &erase);
  797. processed += erasesize;
  798. block_seek = 0;
  799. blockstart += erasesize;
  800. }
  801. if (write_total > count)
  802. free (data);
  803. return processed;
  804. }
  805. /*
  806. * Set obsolete flag at offset - NOR flash only
  807. */
  808. static int flash_flag_obsolete (int dev, int fd, off_t offset)
  809. {
  810. int rc;
  811. struct erase_info_user erase;
  812. erase.start = DEVOFFSET (dev);
  813. erase.length = DEVESIZE (dev);
  814. /* This relies on the fact, that obsolete_flag == 0 */
  815. rc = lseek (fd, offset, SEEK_SET);
  816. if (rc < 0) {
  817. fprintf (stderr, "Cannot seek to set the flag on %s \n",
  818. DEVNAME (dev));
  819. return rc;
  820. }
  821. ioctl (fd, MEMUNLOCK, &erase);
  822. rc = write (fd, &obsolete_flag, sizeof (obsolete_flag));
  823. ioctl (fd, MEMLOCK, &erase);
  824. if (rc < 0)
  825. perror ("Could not set obsolete flag");
  826. return rc;
  827. }
  828. /* Encrypt or decrypt the environment before writing or reading it. */
  829. static int env_aes_cbc_crypt(char *payload, const int enc, uint8_t *key)
  830. {
  831. uint8_t *data = (uint8_t *)payload;
  832. const int len = usable_envsize;
  833. uint8_t key_exp[AES_EXPAND_KEY_LENGTH];
  834. uint32_t aes_blocks;
  835. /* First we expand the key. */
  836. aes_expand_key(key, key_exp);
  837. /* Calculate the number of AES blocks to encrypt. */
  838. aes_blocks = DIV_ROUND_UP(len, AES_KEY_LENGTH);
  839. if (enc)
  840. aes_cbc_encrypt_blocks(key_exp, data, data, aes_blocks);
  841. else
  842. aes_cbc_decrypt_blocks(key_exp, data, data, aes_blocks);
  843. return 0;
  844. }
  845. static int flash_write (int fd_current, int fd_target, int dev_target)
  846. {
  847. int rc;
  848. switch (environment.flag_scheme) {
  849. case FLAG_NONE:
  850. break;
  851. case FLAG_INCREMENTAL:
  852. (*environment.flags)++;
  853. break;
  854. case FLAG_BOOLEAN:
  855. *environment.flags = active_flag;
  856. break;
  857. default:
  858. fprintf (stderr, "Unimplemented flash scheme %u \n",
  859. environment.flag_scheme);
  860. return -1;
  861. }
  862. #ifdef DEBUG
  863. fprintf(stderr, "Writing new environment at 0x%llx on %s\n",
  864. DEVOFFSET (dev_target), DEVNAME (dev_target));
  865. #endif
  866. rc = flash_write_buf(dev_target, fd_target, environment.image,
  867. CUR_ENVSIZE, DEVOFFSET(dev_target),
  868. DEVTYPE(dev_target));
  869. if (rc < 0)
  870. return rc;
  871. if (environment.flag_scheme == FLAG_BOOLEAN) {
  872. /* Have to set obsolete flag */
  873. off_t offset = DEVOFFSET (dev_current) +
  874. offsetof (struct env_image_redundant, flags);
  875. #ifdef DEBUG
  876. fprintf(stderr,
  877. "Setting obsolete flag in environment at 0x%llx on %s\n",
  878. DEVOFFSET (dev_current), DEVNAME (dev_current));
  879. #endif
  880. flash_flag_obsolete (dev_current, fd_current, offset);
  881. }
  882. return 0;
  883. }
  884. static int flash_read (int fd)
  885. {
  886. int rc;
  887. rc = flash_read_buf(dev_current, fd, environment.image, CUR_ENVSIZE,
  888. DEVOFFSET(dev_current), DEVTYPE(dev_current));
  889. if (rc != CUR_ENVSIZE)
  890. return -1;
  891. return 0;
  892. }
  893. static int flash_io (int mode)
  894. {
  895. int fd_current, fd_target, rc, dev_target;
  896. /* dev_current: fd_current, erase_current */
  897. fd_current = open (DEVNAME (dev_current), mode);
  898. if (fd_current < 0) {
  899. fprintf (stderr,
  900. "Can't open %s: %s\n",
  901. DEVNAME (dev_current), strerror (errno));
  902. return -1;
  903. }
  904. if (mode == O_RDWR) {
  905. if (HaveRedundEnv) {
  906. /* switch to next partition for writing */
  907. dev_target = !dev_current;
  908. /* dev_target: fd_target, erase_target */
  909. fd_target = open (DEVNAME (dev_target), mode);
  910. if (fd_target < 0) {
  911. fprintf (stderr,
  912. "Can't open %s: %s\n",
  913. DEVNAME (dev_target),
  914. strerror (errno));
  915. rc = -1;
  916. goto exit;
  917. }
  918. } else {
  919. dev_target = dev_current;
  920. fd_target = fd_current;
  921. }
  922. rc = flash_write (fd_current, fd_target, dev_target);
  923. if (HaveRedundEnv) {
  924. if (close (fd_target)) {
  925. fprintf (stderr,
  926. "I/O error on %s: %s\n",
  927. DEVNAME (dev_target),
  928. strerror (errno));
  929. rc = -1;
  930. }
  931. }
  932. } else {
  933. rc = flash_read (fd_current);
  934. }
  935. exit:
  936. if (close (fd_current)) {
  937. fprintf (stderr,
  938. "I/O error on %s: %s\n",
  939. DEVNAME (dev_current), strerror (errno));
  940. return -1;
  941. }
  942. return rc;
  943. }
  944. /*
  945. * Prevent confusion if running from erased flash memory
  946. */
  947. int fw_env_open(struct env_opts *opts)
  948. {
  949. int crc0, crc0_ok;
  950. unsigned char flag0;
  951. void *addr0;
  952. int crc1, crc1_ok;
  953. unsigned char flag1;
  954. void *addr1;
  955. int ret;
  956. struct env_image_single *single;
  957. struct env_image_redundant *redundant;
  958. if (!opts)
  959. opts = &default_opts;
  960. if (parse_config(opts)) /* should fill envdevices */
  961. return -1;
  962. addr0 = calloc(1, CUR_ENVSIZE);
  963. if (addr0 == NULL) {
  964. fprintf(stderr,
  965. "Not enough memory for environment (%ld bytes)\n",
  966. CUR_ENVSIZE);
  967. return -1;
  968. }
  969. /* read environment from FLASH to local buffer */
  970. environment.image = addr0;
  971. if (HaveRedundEnv) {
  972. redundant = addr0;
  973. environment.crc = &redundant->crc;
  974. environment.flags = &redundant->flags;
  975. environment.data = redundant->data;
  976. } else {
  977. single = addr0;
  978. environment.crc = &single->crc;
  979. environment.flags = NULL;
  980. environment.data = single->data;
  981. }
  982. dev_current = 0;
  983. if (flash_io (O_RDONLY))
  984. return -1;
  985. crc0 = crc32 (0, (uint8_t *) environment.data, ENV_SIZE);
  986. if (opts->aes_flag) {
  987. ret = env_aes_cbc_crypt(environment.data, 0,
  988. opts->aes_key);
  989. if (ret)
  990. return ret;
  991. }
  992. crc0_ok = (crc0 == *environment.crc);
  993. if (!HaveRedundEnv) {
  994. if (!crc0_ok) {
  995. fprintf (stderr,
  996. "Warning: Bad CRC, using default environment\n");
  997. memcpy(environment.data, default_environment, sizeof default_environment);
  998. }
  999. } else {
  1000. flag0 = *environment.flags;
  1001. dev_current = 1;
  1002. addr1 = calloc(1, CUR_ENVSIZE);
  1003. if (addr1 == NULL) {
  1004. fprintf(stderr,
  1005. "Not enough memory for environment (%ld bytes)\n",
  1006. CUR_ENVSIZE);
  1007. return -1;
  1008. }
  1009. redundant = addr1;
  1010. /*
  1011. * have to set environment.image for flash_read(), careful -
  1012. * other pointers in environment still point inside addr0
  1013. */
  1014. environment.image = addr1;
  1015. if (flash_io (O_RDONLY))
  1016. return -1;
  1017. /* Check flag scheme compatibility */
  1018. if (DEVTYPE(dev_current) == MTD_NORFLASH &&
  1019. DEVTYPE(!dev_current) == MTD_NORFLASH) {
  1020. environment.flag_scheme = FLAG_BOOLEAN;
  1021. } else if (DEVTYPE(dev_current) == MTD_NANDFLASH &&
  1022. DEVTYPE(!dev_current) == MTD_NANDFLASH) {
  1023. environment.flag_scheme = FLAG_INCREMENTAL;
  1024. } else if (DEVTYPE(dev_current) == MTD_DATAFLASH &&
  1025. DEVTYPE(!dev_current) == MTD_DATAFLASH) {
  1026. environment.flag_scheme = FLAG_BOOLEAN;
  1027. } else if (DEVTYPE(dev_current) == MTD_UBIVOLUME &&
  1028. DEVTYPE(!dev_current) == MTD_UBIVOLUME) {
  1029. environment.flag_scheme = FLAG_INCREMENTAL;
  1030. } else if (DEVTYPE(dev_current) == MTD_ABSENT &&
  1031. DEVTYPE(!dev_current) == MTD_ABSENT) {
  1032. environment.flag_scheme = FLAG_INCREMENTAL;
  1033. } else {
  1034. fprintf (stderr, "Incompatible flash types!\n");
  1035. return -1;
  1036. }
  1037. crc1 = crc32 (0, (uint8_t *) redundant->data, ENV_SIZE);
  1038. if (opts->aes_flag) {
  1039. ret = env_aes_cbc_crypt(redundant->data, 0,
  1040. opts->aes_key);
  1041. if (ret)
  1042. return ret;
  1043. }
  1044. crc1_ok = (crc1 == redundant->crc);
  1045. flag1 = redundant->flags;
  1046. if (crc0_ok && !crc1_ok) {
  1047. dev_current = 0;
  1048. } else if (!crc0_ok && crc1_ok) {
  1049. dev_current = 1;
  1050. } else if (!crc0_ok && !crc1_ok) {
  1051. fprintf (stderr,
  1052. "Warning: Bad CRC, using default environment\n");
  1053. memcpy (environment.data, default_environment,
  1054. sizeof default_environment);
  1055. dev_current = 0;
  1056. } else {
  1057. switch (environment.flag_scheme) {
  1058. case FLAG_BOOLEAN:
  1059. if (flag0 == active_flag &&
  1060. flag1 == obsolete_flag) {
  1061. dev_current = 0;
  1062. } else if (flag0 == obsolete_flag &&
  1063. flag1 == active_flag) {
  1064. dev_current = 1;
  1065. } else if (flag0 == flag1) {
  1066. dev_current = 0;
  1067. } else if (flag0 == 0xFF) {
  1068. dev_current = 0;
  1069. } else if (flag1 == 0xFF) {
  1070. dev_current = 1;
  1071. } else {
  1072. dev_current = 0;
  1073. }
  1074. break;
  1075. case FLAG_INCREMENTAL:
  1076. if (flag0 == 255 && flag1 == 0)
  1077. dev_current = 1;
  1078. else if ((flag1 == 255 && flag0 == 0) ||
  1079. flag0 >= flag1)
  1080. dev_current = 0;
  1081. else /* flag1 > flag0 */
  1082. dev_current = 1;
  1083. break;
  1084. default:
  1085. fprintf (stderr, "Unknown flag scheme %u \n",
  1086. environment.flag_scheme);
  1087. return -1;
  1088. }
  1089. }
  1090. /*
  1091. * If we are reading, we don't need the flag and the CRC any
  1092. * more, if we are writing, we will re-calculate CRC and update
  1093. * flags before writing out
  1094. */
  1095. if (dev_current) {
  1096. environment.image = addr1;
  1097. environment.crc = &redundant->crc;
  1098. environment.flags = &redundant->flags;
  1099. environment.data = redundant->data;
  1100. free (addr0);
  1101. } else {
  1102. environment.image = addr0;
  1103. /* Other pointers are already set */
  1104. free (addr1);
  1105. }
  1106. #ifdef DEBUG
  1107. fprintf(stderr, "Selected env in %s\n", DEVNAME(dev_current));
  1108. #endif
  1109. }
  1110. return 0;
  1111. }
  1112. static int check_device_config(int dev)
  1113. {
  1114. struct stat st;
  1115. int fd, rc = 0;
  1116. if (DEVOFFSET(dev) % DEVESIZE(dev) != 0) {
  1117. fprintf(stderr, "Environment does not start on (erase) block boundary\n");
  1118. errno = EINVAL;
  1119. return -1;
  1120. }
  1121. if (ENVSIZE(dev) > ENVSECTORS(dev) * DEVESIZE(dev)) {
  1122. fprintf(stderr, "Environment does not fit into available sectors\n");
  1123. errno = EINVAL;
  1124. return -1;
  1125. }
  1126. fd = open(DEVNAME(dev), O_RDONLY);
  1127. if (fd < 0) {
  1128. fprintf(stderr,
  1129. "Cannot open %s: %s\n",
  1130. DEVNAME(dev), strerror(errno));
  1131. return -1;
  1132. }
  1133. rc = fstat(fd, &st);
  1134. if (rc < 0) {
  1135. fprintf(stderr, "Cannot stat the file %s\n",
  1136. DEVNAME(dev));
  1137. goto err;
  1138. }
  1139. if (S_ISCHR(st.st_mode)) {
  1140. struct mtd_info_user mtdinfo;
  1141. rc = ioctl(fd, MEMGETINFO, &mtdinfo);
  1142. if (rc < 0) {
  1143. fprintf(stderr, "Cannot get MTD information for %s\n",
  1144. DEVNAME(dev));
  1145. goto err;
  1146. }
  1147. if (mtdinfo.type != MTD_NORFLASH &&
  1148. mtdinfo.type != MTD_NANDFLASH &&
  1149. mtdinfo.type != MTD_DATAFLASH &&
  1150. mtdinfo.type != MTD_UBIVOLUME) {
  1151. fprintf(stderr, "Unsupported flash type %u on %s\n",
  1152. mtdinfo.type, DEVNAME(dev));
  1153. goto err;
  1154. }
  1155. DEVTYPE(dev) = mtdinfo.type;
  1156. } else {
  1157. uint64_t size;
  1158. DEVTYPE(dev) = MTD_ABSENT;
  1159. /*
  1160. * Check for negative offsets, treat it as backwards offset
  1161. * from the end of the block device
  1162. */
  1163. if (DEVOFFSET(dev) < 0) {
  1164. rc = ioctl(fd, BLKGETSIZE64, &size);
  1165. if (rc < 0) {
  1166. fprintf(stderr, "Could not get block device size on %s\n",
  1167. DEVNAME(dev));
  1168. goto err;
  1169. }
  1170. DEVOFFSET(dev) = DEVOFFSET(dev) + size;
  1171. #ifdef DEBUG
  1172. fprintf(stderr, "Calculated device offset 0x%llx on %s\n",
  1173. DEVOFFSET(dev), DEVNAME(dev));
  1174. #endif
  1175. }
  1176. }
  1177. err:
  1178. close(fd);
  1179. return rc;
  1180. }
  1181. static int parse_config(struct env_opts *opts)
  1182. {
  1183. int rc;
  1184. if (!opts)
  1185. opts = &default_opts;
  1186. #if defined(CONFIG_FILE)
  1187. /* Fills in DEVNAME(), ENVSIZE(), DEVESIZE(). Or don't. */
  1188. if (get_config(opts->config_file)) {
  1189. fprintf(stderr, "Cannot parse config file '%s': %m\n",
  1190. opts->config_file);
  1191. return -1;
  1192. }
  1193. #else
  1194. DEVNAME (0) = DEVICE1_NAME;
  1195. DEVOFFSET (0) = DEVICE1_OFFSET;
  1196. ENVSIZE (0) = ENV1_SIZE;
  1197. /* Default values are: erase-size=env-size */
  1198. DEVESIZE (0) = ENVSIZE (0);
  1199. /* #sectors=env-size/erase-size (rounded up) */
  1200. ENVSECTORS (0) = (ENVSIZE(0) + DEVESIZE(0) - 1) / DEVESIZE(0);
  1201. #ifdef DEVICE1_ESIZE
  1202. DEVESIZE (0) = DEVICE1_ESIZE;
  1203. #endif
  1204. #ifdef DEVICE1_ENVSECTORS
  1205. ENVSECTORS (0) = DEVICE1_ENVSECTORS;
  1206. #endif
  1207. #ifdef HAVE_REDUND
  1208. DEVNAME (1) = DEVICE2_NAME;
  1209. DEVOFFSET (1) = DEVICE2_OFFSET;
  1210. ENVSIZE (1) = ENV2_SIZE;
  1211. /* Default values are: erase-size=env-size */
  1212. DEVESIZE (1) = ENVSIZE (1);
  1213. /* #sectors=env-size/erase-size (rounded up) */
  1214. ENVSECTORS (1) = (ENVSIZE(1) + DEVESIZE(1) - 1) / DEVESIZE(1);
  1215. #ifdef DEVICE2_ESIZE
  1216. DEVESIZE (1) = DEVICE2_ESIZE;
  1217. #endif
  1218. #ifdef DEVICE2_ENVSECTORS
  1219. ENVSECTORS (1) = DEVICE2_ENVSECTORS;
  1220. #endif
  1221. HaveRedundEnv = 1;
  1222. #endif
  1223. #endif
  1224. rc = check_device_config(0);
  1225. if (rc < 0)
  1226. return rc;
  1227. if (HaveRedundEnv) {
  1228. rc = check_device_config(1);
  1229. if (rc < 0)
  1230. return rc;
  1231. if (ENVSIZE(0) != ENVSIZE(1)) {
  1232. fprintf(stderr,
  1233. "Redundant environments have unequal size");
  1234. return -1;
  1235. }
  1236. }
  1237. usable_envsize = CUR_ENVSIZE - sizeof(uint32_t);
  1238. if (HaveRedundEnv)
  1239. usable_envsize -= sizeof(char);
  1240. if (opts->aes_flag)
  1241. usable_envsize &= ~(AES_KEY_LENGTH - 1);
  1242. return 0;
  1243. }
  1244. #if defined(CONFIG_FILE)
  1245. static int get_config (char *fname)
  1246. {
  1247. FILE *fp;
  1248. int i = 0;
  1249. int rc;
  1250. char dump[128];
  1251. char *devname;
  1252. fp = fopen (fname, "r");
  1253. if (fp == NULL)
  1254. return -1;
  1255. while (i < 2 && fgets (dump, sizeof (dump), fp)) {
  1256. /* Skip incomplete conversions and comment strings */
  1257. if (dump[0] == '#')
  1258. continue;
  1259. rc = sscanf(dump, "%ms %lli %lx %lx %lx",
  1260. &devname,
  1261. &DEVOFFSET(i),
  1262. &ENVSIZE(i),
  1263. &DEVESIZE(i),
  1264. &ENVSECTORS(i));
  1265. if (rc < 3)
  1266. continue;
  1267. DEVNAME(i) = devname;
  1268. if (rc < 4)
  1269. /* Assume the erase size is the same as the env-size */
  1270. DEVESIZE(i) = ENVSIZE(i);
  1271. if (rc < 5)
  1272. /* Assume enough env sectors to cover the environment */
  1273. ENVSECTORS (i) = (ENVSIZE(i) + DEVESIZE(i) - 1) / DEVESIZE(i);
  1274. i++;
  1275. }
  1276. fclose (fp);
  1277. HaveRedundEnv = i - 1;
  1278. if (!i) { /* No valid entries found */
  1279. errno = EINVAL;
  1280. return -1;
  1281. } else
  1282. return 0;
  1283. }
  1284. #endif