05ec74ae68f081ecae9c9377dd82e5b8fdcb48b3
[openwrt.git] / package / boot / uboot-envtools / patches / 300-support-env-in-ubivol-chardev.patch
1 From 6e2630a0fc872d0db34157972f6dc3941f6d66dd Mon Sep 17 00:00:00 2001
2 From: Daniel Golle <daniel@makrotopia.org>
3 Date: Mon, 19 May 2014 21:38:01 +0200
4 Subject: [PATCH] tools/env: add support for env in ubi volume chardev
5
6 Signed-off-by: Daniel Golle <daniel@makrotopia.org>
7 ---
8  tools/env/Makefile |  5 ++++
9  tools/env/fw_env.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++-------
10  2 files changed, 71 insertions(+), 10 deletions(-)
11
12 --- a/tools/env/Makefile
13 +++ b/tools/env/Makefile
14 @@ -24,6 +24,11 @@ ifeq ($(MTD_VERSION),old)
15  HOST_EXTRACFLAGS += -DMTD_OLD
16  endif
17  
18 +ifeq ($(UBI),y)
19 +HOST_EXTRACFLAGS += -DUBI
20 +HOST_LOADLIBES = "-Wl,--gc-sections,-lubi-utils"
21 +endif
22 +
23  always := fw_printenv
24  hostprogs-y := fw_printenv
25  
26 --- a/tools/env/fw_env.c
27 +++ b/tools/env/fw_env.c
28 @@ -30,6 +30,9 @@
29  # include <mtd/mtd-user.h>
30  #endif
31  
32 +#ifdef UBI
33 +# include <libubi.h>
34 +#endif
35  #include "fw_env.h"
36  
37  #include <aes.h>
38 @@ -810,6 +813,11 @@ static int flash_write_buf (int dev, int
39         off_t top_of_range;     /* end of the last block we may use */
40         loff_t blockstart;      /* running start of the current block -
41                                    MEMGETBADBLOCK needs 64 bits */
42 +#ifdef UBI
43 +       libubi_t *libubi = NULL;/* pointer to libubi struct */
44 +#else
45 +       void *libubi = NULL;
46 +#endif
47         int rc;
48  
49         /*
50 @@ -915,7 +923,30 @@ static int flash_write_buf (int dev, int
51                         continue;
52                 }
53  
54 -               if (mtd_type != MTD_ABSENT) {
55 +#ifdef UBI
56 +               if (mtd_type == MTD_UBIVOLUME) {
57 +                       struct ubi_vol_info volinfo;
58 +                       libubi = libubi_open();
59 +                       if (libubi)
60 +                               rc = ubi_get_vol_info(libubi,
61 +                                       DEVNAME(dev_current), &volinfo);
62 +                       if (libubi && !rc) {
63 +                               erasesize = volinfo.leb_size;
64 +                               int leb = blockstart / erasesize;
65 +                               if (volinfo.type != UBI_STATIC_VOLUME)
66 +                                       rc = ubi_leb_change_start(libubi, fd,
67 +                                               leb, erasesize);
68 +                               else
69 +                                       rc = ubi_update_start(libubi, fd,
70 +                                               erasesize);
71 +                       }
72 +                       if (libubi && rc) {
73 +                               libubi_close(libubi);
74 +                               libubi = NULL;
75 +                       }
76 +               }
77 +#endif
78 +               if (!libubi && mtd_type != MTD_ABSENT) {
79                         erase.start = blockstart;
80                         ioctl(fd, MEMUNLOCK, &erase);
81                         /* These do not need an explicit erase cycle */
82 @@ -932,7 +963,8 @@ static int flash_write_buf (int dev, int
83                         fprintf (stderr,
84                                  "Seek error on %s: %s\n",
85                                  DEVNAME (dev), strerror (errno));
86 -                       return -1;
87 +                       processed = -1;
88 +                       goto out;
89                 }
90  
91  #ifdef DEBUG
92 @@ -942,10 +974,11 @@ static int flash_write_buf (int dev, int
93                 if (write (fd, data + processed, erasesize) != erasesize) {
94                         fprintf (stderr, "Write error on %s: %s\n",
95                                  DEVNAME (dev), strerror (errno));
96 -                       return -1;
97 +                       processed = -1;
98 +                       goto out;
99                 }
100  
101 -               if (mtd_type != MTD_ABSENT)
102 +               if (!libubi && mtd_type != MTD_ABSENT)
103                         ioctl(fd, MEMLOCK, &erase);
104  
105                 processed  += erasesize;
106 @@ -956,6 +989,11 @@ static int flash_write_buf (int dev, int
107         if (write_total > count)
108                 free (data);
109  
110 +out:
111 +#ifdef UBI
112 +       if (libubi)
113 +               libubi_close(libubi);
114 +#endif
115         return processed;
116  }
117  
118 @@ -1067,12 +1105,8 @@ static int flash_read (int fd)
119  
120         if (S_ISCHR(st.st_mode)) {
121                 rc = ioctl(fd, MEMGETINFO, &mtdinfo);
122 -               if (rc < 0) {
123 -                       fprintf(stderr, "Cannot get MTD information for %s\n",
124 -                               DEVNAME(dev_current));
125 -                       return -1;
126 -               }
127 -               if (mtdinfo.type != MTD_NORFLASH &&
128 +               if (!rc &&
129 +                   mtdinfo.type != MTD_NORFLASH &&
130                     mtdinfo.type != MTD_NANDFLASH &&
131                     mtdinfo.type != MTD_DATAFLASH &&
132                     mtdinfo.type != MTD_UBIVOLUME) {
133 @@ -1080,6 +1114,28 @@ static int flash_read (int fd)
134                                  mtdinfo.type, DEVNAME(dev_current));
135                         return -1;
136                 }
137 +#ifdef UBI
138 +               if (rc) {
139 +                       libubi_t *libubi;
140 +                       struct ubi_vol_info volinfo;
141 +                       libubi = libubi_open();
142 +                       if (!libubi)
143 +                               return -ENOMEM;
144 +
145 +                       rc = ubi_get_vol_info(libubi, DEVNAME(dev_current),
146 +                                               &volinfo);
147 +                       if (rc) {
148 +                               libubi_close(libubi);
149 +                               return -ENODEV;
150 +                       }
151 +                       memset(&mtdinfo, 0, sizeof(mtdinfo));
152 +                       mtdinfo.type = MTD_UBIVOLUME;
153 +                       mtdinfo.size = volinfo.data_bytes;
154 +                       mtdinfo.erasesize = volinfo.leb_size;
155 +                       mtdinfo.writesize = volinfo.leb_size;
156 +                       libubi_close(libubi);
157 +               }
158 +#endif
159         } else {
160                 memset(&mtdinfo, 0, sizeof(mtdinfo));
161                 mtdinfo.type = MTD_ABSENT;