3
0

ubi_tools.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. if (do_mkvol) {
  117. opt_complementary = "-1:d+:n+:a+";
  118. opts = getopt32(argv, "md:n:N:s:a:t:",
  119. &dev_num, &vol_id,
  120. &vol_name, &size_bytes_str, &alignment, &type
  121. );
  122. } else {
  123. opt_complementary = "-1:m+:d+:n+:a+";
  124. opts = getopt32(argv, "m:d:n:N:s:a:t:",
  125. &mtd_num, &dev_num, &vol_id,
  126. &vol_name, &size_bytes_str, &alignment, &type
  127. );
  128. }
  129. #define OPTION_m (1 << 0)
  130. #define OPTION_d (1 << 1)
  131. #define OPTION_n (1 << 2)
  132. #define OPTION_N (1 << 3)
  133. #define OPTION_s (1 << 4)
  134. #define OPTION_a (1 << 5)
  135. #define OPTION_t (1 << 6)
  136. if (opts & OPTION_s)
  137. size_bytes = xatoull_sfx(size_bytes_str, size_suffixes);
  138. argv += optind;
  139. ubi_ctrl = *argv++;
  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. //usage:#define ubiattach_trivial_usage
  145. //usage: "-m MTD_NUM [-d UBI_NUM] UBI_CTRL_DEV"
  146. //usage:#define ubiattach_full_usage "\n\n"
  147. //usage: "Attach MTD device to UBI\n"
  148. //usage: "\n -m MTD_NUM MTD device number to attach"
  149. //usage: "\n -d UBI_NUM UBI device number to assign"
  150. if (do_attach) {
  151. if (!(opts & OPTION_m))
  152. bb_error_msg_and_die("%s device not specified", "MTD");
  153. attach_req.mtd_num = mtd_num;
  154. attach_req.ubi_num = dev_num;
  155. xioctl(fd, UBI_IOCATT, &attach_req);
  156. } else
  157. //usage:#define ubidetach_trivial_usage
  158. //usage: "-d UBI_NUM UBI_CTRL_DEV"
  159. //usage:#define ubidetach_full_usage "\n\n"
  160. //usage: "Detach MTD device from UBI\n"
  161. //usage: "\n -d UBI_NUM UBI device number"
  162. if (do_detach) {
  163. if (!(opts & OPTION_d))
  164. bb_error_msg_and_die("%s device not specified", "UBI");
  165. /* FIXME? kernel expects int32_t* here: */
  166. xioctl(fd, UBI_IOCDET, &dev_num);
  167. } else
  168. //usage:#define ubimkvol_trivial_usage
  169. //usage: "UBI_DEVICE -N NAME [-s SIZE | -m]"
  170. //usage:#define ubimkvol_full_usage "\n\n"
  171. //usage: "Create UBI volume\n"
  172. //usage: "\n -a ALIGNMENT Volume alignment (default 1)"
  173. //usage: "\n -m Set volume size to maximum available"
  174. //usage: "\n -n VOLID Volume ID. If not specified,"
  175. //usage: "\n assigned automatically"
  176. //usage: "\n -N NAME Volume name"
  177. //usage: "\n -s SIZE Size in bytes"
  178. //usage: "\n -t TYPE Volume type (static|dynamic)"
  179. if (do_mkvol) {
  180. if (opts & OPTION_m) {
  181. unsigned leb_avail;
  182. unsigned leb_size;
  183. unsigned num;
  184. char *p;
  185. if (sscanf(ubi_ctrl, "/dev/ubi%u", &num) != 1)
  186. bb_error_msg_and_die("wrong format of UBI device name");
  187. p = path_sys_class_ubi_ubi + sprintf(path_sys_class_ubi_ubi, "%u/", num);
  188. strcpy(p, "avail_eraseblocks");
  189. leb_avail = get_num_from_file(path, UINT_MAX, "Can't get available eraseblocks from '%s'");
  190. strcpy(p, "eraseblock_size");
  191. leb_size = get_num_from_file(path, MAX_SANE_ERASEBLOCK, "Can't get eraseblock size from '%s'");
  192. size_bytes = leb_avail * (unsigned long long)leb_size;
  193. //if (size_bytes <= 0)
  194. // bb_error_msg_and_die("%s invalid maximum size calculated", "UBI");
  195. } else
  196. if (!(opts & OPTION_s))
  197. bb_error_msg_and_die("size not specified");
  198. if (!(opts & OPTION_N))
  199. bb_error_msg_and_die("name not specified");
  200. mkvol_req.vol_id = vol_id;
  201. mkvol_req.vol_type = UBI_DYNAMIC_VOLUME;
  202. if ((opts & OPTION_t) && type[0] == 's')
  203. mkvol_req.vol_type = UBI_STATIC_VOLUME;
  204. mkvol_req.alignment = alignment;
  205. mkvol_req.bytes = size_bytes; /* signed int64_t */
  206. strncpy(mkvol_req.name, vol_name, UBI_MAX_VOLUME_NAME);
  207. mkvol_req.name_len = strlen(vol_name);
  208. if (mkvol_req.name_len > UBI_MAX_VOLUME_NAME)
  209. bb_error_msg_and_die("volume name too long: '%s'", vol_name);
  210. xioctl(fd, UBI_IOCMKVOL, &mkvol_req);
  211. } else
  212. //usage:#define ubirmvol_trivial_usage
  213. //usage: "UBI_DEVICE -n VOLID"
  214. //usage:#define ubirmvol_full_usage "\n\n"
  215. //usage: "Remove UBI volume\n"
  216. //usage: "\n -n VOLID Volume ID"
  217. if (do_rmvol) {
  218. if (!(opts & OPTION_n))
  219. bb_error_msg_and_die("volume id not specified");
  220. /* FIXME? kernel expects int32_t* here: */
  221. xioctl(fd, UBI_IOCRMVOL, &vol_id);
  222. } else
  223. //usage:#define ubirsvol_trivial_usage
  224. //usage: "UBI_DEVICE -n VOLID -s SIZE"
  225. //usage:#define ubirsvol_full_usage "\n\n"
  226. //usage: "Resize UBI volume\n"
  227. //usage: "\n -n VOLID Volume ID"
  228. //usage: "\n -s SIZE Size in bytes"
  229. if (do_rsvol) {
  230. if (!(opts & OPTION_s))
  231. bb_error_msg_and_die("size not specified");
  232. if (!(opts & OPTION_n))
  233. bb_error_msg_and_die("volume id not specified");
  234. rsvol_req.bytes = size_bytes; /* signed int64_t */
  235. rsvol_req.vol_id = vol_id;
  236. xioctl(fd, UBI_IOCRSVOL, &rsvol_req);
  237. } else
  238. //usage:#define ubiupdatevol_trivial_usage
  239. //usage: "UBI_DEVICE [-t | [-s SIZE] IMG_FILE]"
  240. //usage:#define ubiupdatevol_full_usage "\n\n"
  241. //usage: "Update UBI volume\n"
  242. //usage: "\n -t Truncate to zero size"
  243. //usage: "\n -s SIZE Size in bytes to resize to"
  244. if (do_update) {
  245. int64_t bytes64;
  246. if (opts & OPTION_t) {
  247. /* truncate the volume by starting an update for size 0 */
  248. bytes64 = 0;
  249. /* this ioctl expects int64_t* parameter */
  250. xioctl(fd, UBI_IOCVOLUP, &bytes64);
  251. }
  252. else {
  253. struct stat st;
  254. unsigned ubinum, volnum;
  255. unsigned leb_size;
  256. ssize_t len;
  257. char *input_data;
  258. /* Assume that device is in normal format. */
  259. /* Removes need for scanning sysfs tree as full libubi does. */
  260. if (sscanf(ubi_ctrl, "/dev/ubi%u_%u", &ubinum, &volnum) != 2)
  261. bb_error_msg_and_die("wrong format of UBI device name");
  262. sprintf(path_sys_class_ubi_ubi, "%u_%u/usable_eb_size", ubinum, volnum);
  263. leb_size = get_num_from_file(path, MAX_SANE_ERASEBLOCK, "Can't get usable eraseblock size from '%s'");
  264. if (!(opts & OPTION_s)) {
  265. if (!*argv)
  266. bb_show_usage();
  267. xstat(*argv, &st);
  268. size_bytes = st.st_size;
  269. xmove_fd(xopen(*argv, O_RDONLY), STDIN_FILENO);
  270. }
  271. bytes64 = size_bytes;
  272. /* this ioctl expects signed int64_t* parameter */
  273. xioctl(fd, UBI_IOCVOLUP, &bytes64);
  274. input_data = xmalloc(leb_size);
  275. while ((len = full_read(STDIN_FILENO, input_data, leb_size)) > 0) {
  276. xwrite(fd, input_data, len);
  277. }
  278. if (len < 0)
  279. bb_perror_msg_and_die("UBI volume update failed");
  280. }
  281. }
  282. if (ENABLE_FEATURE_CLEAN_UP)
  283. close(fd);
  284. return EXIT_SUCCESS;
  285. }