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>
|
2013-12-03 23:06:38 +00:00
|
|
|
#include <environment.h>
|
2010-10-11 14:34:40 +00:00
|
|
|
|
2013-09-19 12:47:24 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-12-21 13:18:57 +00:00
|
|
|
static int do_crc(int argc, char *argv[])
|
2007-07-05 16:02:16 +00:00
|
|
|
{
|
2011-10-18 15:21:18 +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";
|
2010-10-11 14:34:40 +00:00
|
|
|
#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
|
|
|
|
2013-09-19 12:47:24 +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;
|
2010-10-11 14:34:40 +00:00
|
|
|
#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;
|
2013-09-19 12:47:24 +00:00
|
|
|
case 'V':
|
|
|
|
if (!crc_from_file(optarg, &vcrc))
|
|
|
|
verify = 1;
|
|
|
|
break;
|
2011-12-05 12:46:24 +00:00
|
|
|
default:
|
|
|
|
return COMMAND_ERROR_USAGE;
|
2007-07-14 12:47:35 +00:00
|
|
|
}
|
|
|
|
}
|
2007-07-05 16:02:16 +00:00
|
|
|
|
2009-10-17 10:44:19 +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
|
|
|
|
2010-10-11 14:34:40 +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
|
|
|
|
2013-12-03 23:06:38 +00:00
|
|
|
printf("CRC32 for %s 0x%08lx ... 0x%08lx ==> 0x%08lx",
|
|
|
|
filename, (ulong)start, (ulong)start + total - 1, crc);
|
|
|
|
|
2010-10-11 14:34:40 +00:00
|
|
|
#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
|
|
|
}
|
2010-10-11 14:34:40 +00:00
|
|
|
#endif
|
2007-07-05 16:02:16 +00:00
|
|
|
|
2007-07-14 12:47:35 +00:00
|
|
|
if (verify && crc != vcrc) {
|
2010-12-20 23:02:02 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2010-07-11 10:26:25 +00:00
|
|
|
BAREBOX_CMD_HELP_START(crc)
|
|
|
|
BAREBOX_CMD_HELP_USAGE("crc32 [OPTION] [AREA]\n")
|
|
|
|
BAREBOX_CMD_HELP_SHORT("Calculate a crc32 checksum of a memory area.\n")
|
|
|
|
BAREBOX_CMD_HELP_OPT ("-f <file>", "Use file instead of memory.\n")
|
2010-10-11 14:34:40 +00:00
|
|
|
#ifdef CONFIG_CMD_CRC_CMP
|
2010-07-11 10:26:25 +00:00
|
|
|
BAREBOX_CMD_HELP_OPT ("-F <file>", "Use file to compare.\n")
|
2010-10-11 14:34:40 +00:00
|
|
|
#endif
|
2011-05-14 20:10:54 +00:00
|
|
|
BAREBOX_CMD_HELP_OPT ("-v <crc>", "Verify\n")
|
2013-09-19 12:47:24 +00:00
|
|
|
BAREBOX_CMD_HELP_OPT ("-V <file>", "Verify with crc read from <file>\n")
|
2010-07-11 10:26:25 +00:00
|
|
|
BAREBOX_CMD_HELP_END
|
2007-07-05 16:02:16 +00:00
|
|
|
|
2009-12-15 08:11:09 +00:00
|
|
|
BAREBOX_CMD_START(crc32)
|
2007-07-14 12:47:35 +00:00
|
|
|
.cmd = do_crc,
|
|
|
|
.usage = "crc32 checksum calculation",
|
2009-12-15 08:11:09 +00:00
|
|
|
BAREBOX_CMD_HELP(cmd_crc_help)
|
|
|
|
BAREBOX_CMD_END
|