uboot-envtools: add support for environment in ubi volume
[openwrt/openwrt.git] / package / boot / uboot-envtools / patches / 300-support-env-in-ubivol-chardev.patch
1 From ee2c9e3aef32c05b1d7891858baff19bffed1652 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 | 74 ++++++++++++++++++++++++++++++++++++++++++++++--------
10 2 files changed, 69 insertions(+), 10 deletions(-)
11
12 diff --git a/tools/env/Makefile b/tools/env/Makefile
13 index f5368bc..d006a93 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..5c0acd5 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,9 @@ 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 + if (libubi)
116 + libubi_close(libubi);
117 return processed;
118 }
119
120 @@ -1066,12 +1102,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 @@ -1079,6 +1111,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;
164 --
165 1.9.2
166