ubiattach.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 attach MTD devices to UBI.
  19. *
  20. * Author: Artem Bityutskiy
  21. */
  22. #define PROGRAM_NAME "ubiattach"
  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. int vidoffs;
  36. const char *node;
  37. const char *dev;
  38. int max_beb_per1024;
  39. };
  40. static struct args args = {
  41. .devn = UBI_DEV_NUM_AUTO,
  42. .mtdn = -1,
  43. .vidoffs = 0,
  44. .node = NULL,
  45. .dev = NULL,
  46. .max_beb_per1024 = 0,
  47. };
  48. static const char doc[] = PROGRAM_NAME " version " VERSION
  49. " - a tool to attach MTD device to UBI.";
  50. static const char optionsstr[] =
  51. "-d, --devn=<number> the number to assign to the newly created UBI device\n"
  52. " (assigned automatically if this is not specified)\n"
  53. "-p, --dev-path=<path> path to MTD device node to attach\n"
  54. "-m, --mtdn=<number> MTD device number to attach (alternative method, e.g\n"
  55. " if the character device node does not exist)\n"
  56. "-O, --vid-hdr-offset VID header offset (do not specify this unless you really\n"
  57. " know what you are doing, the default should be optimal)\n"
  58. "-b, --max-beb-per1024 maximum expected bad block number per 1024 eraseblock.\n"
  59. " The default value is correct for most NAND devices.\n"
  60. " Allowed range is 0-768, 0 means the default kernel value.\n"
  61. "-h, --help print help message\n"
  62. "-V, --version print program version";
  63. static const char usage[] =
  64. "Usage: " PROGRAM_NAME " [<UBI control device node file name>]\n"
  65. "\t[-m <MTD device number>] [-d <UBI device number>] [-p <path to device>]\n"
  66. "\t[--mtdn=<MTD device number>] [--devn=<UBI device number>]\n"
  67. "\t[--dev-path=<path to device>]\n"
  68. "\t[--max-beb-per1024=<maximum bad block number per 1024 blocks>]\n"
  69. "UBI control device defaults to " DEFAULT_CTRL_DEV " if not supplied.\n"
  70. "Example 1: " PROGRAM_NAME " -p /dev/mtd0 - attach /dev/mtd0 to UBI\n"
  71. "Example 2: " PROGRAM_NAME " -m 0 - attach MTD device 0 (mtd0) to UBI\n"
  72. "Example 3: " PROGRAM_NAME " -m 0 -d 3 - attach MTD device 0 (mtd0) to UBI\n"
  73. " and create UBI device number 3 (ubi3)\n"
  74. "Example 4: " PROGRAM_NAME " -m 1 -b 25 - attach /dev/mtd1 to UBI and reserve\n"
  75. " 25*C/1024 eraseblocks for bad block handling, where C is the flash\n"
  76. " is total flash chip eraseblocks count, that is flash chip size in\n"
  77. " eraseblocks (including bad eraseblocks). E.g., if the flash chip\n"
  78. " has 4096 PEBs, 100 will be reserved.";
  79. static const struct option long_options[] = {
  80. { .name = "devn", .has_arg = 1, .flag = NULL, .val = 'd' },
  81. { .name = "dev-path", .has_arg = 1, .flag = NULL, .val = 'p' },
  82. { .name = "mtdn", .has_arg = 1, .flag = NULL, .val = 'm' },
  83. { .name = "vid-hdr-offset", .has_arg = 1, .flag = NULL, .val = 'O' },
  84. { .name = "max-beb-per1024", .has_arg = 1, .flag = NULL, .val = 'b' },
  85. { .name = "help", .has_arg = 0, .flag = NULL, .val = 'h' },
  86. { .name = "version", .has_arg = 0, .flag = NULL, .val = 'V' },
  87. { NULL, 0, NULL, 0},
  88. };
  89. static int parse_opt(int argc, char * const argv[])
  90. {
  91. while (1) {
  92. int key, error = 0;
  93. key = getopt_long(argc, argv, "p:m:d:O:b:hV", long_options, NULL);
  94. if (key == -1)
  95. break;
  96. switch (key) {
  97. case 'p':
  98. args.dev = optarg;
  99. break;
  100. case 'd':
  101. args.devn = simple_strtoul(optarg, &error);
  102. if (error || args.devn < 0)
  103. return errmsg("bad UBI device number: \"%s\"", optarg);
  104. break;
  105. case 'm':
  106. args.mtdn = simple_strtoul(optarg, &error);
  107. if (error || args.mtdn < 0)
  108. return errmsg("bad MTD device number: \"%s\"", optarg);
  109. break;
  110. case 'O':
  111. args.vidoffs = simple_strtoul(optarg, &error);
  112. if (error || args.vidoffs <= 0)
  113. return errmsg("bad VID header offset: \"%s\"", optarg);
  114. break;
  115. case 'b':
  116. args.max_beb_per1024 = simple_strtoul(optarg, &error);
  117. if (error || args.max_beb_per1024 < 0 ||
  118. args.max_beb_per1024 > 768)
  119. return errmsg("bad maximum of expected bad blocks (0-768): \"%s\"", optarg);
  120. if (args.max_beb_per1024 == 0)
  121. warnmsg("the default kernel value will be used for maximum expected bad blocks");
  122. break;
  123. case 'h':
  124. printf("%s\n\n", doc);
  125. printf("%s\n\n", usage);
  126. printf("%s\n", optionsstr);
  127. exit(EXIT_SUCCESS);
  128. case 'V':
  129. common_print_version();
  130. exit(EXIT_SUCCESS);
  131. case ':':
  132. return errmsg("parameter is missing");
  133. default:
  134. fprintf(stderr, "Use -h for help\n");
  135. return -1;
  136. }
  137. }
  138. if (optind == argc)
  139. args.node = DEFAULT_CTRL_DEV;
  140. else if (optind != argc - 1)
  141. return errmsg("more then one UBI control device specified (use -h for help)");
  142. else
  143. args.node = argv[optind];
  144. if (args.mtdn == -1 && args.dev == NULL)
  145. return errmsg("MTD device to attach was not specified (use -h for help)");
  146. return 0;
  147. }
  148. int main(int argc, char * const argv[])
  149. {
  150. int err;
  151. libubi_t libubi;
  152. struct ubi_info ubi_info;
  153. struct ubi_dev_info dev_info;
  154. struct ubi_attach_request req;
  155. err = parse_opt(argc, argv);
  156. if (err)
  157. return -1;
  158. libubi = libubi_open();
  159. if (!libubi) {
  160. if (errno == 0)
  161. return errmsg("UBI is not present in the system");
  162. return sys_errmsg("cannot open libubi");
  163. }
  164. /*
  165. * Make sure the kernel is fresh enough and this feature is supported.
  166. */
  167. err = ubi_get_info(libubi, &ubi_info);
  168. if (err) {
  169. sys_errmsg("cannot get UBI information");
  170. goto out_libubi;
  171. }
  172. if (ubi_info.ctrl_major == -1) {
  173. errmsg("MTD attach/detach feature is not supported by your kernel");
  174. goto out_libubi;
  175. }
  176. req.dev_num = args.devn;
  177. req.mtd_num = args.mtdn;
  178. req.vid_hdr_offset = args.vidoffs;
  179. req.mtd_dev_node = args.dev;
  180. req.max_beb_per1024 = args.max_beb_per1024;
  181. err = ubi_attach(libubi, args.node, &req);
  182. if (err < 0) {
  183. if (args.dev)
  184. sys_errmsg("cannot attach \"%s\"", args.dev);
  185. else
  186. sys_errmsg("cannot attach mtd%d", args.mtdn);
  187. goto out_libubi;
  188. } else if (err == 1) {
  189. /* The kernel did not support the 'max_beb_per1024' parameter */
  190. warnmsg("the --max-beb-per1024=%d parameter was ignored", args.max_beb_per1024);
  191. normsg("the UBI kernel driver does not support does not allow changing the reserved PEBs count");
  192. normsg("the support was added in kernel version 3.7, probably you are running older kernel?");
  193. goto out_libubi;
  194. }
  195. /* Print some information about the new UBI device */
  196. err = ubi_get_dev_info1(libubi, req.dev_num, &dev_info);
  197. if (err) {
  198. sys_errmsg("cannot get information about newly created UBI device");
  199. goto out_libubi;
  200. }
  201. printf("UBI device number %d, total %d LEBs (", dev_info.dev_num, dev_info.total_lebs);
  202. util_print_bytes(dev_info.total_bytes, 0);
  203. printf("), available %d LEBs (", dev_info.avail_lebs);
  204. util_print_bytes(dev_info.avail_bytes, 0);
  205. printf("), LEB size ");
  206. util_print_bytes(dev_info.leb_size, 1);
  207. printf("\n");
  208. libubi_close(libubi);
  209. return 0;
  210. out_libubi:
  211. libubi_close(libubi);
  212. return -1;
  213. }