ubiblock.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Copyright (c) Ezequiel Garcia, 2014
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License version 2 as published by
  6. * the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with
  14. * this program; if not, write to the Free Software Foundation, Inc., 51
  15. * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. */
  17. /*
  18. * An utility to create/remove block devices on top of UBI volumes.
  19. */
  20. #define PROGRAM_NAME "ubiblock"
  21. #include <fcntl.h>
  22. #include <stdio.h>
  23. #include <stdint.h>
  24. #include <getopt.h>
  25. #include <stdarg.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <unistd.h>
  29. #include <sys/stat.h>
  30. #include <libubi.h>
  31. #include "common.h"
  32. struct args {
  33. const char *node;
  34. int create;
  35. };
  36. static struct args args;
  37. static const char doc[] = PROGRAM_NAME " version " VERSION
  38. " - a tool to create/remove block device interface from UBI volumes.";
  39. static const char optionsstr[] =
  40. "-c, --create create block on top of a volume\n"
  41. "-r, --remove remove block from volume\n"
  42. "-h, --help print help message\n"
  43. "-V, --version print program version";
  44. static const char usage[] =
  45. "Usage: " PROGRAM_NAME " [-c,-r] <UBI volume node file name>\n"
  46. "Example: " PROGRAM_NAME " --create /dev/ubi0_0";
  47. static const struct option long_options[] = {
  48. { .name = "create", .has_arg = 1, .flag = NULL, .val = 'c' },
  49. { .name = "remove", .has_arg = 1, .flag = NULL, .val = 'r' },
  50. { .name = "help", .has_arg = 0, .flag = NULL, .val = 'h' },
  51. { .name = "version", .has_arg = 0, .flag = NULL, .val = 'V' },
  52. { NULL, 0, NULL, 0}
  53. };
  54. static int parse_opt(int argc, char * const argv[])
  55. {
  56. while (1) {
  57. int key;
  58. key = getopt_long(argc, argv, "c:r:h?V", long_options, NULL);
  59. if (key == -1)
  60. break;
  61. switch (key) {
  62. case 'c':
  63. args.create = 1;
  64. /* fall-through */
  65. case 'r':
  66. args.node = optarg;
  67. break;
  68. case 'h':
  69. printf("%s\n\n", doc);
  70. printf("%s\n\n", usage);
  71. printf("%s\n", optionsstr);
  72. exit(EXIT_SUCCESS);
  73. case '?':
  74. printf("%s\n\n", doc);
  75. printf("%s\n\n", usage);
  76. printf("%s\n", optionsstr);
  77. return -1;
  78. case 'V':
  79. common_print_version();
  80. exit(EXIT_SUCCESS);
  81. default:
  82. fprintf(stderr, "Use -h for help\n");
  83. return -1;
  84. }
  85. }
  86. if (!args.node)
  87. return errmsg("invalid arguments (use -h for help)");
  88. return 0;
  89. }
  90. int main(int argc, char * const argv[])
  91. {
  92. int err, fd;
  93. libubi_t libubi;
  94. err = parse_opt(argc, argv);
  95. if (err)
  96. return -1;
  97. libubi = libubi_open();
  98. if (!libubi) {
  99. if (errno == 0)
  100. errmsg("UBI is not present in the system");
  101. return sys_errmsg("cannot open libubi");
  102. }
  103. err = ubi_probe_node(libubi, args.node);
  104. if (err == 1) {
  105. errmsg("\"%s\" is an UBI device node, not an UBI volume node",
  106. args.node);
  107. goto out_libubi;
  108. } else if (err < 0) {
  109. if (errno == ENODEV)
  110. errmsg("\"%s\" is not an UBI volume node", args.node);
  111. else
  112. sys_errmsg("error while probing \"%s\"", args.node);
  113. goto out_libubi;
  114. }
  115. fd = open(args.node, O_RDWR);
  116. if (fd == -1) {
  117. sys_errmsg("cannot open UBI volume \"%s\"", args.node);
  118. goto out_libubi;
  119. }
  120. if (args.create) {
  121. err = ubi_vol_block_create(fd);
  122. if (err) {
  123. if (errno == ENOSYS)
  124. errmsg("UBI block is not present in the system");
  125. if (errno == ENOTTY)
  126. errmsg("UBI block not supported (check your kernel version)");
  127. sys_errmsg("cannot create block device");
  128. goto out_close;
  129. }
  130. } else {
  131. err = ubi_vol_block_remove(fd);
  132. if (err) {
  133. if (errno == ENOSYS)
  134. errmsg("UBI block is not present in the system");
  135. if (errno == ENOTTY)
  136. errmsg("UBI block not supported (check your kernel version)");
  137. sys_errmsg("cannot remove block device");
  138. goto out_close;
  139. }
  140. }
  141. close(fd);
  142. libubi_close(libubi);
  143. return 0;
  144. out_close:
  145. close(fd);
  146. out_libubi:
  147. libubi_close(libubi);
  148. return -1;
  149. }