9
0
Fork 0
barebox/commands/crc.c

133 lines
3.0 KiB
C
Raw Normal View History

2007-10-16 17:07:21 +00:00
/*
* crc.c - Calculate a crc32 checksum of a memory area
*
* Copyright (c) 2007 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
*
* 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 version 2
* as published by the Free Software Foundation.
*
* 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.
*
*/
2007-07-05 16:02:16 +00:00
#include <common.h>
#include <command.h>
2007-07-14 12:47:35 +00:00
#include <fs.h>
#include <getopt.h>
#include <malloc.h>
#include <environment.h>
static int crc_from_file(const char* file, ulong *crc)
{
char * buf;
buf= read_file(file, NULL);
if (!buf)
return -ENOMEM;
*crc = simple_strtoul(buf, NULL, 16);
return 0;
}
static int do_crc(int argc, char *argv[])
2007-07-05 16:02:16 +00:00
{
loff_t start = 0, size = ~0;
ulong crc = 0, vcrc = 0, total = 0;
2007-07-14 12:47:35 +00:00
char *filename = "/dev/mem";
#ifdef CONFIG_CMD_CRC_CMP
char *vfilename = NULL;
#endif
int opt, err = 0, filegiven = 0, verify = 0;
2007-07-14 12:47:35 +00:00
while((opt = getopt(argc, argv, "f:F:v:V:")) > 0) {
2007-07-14 12:47:35 +00:00
switch(opt) {
case 'f':
filename = optarg;
filegiven = 1;
break;
#ifdef CONFIG_CMD_CRC_CMP
case 'F':
verify = 1;
vfilename = optarg;
break;
#endif
2007-07-14 12:47:35 +00:00
case 'v':
verify = 1;
vcrc = simple_strtoul(optarg, NULL, 0);
break;
case 'V':
if (!crc_from_file(optarg, &vcrc))
verify = 1;
break;
default:
return COMMAND_ERROR_USAGE;
2007-07-14 12:47:35 +00:00
}
}
2007-07-05 16:02:16 +00:00
if (!filegiven && optind == argc)
return COMMAND_ERROR_USAGE;
2007-07-05 16:02:16 +00:00
2007-07-14 12:47:35 +00:00
if (optind < argc) {
if (parse_area_spec(argv[optind], &start, &size)) {
printf("could not parse area description: %s\n", argv[optind]);
return 1;
}
}
2007-07-05 16:02:16 +00:00
if (file_crc(filename, start, size, &crc, &total) < 0)
2007-07-14 12:47:35 +00:00
return 1;
2007-07-05 16:02:16 +00:00
printf("CRC32 for %s 0x%08lx ... 0x%08lx ==> 0x%08lx",
filename, (ulong)start, (ulong)start + total - 1, crc);
#ifdef CONFIG_CMD_CRC_CMP
if (vfilename) {
size = total;
puts("\n");
if (file_crc(vfilename, start, size, &vcrc, &total) < 0)
return 1;
2007-07-05 16:02:16 +00:00
}
#endif
2007-07-05 16:02:16 +00:00
2007-07-14 12:47:35 +00:00
if (verify && crc != vcrc) {
printf(" != 0x%08lx ** ERROR **", vcrc);
2007-07-14 12:47:35 +00:00
err = 1;
2007-07-05 16:02:16 +00:00
}
2007-07-14 12:47:35 +00:00
printf("\n");
2007-07-05 16:02:16 +00:00
2007-07-14 12:47:35 +00:00
return err;
2007-07-05 16:02:16 +00:00
}
BAREBOX_CMD_HELP_START(crc32)
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("Calculate a CRC32 checksum of a memory area.")
BAREBOX_CMD_HELP_TEXT("")
BAREBOX_CMD_HELP_TEXT("Options:")
BAREBOX_CMD_HELP_OPT ("-f FILE", "Use file instead of memory.")
#ifdef CONFIG_CMD_CRC_CMP
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 ("-F FILE", "Use file to compare.")
#endif
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 ("-v CRC", "Verify")
BAREBOX_CMD_HELP_OPT ("-V FILE", "Verify with CRC read from FILE")
BAREBOX_CMD_HELP_END
2007-07-05 16:02:16 +00:00
BAREBOX_CMD_START(crc32)
2007-07-14 12:47:35 +00:00
.cmd = do_crc,
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("CRC32 checksum calculation")
BAREBOX_CMD_OPTS("[-f"
#ifdef CONFIG_CMD_CRC_CMP
"F"
#endif
"vV] AREA")
BAREBOX_CMD_GROUP(CMD_GRP_MEM)
BAREBOX_CMD_HELP(cmd_crc32_help)
BAREBOX_CMD_END