build: set owner root for all files on ubifs creation
[openwrt.git] / package / boot / uboot-envtools / patches / 0002-fw_env-fix-writing-environment-for-mtd-devices.patch
1 From: Oliver Metz <oliver@freetz.org>
2 Subject: [PATCH v2 2/2] fw_env: fix writing environment for mtd devices
3
4 Signed-off-by: Oliver Metz <oliver@freetz.org>
5 Tested-by: Luka Perkov <luka@openwrt.org>
6 ---
7
8 v1 -> v2:
9  * fix checkpatch.pl warnings
10
11 http://lists.denx.de/pipermail/u-boot/2013-August/161695.html
12 http://patchwork.ozlabs.org/patch/270994/
13
14  tools/env/fw_env.c | 70 ++++++++++++++++++++++++++++++++----------------------
15  1 file changed, 42 insertions(+), 28 deletions(-)
16
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 */
21         int rc;
22  
23 -       blocklen = DEVESIZE (dev);
24 +       /*
25 +        * For mtd devices only offset and size of the environment do matter
26 +        */
27 +       if (mtd_type == MTD_ABSENT) {
28 +               blocklen = count;
29 +               top_of_range = offset + count;
30 +               erase_len = blocklen;
31 +               blockstart = offset;
32 +               block_seek = 0;
33 +               write_total = blocklen;
34 +       } else {
35 +               blocklen = DEVESIZE(dev);
36  
37 -       top_of_range = ((DEVOFFSET(dev) / blocklen) +
38 -                                       ENVSECTORS (dev)) * blocklen;
39 +               top_of_range = ((DEVOFFSET(dev) / blocklen) +
40 +                                       ENVSECTORS(dev)) * blocklen;
41  
42 -       erase_offset = (offset / blocklen) * blocklen;
43 +               erase_offset = (offset / blocklen) * blocklen;
44  
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;
49  
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;
56  
57 -       /*
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
60 -        * end of the block
61 -        */
62 -       write_total = ((block_seek + count + blocklen - 1) /
63 -                                               blocklen) * blocklen;
64 +               /*
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
68 +                */
69 +               write_total = ((block_seek + count + blocklen - 1) /
70 +                                                       blocklen) * blocklen;
71 +       }
72  
73         /*
74          * Support data anywhere within erase sectors: read out the complete
75 @@ -834,17 +846,18 @@ static int flash_write_buf (int dev, int
76                         continue;
77                 }
78  
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",
86 -                                        DEVNAME (dev),
87 -                                        strerror (errno));
88 -                               return -1;
89 -                       }
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) {
96 +                                       fprintf(stderr,
97 +                                               "MTD erase error on %s: %s\n",
98 +                                               DEVNAME(dev), strerror(errno));
99 +                                       return -1;
100 +                               }
101 +               }
102  
103                 if (lseek (fd, blockstart, SEEK_SET) == -1) {
104                         fprintf (stderr,
105 @@ -863,7 +876,8 @@ static int flash_write_buf (int dev, int
106                         return -1;
107                 }
108  
109 -               ioctl (fd, MEMLOCK, &erase);
110 +               if (mtd_type != MTD_ABSENT)
111 +                       ioctl(fd, MEMLOCK, &erase);
112  
113                 processed  += blocklen;
114                 block_seek = 0;