blob: 6d49f61d942a4445a25fcea69a4f94fc5606f4b6 [file] [log] [blame]
Denys Vlasenko2ab94032017-10-05 15:33:28 +02001/*
2 * Ported to busybox from mtd-utils.
Baruch Siach3324c962010-06-16 12:22:58 +02003 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02004 * Licensed under GPLv2, see file LICENSE in this source tree.
Baruch Siach3324c962010-06-16 12:22:58 +02005 */
Baruch Siach3324c962010-06-16 12:22:58 +02006//config:config UBIATTACH
Denys Vlasenkob097a842018-12-28 03:20:17 +01007//config: bool "ubiattach (4.2 kb)"
Reuben Dowle3e2834f2011-02-05 03:18:08 +01008//config: default y
Baruch Siach3324c962010-06-16 12:22:58 +02009//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020010//config: Attach MTD device to an UBI device.
Baruch Siach3324c962010-06-16 12:22:58 +020011//config:
12//config:config UBIDETACH
Denys Vlasenkob097a842018-12-28 03:20:17 +010013//config: bool "ubidetach (4.1 kb)"
Reuben Dowle3e2834f2011-02-05 03:18:08 +010014//config: default y
Baruch Siach3324c962010-06-16 12:22:58 +020015//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020016//config: Detach MTD device from an UBI device.
Reuben Dowle3e2834f2011-02-05 03:18:08 +010017//config:
18//config:config UBIMKVOL
Denys Vlasenkob097a842018-12-28 03:20:17 +010019//config: bool "ubimkvol (5.3 kb)"
Reuben Dowle3e2834f2011-02-05 03:18:08 +010020//config: default y
Reuben Dowle3e2834f2011-02-05 03:18:08 +010021//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020022//config: Create a UBI volume.
Reuben Dowle3e2834f2011-02-05 03:18:08 +010023//config:
24//config:config UBIRMVOL
Denys Vlasenkob097a842018-12-28 03:20:17 +010025//config: bool "ubirmvol (4.9 kb)"
Reuben Dowle3e2834f2011-02-05 03:18:08 +010026//config: default y
Reuben Dowle3e2834f2011-02-05 03:18:08 +010027//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020028//config: Delete a UBI volume.
Reuben Dowle3e2834f2011-02-05 03:18:08 +010029//config:
30//config:config UBIRSVOL
Denys Vlasenkob097a842018-12-28 03:20:17 +010031//config: bool "ubirsvol (4.2 kb)"
Reuben Dowle3e2834f2011-02-05 03:18:08 +010032//config: default y
Reuben Dowle3e2834f2011-02-05 03:18:08 +010033//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020034//config: Resize a UBI volume.
Reuben Dowlea6108422011-04-26 04:27:48 +020035//config:
36//config:config UBIUPDATEVOL
Denys Vlasenkob097a842018-12-28 03:20:17 +010037//config: bool "ubiupdatevol (5.2 kb)"
Reuben Dowlea6108422011-04-26 04:27:48 +020038//config: default y
Reuben Dowlea6108422011-04-26 04:27:48 +020039//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020040//config: Update a UBI volume.
Reuben Dowle3e2834f2011-02-05 03:18:08 +010041
Denys Vlasenko205d48e2017-01-29 14:57:33 +010042// APPLET_ODDNAME:name main location suid_type help
43//applet:IF_UBIATTACH( APPLET_ODDNAME(ubiattach, ubi_tools, BB_DIR_USR_SBIN, BB_SUID_DROP, ubiattach))
44//applet:IF_UBIDETACH( APPLET_ODDNAME(ubidetach, ubi_tools, BB_DIR_USR_SBIN, BB_SUID_DROP, ubidetach))
45//applet:IF_UBIMKVOL( APPLET_ODDNAME(ubimkvol, ubi_tools, BB_DIR_USR_SBIN, BB_SUID_DROP, ubimkvol))
46//applet:IF_UBIRMVOL( APPLET_ODDNAME(ubirmvol, ubi_tools, BB_DIR_USR_SBIN, BB_SUID_DROP, ubirmvol))
47//applet:IF_UBIRSVOL( APPLET_ODDNAME(ubirsvol, ubi_tools, BB_DIR_USR_SBIN, BB_SUID_DROP, ubirsvol))
Reuben Dowlea6108422011-04-26 04:27:48 +020048//applet:IF_UBIUPDATEVOL(APPLET_ODDNAME(ubiupdatevol, ubi_tools, BB_DIR_USR_SBIN, BB_SUID_DROP, ubiupdatevol))
Denys Vlasenko798b9452017-08-07 16:00:25 +020049/* not NOEXEC: if flash operation stalls, use less memory in "hung" process */
Reuben Dowle3e2834f2011-02-05 03:18:08 +010050
Denys Vlasenkod1993f12011-04-26 04:32:05 +020051//kbuild:lib-$(CONFIG_UBIATTACH) += ubi_tools.o
52//kbuild:lib-$(CONFIG_UBIDETACH) += ubi_tools.o
53//kbuild:lib-$(CONFIG_UBIMKVOL) += ubi_tools.o
54//kbuild:lib-$(CONFIG_UBIRMVOL) += ubi_tools.o
55//kbuild:lib-$(CONFIG_UBIRSVOL) += ubi_tools.o
56//kbuild:lib-$(CONFIG_UBIUPDATEVOL) += ubi_tools.o
Baruch Siach3324c962010-06-16 12:22:58 +020057
58#include "libbb.h"
Denys Vlasenko7fe1e3f2012-03-11 18:04:14 +010059/* Some versions of kernel have broken headers, need this hack */
60#ifndef __packed
61# define __packed __attribute__((packed))
62#endif
Baruch Siach3324c962010-06-16 12:22:58 +020063#include <mtd/ubi-user.h>
64
Denys Vlasenko115e0a72017-08-07 02:55:33 +020065#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'))
Baruch Siach3324c962010-06-16 12:22:58 +020080
Denys Vlasenko798b9452017-08-07 16:00:25 +020081static unsigned get_num_from_file(const char *path, unsigned max)
Denys Vlasenko65bdf0b2013-06-30 02:38:18 +020082{
83 char buf[sizeof(long long)*3];
84 unsigned long long num;
Denys Vlasenko0bfb9c22011-04-26 04:31:03 +020085
Denys Vlasenko65bdf0b2013-06-30 02:38:18 +020086 if (open_read_close(path, buf, sizeof(buf)) < 0)
Denys Vlasenko798b9452017-08-07 16:00:25 +020087 bb_perror_msg_and_die("can't open '%s'", path);
Denys Vlasenko65bdf0b2013-06-30 02:38:18 +020088 /* It can be \n terminated, xatoull won't work well */
89 if (sscanf(buf, "%llu", &num) != 1 || num > max)
Denys Vlasenko798b9452017-08-07 16:00:25 +020090 bb_error_msg_and_die("number in '%s' is malformed or too large", path);
Denys Vlasenko65bdf0b2013-06-30 02:38:18 +020091 return num;
92}
93
94/* To prevent malloc(1G) accidents */
95#define MAX_SANE_ERASEBLOCK (16*1024*1024)
Baruch Siach3324c962010-06-16 12:22:58 +020096
Reuben Dowle3e2834f2011-02-05 03:18:08 +010097int ubi_tools_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
98int ubi_tools_main(int argc UNUSED_PARAM, char **argv)
Baruch Siach3324c962010-06-16 12:22:58 +020099{
Denys Vlasenko965b7952020-11-30 13:03:03 +0100100 static const struct suffix_mult size_suffixes[] ALIGN_SUFFIX = {
Denys Vlasenkoe9b3fcc2013-06-30 02:47:45 +0200101 { "KiB", 1024 },
102 { "MiB", 1024*1024 },
103 { "GiB", 1024*1024*1024 },
104 { "", 0 }
105 };
106
Baruch Siach3324c962010-06-16 12:22:58 +0200107 unsigned opts;
108 char *ubi_ctrl;
Baruch Siach3324c962010-06-16 12:22:58 +0200109 int fd;
110 int mtd_num;
111 int dev_num = UBI_DEV_NUM_AUTO;
Reuben Dowle3e2834f2011-02-05 03:18:08 +0100112 int vol_id = UBI_VOL_NUM_AUTO;
Micke Prag07fa09a2014-11-02 11:08:24 +0100113 int vid_hdr_offset = 0;
Denys Vlasenko65bdf0b2013-06-30 02:38:18 +0200114 char *vol_name;
115 unsigned long long size_bytes = size_bytes; /* for compiler */
116 char *size_bytes_str;
Reuben Dowle3e2834f2011-02-05 03:18:08 +0100117 int alignment = 1;
Denys Vlasenko65bdf0b2013-06-30 02:38:18 +0200118 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 + /*just in case:*/ 16];
129#define path_sys_class_ubi_ubi (path + sizeof("/sys/class/ubi/ubi")-1)
130
Paul B. Henson985345d2013-08-04 21:07:20 +0200131 strcpy(path, "/sys/class/ubi/ubi");
Denys Vlasenko65bdf0b2013-06-30 02:38:18 +0200132 memset(&req_structs, 0, sizeof(req_structs));
Baruch Siach3324c962010-06-16 12:22:58 +0200133
Denys Vlasenko65bdf0b2013-06-30 02:38:18 +0200134#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)
Denys Vlasenko186b98a2014-09-25 22:10:32 +0200141 if (do_mkvol) {
Denys Vlasenko22542ec2017-08-08 21:55:02 +0200142 opts = getopt32(argv, "^" "md:+n:+N:s:a:+t:O:+" "\0" "-1",
Denys Vlasenko186b98a2014-09-25 22:10:32 +0200143 &dev_num, &vol_id,
Micke Prag07fa09a2014-11-02 11:08:24 +0100144 &vol_name, &size_bytes_str, &alignment, &type,
145 &vid_hdr_offset
Denys Vlasenko186b98a2014-09-25 22:10:32 +0200146 );
147 } else
148 if (do_update) {
Denys Vlasenko22542ec2017-08-08 21:55:02 +0200149 opts = getopt32(argv, "^" "s:at" "\0" "-1", &size_bytes_str);
Denys Vlasenko186b98a2014-09-25 22:10:32 +0200150 opts *= OPTION_s;
151 } else {
Denys Vlasenko22542ec2017-08-08 21:55:02 +0200152 opts = getopt32(argv, "^" "m:+d:+n:+N:s:a:+t:" "\0" "-1",
Denys Vlasenko186b98a2014-09-25 22:10:32 +0200153 &mtd_num, &dev_num, &vol_id,
154 &vol_name, &size_bytes_str, &alignment, &type
155 );
156 }
Denys Vlasenko65bdf0b2013-06-30 02:38:18 +0200157
158 if (opts & OPTION_s)
Denys Vlasenkoe9b3fcc2013-06-30 02:47:45 +0200159 size_bytes = xatoull_sfx(size_bytes_str, size_suffixes);
Denys Vlasenko65bdf0b2013-06-30 02:38:18 +0200160 argv += optind;
Denys Vlasenkof798ba92013-06-30 02:46:44 +0200161 ubi_ctrl = *argv++;
Baruch Siach3324c962010-06-16 12:22:58 +0200162
163 fd = xopen(ubi_ctrl, O_RDWR);
Denys Vlasenko8d3e2252010-08-31 12:42:06 +0200164 //xfstat(fd, &st, ubi_ctrl);
Baruch Siach3324c962010-06-16 12:22:58 +0200165 //if (!S_ISCHR(st.st_mode))
Denys Vlasenko8d3e2252010-08-31 12:42:06 +0200166 // bb_error_msg_and_die("%s: not a char device", ubi_ctrl);
Baruch Siach3324c962010-06-16 12:22:58 +0200167
Denys Vlasenko65bdf0b2013-06-30 02:38:18 +0200168//usage:#define ubiattach_trivial_usage
Micke Prag07fa09a2014-11-02 11:08:24 +0100169//usage: "-m MTD_NUM [-d UBI_NUM] [-O VID_HDR_OFF] UBI_CTRL_DEV"
Denys Vlasenko65bdf0b2013-06-30 02:38:18 +0200170//usage:#define ubiattach_full_usage "\n\n"
171//usage: "Attach MTD device to UBI\n"
172//usage: "\n -m MTD_NUM MTD device number to attach"
173//usage: "\n -d UBI_NUM UBI device number to assign"
Micke Prag07fa09a2014-11-02 11:08:24 +0100174//usage: "\n -O VID_HDR_OFF VID header offset"
Baruch Siach3324c962010-06-16 12:22:58 +0200175 if (do_attach) {
Denys Vlasenko65bdf0b2013-06-30 02:38:18 +0200176 if (!(opts & OPTION_m))
Baruch Siach3324c962010-06-16 12:22:58 +0200177 bb_error_msg_and_die("%s device not specified", "MTD");
178
Denys Vlasenko65bdf0b2013-06-30 02:38:18 +0200179 attach_req.mtd_num = mtd_num;
180 attach_req.ubi_num = dev_num;
Micke Prag07fa09a2014-11-02 11:08:24 +0100181 attach_req.vid_hdr_offset = vid_hdr_offset;
Baruch Siach3324c962010-06-16 12:22:58 +0200182
Denys Vlasenko65bdf0b2013-06-30 02:38:18 +0200183 xioctl(fd, UBI_IOCATT, &attach_req);
Reuben Dowle3e2834f2011-02-05 03:18:08 +0100184 } else
Denys Vlasenko65bdf0b2013-06-30 02:38:18 +0200185
186//usage:#define ubidetach_trivial_usage
187//usage: "-d UBI_NUM UBI_CTRL_DEV"
188//usage:#define ubidetach_full_usage "\n\n"
189//usage: "Detach MTD device from UBI\n"
190//usage: "\n -d UBI_NUM UBI device number"
Reuben Dowle3e2834f2011-02-05 03:18:08 +0100191 if (do_detach) {
Denys Vlasenko65bdf0b2013-06-30 02:38:18 +0200192 if (!(opts & OPTION_d))
Baruch Siach3324c962010-06-16 12:22:58 +0200193 bb_error_msg_and_die("%s device not specified", "UBI");
194
Denys Vlasenko65bdf0b2013-06-30 02:38:18 +0200195 /* FIXME? kernel expects int32_t* here: */
Baruch Siach3324c962010-06-16 12:22:58 +0200196 xioctl(fd, UBI_IOCDET, &dev_num);
Reuben Dowle3e2834f2011-02-05 03:18:08 +0100197 } else
Denys Vlasenko65bdf0b2013-06-30 02:38:18 +0200198
199//usage:#define ubimkvol_trivial_usage
Sven Eisenbergb068cf22016-04-03 21:53:12 +0200200//usage: "-N NAME [-s SIZE | -m] UBI_DEVICE"
Denys Vlasenko65bdf0b2013-06-30 02:38:18 +0200201//usage:#define ubimkvol_full_usage "\n\n"
202//usage: "Create UBI volume\n"
203//usage: "\n -a ALIGNMENT Volume alignment (default 1)"
204//usage: "\n -m Set volume size to maximum available"
205//usage: "\n -n VOLID Volume ID. If not specified,"
206//usage: "\n assigned automatically"
207//usage: "\n -N NAME Volume name"
208//usage: "\n -s SIZE Size in bytes"
209//usage: "\n -t TYPE Volume type (static|dynamic)"
Reuben Dowle3e2834f2011-02-05 03:18:08 +0100210 if (do_mkvol) {
Denys Vlasenko65bdf0b2013-06-30 02:38:18 +0200211 if (opts & OPTION_m) {
Paul B. Hensonf86404f2013-06-02 17:48:05 -0700212 unsigned leb_avail;
213 unsigned leb_size;
214 unsigned num;
Paul B. Hensonf86404f2013-06-02 17:48:05 -0700215 char *p;
Paul B. Hensonf86404f2013-06-02 17:48:05 -0700216
Sven Eisenbergb068cf22016-04-03 21:53:12 +0200217 num = ubi_devnum_from_devname(ubi_ctrl);
Denys Vlasenko65bdf0b2013-06-30 02:38:18 +0200218 p = path_sys_class_ubi_ubi + sprintf(path_sys_class_ubi_ubi, "%u/", num);
Paul B. Hensonf86404f2013-06-02 17:48:05 -0700219
220 strcpy(p, "avail_eraseblocks");
Denys Vlasenko798b9452017-08-07 16:00:25 +0200221 leb_avail = get_num_from_file(path, UINT_MAX);
Paul B. Hensonf86404f2013-06-02 17:48:05 -0700222
223 strcpy(p, "eraseblock_size");
Denys Vlasenko798b9452017-08-07 16:00:25 +0200224 leb_size = get_num_from_file(path, MAX_SANE_ERASEBLOCK);
Paul B. Hensonf86404f2013-06-02 17:48:05 -0700225
Denys Vlasenko65bdf0b2013-06-30 02:38:18 +0200226 size_bytes = leb_avail * (unsigned long long)leb_size;
227 //if (size_bytes <= 0)
228 // bb_error_msg_and_die("%s invalid maximum size calculated", "UBI");
Paul B. Hensonf86404f2013-06-02 17:48:05 -0700229 } else
Reuben Dowle3e2834f2011-02-05 03:18:08 +0100230 if (!(opts & OPTION_s))
James Byrne69374872019-07-02 11:35:03 +0200231 bb_simple_error_msg_and_die("size not specified");
Denys Vlasenko65bdf0b2013-06-30 02:38:18 +0200232
Reuben Dowle3e2834f2011-02-05 03:18:08 +0100233 if (!(opts & OPTION_N))
James Byrne69374872019-07-02 11:35:03 +0200234 bb_simple_error_msg_and_die("name not specified");
Reuben Dowle3e2834f2011-02-05 03:18:08 +0100235
Denys Vlasenko798b9452017-08-07 16:00:25 +0200236 /* the structure is memset(0) above */
Denys Vlasenko65bdf0b2013-06-30 02:38:18 +0200237 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; /* signed int64_t */
Denys Vlasenko798b9452017-08-07 16:00:25 +0200243 /* strnlen avoids overflow of 16-bit field (paranoia) */
244 mkvol_req.name_len = strnlen(vol_name, UBI_MAX_VOLUME_NAME+1);
Denys Vlasenko65bdf0b2013-06-30 02:38:18 +0200245 if (mkvol_req.name_len > UBI_MAX_VOLUME_NAME)
246 bb_error_msg_and_die("volume name too long: '%s'", vol_name);
Denys Vlasenko798b9452017-08-07 16:00:25 +0200247 /* this is safe: .name[] is UBI_MAX_VOLUME_NAME+1 bytes */
248 strcpy(mkvol_req.name, vol_name);
Reuben Dowle3e2834f2011-02-05 03:18:08 +0100249
Denys Vlasenko65bdf0b2013-06-30 02:38:18 +0200250 xioctl(fd, UBI_IOCMKVOL, &mkvol_req);
Reuben Dowle3e2834f2011-02-05 03:18:08 +0100251 } else
Denys Vlasenko65bdf0b2013-06-30 02:38:18 +0200252
253//usage:#define ubirmvol_trivial_usage
Denys Vlasenko1f60d882021-06-15 10:00:18 +0200254//usage: "-n VOLID | -N VOLNAME UBI_DEVICE"
Denys Vlasenko65bdf0b2013-06-30 02:38:18 +0200255//usage:#define ubirmvol_full_usage "\n\n"
256//usage: "Remove UBI volume\n"
257//usage: "\n -n VOLID Volume ID"
Sven Eisenbergb068cf22016-04-03 21:53:12 +0200258//usage: "\n -N VOLNAME Volume name"
Reuben Dowle3e2834f2011-02-05 03:18:08 +0100259 if (do_rmvol) {
Sven Eisenbergb068cf22016-04-03 21:53:12 +0200260 if (!(opts & (OPTION_n|OPTION_N)))
James Byrne69374872019-07-02 11:35:03 +0200261 bb_simple_error_msg_and_die("volume id not specified");
Reuben Dowle3e2834f2011-02-05 03:18:08 +0100262
Sven Eisenbergb068cf22016-04-03 21:53:12 +0200263 if (opts & OPTION_N) {
264 unsigned num = ubi_devnum_from_devname(ubi_ctrl);
Denys Vlasenko6aab9922016-04-03 22:24:51 +0200265 vol_id = ubi_get_volid_by_name(num, vol_name);
Sven Eisenbergb068cf22016-04-03 21:53:12 +0200266 }
267
268 if (sizeof(vol_id) != 4) {
269 /* kernel expects int32_t* in this ioctl */
270 int32_t t = vol_id;
271 xioctl(fd, UBI_IOCRMVOL, &t);
272 } else {
273 xioctl(fd, UBI_IOCRMVOL, &vol_id);
274 }
Reuben Dowle3e2834f2011-02-05 03:18:08 +0100275 } else
Denys Vlasenko65bdf0b2013-06-30 02:38:18 +0200276
277//usage:#define ubirsvol_trivial_usage
Sven Eisenbergb068cf22016-04-03 21:53:12 +0200278//usage: "-n VOLID -s SIZE UBI_DEVICE"
Denys Vlasenko65bdf0b2013-06-30 02:38:18 +0200279//usage:#define ubirsvol_full_usage "\n\n"
280//usage: "Resize UBI volume\n"
281//usage: "\n -n VOLID Volume ID"
282//usage: "\n -s SIZE Size in bytes"
Reuben Dowle3e2834f2011-02-05 03:18:08 +0100283 if (do_rsvol) {
Reuben Dowle3e2834f2011-02-05 03:18:08 +0100284 if (!(opts & OPTION_s))
James Byrne69374872019-07-02 11:35:03 +0200285 bb_simple_error_msg_and_die("size not specified");
Reuben Dowle3e2834f2011-02-05 03:18:08 +0100286 if (!(opts & OPTION_n))
James Byrne69374872019-07-02 11:35:03 +0200287 bb_simple_error_msg_and_die("volume id not specified");
Reuben Dowle3e2834f2011-02-05 03:18:08 +0100288
Denys Vlasenko65bdf0b2013-06-30 02:38:18 +0200289 rsvol_req.bytes = size_bytes; /* signed int64_t */
290 rsvol_req.vol_id = vol_id;
Reuben Dowle3e2834f2011-02-05 03:18:08 +0100291
Denys Vlasenko65bdf0b2013-06-30 02:38:18 +0200292 xioctl(fd, UBI_IOCRSVOL, &rsvol_req);
Reuben Dowlea6108422011-04-26 04:27:48 +0200293 } else
Denys Vlasenko65bdf0b2013-06-30 02:38:18 +0200294
295//usage:#define ubiupdatevol_trivial_usage
Denys Vlasenkob34eb4a2017-08-07 03:24:29 +0200296//usage: "-t UBI_DEVICE | [-s SIZE] UBI_DEVICE IMG_FILE"
Denys Vlasenko65bdf0b2013-06-30 02:38:18 +0200297//usage:#define ubiupdatevol_full_usage "\n\n"
298//usage: "Update UBI volume\n"
299//usage: "\n -t Truncate to zero size"
300//usage: "\n -s SIZE Size in bytes to resize to"
Reuben Dowlea6108422011-04-26 04:27:48 +0200301 if (do_update) {
Denys Vlasenko65bdf0b2013-06-30 02:38:18 +0200302 int64_t bytes64;
Reuben Dowlea6108422011-04-26 04:27:48 +0200303
304 if (opts & OPTION_t) {
Denys Vlasenko65bdf0b2013-06-30 02:38:18 +0200305 /* truncate the volume by starting an update for size 0 */
306 bytes64 = 0;
307 /* this ioctl expects int64_t* parameter */
308 xioctl(fd, UBI_IOCVOLUP, &bytes64);
Reuben Dowlea6108422011-04-26 04:27:48 +0200309 }
310 else {
Reuben Dowlea6108422011-04-26 04:27:48 +0200311 unsigned ubinum, volnum;
312 unsigned leb_size;
Denys Vlasenko798b9452017-08-07 16:00:25 +0200313 char *buf;
Reuben Dowlea6108422011-04-26 04:27:48 +0200314
Denys Vlasenko65bdf0b2013-06-30 02:38:18 +0200315 /* Assume that device is in normal format. */
316 /* Removes need for scanning sysfs tree as full libubi does. */
Reuben Dowlea6108422011-04-26 04:27:48 +0200317 if (sscanf(ubi_ctrl, "/dev/ubi%u_%u", &ubinum, &volnum) != 2)
Denys Vlasenko798b9452017-08-07 16:00:25 +0200318 bb_error_msg_and_die("UBI device name '%s' is not /dev/ubiN_M", ubi_ctrl);
Reuben Dowlea6108422011-04-26 04:27:48 +0200319
Denys Vlasenko65bdf0b2013-06-30 02:38:18 +0200320 sprintf(path_sys_class_ubi_ubi, "%u_%u/usable_eb_size", ubinum, volnum);
Denys Vlasenko798b9452017-08-07 16:00:25 +0200321 leb_size = get_num_from_file(path, MAX_SANE_ERASEBLOCK);
Reuben Dowlea6108422011-04-26 04:27:48 +0200322
Denys Vlasenko798b9452017-08-07 16:00:25 +0200323 if (!*argv)
324 bb_show_usage();
325 if (NOT_LONE_DASH(*argv)) /* mtd-utils supports "-" as stdin */
Denys Vlasenko65bdf0b2013-06-30 02:38:18 +0200326 xmove_fd(xopen(*argv, O_RDONLY), STDIN_FILENO);
Denys Vlasenko798b9452017-08-07 16:00:25 +0200327
328 if (!(opts & OPTION_s)) {
329 struct stat st;
330 xfstat(STDIN_FILENO, &st, *argv);
331 size_bytes = st.st_size;
Reuben Dowlea6108422011-04-26 04:27:48 +0200332 }
333
Denys Vlasenko65bdf0b2013-06-30 02:38:18 +0200334 bytes64 = size_bytes;
335 /* this ioctl expects signed int64_t* parameter */
336 xioctl(fd, UBI_IOCVOLUP, &bytes64);
Reuben Dowlea6108422011-04-26 04:27:48 +0200337
Denys Vlasenko798b9452017-08-07 16:00:25 +0200338 /* can't use bb_copyfd_exact_size(): copy in blocks of exactly leb_size */
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 /* for this case: "ubiupdatevol -s 1024000 $UBIDEV /dev/urandom" */
349 len = size_bytes;
350 }
351 xwrite(fd, buf, len);
352 size_bytes -= len;
Reuben Dowlea6108422011-04-26 04:27:48 +0200353 }
Denys Vlasenko798b9452017-08-07 16:00:25 +0200354 if (ENABLE_FEATURE_CLEAN_UP)
355 free(buf);
Reuben Dowlea6108422011-04-26 04:27:48 +0200356 }
Baruch Siach3324c962010-06-16 12:22:58 +0200357 }
358
359 if (ENABLE_FEATURE_CLEAN_UP)
360 close(fd);
361
362 return EXIT_SUCCESS;
363}