ubidetach.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * Copyright (C) 2007 Nokia Corporation.
  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 delete UBI devices (detach MTD devices from UBI).
  19. *
  20. * Author: Artem Bityutskiy
  21. */
  22. #define PROGRAM_NAME "ubidetach"
  23. #include <stdio.h>
  24. #include <stdint.h>
  25. #include <getopt.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <libubi.h>
  29. #include "common.h"
  30. #define DEFAULT_CTRL_DEV "/dev/ubi_ctrl"
  31. /* The variables below are set by command line arguments */
  32. struct args {
  33. int devn;
  34. int mtdn;
  35. const char *node;
  36. const char *dev;
  37. };
  38. static struct args args = {
  39. .devn = UBI_DEV_NUM_AUTO,
  40. .mtdn = -1,
  41. .node = NULL,
  42. .dev = NULL,
  43. };
  44. static const char doc[] = PROGRAM_NAME " version " VERSION
  45. " - tool to remove UBI devices (detach MTD devices from UBI)";
  46. static const char optionsstr[] =
  47. "-d, --devn=<UBI device number> UBI device number to delete\n"
  48. "-p, --dev-path=<path to device> or alternatively, MTD device node path to detach\n"
  49. "-m, --mtdn=<MTD device number> or alternatively, MTD device number to detach\n"
  50. "-h, --help print help message\n"
  51. "-V, --version print program version";
  52. static const char usage[] =
  53. "Usage: " PROGRAM_NAME " [<UBI control device node file name>]\n"
  54. "\t[-d <UBI device number>] [-m <MTD device number>] [-p <path to device>]\n"
  55. "\t[--devn=<UBI device number>] [--mtdn=<MTD device number>]\n"
  56. "\t[--dev-path=<path to device>]\n"
  57. "UBI control device defaults to " DEFAULT_CTRL_DEV " if not supplied.\n"
  58. "Example 1: " PROGRAM_NAME " -p /dev/mtd0 - detach MTD device /dev/mtd0\n"
  59. "Example 2: " PROGRAM_NAME " -d 2 - delete UBI device 2 (ubi2)\n"
  60. "Example 3: " PROGRAM_NAME " -m 0 - detach MTD device 0 (mtd0)";
  61. static const struct option long_options[] = {
  62. { .name = "devn", .has_arg = 1, .flag = NULL, .val = 'd' },
  63. { .name = "dev-path", .has_arg = 1, .flag = NULL, .val = 'p' },
  64. { .name = "mtdn", .has_arg = 1, .flag = NULL, .val = 'm' },
  65. { .name = "help", .has_arg = 0, .flag = NULL, .val = 'h' },
  66. { .name = "version", .has_arg = 0, .flag = NULL, .val = 'V' },
  67. { NULL, 0, NULL, 0},
  68. };
  69. static int parse_opt(int argc, char * const argv[])
  70. {
  71. while (1) {
  72. int key, error = 0;
  73. key = getopt_long(argc, argv, "p:m:d:hV", long_options, NULL);
  74. if (key == -1)
  75. break;
  76. switch (key) {
  77. case 'p':
  78. args.dev = optarg;
  79. break;
  80. case 'd':
  81. args.devn = simple_strtoul(optarg, &error);
  82. if (error || args.devn < 0)
  83. return errmsg("bad UBI device number: \"%s\"", optarg);
  84. break;
  85. case 'm':
  86. args.mtdn = simple_strtoul(optarg, &error);
  87. if (error || args.mtdn < 0)
  88. return errmsg("bad MTD device number: \"%s\"", optarg);
  89. break;
  90. case 'h':
  91. printf("%s\n\n", doc);
  92. printf("%s\n\n", usage);
  93. printf("%s\n", optionsstr);
  94. exit(EXIT_SUCCESS);
  95. case 'V':
  96. common_print_version();
  97. exit(EXIT_SUCCESS);
  98. case ':':
  99. return errmsg("parameter is missing");
  100. default:
  101. fprintf(stderr, "Use -h for help\n");
  102. return -1;
  103. }
  104. }
  105. if (optind == argc)
  106. args.node = DEFAULT_CTRL_DEV;
  107. else if (optind != argc - 1)
  108. return errmsg("more then one UBI control device specified (use -h for help)");
  109. else
  110. args.node = argv[optind];
  111. if (args.mtdn == -1 && args.devn == -1 && args.dev == NULL)
  112. return errmsg("neither MTD nor UBI devices were specified (use -h for help)");
  113. if (args.devn != -1) {
  114. if (args.mtdn != -1 || args.dev != NULL)
  115. return errmsg("specify either MTD or UBI device (use -h for help)");
  116. } else if (args.mtdn != -1 && args.dev != NULL)
  117. return errmsg("specify either MTD number or device node (use -h for help)");
  118. return 0;
  119. }
  120. int main(int argc, char * const argv[])
  121. {
  122. int err;
  123. libubi_t libubi;
  124. struct ubi_info ubi_info;
  125. err = parse_opt(argc, argv);
  126. if (err)
  127. return -1;
  128. libubi = libubi_open();
  129. if (!libubi) {
  130. if (errno == 0)
  131. return errmsg("UBI is not present in the system");
  132. return sys_errmsg("cannot open libubi");
  133. }
  134. /*
  135. * Make sure the kernel is fresh enough and this feature is supported.
  136. */
  137. err = ubi_get_info(libubi, &ubi_info);
  138. if (err) {
  139. sys_errmsg("cannot get UBI information");
  140. goto out_libubi;
  141. }
  142. if (ubi_info.ctrl_major == -1) {
  143. errmsg("MTD detach/detach feature is not supported by your kernel");
  144. goto out_libubi;
  145. }
  146. if (args.devn != -1) {
  147. err = ubi_remove_dev(libubi, args.node, args.devn);
  148. if (err) {
  149. sys_errmsg("cannot remove ubi%d", args.devn);
  150. goto out_libubi;
  151. }
  152. } else {
  153. if (args.dev != NULL) {
  154. err = ubi_detach(libubi, args.node, args.dev);
  155. if (err) {
  156. sys_errmsg("cannot detach \"%s\"", args.dev);
  157. goto out_libubi;
  158. }
  159. } else {
  160. err = ubi_detach_mtd(libubi, args.node, args.mtdn);
  161. if (err) {
  162. sys_errmsg("cannot detach mtd%d", args.mtdn);
  163. goto out_libubi;
  164. }
  165. }
  166. }
  167. libubi_close(libubi);
  168. return 0;
  169. out_libubi:
  170. libubi_close(libubi);
  171. return -1;
  172. }