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
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
31 #include <linux/ctype.h>
33 static int do_crc(struct command
*cmdtp
, int argc
, char *argv
[])
35 ulong start
= 0, size
= ~0, total
= 0;
36 ulong crc
= 0, vcrc
= 0;
37 char *filename
= "/dev/mem";
39 int fd
, opt
, err
= 0, filegiven
= 0, verify
= 0, now
;
41 while((opt
= getopt(argc
, argv
, "f:v:")) > 0) {
49 vcrc
= simple_strtoul(optarg
, NULL
, 0);
54 if (!filegiven
&& optind
== argc
)
55 return COMMAND_ERROR_USAGE
;
58 if (parse_area_spec(argv
[optind
], &start
, &size
)) {
59 printf("could not parse area description: %s\n", argv
[optind
]);
64 fd
= open(filename
, O_RDONLY
);
66 printf("open %s: %s\n", filename
, errno_str());
71 if (lseek(fd
, start
, SEEK_SET
) == -1) {
81 now
= min((ulong
)4096, size
);
82 now
= read(fd
, buf
, now
);
89 crc
= crc32(crc
, buf
, now
);
94 printf ("CRC32 for %s 0x%08lx ... 0x%08lx ==> 0x%08lx",
95 filename
, start
, start
+ total
- 1, crc
);
97 if (verify
&& crc
!= vcrc
) {
98 printf(" != 0x%08x ** ERROR **", vcrc
);
112 static const __maybe_unused
char cmd_crc_help
[] =
113 "Usage: crc32 [OPTION] [AREA]\n"
114 "Calculate a crc32 checksum of a memory area\n"
116 " -f <file> Use file instead of memory\n"
117 " -v <crc> Verfify\n";
119 BAREBOX_CMD_START(crc32
)
121 .usage
= "crc32 checksum calculation",
122 BAREBOX_CMD_HELP(cmd_crc_help
)