1 // SPDX-License-Identifier: GPL-2.0+
4 * Joe Hershberger, National Instruments, joe.hershberger@ni.com
7 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
15 #include <u-boot/md5.h>
19 * Store the resulting sum to an address or variable
21 static void store_result(const u8 *sum, const char *dest)
28 ptr = (u8 *)simple_strtoul(dest + 1, NULL, 16);
29 for (i = 0; i < 16; i++)
33 char *str_ptr = str_output;
35 for (i = 0; i < 16; i++) {
36 sprintf(str_ptr, "%02x", sum[i]);
39 env_set(dest, str_output);
43 #ifdef CONFIG_MD5SUM_VERIFY
44 static int parse_verify_sum(char *verify_str, u8 *vsum)
46 if (*verify_str == '*') {
49 ptr = (u8 *)simple_strtoul(verify_str + 1, NULL, 16);
50 memcpy(vsum, ptr, 16);
55 if (strlen(verify_str) == 32)
56 vsum_str = verify_str;
58 vsum_str = env_get(verify_str);
59 if (vsum_str == NULL || strlen(vsum_str) != 32)
63 for (i = 0; i < 16; i++) {
64 char *nullp = vsum_str + (i + 1) * 2;
69 simple_strtoul(vsum_str + (i * 2), NULL, 16);
76 int do_md5sum(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
92 if (strcmp(*av, "-v") == 0) {
100 addr = simple_strtoul(*av++, NULL, 16);
101 len = simple_strtoul(*av++, NULL, 16);
103 buf = map_sysmem(addr, len);
104 md5_wd(buf, len, output, CHUNKSZ_MD5);
108 printf("md5 for %08lx ... %08lx ==> ", addr, addr + len - 1);
109 for (i = 0; i < 16; i++)
110 printf("%02x", output[i]);
114 store_result(output, *av);
116 char *verify_str = *av++;
118 if (parse_verify_sum(verify_str, vsum)) {
119 printf("ERROR: %s does not contain a valid md5 sum\n",
123 if (memcmp(output, vsum, 16) != 0) {
124 printf("md5 for %08lx ... %08lx ==> ", addr,
126 for (i = 0; i < 16; i++)
127 printf("%02x", output[i]);
129 for (i = 0; i < 16; i++)
130 printf("%02x", vsum[i]);
131 printf(" ** ERROR **\n");
139 static int do_md5sum(struct cmd_tbl *cmdtp, int flag, int argc,
142 unsigned long addr, len;
148 return CMD_RET_USAGE;
150 addr = simple_strtoul(argv[1], NULL, 16);
151 len = simple_strtoul(argv[2], NULL, 16);
153 buf = map_sysmem(addr, len);
154 md5_wd(buf, len, output, CHUNKSZ_MD5);
157 printf("md5 for %08lx ... %08lx ==> ", addr, addr + len - 1);
158 for (i = 0; i < 16; i++)
159 printf("%02x", output[i]);
163 store_result(output, argv[3]);
169 #ifdef CONFIG_MD5SUM_VERIFY
171 md5sum, 5, 1, do_md5sum,
172 "compute MD5 message digest",
173 "address count [[*]sum]\n"
174 " - compute MD5 message digest [save to sum]\n"
175 "md5sum -v address count [*]sum\n"
176 " - verify md5sum of memory area"
180 md5sum, 4, 1, do_md5sum,
181 "compute MD5 message digest",
182 "address count [[*]sum]\n"
183 " - compute MD5 message digest [save to sum]"