1 From: Oliver Metz <oliver@freetz.org>
2 Subject: [PATCH v2 2/2] fw_env: fix writing environment for mtd devices
4 Signed-off-by: Oliver Metz <oliver@freetz.org>
5 Tested-by: Luka Perkov <luka@openwrt.org>
9 * fix checkpatch.pl warnings
11 http://lists.denx.de/pipermail/u-boot/2013-August/161695.html
12 http://patchwork.ozlabs.org/patch/270994/
14 tools/env/fw_env.c | 70 ++++++++++++++++++++++++++++++++----------------------
15 1 file changed, 42 insertions(+), 28 deletions(-)
17 --- a/tools/env/fw_env.c
18 +++ b/tools/env/fw_env.c
19 @@ -743,27 +743,39 @@ static int flash_write_buf (int dev, int
20 MEMGETBADBLOCK needs 64 bits */
23 - blocklen = DEVESIZE (dev);
25 + * For mtd devices only offset and size of the environment do matter
27 + if (mtd_type == MTD_ABSENT) {
29 + top_of_range = offset + count;
30 + erase_len = blocklen;
31 + blockstart = offset;
33 + write_total = blocklen;
35 + blocklen = DEVESIZE(dev);
37 - top_of_range = ((DEVOFFSET(dev) / blocklen) +
38 - ENVSECTORS (dev)) * blocklen;
39 + top_of_range = ((DEVOFFSET(dev) / blocklen) +
40 + ENVSECTORS(dev)) * blocklen;
42 - erase_offset = (offset / blocklen) * blocklen;
43 + erase_offset = (offset / blocklen) * blocklen;
45 - /* Maximum area we may use */
46 - erase_len = top_of_range - erase_offset;
47 + /* Maximum area we may use */
48 + erase_len = top_of_range - erase_offset;
50 - blockstart = erase_offset;
51 - /* Offset inside a block */
52 - block_seek = offset - erase_offset;
53 + blockstart = erase_offset;
54 + /* Offset inside a block */
55 + block_seek = offset - erase_offset;
58 - * Data size we actually have to write: from the start of the block
59 - * to the start of the data, then count bytes of data, and to the
62 - write_total = ((block_seek + count + blocklen - 1) /
63 - blocklen) * blocklen;
65 + * Data size we actually write: from the start of the block
66 + * to the start of the data, then count bytes of data, and
67 + * to the end of the block
69 + write_total = ((block_seek + count + blocklen - 1) /
70 + blocklen) * blocklen;
74 * Support data anywhere within erase sectors: read out the complete
75 @@ -834,17 +846,18 @@ static int flash_write_buf (int dev, int
79 - erase.start = blockstart;
80 - ioctl (fd, MEMUNLOCK, &erase);
81 - /* These do not need an explicit erase cycle */
82 - if (mtd_type != MTD_ABSENT &&
83 - mtd_type != MTD_DATAFLASH)
84 - if (ioctl (fd, MEMERASE, &erase) != 0) {
85 - fprintf (stderr, "MTD erase error on %s: %s\n",
90 + if (mtd_type != MTD_ABSENT) {
91 + erase.start = blockstart;
92 + ioctl(fd, MEMUNLOCK, &erase);
93 + /* These do not need an explicit erase cycle */
94 + if (mtd_type != MTD_DATAFLASH)
95 + if (ioctl(fd, MEMERASE, &erase) != 0) {
97 + "MTD erase error on %s: %s\n",
98 + DEVNAME(dev), strerror(errno));
103 if (lseek (fd, blockstart, SEEK_SET) == -1) {
105 @@ -863,7 +876,8 @@ static int flash_write_buf (int dev, int
109 - ioctl (fd, MEMLOCK, &erase);
110 + if (mtd_type != MTD_ABSENT)
111 + ioctl(fd, MEMLOCK, &erase);
113 processed += blocklen;