aboutsummaryrefslogtreecommitdiff
path: root/miscutils/ubi_tools.c
diff options
context:
space:
mode:
Diffstat (limited to 'miscutils/ubi_tools.c')
-rw-r--r--miscutils/ubi_tools.c91
1 files changed, 57 insertions, 34 deletions
diff --git a/miscutils/ubi_tools.c b/miscutils/ubi_tools.c
index c6ba22adf..d142d1144 100644
--- a/miscutils/ubi_tools.c
+++ b/miscutils/ubi_tools.c
@@ -52,6 +52,7 @@
52//applet:IF_UBIRMVOL( APPLET_ODDNAME(ubirmvol, ubi_tools, BB_DIR_USR_SBIN, BB_SUID_DROP, ubirmvol)) 52//applet:IF_UBIRMVOL( APPLET_ODDNAME(ubirmvol, ubi_tools, BB_DIR_USR_SBIN, BB_SUID_DROP, ubirmvol))
53//applet:IF_UBIRSVOL( APPLET_ODDNAME(ubirsvol, ubi_tools, BB_DIR_USR_SBIN, BB_SUID_DROP, ubirsvol)) 53//applet:IF_UBIRSVOL( APPLET_ODDNAME(ubirsvol, ubi_tools, BB_DIR_USR_SBIN, BB_SUID_DROP, ubirsvol))
54//applet:IF_UBIUPDATEVOL(APPLET_ODDNAME(ubiupdatevol, ubi_tools, BB_DIR_USR_SBIN, BB_SUID_DROP, ubiupdatevol)) 54//applet:IF_UBIUPDATEVOL(APPLET_ODDNAME(ubiupdatevol, ubi_tools, BB_DIR_USR_SBIN, BB_SUID_DROP, ubiupdatevol))
55/* not NOEXEC: if flash operation stalls, use less memory in "hung" process */
55 56
56//kbuild:lib-$(CONFIG_UBIATTACH) += ubi_tools.o 57//kbuild:lib-$(CONFIG_UBIATTACH) += ubi_tools.o
57//kbuild:lib-$(CONFIG_UBIDETACH) += ubi_tools.o 58//kbuild:lib-$(CONFIG_UBIDETACH) += ubi_tools.o
@@ -67,23 +68,32 @@
67#endif 68#endif
68#include <mtd/ubi-user.h> 69#include <mtd/ubi-user.h>
69 70
70#define do_attach (ENABLE_UBIATTACH && applet_name[3] == 'a') 71#define UBI_APPLET_CNT (0 \
71#define do_detach (ENABLE_UBIDETACH && applet_name[3] == 'd') 72 + ENABLE_UBIATTACH \
72#define do_mkvol (ENABLE_UBIMKVOL && applet_name[3] == 'm') 73 + ENABLE_UBIDETACH \
73#define do_rmvol (ENABLE_UBIRMVOL && applet_name[4] == 'm') 74 + ENABLE_UBIMKVOL \
74#define do_rsvol (ENABLE_UBIRSVOL && applet_name[4] == 's') 75 + ENABLE_UBIRMVOL \
75#define do_update (ENABLE_UBIUPDATEVOL && applet_name[3] == 'u') 76 + ENABLE_UBIRSVOL \
76 77 + ENABLE_UBIUPDATEVOL \
77static unsigned get_num_from_file(const char *path, unsigned max, const char *errmsg) 78 )
79
80#define do_attach (ENABLE_UBIATTACH && (UBI_APPLET_CNT == 1 || applet_name[4] == 't'))
81#define do_detach (ENABLE_UBIDETACH && (UBI_APPLET_CNT == 1 || applet_name[4] == 'e'))
82#define do_mkvol (ENABLE_UBIMKVOL && (UBI_APPLET_CNT == 1 || applet_name[4] == 'k'))
83#define do_rmvol (ENABLE_UBIRMVOL && (UBI_APPLET_CNT == 1 || applet_name[4] == 'm'))
84#define do_rsvol (ENABLE_UBIRSVOL && (UBI_APPLET_CNT == 1 || applet_name[4] == 's'))
85#define do_update (ENABLE_UBIUPDATEVOL && (UBI_APPLET_CNT == 1 || applet_name[4] == 'p'))
86
87static unsigned get_num_from_file(const char *path, unsigned max)
78{ 88{
79 char buf[sizeof(long long)*3]; 89 char buf[sizeof(long long)*3];
80 unsigned long long num; 90 unsigned long long num;
81 91
82 if (open_read_close(path, buf, sizeof(buf)) < 0) 92 if (open_read_close(path, buf, sizeof(buf)) < 0)
83 bb_perror_msg_and_die(errmsg, path); 93 bb_perror_msg_and_die("can't open '%s'", path);
84 /* It can be \n terminated, xatoull won't work well */ 94 /* It can be \n terminated, xatoull won't work well */
85 if (sscanf(buf, "%llu", &num) != 1 || num > max) 95 if (sscanf(buf, "%llu", &num) != 1 || num > max)
86 bb_error_msg_and_die(errmsg, path); 96 bb_error_msg_and_die("number in '%s' is malformed or too large", path);
87 return num; 97 return num;
88} 98}
89 99
@@ -135,20 +145,17 @@ int ubi_tools_main(int argc UNUSED_PARAM, char **argv)
135#define OPTION_a (1 << 5) 145#define OPTION_a (1 << 5)
136#define OPTION_t (1 << 6) 146#define OPTION_t (1 << 6)
137 if (do_mkvol) { 147 if (do_mkvol) {
138 opt_complementary = "-1"; 148 opts = getopt32(argv, "^" "md:+n:+N:s:a:+t:O:+" "\0" "-1",
139 opts = getopt32(argv, "md:+n:+N:s:a:+t:O:+",
140 &dev_num, &vol_id, 149 &dev_num, &vol_id,
141 &vol_name, &size_bytes_str, &alignment, &type, 150 &vol_name, &size_bytes_str, &alignment, &type,
142 &vid_hdr_offset 151 &vid_hdr_offset
143 ); 152 );
144 } else 153 } else
145 if (do_update) { 154 if (do_update) {
146 opt_complementary = "-1"; 155 opts = getopt32(argv, "^" "s:at" "\0" "-1", &size_bytes_str);
147 opts = getopt32(argv, "s:at", &size_bytes_str);
148 opts *= OPTION_s; 156 opts *= OPTION_s;
149 } else { 157 } else {
150 opt_complementary = "-1"; 158 opts = getopt32(argv, "^" "m:+d:+n:+N:s:a:+t:" "\0" "-1",
151 opts = getopt32(argv, "m:+d:+n:+N:s:a:+t:",
152 &mtd_num, &dev_num, &vol_id, 159 &mtd_num, &dev_num, &vol_id,
153 &vol_name, &size_bytes_str, &alignment, &type 160 &vol_name, &size_bytes_str, &alignment, &type
154 ); 161 );
@@ -217,10 +224,10 @@ int ubi_tools_main(int argc UNUSED_PARAM, char **argv)
217 p = path_sys_class_ubi_ubi + sprintf(path_sys_class_ubi_ubi, "%u/", num); 224 p = path_sys_class_ubi_ubi + sprintf(path_sys_class_ubi_ubi, "%u/", num);
218 225
219 strcpy(p, "avail_eraseblocks"); 226 strcpy(p, "avail_eraseblocks");
220 leb_avail = get_num_from_file(path, UINT_MAX, "Can't get available eraseblocks from '%s'"); 227 leb_avail = get_num_from_file(path, UINT_MAX);
221 228
222 strcpy(p, "eraseblock_size"); 229 strcpy(p, "eraseblock_size");
223 leb_size = get_num_from_file(path, MAX_SANE_ERASEBLOCK, "Can't get eraseblock size from '%s'"); 230 leb_size = get_num_from_file(path, MAX_SANE_ERASEBLOCK);
224 231
225 size_bytes = leb_avail * (unsigned long long)leb_size; 232 size_bytes = leb_avail * (unsigned long long)leb_size;
226 //if (size_bytes <= 0) 233 //if (size_bytes <= 0)
@@ -232,16 +239,19 @@ int ubi_tools_main(int argc UNUSED_PARAM, char **argv)
232 if (!(opts & OPTION_N)) 239 if (!(opts & OPTION_N))
233 bb_error_msg_and_die("name not specified"); 240 bb_error_msg_and_die("name not specified");
234 241
242 /* the structure is memset(0) above */
235 mkvol_req.vol_id = vol_id; 243 mkvol_req.vol_id = vol_id;
236 mkvol_req.vol_type = UBI_DYNAMIC_VOLUME; 244 mkvol_req.vol_type = UBI_DYNAMIC_VOLUME;
237 if ((opts & OPTION_t) && type[0] == 's') 245 if ((opts & OPTION_t) && type[0] == 's')
238 mkvol_req.vol_type = UBI_STATIC_VOLUME; 246 mkvol_req.vol_type = UBI_STATIC_VOLUME;
239 mkvol_req.alignment = alignment; 247 mkvol_req.alignment = alignment;
240 mkvol_req.bytes = size_bytes; /* signed int64_t */ 248 mkvol_req.bytes = size_bytes; /* signed int64_t */
241 strncpy(mkvol_req.name, vol_name, UBI_MAX_VOLUME_NAME); 249 /* strnlen avoids overflow of 16-bit field (paranoia) */
242 mkvol_req.name_len = strlen(vol_name); 250 mkvol_req.name_len = strnlen(vol_name, UBI_MAX_VOLUME_NAME+1);
243 if (mkvol_req.name_len > UBI_MAX_VOLUME_NAME) 251 if (mkvol_req.name_len > UBI_MAX_VOLUME_NAME)
244 bb_error_msg_and_die("volume name too long: '%s'", vol_name); 252 bb_error_msg_and_die("volume name too long: '%s'", vol_name);
253 /* this is safe: .name[] is UBI_MAX_VOLUME_NAME+1 bytes */
254 strcpy(mkvol_req.name, vol_name);
245 255
246 xioctl(fd, UBI_IOCMKVOL, &mkvol_req); 256 xioctl(fd, UBI_IOCMKVOL, &mkvol_req);
247 } else 257 } else
@@ -289,7 +299,7 @@ int ubi_tools_main(int argc UNUSED_PARAM, char **argv)
289 } else 299 } else
290 300
291//usage:#define ubiupdatevol_trivial_usage 301//usage:#define ubiupdatevol_trivial_usage
292//usage: "[-t | [-s SIZE] IMG_FILE] UBI_DEVICE" 302//usage: "-t UBI_DEVICE | [-s SIZE] UBI_DEVICE IMG_FILE"
293//usage:#define ubiupdatevol_full_usage "\n\n" 303//usage:#define ubiupdatevol_full_usage "\n\n"
294//usage: "Update UBI volume\n" 304//usage: "Update UBI volume\n"
295//usage: "\n -t Truncate to zero size" 305//usage: "\n -t Truncate to zero size"
@@ -304,24 +314,25 @@ int ubi_tools_main(int argc UNUSED_PARAM, char **argv)
304 xioctl(fd, UBI_IOCVOLUP, &bytes64); 314 xioctl(fd, UBI_IOCVOLUP, &bytes64);
305 } 315 }
306 else { 316 else {
307 struct stat st;
308 unsigned ubinum, volnum; 317 unsigned ubinum, volnum;
309 unsigned leb_size; 318 unsigned leb_size;
310 ssize_t len; 319 char *buf;
311 char *input_data;
312 320
313 /* Assume that device is in normal format. */ 321 /* Assume that device is in normal format. */
314 /* Removes need for scanning sysfs tree as full libubi does. */ 322 /* Removes need for scanning sysfs tree as full libubi does. */
315 if (sscanf(ubi_ctrl, "/dev/ubi%u_%u", &ubinum, &volnum) != 2) 323 if (sscanf(ubi_ctrl, "/dev/ubi%u_%u", &ubinum, &volnum) != 2)
316 bb_error_msg_and_die("wrong format of UBI device name"); 324 bb_error_msg_and_die("UBI device name '%s' is not /dev/ubiN_M", ubi_ctrl);
317 325
318 sprintf(path_sys_class_ubi_ubi, "%u_%u/usable_eb_size", ubinum, volnum); 326 sprintf(path_sys_class_ubi_ubi, "%u_%u/usable_eb_size", ubinum, volnum);
319 leb_size = get_num_from_file(path, MAX_SANE_ERASEBLOCK, "Can't get usable eraseblock size from '%s'"); 327 leb_size = get_num_from_file(path, MAX_SANE_ERASEBLOCK);
320 328
321 if (!(opts & OPTION_s)) { 329 if (!*argv)
322 if (!*argv) 330 bb_show_usage();
323 bb_show_usage(); 331 if (NOT_LONE_DASH(*argv)) /* mtd-utils supports "-" as stdin */
324 xmove_fd(xopen(*argv, O_RDONLY), STDIN_FILENO); 332 xmove_fd(xopen(*argv, O_RDONLY), STDIN_FILENO);
333
334 if (!(opts & OPTION_s)) {
335 struct stat st;
325 xfstat(STDIN_FILENO, &st, *argv); 336 xfstat(STDIN_FILENO, &st, *argv);
326 size_bytes = st.st_size; 337 size_bytes = st.st_size;
327 } 338 }
@@ -330,12 +341,24 @@ int ubi_tools_main(int argc UNUSED_PARAM, char **argv)
330 /* this ioctl expects signed int64_t* parameter */ 341 /* this ioctl expects signed int64_t* parameter */
331 xioctl(fd, UBI_IOCVOLUP, &bytes64); 342 xioctl(fd, UBI_IOCVOLUP, &bytes64);
332 343
333 input_data = xmalloc(leb_size); 344 /* can't use bb_copyfd_exact_size(): copy in blocks of exactly leb_size */
334 while ((len = full_read(STDIN_FILENO, input_data, leb_size)) > 0) { 345 buf = xmalloc(leb_size);
335 xwrite(fd, input_data, len); 346 while (size_bytes != 0) {
347 int len = full_read(STDIN_FILENO, buf, leb_size);
348 if (len <= 0) {
349 if (len < 0)
350 bb_perror_msg_and_die("read error from '%s'", *argv);
351 break;
352 }
353 if ((unsigned)len > size_bytes) {
354 /* for this case: "ubiupdatevol -s 1024000 $UBIDEV /dev/urandom" */
355 len = size_bytes;
356 }
357 xwrite(fd, buf, len);
358 size_bytes -= len;
336 } 359 }
337 if (len < 0) 360 if (ENABLE_FEATURE_CLEAN_UP)
338 bb_perror_msg_and_die("UBI volume update failed"); 361 free(buf);
339 } 362 }
340 } 363 }
341 364