]> git.ipfire.org Git - people/ms/u-boot.git/blame - common/cmd_md5sum.c
cmd_sf: Add print mesgs on sf read/write commands
[people/ms/u-boot.git] / common / cmd_md5sum.c
CommitLineData
c3d2a17c 1/*
5ab177be
JH
2 * (C) Copyright 2011
3 * Joe Hershberger, National Instruments, joe.hershberger@ni.com
4 *
c3d2a17c
MF
5 * (C) Copyright 2000
6 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
7 *
8 * See file CREDITS for list of people who contributed to this
9 * project.
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of
14 * the License, or (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
24 * MA 02111-1307 USA
25 */
26
27#include <common.h>
28#include <command.h>
29#include <u-boot/md5.h>
30
ecd72950
JH
31/*
32 * Store the resulting sum to an address or variable
33 */
34static void store_result(const u8 *sum, const char *dest)
35{
36 unsigned int i;
37
38 if (*dest == '*') {
39 u8 *ptr;
40
41 ptr = (u8 *)simple_strtoul(dest + 1, NULL, 16);
42 for (i = 0; i < 16; i++)
43 *ptr++ = sum[i];
44 } else {
45 char str_output[33];
46 char *str_ptr = str_output;
47
48 for (i = 0; i < 16; i++) {
49 sprintf(str_ptr, "%02x", sum[i]);
50 str_ptr += 2;
51 }
52 str_ptr = '\0';
53 setenv(dest, str_output);
54 }
55}
56
5ab177be
JH
57#ifdef CONFIG_MD5SUM_VERIFY
58static int parse_verify_sum(char *verify_str, u8 *vsum)
59{
60 if (*verify_str == '*') {
61 u8 *ptr;
62
63 ptr = (u8 *)simple_strtoul(verify_str + 1, NULL, 16);
64 memcpy(vsum, ptr, 16);
65 } else {
66 unsigned int i;
67 char *vsum_str;
68
69 if (strlen(verify_str) == 32)
70 vsum_str = verify_str;
71 else {
72 vsum_str = getenv(verify_str);
73 if (vsum_str == NULL || strlen(vsum_str) != 32)
74 return 1;
75 }
76
77 for (i = 0; i < 16; i++) {
78 char *nullp = vsum_str + (i + 1) * 2;
79 char end = *nullp;
80
81 *nullp = '\0';
82 *(u8 *)(vsum + i) =
83 simple_strtoul(vsum_str + (i * 2), NULL, 16);
84 *nullp = end;
85 }
86 }
87 return 0;
88}
89
90int do_md5sum(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
91{
92 ulong addr, len;
93 unsigned int i;
94 u8 output[16];
95 u8 vsum[16];
96 int verify = 0;
97 int ac;
98 char * const *av;
99
100 if (argc < 3)
101 return CMD_RET_USAGE;
102
103 av = argv + 1;
104 ac = argc - 1;
105 if (strcmp(*av, "-v") == 0) {
106 verify = 1;
107 av++;
108 ac--;
109 if (ac < 3)
110 return CMD_RET_USAGE;
111 }
112
113 addr = simple_strtoul(*av++, NULL, 16);
114 len = simple_strtoul(*av++, NULL, 16);
115
116 md5_wd((unsigned char *) addr, len, output, CHUNKSZ_MD5);
117
118 if (!verify) {
119 printf("md5 for %08lx ... %08lx ==> ", addr, addr + len - 1);
120 for (i = 0; i < 16; i++)
121 printf("%02x", output[i]);
122 printf("\n");
ecd72950
JH
123
124 if (ac > 2)
125 store_result(output, *av);
5ab177be
JH
126 } else {
127 char *verify_str = *av++;
128
129 if (parse_verify_sum(verify_str, vsum)) {
130 printf("ERROR: %s does not contain a valid md5 sum\n",
131 verify_str);
132 return 1;
133 }
134 if (memcmp(output, vsum, 16) != 0) {
135 printf("md5 for %08lx ... %08lx ==> ", addr,
136 addr + len - 1);
137 for (i = 0; i < 16; i++)
138 printf("%02x", output[i]);
139 printf(" != ");
140 for (i = 0; i < 16; i++)
141 printf("%02x", vsum[i]);
142 printf(" ** ERROR **\n");
143 return 1;
144 }
145 }
146
147 return 0;
148}
149#else
c3d2a17c
MF
150static int do_md5sum(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
151{
152 unsigned long addr, len;
153 unsigned int i;
154 u8 output[16];
155
156 if (argc < 3)
4c12eeb8 157 return CMD_RET_USAGE;
c3d2a17c
MF
158
159 addr = simple_strtoul(argv[1], NULL, 16);
160 len = simple_strtoul(argv[2], NULL, 16);
161
a16a84b7 162 md5_wd((unsigned char *) addr, len, output, CHUNKSZ_MD5);
c3d2a17c
MF
163 printf("md5 for %08lx ... %08lx ==> ", addr, addr + len - 1);
164 for (i = 0; i < 16; i++)
165 printf("%02x", output[i]);
166 printf("\n");
167
ecd72950
JH
168 if (argc > 3)
169 store_result(output, argv[3]);
170
c3d2a17c
MF
171 return 0;
172}
5ab177be 173#endif
c3d2a17c 174
5ab177be
JH
175#ifdef CONFIG_MD5SUM_VERIFY
176U_BOOT_CMD(
177 md5sum, 5, 1, do_md5sum,
178 "compute MD5 message digest",
ecd72950
JH
179 "address count [[*]sum]\n"
180 " - compute MD5 message digest [save to sum]\n"
5ab177be
JH
181 "md5sum -v address count [*]sum\n"
182 " - verify md5sum of memory area"
183);
184#else
c3d2a17c 185U_BOOT_CMD(
ecd72950 186 md5sum, 4, 1, do_md5sum,
c3d2a17c 187 "compute MD5 message digest",
ecd72950
JH
188 "address count [[*]sum]\n"
189 " - compute MD5 message digest [save to sum]"
c3d2a17c 190);
5ab177be 191#endif