summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorSascha Hauer <s.hauer@pengutronix.de>2016-02-26 10:31:03 +0100
committerSascha Hauer <s.hauer@pengutronix.de>2016-04-06 10:17:52 +0200
commit1d88c6697722806d6c16bfadffb7c3c4d666e8e5 (patch)
treeebb966113ddc776d1723f669ac2213a53e517692 /lib
parenteef520a32b72606793cd02b2af57e559c64f8620 (diff)
downloadbarebox-1d88c6697722806d6c16bfadffb7c3c4d666e8e5.tar.gz
barebox-1d88c6697722806d6c16bfadffb7c3c4d666e8e5.tar.xz
ubiformat: Use mtd-peb API
This changes ubiformat from the libmtd API to the mtd-peb API. This makes the libmtd API unnecessary and it can be removed in the next step. Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'lib')
-rw-r--r--lib/libscan.c28
-rw-r--r--lib/libubigen.c23
2 files changed, 21 insertions, 30 deletions
diff --git a/lib/libscan.c b/lib/libscan.c
index 29d1cdbec3..bf298a70bb 100644
--- a/lib/libscan.c
+++ b/lib/libscan.c
@@ -26,26 +26,28 @@
#include <linux/mtd/mtd.h>
#include <linux/stat.h>
#include <linux/mtd/mtd-abi.h>
-#include <mtd/libmtd.h>
+#include <mtd/mtd-peb.h>
#include <mtd/libscan.h>
#include <mtd/ubi-user.h>
#include <mtd/utils.h>
#include <mtd/ubi-media.h>
#include <asm-generic/div64.h>
-int libscan_ubi_scan(struct mtd_dev_info *mtd, int fd, struct ubi_scan_info **info,
+int libscan_ubi_scan(struct mtd_info *mtd, struct ubi_scan_info **info,
int verbose)
{
- int eb, v = (verbose == 2), pr = (verbose == 1);
+ int eb, v = (verbose == 2), pr = (verbose == 1), eb_cnt;
struct ubi_scan_info *si;
unsigned long long sum = 0;
+ eb_cnt = mtd_div_by_eb(mtd->size, mtd);
+
si = calloc(1, sizeof(struct ubi_scan_info));
if (!si)
return sys_errmsg("cannot allocate %zd bytes of memory",
sizeof(struct ubi_scan_info));
- si->ec = calloc(mtd->eb_cnt, sizeof(uint32_t));
+ si->ec = calloc(eb_cnt, sizeof(uint32_t));
if (!si->ec) {
sys_errmsg("cannot allocate %zd bytes of memory",
sizeof(struct ubi_scan_info));
@@ -54,8 +56,8 @@ int libscan_ubi_scan(struct mtd_dev_info *mtd, int fd, struct ubi_scan_info **in
si->vid_hdr_offs = si->data_offs = -1;
- verbose(v, "start scanning eraseblocks 0-%d", mtd->eb_cnt);
- for (eb = 0; eb < mtd->eb_cnt; eb++) {
+ verbose(v, "start scanning eraseblocks 0-%d", eb_cnt);
+ for (eb = 0; eb < eb_cnt; eb++) {
int ret;
uint32_t crc;
struct ubi_ec_hdr ech;
@@ -65,10 +67,10 @@ int libscan_ubi_scan(struct mtd_dev_info *mtd, int fd, struct ubi_scan_info **in
normsg_cont("scanning eraseblock %d", eb);
if (pr) {
printf("\r" PROGRAM_NAME ": scanning eraseblock %d -- %2u %% complete ",
- eb, (eb + 1) * 100 / mtd->eb_cnt);
+ eb, (eb + 1) * 100 / eb_cnt);
}
- ret = mtd_is_bad(mtd, fd, eb);
+ ret = mtd_peb_is_bad(mtd, eb);
if (ret == -1)
goto out_ec;
if (ret) {
@@ -79,7 +81,7 @@ int libscan_ubi_scan(struct mtd_dev_info *mtd, int fd, struct ubi_scan_info **in
continue;
}
- ret = libmtd_read(mtd, fd, eb, 0, &ech, sizeof(struct ubi_ec_hdr));
+ ret = mtd_peb_read(mtd, &ech, eb, 0, sizeof(struct ubi_ec_hdr));
if (ret < 0)
goto out_ec;
@@ -121,14 +123,14 @@ int libscan_ubi_scan(struct mtd_dev_info *mtd, int fd, struct ubi_scan_info **in
if (si->vid_hdr_offs == -1) {
si->vid_hdr_offs = be32_to_cpu(ech.vid_hdr_offset);
si->data_offs = be32_to_cpu(ech.data_offset);
- if (si->data_offs % mtd->min_io_size) {
+ if (si->data_offs % mtd->writesize) {
if (pr)
printf("\n");
if (v)
printf(": corrupted because of the below\n");
warnmsg("bad data offset %d at eraseblock %d (n"
"of multiple of min. I/O unit size %d)",
- si->data_offs, eb, mtd->min_io_size);
+ si->data_offs, eb, mtd->writesize);
warnmsg("treat eraseblock %d as corrupted", eb);
si->corrupted_cnt += 1;
si->ec[eb] = EB_CORRUPTED;
@@ -174,7 +176,7 @@ int libscan_ubi_scan(struct mtd_dev_info *mtd, int fd, struct ubi_scan_info **in
if (si->ok_cnt != 0) {
/* Calculate mean erase counter */
- for (eb = 0; eb < mtd->eb_cnt; eb++) {
+ for (eb = 0; eb < eb_cnt; eb++) {
if (si->ec[eb] > EC_MAX)
continue;
sum += si->ec[eb];
@@ -183,7 +185,7 @@ int libscan_ubi_scan(struct mtd_dev_info *mtd, int fd, struct ubi_scan_info **in
si->mean_ec = sum;
}
- si->good_cnt = mtd->eb_cnt - si->bad_cnt;
+ si->good_cnt = eb_cnt - si->bad_cnt;
verbose(v, "finished, mean EC %lld, %d OK, %d corrupted, %d empty, %d "
"alien, bad %d", si->mean_ec, si->ok_cnt, si->corrupted_cnt,
si->empty_cnt, si->alien_cnt, si->bad_cnt);
diff --git a/lib/libubigen.c b/lib/libubigen.c
index 900632905b..77ebb053c2 100644
--- a/lib/libubigen.c
+++ b/lib/libubigen.c
@@ -34,6 +34,7 @@
#include <mtd/utils.h>
#include <mtd/ubi-media.h>
#include <mtd/libubigen.h>
+#include <mtd/mtd-peb.h>
void ubigen_info_init(struct ubigen_info *ui, int peb_size, int min_io_size,
int subpage_size, int vid_hdr_offs, int ubi_ver,
@@ -247,13 +248,12 @@ out_free:
int ubigen_write_layout_vol(const struct ubigen_info *ui, int peb1, int peb2,
long long ec1, long long ec2,
- struct ubi_vtbl_record *vtbl, int fd)
+ struct ubi_vtbl_record *vtbl, struct mtd_info *mtd)
{
int ret;
struct ubigen_vol_info vi;
char *outbuf;
struct ubi_vid_hdr *vid_hdr;
- off_t seek;
vi.bytes = ui->leb_size * UBI_LAYOUT_VOLUME_EBS;
vi.id = UBI_LAYOUT_VOLUME_ID;
@@ -277,29 +277,18 @@ int ubigen_write_layout_vol(const struct ubigen_info *ui, int peb1, int peb2,
memset(outbuf + ui->data_offs + ui->vtbl_size, 0xFF,
ui->peb_size - ui->data_offs - ui->vtbl_size);
- seek = (off_t) peb1 * ui->peb_size;
- if (lseek(fd, seek, SEEK_SET) != seek) {
- sys_errmsg("cannot seek output file");
- goto out_free;
- }
-
ubigen_init_ec_hdr(ui, (struct ubi_ec_hdr *)outbuf, ec1);
ubigen_init_vid_hdr(ui, &vi, vid_hdr, 0, NULL, 0);
- ret = write(fd, outbuf, ui->peb_size);
- if (ret != ui->peb_size) {
+ ret = mtd_peb_write(mtd, outbuf, peb1, 0, ui->peb_size);
+ if (ret < 0) {
sys_errmsg("cannot write %d bytes", ui->peb_size);
goto out_free;
}
- seek = (off_t) peb2 * ui->peb_size;
- if (lseek(fd, seek, SEEK_SET) != seek) {
- sys_errmsg("cannot seek output file");
- goto out_free;
- }
ubigen_init_ec_hdr(ui, (struct ubi_ec_hdr *)outbuf, ec2);
ubigen_init_vid_hdr(ui, &vi, vid_hdr, 1, NULL, 0);
- ret = write(fd, outbuf, ui->peb_size);
- if (ret != ui->peb_size) {
+ ret = mtd_peb_write(mtd, outbuf, peb2, 0, ui->peb_size);
+ if (ret < 0) {
sys_errmsg("cannot write %d bytes", ui->peb_size);
goto out_free;
}