arm start.c: Make runtime function address calculation tolerant for more compilers
[barebox-mini2440.git] / commands / crc.c
blob4842cdc82f386cb24069f9fa522330859cc59f87
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(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";
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 (start > 0) {
71 if (lseek(fd, start, SEEK_SET) == -1) {
72 perror("lseek");
73 err = 1;
74 goto out;
78 buf = xmalloc(4096);
80 while (size) {
81 now = min((ulong)4096, size);
82 now = read(fd, buf, now);
83 if (now < 0) {
84 perror("read");
85 goto out_free;
87 if (!now)
88 break;
89 crc = crc32(crc, buf, now);
90 size -= now;
91 total += 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);
99 err = 1;
102 printf("\n");
104 out_free:
105 free(buf);
106 out:
107 close(fd);
109 return err;
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"
115 "Options:\n"
116 " -f <file> Use file instead of memory\n"
117 " -v <crc> Verfify\n";
119 BAREBOX_CMD_START(crc32)
120 .cmd = do_crc,
121 .usage = "crc32 checksum calculation",
122 BAREBOX_CMD_HELP(cmd_crc_help)
123 BAREBOX_CMD_END