3
0

ubi_tools.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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: "-N NAME [-s SIZE | -m] UBI_DEVICE"
  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. num = ubi_devnum_from_devname(ubi_ctrl);
  195. p = path_sys_class_ubi_ubi + sprintf(path_sys_class_ubi_ubi, "%u/", num);
  196. strcpy(p, "avail_eraseblocks");
  197. leb_avail = get_num_from_file(path, UINT_MAX, "Can't get available eraseblocks from '%s'");
  198. strcpy(p, "eraseblock_size");
  199. leb_size = get_num_from_file(path, MAX_SANE_ERASEBLOCK, "Can't get eraseblock size from '%s'");
  200. size_bytes = leb_avail * (unsigned long long)leb_size;
  201. //if (size_bytes <= 0)
  202. // bb_error_msg_and_die("%s invalid maximum size calculated", "UBI");
  203. } else
  204. if (!(opts & OPTION_s))
  205. bb_error_msg_and_die("size not specified");
  206. if (!(opts & OPTION_N))
  207. bb_error_msg_and_die("name not specified");
  208. mkvol_req.vol_id = vol_id;
  209. mkvol_req.vol_type = UBI_DYNAMIC_VOLUME;
  210. if ((opts & OPTION_t) && type[0] == 's')
  211. mkvol_req.vol_type = UBI_STATIC_VOLUME;
  212. mkvol_req.alignment = alignment;
  213. mkvol_req.bytes = size_bytes; /* signed int64_t */
  214. strncpy(mkvol_req.name, vol_name, UBI_MAX_VOLUME_NAME);
  215. mkvol_req.name_len = strlen(vol_name);
  216. if (mkvol_req.name_len > UBI_MAX_VOLUME_NAME)
  217. bb_error_msg_and_die("volume name too long: '%s'", vol_name);
  218. xioctl(fd, UBI_IOCMKVOL, &mkvol_req);
  219. } else
  220. //usage:#define ubirmvol_trivial_usage
  221. //usage: "-n VOLID / -N VOLNAME UBI_DEVICE"
  222. //usage:#define ubirmvol_full_usage "\n\n"
  223. //usage: "Remove UBI volume\n"
  224. //usage: "\n -n VOLID Volume ID"
  225. //usage: "\n -N VOLNAME Volume name"
  226. if (do_rmvol) {
  227. if (!(opts & (OPTION_n|OPTION_N)))
  228. bb_error_msg_and_die("volume id not specified");
  229. if (opts & OPTION_N) {
  230. unsigned num = ubi_devnum_from_devname(ubi_ctrl);
  231. vol_id = ubi_get_volid_by_name(num, vol_name);
  232. }
  233. if (sizeof(vol_id) != 4) {
  234. /* kernel expects int32_t* in this ioctl */
  235. int32_t t = vol_id;
  236. xioctl(fd, UBI_IOCRMVOL, &t);
  237. } else {
  238. xioctl(fd, UBI_IOCRMVOL, &vol_id);
  239. }
  240. } else
  241. //usage:#define ubirsvol_trivial_usage
  242. //usage: "-n VOLID -s SIZE UBI_DEVICE"
  243. //usage:#define ubirsvol_full_usage "\n\n"
  244. //usage: "Resize UBI volume\n"
  245. //usage: "\n -n VOLID Volume ID"
  246. //usage: "\n -s SIZE Size in bytes"
  247. if (do_rsvol) {
  248. if (!(opts & OPTION_s))
  249. bb_error_msg_and_die("size not specified");
  250. if (!(opts & OPTION_n))
  251. bb_error_msg_and_die("volume id not specified");
  252. rsvol_req.bytes = size_bytes; /* signed int64_t */
  253. rsvol_req.vol_id = vol_id;
  254. xioctl(fd, UBI_IOCRSVOL, &rsvol_req);
  255. } else
  256. //usage:#define ubiupdatevol_trivial_usage
  257. //usage: "[-t | [-s SIZE] IMG_FILE] UBI_DEVICE"
  258. //usage:#define ubiupdatevol_full_usage "\n\n"
  259. //usage: "Update UBI volume\n"
  260. //usage: "\n -t Truncate to zero size"
  261. //usage: "\n -s SIZE Size in bytes to resize to"
  262. if (do_update) {
  263. int64_t bytes64;
  264. if (opts & OPTION_t) {
  265. /* truncate the volume by starting an update for size 0 */
  266. bytes64 = 0;
  267. /* this ioctl expects int64_t* parameter */
  268. xioctl(fd, UBI_IOCVOLUP, &bytes64);
  269. }
  270. else {
  271. struct stat st;
  272. unsigned ubinum, volnum;
  273. unsigned leb_size;
  274. ssize_t len;
  275. char *input_data;
  276. /* Assume that device is in normal format. */
  277. /* Removes need for scanning sysfs tree as full libubi does. */
  278. if (sscanf(ubi_ctrl, "/dev/ubi%u_%u", &ubinum, &volnum) != 2)
  279. bb_error_msg_and_die("wrong format of UBI device name");
  280. sprintf(path_sys_class_ubi_ubi, "%u_%u/usable_eb_size", ubinum, volnum);
  281. leb_size = get_num_from_file(path, MAX_SANE_ERASEBLOCK, "Can't get usable eraseblock size from '%s'");
  282. if (!(opts & OPTION_s)) {
  283. if (!*argv)
  284. bb_show_usage();
  285. xmove_fd(xopen(*argv, O_RDONLY), STDIN_FILENO);
  286. xfstat(STDIN_FILENO, &st, *argv);
  287. size_bytes = st.st_size;
  288. }
  289. bytes64 = size_bytes;
  290. /* this ioctl expects signed int64_t* parameter */
  291. xioctl(fd, UBI_IOCVOLUP, &bytes64);
  292. input_data = xmalloc(leb_size);
  293. while ((len = full_read(STDIN_FILENO, input_data, leb_size)) > 0) {
  294. xwrite(fd, input_data, len);
  295. }
  296. if (len < 0)
  297. bb_perror_msg_and_die("UBI volume update failed");
  298. }
  299. }
  300. if (ENABLE_FEATURE_CLEAN_UP)
  301. close(fd);
  302. return EXIT_SUCCESS;
  303. }