blob: 9d2d4180c5cf8262ae9ea83cd00b0a4943a24b60 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Mike Frysingerc3d2a172011-04-02 21:40:19 -04002/*
Joe Hershberger5ab177b2012-08-17 11:00:47 +00003 * (C) Copyright 2011
4 * Joe Hershberger, National Instruments, joe.hershberger@ni.com
5 *
Mike Frysingerc3d2a172011-04-02 21:40:19 -04006 * (C) Copyright 2000
7 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
Mike Frysingerc3d2a172011-04-02 21:40:19 -04008 */
9
10#include <common.h>
11#include <command.h>
Joe Hershberger0eb25b62015-03-22 17:08:59 -050012#include <mapmem.h>
Mike Frysingerc3d2a172011-04-02 21:40:19 -040013#include <u-boot/md5.h>
Suriyan Ramasami79444952014-11-17 14:39:34 -080014#include <asm/io.h>
Mike Frysingerc3d2a172011-04-02 21:40:19 -040015
Joe Hershbergerecd72952012-08-17 11:00:48 +000016/*
17 * Store the resulting sum to an address or variable
18 */
19static void store_result(const u8 *sum, const char *dest)
20{
21 unsigned int i;
22
23 if (*dest == '*') {
24 u8 *ptr;
25
26 ptr = (u8 *)simple_strtoul(dest + 1, NULL, 16);
27 for (i = 0; i < 16; i++)
28 *ptr++ = sum[i];
29 } else {
30 char str_output[33];
31 char *str_ptr = str_output;
32
33 for (i = 0; i < 16; i++) {
34 sprintf(str_ptr, "%02x", sum[i]);
35 str_ptr += 2;
36 }
Simon Glass382bee52017-08-03 12:22:09 -060037 env_set(dest, str_output);
Joe Hershbergerecd72952012-08-17 11:00:48 +000038 }
39}
40
Joe Hershberger5ab177b2012-08-17 11:00:47 +000041#ifdef CONFIG_MD5SUM_VERIFY
42static int parse_verify_sum(char *verify_str, u8 *vsum)
43{
44 if (*verify_str == '*') {
45 u8 *ptr;
46
47 ptr = (u8 *)simple_strtoul(verify_str + 1, NULL, 16);
48 memcpy(vsum, ptr, 16);
49 } else {
50 unsigned int i;
51 char *vsum_str;
52
53 if (strlen(verify_str) == 32)
54 vsum_str = verify_str;
55 else {
Simon Glass00caae62017-08-03 12:22:12 -060056 vsum_str = env_get(verify_str);
Joe Hershberger5ab177b2012-08-17 11:00:47 +000057 if (vsum_str == NULL || strlen(vsum_str) != 32)
58 return 1;
59 }
60
61 for (i = 0; i < 16; i++) {
62 char *nullp = vsum_str + (i + 1) * 2;
63 char end = *nullp;
64
65 *nullp = '\0';
66 *(u8 *)(vsum + i) =
67 simple_strtoul(vsum_str + (i * 2), NULL, 16);
68 *nullp = end;
69 }
70 }
71 return 0;
72}
73
74int do_md5sum(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
75{
76 ulong addr, len;
77 unsigned int i;
78 u8 output[16];
79 u8 vsum[16];
80 int verify = 0;
81 int ac;
82 char * const *av;
Suriyan Ramasami79444952014-11-17 14:39:34 -080083 void *buf;
Joe Hershberger5ab177b2012-08-17 11:00:47 +000084
85 if (argc < 3)
86 return CMD_RET_USAGE;
87
88 av = argv + 1;
89 ac = argc - 1;
90 if (strcmp(*av, "-v") == 0) {
91 verify = 1;
92 av++;
93 ac--;
94 if (ac < 3)
95 return CMD_RET_USAGE;
96 }
97
98 addr = simple_strtoul(*av++, NULL, 16);
99 len = simple_strtoul(*av++, NULL, 16);
100
Suriyan Ramasami79444952014-11-17 14:39:34 -0800101 buf = map_sysmem(addr, len);
102 md5_wd(buf, len, output, CHUNKSZ_MD5);
103 unmap_sysmem(buf);
Joe Hershberger5ab177b2012-08-17 11:00:47 +0000104
105 if (!verify) {
106 printf("md5 for %08lx ... %08lx ==> ", addr, addr + len - 1);
107 for (i = 0; i < 16; i++)
108 printf("%02x", output[i]);
109 printf("\n");
Joe Hershbergerecd72952012-08-17 11:00:48 +0000110
111 if (ac > 2)
112 store_result(output, *av);
Joe Hershberger5ab177b2012-08-17 11:00:47 +0000113 } else {
114 char *verify_str = *av++;
115
116 if (parse_verify_sum(verify_str, vsum)) {
117 printf("ERROR: %s does not contain a valid md5 sum\n",
118 verify_str);
119 return 1;
120 }
121 if (memcmp(output, vsum, 16) != 0) {
122 printf("md5 for %08lx ... %08lx ==> ", addr,
123 addr + len - 1);
124 for (i = 0; i < 16; i++)
125 printf("%02x", output[i]);
126 printf(" != ");
127 for (i = 0; i < 16; i++)
128 printf("%02x", vsum[i]);
129 printf(" ** ERROR **\n");
130 return 1;
131 }
132 }
133
134 return 0;
135}
136#else
Mike Frysingerc3d2a172011-04-02 21:40:19 -0400137static int do_md5sum(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
138{
139 unsigned long addr, len;
140 unsigned int i;
141 u8 output[16];
Suriyan Ramasami79444952014-11-17 14:39:34 -0800142 void *buf;
Mike Frysingerc3d2a172011-04-02 21:40:19 -0400143
144 if (argc < 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000145 return CMD_RET_USAGE;
Mike Frysingerc3d2a172011-04-02 21:40:19 -0400146
147 addr = simple_strtoul(argv[1], NULL, 16);
148 len = simple_strtoul(argv[2], NULL, 16);
149
Suriyan Ramasami79444952014-11-17 14:39:34 -0800150 buf = map_sysmem(addr, len);
151 md5_wd(buf, len, output, CHUNKSZ_MD5);
152 unmap_sysmem(buf);
153
Mike Frysingerc3d2a172011-04-02 21:40:19 -0400154 printf("md5 for %08lx ... %08lx ==> ", addr, addr + len - 1);
155 for (i = 0; i < 16; i++)
156 printf("%02x", output[i]);
157 printf("\n");
158
Joe Hershbergerecd72952012-08-17 11:00:48 +0000159 if (argc > 3)
160 store_result(output, argv[3]);
161
Mike Frysingerc3d2a172011-04-02 21:40:19 -0400162 return 0;
163}
Joe Hershberger5ab177b2012-08-17 11:00:47 +0000164#endif
Mike Frysingerc3d2a172011-04-02 21:40:19 -0400165
Joe Hershberger5ab177b2012-08-17 11:00:47 +0000166#ifdef CONFIG_MD5SUM_VERIFY
167U_BOOT_CMD(
168 md5sum, 5, 1, do_md5sum,
169 "compute MD5 message digest",
Joe Hershbergerecd72952012-08-17 11:00:48 +0000170 "address count [[*]sum]\n"
171 " - compute MD5 message digest [save to sum]\n"
Joe Hershberger5ab177b2012-08-17 11:00:47 +0000172 "md5sum -v address count [*]sum\n"
173 " - verify md5sum of memory area"
174);
175#else
Mike Frysingerc3d2a172011-04-02 21:40:19 -0400176U_BOOT_CMD(
Joe Hershbergerecd72952012-08-17 11:00:48 +0000177 md5sum, 4, 1, do_md5sum,
Mike Frysingerc3d2a172011-04-02 21:40:19 -0400178 "compute MD5 message digest",
Joe Hershbergerecd72952012-08-17 11:00:48 +0000179 "address count [[*]sum]\n"
180 " - compute MD5 message digest [save to sum]"
Mike Frysingerc3d2a172011-04-02 21:40:19 -0400181);
Joe Hershberger5ab177b2012-08-17 11:00:47 +0000182#endif