summaryrefslogtreecommitdiffstats
path: root/commands
diff options
context:
space:
mode:
authorSascha Hauer <s.hauer@pengutronix.de>2016-04-08 13:37:28 +0200
committerSascha Hauer <s.hauer@pengutronix.de>2016-04-08 13:37:28 +0200
commitfd50a8d758cb79a6ca833e82e6d533a3bbcc247d (patch)
treede862ee99138ea54e97234ec4a1ae172e87487de /commands
parent0d3f88a6dbe59bced2530e69d241f569be58cf3c (diff)
parent60f2c23684797173169a940abf9f1985537c156e (diff)
downloadbarebox-fd50a8d758cb79a6ca833e82e6d533a3bbcc247d.tar.gz
barebox-fd50a8d758cb79a6ca833e82e6d533a3bbcc247d.tar.xz
Merge branch 'for-next/imx-bbu-nand-fcb'
Diffstat (limited to 'commands')
-rw-r--r--commands/Kconfig18
-rw-r--r--commands/Makefile1
-rw-r--r--commands/barebox-update.c26
-rw-r--r--commands/nand-bitflip.c117
-rw-r--r--commands/nand.c79
-rw-r--r--commands/ubiformat.c191
6 files changed, 317 insertions, 115 deletions
diff --git a/commands/Kconfig b/commands/Kconfig
index 9519a44cfc..875c5f4f01 100644
--- a/commands/Kconfig
+++ b/commands/Kconfig
@@ -694,7 +694,6 @@ config CMD_UBI
config CMD_UBIFORMAT
tristate
depends on MTD_UBI
- select LIBMTD
select LIBSCAN
select LIBUBIGEN
prompt "ubiformat"
@@ -1909,6 +1908,23 @@ config CMD_NANDTEST
-o OFFS start offset on flash
-l LEN length of flash to test
+config CMD_NAND_BITFLIP
+ tristate
+ depends on NAND
+ prompt "nand_bitflip"
+ help
+ nand_bitflip - Create bitflips on Nand pages. This command is useful for testing
+ purposes.
+
+ Usage: nand_bitflip NANDDEV
+
+ This command creates bitflips on Nand pages.
+ Options:
+ -b <block> block to work on
+ -o <offset> offset in Nand
+ -r flip random bits
+ -n <numbitflips> Specify maximum number of bitflips to generate
+
config CMD_POWEROFF
tristate
depends on HAS_POWEROFF
diff --git a/commands/Makefile b/commands/Makefile
index 8975d4bd40..f1b482f049 100644
--- a/commands/Makefile
+++ b/commands/Makefile
@@ -116,3 +116,4 @@ obj-$(CONFIG_CMD_DHCP) += dhcp.o
obj-$(CONFIG_CMD_DHRYSTONE) += dhrystone.o
obj-$(CONFIG_CMD_SPD_DECODE) += spd_decode.o
obj-$(CONFIG_CMD_MMC_EXTCSD) += mmc_extcsd.o
+obj-$(CONFIG_CMD_NAND_BITFLIP) += nand-bitflip.o
diff --git a/commands/barebox-update.c b/commands/barebox-update.c
index 92e0efab6a..c2f2b68e08 100644
--- a/commands/barebox-update.c
+++ b/commands/barebox-update.c
@@ -26,10 +26,10 @@
static int do_barebox_update(int argc, char *argv[])
{
- int opt, ret;
+ int opt, ret, repair = 0;
struct bbu_data data = {};
- while ((opt = getopt(argc, argv, "t:yf:ld:")) > 0) {
+ while ((opt = getopt(argc, argv, "t:yf:ld:r")) > 0) {
switch (opt) {
case 'd':
data.devicefile = optarg;
@@ -48,19 +48,24 @@ static int do_barebox_update(int argc, char *argv[])
printf("registered update handlers:\n");
bbu_handlers_list();
return 0;
+ case 'r':
+ repair = 1;
+ break;
default:
return COMMAND_ERROR_USAGE;
}
}
- if (!(argc - optind))
- return COMMAND_ERROR_USAGE;
-
- data.imagefile = argv[optind];
+ if (argc - optind > 0) {
+ data.imagefile = argv[optind];
- data.image = read_file(data.imagefile, &data.len);
- if (!data.image)
- return -errno;
+ data.image = read_file(data.imagefile, &data.len);
+ if (!data.image)
+ return -errno;
+ } else {
+ if (!repair)
+ return COMMAND_ERROR_USAGE;
+ }
ret = barebox_update(&data);
@@ -74,6 +79,7 @@ BAREBOX_CMD_HELP_TEXT("Options:")
BAREBOX_CMD_HELP_OPT("-l\t", "list registered targets")
BAREBOX_CMD_HELP_OPT("-t TARGET", "specify data target handler name")
BAREBOX_CMD_HELP_OPT("-d DEVICE", "write image to DEVICE")
+BAREBOX_CMD_HELP_OPT("-r\t", "refresh or repair. Do not update, but repair an existing image")
BAREBOX_CMD_HELP_OPT("-y\t", "autom. use 'yes' when asking confirmations")
BAREBOX_CMD_HELP_OPT("-f LEVEL", "set force level")
BAREBOX_CMD_HELP_END
@@ -81,7 +87,7 @@ BAREBOX_CMD_HELP_END
BAREBOX_CMD_START(barebox_update)
.cmd = do_barebox_update,
BAREBOX_CMD_DESC("update barebox to persistent media")
- BAREBOX_CMD_OPTS("[-ltdyf] [IMAGE]")
+ BAREBOX_CMD_OPTS("[-ltdyfr] [IMAGE]")
BAREBOX_CMD_GROUP(CMD_GRP_MISC)
BAREBOX_CMD_HELP(cmd_barebox_update_help)
BAREBOX_CMD_END
diff --git a/commands/nand-bitflip.c b/commands/nand-bitflip.c
new file mode 100644
index 0000000000..fe56f222cf
--- /dev/null
+++ b/commands/nand-bitflip.c
@@ -0,0 +1,117 @@
+/*
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; version 2.
+ *
+ * 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.
+ */
+
+#include <common.h>
+#include <command.h>
+#include <malloc.h>
+#include <getopt.h>
+#include <fs.h>
+#include <fcntl.h>
+#include <ioctl.h>
+#include <linux/mtd/mtd.h>
+#include <mtd/mtd-peb.h>
+
+static int do_nand_bitflip(int argc, char *argv[])
+{
+ int opt, ret, fd;
+ static struct mtd_info_user meminfo;
+ int block = 0;
+ int random = 0;
+ int num_bitflips = 1;
+ loff_t offset = 0, roffset;
+ int check = 0;
+ size_t r;
+ void *buf;
+
+ while ((opt = getopt(argc, argv, "b:rn:o:c")) > 0) {
+ switch (opt) {
+ case 'r':
+ random = 1;
+ break;
+ case 'n':
+ num_bitflips = simple_strtoul(optarg, NULL, 0);
+ break;
+ case 'b':
+ block = simple_strtoul(optarg, NULL, 0);
+ break;
+ case 'o':
+ offset = simple_strtoull(optarg, NULL, 0);
+ break;
+ case 'c':
+ check = 1;
+ break;
+ default:
+ return COMMAND_ERROR_USAGE;
+ }
+ }
+
+ if (optind >= argc)
+ return COMMAND_ERROR_USAGE;
+
+ fd = open(argv[optind], O_RDWR);
+ if (fd < 0)
+ return fd;
+
+ ret = ioctl(fd, MEMGETINFO, &meminfo);
+
+ close(fd);
+
+ if (ret)
+ return ret;
+
+ block += mtd_div_by_eb(offset, meminfo.mtd);
+ offset = mtd_mod_by_eb(offset, meminfo.mtd);
+
+ if (!check) {
+ ret = mtd_peb_create_bitflips(meminfo.mtd, block, offset, meminfo.writesize,
+ num_bitflips, random, 1);
+ if (ret) {
+ printf("Creating bitflips failed with: %s\n", strerror(-ret));
+ return ret;
+ }
+ }
+
+ buf = xzalloc(meminfo.writesize);
+
+ roffset = (loff_t)block * meminfo.mtd->erasesize + offset;
+ ret = meminfo.mtd->read(meminfo.mtd, roffset, meminfo.writesize, &r, buf);
+ if (ret > 0) {
+ printf("page at block %d, offset 0x%08llx has %d bitflips%s\n",
+ block, offset, ret,
+ ret >= meminfo.mtd->bitflip_threshold ? ", needs cleanup" : "");
+ } else if (!ret) {
+ printf("No bitflips found on block %d, offset 0x%08llx\n", block, offset);
+ } else {
+ printf("Reading block %d, offset 0x%08llx failed with: %s\n", block, offset,
+ strerror(-ret));
+ }
+
+ free(buf);
+
+ return 0;
+}
+
+BAREBOX_CMD_HELP_START(nand_bitflip)
+BAREBOX_CMD_HELP_TEXT("This command creates bitflips on Nand pages.")
+BAREBOX_CMD_HELP_TEXT("Options:")
+BAREBOX_CMD_HELP_OPT ("-b <block>", "block to work on")
+BAREBOX_CMD_HELP_OPT ("-o <offset>", "offset in Nand")
+BAREBOX_CMD_HELP_OPT ("-r\t", "flip random bits")
+BAREBOX_CMD_HELP_OPT ("-n <numbitflips>", "Specify maximum number of bitflips to generate")
+BAREBOX_CMD_HELP_END
+
+BAREBOX_CMD_START(nand_bitflip)
+ .cmd = do_nand_bitflip,
+ BAREBOX_CMD_DESC("Create bitflips on Nand pages")
+ BAREBOX_CMD_OPTS("NANDDEV")
+ BAREBOX_CMD_GROUP(CMD_GRP_HWMANIP)
+ BAREBOX_CMD_HELP(cmd_nand_bitflip_help)
+BAREBOX_CMD_END
diff --git a/commands/nand.c b/commands/nand.c
index c330ad1dc4..b065a661c0 100644
--- a/commands/nand.c
+++ b/commands/nand.c
@@ -32,14 +32,19 @@
#define NAND_ADD 1
#define NAND_DEL 2
#define NAND_MARKBAD 3
+#define NAND_MARKGOOD 4
+#define NAND_INFO 5
static int do_nand(int argc, char *argv[])
{
int opt;
int command = 0;
loff_t badblock = 0;
+ int fd;
+ int ret;
+ struct mtd_info_user mtdinfo;
- while((opt = getopt(argc, argv, "adb:")) > 0) {
+ while((opt = getopt(argc, argv, "adb:g:i")) > 0) {
if (command) {
printf("only one command may be given\n");
return 1;
@@ -55,12 +60,27 @@ static int do_nand(int argc, char *argv[])
case 'b':
command = NAND_MARKBAD;
badblock = strtoull_suffix(optarg, NULL, 0);
+ break;
+ case 'g':
+ command = NAND_MARKGOOD;
+ badblock = strtoull_suffix(optarg, NULL, 0);
+ break;
+ case 'i':
+ command = NAND_INFO;
+ break;
+ default:
+ return COMMAND_ERROR_USAGE;
}
}
if (optind >= argc)
return COMMAND_ERROR_USAGE;
+ if (!command) {
+ printf("No action given\n");
+ return COMMAND_ERROR_USAGE;
+ }
+
if (command == NAND_ADD) {
while (optind < argc) {
if (dev_add_bb_dev(basename(argv[optind]), NULL))
@@ -77,19 +97,32 @@ static int do_nand(int argc, char *argv[])
}
}
- if (command == NAND_MARKBAD) {
- int ret = 0, fd;
+ fd = open(argv[optind], O_RDWR);
+ if (fd < 0) {
+ perror("open");
+ return 1;
+ }
- printf("marking block at 0x%08llx on %s as bad\n",
- badblock, argv[optind]);
+ ret = ioctl(fd, MEMGETINFO, &mtdinfo);
+ if (ret)
+ goto out;
- fd = open(argv[optind], O_RDWR);
- if (fd < 0) {
- perror("open");
- return 1;
+ if (command == NAND_MARKBAD || command == NAND_MARKGOOD) {
+ const char *str;
+ int ctl;
+
+ if (command == NAND_MARKBAD) {
+ str = "bad";
+ ctl = MEMSETBADBLOCK;
+ } else {
+ str = "good";
+ ctl = MEMSETGOODBLOCK;
}
- ret = ioctl(fd, MEMSETBADBLOCK, &badblock);
+ printf("marking block at 0x%08llx on %s as %s\n",
+ badblock, argv[optind], str);
+
+ ret = ioctl(fd, ctl, &badblock);
if (ret) {
if (ret == -EINVAL)
printf("Maybe offset %lld is out of range.\n",
@@ -98,11 +131,29 @@ static int do_nand(int argc, char *argv[])
perror("ioctl");
}
- close(fd);
- return ret;
+ goto out;
+ }
+
+ if (command == NAND_INFO) {
+ loff_t ofs;
+ int bad = 0;
+
+ for (ofs = 0; ofs < mtdinfo.size; ofs += mtdinfo.erasesize) {
+ if (ioctl(fd, MEMGETBADBLOCK, &ofs)) {
+ printf("Block at 0x%08llx is bad\n", ofs);
+ bad = 1;
+ }
+ }
+
+ if (!bad)
+ printf("No bad blocks\n");
}
- return 0;
+ ret = 0;
+out:
+ close(fd);
+
+ return ret;
}
BAREBOX_CMD_HELP_START(nand)
@@ -110,6 +161,8 @@ BAREBOX_CMD_HELP_TEXT("Options:")
BAREBOX_CMD_HELP_OPT ("-a", "register a bad block aware device ontop of a normal NAND device")
BAREBOX_CMD_HELP_OPT ("-d", "deregister a bad block aware device")
BAREBOX_CMD_HELP_OPT ("-b OFFS", "mark block at OFFSet as bad")
+BAREBOX_CMD_HELP_OPT ("-g OFFS", "mark block at OFFSet as good")
+BAREBOX_CMD_HELP_OPT ("-i", "info. Show information about bad blocks")
BAREBOX_CMD_HELP_END
BAREBOX_CMD_START(nand)
diff --git a/commands/ubiformat.c b/commands/ubiformat.c
index f9c50b7936..0172654231 100644
--- a/commands/ubiformat.c
+++ b/commands/ubiformat.c
@@ -46,12 +46,12 @@
#include <linux/stat.h>
#include <linux/log2.h>
#include <linux/mtd/mtd-abi.h>
-#include <mtd/libmtd.h>
#include <mtd/libscan.h>
#include <mtd/libubigen.h>
#include <mtd/ubi-user.h>
#include <mtd/utils.h>
#include <mtd/ubi-media.h>
+#include <mtd/mtd-peb.h>
/* The variables below are set by command line arguments */
struct args {
@@ -68,7 +68,6 @@ struct args {
long long ec;
const char *image;
const char *node;
- int node_fd;
};
static struct args args;
@@ -166,16 +165,18 @@ static int parse_opt(int argc, char *argv[])
return 0;
}
-static void print_bad_eraseblocks(const struct mtd_dev_info *mtd,
+static void print_bad_eraseblocks(struct mtd_info *mtd,
const struct ubi_scan_info *si)
{
- int first = 1, eb;
+ int first = 1, eb, eb_cnt;
+
+ eb_cnt = mtd_div_by_eb(mtd->size, mtd);
if (si->bad_cnt == 0)
return;
normsg_cont("%d bad eraseblocks found, numbers: ", si->bad_cnt);
- for (eb = 0; eb < mtd->eb_cnt; eb++) {
+ for (eb = 0; eb < eb_cnt; eb++) {
if (si->ec[eb] != EB_BAD)
continue;
if (first) {
@@ -210,7 +211,7 @@ static int change_ech(struct ubi_ec_hdr *hdr, uint32_t image_seq,
return 0;
}
-static int drop_ffs(const struct mtd_dev_info *mtd, const void *buf, int len)
+static int drop_ffs(struct mtd_info *mtd, const void *buf, int len)
{
int i;
@@ -220,8 +221,8 @@ static int drop_ffs(const struct mtd_dev_info *mtd, const void *buf, int len)
/* The resulting length must be aligned to the minimum flash I/O size */
len = i + 1;
- len = (len + mtd->min_io_size - 1) / mtd->min_io_size;
- len *= mtd->min_io_size;
+ len = (len + mtd->writesize - 1) / mtd->writesize;
+ len *= mtd->writesize;
return len;
}
@@ -271,20 +272,20 @@ static int consecutive_bad_check(int eb)
}
/* TODO: we should actually torture the PEB before marking it as bad */
-static int mark_bad(const struct mtd_dev_info *mtd, struct ubi_scan_info *si, int eb)
+static int mark_bad(struct mtd_info *mtd, struct ubi_scan_info *si, int eb)
{
int err;
if (!args.quiet)
normsg_cont("marking block %d bad\n", eb);
- if (!mtd->bb_allowed) {
+ if (!mtd_can_have_bb(mtd)) {
if (!args.quiet)
printf("\n");
return errmsg("bad blocks not supported by this flash");
}
- err = mtd_mark_bad(mtd, args.node_fd, eb);
+ err = mtd_peb_mark_bad(mtd, eb);
if (err)
return err;
@@ -294,24 +295,26 @@ static int mark_bad(const struct mtd_dev_info *mtd, struct ubi_scan_info *si, in
return consecutive_bad_check(eb);
}
-static int flash_image(const struct mtd_dev_info *mtd,
+static int flash_image(struct mtd_info *mtd,
const struct ubigen_info *ui, struct ubi_scan_info *si)
{
- int fd, img_ebs, eb, written_ebs = 0, ret = -1;
+ int fd, img_ebs, eb, written_ebs = 0, ret = -1, eb_cnt;
off_t st_size;
char *buf = NULL;
+ eb_cnt = mtd_num_pebs(mtd);
+
fd = open_file(&st_size);
if (fd < 0)
return fd;
- buf = malloc(mtd->eb_size);
+ buf = malloc(mtd->erasesize);
if (!buf) {
- sys_errmsg("cannot allocate %d bytes of memory", mtd->eb_size);
+ sys_errmsg("cannot allocate %d bytes of memory", mtd->erasesize);
goto out_close;
}
- img_ebs = st_size / mtd->eb_size;
+ img_ebs = st_size / mtd->erasesize;
if (img_ebs > si->good_cnt) {
sys_errmsg("file \"%s\" is too large (%lld bytes)",
@@ -319,10 +322,10 @@ static int flash_image(const struct mtd_dev_info *mtd,
goto out_close;
}
- if (st_size % mtd->eb_size) {
+ if (st_size % mtd->erasesize) {
sys_errmsg("file \"%s\" (size %lld bytes) is not multiple of "
"eraseblock size (%d bytes)",
- args.image, (long long)st_size, mtd->eb_size);
+ args.image, (long long)st_size, mtd->erasesize);
goto out_close;
}
@@ -332,13 +335,13 @@ static int flash_image(const struct mtd_dev_info *mtd,
}
verbose(args.verbose, "will write %d eraseblocks", img_ebs);
- for (eb = 0; eb < mtd->eb_cnt; eb++) {
+ for (eb = 0; eb < eb_cnt; eb++) {
int err, new_len;
long long ec;
if (!args.quiet && !args.verbose) {
printf("\rubiformat: flashing eraseblock %d -- %2u %% complete ",
- eb, (eb + 1) * 100 / mtd->eb_cnt);
+ eb, (eb + 1) * 100 / eb_cnt);
}
if (si->ec[eb] == EB_BAD)
@@ -348,13 +351,13 @@ static int flash_image(const struct mtd_dev_info *mtd,
normsg_cont("eraseblock %d: erase", eb);
}
- err = libmtd_erase(mtd, args.node_fd, eb);
+ err = mtd_peb_erase(mtd, eb);
if (err) {
if (!args.quiet)
printf("\n");
sys_errmsg("failed to erase eraseblock %d", eb);
- if (errno != EIO)
+ if (err != EIO)
goto out_close;
if (mark_bad(mtd, si, eb))
@@ -363,7 +366,7 @@ static int flash_image(const struct mtd_dev_info *mtd,
continue;
}
- err = read_full(fd, buf, mtd->eb_size);
+ err = read_full(fd, buf, mtd->erasesize);
if (err < 0) {
sys_errmsg("failed to read eraseblock %d from \"%s\"",
written_ebs, args.image);
@@ -392,20 +395,21 @@ static int flash_image(const struct mtd_dev_info *mtd,
printf(", write data\n");
}
- new_len = drop_ffs(mtd, buf, mtd->eb_size);
+ new_len = drop_ffs(mtd, buf, mtd->erasesize);
- err = libmtd_write(mtd, args.node_fd, eb, 0, buf, new_len);
+ err = mtd_peb_write(mtd, buf, eb, 0, new_len);
if (err) {
sys_errmsg("cannot write eraseblock %d", eb);
- if (errno != EIO)
+ if (err != EIO)
+ goto out_close;
+
+ err = mtd_peb_torture(mtd, eb);
+ if (err < 0 && err != -EIO)
+ goto out_close;
+ if (err == -EIO && consecutive_bad_check(eb))
goto out_close;
- err = mtd_torture(mtd, args.node_fd, eb);
- if (err) {
- if (mark_bad(mtd, si, eb))
- goto out_close;
- }
continue;
}
if (++written_ebs >= img_ebs)
@@ -423,30 +427,32 @@ out_close:
return ret;
}
-static int format(const struct mtd_dev_info *mtd,
+static int format(struct mtd_info *mtd,
const struct ubigen_info *ui, struct ubi_scan_info *si,
- int start_eb, int novtbl)
+ int start_eb, int novtbl, int subpage_size)
{
- int eb, err, write_size;
+ int eb, err, write_size, eb_cnt;
struct ubi_ec_hdr *hdr;
struct ubi_vtbl_record *vtbl;
int eb1 = -1, eb2 = -1;
long long ec1 = -1, ec2 = -1;
- write_size = UBI_EC_HDR_SIZE + mtd->subpage_size - 1;
- write_size /= mtd->subpage_size;
- write_size *= mtd->subpage_size;
+ eb_cnt = mtd_num_pebs(mtd);
+
+ write_size = UBI_EC_HDR_SIZE + subpage_size - 1;
+ write_size /= subpage_size;
+ write_size *= subpage_size;
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++) {
+ for (eb = start_eb; eb < eb_cnt; eb++) {
long long ec;
if (!args.quiet && !args.verbose) {
printf("\rubiformat: formatting eraseblock %d -- %2u %% complete ",
- eb, (eb + 1 - start_eb) * 100 / (mtd->eb_cnt - start_eb));
+ eb, (eb + 1 - start_eb) * 100 / (eb_cnt - start_eb));
}
if (si->ec[eb] == EB_BAD)
@@ -464,13 +470,13 @@ static int format(const struct mtd_dev_info *mtd,
normsg_cont("eraseblock %d: erase", eb);
}
- err = libmtd_erase(mtd, args.node_fd, eb);
+ err = mtd_peb_erase(mtd, eb);
if (err) {
if (!args.quiet)
printf("\n");
sys_errmsg("failed to erase eraseblock %d", eb);
- if (errno != EIO)
+ if (err != EIO)
goto out_free;
if (mark_bad(mtd, si, eb))
@@ -495,7 +501,7 @@ static int format(const struct mtd_dev_info *mtd,
printf(", write EC %lld\n", ec);
}
- err = libmtd_write(mtd, args.node_fd, eb, 0, hdr, write_size);
+ err = mtd_peb_write(mtd, hdr, eb, 0, write_size);
if (err) {
if (!args.quiet && !args.verbose)
printf("\n");
@@ -503,16 +509,17 @@ static int format(const struct mtd_dev_info *mtd,
write_size, eb);
if (errno != EIO) {
- if (args.subpage_size != mtd->min_io_size)
+ if (args.subpage_size != mtd->writesize)
normsg("may be sub-page size is incorrect?");
goto out_free;
}
- err = mtd_torture(mtd, args.node_fd, eb);
- if (err) {
- if (mark_bad(mtd, si, eb))
- goto out_free;
- }
+ err = mtd_peb_torture(mtd, eb);
+ if (err < 0 && err != -EIO)
+ goto out_free;
+ if (err == -EIO && consecutive_bad_check(eb))
+ goto out_free;
+
continue;
}
@@ -532,8 +539,7 @@ static int format(const struct mtd_dev_info *mtd,
if (!vtbl)
goto out_free;
- err = ubigen_write_layout_vol(ui, eb1, eb2, ec1, ec2, vtbl,
- args.node_fd);
+ err = ubigen_write_layout_vol(ui, eb1, eb2, ec1, ec2, vtbl, mtd);
free(vtbl);
if (err) {
errmsg("cannot write layout volume");
@@ -551,78 +557,82 @@ out_free:
int do_ubiformat(int argc, char *argv[])
{
- int err, verbose;
- struct mtd_dev_info mtd;
+ int err, verbose, fd, eb_cnt;
+ struct mtd_info *mtd;
struct ubigen_info ui;
struct ubi_scan_info *si;
+ struct mtd_info_user mtd_user;
err = parse_opt(argc, argv);
if (err)
return err;
- err = mtd_get_dev_info(args.node, &mtd);
- if (err) {
- sys_errmsg("cannot get information about \"%s\"", args.node);
- goto out_close_mtd;
+ fd = open(args.node, O_RDWR);
+ if (fd < 0)
+ return sys_errmsg("cannot open \"%s\"", args.node);
+
+ if (ioctl(fd, MEMGETINFO, &mtd_user)) {
+ sys_errmsg("MEMGETINFO ioctl request failed");
+ goto out_close;
}
- if (!is_power_of_2(mtd.min_io_size)) {
+ mtd = mtd_user.mtd;
+
+ if (!is_power_of_2(mtd->writesize)) {
errmsg("min. I/O size is %d, but should be power of 2",
- mtd.min_io_size);
- goto out_close_mtd;
+ mtd->writesize);
+ goto out_close;
}
- if (args.subpage_size && args.subpage_size != mtd.subpage_size) {
- mtd.subpage_size = args.subpage_size;
- args.manual_subpage = 1;
+ if (args.subpage_size) {
+ if (args.subpage_size != mtd->writesize >> mtd->subpage_sft)
+ args.manual_subpage = 1;
+ } else {
+ args.subpage_size = mtd->writesize >> mtd->subpage_sft;
}
if (args.manual_subpage) {
/* Do some sanity check */
- if (args.subpage_size > mtd.min_io_size) {
+ if (args.subpage_size > mtd->writesize) {
errmsg("sub-page cannot be larger than min. I/O unit");
- goto out_close_mtd;
+ goto out_close;
}
- if (mtd.min_io_size % args.subpage_size) {
+ if (mtd->writesize % args.subpage_size) {
errmsg("min. I/O unit size should be multiple of "
"sub-page size");
- goto out_close_mtd;
+ goto out_close;
}
}
- args.node_fd = open(args.node, O_RDWR);
- if (args.node_fd < 0) {
- sys_errmsg("cannot open \"%s\"", args.node);
- goto out_close_mtd;
- }
-
/* Validate VID header offset if it was specified */
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;
}
- if (args.vid_hdr_offs + (int)UBI_VID_HDR_SIZE > mtd.eb_size) {
+ if (args.vid_hdr_offs + (int)UBI_VID_HDR_SIZE > mtd->erasesize) {
errmsg("bad VID header offset");
goto out_close;
}
}
- if (!mtd.writable) {
- errmsg("%s (%s) is a read-only device", mtd.node, args.node);
+ if (!(mtd->flags & MTD_WRITEABLE)) {
+ errmsg("%s is a read-only device", args.node);
goto out_close;
}
/* Make sure this MTD device is not attached to UBI */
/* FIXME! Find a proper way to do this in barebox! */
+ eb_cnt = mtd_div_by_eb(mtd->size, mtd);
+
if (!args.quiet) {
- normsg_cont("%s (%s), size %lld bytes (%s)", mtd.node, mtd.type_str,
- mtd.size, size_human_readable(mtd.size));
- printf(", %d eraseblocks of %d bytes (%s)", mtd.eb_cnt,
- mtd.eb_size, size_human_readable(mtd.eb_size));
- printf(", min. I/O size %d bytes\n", mtd.min_io_size);
+ normsg_cont("%s (%s), size %lld bytes (%s)", args.node, mtd_type_str(mtd),
+ mtd->size, size_human_readable(mtd->size));
+ printf(", %d eraseblocks of %d bytes (%s)", eb_cnt,
+ mtd->erasesize, size_human_readable(mtd->erasesize));
+ printf(", min. I/O size %d bytes\n", mtd->writesize);
}
if (args.quiet)
@@ -631,9 +641,9 @@ int do_ubiformat(int argc, char *argv[])
verbose = 2;
else
verbose = 1;
- err = libscan_ubi_scan(&mtd, args.node_fd, &si, verbose);
+ err = libscan_ubi_scan(mtd, &si, verbose);
if (err) {
- errmsg("failed to scan %s (%s)", mtd.node, args.node);
+ errmsg("failed to scan %s", args.node);
goto out_close;
}
@@ -644,7 +654,7 @@ int do_ubiformat(int argc, char *argv[])
if (si->good_cnt < 2 && (!args.novtbl || args.image)) {
errmsg("too few non-bad eraseblocks (%d) on %s",
- si->good_cnt, mtd.node);
+ si->good_cnt, args.node);
goto out_free;
}
@@ -656,7 +666,7 @@ int do_ubiformat(int argc, char *argv[])
normsg("%d eraseblocks are supposedly empty", si->empty_cnt);
if (si->corrupted_cnt)
normsg("%d corrupted erase counters", si->corrupted_cnt);
- print_bad_eraseblocks(&mtd, si);
+ print_bad_eraseblocks(mtd, si);
}
if (si->alien_cnt) {
@@ -717,7 +727,7 @@ int do_ubiformat(int argc, char *argv[])
if (!args.quiet && args.override_ec)
normsg("use erase counter %lld for all eraseblocks", args.ec);
- ubigen_info_init(&ui, mtd.eb_size, mtd.min_io_size, mtd.subpage_size,
+ ubigen_info_init(&ui, mtd->erasesize, mtd->writesize, args.subpage_size,
args.vid_hdr_offs, args.ubi_ver, args.image_seq);
if (si->vid_hdr_offs != -1 && ui.vid_hdr_offs != si->vid_hdr_offs) {
@@ -735,28 +745,27 @@ int do_ubiformat(int argc, char *argv[])
}
if (args.image) {
- err = flash_image(&mtd, &ui, si);
+ err = flash_image(mtd, &ui, si);
if (err < 0)
goto out_free;
- err = format(&mtd, &ui, si, err, 1);
+ err = format(mtd, &ui, si, err, 1, args.subpage_size);
if (err)
goto out_free;
} else {
- err = format(&mtd, &ui, si, 0, args.novtbl);
+ err = format(mtd, &ui, si, 0, args.novtbl, args.subpage_size);
if (err)
goto out_free;
}
libscan_ubi_scan_free(si);
- close(args.node_fd);
return 0;
out_free:
libscan_ubi_scan_free(si);
out_close:
- close(args.node_fd);
-out_close_mtd:
+ close(fd);
+
return 1;
}