ubi_tools.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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"
  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"
  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"
  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"
  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"
  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"
  42. //config: default y
  43. //config: select PLATFORM_LINUX
  44. //config: help
  45. //config: Update a UBI volume.
  46. //applet:IF_UBIATTACH(APPLET_ODDNAME(ubiattach, ubi_tools, BB_DIR_USR_SBIN, BB_SUID_DROP, ubiattach))
  47. //applet:IF_UBIDETACH(APPLET_ODDNAME(ubidetach, ubi_tools, BB_DIR_USR_SBIN, BB_SUID_DROP, ubidetach))
  48. //applet:IF_UBIMKVOL(APPLET_ODDNAME(ubimkvol, ubi_tools, BB_DIR_USR_SBIN, BB_SUID_DROP, ubimkvol))
  49. //applet:IF_UBIRMVOL(APPLET_ODDNAME(ubirmvol, ubi_tools, BB_DIR_USR_SBIN, BB_SUID_DROP, ubirmvol))
  50. //applet:IF_UBIRSVOL(APPLET_ODDNAME(ubirsvol, ubi_tools, BB_DIR_USR_SBIN, BB_SUID_DROP, ubirsvol))
  51. //applet:IF_UBIUPDATEVOL(APPLET_ODDNAME(ubiupdatevol, ubi_tools, BB_DIR_USR_SBIN, BB_SUID_DROP, ubiupdatevol))
  52. //kbuild:lib-$(CONFIG_UBIATTACH) += ubi_tools.o
  53. //kbuild:lib-$(CONFIG_UBIDETACH) += ubi_tools.o
  54. //kbuild:lib-$(CONFIG_UBIMKVOL) += ubi_tools.o
  55. //kbuild:lib-$(CONFIG_UBIRMVOL) += ubi_tools.o
  56. //kbuild:lib-$(CONFIG_UBIRSVOL) += ubi_tools.o
  57. //kbuild:lib-$(CONFIG_UBIUPDATEVOL) += ubi_tools.o
  58. #include "libbb.h"
  59. /* Some versions of kernel have broken headers, need this hack */
  60. #ifndef __packed
  61. # define __packed __attribute__((packed))
  62. #endif
  63. #include <mtd/ubi-user.h>
  64. #define OPTION_M (1 << 0)
  65. #define OPTION_D (1 << 1)
  66. #define OPTION_n (1 << 2)
  67. #define OPTION_N (1 << 3)
  68. #define OPTION_s (1 << 4)
  69. #define OPTION_a (1 << 5)
  70. #define OPTION_t (1 << 6)
  71. #define do_attach (ENABLE_UBIATTACH && applet_name[3] == 'a')
  72. #define do_detach (ENABLE_UBIDETACH && applet_name[3] == 'd')
  73. #define do_mkvol (ENABLE_UBIMKVOL && applet_name[3] == 'm')
  74. #define do_rmvol (ENABLE_UBIRMVOL && applet_name[4] == 'm')
  75. #define do_rsvol (ENABLE_UBIRSVOL && applet_name[4] == 's')
  76. #define do_update (ENABLE_UBIUPDATEVOL && applet_name[3] == 'u')
  77. //usage:#define ubiattach_trivial_usage
  78. //usage: "-m MTD_NUM [-d UBI_NUM] UBI_CTRL_DEV"
  79. //usage:#define ubiattach_full_usage "\n\n"
  80. //usage: "Attach MTD device to UBI\n"
  81. //usage: "\n -m MTD_NUM MTD device number to attach"
  82. //usage: "\n -d UBI_NUM UBI device number to assign"
  83. //usage:
  84. //usage:#define ubidetach_trivial_usage
  85. //usage: "-d UBI_NUM UBI_CTRL_DEV"
  86. //usage:#define ubidetach_full_usage "\n\n"
  87. //usage: "Detach MTD device from UBI\n"
  88. //usage: "\n -d UBI_NUM UBI device number"
  89. //usage:
  90. //usage:#define ubimkvol_trivial_usage
  91. //usage: "UBI_DEVICE -N NAME -s SIZE"
  92. //usage:#define ubimkvol_full_usage "\n\n"
  93. //usage: "Create UBI volume\n"
  94. //usage: "\n -a ALIGNMENT Volume alignment (default 1)"
  95. //usage: "\n -n VOLID Volume ID, if not specified, it"
  96. //usage: "\n will be assigned automatically"
  97. //usage: "\n -N NAME Volume name"
  98. //usage: "\n -s SIZE Size in bytes"
  99. //usage: "\n -t TYPE Volume type (static|dynamic)"
  100. //usage:
  101. //usage:#define ubirmvol_trivial_usage
  102. //usage: "UBI_DEVICE -n VOLID"
  103. //usage:#define ubirmvol_full_usage "\n\n"
  104. //usage: "Remove UBI volume\n"
  105. //usage: "\n -n VOLID Volume ID"
  106. //usage:
  107. //usage:#define ubirsvol_trivial_usage
  108. //usage: "UBI_DEVICE -n VOLID -s SIZE"
  109. //usage:#define ubirsvol_full_usage "\n\n"
  110. //usage: "Resize UBI volume\n"
  111. //usage: "\n -n VOLID Volume ID to resize"
  112. //usage: "\n -s SIZE Size in bytes"
  113. //usage:
  114. //usage:#define ubiupdatevol_trivial_usage
  115. //usage: "UBI_DEVICE [IMG_FILE]"
  116. //usage:#define ubiupdatevol_full_usage "\n\n"
  117. //usage: "Update UBI volume\n"
  118. //usage: "\n -t Truncate UBI volume"
  119. //usage: "\n -s SIZE Bytes in input (if reading stdin)"
  120. int ubi_tools_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  121. int ubi_tools_main(int argc UNUSED_PARAM, char **argv)
  122. {
  123. unsigned opts;
  124. char *ubi_ctrl;
  125. //struct stat st;
  126. int fd;
  127. int mtd_num;
  128. int dev_num = UBI_DEV_NUM_AUTO;
  129. int vol_id = UBI_VOL_NUM_AUTO;
  130. char *vol_name = NULL;
  131. int size_bytes;
  132. int alignment = 1;
  133. char *type = NULL;
  134. opt_complementary = "-1:m+:d+:n+:s+:a+";
  135. opts = getopt32(argv, "m:d:n:N:s:a:t::",
  136. &mtd_num, &dev_num, &vol_id,
  137. &vol_name, &size_bytes, &alignment, &type
  138. );
  139. ubi_ctrl = argv[optind];
  140. fd = xopen(ubi_ctrl, O_RDWR);
  141. //xfstat(fd, &st, ubi_ctrl);
  142. //if (!S_ISCHR(st.st_mode))
  143. // bb_error_msg_and_die("%s: not a char device", ubi_ctrl);
  144. if (do_attach) {
  145. struct ubi_attach_req req;
  146. if (!(opts & OPTION_M))
  147. bb_error_msg_and_die("%s device not specified", "MTD");
  148. memset(&req, 0, sizeof(req));
  149. req.mtd_num = mtd_num;
  150. req.ubi_num = dev_num;
  151. xioctl(fd, UBI_IOCATT, &req);
  152. } else
  153. if (do_detach) {
  154. if (!(opts & OPTION_D))
  155. bb_error_msg_and_die("%s device not specified", "UBI");
  156. xioctl(fd, UBI_IOCDET, &dev_num);
  157. } else
  158. if (do_mkvol) {
  159. struct ubi_mkvol_req req;
  160. int vol_name_len;
  161. if (!(opts & OPTION_s))
  162. bb_error_msg_and_die("%s size not specified", "UBI");
  163. if (!(opts & OPTION_N))
  164. bb_error_msg_and_die("%s name not specified", "UBI");
  165. vol_name_len = strlen(vol_name);
  166. if (vol_name_len > UBI_MAX_VOLUME_NAME)
  167. bb_error_msg_and_die("%s volume name too long", "UBI");
  168. memset(&req, 0, sizeof(req));
  169. req.vol_id = vol_id;
  170. if ((opts & OPTION_t) && type) {
  171. if (type[0] == 's')
  172. req.vol_type = UBI_STATIC_VOLUME;
  173. else
  174. req.vol_type = UBI_DYNAMIC_VOLUME;
  175. } else {
  176. req.vol_type = UBI_DYNAMIC_VOLUME;
  177. }
  178. req.alignment = alignment;
  179. req.bytes = size_bytes;
  180. strncpy(req.name, vol_name, UBI_MAX_VOLUME_NAME);
  181. req.name_len = vol_name_len;
  182. xioctl(fd, UBI_IOCMKVOL, &req);
  183. } else
  184. if (do_rmvol) {
  185. if (!(opts & OPTION_n))
  186. bb_error_msg_and_die("%s volume id not specified", "UBI");
  187. xioctl(fd, UBI_IOCRMVOL, &vol_id);
  188. } else
  189. if (do_rsvol) {
  190. struct ubi_rsvol_req req;
  191. if (!(opts & OPTION_s))
  192. bb_error_msg_and_die("%s size not specified", "UBI");
  193. if (!(opts & OPTION_n))
  194. bb_error_msg_and_die("%s volume id not specified", "UBI");
  195. memset(&req, 0, sizeof(req));
  196. req.bytes = size_bytes;
  197. req.vol_id = vol_id;
  198. xioctl(fd, UBI_IOCRSVOL, &req);
  199. } else
  200. if (do_update) {
  201. long long bytes;
  202. if (opts & OPTION_t) {
  203. // truncate the volume by starting an update for size 0
  204. bytes = 0;
  205. xioctl(fd, UBI_IOCVOLUP, &bytes);
  206. }
  207. else {
  208. struct stat st;
  209. char buf[sizeof("/sys/class/ubi/ubi%d_%d/usable_eb_size") + 2 * sizeof(int)*3];
  210. int input_fd;
  211. unsigned ubinum, volnum;
  212. unsigned leb_size;
  213. ssize_t len;
  214. char *input_data;
  215. // Make assumption that device not is in normal format.
  216. // Removes need for scanning sysfs tree as full libubi does
  217. if (sscanf(ubi_ctrl, "/dev/ubi%u_%u", &ubinum, &volnum) != 2)
  218. bb_error_msg_and_die("%s volume node not in correct format", "UBI");
  219. sprintf(buf, "/sys/class/ubi/ubi%u_%u/usable_eb_size", ubinum, volnum);
  220. if (open_read_close(buf, buf, sizeof(buf)) <= 0)
  221. bb_error_msg_and_die("%s could not get LEB size", "UBI");
  222. if (sscanf(buf, "%u", &leb_size) != 1)
  223. bb_error_msg_and_die("%s could not get LEB size", "UBI");
  224. if (opts & OPTION_s) {
  225. input_fd = 0;
  226. } else {
  227. if (!argv[optind+1])
  228. bb_show_usage();
  229. xstat(argv[optind+1], &st);
  230. size_bytes = st.st_size;
  231. input_fd = xopen(argv[optind+1], O_RDONLY);
  232. }
  233. bytes = size_bytes;
  234. xioctl(fd, UBI_IOCVOLUP, &bytes);
  235. input_data = xmalloc(leb_size);
  236. while ((len = full_read(input_fd, input_data, leb_size)) > 0) {
  237. xwrite(fd, input_data, len);
  238. }
  239. if (len < 0)
  240. bb_error_msg_and_die("%s volume update failed", "UBI");
  241. if (ENABLE_FEATURE_CLEAN_UP)
  242. close(input_fd);
  243. }
  244. }
  245. if (ENABLE_FEATURE_CLEAN_UP)
  246. close(fd);
  247. return EXIT_SUCCESS;
  248. }