ubi_tools.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /*
  2. * Ported to busybox from mtd-utils.
  3. *
  4. * Licensed under GPLv2, see file LICENSE in this source tree.
  5. */
  6. //config:config UBIATTACH
  7. //config: bool "ubiattach (4.7 kb)"
  8. //config: default y
  9. //config: select PLATFORM_LINUX
  10. //config: help
  11. //config: Attach MTD device to an UBI device.
  12. //config:
  13. //config:config UBIDETACH
  14. //config: bool "ubidetach (4.6 kb)"
  15. //config: default y
  16. //config: select PLATFORM_LINUX
  17. //config: help
  18. //config: Detach MTD device from an UBI device.
  19. //config:
  20. //config:config UBIMKVOL
  21. //config: bool "ubimkvol (5.8 kb)"
  22. //config: default y
  23. //config: select PLATFORM_LINUX
  24. //config: help
  25. //config: Create a UBI volume.
  26. //config:
  27. //config:config UBIRMVOL
  28. //config: bool "ubirmvol (5.2 kb)"
  29. //config: default y
  30. //config: select PLATFORM_LINUX
  31. //config: help
  32. //config: Delete a UBI volume.
  33. //config:
  34. //config:config UBIRSVOL
  35. //config: bool "ubirsvol (4.6 kb)"
  36. //config: default y
  37. //config: select PLATFORM_LINUX
  38. //config: help
  39. //config: Resize a UBI volume.
  40. //config:
  41. //config:config UBIUPDATEVOL
  42. //config: bool "ubiupdatevol (5.6 kb)"
  43. //config: default y
  44. //config: select PLATFORM_LINUX
  45. //config: help
  46. //config: Update a UBI volume.
  47. // APPLET_ODDNAME:name main location suid_type help
  48. //applet:IF_UBIATTACH( APPLET_ODDNAME(ubiattach, ubi_tools, BB_DIR_USR_SBIN, BB_SUID_DROP, ubiattach))
  49. //applet:IF_UBIDETACH( APPLET_ODDNAME(ubidetach, ubi_tools, BB_DIR_USR_SBIN, BB_SUID_DROP, ubidetach))
  50. //applet:IF_UBIMKVOL( APPLET_ODDNAME(ubimkvol, ubi_tools, BB_DIR_USR_SBIN, BB_SUID_DROP, ubimkvol))
  51. //applet:IF_UBIRMVOL( APPLET_ODDNAME(ubirmvol, ubi_tools, BB_DIR_USR_SBIN, BB_SUID_DROP, ubirmvol))
  52. //applet:IF_UBIRSVOL( APPLET_ODDNAME(ubirsvol, ubi_tools, BB_DIR_USR_SBIN, BB_SUID_DROP, ubirsvol))
  53. //applet:IF_UBIUPDATEVOL(APPLET_ODDNAME(ubiupdatevol, ubi_tools, BB_DIR_USR_SBIN, BB_SUID_DROP, ubiupdatevol))
  54. /* not NOEXEC: if flash operation stalls, use less memory in "hung" process */
  55. //kbuild:lib-$(CONFIG_UBIATTACH) += ubi_tools.o
  56. //kbuild:lib-$(CONFIG_UBIDETACH) += ubi_tools.o
  57. //kbuild:lib-$(CONFIG_UBIMKVOL) += ubi_tools.o
  58. //kbuild:lib-$(CONFIG_UBIRMVOL) += ubi_tools.o
  59. //kbuild:lib-$(CONFIG_UBIRSVOL) += ubi_tools.o
  60. //kbuild:lib-$(CONFIG_UBIUPDATEVOL) += ubi_tools.o
  61. #include "libbb.h"
  62. /* Some versions of kernel have broken headers, need this hack */
  63. #ifndef __packed
  64. # define __packed __attribute__((packed))
  65. #endif
  66. #include <mtd/ubi-user.h>
  67. #define UBI_APPLET_CNT (0 \
  68. + ENABLE_UBIATTACH \
  69. + ENABLE_UBIDETACH \
  70. + ENABLE_UBIMKVOL \
  71. + ENABLE_UBIRMVOL \
  72. + ENABLE_UBIRSVOL \
  73. + ENABLE_UBIUPDATEVOL \
  74. )
  75. #define do_attach (ENABLE_UBIATTACH && (UBI_APPLET_CNT == 1 || applet_name[4] == 't'))
  76. #define do_detach (ENABLE_UBIDETACH && (UBI_APPLET_CNT == 1 || applet_name[4] == 'e'))
  77. #define do_mkvol (ENABLE_UBIMKVOL && (UBI_APPLET_CNT == 1 || applet_name[4] == 'k'))
  78. #define do_rmvol (ENABLE_UBIRMVOL && (UBI_APPLET_CNT == 1 || applet_name[4] == 'm'))
  79. #define do_rsvol (ENABLE_UBIRSVOL && (UBI_APPLET_CNT == 1 || applet_name[4] == 's'))
  80. #define do_update (ENABLE_UBIUPDATEVOL && (UBI_APPLET_CNT == 1 || applet_name[4] == 'p'))
  81. static unsigned get_num_from_file(const char *path, unsigned max)
  82. {
  83. char buf[sizeof(long long)*3];
  84. unsigned long long num;
  85. if (open_read_close(path, buf, sizeof(buf)) < 0)
  86. bb_perror_msg_and_die("can't open '%s'", path);
  87. /* It can be \n terminated, xatoull won't work well */
  88. if (sscanf(buf, "%llu", &num) != 1 || num > max)
  89. bb_error_msg_and_die("number in '%s' is malformed or too large", path);
  90. return num;
  91. }
  92. /* To prevent malloc(1G) accidents */
  93. #define MAX_SANE_ERASEBLOCK (16*1024*1024)
  94. int ubi_tools_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  95. int ubi_tools_main(int argc UNUSED_PARAM, char **argv)
  96. {
  97. static const struct suffix_mult size_suffixes[] = {
  98. { "KiB", 1024 },
  99. { "MiB", 1024*1024 },
  100. { "GiB", 1024*1024*1024 },
  101. { "", 0 }
  102. };
  103. unsigned opts;
  104. char *ubi_ctrl;
  105. int fd;
  106. int mtd_num;
  107. int dev_num = UBI_DEV_NUM_AUTO;
  108. int vol_id = UBI_VOL_NUM_AUTO;
  109. int vid_hdr_offset = 0;
  110. char *vol_name;
  111. unsigned long long size_bytes = size_bytes; /* for compiler */
  112. char *size_bytes_str;
  113. int alignment = 1;
  114. char *type;
  115. union {
  116. struct ubi_attach_req attach_req;
  117. struct ubi_mkvol_req mkvol_req;
  118. struct ubi_rsvol_req rsvol_req;
  119. } req_structs;
  120. #define attach_req req_structs.attach_req
  121. #define mkvol_req req_structs.mkvol_req
  122. #define rsvol_req req_structs.rsvol_req
  123. char path[sizeof("/sys/class/ubi/ubi%d_%d/usable_eb_size")
  124. + 2 * sizeof(int)*3 + /*just in case:*/ 16];
  125. #define path_sys_class_ubi_ubi (path + sizeof("/sys/class/ubi/ubi")-1)
  126. strcpy(path, "/sys/class/ubi/ubi");
  127. memset(&req_structs, 0, sizeof(req_structs));
  128. #define OPTION_m (1 << 0)
  129. #define OPTION_d (1 << 1)
  130. #define OPTION_n (1 << 2)
  131. #define OPTION_N (1 << 3)
  132. #define OPTION_s (1 << 4)
  133. #define OPTION_a (1 << 5)
  134. #define OPTION_t (1 << 6)
  135. if (do_mkvol) {
  136. opts = getopt32(argv, "^" "md:+n:+N:s:a:+t:O:+" "\0" "-1",
  137. &dev_num, &vol_id,
  138. &vol_name, &size_bytes_str, &alignment, &type,
  139. &vid_hdr_offset
  140. );
  141. } else
  142. if (do_update) {
  143. opts = getopt32(argv, "^" "s:at" "\0" "-1", &size_bytes_str);
  144. opts *= OPTION_s;
  145. } else {
  146. opts = getopt32(argv, "^" "m:+d:+n:+N:s:a:+t:" "\0" "-1",
  147. &mtd_num, &dev_num, &vol_id,
  148. &vol_name, &size_bytes_str, &alignment, &type
  149. );
  150. }
  151. if (opts & OPTION_s)
  152. size_bytes = xatoull_sfx(size_bytes_str, size_suffixes);
  153. argv += optind;
  154. ubi_ctrl = *argv++;
  155. fd = xopen(ubi_ctrl, O_RDWR);
  156. //xfstat(fd, &st, ubi_ctrl);
  157. //if (!S_ISCHR(st.st_mode))
  158. // bb_error_msg_and_die("%s: not a char device", ubi_ctrl);
  159. //usage:#define ubiattach_trivial_usage
  160. //usage: "-m MTD_NUM [-d UBI_NUM] [-O VID_HDR_OFF] UBI_CTRL_DEV"
  161. //usage:#define ubiattach_full_usage "\n\n"
  162. //usage: "Attach MTD device to UBI\n"
  163. //usage: "\n -m MTD_NUM MTD device number to attach"
  164. //usage: "\n -d UBI_NUM UBI device number to assign"
  165. //usage: "\n -O VID_HDR_OFF VID header offset"
  166. if (do_attach) {
  167. if (!(opts & OPTION_m))
  168. bb_error_msg_and_die("%s device not specified", "MTD");
  169. attach_req.mtd_num = mtd_num;
  170. attach_req.ubi_num = dev_num;
  171. attach_req.vid_hdr_offset = vid_hdr_offset;
  172. xioctl(fd, UBI_IOCATT, &attach_req);
  173. } else
  174. //usage:#define ubidetach_trivial_usage
  175. //usage: "-d UBI_NUM UBI_CTRL_DEV"
  176. //usage:#define ubidetach_full_usage "\n\n"
  177. //usage: "Detach MTD device from UBI\n"
  178. //usage: "\n -d UBI_NUM UBI device number"
  179. if (do_detach) {
  180. if (!(opts & OPTION_d))
  181. bb_error_msg_and_die("%s device not specified", "UBI");
  182. /* FIXME? kernel expects int32_t* here: */
  183. xioctl(fd, UBI_IOCDET, &dev_num);
  184. } else
  185. //usage:#define ubimkvol_trivial_usage
  186. //usage: "-N NAME [-s SIZE | -m] UBI_DEVICE"
  187. //usage:#define ubimkvol_full_usage "\n\n"
  188. //usage: "Create UBI volume\n"
  189. //usage: "\n -a ALIGNMENT Volume alignment (default 1)"
  190. //usage: "\n -m Set volume size to maximum available"
  191. //usage: "\n -n VOLID Volume ID. If not specified,"
  192. //usage: "\n assigned automatically"
  193. //usage: "\n -N NAME Volume name"
  194. //usage: "\n -s SIZE Size in bytes"
  195. //usage: "\n -t TYPE Volume type (static|dynamic)"
  196. if (do_mkvol) {
  197. if (opts & OPTION_m) {
  198. unsigned leb_avail;
  199. unsigned leb_size;
  200. unsigned num;
  201. char *p;
  202. num = ubi_devnum_from_devname(ubi_ctrl);
  203. p = path_sys_class_ubi_ubi + sprintf(path_sys_class_ubi_ubi, "%u/", num);
  204. strcpy(p, "avail_eraseblocks");
  205. leb_avail = get_num_from_file(path, UINT_MAX);
  206. strcpy(p, "eraseblock_size");
  207. leb_size = get_num_from_file(path, MAX_SANE_ERASEBLOCK);
  208. size_bytes = leb_avail * (unsigned long long)leb_size;
  209. //if (size_bytes <= 0)
  210. // bb_error_msg_and_die("%s invalid maximum size calculated", "UBI");
  211. } else
  212. if (!(opts & OPTION_s))
  213. bb_error_msg_and_die("size not specified");
  214. if (!(opts & OPTION_N))
  215. bb_error_msg_and_die("name not specified");
  216. /* the structure is memset(0) above */
  217. mkvol_req.vol_id = vol_id;
  218. mkvol_req.vol_type = UBI_DYNAMIC_VOLUME;
  219. if ((opts & OPTION_t) && type[0] == 's')
  220. mkvol_req.vol_type = UBI_STATIC_VOLUME;
  221. mkvol_req.alignment = alignment;
  222. mkvol_req.bytes = size_bytes; /* signed int64_t */
  223. /* strnlen avoids overflow of 16-bit field (paranoia) */
  224. mkvol_req.name_len = strnlen(vol_name, UBI_MAX_VOLUME_NAME+1);
  225. if (mkvol_req.name_len > UBI_MAX_VOLUME_NAME)
  226. bb_error_msg_and_die("volume name too long: '%s'", vol_name);
  227. /* this is safe: .name[] is UBI_MAX_VOLUME_NAME+1 bytes */
  228. strcpy(mkvol_req.name, vol_name);
  229. xioctl(fd, UBI_IOCMKVOL, &mkvol_req);
  230. } else
  231. //usage:#define ubirmvol_trivial_usage
  232. //usage: "-n VOLID / -N VOLNAME UBI_DEVICE"
  233. //usage:#define ubirmvol_full_usage "\n\n"
  234. //usage: "Remove UBI volume\n"
  235. //usage: "\n -n VOLID Volume ID"
  236. //usage: "\n -N VOLNAME Volume name"
  237. if (do_rmvol) {
  238. if (!(opts & (OPTION_n|OPTION_N)))
  239. bb_error_msg_and_die("volume id not specified");
  240. if (opts & OPTION_N) {
  241. unsigned num = ubi_devnum_from_devname(ubi_ctrl);
  242. vol_id = ubi_get_volid_by_name(num, vol_name);
  243. }
  244. if (sizeof(vol_id) != 4) {
  245. /* kernel expects int32_t* in this ioctl */
  246. int32_t t = vol_id;
  247. xioctl(fd, UBI_IOCRMVOL, &t);
  248. } else {
  249. xioctl(fd, UBI_IOCRMVOL, &vol_id);
  250. }
  251. } else
  252. //usage:#define ubirsvol_trivial_usage
  253. //usage: "-n VOLID -s SIZE UBI_DEVICE"
  254. //usage:#define ubirsvol_full_usage "\n\n"
  255. //usage: "Resize UBI volume\n"
  256. //usage: "\n -n VOLID Volume ID"
  257. //usage: "\n -s SIZE Size in bytes"
  258. if (do_rsvol) {
  259. if (!(opts & OPTION_s))
  260. bb_error_msg_and_die("size not specified");
  261. if (!(opts & OPTION_n))
  262. bb_error_msg_and_die("volume id not specified");
  263. rsvol_req.bytes = size_bytes; /* signed int64_t */
  264. rsvol_req.vol_id = vol_id;
  265. xioctl(fd, UBI_IOCRSVOL, &rsvol_req);
  266. } else
  267. //usage:#define ubiupdatevol_trivial_usage
  268. //usage: "-t UBI_DEVICE | [-s SIZE] UBI_DEVICE IMG_FILE"
  269. //usage:#define ubiupdatevol_full_usage "\n\n"
  270. //usage: "Update UBI volume\n"
  271. //usage: "\n -t Truncate to zero size"
  272. //usage: "\n -s SIZE Size in bytes to resize to"
  273. if (do_update) {
  274. int64_t bytes64;
  275. if (opts & OPTION_t) {
  276. /* truncate the volume by starting an update for size 0 */
  277. bytes64 = 0;
  278. /* this ioctl expects int64_t* parameter */
  279. xioctl(fd, UBI_IOCVOLUP, &bytes64);
  280. }
  281. else {
  282. unsigned ubinum, volnum;
  283. unsigned leb_size;
  284. char *buf;
  285. /* Assume that device is in normal format. */
  286. /* Removes need for scanning sysfs tree as full libubi does. */
  287. if (sscanf(ubi_ctrl, "/dev/ubi%u_%u", &ubinum, &volnum) != 2)
  288. bb_error_msg_and_die("UBI device name '%s' is not /dev/ubiN_M", ubi_ctrl);
  289. sprintf(path_sys_class_ubi_ubi, "%u_%u/usable_eb_size", ubinum, volnum);
  290. leb_size = get_num_from_file(path, MAX_SANE_ERASEBLOCK);
  291. if (!*argv)
  292. bb_show_usage();
  293. if (NOT_LONE_DASH(*argv)) /* mtd-utils supports "-" as stdin */
  294. xmove_fd(xopen(*argv, O_RDONLY), STDIN_FILENO);
  295. if (!(opts & OPTION_s)) {
  296. struct stat st;
  297. xfstat(STDIN_FILENO, &st, *argv);
  298. size_bytes = st.st_size;
  299. }
  300. bytes64 = size_bytes;
  301. /* this ioctl expects signed int64_t* parameter */
  302. xioctl(fd, UBI_IOCVOLUP, &bytes64);
  303. /* can't use bb_copyfd_exact_size(): copy in blocks of exactly leb_size */
  304. buf = xmalloc(leb_size);
  305. while (size_bytes != 0) {
  306. int len = full_read(STDIN_FILENO, buf, leb_size);
  307. if (len <= 0) {
  308. if (len < 0)
  309. bb_perror_msg_and_die("read error from '%s'", *argv);
  310. break;
  311. }
  312. if ((unsigned)len > size_bytes) {
  313. /* for this case: "ubiupdatevol -s 1024000 $UBIDEV /dev/urandom" */
  314. len = size_bytes;
  315. }
  316. xwrite(fd, buf, len);
  317. size_bytes -= len;
  318. }
  319. if (ENABLE_FEATURE_CLEAN_UP)
  320. free(buf);
  321. }
  322. }
  323. if (ENABLE_FEATURE_CLEAN_UP)
  324. close(fd);
  325. return EXIT_SUCCESS;
  326. }