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