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.c63
1 files changed, 39 insertions, 24 deletions
diff --git a/miscutils/ubi_tools.c b/miscutils/ubi_tools.c
index 494718ccf..123551e94 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
@@ -83,16 +84,16 @@
83#define do_rsvol (ENABLE_UBIRSVOL && (UBI_APPLET_CNT == 1 || applet_name[4] == 's')) 84#define do_rsvol (ENABLE_UBIRSVOL && (UBI_APPLET_CNT == 1 || applet_name[4] == 's'))
84#define do_update (ENABLE_UBIUPDATEVOL && (UBI_APPLET_CNT == 1 || applet_name[4] == 'p')) 85#define do_update (ENABLE_UBIUPDATEVOL && (UBI_APPLET_CNT == 1 || applet_name[4] == 'p'))
85 86
86static unsigned get_num_from_file(const char *path, unsigned max, const char *errmsg) 87static unsigned get_num_from_file(const char *path, unsigned max)
87{ 88{
88 char buf[sizeof(long long)*3]; 89 char buf[sizeof(long long)*3];
89 unsigned long long num; 90 unsigned long long num;
90 91
91 if (open_read_close(path, buf, sizeof(buf)) < 0) 92 if (open_read_close(path, buf, sizeof(buf)) < 0)
92 bb_perror_msg_and_die(errmsg, path); 93 bb_perror_msg_and_die("can't open '%s'", path);
93 /* It can be \n terminated, xatoull won't work well */ 94 /* It can be \n terminated, xatoull won't work well */
94 if (sscanf(buf, "%llu", &num) != 1 || num > max) 95 if (sscanf(buf, "%llu", &num) != 1 || num > max)
95 bb_error_msg_and_die(errmsg, path); 96 bb_error_msg_and_die("number in '%s' is malformed or too large", path);
96 return num; 97 return num;
97} 98}
98 99
@@ -226,10 +227,10 @@ int ubi_tools_main(int argc UNUSED_PARAM, char **argv)
226 p = path_sys_class_ubi_ubi + sprintf(path_sys_class_ubi_ubi, "%u/", num); 227 p = path_sys_class_ubi_ubi + sprintf(path_sys_class_ubi_ubi, "%u/", num);
227 228
228 strcpy(p, "avail_eraseblocks"); 229 strcpy(p, "avail_eraseblocks");
229 leb_avail = get_num_from_file(path, UINT_MAX, "Can't get available eraseblocks from '%s'"); 230 leb_avail = get_num_from_file(path, UINT_MAX);
230 231
231 strcpy(p, "eraseblock_size"); 232 strcpy(p, "eraseblock_size");
232 leb_size = get_num_from_file(path, MAX_SANE_ERASEBLOCK, "Can't get eraseblock size from '%s'"); 233 leb_size = get_num_from_file(path, MAX_SANE_ERASEBLOCK);
233 234
234 size_bytes = leb_avail * (unsigned long long)leb_size; 235 size_bytes = leb_avail * (unsigned long long)leb_size;
235 //if (size_bytes <= 0) 236 //if (size_bytes <= 0)
@@ -241,16 +242,19 @@ int ubi_tools_main(int argc UNUSED_PARAM, char **argv)
241 if (!(opts & OPTION_N)) 242 if (!(opts & OPTION_N))
242 bb_error_msg_and_die("name not specified"); 243 bb_error_msg_and_die("name not specified");
243 244
245 /* the structure is memset(0) above */
244 mkvol_req.vol_id = vol_id; 246 mkvol_req.vol_id = vol_id;
245 mkvol_req.vol_type = UBI_DYNAMIC_VOLUME; 247 mkvol_req.vol_type = UBI_DYNAMIC_VOLUME;
246 if ((opts & OPTION_t) && type[0] == 's') 248 if ((opts & OPTION_t) && type[0] == 's')
247 mkvol_req.vol_type = UBI_STATIC_VOLUME; 249 mkvol_req.vol_type = UBI_STATIC_VOLUME;
248 mkvol_req.alignment = alignment; 250 mkvol_req.alignment = alignment;
249 mkvol_req.bytes = size_bytes; /* signed int64_t */ 251 mkvol_req.bytes = size_bytes; /* signed int64_t */
250 strncpy(mkvol_req.name, vol_name, UBI_MAX_VOLUME_NAME); 252 /* strnlen avoids overflow of 16-bit field (paranoia) */
251 mkvol_req.name_len = strlen(vol_name); 253 mkvol_req.name_len = strnlen(vol_name, UBI_MAX_VOLUME_NAME+1);
252 if (mkvol_req.name_len > UBI_MAX_VOLUME_NAME) 254 if (mkvol_req.name_len > UBI_MAX_VOLUME_NAME)
253 bb_error_msg_and_die("volume name too long: '%s'", vol_name); 255 bb_error_msg_and_die("volume name too long: '%s'", vol_name);
256 /* this is safe: .name[] is UBI_MAX_VOLUME_NAME+1 bytes */
257 strcpy(mkvol_req.name, vol_name);
254 258
255 xioctl(fd, UBI_IOCMKVOL, &mkvol_req); 259 xioctl(fd, UBI_IOCMKVOL, &mkvol_req);
256 } else 260 } else
@@ -315,38 +319,49 @@ int ubi_tools_main(int argc UNUSED_PARAM, char **argv)
315 else { 319 else {
316 unsigned ubinum, volnum; 320 unsigned ubinum, volnum;
317 unsigned leb_size; 321 unsigned leb_size;
318 ssize_t len; 322 char *buf;
319 char *input_data;
320 323
321 /* Assume that device is in normal format. */ 324 /* Assume that device is in normal format. */
322 /* Removes need for scanning sysfs tree as full libubi does. */ 325 /* Removes need for scanning sysfs tree as full libubi does. */
323 if (sscanf(ubi_ctrl, "/dev/ubi%u_%u", &ubinum, &volnum) != 2) 326 if (sscanf(ubi_ctrl, "/dev/ubi%u_%u", &ubinum, &volnum) != 2)
324 bb_error_msg_and_die("wrong format of UBI device name"); 327 bb_error_msg_and_die("UBI device name '%s' is not /dev/ubiN_M", ubi_ctrl);
325 328
326 sprintf(path_sys_class_ubi_ubi, "%u_%u/usable_eb_size", ubinum, volnum); 329 sprintf(path_sys_class_ubi_ubi, "%u_%u/usable_eb_size", ubinum, volnum);
327 leb_size = get_num_from_file(path, MAX_SANE_ERASEBLOCK, "Can't get usable eraseblock size from '%s'"); 330 leb_size = get_num_from_file(path, MAX_SANE_ERASEBLOCK);
328 331
329 if (!(opts & OPTION_t)) { 332 if (!*argv)
330 if (!*argv) 333 bb_show_usage();
331 bb_show_usage(); 334 if (NOT_LONE_DASH(*argv)) /* mtd-utils supports "-" as stdin */
332 xmove_fd(xopen(*argv, O_RDONLY), STDIN_FILENO); 335 xmove_fd(xopen(*argv, O_RDONLY), STDIN_FILENO);
333 if (!(opts & OPTION_s)) { 336
334 struct stat st; 337 if (!(opts & OPTION_s)) {
335 xfstat(STDIN_FILENO, &st, *argv); 338 struct stat st;
336 size_bytes = st.st_size; 339 xfstat(STDIN_FILENO, &st, *argv);
337 } 340 size_bytes = st.st_size;
338 } 341 }
339 342
340 bytes64 = size_bytes; 343 bytes64 = size_bytes;
341 /* this ioctl expects signed int64_t* parameter */ 344 /* this ioctl expects signed int64_t* parameter */
342 xioctl(fd, UBI_IOCVOLUP, &bytes64); 345 xioctl(fd, UBI_IOCVOLUP, &bytes64);
343 346
344 input_data = xmalloc(leb_size); 347 /* can't use bb_copyfd_exact_size(): copy in blocks of exactly leb_size */
345 while ((len = full_read(STDIN_FILENO, input_data, leb_size)) > 0) { 348 buf = xmalloc(leb_size);
346 xwrite(fd, input_data, len); 349 while (size_bytes != 0) {
350 int len = full_read(STDIN_FILENO, buf, leb_size);
351 if (len <= 0) {
352 if (len < 0)
353 bb_perror_msg_and_die("read error from '%s'", *argv);
354 break;
355 }
356 if ((unsigned)len > size_bytes) {
357 /* for this case: "ubiupdatevol -s 1024000 $UBIDEV /dev/urandom" */
358 len = size_bytes;
359 }
360 xwrite(fd, buf, len);
361 size_bytes -= len;
347 } 362 }
348 if (len < 0) 363 if (ENABLE_FEATURE_CLEAN_UP)
349 bb_perror_msg_and_die("UBI volume update failed"); 364 free(buf);
350 } 365 }
351 } 366 }
352 367