diff options
Diffstat (limited to 'ubi-utils/src')
-rw-r--r-- | ubi-utils/src/libmtd.c | 39 | ||||
-rw-r--r-- | ubi-utils/src/libscan.c | 6 | ||||
-rw-r--r-- | ubi-utils/src/ubiformat.c | 41 |
3 files changed, 47 insertions, 39 deletions
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; } |