9
0
Fork 0
barebox/commands/flash.c

172 lines
4.5 KiB
C
Raw Normal View History

2002-09-18 12:49:44 +00:00
/*
2007-10-16 17:07:21 +00:00
* erase, protect, unprotect - FLASH support
*
2002-09-18 12:49:44 +00:00
* (C) Copyright 2000
* Wolfgang Denk, DENX Software Engineering, wd@denx.de.
*
* See file CREDITS for list of people who contributed to this
* project.
*
* 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.
*
*/
#include <common.h>
#include <command.h>
2007-07-05 16:01:48 +00:00
#include <errno.h>
2007-07-05 16:01:55 +00:00
#include <getopt.h>
#include <fs.h>
#include <fcntl.h>
#include <linux/stat.h>
static int do_flerase(int argc, char *argv[])
2002-09-18 12:49:44 +00:00
{
int fd;
2007-07-05 16:01:55 +00:00
char *filename = NULL;
struct stat s;
loff_t start = 0, size = ~0;
int ret = 0;
2002-09-18 12:49:44 +00:00
if (argc == 1)
return COMMAND_ERROR_USAGE;
2007-07-05 16:01:55 +00:00
filename = argv[1];
2007-07-05 16:01:55 +00:00
if (stat(filename, &s)) {
2007-07-14 12:45:45 +00:00
printf("stat %s: %s\n", filename, errno_str());
2002-09-18 12:49:44 +00:00
return 1;
}
2007-07-05 16:01:39 +00:00
2007-07-05 16:01:55 +00:00
size = s.st_size;
fd = open(filename, O_WRONLY);
if (fd < 0) {
printf("open %s: %s\n", filename, errno_str());
2007-07-05 16:01:55 +00:00
return 1;
}
if (argc == 3 && parse_area_spec(argv[2], &start, &size)) {
printf("could not parse: %s\n", argv[optind]);
ret = 1;
goto out;
}
2007-07-05 16:01:55 +00:00
if (erase(fd, size, start)) {
perror("erase");
ret = 1;
}
out:
2007-07-05 16:01:55 +00:00
close(fd);
return ret;
2002-09-18 12:49:44 +00:00
}
BAREBOX_CMD_HELP_START(erase)
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("Erase the flash memory handled by DEVICE. Which AREA will be erased")
BAREBOX_CMD_HELP_TEXT("depends on the device: If the device represents the whole flash")
BAREBOX_CMD_HELP_TEXT("memory, the whole memory will be erased. If the device represents a")
BAREBOX_CMD_HELP_TEXT("partition on a main flash memory, only this partition part will be")
BAREBOX_CMD_HELP_TEXT("erased.")
BAREBOX_CMD_HELP_TEXT("")
BAREBOX_CMD_HELP_TEXT("Use 'addpart' and 'delpart' to manage partitions.")
BAREBOX_CMD_HELP_END
BAREBOX_CMD_START(erase)
.cmd = do_flerase,
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("erase flash memory")
BAREBOX_CMD_OPTS("DEVICE [AREA]")
BAREBOX_CMD_GROUP(CMD_GRP_HWMANIP)
BAREBOX_CMD_HELP(cmd_erase_help)
BAREBOX_CMD_END
static int do_protect(int argc, char *argv[])
2002-09-18 12:49:44 +00:00
{
int fd;
2007-07-16 08:29:28 +00:00
char *filename = NULL;
struct stat s;
int prot = 1;
loff_t start = 0, size = ~0;
int ret = 0, err;
2007-07-16 08:29:28 +00:00
if (argc == 1)
return COMMAND_ERROR_USAGE;
2007-07-16 08:29:28 +00:00
filename = argv[1];
2007-07-16 08:29:28 +00:00
if (*argv[0] == 'u')
prot = 0;
2007-07-16 08:29:28 +00:00
if (stat(filename, &s)) {
printf("stat %s: %s\n", filename, errno_str());
2002-09-18 12:49:44 +00:00
return 1;
}
2007-07-16 08:29:28 +00:00
size = s.st_size;
fd = open(filename, O_WRONLY);
if (fd < 0) {
printf("open %s: %s\n", filename, errno_str());
2002-09-18 12:49:44 +00:00
return 1;
}
2007-07-05 16:01:39 +00:00
if (argc == 3)
if (parse_area_spec(argv[2], &start, &size)) {
printf("could not parse: %s\n", argv[optind]);
ret = 1;
goto out;
}
2007-07-16 08:29:28 +00:00
err = protect(fd, size, start, prot);
if (err && err != -ENOSYS) {
2007-07-16 08:29:28 +00:00
perror("protect");
ret = 1;
goto out;
2002-09-18 12:49:44 +00:00
}
out:
2007-07-16 08:29:28 +00:00
close(fd);
return ret;
2002-09-18 12:49:44 +00:00
}
BAREBOX_CMD_HELP_START(protect)
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("Protect the flash memory behind the device. It depends on the device")
BAREBOX_CMD_HELP_TEXT("given, what area will be protected. If the device represents the whole")
BAREBOX_CMD_HELP_TEXT("flash memory, the whole memory will be protected. If the device")
BAREBOX_CMD_HELP_TEXT("represents a partition on a main flash memory, only this partition part")
BAREBOX_CMD_HELP_TEXT("will be protected.")
BAREBOX_CMD_HELP_TEXT("")
BAREBOX_CMD_HELP_TEXT("Use 'addpart' and 'delpart' to manage partitions.")
BAREBOX_CMD_HELP_END
BAREBOX_CMD_START(protect)
.cmd = do_protect,
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("enable flash write protection")
BAREBOX_CMD_OPTS("DEVICE [AREA]")
BAREBOX_CMD_GROUP(CMD_GRP_HWMANIP)
BAREBOX_CMD_HELP(cmd_protect_help)
BAREBOX_CMD_END
2007-07-16 08:29:28 +00:00
BAREBOX_CMD_HELP_START(unprotect)
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("Unprotect the flash memory behind the device. It depends on the device")
BAREBOX_CMD_HELP_TEXT("given, what area will be unprotected. If the device represents the whole")
BAREBOX_CMD_HELP_TEXT("flash memory, the whole memory will be unprotected. If the device")
BAREBOX_CMD_HELP_TEXT("represents a partition on a main flash memory, only this partition part")
BAREBOX_CMD_HELP_TEXT("will be unprotected.")
BAREBOX_CMD_HELP_END
BAREBOX_CMD_START(unprotect)
2007-07-16 08:29:28 +00:00
.cmd = do_protect,
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("disable flash write protection")
BAREBOX_CMD_OPTS("DEVICE [AREA]")
BAREBOX_CMD_GROUP(CMD_GRP_HWMANIP)
BAREBOX_CMD_HELP(cmd_unprotect_help)
BAREBOX_CMD_END