summaryrefslogtreecommitdiffstats
path: root/commands/mc.c
blob: 3c45b5d0272f5e4c23bbd61b4f8eeb15df607eb6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
 * (C) Copyright 2000
 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
 *
 * See file CREDITS for list of people who contributed to this
 * project.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 */

/**
 * @file
 * @brief memcmp: memory compare command
 */

#include <common.h>
#include <command.h>
#include <fcntl.h>
#include <linux/stat.h> /* FIXME */
#include <fs.h>
#include <errno.h>
#include <xfuncs.h>	/* for xmalloc */
#include <malloc.h>	/* for free */
#include <getopt.h>

static int do_mem_cmp(cmd_tbl_t *cmdtp, int argc, char *argv[])
{
	ulong	addr1, addr2, count = ~0;
	int	mode  = O_RWSIZE_1;
	char   *sourcefile = memory_device;
	char   *destfile = memory_device;
	int     sourcefd, destfd;
	char   *rw_buf1;
	int     ret = 1;
	int     offset = 0;
	struct  stat statbuf;

	if (mem_parse_options(argc, argv, "bwls:d:", &mode, &sourcefile, &destfile) < 0)
		return 1;

	if (optind + 2 > argc) {
		u_boot_cmd_usage(cmdtp);
		return 1;
	}

	addr1 = simple_strtoul(argv[optind], NULL, 0);
	addr2 = simple_strtoul(argv[optind + 1], NULL, 0);

	if (optind + 2 == argc) {
		if (sourcefile == memory_device) {
			printf("source and count not given\n");
			return 1;
		}
		if (stat(sourcefile, &statbuf)) {
			perror("stat");
			return 1;
		}
		count = statbuf.st_size - addr1;
	} else {
		count = simple_strtoul(argv[optind + 2], NULL, 0);
	}

	sourcefd = open_and_lseek(sourcefile, mode | O_RDONLY, addr1);
	if (sourcefd < 0)
		return 1;

	destfd = open_and_lseek(destfile, mode | O_RDONLY, addr2);
	if (destfd < 0) {
		close(sourcefd);
		return 1;
	}

	rw_buf1 = xmalloc(RW_BUF_SIZE);

	while (count > 0) {
		int now, r1, r2, i;

		now = min(RW_BUF_SIZE, count);

		r1 = read(sourcefd, rw_buf,  now);
		if (r1 < 0) {
			perror("read");
			goto out;
		}

		r2 = read(destfd, rw_buf1, now);
		if (r2 < 0) {
			perror("read");
			goto out;
		}

		if (r1 != now || r2 != now) {
			printf("regions differ in size\n");
			goto out;
		}

		for (i = 0; i < now; i++) {
			if (rw_buf[i] != rw_buf1[i]) {
				printf("files differ at offset %d\n", offset);
				goto out;
			}
			offset++;
		}

		count -= now;
	}

	printf("OK\n");
	ret = 0;
out:
	close(sourcefd);
	close(destfd);
	free(rw_buf1);

	return ret;
}

static __maybe_unused char cmd_memcmp_help[] =
"Usage: memcmp [OPTIONS] <addr1> <addr2> <count>\n"
"\n"
"options:\n"
"  -b, -w, -l	use byte, halfword, or word accesses\n"
"  -s <file>    source file (default /dev/mem)\n"
"  -d <file>    destination file (default /dev/mem)\n"
"\n"
"Compare memory regions specified with addr1 and addr2\n"
"of size <count> bytes. If source is a file count can\n"
"be left unspecified in which case the whole file is\n"
"compared\n";

U_BOOT_CMD_START(memcmp)
	.maxargs	= CONFIG_MAXARGS,
	.cmd		= do_mem_cmp,
	.usage		= "memory compare",
	U_BOOT_CMD_HELP(cmd_memcmp_help)
U_BOOT_CMD_END

/**
@page mc_command memcmp: compare something with something other

Usage is: memcmp [OPTIONS] \<addr1> \<addr2> \<count>

Options are:
- -s \<file>   source file (default /dev/mem)
- -d \<file>   destination file (default /dev/mem)
- -b          access in bytes
- -w          access in halfwords (16bit)
- -l          access in words (32bit)

Compare memory regions specified with \<addr1> and \<addr2> of size \<count>
bytes. If source is a file \<count> can be left unspecified in which case the
whole file is compared.

@note The access type might be important in some physical I/O areas.

Usage examples:

Compare two 64kiB memory areas in CPU's physical address range:
@verbatim
uboot:/ memcmp -b 0x400 0xc0000000 0x10000
@endverbatim

Compare a file content with a physical memory area content (to check if
programming the flash memory was successfully):
@verbatim
uboot:/ memcmp -b -s /my_file.bin -d /dev/mem 0x0 0xc0000000
@endverbatim

Compare two files (also to check if programming the flash memory was
successfully):
@verbatim
uboot:/ memcmp -b -s /my_file.bin -d /dev/nor0.partition1
@endverbatim
@note In this case \c my_file.bin could be smaller than the \c nor0.partition1.
\c memcmp stops to compare when the end of the shorter file ends.
*/