long long util_get_bytes(const char *str);
void util_print_bytes(long long bytes, int bracket);
int util_srand(void);
+char *mtd_find_dev_node(const char *id);
/*
* The following helpers are here to avoid compiler complaints about unchecked
#include <stdlib.h>
#include <unistd.h>
#include "common.h"
+#include "libmtd.h"
+
+#define MTD_DEV_PATT "/dev/mtd%d"
/**
* get_multiplier - convert size specifier to an integer multiplier.
srand(seed);
return 0;
}
+
+/**
+ * mtd_find_dev_node - Find the device node for an MTD
+ * @id: Identifier for the MTD. this can be the device node itself, or
+ * "mtd:<name>" to look up MTD by name
+ *
+ * This is a helper function to convert MTD device identifiers into their
+ * device node.
+ *
+ * Returns a pointer to a string containing the device node that must be
+ * free'd, or NULL on failure.
+ */
+char *mtd_find_dev_node(const char *id)
+{
+ struct mtd_dev_info info;
+ struct libmtd_t *lib_mtd;
+ char *node;
+ int ret;
+
+ if (strncmp(id, "mtd:", 4)) {
+ /* Assume @id is the device node */
+ return strdup(id);
+ }
+
+ /* Search for MTD matching name */
+ id += 4;
+
+ lib_mtd = libmtd_open();
+ if (!lib_mtd)
+ return NULL;
+
+ ret = mtd_get_dev_info2(lib_mtd, id, &info);
+ libmtd_close(lib_mtd);
+ if (ret < 0)
+ return NULL;
+
+ node = malloc(strlen(MTD_DEV_PATT) + 20);
+ if (!node)
+ return NULL;
+
+ sprintf(node, MTD_DEV_PATT, info.mtd_num);
+ return node;
+}
ftl_check_SOURCES += include/mtd/ftl-user.h
mtd_debug_SOURCES = misc-utils/mtd_debug.c
+mtd_debug_LDADD = libmtd.a
mtdpart_SOURCES = misc-utils/mtdpart.c
+mtdpart_LDADD = libmtd.a
docfdisk_SOURCES = misc-utils/docfdisk.c include/mtd_swab.h
docfdisk_SOURCES += include/mtd/inftl-user.h include/mtd/ftl-user.h
fectest_LDADD = libmtd.a
flash_lock_SOURCES = misc-utils/flash_lock.c
+flash_lock_LDADD = libmtd.a
flash_unlock_SOURCES = misc-utils/flash_unlock.c
+flash_unlock_LDADD = libmtd.a
flash_otp_info_SOURCES = misc-utils/flash_otp_info.c
flash_otp_write_SOURCES = misc-utils/flash_otp_write.c
flashcp_SOURCES = misc-utils/flashcp.c
+flashcp_LDADD = libmtd.a
flash_erase_SOURCES = misc-utils/flash_erase.c
flash_erase_LDADD = libmtd.a
" --silent same as --quiet\n"
" --help display this help and exit\n"
" --version output version information and exit\n",
+ "\n"
+ " MTD_DEVICE MTD device node or 'mtd:<name>'\n"
PROGRAM_NAME);
}
}
switch (argc - optind) {
case 3:
- mtd_device = argv[optind];
+ mtd_device = mtd_find_dev_node(argv[optind]);
+ if (!mtd_device)
+ return errmsg("Can't find MTD device %s", argv[optind]);
start = simple_strtoull(argv[optind + 1], &error);
eb_cnt = simple_strtoul(argv[optind + 2], &error);
break;
" -l --lock Lock a region of flash\n"
" -u --unlock Unlock a region of flash\n"
"\n"
+ " <mtd device> MTD device node or 'mtd:<name>'\n"
+ "\n"
"If offset is not specified, it defaults to 0.\n"
"If block count is not specified, it defaults to all blocks.\n"
"A block count of -1 means all blocks.\n",
}
/* First non-option argument */
- dev = argv[arg_idx++];
+ dev = mtd_find_dev_node(argv[arg_idx]);
+ if (!dev) {
+ errmsg("MTD device not found %s", argv[arg_idx]);
+ usage(EXIT_FAILURE);
+ }
+ arg_idx++;
/* Second non-option argument */
if (arg_idx < argc)
" -A | --erase-all Erases the whole device regardless of the image size\n"
" -V | --version Show version information and exit\n"
" <filename> File which you want to copy to flash\n"
- " <device> Flash device to write to (e.g. /dev/mtd0, /dev/mtd1, etc.)\n"
+ " <device> Flash device node or 'mtd:<name>' to write to (e.g. /dev/mtd0, /dev/mtd1, mtd:data, etc.)\n"
"\n",
PROGRAM_NAME);
DEBUG("Got filename: %s\n",filename);
flags |= FLAG_DEVICE;
- device = argv[optind+1];
+ device = mtd_find_dev_node(argv[optind+1]);
+ if (!device)
+ log_failure("Failed to find device %s\n", argv[optind+1]);
+
DEBUG("Got device: %s\n",device);
}
{
int err = 0, fd;
int open_flag;
+ char *dev;
enum {
OPT_INFO,
showusage();
/* open device */
+ dev = mtd_find_dev_node(argv[2]);
+ if (!dev)
+ errmsg_die("Failed to find MTD device %s", argv[2]);
+
open_flag = (option == OPT_INFO || option == OPT_READ) ? O_RDONLY : O_RDWR;
- if ((fd = open(argv[2], O_SYNC | open_flag)) < 0)
+ if ((fd = open(dev, O_SYNC | open_flag)) < 0)
errmsg_die("open()");
switch (option) {
" -h, --help Display this help and exit\n"
" -V, --version Output version information and exit\n"
"\n"
+" <MTD_DEVICE> MTD device node or 'mtd:<name>'\n"
+"\n"
"START location and SIZE of the partition are in bytes. They should align on\n"
"eraseblock size. If SIZE is 0 the partition will go to end of MTD device.\n",
PROGRAM_NAME
display_help(EXIT_FAILURE);
const char *s_command = argv[optind++];
- mtddev = argv[optind++];
+ mtddev = mtd_find_dev_node(argv[optind]);
+ if (!mtddev)
+ errmsg_die("MTD device not found %s", argv[optind]);
+ optind++;
if (strcmp(s_command, "del") == 0 && (argc - optind) == 1) {
const char *s_part_no = argv[optind++];
}
}
- if (optind < argc)
- mtddev = argv[optind++];
- else
+ if (optind < argc) {
+ mtddev = mtd_find_dev_node(argv[optind]);
+ if (!mtddev)
+ errmsg_die("Can't find MTD device %s", argv[optind]);
+ optind++;
+ } else {
errmsg_die("No device specified!\n");
+ }
if (optind < argc)
usage(EXIT_FAILURE);
}
}
- if (optind < argc)
- mtddev = argv[optind++];
- else
+ if (optind < argc) {
+ mtddev = mtd_find_dev_node(argv[optind]);
+ if (!mtddev)
+ errmsg_die("Can't find MTD device %s", argv[optind]);
+ optind++;
+ } else {
errmsg_die("No device specified!\n");
+ }
if (optind < argc)
usage(EXIT_FAILURE);
}
}
- if (optind < argc)
- mtddev = argv[optind++];
- else
+ if (optind < argc) {
+ mtddev = mtd_find_dev_node(argv[optind]);
+ if (!mtddev)
+ errmsg_die("Can't find MTD device %s", argv[optind]);
+ optind++;
+ } else {
errmsg_die("No device specified!\n");
+ }
if (optind < argc)
usage(EXIT_FAILURE);
}
}
- if (optind < argc)
- mtddev = argv[optind++];
- else
+ if (optind < argc) {
+ mtddev = mtd_find_dev_node(argv[optind]);
+ if (!mtddev)
+ errmsg_die("Can't find MTD device %s", argv[optind]);
+ optind++;
+ } else {
errmsg_die("No device specified!\n");
+ }
if (optind < argc)
usage(EXIT_FAILURE);
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"
+ "Usage: %1$s <mtd device> [--map | -M] [--ubi-info | -u]\n"
" %1$s --all [--ubi-info | -u]\n"
" %1$s [--help | --version]\n"
"\n"
"-h, --help print help message\n"
"-V, --version print program version\n"
"\n"
+ "<mtd device> MTD device node or 'mtd:<name>'\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"
}
}
- if (optind == argc - 1)
- args.node = argv[optind];
- else if (optind < argc)
+ if (optind == argc - 1) {
+ args.node = mtd_find_dev_node(argv[optind]);
+ if (!args.node)
+ return errmsg("Failed to find MTD device %s", 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;