summaryrefslogtreecommitdiffstats
path: root/commands/crc.c
diff options
context:
space:
mode:
authorJean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>2010-10-11 16:34:40 +0200
committerSascha Hauer <s.hauer@pengutronix.de>2010-10-12 21:32:44 +0200
commita828cef9b5bc5f1b3c50193dda069697e5b9691b (patch)
tree94f8eca63fbbde45a999aa00a35f64c97514e13c /commands/crc.c
parent4dfd5b5c621c88ea94d39afd6c3d7395a9dd94fd (diff)
downloadbarebox-a828cef9b5bc5f1b3c50193dda069697e5b9691b.tar.gz
barebox-a828cef9b5bc5f1b3c50193dda069697e5b9691b.tar.xz
commands/crc32: add compare 2 files crc
add -F options to compare to file crc it's usefull to compare what you flash in a partition it's selectable by CONFIG_CMD_CRC_CMP Signed-off-by: Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'commands/crc.c')
-rw-r--r--commands/crc.c111
1 files changed, 74 insertions, 37 deletions
diff --git a/commands/crc.c b/commands/crc.c
index 4842cdc82f..d3e08654e9 100644
--- a/commands/crc.c
+++ b/commands/crc.c
@@ -30,20 +30,80 @@
#include <malloc.h>
#include <linux/ctype.h>
+static int file_crc(char* filename, ulong start, ulong size, ulong *crc,
+ ulong *total)
+{
+ int fd, now;
+ int ret = 0;
+ char *buf;
+
+ *total = 0;
+ *crc = 0;
+
+ fd = open(filename, O_RDONLY);
+ if (fd < 0) {
+ printf("open %s: %s\n", filename, errno_str());
+ return fd;
+ }
+
+ if (start > 0) {
+ ret = lseek(fd, start, SEEK_SET);
+ if (ret == -1) {
+ perror("lseek");
+ goto out;
+ }
+ }
+
+ buf = xmalloc(4096);
+
+ while (size) {
+ now = min((ulong)4096, size);
+ now = read(fd, buf, now);
+ if (now < 0) {
+ ret = now;
+ perror("read");
+ goto out_free;
+ }
+ if (!now)
+ break;
+ *crc = crc32(*crc, buf, now);
+ size -= now;
+ *total += now;
+ }
+
+ printf ("CRC32 for %s 0x%08lx ... 0x%08lx ==> 0x%08lx",
+ filename, start, start + *total - 1, *crc);
+
+out_free:
+ free(buf);
+out:
+ close(fd);
+
+ return ret;
+}
+
static int do_crc(struct command *cmdtp, int argc, char *argv[])
{
ulong start = 0, size = ~0, total = 0;
ulong crc = 0, vcrc = 0;
char *filename = "/dev/mem";
- char *buf;
- int fd, opt, err = 0, filegiven = 0, verify = 0, now;
+#ifdef CONFIG_CMD_CRC_CMP
+ char *vfilename = NULL;
+#endif
+ int opt, err = 0, filegiven = 0, verify = 0;
- while((opt = getopt(argc, argv, "f:v:")) > 0) {
+ while((opt = getopt(argc, argv, "f:F:v:")) > 0) {
switch(opt) {
case 'f':
filename = optarg;
filegiven = 1;
break;
+#ifdef CONFIG_CMD_CRC_CMP
+ case 'F':
+ verify = 1;
+ vfilename = optarg;
+ break;
+#endif
case 'v':
verify = 1;
vcrc = simple_strtoul(optarg, NULL, 0);
@@ -61,38 +121,17 @@ static int do_crc(struct command *cmdtp, int argc, char *argv[])
}
}
- fd = open(filename, O_RDONLY);
- if (fd < 0) {
- printf("open %s: %s\n", filename, errno_str());
+ if (file_crc(filename, start, size, &crc, &total) < 0)
return 1;
- }
- if (start > 0) {
- if (lseek(fd, start, SEEK_SET) == -1) {
- perror("lseek");
- err = 1;
- goto out;
- }
- }
-
- buf = xmalloc(4096);
-
- while (size) {
- now = min((ulong)4096, size);
- now = read(fd, buf, now);
- if (now < 0) {
- perror("read");
- goto out_free;
- }
- if (!now)
- break;
- crc = crc32(crc, buf, now);
- size -= now;
- total += now;
+#ifdef CONFIG_CMD_CRC_CMP
+ if (vfilename) {
+ size = total;
+ puts("\n");
+ if (file_crc(vfilename, start, size, &vcrc, &total) < 0)
+ return 1;
}
-
- printf ("CRC32 for %s 0x%08lx ... 0x%08lx ==> 0x%08lx",
- filename, start, start + total - 1, crc);
+#endif
if (verify && crc != vcrc) {
printf(" != 0x%08x ** ERROR **", vcrc);
@@ -101,11 +140,6 @@ static int do_crc(struct command *cmdtp, int argc, char *argv[])
printf("\n");
-out_free:
- free(buf);
-out:
- close(fd);
-
return err;
}
@@ -114,6 +148,9 @@ static const __maybe_unused char cmd_crc_help[] =
"Calculate a crc32 checksum of a memory area\n"
"Options:\n"
" -f <file> Use file instead of memory\n"
+#ifdef CONFIG_CMD_CRC_CMP
+" -F <file> Use file to compare\n"
+#endif
" -v <crc> Verfify\n";
BAREBOX_CMD_START(crc32)