Skip to content
Snippets Groups Projects
Commit 14fb5b25 authored by Stefan Agner's avatar Stefan Agner Committed by Tom Rini
Browse files

tools/env: complete environment device config early


Currently flash_read completes a crucial part of the environment
device configuration, the device type (mtd_type). This is rather
confusing as flash_io calls flash_read conditionally, and one might
think flash_write, which also makes use of mtd_type, gets called
before flash_read. But since flash_io is always called with O_RDONLY
first, this is not actually the case in reality.

However, it is much cleaner to complete and verify the config early
in parse_config. This also prepares the code for further extension.

Signed-off-by: default avatarStefan Agner <stefan.agner@toradex.com>
Reviewed-by: Andreas Fenkart
parent 28f0014b
Branches
Tags
No related merge requests found
...@@ -1021,41 +1021,10 @@ static int flash_write (int fd_current, int fd_target, int dev_target) ...@@ -1021,41 +1021,10 @@ static int flash_write (int fd_current, int fd_target, int dev_target)
static int flash_read (int fd) static int flash_read (int fd)
{ {
struct mtd_info_user mtdinfo;
struct stat st;
int rc; int rc;
rc = fstat(fd, &st);
if (rc < 0) {
fprintf(stderr, "Cannot stat the file %s\n",
DEVNAME(dev_current));
return -1;
}
if (S_ISCHR(st.st_mode)) {
rc = ioctl(fd, MEMGETINFO, &mtdinfo);
if (rc < 0) {
fprintf(stderr, "Cannot get MTD information for %s\n",
DEVNAME(dev_current));
return -1;
}
if (mtdinfo.type != MTD_NORFLASH &&
mtdinfo.type != MTD_NANDFLASH &&
mtdinfo.type != MTD_DATAFLASH &&
mtdinfo.type != MTD_UBIVOLUME) {
fprintf (stderr, "Unsupported flash type %u on %s\n",
mtdinfo.type, DEVNAME(dev_current));
return -1;
}
} else {
memset(&mtdinfo, 0, sizeof(mtdinfo));
mtdinfo.type = MTD_ABSENT;
}
DEVTYPE(dev_current) = mtdinfo.type;
rc = flash_read_buf(dev_current, fd, environment.image, CUR_ENVSIZE, rc = flash_read_buf(dev_current, fd, environment.image, CUR_ENVSIZE,
DEVOFFSET (dev_current), mtdinfo.type); DEVOFFSET(dev_current), DEVTYPE(dev_current));
if (rc != CUR_ENVSIZE) if (rc != CUR_ENVSIZE)
return -1; return -1;
...@@ -1328,10 +1297,55 @@ int fw_env_open(struct env_opts *opts) ...@@ -1328,10 +1297,55 @@ int fw_env_open(struct env_opts *opts)
return 0; return 0;
} }
static int check_device_config(int dev)
{
struct stat st;
int fd, rc = 0;
fd = open(DEVNAME(dev), O_RDONLY);
if (fd < 0) {
fprintf(stderr,
"Cannot open %s: %s\n",
DEVNAME(dev), strerror(errno));
return -1;
}
rc = fstat(fd, &st);
if (rc < 0) {
fprintf(stderr, "Cannot stat the file %s\n",
DEVNAME(dev));
goto err;
}
if (S_ISCHR(st.st_mode)) {
struct mtd_info_user mtdinfo;
rc = ioctl(fd, MEMGETINFO, &mtdinfo);
if (rc < 0) {
fprintf(stderr, "Cannot get MTD information for %s\n",
DEVNAME(dev));
goto err;
}
if (mtdinfo.type != MTD_NORFLASH &&
mtdinfo.type != MTD_NANDFLASH &&
mtdinfo.type != MTD_DATAFLASH &&
mtdinfo.type != MTD_UBIVOLUME) {
fprintf(stderr, "Unsupported flash type %u on %s\n",
mtdinfo.type, DEVNAME(dev));
goto err;
}
DEVTYPE(dev) = mtdinfo.type;
} else {
DEVTYPE(dev) = MTD_ABSENT;
}
err:
close(fd);
return rc;
}
static int parse_config(struct env_opts *opts) static int parse_config(struct env_opts *opts)
{ {
struct stat st; int rc;
if (!opts) if (!opts)
opts = &default_opts; opts = &default_opts;
...@@ -1375,26 +1389,22 @@ static int parse_config(struct env_opts *opts) ...@@ -1375,26 +1389,22 @@ static int parse_config(struct env_opts *opts)
HaveRedundEnv = 1; HaveRedundEnv = 1;
#endif #endif
#endif #endif
if (stat (DEVNAME (0), &st)) { rc = check_device_config(0);
fprintf (stderr, if (rc < 0)
"Cannot access MTD device %s: %s\n", return rc;
DEVNAME (0), strerror (errno));
return -1;
}
if (HaveRedundEnv && stat (DEVNAME (1), &st)) { if (HaveRedundEnv) {
fprintf (stderr, rc = check_device_config(1);
"Cannot access MTD device %s: %s\n", if (rc < 0)
DEVNAME (1), strerror (errno)); return rc;
return -1;
}
if (HaveRedundEnv && ENVSIZE(0) != ENVSIZE(1)) { if (ENVSIZE(0) != ENVSIZE(1)) {
ENVSIZE(0) = ENVSIZE(1) = min(ENVSIZE(0), ENVSIZE(1)); ENVSIZE(0) = ENVSIZE(1) = min(ENVSIZE(0), ENVSIZE(1));
fprintf(stderr, fprintf(stderr,
"Redundant environments have inequal size, set to 0x%08lx\n", "Redundant environments have inequal size, set to 0x%08lx\n",
ENVSIZE(1)); ENVSIZE(1));
} }
}
usable_envsize = CUR_ENVSIZE - sizeof(uint32_t); usable_envsize = CUR_ENVSIZE - sizeof(uint32_t);
if (HaveRedundEnv) if (HaveRedundEnv)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment