1 tools/fw_env: fix writing environment for mtd devices
3 Signed-off-by: Oliver Metz <oliver@freetz.org>
5 tools/env/fw_env.c | 71 ++++++++++++++++++++++++++++++++----------------------
6 1 file changed, 42 insertions(+), 29 deletions(-)
8 --- a/tools/env/fw_env.c
9 +++ b/tools/env/fw_env.c
10 @@ -743,27 +743,39 @@ static int flash_write_buf (int dev, int
11 MEMGETBADBLOCK needs 64 bits */
14 - blocklen = DEVESIZE (dev);
16 + * For mtd devices only offset and size of the environment do matter
18 + if (mtd_type == MTD_ABSENT) {
20 + top_of_range = offset + count;
21 + erase_len = blocklen;
22 + blockstart = offset;
24 + write_total = blocklen;
26 + blocklen = DEVESIZE (dev);
28 - top_of_range = ((DEVOFFSET(dev) / blocklen) +
29 - ENVSECTORS (dev)) * blocklen;
30 + top_of_range = ((DEVOFFSET(dev) / blocklen) +
31 + ENVSECTORS (dev)) * blocklen;
33 - erase_offset = (offset / blocklen) * blocklen;
34 + erase_offset = (offset / blocklen) * blocklen;
36 - /* Maximum area we may use */
37 - erase_len = top_of_range - erase_offset;
38 + /* Maximum area we may use */
39 + erase_len = top_of_range - erase_offset;
41 - blockstart = erase_offset;
42 - /* Offset inside a block */
43 - block_seek = offset - erase_offset;
44 + blockstart = erase_offset;
45 + /* Offset inside a block */
46 + block_seek = offset - erase_offset;
49 - * Data size we actually have to write: from the start of the block
50 - * to the start of the data, then count bytes of data, and to the
53 - write_total = ((block_seek + count + blocklen - 1) /
54 - blocklen) * blocklen;
56 + * Data size we actually write: from the start of the block
57 + * to the start of the data, then count bytes of data, and to the
60 + write_total = ((block_seek + count + blocklen - 1) /
61 + blocklen) * blocklen;
65 * Support data anywhere within erase sectors: read out the complete
66 @@ -834,17 +846,18 @@ static int flash_write_buf (int dev, int
70 - erase.start = blockstart;
71 - ioctl (fd, MEMUNLOCK, &erase);
72 - /* These do not need an explicit erase cycle */
73 - if (mtd_type != MTD_ABSENT &&
74 - mtd_type != MTD_DATAFLASH)
75 - if (ioctl (fd, MEMERASE, &erase) != 0) {
76 - fprintf (stderr, "MTD erase error on %s: %s\n",
81 + if (mtd_type != MTD_ABSENT) {
82 + erase.start = blockstart;
83 + ioctl (fd, MEMUNLOCK, &erase);
84 + /* These do not need an explicit erase cycle */
85 + if (mtd_type != MTD_DATAFLASH)
86 + if (ioctl (fd, MEMERASE, &erase) != 0) {
87 + fprintf (stderr, "MTD erase error on %s: %s\n",
94 if (lseek (fd, blockstart, SEEK_SET) == -1) {
96 @@ -862,8 +875,8 @@ static int flash_write_buf (int dev, int
97 DEVNAME (dev), strerror (errno));
101 - ioctl (fd, MEMLOCK, &erase);
102 + if (mtd_type != MTD_ABSENT)
103 + ioctl (fd, MEMLOCK, &erase);
105 processed += blocklen;