[U-Boot] [PATCH 1/2] tools/env: complete environment device config early

Andreas Fenkart afenkart at gmail.com
Sun Jul 17 15:18:10 CEST 2016


2016-07-14 2:14 GMT+02:00 Stefan Agner <stefan at agner.ch>:
> From: Stefan Agner <stefan.agner at toradex.com>
>
> 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: Stefan Agner <stefan.agner at toradex.com>
Reviewed-by: Andreas Fenkart
> ---
>
>  tools/env/fw_env.c | 110 +++++++++++++++++++++++++++++------------------------
>  1 file changed, 60 insertions(+), 50 deletions(-)
>
> diff --git a/tools/env/fw_env.c b/tools/env/fw_env.c
> index 692abda..06d6865 100644
> --- a/tools/env/fw_env.c
> +++ b/tools/env/fw_env.c
> @@ -1021,41 +1021,10 @@ static int flash_write (int fd_current, int fd_target, int dev_target)
>
>  static int flash_read (int fd)
>  {
> -       struct mtd_info_user mtdinfo;
> -       struct stat st;
>         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,
> -                            DEVOFFSET (dev_current), mtdinfo.type);
> +                           DEVOFFSET(dev_current), DEVTYPE(dev_current));
>         if (rc != CUR_ENVSIZE)
>                 return -1;
>
> @@ -1328,10 +1297,55 @@ int fw_env_open(struct env_opts *opts)
>         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)
>  {
> -       struct stat st;
> +       int rc;
>
>         if (!opts)
>                 opts = &default_opts;
> @@ -1375,25 +1389,21 @@ static int parse_config(struct env_opts *opts)
>         HaveRedundEnv = 1;
>  #endif
>  #endif
> -       if (stat (DEVNAME (0), &st)) {
> -               fprintf (stderr,
> -                       "Cannot access MTD device %s: %s\n",
> -                       DEVNAME (0), strerror (errno));
> -               return -1;
> -       }
> +       rc = check_device_config(0);
> +       if (rc < 0)
> +               return rc;
>
> -       if (HaveRedundEnv && stat (DEVNAME (1), &st)) {
> -               fprintf (stderr,
> -                       "Cannot access MTD device %s: %s\n",
> -                       DEVNAME (1), strerror (errno));
> -               return -1;
> -       }
> +       if (HaveRedundEnv) {
> +               rc = check_device_config(1);
> +               if (rc < 0)
> +                       return rc;
>
> -       if (HaveRedundEnv && ENVSIZE(0) != ENVSIZE(1)) {
> -               ENVSIZE(0) = ENVSIZE(1) = min(ENVSIZE(0), ENVSIZE(1));
> -               fprintf(stderr,
> -                       "Redundant environments have inequal size, set to 0x%08lx\n",
> -                       ENVSIZE(1));
> +               if (ENVSIZE(0) != ENVSIZE(1)) {
> +                       ENVSIZE(0) = ENVSIZE(1) = min(ENVSIZE(0), ENVSIZE(1));
> +                       fprintf(stderr,
> +                               "Redundant environments have inequal size, set to 0x%08lx\n",
> +                               ENVSIZE(1));
> +               }
>         }
>
>         usable_envsize = CUR_ENVSIZE - sizeof(uint32_t);
> --
> 2.9.0
>


More information about the U-Boot mailing list