Skip to contentSkip to conversation

tools/env: ensure environment starts at erase block boundary

RFC In review Andreas Fenkart 3 participants 3 messages Aug 11, 2016 – Aug 16, 2016

lore
Share & tools
lore
#0 lore raw

RFC v2 1/1 tools/env: ensure environment starts at erase block boundary

56086921 added support for unaligned environments access. U-boot itself does not support this:
- env_nand.c fails when using an unaligned offset. It produces an error in nand_erase_opts{drivers/mtd/nand/nand_util.c}
- in env_sf/env_flash the unused space at the end is preserved, but not in the beginning. block alignment is assumed
- env_sata/env_mmc aligns offset/length to the block size of the underlying device. data is silently redirected to the beginning of a block

There is seems no use case for unaligned environment. If there is some useful data at the beginning of the the block (e.g. end of u-boot) that would be very unsafe. If the redundant environments are hosted by the same erase block then that invalidates the idea of double buffering. It might be that unaligned access was allowed in the past, and that people with legacy u-boot are trapped. But at the time of 56086921 it wasn't supported and due to reasons above I guess it was never introduced.
I prefer to remove that (unused) feature in favor of simplicity

---
 tools/env/fw_env.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
tools/env/fw_env.c +12 -0
index 6b0dcaa..d2b167d 100644
@@ -1294,6 +1294,18 @@ static int check_device_config(int dev)
 	struct stat st;
 	int fd, rc = 0;
 
+	if (DEVOFFSET(dev) % DEVESIZE(dev) != 0) {
+		fprintf(stderr, "Environment does not start on erase block boundary\n");
+		errno = EINVAL;
+		return -1;
+	}
+
+	if (ENVSIZE(dev) > ENVSECTORS(dev) * DEVESIZE(dev)) {
+		fprintf(stderr, "Environment does not fit into available sectors\n");
+		errno = EINVAL;
+		return -1;
+	}
+
 	fd = open(DEVNAME(dev), O_RDONLY);
 	if (fd < 0) {
 		fprintf(stderr,
Signature · 1 line
2.8.1
Acked-by on v2 1/1
#1 lore raw
Quoted 10 lines · Andreas Fenkart

56086921 added support for unaligned environments access. U-boot itself does not support this:
- env_nand.c fails when using an unaligned offset. It produces an error in nand_erase_opts{drivers/mtd/nand/nand_util.c}
- in env_sf/env_flash the unused space at the end is preserved, but not in the beginning. block alignment is assumed
- env_sata/env_mmc aligns offset/length to the block size of the underlying device. data is silently redirected to the beginning of a block

Quoted 9 lines

There is seems no use case for unaligned environment. If there is some useful data at the beginning of the the block (e.g. end of u-boot) that would be very unsafe. If the redundant environments are hosted by the same erase block then that invalidates the idea of double buffering. It might be that unaligned access was allowed in the past, and that people with legacy u-boot are trapped. But at the time of 56086921 it wasn't supported and due to reasons above I guess it was never introduced.
I prefer to remove that (unused) feature in favor of simplicity

I also don't see any value supporting unaligned environment, so FWIW:

Small nit below:

Quoted 11 lines
---
 tools/env/fw_env.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
tools/env/fw_env.c +0 -0
index 6b0dcaa..d2b167d 100644

tools/env/fw_env.c static int check_device_config(int dev) · line 1298

Quoted 5 lines
tools/env/fw_env.c +2 -0
@@ -1294,6 +1294,18 @@ static int check_device_config(int dev)
 	struct stat st;
 	int fd, rc = 0;
 
+	if (DEVOFFSET(dev) % DEVESIZE(dev) != 0) {
+		fprintf(stderr, "Environment does not start on erase block boundary\n");

Erase block is sometwhat confusing in the MMC context, maybe parenthesize "erase"?

--
Stefan

Quoted 13 lines
tools/env/fw_env.c +10 -0
+		errno = EINVAL;
+		return -1;
+	}
+
+	if (ENVSIZE(dev) > ENVSECTORS(dev) * DEVESIZE(dev)) {
+		fprintf(stderr, "Environment does not fit into available sectors\n");
+		errno = EINVAL;
+		return -1;
+	}
+
 	fd = open(DEVNAME(dev), O_RDONLY);
 	if (fd < 0) {
 		fprintf(stderr,
#2 lore raw
In reply to Andreas Fenkart
Quoted 20 lines · Andreas Fenkart

56086921 added support for unaligned environments access. U-boot itself does not support this:
- env_nand.c fails when using an unaligned offset. It produces an error in nand_erase_opts{drivers/mtd/nand/nand_util.c}
- in env_sf/env_flash the unused space at the end is preserved, but not in the beginning. block alignment is assumed
- env_sata/env_mmc aligns offset/length to the block size of the underlying device. data is silently redirected to the beginning of a block

There is seems no use case for unaligned environment. If there is some useful data at the beginning of the the block (e.g. end of u-boot) that would be very unsafe. If the redundant environments are hosted by the same erase block then that invalidates the idea of double buffering. It might be that unaligned access was allowed in the past, and that people with legacy u-boot are trapped. But at the time of 56086921 it wasn't supported and due to reasons above I guess it was never introduced.
I prefer to remove that (unused) feature in favor of simplicity

Quoted 2 lines

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20160815/5d9cae0e/attachment.sig>