Commit f4742ca0fb94e531b8dcddd1ac0799ee4c9c2605

Authored by Stefan Agner
Committed by Tom Rini
1 parent 14fb5b252a

tools/env: allow negative offsets

A negative value for the offset is treated as a backwards offset for
from the end of the device/partition for block devices. This aligns
the behavior of the config file with the syntax of CONFIG_ENV_OFFSET
where the functionality has been introduced with
commit 5c088ee841f9 ("env_mmc: allow negative CONFIG_ENV_OFFSET").

Signed-off-by: Stefan Agner <stefan.agner@toradex.com>

Showing 2 changed files with 35 additions and 9 deletions Side-by-side Diff

... ... @@ -14,6 +14,7 @@
14 14 #include <errno.h>
15 15 #include <env_flags.h>
16 16 #include <fcntl.h>
  17 +#include <linux/fs.h>
17 18 #include <linux/stringify.h>
18 19 #include <ctype.h>
19 20 #include <stdio.h>
... ... @@ -51,7 +52,7 @@
51 52  
52 53 struct envdev_s {
53 54 const char *devname; /* Device name */
54   - ulong devoff; /* Device offset */
  55 + long long devoff; /* Device offset */
55 56 ulong env_size; /* environment size */
56 57 ulong erase_size; /* device erase size */
57 58 ulong env_sectors; /* number of environment sectors */
... ... @@ -994,7 +995,7 @@
994 995 }
995 996  
996 997 #ifdef DEBUG
997   - fprintf(stderr, "Writing new environment at 0x%lx on %s\n",
  998 + fprintf(stderr, "Writing new environment at 0x%llx on %s\n",
998 999 DEVOFFSET (dev_target), DEVNAME (dev_target));
999 1000 #endif
1000 1001  
... ... @@ -1010,7 +1011,7 @@
1010 1011 offsetof (struct env_image_redundant, flags);
1011 1012 #ifdef DEBUG
1012 1013 fprintf(stderr,
1013   - "Setting obsolete flag in environment at 0x%lx on %s\n",
  1014 + "Setting obsolete flag in environment at 0x%llx on %s\n",
1014 1015 DEVOFFSET (dev_current), DEVNAME (dev_current));
1015 1016 #endif
1016 1017 flash_flag_obsolete (dev_current, fd_current, offset);
1017 1018  
... ... @@ -1335,7 +1336,27 @@
1335 1336 }
1336 1337 DEVTYPE(dev) = mtdinfo.type;
1337 1338 } else {
  1339 + uint64_t size;
1338 1340 DEVTYPE(dev) = MTD_ABSENT;
  1341 +
  1342 + /*
  1343 + * Check for negative offsets, treat it as backwards offset
  1344 + * from the end of the block device
  1345 + */
  1346 + if (DEVOFFSET(dev) < 0) {
  1347 + rc = ioctl(fd, BLKGETSIZE64, &size);
  1348 + if (rc < 0) {
  1349 + fprintf(stderr, "Could not get block device size on %s\n",
  1350 + DEVNAME(dev));
  1351 + goto err;
  1352 + }
  1353 +
  1354 + DEVOFFSET(dev) = DEVOFFSET(dev) + size;
  1355 +#ifdef DEBUG
  1356 + fprintf(stderr, "Calculated device offset 0x%llx on %s\n",
  1357 + DEVOFFSET(dev), DEVNAME(dev));
  1358 +#endif
  1359 + }
1339 1360 }
1340 1361  
1341 1362 err:
... ... @@ -1434,12 +1455,12 @@
1434 1455 if (dump[0] == '#')
1435 1456 continue;
1436 1457  
1437   - rc = sscanf (dump, "%ms %lx %lx %lx %lx",
1438   - &devname,
1439   - &DEVOFFSET (i),
1440   - &ENVSIZE (i),
1441   - &DEVESIZE (i),
1442   - &ENVSECTORS (i));
  1458 + rc = sscanf(dump, "%ms %lli %lx %lx %lx",
  1459 + &devname,
  1460 + &DEVOFFSET(i),
  1461 + &ENVSIZE(i),
  1462 + &DEVESIZE(i),
  1463 + &ENVSECTORS(i));
1443 1464  
1444 1465 if (rc < 3)
1445 1466 continue;
tools/env/fw_env.config
... ... @@ -4,6 +4,7 @@
4 4 # Notice, that the "Number of sectors" is not required on NOR and SPI-dataflash.
5 5 # Futhermore, if the Flash sector size is omitted, this value is assumed to
6 6 # be the same as the Environment size, which is valid for NOR and SPI-dataflash
  7 +# Device offset must be prefixed with 0x to be parsed as a hexadecimal value.
7 8  
8 9 # NOR example
9 10 # MTD device name Device offset Env. size Flash sector size Number of sectors
10 11  
... ... @@ -18,8 +19,12 @@
18 19 # NAND example
19 20 #/dev/mtd0 0x4000 0x4000 0x20000 2
20 21  
  22 +# On a block device a negative offset is treated as a backwards offset from the
  23 +# end of the device/partition, rather than a forwards offset from the start.
  24 +
21 25 # Block device example
22 26 #/dev/mmcblk0 0xc0000 0x20000
  27 +#/dev/mmcblk0 -0x20000 0x20000
23 28  
24 29 # VFAT example
25 30 #/boot/uboot.env 0x0000 0x4000