mtdinfo.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /*
  2. * Copyright (C) 2009 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 MTD information.
  19. *
  20. * Author: Artem Bityutskiy
  21. */
  22. #define PROGRAM_NAME "mtdinfo"
  23. #include <stdint.h>
  24. #include <stdio.h>
  25. #include <getopt.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <unistd.h>
  29. #include <mtd/mtd-user.h>
  30. #include <libubigen.h>
  31. #include <libmtd.h>
  32. #include "common.h"
  33. /* The variables below are set by command line arguments */
  34. struct args {
  35. unsigned int all:1;
  36. unsigned int ubinfo:1;
  37. unsigned int map:1;
  38. const char *node;
  39. };
  40. static struct args args = {
  41. .ubinfo = 0,
  42. .all = 0,
  43. .node = NULL,
  44. };
  45. static void display_help(void)
  46. {
  47. printf(
  48. "%1$s version %2$s - a tool to print MTD information.\n"
  49. "\n"
  50. "Usage: %1$s <MTD node file path> [--map | -M] [--ubi-info | -u]\n"
  51. " %1$s --all [--ubi-info | -u]\n"
  52. " %1$s [--help | --version]\n"
  53. "\n"
  54. "Options:\n"
  55. "-u, --ubi-info print what would UBI layout be if it was put\n"
  56. " on this MTD device\n"
  57. "-M, --map print eraseblock map\n"
  58. "-a, --all print information about all MTD devices\n"
  59. " Note: `--all' may give less info per device\n"
  60. " than, e.g., `mtdinfo /dev/mtdX'\n"
  61. "-h, --help print help message\n"
  62. "-V, --version print program version\n"
  63. "\n"
  64. "Examples:\n"
  65. " %1$s /dev/mtd0 print information MTD device /dev/mtd0\n"
  66. " %1$s /dev/mtd0 -u print information MTD device /dev/mtd0\n"
  67. " %4$*3$s and include UBI layout information\n"
  68. " %1$s -a print information about all MTD devices\n",
  69. PROGRAM_NAME, VERSION, (int)strlen(PROGRAM_NAME) + 3, "");
  70. }
  71. static const struct option long_options[] = {
  72. { .name = "ubi-info", .has_arg = 0, .flag = NULL, .val = 'u' },
  73. { .name = "map", .has_arg = 0, .flag = NULL, .val = 'M' },
  74. { .name = "all", .has_arg = 0, .flag = NULL, .val = 'a' },
  75. { .name = "help", .has_arg = 0, .flag = NULL, .val = 'h' },
  76. { .name = "version", .has_arg = 0, .flag = NULL, .val = 'V' },
  77. { NULL, 0, NULL, 0},
  78. };
  79. static int parse_opt(int argc, char * const argv[])
  80. {
  81. while (1) {
  82. int key;
  83. key = getopt_long(argc, argv, "auMhV", long_options, NULL);
  84. if (key == -1)
  85. break;
  86. switch (key) {
  87. case 'a':
  88. args.all = 1;
  89. break;
  90. case 'u':
  91. args.ubinfo = 1;
  92. break;
  93. case 'M':
  94. args.map = 1;
  95. break;
  96. case 'h':
  97. display_help();
  98. exit(EXIT_SUCCESS);
  99. case 'V':
  100. common_print_version();
  101. exit(EXIT_SUCCESS);
  102. case ':':
  103. return errmsg("parameter is missing");
  104. default:
  105. fprintf(stderr, "Use -h for help\n");
  106. return -1;
  107. }
  108. }
  109. if (optind == argc - 1)
  110. args.node = argv[optind];
  111. else if (optind < argc)
  112. return errmsg("more then one MTD device specified (use -h for help)");
  113. if (args.all && args.node)
  114. args.node = NULL;
  115. if (args.map && !args.node)
  116. return errmsg("-M requires MTD device node name");
  117. return 0;
  118. }
  119. static int translate_dev(libmtd_t libmtd, const char *node)
  120. {
  121. int err;
  122. struct mtd_dev_info mtd;
  123. err = mtd_get_dev_info(libmtd, node, &mtd);
  124. if (err) {
  125. if (errno == ENODEV)
  126. return errmsg("\"%s\" does not correspond to any "
  127. "existing MTD device", node);
  128. return sys_errmsg("cannot get information about MTD "
  129. "device \"%s\"", node);
  130. }
  131. return mtd.mtd_num;
  132. }
  133. static void print_ubi_info(const struct mtd_info *mtd_info,
  134. const struct mtd_dev_info *mtd)
  135. {
  136. struct ubigen_info ui;
  137. if (!mtd_info->sysfs_supported) {
  138. errmsg("cannot provide UBI info, becasue sub-page size is "
  139. "not known");
  140. return;
  141. }
  142. ubigen_info_init(&ui, mtd->eb_size, mtd->min_io_size, mtd->subpage_size,
  143. 0, 1, 0);
  144. printf("Default UBI VID header offset: %d\n", ui.vid_hdr_offs);
  145. printf("Default UBI data offset: %d\n", ui.data_offs);
  146. printf("Default UBI LEB size: ");
  147. util_print_bytes(ui.leb_size, 0);
  148. printf("\n");
  149. printf("Maximum UBI volumes count: %d\n", ui.max_volumes);
  150. }
  151. static void print_region_map(const struct mtd_dev_info *mtd, int fd,
  152. const region_info_t *reginfo)
  153. {
  154. unsigned long start;
  155. int i, width;
  156. int ret_locked, errno_locked, ret_bad, errno_bad;
  157. printf("Eraseblock map:\n");
  158. /* Figure out the number of spaces to pad w/out libm */
  159. for (i = 1, width = 0; i < reginfo->numblocks; i *= 10, ++width)
  160. continue;
  161. /* If we don't have a fd to query, just show the bare map */
  162. if (fd == -1) {
  163. ret_locked = ret_bad = -1;
  164. errno_locked = errno_bad = ENODEV;
  165. } else
  166. ret_locked = ret_bad = errno_locked = errno_bad = 0;
  167. for (i = 0; i < reginfo->numblocks; ++i) {
  168. start = reginfo->offset + i * reginfo->erasesize;
  169. printf(" %*i: %08lx ", width, i, start);
  170. if (ret_locked != -1) {
  171. ret_locked = mtd_is_locked(mtd, fd, i);
  172. if (ret_locked == 1)
  173. printf("RO ");
  174. else
  175. errno_locked = errno;
  176. }
  177. if (ret_locked != 1)
  178. printf(" ");
  179. if (ret_bad != -1) {
  180. ret_bad = mtd_is_bad(mtd, fd, i);
  181. if (ret_bad == 1)
  182. printf("BAD ");
  183. else
  184. errno_bad = errno;
  185. }
  186. if (ret_bad != 1)
  187. printf(" ");
  188. if (((i + 1) % 4) == 0)
  189. printf("\n");
  190. }
  191. if (i % 4)
  192. printf("\n");
  193. if (ret_locked == -1 && errno_locked != EOPNOTSUPP) {
  194. errno = errno_locked;
  195. sys_errmsg("could not read locked block info");
  196. }
  197. if (mtd->bb_allowed && ret_bad == -1 && errno_bad != EOPNOTSUPP) {
  198. errno = errno_bad;
  199. sys_errmsg("could not read bad block info");
  200. }
  201. }
  202. static void print_region_info(const struct mtd_dev_info *mtd)
  203. {
  204. region_info_t reginfo;
  205. int r, fd;
  206. /*
  207. * If we don't have any region info, just return
  208. *
  209. * FIXME: We can't get region_info (via ioctl) without having the MTD
  210. * node path. This is a problem for `mtdinfo -a', for example,
  211. * since it doesn't provide any filepath information.
  212. */
  213. if (!args.node || (!args.map && mtd->region_cnt == 0))
  214. return;
  215. memset(&reginfo, 0, sizeof(reginfo));
  216. /* First open the device so we can query it */
  217. fd = open(args.node, O_RDONLY | O_CLOEXEC);
  218. if (fd == -1) {
  219. sys_errmsg("couldn't open MTD dev: %s", args.node);
  220. if (mtd->region_cnt)
  221. return;
  222. }
  223. /* Walk all the regions and show the map for them */
  224. if (mtd->region_cnt) {
  225. for (r = 0; r < mtd->region_cnt; ++r) {
  226. printf("Eraseblock region %i: ", r);
  227. if (mtd_regioninfo(fd, r, &reginfo) == 0) {
  228. printf(" offset: %#x size: %#x numblocks: %#x\n",
  229. reginfo.offset, reginfo.erasesize,
  230. reginfo.numblocks);
  231. if (args.map)
  232. print_region_map(mtd, fd, &reginfo);
  233. } else
  234. printf(" info is unavailable\n");
  235. }
  236. } else {
  237. reginfo.offset = 0;
  238. reginfo.erasesize = mtd->eb_size;
  239. reginfo.numblocks = mtd->eb_cnt;
  240. reginfo.regionindex = 0;
  241. print_region_map(mtd, fd, &reginfo);
  242. }
  243. if (fd != -1)
  244. close(fd);
  245. }
  246. static int print_dev_info(libmtd_t libmtd, const struct mtd_info *mtd_info, int mtdn)
  247. {
  248. int err;
  249. struct mtd_dev_info mtd;
  250. err = mtd_get_dev_info1(libmtd, mtdn, &mtd);
  251. if (err) {
  252. if (errno == ENODEV)
  253. return errmsg("mtd%d does not correspond to any "
  254. "existing MTD device", mtdn);
  255. return sys_errmsg("cannot get information about MTD device %d",
  256. mtdn);
  257. }
  258. printf("mtd%d\n", mtd.mtd_num);
  259. printf("Name: %s\n", mtd.name);
  260. printf("Type: %s\n", mtd.type_str);
  261. printf("Eraseblock size: ");
  262. util_print_bytes(mtd.eb_size, 0);
  263. printf("\n");
  264. printf("Amount of eraseblocks: %d (", mtd.eb_cnt);
  265. util_print_bytes(mtd.size, 0);
  266. printf(")\n");
  267. printf("Minimum input/output unit size: %d %s\n",
  268. mtd.min_io_size, mtd.min_io_size > 1 ? "bytes" : "byte");
  269. if (mtd_info->sysfs_supported)
  270. printf("Sub-page size: %d %s\n",
  271. mtd.subpage_size,
  272. mtd.subpage_size > 1 ? "bytes" : "byte");
  273. else if (mtd.type == MTD_NANDFLASH || mtd.type == MTD_MLCNANDFLASH)
  274. printf("Sub-page size: unknown\n");
  275. if (mtd.oob_size > 0)
  276. printf("OOB size: %d bytes\n",
  277. mtd.oob_size);
  278. if (mtd.region_cnt > 0)
  279. printf("Additional erase regions: %d\n", mtd.oob_size);
  280. if (mtd_info->sysfs_supported)
  281. printf("Character device major/minor: %d:%d\n",
  282. mtd.major, mtd.minor);
  283. printf("Bad blocks are allowed: %s\n",
  284. mtd.bb_allowed ? "true" : "false");
  285. printf("Device is writable: %s\n",
  286. mtd.writable ? "true" : "false");
  287. if (args.ubinfo)
  288. print_ubi_info(mtd_info, &mtd);
  289. print_region_info(&mtd);
  290. printf("\n");
  291. return 0;
  292. }
  293. static int print_general_info(libmtd_t libmtd, const struct mtd_info *mtd_info,
  294. int all)
  295. {
  296. int i, err, first = 1;
  297. struct mtd_dev_info mtd;
  298. printf("Count of MTD devices: %d\n", mtd_info->mtd_dev_cnt);
  299. if (mtd_info->mtd_dev_cnt == 0)
  300. return 0;
  301. for (i = mtd_info->lowest_mtd_num;
  302. i <= mtd_info->highest_mtd_num; i++) {
  303. err = mtd_get_dev_info1(libmtd, i, &mtd);
  304. if (err == -1) {
  305. if (errno == ENODEV)
  306. continue;
  307. return sys_errmsg("libmtd failed to get MTD device %d "
  308. "information", i);
  309. }
  310. if (!first)
  311. printf(", mtd%d", i);
  312. else {
  313. printf("Present MTD devices: mtd%d", i);
  314. first = 0;
  315. }
  316. }
  317. printf("\n");
  318. printf("Sysfs interface supported: %s\n",
  319. mtd_info->sysfs_supported ? "yes" : "no");
  320. if (!all)
  321. return 0;
  322. printf("\n");
  323. for (i = mtd_info->lowest_mtd_num;
  324. i <= mtd_info->highest_mtd_num; i++) {
  325. if (!mtd_dev_present(libmtd, i))
  326. continue;
  327. err = print_dev_info(libmtd, mtd_info, i);
  328. if (err)
  329. return err;
  330. }
  331. return 0;
  332. }
  333. int main(int argc, char * const argv[])
  334. {
  335. int err;
  336. libmtd_t libmtd;
  337. struct mtd_info mtd_info;
  338. err = parse_opt(argc, argv);
  339. if (err)
  340. return -1;
  341. libmtd = libmtd_open();
  342. if (libmtd == NULL) {
  343. if (errno == 0)
  344. return errmsg("MTD is not present in the system");
  345. return sys_errmsg("cannot open libmtd");
  346. }
  347. err = mtd_get_info(libmtd, &mtd_info);
  348. if (err)
  349. return sys_errmsg("cannot get MTD information");
  350. if (!args.all && args.node) {
  351. int mtdn;
  352. /*
  353. * A character device was specified, translate this to MTD
  354. * device number.
  355. */
  356. mtdn = translate_dev(libmtd, args.node);
  357. if (mtdn < 0)
  358. goto out_libmtd;
  359. err = print_dev_info(libmtd, &mtd_info, mtdn);
  360. } else
  361. err = print_general_info(libmtd, &mtd_info, args.all);
  362. if (err)
  363. goto out_libmtd;
  364. libmtd_close(libmtd);
  365. return 0;
  366. out_libmtd:
  367. libmtd_close(libmtd);
  368. return -1;
  369. }