blob: 3f81fdf6ef25bd3631baa86359b0256c78cfe767 [file] [log] [blame]
Mike Frysingera2274902011-04-02 21:40:19 -04001/*
Joe Hershbergercd39f642012-08-17 11:00:47 +00002 * (C) Copyright 2011
3 * Joe Hershberger, National Instruments, joe.hershberger@ni.com
4 *
Mike Frysingera2274902011-04-02 21:40:19 -04005 * (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
Joe Hershberger47685cf2012-08-17 11:00:48 +000031/*
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
Joe Hershbergercd39f642012-08-17 11:00:47 +000057#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");
Joe Hershberger47685cf2012-08-17 11:00:48 +0000123
124 if (ac > 2)
125 store_result(output, *av);
Joe Hershbergercd39f642012-08-17 11:00:47 +0000126 } 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
Mike Frysingera2274902011-04-02 21:40:19 -0400150static 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)
Simon Glassa06dfc72011-12-10 08:44:01 +0000157 return CMD_RET_USAGE;
Mike Frysingera2274902011-04-02 21:40:19 -0400158
159 addr = simple_strtoul(argv[1], NULL, 16);
160 len = simple_strtoul(argv[2], NULL, 16);
161
Jens Scharsig94f23da2011-07-18 14:39:11 +0200162 md5_wd((unsigned char *) addr, len, output, CHUNKSZ_MD5);
Mike Frysingera2274902011-04-02 21:40:19 -0400163 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
Joe Hershberger47685cf2012-08-17 11:00:48 +0000168 if (argc > 3)
169 store_result(output, argv[3]);
170
Mike Frysingera2274902011-04-02 21:40:19 -0400171 return 0;
172}
Joe Hershbergercd39f642012-08-17 11:00:47 +0000173#endif
Mike Frysingera2274902011-04-02 21:40:19 -0400174
Joe Hershbergercd39f642012-08-17 11:00:47 +0000175#ifdef CONFIG_MD5SUM_VERIFY
176U_BOOT_CMD(
177 md5sum, 5, 1, do_md5sum,
178 "compute MD5 message digest",
Joe Hershberger47685cf2012-08-17 11:00:48 +0000179 "address count [[*]sum]\n"
180 " - compute MD5 message digest [save to sum]\n"
Joe Hershbergercd39f642012-08-17 11:00:47 +0000181 "md5sum -v address count [*]sum\n"
182 " - verify md5sum of memory area"
183);
184#else
Mike Frysingera2274902011-04-02 21:40:19 -0400185U_BOOT_CMD(
Joe Hershberger47685cf2012-08-17 11:00:48 +0000186 md5sum, 4, 1, do_md5sum,
Mike Frysingera2274902011-04-02 21:40:19 -0400187 "compute MD5 message digest",
Joe Hershberger47685cf2012-08-17 11:00:48 +0000188 "address count [[*]sum]\n"
189 " - compute MD5 message digest [save to sum]"
Mike Frysingera2274902011-04-02 21:40:19 -0400190);
Joe Hershbergercd39f642012-08-17 11:00:47 +0000191#endif