3
0

ubi_tools.c 10 KB

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