cfi new: fix new disabling buffer support
[barebox-mini2440.git] / commands / crc.c
blob6983c9d0900463bc279f02d25b00c9e843ef166b
1 /*
2 * crc.c - Calculate a crc32 checksum of a memory area
4 * Copyright (c) 2007 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
6 * See file CREDITS for list of people who contributed to this
7 * project.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2
11 * as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <common.h>
24 #include <command.h>
25 #include <fs.h>
26 #include <getopt.h>
27 #include <fcntl.h>
28 #include <errno.h>
29 #include <xfuncs.h>
30 #include <malloc.h>
31 #include <linux/ctype.h>
33 static int do_crc (cmd_tbl_t *cmdtp, int argc, char *argv[])
35 ulong start = 0, size = ~0, total = 0;
36 ulong crc = 0, vcrc = 0;
37 char *filename = "/dev/mem";
38 char *buf;
39 int fd, opt, err = 0, filegiven = 0, verify = 0, now;
41 while((opt = getopt(argc, argv, "f:v:")) > 0) {
42 switch(opt) {
43 case 'f':
44 filename = optarg;
45 filegiven = 1;
46 break;
47 case 'v':
48 verify = 1;
49 vcrc = simple_strtoul(optarg, NULL, 0);
50 break;
54 if (!filegiven && optind == argc)
55 return COMMAND_ERROR_USAGE;
57 if (optind < argc) {
58 if (parse_area_spec(argv[optind], &start, &size)) {
59 printf("could not parse area description: %s\n", argv[optind]);
60 return 1;
64 fd = open(filename, O_RDONLY);
65 if (fd < 0) {
66 printf("open %s: %s\n", filename, errno_str());
67 return 1;
70 if (lseek(fd, start, SEEK_SET) == -1) {
71 perror("lseek");
72 err = 1;
73 goto out;
76 buf = xmalloc(4096);
78 while (size) {
79 now = min((ulong)4096, size);
80 now = read(fd, buf, now);
81 if (now < 0) {
82 perror("read");
83 goto out_free;
85 if (!now)
86 break;
87 crc = crc32(crc, buf, now);
88 size -= now;
89 total += now;
92 printf ("CRC32 for %s 0x%08lx ... 0x%08lx ==> 0x%08lx",
93 filename, start, start + total - 1, crc);
95 if (verify && crc != vcrc) {
96 printf(" != 0x%08x ** ERROR **", vcrc);
97 err = 1;
100 printf("\n");
102 out_free:
103 free(buf);
104 out:
105 close(fd);
107 return err;
110 static const __maybe_unused char cmd_crc_help[] =
111 "Usage: crc32 [OPTION] [AREA]\n"
112 "Calculate a crc32 checksum of a memory area\n"
113 "Options:\n"
114 " -f <file> Use file instead of memory\n"
115 " -v <crc> Verfify\n";
117 U_BOOT_CMD_START(crc32)
118 .cmd = do_crc,
119 .usage = "crc32 checksum calculation",
120 U_BOOT_CMD_HELP(cmd_crc_help)
121 U_BOOT_CMD_END