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#include "libbb.h"
59
60#ifndef __packed
61# define __packed __attribute__((packed))
62#endif
63#include <mtd/ubi-user.h>
64
65#define UBI_APPLET_CNT (0 \
66 + ENABLE_UBIATTACH \
67 + ENABLE_UBIDETACH \
68 + ENABLE_UBIMKVOL \
69 + ENABLE_UBIRMVOL \
70 + ENABLE_UBIRSVOL \
71 + ENABLE_UBIUPDATEVOL \
72 )
73
74#define do_attach (ENABLE_UBIATTACH && (UBI_APPLET_CNT == 1 || applet_name[4] == 't'))
75#define do_detach (ENABLE_UBIDETACH && (UBI_APPLET_CNT == 1 || applet_name[4] == 'e'))
76#define do_mkvol (ENABLE_UBIMKVOL && (UBI_APPLET_CNT == 1 || applet_name[4] == 'k'))
77#define do_rmvol (ENABLE_UBIRMVOL && (UBI_APPLET_CNT == 1 || applet_name[4] == 'm'))
78#define do_rsvol (ENABLE_UBIRSVOL && (UBI_APPLET_CNT == 1 || applet_name[4] == 's'))
79#define do_update (ENABLE_UBIUPDATEVOL && (UBI_APPLET_CNT == 1 || applet_name[4] == 'p'))
80
81static unsigned get_num_from_file(const char *path, unsigned max)
82{
83 char buf[sizeof(long long)*3];
84 unsigned long long num;
85
86 if (open_read_close(path, buf, sizeof(buf)) < 0)
87 bb_perror_msg_and_die("can't open '%s'", path);
88
89 if (sscanf(buf, "%llu", &num) != 1 || num > max)
90 bb_error_msg_and_die("number in '%s' is malformed or too large", path);
91 return num;
92}
93
94
95#define MAX_SANE_ERASEBLOCK (16*1024*1024)
96
97int ubi_tools_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
98int ubi_tools_main(int argc UNUSED_PARAM, char **argv)
99{
100 static const struct suffix_mult size_suffixes[] ALIGN_SUFFIX = {
101 { "KiB", 1024 },
102 { "MiB", 1024*1024 },
103 { "GiB", 1024*1024*1024 },
104 { "", 0 }
105 };
106
107 unsigned opts;
108 char *ubi_ctrl;
109 int fd;
110 int mtd_num;
111 int dev_num = UBI_DEV_NUM_AUTO;
112 int vol_id = UBI_VOL_NUM_AUTO;
113 int vid_hdr_offset = 0;
114 char *vol_name;
115 unsigned long long size_bytes = size_bytes;
116 char *size_bytes_str;
117 int alignment = 1;
118 char *type;
119 union {
120 struct ubi_attach_req attach_req;
121 struct ubi_mkvol_req mkvol_req;
122 struct ubi_rsvol_req rsvol_req;
123 } req_structs;
124#define attach_req req_structs.attach_req
125#define mkvol_req req_structs.mkvol_req
126#define rsvol_req req_structs.rsvol_req
127 char path[sizeof("/sys/class/ubi/ubi%d_%d/usable_eb_size")
128 + 2 * sizeof(int)*3 + 16];
129#define path_sys_class_ubi_ubi (path + sizeof("/sys/class/ubi/ubi")-1)
130
131 strcpy(path, "/sys/class/ubi/ubi");
132 memset(&req_structs, 0, sizeof(req_structs));
133
134#define OPTION_m (1 << 0)
135#define OPTION_d (1 << 1)
136#define OPTION_n (1 << 2)
137#define OPTION_N (1 << 3)
138#define OPTION_s (1 << 4)
139#define OPTION_a (1 << 5)
140#define OPTION_t (1 << 6)
141 if (do_mkvol) {
142 opts = getopt32(argv, "^" "md:+n:+N:s:a:+t:O:+" "\0" "-1",
143 &dev_num, &vol_id,
144 &vol_name, &size_bytes_str, &alignment, &type,
145 &vid_hdr_offset
146 );
147 } else
148 if (do_update) {
149 opts = getopt32(argv, "^" "s:at" "\0" "-1", &size_bytes_str);
150 opts *= OPTION_s;
151 } else {
152 opts = getopt32(argv, "^" "m:+d:+n:+N:s:a:+t:" "\0" "-1",
153 &mtd_num, &dev_num, &vol_id,
154 &vol_name, &size_bytes_str, &alignment, &type
155 );
156 }
157
158 if (opts & OPTION_s)
159 size_bytes = xatoull_sfx(size_bytes_str, size_suffixes);
160 argv += optind;
161 ubi_ctrl = *argv++;
162
163 fd = xopen(ubi_ctrl, O_RDWR);
164
165
166
167
168
169
170
171
172
173
174
175 if (do_attach) {
176 if (!(opts & OPTION_m))
177 bb_error_msg_and_die("%s device not specified", "MTD");
178
179 attach_req.mtd_num = mtd_num;
180 attach_req.ubi_num = dev_num;
181 attach_req.vid_hdr_offset = vid_hdr_offset;
182
183 xioctl(fd, UBI_IOCATT, &attach_req);
184 } else
185
186
187
188
189
190
191 if (do_detach) {
192 if (!(opts & OPTION_d))
193 bb_error_msg_and_die("%s device not specified", "UBI");
194
195
196 xioctl(fd, UBI_IOCDET, &dev_num);
197 } else
198
199
200
201
202
203
204
205
206
207
208
209
210 if (do_mkvol) {
211 if (opts & OPTION_m) {
212 unsigned leb_avail;
213 unsigned leb_size;
214 unsigned num;
215 char *p;
216
217 num = ubi_devnum_from_devname(ubi_ctrl);
218 p = path_sys_class_ubi_ubi + sprintf(path_sys_class_ubi_ubi, "%u/", num);
219
220 strcpy(p, "avail_eraseblocks");
221 leb_avail = get_num_from_file(path, UINT_MAX);
222
223 strcpy(p, "eraseblock_size");
224 leb_size = get_num_from_file(path, MAX_SANE_ERASEBLOCK);
225
226 size_bytes = leb_avail * (unsigned long long)leb_size;
227
228
229 } else
230 if (!(opts & OPTION_s))
231 bb_simple_error_msg_and_die("size not specified");
232
233 if (!(opts & OPTION_N))
234 bb_simple_error_msg_and_die("name not specified");
235
236
237 mkvol_req.vol_id = vol_id;
238 mkvol_req.vol_type = UBI_DYNAMIC_VOLUME;
239 if ((opts & OPTION_t) && type[0] == 's')
240 mkvol_req.vol_type = UBI_STATIC_VOLUME;
241 mkvol_req.alignment = alignment;
242 mkvol_req.bytes = size_bytes;
243
244 mkvol_req.name_len = strnlen(vol_name, UBI_MAX_VOLUME_NAME+1);
245 if (mkvol_req.name_len > UBI_MAX_VOLUME_NAME)
246 bb_error_msg_and_die("volume name too long: '%s'", vol_name);
247
248 strcpy(mkvol_req.name, vol_name);
249
250 xioctl(fd, UBI_IOCMKVOL, &mkvol_req);
251 } else
252
253
254
255
256
257
258
259 if (do_rmvol) {
260 if (!(opts & (OPTION_n|OPTION_N)))
261 bb_simple_error_msg_and_die("volume id not specified");
262
263 if (opts & OPTION_N) {
264 unsigned num = ubi_devnum_from_devname(ubi_ctrl);
265 vol_id = ubi_get_volid_by_name(num, vol_name);
266 }
267
268 if (sizeof(vol_id) != 4) {
269
270 int32_t t = vol_id;
271 xioctl(fd, UBI_IOCRMVOL, &t);
272 } else {
273 xioctl(fd, UBI_IOCRMVOL, &vol_id);
274 }
275 } else
276
277
278
279
280
281
282
283 if (do_rsvol) {
284 if (!(opts & OPTION_s))
285 bb_simple_error_msg_and_die("size not specified");
286 if (!(opts & OPTION_n))
287 bb_simple_error_msg_and_die("volume id not specified");
288
289 rsvol_req.bytes = size_bytes;
290 rsvol_req.vol_id = vol_id;
291
292 xioctl(fd, UBI_IOCRSVOL, &rsvol_req);
293 } else
294
295
296
297
298
299
300
301 if (do_update) {
302 int64_t bytes64;
303
304 if (opts & OPTION_t) {
305
306 bytes64 = 0;
307
308 xioctl(fd, UBI_IOCVOLUP, &bytes64);
309 }
310 else {
311 unsigned ubinum, volnum;
312 unsigned leb_size;
313 char *buf;
314
315
316
317 if (sscanf(ubi_ctrl, "/dev/ubi%u_%u", &ubinum, &volnum) != 2)
318 bb_error_msg_and_die("UBI device name '%s' is not /dev/ubiN_M", ubi_ctrl);
319
320 sprintf(path_sys_class_ubi_ubi, "%u_%u/usable_eb_size", ubinum, volnum);
321 leb_size = get_num_from_file(path, MAX_SANE_ERASEBLOCK);
322
323 if (!*argv)
324 bb_show_usage();
325 if (NOT_LONE_DASH(*argv))
326 xmove_fd(xopen(*argv, O_RDONLY), STDIN_FILENO);
327
328 if (!(opts & OPTION_s)) {
329 struct stat st;
330 xfstat(STDIN_FILENO, &st, *argv);
331 size_bytes = st.st_size;
332 }
333
334 bytes64 = size_bytes;
335
336 xioctl(fd, UBI_IOCVOLUP, &bytes64);
337
338
339 buf = xmalloc(leb_size);
340 while (size_bytes != 0) {
341 int len = full_read(STDIN_FILENO, buf, leb_size);
342 if (len <= 0) {
343 if (len < 0)
344 bb_perror_msg_and_die("read error from '%s'", *argv);
345 break;
346 }
347 if ((unsigned)len > size_bytes) {
348
349 len = size_bytes;
350 }
351 xwrite(fd, buf, len);
352 size_bytes -= len;
353 }
354 if (ENABLE_FEATURE_CLEAN_UP)
355 free(buf);
356 }
357 }
358
359 if (ENABLE_FEATURE_CLEAN_UP)
360 close(fd);
361
362 return EXIT_SUCCESS;
363}
364