[package] uboot-envtools: upgrade to 2013.07-rc1
[openwrt.git] / package / boot / uboot-envtools / patches / 115-writing-environment-for-mtd-devices.patch
1 tools/fw_env: fix writing environment for mtd devices
2
3 Signed-off-by: Oliver Metz <oliver@freetz.org>
4 ---
5  tools/env/fw_env.c | 71 ++++++++++++++++++++++++++++++++----------------------
6  1 file changed, 42 insertions(+), 29 deletions(-)
7
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 */
12         int rc;
13  
14 -       blocklen = DEVESIZE (dev);
15 +       /*
16 +        * For mtd devices only offset and size of the environment do matter
17 +        */
18 +       if (mtd_type == MTD_ABSENT) {
19 +               blocklen = count;
20 +               top_of_range = offset + count;
21 +               erase_len = blocklen;
22 +               blockstart = offset;
23 +               block_seek = 0;
24 +               write_total = blocklen;
25 +       } else {
26 +               blocklen = DEVESIZE (dev);
27  
28 -       top_of_range = ((DEVOFFSET(dev) / blocklen) +
29 -                                       ENVSECTORS (dev)) * blocklen;
30 +               top_of_range = ((DEVOFFSET(dev) / blocklen) +
31 +                                               ENVSECTORS (dev)) * blocklen;
32  
33 -       erase_offset = (offset / blocklen) * blocklen;
34 +               erase_offset = (offset / blocklen) * blocklen;
35  
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;
40  
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;
47  
48 -       /*
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
51 -        * end of the block
52 -        */
53 -       write_total = ((block_seek + count + blocklen - 1) /
54 -                                               blocklen) * blocklen;
55 +               /*
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
58 +                * end of the block
59 +                */
60 +               write_total = ((block_seek + count + blocklen - 1) /
61 +                                                       blocklen) * blocklen;
62 +       }
63  
64         /*
65          * Support data anywhere within erase sectors: read out the complete
66 @@ -834,17 +846,18 @@ static int flash_write_buf (int dev, int
67                         continue;
68                 }
69  
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",
77 -                                        DEVNAME (dev),
78 -                                        strerror (errno));
79 -                               return -1;
80 -                       }
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",
88 +                                                DEVNAME (dev),
89 +                                                strerror (errno));
90 +                                       return -1;
91 +                               }
92 +               }
93  
94                 if (lseek (fd, blockstart, SEEK_SET) == -1) {
95                         fprintf (stderr,
96 @@ -862,8 +875,8 @@ static int flash_write_buf (int dev, int
97                                  DEVNAME (dev), strerror (errno));
98                         return -1;
99                 }
100 -
101 -               ioctl (fd, MEMLOCK, &erase);
102 +               if (mtd_type != MTD_ABSENT)
103 +                       ioctl (fd, MEMLOCK, &erase);
104  
105                 processed  += blocklen;
106                 block_seek = 0;