2013-01-09 17:21:24 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 Nokia Corporation
|
|
|
|
* Copyright (C) 2012 Wolfram Sang, Pengutronix
|
|
|
|
*
|
|
|
|
* 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; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* An utility to format MTD devices into UBI and flash UBI images.
|
|
|
|
*
|
|
|
|
* Author: Artem Bityutskiy
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Maximum amount of consequtive eraseblocks which are considered as normal by
|
|
|
|
* this utility. Otherwise it is assume that something is wrong with the flash
|
|
|
|
* or the driver, and eraseblocks are stopped being marked as bad.
|
|
|
|
*/
|
|
|
|
#define MAX_CONSECUTIVE_BAD_BLOCKS 4
|
|
|
|
|
2014-06-17 19:38:09 +00:00
|
|
|
#define PROGRAM_NAME "ubiformat"
|
2013-01-09 17:21:24 +00:00
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <command.h>
|
|
|
|
#include <fs.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <crc.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <clock.h>
|
|
|
|
#include <malloc.h>
|
|
|
|
#include <ioctl.h>
|
|
|
|
#include <libbb.h>
|
2014-07-30 08:43:51 +00:00
|
|
|
#include <libfile.h>
|
2016-04-22 06:47:15 +00:00
|
|
|
#include <ubiformat.h>
|
2013-01-09 17:21:24 +00:00
|
|
|
#include <linux/mtd/mtd.h>
|
2016-03-08 10:04:10 +00:00
|
|
|
#include <linux/mtd/ubi.h>
|
2013-01-09 17:21:24 +00:00
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/stat.h>
|
|
|
|
#include <linux/log2.h>
|
|
|
|
#include <linux/mtd/mtd-abi.h>
|
|
|
|
#include <mtd/libscan.h>
|
|
|
|
#include <mtd/libubigen.h>
|
|
|
|
#include <mtd/ubi-user.h>
|
|
|
|
#include <mtd/utils.h>
|
|
|
|
#include <mtd/ubi-media.h>
|
2016-02-26 09:31:03 +00:00
|
|
|
#include <mtd/mtd-peb.h>
|
2013-01-09 17:21:24 +00:00
|
|
|
|
2016-04-22 06:36:10 +00:00
|
|
|
static int parse_opt(int argc, char *argv[], struct ubiformat_args *args,
|
|
|
|
char **node)
|
2013-01-09 17:21:24 +00:00
|
|
|
{
|
|
|
|
srand(get_time_ns());
|
2016-04-22 06:21:10 +00:00
|
|
|
memset(args, 0, sizeof(*args));
|
2013-01-09 17:21:24 +00:00
|
|
|
|
|
|
|
while (1) {
|
|
|
|
int key;
|
|
|
|
unsigned long int image_seq;
|
|
|
|
|
2015-05-19 13:29:21 +00:00
|
|
|
key = getopt(argc, argv, "nyqve:x:s:O:f:S:Q:");
|
2013-01-09 17:21:24 +00:00
|
|
|
if (key == -1)
|
|
|
|
break;
|
|
|
|
|
|
|
|
switch (key) {
|
|
|
|
case 's':
|
2016-04-22 06:21:10 +00:00
|
|
|
args->subpage_size = strtoull_suffix(optarg, NULL, 0);
|
|
|
|
if (args->subpage_size <= 0)
|
2013-01-09 17:21:24 +00:00
|
|
|
return errmsg("bad sub-page size: \"%s\"", optarg);
|
2016-04-22 06:21:10 +00:00
|
|
|
if (!is_power_of_2(args->subpage_size))
|
2013-01-09 17:21:24 +00:00
|
|
|
return errmsg("sub-page size should be power of 2");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'O':
|
2016-04-22 06:21:10 +00:00
|
|
|
args->vid_hdr_offs = simple_strtoul(optarg, NULL, 0);
|
|
|
|
if (args->vid_hdr_offs <= 0)
|
2013-01-09 17:21:24 +00:00
|
|
|
return errmsg("bad VID header offset: \"%s\"", optarg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'e':
|
2016-04-22 06:21:10 +00:00
|
|
|
args->ec = simple_strtoull(optarg, NULL, 0);
|
|
|
|
if (args->ec < 0)
|
2013-01-09 17:21:24 +00:00
|
|
|
return errmsg("bad erase counter value: \"%s\"", optarg);
|
2016-04-22 06:21:10 +00:00
|
|
|
if (args->ec >= EC_MAX)
|
|
|
|
return errmsg("too high erase %llu, counter, max is %u", args->ec, EC_MAX);
|
|
|
|
args->override_ec = 1;
|
2013-01-09 17:21:24 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'f':
|
2016-04-22 06:21:10 +00:00
|
|
|
args->image = optarg;
|
2013-01-09 17:21:24 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'n':
|
2016-04-22 06:21:10 +00:00
|
|
|
args->novtbl = 1;
|
2013-01-09 17:21:24 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'y':
|
2016-04-22 06:21:10 +00:00
|
|
|
args->yes = 1;
|
2013-01-09 17:21:24 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'q':
|
2016-04-22 06:21:10 +00:00
|
|
|
args->quiet = 1;
|
2013-01-09 17:21:24 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'x':
|
2016-04-22 06:21:10 +00:00
|
|
|
args->ubi_ver = simple_strtoul(optarg, NULL, 0);
|
|
|
|
if (args->ubi_ver < 0)
|
2013-01-09 17:21:24 +00:00
|
|
|
return errmsg("bad UBI version: \"%s\"", optarg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'Q':
|
|
|
|
image_seq = simple_strtoul(optarg, NULL, 0);
|
|
|
|
if (image_seq > 0xFFFFFFFF)
|
|
|
|
return errmsg("bad UBI image sequence number: \"%s\"", optarg);
|
2016-04-22 06:21:10 +00:00
|
|
|
args->image_seq = image_seq;
|
2013-01-09 17:21:24 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'v':
|
2016-04-22 06:21:10 +00:00
|
|
|
args->verbose = 1;
|
2013-01-09 17:21:24 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return COMMAND_ERROR_USAGE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-22 06:21:10 +00:00
|
|
|
if (args->quiet && args->verbose)
|
2013-01-09 17:21:24 +00:00
|
|
|
return errmsg("using \"-q\" and \"-v\" at the same time does not make sense");
|
|
|
|
|
|
|
|
if (optind == argc)
|
|
|
|
return errmsg("MTD device name was not specified");
|
|
|
|
else if (optind != argc - 1)
|
|
|
|
return errmsg("more then one MTD device specified");
|
|
|
|
|
2016-04-22 06:21:10 +00:00
|
|
|
if (args->image && args->novtbl)
|
2013-01-09 17:21:24 +00:00
|
|
|
return errmsg("-n cannot be used together with -f");
|
|
|
|
|
|
|
|
|
2016-04-22 06:36:10 +00:00
|
|
|
*node = argv[optind];
|
|
|
|
|
2013-01-09 17:21:24 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-04-22 06:47:15 +00:00
|
|
|
static int do_ubiformat(int argc, char *argv[])
|
2016-04-22 06:36:10 +00:00
|
|
|
{
|
|
|
|
int err, fd;
|
|
|
|
struct ubiformat_args args;
|
|
|
|
char *node = NULL;
|
|
|
|
struct mtd_info_user mtd_user;
|
|
|
|
|
|
|
|
err = parse_opt(argc, argv, &args, &node);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
fd = open(node, O_RDWR);
|
|
|
|
if (fd < 0)
|
|
|
|
return sys_errmsg("cannot open \"%s\"", node);
|
|
|
|
|
|
|
|
err = ioctl(fd, MEMGETINFO, &mtd_user);
|
|
|
|
if (err) {
|
|
|
|
sys_errmsg("MEMGETINFO ioctl request failed");
|
|
|
|
goto out_close;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ubiformat(mtd_user.mtd, &args);
|
|
|
|
|
2013-01-09 17:21:24 +00:00
|
|
|
out_close:
|
2016-02-26 09:31:03 +00:00
|
|
|
close(fd);
|
|
|
|
|
2016-04-22 06:36:10 +00:00
|
|
|
return err;
|
2013-01-09 17:21:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BAREBOX_CMD_HELP_START(ubiformat)
|
commands: harmonize in-barebox documentation
This patch does probably too much, but it's hard (and very
cumbersome/time consuming) to break it out. What is does is this:
* each command has one short description, e.g. "list MUX configuration"
* made sure the short descriptions start lowercase
* each command has one usage. That string contains just the
options, e.g. "[-npn]". It's not part of the long help text.
* that is, it doesn't say "[OPTIONS]" anymore, every usable option
is listed by character in this (short) option string (the long
description is in the long help text, as before)
* help texts have been reworked, to make them
- sometimes smaller
- sometimes describe the options better
- more often present themselves in a nicer format
* all long help texts are now created with BUSYBOX_CMD_HELP_
macros, no more 'static const __maybe_unused char cmd_foobar_help[]'
* made sure the long help texts starts uppercase
* because cmdtp->name and cmdtp->opts together provide the new usage,
all "Usage: foobar" texts have been removed from the long help texts
* BUSYBOX_CMD_HELP_TEXT() provides the trailing newline by itself, this
is nicer in the source code
* BUSYBOX_CMD_HELP_OPT() provides the trailing newline by itself
* made sure no line gets longer than 77 characters
* delibertely renamed cmdtp->usage, so that we can get compile-time
errors (e.g. in out-of-tree modules that use register_command()
* the 'help' command can now always emit the usage, even without
compiled long help texts
* 'help -v' gives a list of commands with their short description, this
is similar like the old "help" command before my patchset
* 'help -a' gives out help of all commands
Signed-off-by: Holger Schurig <holgerschurig@gmail.com>
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
2014-05-13 08:28:42 +00:00
|
|
|
BAREBOX_CMD_HELP_TEXT("A tool to format MTD devices and flash UBI images")
|
|
|
|
BAREBOX_CMD_HELP_TEXT("")
|
|
|
|
BAREBOX_CMD_HELP_TEXT("Options:")
|
|
|
|
BAREBOX_CMD_HELP_OPT("-s BYTES", "minimum input/output unit used for UBI headers")
|
|
|
|
BAREBOX_CMD_HELP_OPT("\t", "e.g. sub-page size in case of NAND flash")
|
|
|
|
BAREBOX_CMD_HELP_OPT("-O OFFS\t", "offset if the VID header from start of the")
|
|
|
|
BAREBOX_CMD_HELP_OPT("\t", "physical eraseblock (default is the next minimum I/O unit or")
|
|
|
|
BAREBOX_CMD_HELP_OPT("\t", "sub-page after the EC header)")
|
|
|
|
BAREBOX_CMD_HELP_OPT("-n\t", "only erase all eraseblock and preserve erase")
|
|
|
|
BAREBOX_CMD_HELP_OPT("\t", "counters, do not write empty volume table")
|
|
|
|
BAREBOX_CMD_HELP_OPT("-f FILE\t", "flash image file")
|
|
|
|
BAREBOX_CMD_HELP_OPT("-e VALUE", "use VALUE as erase counter value for all eraseblocks")
|
|
|
|
BAREBOX_CMD_HELP_OPT("-x NUM\t", "UBI version number to put to EC headers (default 1)")
|
|
|
|
BAREBOX_CMD_HELP_OPT("-Q NUM\t", "32-bit UBI image sequence number to use")
|
2015-08-28 07:13:20 +00:00
|
|
|
BAREBOX_CMD_HELP_OPT("-y\t", "Assume yes for all questions")
|
commands: harmonize in-barebox documentation
This patch does probably too much, but it's hard (and very
cumbersome/time consuming) to break it out. What is does is this:
* each command has one short description, e.g. "list MUX configuration"
* made sure the short descriptions start lowercase
* each command has one usage. That string contains just the
options, e.g. "[-npn]". It's not part of the long help text.
* that is, it doesn't say "[OPTIONS]" anymore, every usable option
is listed by character in this (short) option string (the long
description is in the long help text, as before)
* help texts have been reworked, to make them
- sometimes smaller
- sometimes describe the options better
- more often present themselves in a nicer format
* all long help texts are now created with BUSYBOX_CMD_HELP_
macros, no more 'static const __maybe_unused char cmd_foobar_help[]'
* made sure the long help texts starts uppercase
* because cmdtp->name and cmdtp->opts together provide the new usage,
all "Usage: foobar" texts have been removed from the long help texts
* BUSYBOX_CMD_HELP_TEXT() provides the trailing newline by itself, this
is nicer in the source code
* BUSYBOX_CMD_HELP_OPT() provides the trailing newline by itself
* made sure no line gets longer than 77 characters
* delibertely renamed cmdtp->usage, so that we can get compile-time
errors (e.g. in out-of-tree modules that use register_command()
* the 'help' command can now always emit the usage, even without
compiled long help texts
* 'help -v' gives a list of commands with their short description, this
is similar like the old "help" command before my patchset
* 'help -a' gives out help of all commands
Signed-off-by: Holger Schurig <holgerschurig@gmail.com>
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
2014-05-13 08:28:42 +00:00
|
|
|
BAREBOX_CMD_HELP_OPT("-q\t", "suppress progress percentage information")
|
|
|
|
BAREBOX_CMD_HELP_OPT("-v\t", "be verbose")
|
|
|
|
BAREBOX_CMD_HELP_TEXT("")
|
2014-06-17 19:38:09 +00:00
|
|
|
BAREBOX_CMD_HELP_TEXT("Example 1: ubiformat /dev/nand0 -y - format nand0 and assume yes")
|
|
|
|
BAREBOX_CMD_HELP_TEXT("Example 2: ubiformat /dev/nand0 -q -e 0 - format nand0,")
|
commands: harmonize in-barebox documentation
This patch does probably too much, but it's hard (and very
cumbersome/time consuming) to break it out. What is does is this:
* each command has one short description, e.g. "list MUX configuration"
* made sure the short descriptions start lowercase
* each command has one usage. That string contains just the
options, e.g. "[-npn]". It's not part of the long help text.
* that is, it doesn't say "[OPTIONS]" anymore, every usable option
is listed by character in this (short) option string (the long
description is in the long help text, as before)
* help texts have been reworked, to make them
- sometimes smaller
- sometimes describe the options better
- more often present themselves in a nicer format
* all long help texts are now created with BUSYBOX_CMD_HELP_
macros, no more 'static const __maybe_unused char cmd_foobar_help[]'
* made sure the long help texts starts uppercase
* because cmdtp->name and cmdtp->opts together provide the new usage,
all "Usage: foobar" texts have been removed from the long help texts
* BUSYBOX_CMD_HELP_TEXT() provides the trailing newline by itself, this
is nicer in the source code
* BUSYBOX_CMD_HELP_OPT() provides the trailing newline by itself
* made sure no line gets longer than 77 characters
* delibertely renamed cmdtp->usage, so that we can get compile-time
errors (e.g. in out-of-tree modules that use register_command()
* the 'help' command can now always emit the usage, even without
compiled long help texts
* 'help -v' gives a list of commands with their short description, this
is similar like the old "help" command before my patchset
* 'help -a' gives out help of all commands
Signed-off-by: Holger Schurig <holgerschurig@gmail.com>
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
2014-05-13 08:28:42 +00:00
|
|
|
BAREBOX_CMD_HELP_TEXT("\tbe quiet and force erase counter value 0.")
|
2013-01-09 17:21:24 +00:00
|
|
|
BAREBOX_CMD_HELP_END
|
|
|
|
|
|
|
|
BAREBOX_CMD_START(ubiformat)
|
|
|
|
.cmd = do_ubiformat,
|
commands: harmonize in-barebox documentation
This patch does probably too much, but it's hard (and very
cumbersome/time consuming) to break it out. What is does is this:
* each command has one short description, e.g. "list MUX configuration"
* made sure the short descriptions start lowercase
* each command has one usage. That string contains just the
options, e.g. "[-npn]". It's not part of the long help text.
* that is, it doesn't say "[OPTIONS]" anymore, every usable option
is listed by character in this (short) option string (the long
description is in the long help text, as before)
* help texts have been reworked, to make them
- sometimes smaller
- sometimes describe the options better
- more often present themselves in a nicer format
* all long help texts are now created with BUSYBOX_CMD_HELP_
macros, no more 'static const __maybe_unused char cmd_foobar_help[]'
* made sure the long help texts starts uppercase
* because cmdtp->name and cmdtp->opts together provide the new usage,
all "Usage: foobar" texts have been removed from the long help texts
* BUSYBOX_CMD_HELP_TEXT() provides the trailing newline by itself, this
is nicer in the source code
* BUSYBOX_CMD_HELP_OPT() provides the trailing newline by itself
* made sure no line gets longer than 77 characters
* delibertely renamed cmdtp->usage, so that we can get compile-time
errors (e.g. in out-of-tree modules that use register_command()
* the 'help' command can now always emit the usage, even without
compiled long help texts
* 'help -v' gives a list of commands with their short description, this
is similar like the old "help" command before my patchset
* 'help -a' gives out help of all commands
Signed-off-by: Holger Schurig <holgerschurig@gmail.com>
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
2014-05-13 08:28:42 +00:00
|
|
|
BAREBOX_CMD_DESC("format an ubi volume")
|
|
|
|
BAREBOX_CMD_OPTS("[-sOnfexQqv] MTDEVICE")
|
commands: group 'help' output
The old output of "help" was just producing a long list, that usually
scrolled of the screen (even on a X11 terminal). This list is more
compact, and also sorted by groups.
The old output format (plus grouping) is now available with 'help -v'.
Example:
Information commands:
?, devinfo, help, iomem, meminfo, version
Boot commands:
boot, bootm, go, loadb, loads, loadx, loady, saves, uimage
...
Signed-off-by: Holger Schurig <holgerschurig@gmail.com>
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
2014-05-13 14:14:05 +00:00
|
|
|
BAREBOX_CMD_GROUP(CMD_GRP_PART)
|
2013-01-09 17:21:24 +00:00
|
|
|
BAREBOX_CMD_HELP(cmd_ubiformat_help)
|
|
|
|
BAREBOX_CMD_END
|