ubi_tools.c 11 KB

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