ubinfo.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /*
  2. * Copyright (C) 2007, 2008 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 get UBI information.
  19. *
  20. * Author: Artem Bityutskiy
  21. */
  22. #define PROGRAM_NAME "ubinfo"
  23. #include <stdint.h>
  24. #include <stdio.h>
  25. #include <getopt.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <libubi.h>
  29. #include "common.h"
  30. /* The variables below are set by command line arguments */
  31. struct args {
  32. int devn;
  33. int vol_id;
  34. int all;
  35. const char *node;
  36. const char *vol_name;
  37. };
  38. static struct args args = {
  39. .vol_id = -1,
  40. .devn = -1,
  41. .all = 0,
  42. .node = NULL,
  43. .vol_name = NULL,
  44. };
  45. static const char doc[] = PROGRAM_NAME " version " VERSION
  46. " - a tool to print UBI information.";
  47. static const char optionsstr[] =
  48. "-d, --devn=<UBI device number> UBI device number to get information about\n"
  49. "-n, --vol_id=<volume ID> ID of UBI volume to print information about\n"
  50. "-N, --name=<volume name> name of UBI volume to print information about\n"
  51. "-a, --all print information about all devices and volumes,\n"
  52. " or about all volumes if the UBI device was\n"
  53. " specified\n"
  54. "-h, --help print help message\n"
  55. "-V, --version print program version";
  56. static const char usage[] =
  57. "Usage 1: " PROGRAM_NAME " [-d <UBI device number>] [-n <volume ID> | -N <volume name>] [-a] [-h] [-V]\n"
  58. "\t\t[--vol_id=<volume ID> | --name <volume name>] [--devn <UBI device number>] [--all] [--help] [--version]\n"
  59. "Usage 2: " PROGRAM_NAME " <UBI device node file name> [-a] [-h] [-V] [--all] [--help] [--version]\n"
  60. "Usage 3: " PROGRAM_NAME " <UBI volume node file name> [-h] [-V] [--help] [--version]\n\n"
  61. "Example 1: " PROGRAM_NAME " - (no arguments) print general UBI information\n"
  62. "Example 2: " PROGRAM_NAME " -d 1 - print information about UBI device number 1\n"
  63. "Example 3: " PROGRAM_NAME " /dev/ubi0 -a - print information about all volumes of UBI\n"
  64. " device /dev/ubi0\n"
  65. "Example 4: " PROGRAM_NAME " /dev/ubi1_0 - print information about UBI volume /dev/ubi1_0\n"
  66. "Example 5: " PROGRAM_NAME " -a - print all information\n";
  67. static const struct option long_options[] = {
  68. { .name = "devn", .has_arg = 1, .flag = NULL, .val = 'd' },
  69. { .name = "vol_id", .has_arg = 1, .flag = NULL, .val = 'n' },
  70. { .name = "name", .has_arg = 1, .flag = NULL, .val = 'N' },
  71. { .name = "all", .has_arg = 0, .flag = NULL, .val = 'a' },
  72. { .name = "help", .has_arg = 0, .flag = NULL, .val = 'h' },
  73. { .name = "version", .has_arg = 0, .flag = NULL, .val = 'V' },
  74. { NULL, 0, NULL, 0},
  75. };
  76. static int parse_opt(int argc, char * const argv[])
  77. {
  78. while (1) {
  79. int key, error = 0;
  80. key = getopt_long(argc, argv, "an:N:d:hV", long_options, NULL);
  81. if (key == -1)
  82. break;
  83. switch (key) {
  84. case 'a':
  85. args.all = 1;
  86. break;
  87. case 'n':
  88. args.vol_id = simple_strtoul(optarg, &error);
  89. if (error || args.vol_id < 0)
  90. return errmsg("bad volume ID: " "\"%s\"", optarg);
  91. break;
  92. case 'N':
  93. args.vol_name = optarg;
  94. break;
  95. case 'd':
  96. args.devn = simple_strtoul(optarg, &error);
  97. if (error || args.devn < 0)
  98. return errmsg("bad UBI device number: \"%s\"", optarg);
  99. break;
  100. case 'h':
  101. printf("%s\n\n", doc);
  102. printf("%s\n\n", usage);
  103. printf("%s\n", optionsstr);
  104. exit(EXIT_SUCCESS);
  105. case 'V':
  106. common_print_version();
  107. exit(EXIT_SUCCESS);
  108. case ':':
  109. return errmsg("parameter is missing");
  110. default:
  111. fprintf(stderr, "Use -h for help\n");
  112. return -1;
  113. }
  114. }
  115. if (optind == argc - 1)
  116. args.node = argv[optind];
  117. else if (optind < argc)
  118. return errmsg("more then one UBI device specified (use -h for help)");
  119. return 0;
  120. }
  121. static int translate_dev(libubi_t libubi, const char *node)
  122. {
  123. int err;
  124. err = ubi_probe_node(libubi, node);
  125. if (err == -1) {
  126. if (errno != ENODEV)
  127. return sys_errmsg("error while probing \"%s\"", node);
  128. return errmsg("\"%s\" does not correspond to any UBI device or volume", node);
  129. }
  130. if (err == 1) {
  131. struct ubi_dev_info dev_info;
  132. err = ubi_get_dev_info(libubi, node, &dev_info);
  133. if (err)
  134. return sys_errmsg("cannot get information about UBI device \"%s\"", node);
  135. args.devn = dev_info.dev_num;
  136. } else {
  137. struct ubi_vol_info vol_info;
  138. err = ubi_get_vol_info(libubi, node, &vol_info);
  139. if (err)
  140. return sys_errmsg("cannot get information about UBI volume \"%s\"", node);
  141. if (args.vol_id != -1)
  142. return errmsg("both volume character device node (\"%s\") and "
  143. "volume ID (%d) are specify, use only one of them"
  144. "(use -h for help)", node, args.vol_id);
  145. args.devn = vol_info.dev_num;
  146. args.vol_id = vol_info.vol_id;
  147. }
  148. return 0;
  149. }
  150. static int get_vol_id_by_name(libubi_t libubi, int dev_num, const char *name)
  151. {
  152. int err;
  153. struct ubi_vol_info vol_info;
  154. err = ubi_get_vol_info1_nm(libubi, dev_num, name, &vol_info);
  155. if (err)
  156. return sys_errmsg("cannot get information about volume \"%s\" on ubi%d\n", name, dev_num);
  157. args.vol_id = vol_info.vol_id;
  158. return 0;
  159. }
  160. static int print_vol_info(libubi_t libubi, int dev_num, int vol_id)
  161. {
  162. int err;
  163. struct ubi_vol_info vol_info;
  164. err = ubi_get_vol_info1(libubi, dev_num, vol_id, &vol_info);
  165. if (err)
  166. return sys_errmsg("cannot get information about UBI volume %d on ubi%d",
  167. vol_id, dev_num);
  168. printf("Volume ID: %d (on ubi%d)\n", vol_info.vol_id, vol_info.dev_num);
  169. printf("Type: %s\n",
  170. vol_info.type == UBI_DYNAMIC_VOLUME ? "dynamic" : "static");
  171. printf("Alignment: %d\n", vol_info.alignment);
  172. printf("Size: %d LEBs (", vol_info.rsvd_lebs);
  173. util_print_bytes(vol_info.rsvd_bytes, 0);
  174. printf(")\n");
  175. if (vol_info.type == UBI_STATIC_VOLUME) {
  176. printf("Data bytes: ");
  177. util_print_bytes(vol_info.data_bytes, 1);
  178. printf("\n");
  179. }
  180. printf("State: %s\n", vol_info.corrupted ? "corrupted" : "OK");
  181. printf("Name: %s\n", vol_info.name);
  182. printf("Character device major/minor: %d:%d\n",
  183. vol_info.major, vol_info.minor);
  184. return 0;
  185. }
  186. static int print_dev_info(libubi_t libubi, int dev_num, int all)
  187. {
  188. int i, err, first = 1;
  189. struct ubi_dev_info dev_info;
  190. struct ubi_vol_info vol_info;
  191. err = ubi_get_dev_info1(libubi, dev_num, &dev_info);
  192. if (err)
  193. return sys_errmsg("cannot get information about UBI device %d", dev_num);
  194. printf("ubi%d\n", dev_info.dev_num);
  195. printf("Volumes count: %d\n", dev_info.vol_count);
  196. printf("Logical eraseblock size: ");
  197. util_print_bytes(dev_info.leb_size, 0);
  198. printf("\n");
  199. printf("Total amount of logical eraseblocks: %d (", dev_info.total_lebs);
  200. util_print_bytes(dev_info.total_bytes, 0);
  201. printf(")\n");
  202. printf("Amount of available logical eraseblocks: %d (", dev_info.avail_lebs);
  203. util_print_bytes(dev_info.avail_bytes, 0);
  204. printf(")\n");
  205. printf("Maximum count of volumes %d\n", dev_info.max_vol_count);
  206. printf("Count of bad physical eraseblocks: %d\n", dev_info.bad_count);
  207. printf("Count of reserved physical eraseblocks: %d\n", dev_info.bad_rsvd);
  208. printf("Current maximum erase counter value: %lld\n", dev_info.max_ec);
  209. printf("Minimum input/output unit size: %d %s\n",
  210. dev_info.min_io_size, dev_info.min_io_size > 1 ? "bytes" : "byte");
  211. printf("Character device major/minor: %d:%d\n",
  212. dev_info.major, dev_info.minor);
  213. if (dev_info.vol_count == 0)
  214. return 0;
  215. printf("Present volumes: ");
  216. for (i = dev_info.lowest_vol_id;
  217. i <= dev_info.highest_vol_id; i++) {
  218. err = ubi_get_vol_info1(libubi, dev_info.dev_num, i, &vol_info);
  219. if (err == -1) {
  220. if (errno == ENOENT)
  221. continue;
  222. return sys_errmsg("libubi failed to probe volume %d on ubi%d",
  223. i, dev_info.dev_num);
  224. }
  225. if (!first)
  226. printf(", %d", i);
  227. else {
  228. printf("%d", i);
  229. first = 0;
  230. }
  231. }
  232. printf("\n");
  233. if (!all)
  234. return 0;
  235. first = 1;
  236. printf("\n");
  237. for (i = dev_info.lowest_vol_id;
  238. i <= dev_info.highest_vol_id; i++) {
  239. if(!first)
  240. printf("-----------------------------------\n");
  241. err = ubi_get_vol_info1(libubi, dev_info.dev_num, i, &vol_info);
  242. if (err == -1) {
  243. if (errno == ENOENT)
  244. continue;
  245. return sys_errmsg("libubi failed to probe volume %d on ubi%d",
  246. i, dev_info.dev_num);
  247. }
  248. first = 0;
  249. err = print_vol_info(libubi, dev_info.dev_num, i);
  250. if (err)
  251. return err;
  252. }
  253. return 0;
  254. }
  255. static int print_general_info(libubi_t libubi, int all)
  256. {
  257. int i, err, first = 1;
  258. struct ubi_info ubi_info;
  259. struct ubi_dev_info dev_info;
  260. err = ubi_get_info(libubi, &ubi_info);
  261. if (err)
  262. return sys_errmsg("cannot get UBI information");
  263. printf("UBI version: %d\n", ubi_info.version);
  264. printf("Count of UBI devices: %d\n", ubi_info.dev_count);
  265. if (ubi_info.ctrl_major != -1)
  266. printf("UBI control device major/minor: %d:%d\n",
  267. ubi_info.ctrl_major, ubi_info.ctrl_minor);
  268. else
  269. printf("UBI control device is not supported by this kernel\n");
  270. if (ubi_info.dev_count == 0)
  271. return 0;
  272. printf("Present UBI devices: ");
  273. for (i = ubi_info.lowest_dev_num;
  274. i <= ubi_info.highest_dev_num; i++) {
  275. err = ubi_get_dev_info1(libubi, i, &dev_info);
  276. if (err == -1) {
  277. if (errno == ENOENT)
  278. continue;
  279. printf("\n");
  280. return sys_errmsg("libubi failed to probe UBI device %d", i);
  281. }
  282. if (!first)
  283. printf(", ubi%d", i);
  284. else {
  285. printf("ubi%d", i);
  286. first = 0;
  287. }
  288. }
  289. printf("\n");
  290. if (!all)
  291. return 0;
  292. first = 1;
  293. printf("\n");
  294. for (i = ubi_info.lowest_dev_num;
  295. i <= ubi_info.highest_dev_num; i++) {
  296. if (!ubi_dev_present(libubi, i))
  297. continue;
  298. if(!first)
  299. printf("\n===================================\n\n");
  300. first = 0;
  301. err = print_dev_info(libubi, i, all);
  302. if (err)
  303. return err;
  304. }
  305. return 0;
  306. }
  307. int main(int argc, char * const argv[])
  308. {
  309. int err;
  310. libubi_t libubi;
  311. err = parse_opt(argc, argv);
  312. if (err)
  313. return -1;
  314. libubi = libubi_open();
  315. if (!libubi) {
  316. if (errno == 0)
  317. return errmsg("UBI is not present in the system");
  318. return sys_errmsg("cannot open libubi");
  319. }
  320. if (args.node) {
  321. /*
  322. * A character device was specified, translate this into UBI
  323. * device number and volume ID.
  324. */
  325. err = translate_dev(libubi, args.node);
  326. if (err)
  327. goto out_libubi;
  328. }
  329. if (args.vol_name && args.devn == -1) {
  330. errmsg("volume name is specified, but UBI device number is not "
  331. "(use -h for help)\n");
  332. goto out_libubi;
  333. }
  334. if (args.vol_name) {
  335. err = get_vol_id_by_name(libubi, args.devn, args.vol_name);
  336. if (err)
  337. goto out_libubi;
  338. }
  339. if (args.vol_id != -1 && args.devn == -1) {
  340. errmsg("volume ID is specified, but UBI device number is not "
  341. "(use -h for help)\n");
  342. goto out_libubi;
  343. }
  344. if (args.devn != -1 && args.vol_id != -1) {
  345. print_vol_info(libubi, args.devn, args.vol_id);
  346. goto out;
  347. }
  348. if (args.devn == -1 && args.vol_id == -1)
  349. err = print_general_info(libubi, args.all);
  350. else if (args.devn != -1 && args.vol_id == -1)
  351. err = print_dev_info(libubi, args.devn, args.all);
  352. if (err)
  353. goto out_libubi;
  354. out:
  355. libubi_close(libubi);
  356. return 0;
  357. out_libubi:
  358. libubi_close(libubi);
  359. return -1;
  360. }