3 * Joe Hershberger, National Instruments, joe.hershberger@ni.com
6 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
8 * SPDX-License-Identifier: GPL-2.0+
14 #include <u-boot/md5.h>
18 * Store the resulting sum to an address or variable
20 static void store_result(const u8 *sum, const char *dest)
27 ptr = (u8 *)simple_strtoul(dest + 1, NULL, 16);
28 for (i = 0; i < 16; i++)
32 char *str_ptr = str_output;
34 for (i = 0; i < 16; i++) {
35 sprintf(str_ptr, "%02x", sum[i]);
38 setenv(dest, str_output);
42 #ifdef CONFIG_MD5SUM_VERIFY
43 static int parse_verify_sum(char *verify_str, u8 *vsum)
45 if (*verify_str == '*') {
48 ptr = (u8 *)simple_strtoul(verify_str + 1, NULL, 16);
49 memcpy(vsum, ptr, 16);
54 if (strlen(verify_str) == 32)
55 vsum_str = verify_str;
57 vsum_str = getenv(verify_str);
58 if (vsum_str == NULL || strlen(vsum_str) != 32)
62 for (i = 0; i < 16; i++) {
63 char *nullp = vsum_str + (i + 1) * 2;
68 simple_strtoul(vsum_str + (i * 2), NULL, 16);
75 int do_md5sum(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
91 if (strcmp(*av, "-v") == 0) {
99 addr = simple_strtoul(*av++, NULL, 16);
100 len = simple_strtoul(*av++, NULL, 16);
102 buf = map_sysmem(addr, len);
103 md5_wd(buf, len, output, CHUNKSZ_MD5);
107 printf("md5 for %08lx ... %08lx ==> ", addr, addr + len - 1);
108 for (i = 0; i < 16; i++)
109 printf("%02x", output[i]);
113 store_result(output, *av);
115 char *verify_str = *av++;
117 if (parse_verify_sum(verify_str, vsum)) {
118 printf("ERROR: %s does not contain a valid md5 sum\n",
122 if (memcmp(output, vsum, 16) != 0) {
123 printf("md5 for %08lx ... %08lx ==> ", addr,
125 for (i = 0; i < 16; i++)
126 printf("%02x", output[i]);
128 for (i = 0; i < 16; i++)
129 printf("%02x", vsum[i]);
130 printf(" ** ERROR **\n");
138 static int do_md5sum(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
140 unsigned long addr, len;
146 return CMD_RET_USAGE;
148 addr = simple_strtoul(argv[1], NULL, 16);
149 len = simple_strtoul(argv[2], NULL, 16);
151 buf = map_sysmem(addr, len);
152 md5_wd(buf, len, output, CHUNKSZ_MD5);
155 printf("md5 for %08lx ... %08lx ==> ", addr, addr + len - 1);
156 for (i = 0; i < 16; i++)
157 printf("%02x", output[i]);
161 store_result(output, argv[3]);
167 #ifdef CONFIG_MD5SUM_VERIFY
169 md5sum, 5, 1, do_md5sum,
170 "compute MD5 message digest",
171 "address count [[*]sum]\n"
172 " - compute MD5 message digest [save to sum]\n"
173 "md5sum -v address count [*]sum\n"
174 " - verify md5sum of memory area"
178 md5sum, 4, 1, do_md5sum,
179 "compute MD5 message digest",
180 "address count [[*]sum]\n"
181 " - compute MD5 message digest [save to sum]"