uboot-envtools: fix build if ubi volume support is disabled
[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 diff --git a/tools/env/Makefile b/tools/env/Makefile
13 index f5368bc..526d44d 100644
14 --- a/tools/env/Makefile
15 +++ b/tools/env/Makefile
16 @@ -20,6 +20,11 @@ ifeq ($(MTD_VERSION),old)
17  HOST_EXTRACFLAGS += -DMTD_OLD
18  endif
19  
20 +ifeq ($(UBI),y)
21 +HOST_EXTRACFLAGS += -DUBI
22 +HOST_LOADLIBES = "-Wl,--gc-sections,-lubi-utils"
23 +endif
24 +
25  always := fw_printenv
26  hostprogs-y := fw_printenv_unstripped
27  
28 diff --git a/tools/env/fw_env.c b/tools/env/fw_env.c
29 index 30d5b03..2217a25 100644
30 --- a/tools/env/fw_env.c
31 +++ b/tools/env/fw_env.c
32 @@ -29,6 +29,9 @@
33  # include <mtd/mtd-user.h>
34  #endif
35  
36 +#ifdef UBI
37 +# include <libubi.h>
38 +#endif
39  #include "fw_env.h"
40  
41  #include <aes.h>
42 @@ -809,6 +812,11 @@ static int flash_write_buf (int dev, int fd, void *buf, size_t count,
43         off_t top_of_range;     /* end of the last block we may use */
44         loff_t blockstart;      /* running start of the current block -
45                                    MEMGETBADBLOCK needs 64 bits */
46 +#ifdef UBI
47 +       libubi_t *libubi = NULL;/* pointer to libubi struct */
48 +#else
49 +       void *libubi = NULL;
50 +#endif
51         int rc;
52  
53         /*
54 @@ -914,7 +922,30 @@ static int flash_write_buf (int dev, int fd, void *buf, size_t count,
55                         continue;
56                 }
57  
58 -               if (mtd_type != MTD_ABSENT) {
59 +#ifdef UBI
60 +               if (mtd_type == MTD_UBIVOLUME) {
61 +                       struct ubi_vol_info volinfo;
62 +                       libubi = libubi_open();
63 +                       if (libubi)
64 +                               rc = ubi_get_vol_info(libubi,
65 +                                       DEVNAME(dev_current), &volinfo);
66 +                       if (libubi && !rc) {
67 +                               erasesize = volinfo.leb_size;
68 +                               int leb = blockstart / erasesize;
69 +                               if (volinfo.type != UBI_STATIC_VOLUME)
70 +                                       rc = ubi_leb_change_start(libubi, fd,
71 +                                               leb, erasesize);
72 +                               else
73 +                                       rc = ubi_update_start(libubi, fd,
74 +                                               erasesize);
75 +                       }
76 +                       if (libubi && rc) {
77 +                               libubi_close(libubi);
78 +                               libubi = NULL;
79 +                       }
80 +               }
81 +#endif
82 +               if (!libubi && mtd_type != MTD_ABSENT) {
83                         erase.start = blockstart;
84                         ioctl(fd, MEMUNLOCK, &erase);
85                         /* These do not need an explicit erase cycle */
86 @@ -931,7 +962,8 @@ static int flash_write_buf (int dev, int fd, void *buf, size_t count,
87                         fprintf (stderr,
88                                  "Seek error on %s: %s\n",
89                                  DEVNAME (dev), strerror (errno));
90 -                       return -1;
91 +                       processed = -1;
92 +                       goto out;
93                 }
94  
95  #ifdef DEBUG
96 @@ -941,10 +973,11 @@ static int flash_write_buf (int dev, int fd, void *buf, size_t count,
97                 if (write (fd, data + processed, erasesize) != erasesize) {
98                         fprintf (stderr, "Write error on %s: %s\n",
99                                  DEVNAME (dev), strerror (errno));
100 -                       return -1;
101 +                       processed = -1;
102 +                       goto out;
103                 }
104  
105 -               if (mtd_type != MTD_ABSENT)
106 +               if (!libubi && mtd_type != MTD_ABSENT)
107                         ioctl(fd, MEMLOCK, &erase);
108  
109                 processed  += erasesize;
110 @@ -955,6 +988,11 @@ static int flash_write_buf (int dev, int fd, void *buf, size_t count,
111         if (write_total > count)
112                 free (data);
113  
114 +out:
115 +#ifdef UBI
116 +       if (libubi)
117 +               libubi_close(libubi);
118 +#endif
119         return processed;
120  }
121  
122 @@ -1066,12 +1104,8 @@ static int flash_read (int fd)
123  
124         if (S_ISCHR(st.st_mode)) {
125                 rc = ioctl(fd, MEMGETINFO, &mtdinfo);
126 -               if (rc < 0) {
127 -                       fprintf(stderr, "Cannot get MTD information for %s\n",
128 -                               DEVNAME(dev_current));
129 -                       return -1;
130 -               }
131 -               if (mtdinfo.type != MTD_NORFLASH &&
132 +               if (!rc &&
133 +                   mtdinfo.type != MTD_NORFLASH &&
134                     mtdinfo.type != MTD_NANDFLASH &&
135                     mtdinfo.type != MTD_DATAFLASH &&
136                     mtdinfo.type != MTD_UBIVOLUME) {
137 @@ -1079,6 +1113,28 @@ static int flash_read (int fd)
138                                  mtdinfo.type, DEVNAME(dev_current));
139                         return -1;
140                 }
141 +#ifdef UBI
142 +               if (rc) {
143 +                       libubi_t *libubi;
144 +                       struct ubi_vol_info volinfo;
145 +                       libubi = libubi_open();
146 +                       if (!libubi)
147 +                               return -ENOMEM;
148 +
149 +                       rc = ubi_get_vol_info(libubi, DEVNAME(dev_current),
150 +                                               &volinfo);
151 +                       if (rc) {
152 +                               libubi_close(libubi);
153 +                               return -ENODEV;
154 +                       }
155 +                       memset(&mtdinfo, 0, sizeof(mtdinfo));
156 +                       mtdinfo.type = MTD_UBIVOLUME;
157 +                       mtdinfo.size = volinfo.data_bytes;
158 +                       mtdinfo.erasesize = volinfo.leb_size;
159 +                       mtdinfo.writesize = volinfo.leb_size;
160 +                       libubi_close(libubi);
161 +               }
162 +#endif
163         } else {
164                 memset(&mtdinfo, 0, sizeof(mtdinfo));
165                 mtdinfo.type = MTD_ABSENT;
166 -- 
167 2.0.0
168