3
0

ubi_tools.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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_ODDNAME:name main location suid_type help
  47. //applet:IF_UBIATTACH( APPLET_ODDNAME(ubiattach, ubi_tools, BB_DIR_USR_SBIN, BB_SUID_DROP, ubiattach))
  48. //applet:IF_UBIDETACH( APPLET_ODDNAME(ubidetach, ubi_tools, BB_DIR_USR_SBIN, BB_SUID_DROP, ubidetach))
  49. //applet:IF_UBIMKVOL( APPLET_ODDNAME(ubimkvol, ubi_tools, BB_DIR_USR_SBIN, BB_SUID_DROP, ubimkvol))
  50. //applet:IF_UBIRMVOL( APPLET_ODDNAME(ubirmvol, ubi_tools, BB_DIR_USR_SBIN, BB_SUID_DROP, ubirmvol))
  51. //applet:IF_UBIRSVOL( APPLET_ODDNAME(ubirsvol, ubi_tools, BB_DIR_USR_SBIN, BB_SUID_DROP, ubirsvol))
  52. //applet:IF_UBIUPDATEVOL(APPLET_ODDNAME(ubiupdatevol, ubi_tools, BB_DIR_USR_SBIN, BB_SUID_DROP, ubiupdatevol))
  53. //kbuild:lib-$(CONFIG_UBIATTACH) += ubi_tools.o
  54. //kbuild:lib-$(CONFIG_UBIDETACH) += ubi_tools.o
  55. //kbuild:lib-$(CONFIG_UBIMKVOL) += ubi_tools.o
  56. //kbuild:lib-$(CONFIG_UBIRMVOL) += ubi_tools.o
  57. //kbuild:lib-$(CONFIG_UBIRSVOL) += ubi_tools.o
  58. //kbuild:lib-$(CONFIG_UBIUPDATEVOL) += ubi_tools.o
  59. #include "libbb.h"
  60. /* Some versions of kernel have broken headers, need this hack */
  61. #ifndef __packed
  62. # define __packed __attribute__((packed))
  63. #endif
  64. #include <mtd/ubi-user.h>
  65. #define do_attach (ENABLE_UBIATTACH && applet_name[3] == 'a')
  66. #define do_detach (ENABLE_UBIDETACH && applet_name[3] == 'd')
  67. #define do_mkvol (ENABLE_UBIMKVOL && applet_name[3] == 'm')
  68. #define do_rmvol (ENABLE_UBIRMVOL && applet_name[4] == 'm')
  69. #define do_rsvol (ENABLE_UBIRSVOL && applet_name[4] == 's')
  70. #define do_update (ENABLE_UBIUPDATEVOL && applet_name[3] == 'u')
  71. static unsigned get_num_from_file(const char *path, unsigned max, const char *errmsg)
  72. {
  73. char buf[sizeof(long long)*3];
  74. unsigned long long num;
  75. if (open_read_close(path, buf, sizeof(buf)) < 0)
  76. bb_perror_msg_and_die(errmsg, path);
  77. /* It can be \n terminated, xatoull won't work well */
  78. if (sscanf(buf, "%llu", &num) != 1 || num > max)
  79. bb_error_msg_and_die(errmsg, path);
  80. return num;
  81. }
  82. /* To prevent malloc(1G) accidents */
  83. #define MAX_SANE_ERASEBLOCK (16*1024*1024)
  84. int ubi_tools_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  85. int ubi_tools_main(int argc UNUSED_PARAM, char **argv)
  86. {
  87. static const struct suffix_mult size_suffixes[] = {
  88. { "KiB", 1024 },
  89. { "MiB", 1024*1024 },
  90. { "GiB", 1024*1024*1024 },
  91. { "", 0 }
  92. };
  93. unsigned opts;
  94. char *ubi_ctrl;
  95. int fd;
  96. int mtd_num;
  97. int dev_num = UBI_DEV_NUM_AUTO;
  98. int vol_id = UBI_VOL_NUM_AUTO;
  99. int vid_hdr_offset = 0;
  100. char *vol_name;
  101. unsigned long long size_bytes = size_bytes; /* for compiler */
  102. char *size_bytes_str;
  103. int alignment = 1;
  104. char *type;
  105. union {
  106. struct ubi_attach_req attach_req;
  107. struct ubi_mkvol_req mkvol_req;
  108. struct ubi_rsvol_req rsvol_req;
  109. } req_structs;
  110. #define attach_req req_structs.attach_req
  111. #define mkvol_req req_structs.mkvol_req
  112. #define rsvol_req req_structs.rsvol_req
  113. char path[sizeof("/sys/class/ubi/ubi%d_%d/usable_eb_size")
  114. + 2 * sizeof(int)*3 + /*just in case:*/ 16];
  115. #define path_sys_class_ubi_ubi (path + sizeof("/sys/class/ubi/ubi")-1)
  116. strcpy(path, "/sys/class/ubi/ubi");
  117. memset(&req_structs, 0, sizeof(req_structs));
  118. #define OPTION_m (1 << 0)
  119. #define OPTION_d (1 << 1)
  120. #define OPTION_n (1 << 2)
  121. #define OPTION_N (1 << 3)
  122. #define OPTION_s (1 << 4)
  123. #define OPTION_a (1 << 5)
  124. #define OPTION_t (1 << 6)
  125. if (do_mkvol) {
  126. opt_complementary = "-1";
  127. opts = getopt32(argv, "md:+n:+N:s:a:+t:O:+",
  128. &dev_num, &vol_id,
  129. &vol_name, &size_bytes_str, &alignment, &type,
  130. &vid_hdr_offset
  131. );
  132. } else
  133. if (do_update) {
  134. opt_complementary = "-1";
  135. opts = getopt32(argv, "s:at", &size_bytes_str);
  136. opts *= OPTION_s;
  137. } else {
  138. opt_complementary = "-1";
  139. opts = getopt32(argv, "m:+d:+n:+N:s:a:+t:",
  140. &mtd_num, &dev_num, &vol_id,
  141. &vol_name, &size_bytes_str, &alignment, &type
  142. );
  143. }
  144. if (opts & OPTION_s)
  145. size_bytes = xatoull_sfx(size_bytes_str, size_suffixes);
  146. argv += optind;
  147. ubi_ctrl = *argv++;
  148. fd = xopen(ubi_ctrl, O_RDWR);
  149. //xfstat(fd, &st, ubi_ctrl);
  150. //if (!S_ISCHR(st.st_mode))
  151. // bb_error_msg_and_die("%s: not a char device", ubi_ctrl);
  152. //usage:#define ubiattach_trivial_usage
  153. //usage: "-m MTD_NUM [-d UBI_NUM] [-O VID_HDR_OFF] UBI_CTRL_DEV"
  154. //usage:#define ubiattach_full_usage "\n\n"
  155. //usage: "Attach MTD device to UBI\n"
  156. //usage: "\n -m MTD_NUM MTD device number to attach"
  157. //usage: "\n -d UBI_NUM UBI device number to assign"
  158. //usage: "\n -O VID_HDR_OFF VID header offset"
  159. if (do_attach) {
  160. if (!(opts & OPTION_m))
  161. bb_error_msg_and_die("%s device not specified", "MTD");
  162. attach_req.mtd_num = mtd_num;
  163. attach_req.ubi_num = dev_num;
  164. attach_req.vid_hdr_offset = vid_hdr_offset;
  165. xioctl(fd, UBI_IOCATT, &attach_req);
  166. } else
  167. //usage:#define ubidetach_trivial_usage
  168. //usage: "-d UBI_NUM UBI_CTRL_DEV"
  169. //usage:#define ubidetach_full_usage "\n\n"
  170. //usage: "Detach MTD device from UBI\n"
  171. //usage: "\n -d UBI_NUM UBI device number"
  172. if (do_detach) {
  173. if (!(opts & OPTION_d))
  174. bb_error_msg_and_die("%s device not specified", "UBI");
  175. /* FIXME? kernel expects int32_t* here: */
  176. xioctl(fd, UBI_IOCDET, &dev_num);
  177. } else
  178. //usage:#define ubimkvol_trivial_usage
  179. //usage: "-N NAME [-s SIZE | -m] UBI_DEVICE"
  180. //usage:#define ubimkvol_full_usage "\n\n"
  181. //usage: "Create UBI volume\n"
  182. //usage: "\n -a ALIGNMENT Volume alignment (default 1)"
  183. //usage: "\n -m Set volume size to maximum available"
  184. //usage: "\n -n VOLID Volume ID. If not specified,"
  185. //usage: "\n assigned automatically"
  186. //usage: "\n -N NAME Volume name"
  187. //usage: "\n -s SIZE Size in bytes"
  188. //usage: "\n -t TYPE Volume type (static|dynamic)"
  189. if (do_mkvol) {
  190. if (opts & OPTION_m) {
  191. unsigned leb_avail;
  192. unsigned leb_size;
  193. unsigned num;
  194. char *p;
  195. num = ubi_devnum_from_devname(ubi_ctrl);
  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: "-n VOLID / -N VOLNAME UBI_DEVICE"
  223. //usage:#define ubirmvol_full_usage "\n\n"
  224. //usage: "Remove UBI volume\n"
  225. //usage: "\n -n VOLID Volume ID"
  226. //usage: "\n -N VOLNAME Volume name"
  227. if (do_rmvol) {
  228. if (!(opts & (OPTION_n|OPTION_N)))
  229. bb_error_msg_and_die("volume id not specified");
  230. if (opts & OPTION_N) {
  231. unsigned num = ubi_devnum_from_devname(ubi_ctrl);
  232. vol_id = ubi_get_volid_by_name(num, vol_name);
  233. }
  234. if (sizeof(vol_id) != 4) {
  235. /* kernel expects int32_t* in this ioctl */
  236. int32_t t = vol_id;
  237. xioctl(fd, UBI_IOCRMVOL, &t);
  238. } else {
  239. xioctl(fd, UBI_IOCRMVOL, &vol_id);
  240. }
  241. } else
  242. //usage:#define ubirsvol_trivial_usage
  243. //usage: "-n VOLID -s SIZE UBI_DEVICE"
  244. //usage:#define ubirsvol_full_usage "\n\n"
  245. //usage: "Resize UBI volume\n"
  246. //usage: "\n -n VOLID Volume ID"
  247. //usage: "\n -s SIZE Size in bytes"
  248. if (do_rsvol) {
  249. if (!(opts & OPTION_s))
  250. bb_error_msg_and_die("size not specified");
  251. if (!(opts & OPTION_n))
  252. bb_error_msg_and_die("volume id not specified");
  253. rsvol_req.bytes = size_bytes; /* signed int64_t */
  254. rsvol_req.vol_id = vol_id;
  255. xioctl(fd, UBI_IOCRSVOL, &rsvol_req);
  256. } else
  257. //usage:#define ubiupdatevol_trivial_usage
  258. //usage: "[-t | [-s SIZE] IMG_FILE] UBI_DEVICE"
  259. //usage:#define ubiupdatevol_full_usage "\n\n"
  260. //usage: "Update UBI volume\n"
  261. //usage: "\n -t Truncate to zero size"
  262. //usage: "\n -s SIZE Size in bytes to resize to"
  263. if (do_update) {
  264. int64_t bytes64;
  265. if (opts & OPTION_t) {
  266. /* truncate the volume by starting an update for size 0 */
  267. bytes64 = 0;
  268. /* this ioctl expects int64_t* parameter */
  269. xioctl(fd, UBI_IOCVOLUP, &bytes64);
  270. }
  271. else {
  272. struct stat st;
  273. unsigned ubinum, volnum;
  274. unsigned leb_size;
  275. ssize_t len;
  276. char *input_data;
  277. /* Assume that device is in normal format. */
  278. /* Removes need for scanning sysfs tree as full libubi does. */
  279. if (sscanf(ubi_ctrl, "/dev/ubi%u_%u", &ubinum, &volnum) != 2)
  280. bb_error_msg_and_die("wrong format of UBI device name");
  281. sprintf(path_sys_class_ubi_ubi, "%u_%u/usable_eb_size", ubinum, volnum);
  282. leb_size = get_num_from_file(path, MAX_SANE_ERASEBLOCK, "Can't get usable eraseblock size from '%s'");
  283. if (!(opts & OPTION_s)) {
  284. if (!*argv)
  285. bb_show_usage();
  286. xmove_fd(xopen(*argv, O_RDONLY), STDIN_FILENO);
  287. xfstat(STDIN_FILENO, &st, *argv);
  288. size_bytes = st.st_size;
  289. }
  290. bytes64 = size_bytes;
  291. /* this ioctl expects signed int64_t* parameter */
  292. xioctl(fd, UBI_IOCVOLUP, &bytes64);
  293. input_data = xmalloc(leb_size);
  294. while ((len = full_read(STDIN_FILENO, input_data, leb_size)) > 0) {
  295. xwrite(fd, input_data, len);
  296. }
  297. if (len < 0)
  298. bb_perror_msg_and_die("UBI volume update failed");
  299. }
  300. }
  301. if (ENABLE_FEATURE_CLEAN_UP)
  302. close(fd);
  303. return EXIT_SUCCESS;
  304. }