summaryrefslogtreecommitdiff
path: root/package/boot/uboot-envtools/patches/115-writing-environment-for-mtd-devices.patch
blob: 96d0fd98921278d98e1d9c864d00f8942d5f80b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
tools/fw_env: fix writing environment for mtd devices

Signed-off-by: Oliver Metz <oliver@freetz.org>
---
 tools/env/fw_env.c | 71 ++++++++++++++++++++++++++++++++----------------------
 1 file changed, 42 insertions(+), 29 deletions(-)

--- a/tools/env/fw_env.c
+++ b/tools/env/fw_env.c
@@ -743,27 +743,39 @@ static int flash_write_buf (int dev, int
 				   MEMGETBADBLOCK needs 64 bits */
 	int rc;
 
-	blocklen = DEVESIZE (dev);
+	/*
+	 * For mtd devices only offset and size of the environment do matter
+	 */
+	if (mtd_type == MTD_ABSENT) {
+		blocklen = count;
+		top_of_range = offset + count;
+		erase_len = blocklen;
+		blockstart = offset;
+		block_seek = 0;
+		write_total = blocklen;
+	} else {
+		blocklen = DEVESIZE (dev);
 
-	top_of_range = ((DEVOFFSET(dev) / blocklen) +
-					ENVSECTORS (dev)) * blocklen;
+		top_of_range = ((DEVOFFSET(dev) / blocklen) +
+						ENVSECTORS (dev)) * blocklen;
 
-	erase_offset = (offset / blocklen) * blocklen;
+		erase_offset = (offset / blocklen) * blocklen;
 
-	/* Maximum area we may use */
-	erase_len = top_of_range - erase_offset;
+		/* Maximum area we may use */
+		erase_len = top_of_range - erase_offset;
 
-	blockstart = erase_offset;
-	/* Offset inside a block */
-	block_seek = offset - erase_offset;
+		blockstart = erase_offset;
+		/* Offset inside a block */
+		block_seek = offset - erase_offset;
 
-	/*
-	 * Data size we actually have to write: from the start of the block
-	 * to the start of the data, then count bytes of data, and to the
-	 * end of the block
-	 */
-	write_total = ((block_seek + count + blocklen - 1) /
-						blocklen) * blocklen;
+		/*
+		 * Data size we actually write: from the start of the block
+		 * to the start of the data, then count bytes of data, and to the
+		 * end of the block
+		 */
+		write_total = ((block_seek + count + blocklen - 1) /
+							blocklen) * blocklen;
+	}
 
 	/*
 	 * Support data anywhere within erase sectors: read out the complete
@@ -834,17 +846,18 @@ static int flash_write_buf (int dev, int
 			continue;
 		}
 
-		erase.start = blockstart;
-		ioctl (fd, MEMUNLOCK, &erase);
-		/* These do not need an explicit erase cycle */
-		if (mtd_type != MTD_ABSENT &&
-		    mtd_type != MTD_DATAFLASH)
-			if (ioctl (fd, MEMERASE, &erase) != 0) {
-				fprintf (stderr, "MTD erase error on %s: %s\n",
-					 DEVNAME (dev),
-					 strerror (errno));
-				return -1;
-			}
+		if (mtd_type != MTD_ABSENT) {
+			erase.start = blockstart;
+			ioctl (fd, MEMUNLOCK, &erase);
+			/* These do not need an explicit erase cycle */
+			if (mtd_type != MTD_DATAFLASH)
+				if (ioctl (fd, MEMERASE, &erase) != 0) {
+					fprintf (stderr, "MTD erase error on %s: %s\n",
+						 DEVNAME (dev),
+						 strerror (errno));
+					return -1;
+				}
+		}
 
 		if (lseek (fd, blockstart, SEEK_SET) == -1) {
 			fprintf (stderr,
@@ -862,8 +875,8 @@ static int flash_write_buf (int dev, int
 				 DEVNAME (dev), strerror (errno));
 			return -1;
 		}
-
-		ioctl (fd, MEMLOCK, &erase);
+		if (mtd_type != MTD_ABSENT)
+			ioctl (fd, MEMLOCK, &erase);
 
 		processed  += blocklen;
 		block_seek = 0;