aboutsummaryrefslogtreecommitdiff
path: root/ubi-utils
diff options
context:
space:
mode:
Diffstat (limited to 'ubi-utils')
-rw-r--r--ubi-utils/include/libmtd.h23
-rw-r--r--ubi-utils/include/libscan.h4
-rw-r--r--ubi-utils/src/libmtd.c39
-rw-r--r--ubi-utils/src/libscan.c6
-rw-r--r--ubi-utils/src/ubiformat.c41
5 files changed, 64 insertions, 49 deletions
diff --git a/ubi-utils/include/libmtd.h b/ubi-utils/include/libmtd.h
index c50059d..9c7a6e3 100644
--- a/ubi-utils/include/libmtd.h
+++ b/ubi-utils/include/libmtd.h
@@ -41,7 +41,6 @@ extern "C" {
* @subpage_size: sub-page size (not set by 'mtd_get_info()'!!!)
* @rdonly: non-zero if the device is read-only
* @allows_bb: non-zero if the MTD device may have bad eraseblocks
- * @fd: descriptor of the opened MTD character device node
*/
struct mtd_info
{
@@ -57,7 +56,6 @@ struct mtd_info
int subpage_size;
unsigned int rdonly:1;
unsigned int allows_bb:1;
- int fd;
};
/**
@@ -74,36 +72,40 @@ int mtd_get_info(const char *node, struct mtd_info *mtd);
/**
* mtd_erase - erase an eraseblock.
* @mtd: MTD device description object
+ * @fd: MTD device node file descriptor
* @eb: eraseblock to erase
*
- * This function erases the eraseblock and returns %0 in case of success and
- * %-1 in case of failure.
+ * This function erases eraseblock @eb of MTD device decribed by @fd. Returns
+ * %0 in case of success and %-1 in case of failure.
*/
-int mtd_erase(const struct mtd_info *mtd, int eb);
+int mtd_erase(const struct mtd_info *mtd, int fd, int eb);
/**
* mtd_is_bad - check if eraseblock is bad.
* @mtd: MTD device description object
+ * @fd: MTD device node file descriptor
* @eb: eraseblock to check
*
* This function checks if eraseblock @eb is bad. Returns %0 if not, %1 if yes,
* and %-1 in case of failure.
*/
-int mtd_is_bad(const struct mtd_info *mtd, int eb);
+int mtd_is_bad(const struct mtd_info *mtd, int fd, int eb);
/**
* mtd_mark_bad - marks the block as bad.
* @mtd: MTD device description object
+ * @fd: MTD device node file descriptor
* @eb: eraseblock to mark bad
*
* This function marks the eraseblock @eb as bad. Returns %0 if success
* %-1 if failure
*/
-int mtd_mark_bad(const struct mtd_info *mtd, int eb);
+int mtd_mark_bad(const struct mtd_info *mtd, int fd, int eb);
/**
* mtd_read - read data from an MTD device.
* @mtd: MTD device description object
+ * @fd: MTD device node file descriptor
* @eb: eraseblock to read from
* @offs: offset withing the eraseblock to read from
* @buf: buffer to read data to
@@ -113,11 +115,13 @@ int mtd_mark_bad(const struct mtd_info *mtd, int eb);
* of the MTD device defined by @mtd and stores the read data at buffer @buf.
* Returns %0 in case of success and %-1 in case of failure.
*/
-int mtd_read(const struct mtd_info *mtd, int eb, int offs, void *buf, int len);
+int mtd_read(const struct mtd_info *mtd, int fd, int eb, int offs, void *buf,
+ int len);
/**
* mtd_write - write data to an MTD device.
* @mtd: MTD device description object
+ * @fd: MTD device node file descriptor
* @eb: eraseblock to write to
* @offs: offset withing the eraseblock to write to
* @buf: buffer to write
@@ -127,7 +131,8 @@ int mtd_read(const struct mtd_info *mtd, int eb, int offs, void *buf, int len);
* of the MTD device defined by @mtd. Returns %0 in case of success and %-1 in
* case of failure.
*/
-int mtd_write(const struct mtd_info *mtd, int eb, int offs, void *buf, int len);
+int mtd_write(const struct mtd_info *mtd, int fd, int eb, int offs, void *buf,
+ int len);
#ifdef __cplusplus
}
diff --git a/ubi-utils/include/libscan.h b/ubi-utils/include/libscan.h
index 5afc93e..8597b98 100644
--- a/ubi-utils/include/libscan.h
+++ b/ubi-utils/include/libscan.h
@@ -92,11 +92,13 @@ struct mtd_info;
/**
* ubi_scan - scan an MTD device.
* @mtd: information about the MTD device to scan
+ * @fd: MTD device node file descriptor
* @info: the result of the scanning is returned here
* @verbose: verbose mode: %0 - be silent, %1 - output progress information,
* 2 - debugging output mode
*/
-int ubi_scan(struct mtd_info *mtd, struct ubi_scan_info **info, int verbose);
+int ubi_scan(struct mtd_info *mtd, int fd, struct ubi_scan_info **info,
+ int verbose);
/**
* ubi_scan_free - free scanning information.
diff --git a/ubi-utils/src/libmtd.c b/ubi-utils/src/libmtd.c
index 4cc9bdb..127d0ec 100644
--- a/ubi-utils/src/libmtd.c
+++ b/ubi-utils/src/libmtd.c
@@ -37,7 +37,7 @@ int mtd_get_info(const char *node, struct mtd_info *mtd)
{
struct stat st;
struct mtd_info_user ui;
- int ret;
+ int fd, ret;
loff_t offs = 0;
if (stat(node, &st))
@@ -60,16 +60,16 @@ int mtd_get_info(const char *node, struct mtd_info *mtd)
mtd->num = mtd->minor / 2;
mtd->rdonly = mtd->minor & 1;
- mtd->fd = open(node, O_RDWR);
- if (mtd->fd == -1)
+ fd = open(node, O_RDWR);
+ if (fd == -1)
return sys_errmsg("cannot open \"%s\"", node);
- if (ioctl(mtd->fd, MEMGETINFO, &ui)) {
+ if (ioctl(fd, MEMGETINFO, &ui)) {
sys_errmsg("MEMGETINFO ioctl request failed");
goto out_close;
}
- ret = ioctl(mtd->fd, MEMGETBADBLOCK, &offs);
+ ret = ioctl(fd, MEMGETBADBLOCK, &offs);
if (ret == -1) {
if (errno != EOPNOTSUPP) {
sys_errmsg("MEMGETBADBLOCK ioctl failed");
@@ -133,23 +133,24 @@ int mtd_get_info(const char *node, struct mtd_info *mtd)
if (!(ui.flags & MTD_WRITEABLE))
mtd->rdonly = 1;
+ close(fd);
return 0;
out_close:
- close(mtd->fd);
+ close(fd);
return -1;
}
-int mtd_erase(const struct mtd_info *mtd, int eb)
+int mtd_erase(const struct mtd_info *mtd, int fd, int eb)
{
struct erase_info_user ei;
ei.start = eb * mtd->eb_size;;
ei.length = mtd->eb_size;
- return ioctl(mtd->fd, MEMERASE, &ei);
+ return ioctl(fd, MEMERASE, &ei);
}
-int mtd_is_bad(const struct mtd_info *mtd, int eb)
+int mtd_is_bad(const struct mtd_info *mtd, int fd, int eb)
{
int ret;
loff_t seek;
@@ -165,14 +166,14 @@ int mtd_is_bad(const struct mtd_info *mtd, int eb)
return 0;
seek = (loff_t)eb * mtd->eb_size;
- ret = ioctl(mtd->fd, MEMGETBADBLOCK, &seek);
+ ret = ioctl(fd, MEMGETBADBLOCK, &seek);
if (ret == -1)
return sys_errmsg("MEMGETBADBLOCK ioctl failed for "
"eraseblock %d (mtd%d)", eb, mtd->num);
return ret;
}
-int mtd_mark_bad(const struct mtd_info *mtd, int eb)
+int mtd_mark_bad(const struct mtd_info *mtd, int fd, int eb)
{
int ret;
loff_t seek;
@@ -190,14 +191,15 @@ int mtd_mark_bad(const struct mtd_info *mtd, int eb)
}
seek = (loff_t)eb * mtd->eb_size;
- ret = ioctl(mtd->fd, MEMSETBADBLOCK, &seek);
+ ret = ioctl(fd, MEMSETBADBLOCK, &seek);
if (ret == -1)
return sys_errmsg("MEMSETBADBLOCK ioctl failed for "
"eraseblock %d (mtd%d)", eb, mtd->num);
return 0;
}
-int mtd_read(const struct mtd_info *mtd, int eb, int offs, void *buf, int len)
+int mtd_read(const struct mtd_info *mtd, int fd, int eb, int offs, void *buf,
+ int len)
{
int ret, rd = 0;
off_t seek;
@@ -217,12 +219,12 @@ int mtd_read(const struct mtd_info *mtd, int eb, int offs, void *buf, int len)
/* Seek to the beginning of the eraseblock */
seek = (off_t)eb * mtd->eb_size + offs;
- if (lseek(mtd->fd, seek, SEEK_SET) != seek)
+ if (lseek(fd, seek, SEEK_SET) != seek)
return sys_errmsg("cannot seek mtd%d to offset %llu",
mtd->num, (unsigned long long)seek);
while (rd < len) {
- ret = read(mtd->fd, buf, len);
+ ret = read(fd, buf, len);
if (ret < 0)
return sys_errmsg("cannot read %d bytes from mtd%d (eraseblock %d, offset %d)",
len, mtd->num, eb, offs);
@@ -232,7 +234,8 @@ int mtd_read(const struct mtd_info *mtd, int eb, int offs, void *buf, int len)
return 0;
}
-int mtd_write(const struct mtd_info *mtd, int eb, int offs, void *buf, int len)
+int mtd_write(const struct mtd_info *mtd, int fd, int eb, int offs, void *buf,
+ int len)
{
int ret;
off_t seek;
@@ -266,11 +269,11 @@ int mtd_write(const struct mtd_info *mtd, int eb, int offs, void *buf, int len)
/* Seek to the beginning of the eraseblock */
seek = (off_t)eb * mtd->eb_size + offs;
- if (lseek(mtd->fd, seek, SEEK_SET) != seek)
+ if (lseek(fd, seek, SEEK_SET) != seek)
return sys_errmsg("cannot seek mtd%d to offset %llu",
mtd->num, (unsigned long long)seek);
- ret = write(mtd->fd, buf, len);
+ ret = write(fd, buf, len);
if (ret != len)
return sys_errmsg("cannot write %d bytes to mtd%d (eraseblock %d, offset %d)",
len, mtd->num, eb, offs);
diff --git a/ubi-utils/src/libscan.c b/ubi-utils/src/libscan.c
index dc1f083..4d64bca 100644
--- a/ubi-utils/src/libscan.c
+++ b/ubi-utils/src/libscan.c
@@ -48,7 +48,7 @@ static int all_ff(const void *buf, int len)
return 1;
}
-int ubi_scan(struct mtd_info *mtd, struct ubi_scan_info **info, int verbose)
+int ubi_scan(struct mtd_info *mtd, int fd, struct ubi_scan_info **info, int verbose)
{
int eb, v = (verbose == 2), pr = (verbose == 1);
struct ubi_scan_info *si;
@@ -85,7 +85,7 @@ int ubi_scan(struct mtd_info *mtd, struct ubi_scan_info **info, int verbose)
fflush(stdout);
}
- ret = mtd_is_bad(mtd, eb);
+ ret = mtd_is_bad(mtd, fd, eb);
if (ret == -1)
goto out_ec;
if (ret) {
@@ -96,7 +96,7 @@ int ubi_scan(struct mtd_info *mtd, struct ubi_scan_info **info, int verbose)
continue;
}
- ret = mtd_read(mtd, eb, 0, &hdr, sizeof(struct ubi_ec_hdr));;
+ ret = mtd_read(mtd, fd, eb, 0, &hdr, sizeof(struct ubi_ec_hdr));
if (ret < 0)
goto out_ec;
diff --git a/ubi-utils/src/ubiformat.c b/ubi-utils/src/ubiformat.c
index 3a1b198..2226a75 100644
--- a/ubi-utils/src/ubiformat.c
+++ b/ubi-utils/src/ubiformat.c
@@ -62,6 +62,7 @@ struct args {
long long ec;
const char *image;
const char *node;
+ int node_fd;
};
static struct args args =
@@ -412,7 +413,7 @@ static int mark_bad(const struct mtd_info *mtd, struct ubi_scan_info *si, int eb
return errmsg("bad blocks not supported by this flash");
}
- err = mtd_mark_bad(mtd, eb);
+ err = mtd_mark_bad(mtd, args.node_fd, eb);
if (err)
return err;
@@ -468,7 +469,7 @@ static int flash_image(const struct mtd_info *mtd, struct ubi_scan_info *si)
fflush(stdout);
}
- err = mtd_erase(mtd, eb);
+ err = mtd_erase(mtd, args.node_fd, eb);
if (err) {
if (!args.quiet)
printf("\n");
@@ -518,7 +519,7 @@ static int flash_image(const struct mtd_info *mtd, struct ubi_scan_info *si)
new_len = drop_ffs(mtd, buf, mtd->eb_size);
- err = mtd_write(mtd, eb, 0, buf, new_len);
+ err = mtd_write(mtd, args.node_fd, eb, 0, buf, new_len);
if (err) {
if (!args.quiet)
printf("\n");
@@ -565,7 +566,6 @@ static int format(const struct mtd_info *mtd, const struct ubigen_info *ui,
hdr = malloc(write_size);
if (!hdr)
return sys_errmsg("cannot allocate %d bytes of memory", write_size);
-
memset(hdr, 0xFF, write_size);
for (eb = start_eb; eb < mtd->eb_cnt; eb++) {
@@ -593,7 +593,7 @@ static int format(const struct mtd_info *mtd, const struct ubigen_info *ui,
fflush(stdout);
}
- err = mtd_erase(mtd, eb);
+ err = mtd_erase(mtd, args.node_fd, eb);
if (err) {
if (!args.quiet)
printf("\n");
@@ -625,7 +625,7 @@ static int format(const struct mtd_info *mtd, const struct ubigen_info *ui,
fflush(stdout);
}
- err = mtd_write(mtd, eb, 0, hdr, write_size);
+ err = mtd_write(mtd, args.node_fd, eb, 0, hdr, write_size);
if (err) {
if (!args.quiet && !args.verbose)
printf("\n");
@@ -662,7 +662,8 @@ static int format(const struct mtd_info *mtd, const struct ubigen_info *ui,
if (!vtbl)
goto out_free;
- err = ubigen_write_layout_vol(ui, eb1, eb2, ec1, ec2, vtbl, mtd->fd);
+ err = ubigen_write_layout_vol(ui, eb1, eb2, ec1, ec2, vtbl,
+ args.node_fd);
free(vtbl);
if (err) {
errmsg("cannot write layout volume");
@@ -694,17 +695,21 @@ int main(int argc, char * const argv[])
if (err)
return errmsg("cannot get information about \"%s\"", args.node);
+ args.node_fd = open(args.node, O_RDWR);
+ if (args.node_fd == -1)
+ return sys_errmsg("cannot open \"%s\"", args.node);
+
if (args.subpage_size == 0)
args.subpage_size = mtd.min_io_size;
else {
if (args.subpage_size > mtd.min_io_size) {
errmsg("sub-page cannot be larger than min. I/O unit");
- goto out_close;
+ goto out;
}
if (mtd.min_io_size % args.subpage_size) {
errmsg("min. I/O unit size should be multiple of sub-page size");
- goto out_close;
+ goto out;
}
}
@@ -712,11 +717,11 @@ int main(int argc, char * const argv[])
if (args.vid_hdr_offs != 0) {
if (args.vid_hdr_offs % 8) {
errmsg("VID header offset has to be multiple of min. I/O unit size");
- goto out_close;
+ goto out;
}
if (args.vid_hdr_offs + (int)UBI_VID_HDR_SIZE > mtd.eb_size) {
errmsg("bad VID header offset");
- goto out_close;
+ goto out;
}
}
@@ -729,7 +734,7 @@ int main(int argc, char * const argv[])
if (mtd.rdonly) {
errmsg("mtd%d (%s) is a read-only device", mtd.num, args.node);
- goto out_close;
+ goto out;
}
/* Make sure this MTD device is not attached to UBI */
@@ -742,7 +747,7 @@ int main(int argc, char * const argv[])
if (!err) {
errmsg("please, first detach mtd%d (%s) from ubi%d",
mtd.num, args.node, ubi_dev_num);
- goto out_close;
+ goto out;
}
}
@@ -760,10 +765,10 @@ int main(int argc, char * const argv[])
verbose = 2;
else
verbose = 1;
- err = ubi_scan(&mtd, &si, verbose);
+ err = ubi_scan(&mtd, args.node_fd, &si, verbose);
if (err) {
errmsg("failed to scan mtd%d (%s)", mtd.num, args.node);
- goto out_close;
+ goto out;
}
if (si->good_cnt == 0) {
@@ -877,12 +882,12 @@ int main(int argc, char * const argv[])
}
ubi_scan_free(si);
- close(mtd.fd);
+ close(args.node_fd);
return 0;
out_free:
ubi_scan_free(si);
-out_close:
- close(mtd.fd);
+out:
+ close(args.node_fd);
return -1;
}