ubi_tools.c 12 KB

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