ubi_tools.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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 do_attach (ENABLE_UBIATTACH && applet_name[3] == 'a')
  65. #define do_detach (ENABLE_UBIDETACH && applet_name[3] == 'd')
  66. #define do_mkvol (ENABLE_UBIMKVOL && applet_name[3] == 'm')
  67. #define do_rmvol (ENABLE_UBIRMVOL && applet_name[4] == 'm')
  68. #define do_rsvol (ENABLE_UBIRSVOL && applet_name[4] == 's')
  69. #define do_update (ENABLE_UBIUPDATEVOL && applet_name[3] == 'u')
  70. static unsigned get_num_from_file(const char *path, unsigned max, const char *errmsg)
  71. {
  72. char buf[sizeof(long long)*3];
  73. unsigned long long num;
  74. if (open_read_close(path, buf, sizeof(buf)) < 0)
  75. bb_perror_msg_and_die(errmsg, path);
  76. /* It can be \n terminated, xatoull won't work well */
  77. if (sscanf(buf, "%llu", &num) != 1 || num > max)
  78. bb_error_msg_and_die(errmsg, path);
  79. return num;
  80. }
  81. /* To prevent malloc(1G) accidents */
  82. #define MAX_SANE_ERASEBLOCK (16*1024*1024)
  83. int ubi_tools_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  84. int ubi_tools_main(int argc UNUSED_PARAM, char **argv)
  85. {
  86. static const struct suffix_mult size_suffixes[] = {
  87. { "KiB", 1024 },
  88. { "MiB", 1024*1024 },
  89. { "GiB", 1024*1024*1024 },
  90. { "", 0 }
  91. };
  92. unsigned opts;
  93. char *ubi_ctrl;
  94. int fd;
  95. int mtd_num;
  96. int dev_num = UBI_DEV_NUM_AUTO;
  97. int vol_id = UBI_VOL_NUM_AUTO;
  98. char *vol_name;
  99. unsigned long long size_bytes = size_bytes; /* for compiler */
  100. char *size_bytes_str;
  101. int alignment = 1;
  102. char *type;
  103. union {
  104. struct ubi_attach_req attach_req;
  105. struct ubi_mkvol_req mkvol_req;
  106. struct ubi_rsvol_req rsvol_req;
  107. } req_structs;
  108. #define attach_req req_structs.attach_req
  109. #define mkvol_req req_structs.mkvol_req
  110. #define rsvol_req req_structs.rsvol_req
  111. char path[sizeof("/sys/class/ubi/ubi%d_%d/usable_eb_size")
  112. + 2 * sizeof(int)*3 + /*just in case:*/ 16];
  113. #define path_sys_class_ubi_ubi (path + sizeof("/sys/class/ubi/ubi")-1)
  114. strcpy(path, "/sys/class/ubi/ubi");
  115. memset(&req_structs, 0, sizeof(req_structs));
  116. #define OPTION_m (1 << 0)
  117. #define OPTION_d (1 << 1)
  118. #define OPTION_n (1 << 2)
  119. #define OPTION_N (1 << 3)
  120. #define OPTION_s (1 << 4)
  121. #define OPTION_a (1 << 5)
  122. #define OPTION_t (1 << 6)
  123. if (do_mkvol) {
  124. opt_complementary = "-1:d+:n+:a+";
  125. opts = getopt32(argv, "md:n:N:s:a:t:",
  126. &dev_num, &vol_id,
  127. &vol_name, &size_bytes_str, &alignment, &type
  128. );
  129. } else
  130. if (do_update) {
  131. opt_complementary = "-1";
  132. opts = getopt32(argv, "s:at", &size_bytes_str);
  133. opts *= OPTION_s;
  134. } else {
  135. opt_complementary = "-1:m+:d+:n+:a+";
  136. opts = getopt32(argv, "m:d:n:N:s:a:t:",
  137. &mtd_num, &dev_num, &vol_id,
  138. &vol_name, &size_bytes_str, &alignment, &type
  139. );
  140. }
  141. if (opts & OPTION_s)
  142. size_bytes = xatoull_sfx(size_bytes_str, size_suffixes);
  143. argv += optind;
  144. ubi_ctrl = *argv++;
  145. fd = xopen(ubi_ctrl, O_RDWR);
  146. //xfstat(fd, &st, ubi_ctrl);
  147. //if (!S_ISCHR(st.st_mode))
  148. // bb_error_msg_and_die("%s: not a char device", ubi_ctrl);
  149. //usage:#define ubiattach_trivial_usage
  150. //usage: "-m MTD_NUM [-d UBI_NUM] UBI_CTRL_DEV"
  151. //usage:#define ubiattach_full_usage "\n\n"
  152. //usage: "Attach MTD device to UBI\n"
  153. //usage: "\n -m MTD_NUM MTD device number to attach"
  154. //usage: "\n -d UBI_NUM UBI device number to assign"
  155. if (do_attach) {
  156. if (!(opts & OPTION_m))
  157. bb_error_msg_and_die("%s device not specified", "MTD");
  158. attach_req.mtd_num = mtd_num;
  159. attach_req.ubi_num = dev_num;
  160. xioctl(fd, UBI_IOCATT, &attach_req);
  161. } else
  162. //usage:#define ubidetach_trivial_usage
  163. //usage: "-d UBI_NUM UBI_CTRL_DEV"
  164. //usage:#define ubidetach_full_usage "\n\n"
  165. //usage: "Detach MTD device from UBI\n"
  166. //usage: "\n -d UBI_NUM UBI device number"
  167. if (do_detach) {
  168. if (!(opts & OPTION_d))
  169. bb_error_msg_and_die("%s device not specified", "UBI");
  170. /* FIXME? kernel expects int32_t* here: */
  171. xioctl(fd, UBI_IOCDET, &dev_num);
  172. } else
  173. //usage:#define ubimkvol_trivial_usage
  174. //usage: "UBI_DEVICE -N NAME [-s SIZE | -m]"
  175. //usage:#define ubimkvol_full_usage "\n\n"
  176. //usage: "Create UBI volume\n"
  177. //usage: "\n -a ALIGNMENT Volume alignment (default 1)"
  178. //usage: "\n -m Set volume size to maximum available"
  179. //usage: "\n -n VOLID Volume ID. If not specified,"
  180. //usage: "\n assigned automatically"
  181. //usage: "\n -N NAME Volume name"
  182. //usage: "\n -s SIZE Size in bytes"
  183. //usage: "\n -t TYPE Volume type (static|dynamic)"
  184. if (do_mkvol) {
  185. if (opts & OPTION_m) {
  186. unsigned leb_avail;
  187. unsigned leb_size;
  188. unsigned num;
  189. char *p;
  190. if (sscanf(ubi_ctrl, "/dev/ubi%u", &num) != 1)
  191. bb_error_msg_and_die("wrong format of UBI device name");
  192. p = path_sys_class_ubi_ubi + sprintf(path_sys_class_ubi_ubi, "%u/", num);
  193. strcpy(p, "avail_eraseblocks");
  194. leb_avail = get_num_from_file(path, UINT_MAX, "Can't get available eraseblocks from '%s'");
  195. strcpy(p, "eraseblock_size");
  196. leb_size = get_num_from_file(path, MAX_SANE_ERASEBLOCK, "Can't get eraseblock size from '%s'");
  197. size_bytes = leb_avail * (unsigned long long)leb_size;
  198. //if (size_bytes <= 0)
  199. // bb_error_msg_and_die("%s invalid maximum size calculated", "UBI");
  200. } else
  201. if (!(opts & OPTION_s))
  202. bb_error_msg_and_die("size not specified");
  203. if (!(opts & OPTION_N))
  204. bb_error_msg_and_die("name not specified");
  205. mkvol_req.vol_id = vol_id;
  206. mkvol_req.vol_type = UBI_DYNAMIC_VOLUME;
  207. if ((opts & OPTION_t) && type[0] == 's')
  208. mkvol_req.vol_type = UBI_STATIC_VOLUME;
  209. mkvol_req.alignment = alignment;
  210. mkvol_req.bytes = size_bytes; /* signed int64_t */
  211. strncpy(mkvol_req.name, vol_name, UBI_MAX_VOLUME_NAME);
  212. mkvol_req.name_len = strlen(vol_name);
  213. if (mkvol_req.name_len > UBI_MAX_VOLUME_NAME)
  214. bb_error_msg_and_die("volume name too long: '%s'", vol_name);
  215. xioctl(fd, UBI_IOCMKVOL, &mkvol_req);
  216. } else
  217. //usage:#define ubirmvol_trivial_usage
  218. //usage: "UBI_DEVICE -n VOLID"
  219. //usage:#define ubirmvol_full_usage "\n\n"
  220. //usage: "Remove UBI volume\n"
  221. //usage: "\n -n VOLID Volume ID"
  222. if (do_rmvol) {
  223. if (!(opts & OPTION_n))
  224. bb_error_msg_and_die("volume id not specified");
  225. /* FIXME? kernel expects int32_t* here: */
  226. xioctl(fd, UBI_IOCRMVOL, &vol_id);
  227. } else
  228. //usage:#define ubirsvol_trivial_usage
  229. //usage: "UBI_DEVICE -n VOLID -s SIZE"
  230. //usage:#define ubirsvol_full_usage "\n\n"
  231. //usage: "Resize UBI volume\n"
  232. //usage: "\n -n VOLID Volume ID"
  233. //usage: "\n -s SIZE Size in bytes"
  234. if (do_rsvol) {
  235. if (!(opts & OPTION_s))
  236. bb_error_msg_and_die("size not specified");
  237. if (!(opts & OPTION_n))
  238. bb_error_msg_and_die("volume id not specified");
  239. rsvol_req.bytes = size_bytes; /* signed int64_t */
  240. rsvol_req.vol_id = vol_id;
  241. xioctl(fd, UBI_IOCRSVOL, &rsvol_req);
  242. } else
  243. //usage:#define ubiupdatevol_trivial_usage
  244. //usage: "UBI_DEVICE [-t | [-s SIZE] IMG_FILE]"
  245. //usage:#define ubiupdatevol_full_usage "\n\n"
  246. //usage: "Update UBI volume\n"
  247. //usage: "\n -t Truncate to zero size"
  248. //usage: "\n -s SIZE Size in bytes to resize to"
  249. if (do_update) {
  250. int64_t bytes64;
  251. if (opts & OPTION_t) {
  252. /* truncate the volume by starting an update for size 0 */
  253. bytes64 = 0;
  254. /* this ioctl expects int64_t* parameter */
  255. xioctl(fd, UBI_IOCVOLUP, &bytes64);
  256. }
  257. else {
  258. struct stat st;
  259. unsigned ubinum, volnum;
  260. unsigned leb_size;
  261. ssize_t len;
  262. char *input_data;
  263. /* Assume that device is in normal format. */
  264. /* Removes need for scanning sysfs tree as full libubi does. */
  265. if (sscanf(ubi_ctrl, "/dev/ubi%u_%u", &ubinum, &volnum) != 2)
  266. bb_error_msg_and_die("wrong format of UBI device name");
  267. sprintf(path_sys_class_ubi_ubi, "%u_%u/usable_eb_size", ubinum, volnum);
  268. leb_size = get_num_from_file(path, MAX_SANE_ERASEBLOCK, "Can't get usable eraseblock size from '%s'");
  269. if (!(opts & OPTION_s)) {
  270. if (!*argv)
  271. bb_show_usage();
  272. xmove_fd(xopen(*argv, O_RDONLY), STDIN_FILENO);
  273. xfstat(STDIN_FILENO, &st, *argv);
  274. size_bytes = st.st_size;
  275. }
  276. bytes64 = size_bytes;
  277. /* this ioctl expects signed int64_t* parameter */
  278. xioctl(fd, UBI_IOCVOLUP, &bytes64);
  279. input_data = xmalloc(leb_size);
  280. while ((len = full_read(STDIN_FILENO, input_data, leb_size)) > 0) {
  281. xwrite(fd, input_data, len);
  282. }
  283. if (len < 0)
  284. bb_perror_msg_and_die("UBI volume update failed");
  285. }
  286. }
  287. if (ENABLE_FEATURE_CLEAN_UP)
  288. close(fd);
  289. return EXIT_SUCCESS;
  290. }