summaryrefslogtreecommitdiff
path: root/package/boot/uboot-envtools/patches/300-support-env-in-ubivol-chardev.patch
blob: 41089c50e7b45e16adca05c063fae71617d1371e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
From 6e2630a0fc872d0db34157972f6dc3941f6d66dd Mon Sep 17 00:00:00 2001
From: Daniel Golle <daniel@makrotopia.org>
Date: Mon, 19 May 2014 21:38:01 +0200
Subject: [PATCH] tools/env: add support for env in ubi volume chardev

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
---
 tools/env/Makefile |  5 ++++
 tools/env/fw_env.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++-------
 2 files changed, 71 insertions(+), 10 deletions(-)

diff --git a/tools/env/Makefile b/tools/env/Makefile
index f5368bc..526d44d 100644
--- a/tools/env/Makefile
+++ b/tools/env/Makefile
@@ -20,6 +20,11 @@ ifeq ($(MTD_VERSION),old)
 HOST_EXTRACFLAGS += -DMTD_OLD
 endif
 
+ifeq ($(UBI),y)
+HOST_EXTRACFLAGS += -DUBI
+HOST_LOADLIBES = "-Wl,--gc-sections,-lubi-utils"
+endif
+
 always := fw_printenv
 hostprogs-y := fw_printenv_unstripped
 
diff --git a/tools/env/fw_env.c b/tools/env/fw_env.c
index 30d5b03..2217a25 100644
--- a/tools/env/fw_env.c
+++ b/tools/env/fw_env.c
@@ -29,6 +29,9 @@
 # include <mtd/mtd-user.h>
 #endif
 
+#ifdef UBI
+# include <libubi.h>
+#endif
 #include "fw_env.h"
 
 #include <aes.h>
@@ -809,6 +812,11 @@ static int flash_write_buf (int dev, int fd, void *buf, size_t count,
 	off_t top_of_range;	/* end of the last block we may use */
 	loff_t blockstart;	/* running start of the current block -
 				   MEMGETBADBLOCK needs 64 bits */
+#ifdef UBI
+	libubi_t *libubi = NULL;/* pointer to libubi struct */
+#else
+	void *libubi = NULL;
+#endif
 	int rc;
 
 	/*
@@ -914,7 +922,30 @@ static int flash_write_buf (int dev, int fd, void *buf, size_t count,
 			continue;
 		}
 
-		if (mtd_type != MTD_ABSENT) {
+#ifdef UBI
+		if (mtd_type == MTD_UBIVOLUME) {
+			struct ubi_vol_info volinfo;
+			libubi = libubi_open();
+			if (libubi)
+				rc = ubi_get_vol_info(libubi,
+					DEVNAME(dev_current), &volinfo);
+			if (libubi && !rc) {
+				erasesize = volinfo.leb_size;
+				int leb = blockstart / erasesize;
+				if (volinfo.type != UBI_STATIC_VOLUME)
+					rc = ubi_leb_change_start(libubi, fd,
+						leb, erasesize);
+				else
+					rc = ubi_update_start(libubi, fd,
+						erasesize);
+			}
+			if (libubi && rc) {
+				libubi_close(libubi);
+				libubi = NULL;
+			}
+		}
+#endif
+		if (!libubi && mtd_type != MTD_ABSENT) {
 			erase.start = blockstart;
 			ioctl(fd, MEMUNLOCK, &erase);
 			/* These do not need an explicit erase cycle */
@@ -931,7 +962,8 @@ static int flash_write_buf (int dev, int fd, void *buf, size_t count,
 			fprintf (stderr,
 				 "Seek error on %s: %s\n",
 				 DEVNAME (dev), strerror (errno));
-			return -1;
+			processed = -1;
+			goto out;
 		}
 
 #ifdef DEBUG
@@ -941,10 +973,11 @@ static int flash_write_buf (int dev, int fd, void *buf, size_t count,
 		if (write (fd, data + processed, erasesize) != erasesize) {
 			fprintf (stderr, "Write error on %s: %s\n",
 				 DEVNAME (dev), strerror (errno));
-			return -1;
+			processed = -1;
+			goto out;
 		}
 
-		if (mtd_type != MTD_ABSENT)
+		if (!libubi && mtd_type != MTD_ABSENT)
 			ioctl(fd, MEMLOCK, &erase);
 
 		processed  += erasesize;
@@ -955,6 +988,11 @@ static int flash_write_buf (int dev, int fd, void *buf, size_t count,
 	if (write_total > count)
 		free (data);
 
+out:
+#ifdef UBI
+	if (libubi)
+		libubi_close(libubi);
+#endif
 	return processed;
 }
 
@@ -1066,12 +1104,8 @@ static int flash_read (int fd)
 
 	if (S_ISCHR(st.st_mode)) {
 		rc = ioctl(fd, MEMGETINFO, &mtdinfo);
-		if (rc < 0) {
-			fprintf(stderr, "Cannot get MTD information for %s\n",
-				DEVNAME(dev_current));
-			return -1;
-		}
-		if (mtdinfo.type != MTD_NORFLASH &&
+		if (!rc &&
+		    mtdinfo.type != MTD_NORFLASH &&
 		    mtdinfo.type != MTD_NANDFLASH &&
 		    mtdinfo.type != MTD_DATAFLASH &&
 		    mtdinfo.type != MTD_UBIVOLUME) {
@@ -1079,6 +1113,28 @@ static int flash_read (int fd)
 				 mtdinfo.type, DEVNAME(dev_current));
 			return -1;
 		}
+#ifdef UBI
+		if (rc) {
+			libubi_t *libubi;
+			struct ubi_vol_info volinfo;
+			libubi = libubi_open();
+			if (!libubi)
+				return -ENOMEM;
+
+			rc = ubi_get_vol_info(libubi, DEVNAME(dev_current),
+						&volinfo);
+			if (rc) {
+				libubi_close(libubi);
+				return -ENODEV;
+			}
+			memset(&mtdinfo, 0, sizeof(mtdinfo));
+			mtdinfo.type = MTD_UBIVOLUME;
+			mtdinfo.size = volinfo.data_bytes;
+			mtdinfo.erasesize = volinfo.leb_size;
+			mtdinfo.writesize = volinfo.leb_size;
+			libubi_close(libubi);
+		}
+#endif
 	} else {
 		memset(&mtdinfo, 0, sizeof(mtdinfo));
 		mtdinfo.type = MTD_ABSENT;
-- 
2.0.0