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.
13 #include <u-boot/md5.h>
17 * Store the resulting sum to an address or variable
19 static void store_result(const u8 *sum, const char *dest)
26 ptr = (u8 *)simple_strtoul(dest + 1, NULL, 16);
27 for (i = 0; i < 16; i++)
31 char *str_ptr = str_output;
33 for (i = 0; i < 16; i++) {
34 sprintf(str_ptr, "%02x", sum[i]);
37 env_set(dest, str_output);
41 #ifdef CONFIG_MD5SUM_VERIFY
42 static int parse_verify_sum(char *verify_str, u8 *vsum)
44 if (*verify_str == '*') {
47 ptr = (u8 *)simple_strtoul(verify_str + 1, NULL, 16);
48 memcpy(vsum, ptr, 16);
53 if (strlen(verify_str) == 32)
54 vsum_str = verify_str;
56 vsum_str = env_get(verify_str);
57 if (vsum_str == NULL || strlen(vsum_str) != 32)
61 for (i = 0; i < 16; i++) {
62 char *nullp = vsum_str + (i + 1) * 2;
67 simple_strtoul(vsum_str + (i * 2), NULL, 16);
74 int do_md5sum(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
90 if (strcmp(*av, "-v") == 0) {
98 addr = simple_strtoul(*av++, NULL, 16);
99 len = simple_strtoul(*av++, NULL, 16);
101 buf = map_sysmem(addr, len);
102 md5_wd(buf, len, output, CHUNKSZ_MD5);
106 printf("md5 for %08lx ... %08lx ==> ", addr, addr + len - 1);
107 for (i = 0; i < 16; i++)
108 printf("%02x", output[i]);
112 store_result(output, *av);
114 char *verify_str = *av++;
116 if (parse_verify_sum(verify_str, vsum)) {
117 printf("ERROR: %s does not contain a valid md5 sum\n",
121 if (memcmp(output, vsum, 16) != 0) {
122 printf("md5 for %08lx ... %08lx ==> ", addr,
124 for (i = 0; i < 16; i++)
125 printf("%02x", output[i]);
127 for (i = 0; i < 16; i++)
128 printf("%02x", vsum[i]);
129 printf(" ** ERROR **\n");
137 static int do_md5sum(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
139 unsigned long addr, len;
145 return CMD_RET_USAGE;
147 addr = simple_strtoul(argv[1], NULL, 16);
148 len = simple_strtoul(argv[2], NULL, 16);
150 buf = map_sysmem(addr, len);
151 md5_wd(buf, len, output, CHUNKSZ_MD5);
154 printf("md5 for %08lx ... %08lx ==> ", addr, addr + len - 1);
155 for (i = 0; i < 16; i++)
156 printf("%02x", output[i]);
160 store_result(output, argv[3]);
166 #ifdef CONFIG_MD5SUM_VERIFY
168 md5sum, 5, 1, do_md5sum,
169 "compute MD5 message digest",
170 "address count [[*]sum]\n"
171 " - compute MD5 message digest [save to sum]\n"
172 "md5sum -v address count [*]sum\n"
173 " - verify md5sum of memory area"
177 md5sum, 4, 1, do_md5sum,
178 "compute MD5 message digest",
179 "address count [[*]sum]\n"
180 " - compute MD5 message digest [save to sum]"