9
0
Fork 0
barebox/commands/crc.c

101 lines
1.9 KiB
C
Raw Normal View History

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 <fcntl.h>
#include <errno.h>
#include <xfuncs.h>
#include <malloc.h>
#include <linux/ctype.h>
2007-09-28 08:07:26 +00:00
static int do_crc (cmd_tbl_t *cmdtp, int argc, char *argv[])
2007-07-05 16:02:16 +00:00
{
2007-07-14 12:47:35 +00:00
ulong start = 0, size = ~0, total = 0, now;
ulong crc = 0, vcrc = 0;
char *filename = "/dev/mem";
char *buf;
int fd, opt, err = 0, filegiven = 0, verify = 0;
getopt_reset();
while((opt = getopt(argc, argv, "f:v:")) > 0) {
switch(opt) {
case 'f':
filename = optarg;
filegiven = 1;
break;
case 'v':
verify = 1;
vcrc = simple_strtoul(optarg, NULL, 0);
break;
}
}
2007-07-05 16:02:16 +00:00
2007-07-14 12:47:35 +00:00
if (!filegiven && optind == argc) {
u_boot_cmd_usage(cmdtp);
2007-07-05 16:02:16 +00:00
return 1;
}
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
2007-07-14 12:47:35 +00:00
fd = open(filename, O_RDONLY);
if (fd < 0) {
printf("open %s: %s\n", filename, errno_str());
return 1;
}
2007-07-05 16:02:16 +00:00
2007-07-14 12:47:35 +00:00
if (lseek(fd, start, SEEK_SET) < 0) {
printf("file is smaller than start address\n");
err = 1;
goto out;
}
2007-07-05 16:02:16 +00:00
2007-07-14 12:47:35 +00:00
buf = xmalloc(4096);
2007-07-05 16:02:16 +00:00
2007-07-14 12:47:35 +00:00
while (size) {
now = min((ulong)4096, size);
now = read(fd, buf, now);
if (!now)
break;
crc = crc32(crc, buf, now);
size -= now;
total += now;
2007-07-05 16:02:16 +00:00
}
2007-07-14 12:47:35 +00:00
printf ("CRC32 for %s 0x%08lx ... 0x%08lx ==> 0x%08lx",
filename, start, start + total - 1, crc);
2007-07-05 16:02:16 +00:00
2007-07-14 12:47:35 +00:00
if (verify && crc != vcrc) {
printf(" != 0x%08x ** ERROR **", vcrc);
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
free(buf);
out:
close(fd);
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
}
2007-07-14 12:47:35 +00:00
static __maybe_unused char cmd_crc_help[] =
"Usage: crc32 [OPTION] [AREA]\n"
"Calculate a crc32 checksum of a memory area\n"
"Options:\n"
" -f <file> Use file instead of memory\n"
" -v <crc> Verfify\n";
2007-07-05 16:02:16 +00:00
U_BOOT_CMD_START(crc32)
2007-07-14 12:47:35 +00:00
.maxargs = CONFIG_MAXARGS,
.cmd = do_crc,
.usage = "crc32 checksum calculation",
U_BOOT_CMD_HELP(cmd_crc_help)
2007-07-05 16:02:16 +00:00
U_BOOT_CMD_END