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