blob: c737cb23b5e27ee4203cf2290e2068599ac40203 [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 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02008 * SPDX-License-Identifier: GPL-2.0+
Mike Frysingera2274902011-04-02 21:40:19 -04009 */
10
11#include <common.h>
12#include <command.h>
Joe Hershberger65b905b2015-03-22 17:08:59 -050013#include <mapmem.h>
Mike Frysingera2274902011-04-02 21:40:19 -040014#include <u-boot/md5.h>
Suriyan Ramasami95bfacb2014-11-17 14:39:34 -080015#include <asm/io.h>
Mike Frysingera2274902011-04-02 21:40:19 -040016
Joe Hershberger47685cf2012-08-17 11:00:48 +000017/*
18 * Store the resulting sum to an address or variable
19 */
20static void store_result(const u8 *sum, const char *dest)
21{
22 unsigned int i;
23
24 if (*dest == '*') {
25 u8 *ptr;
26
27 ptr = (u8 *)simple_strtoul(dest + 1, NULL, 16);
28 for (i = 0; i < 16; i++)
29 *ptr++ = sum[i];
30 } else {
31 char str_output[33];
32 char *str_ptr = str_output;
33
34 for (i = 0; i < 16; i++) {
35 sprintf(str_ptr, "%02x", sum[i]);
36 str_ptr += 2;
37 }
Simon Glass6a38e412017-08-03 12:22:09 -060038 env_set(dest, str_output);
Joe Hershberger47685cf2012-08-17 11:00:48 +000039 }
40}
41
Joe Hershbergercd39f642012-08-17 11:00:47 +000042#ifdef CONFIG_MD5SUM_VERIFY
43static int parse_verify_sum(char *verify_str, u8 *vsum)
44{
45 if (*verify_str == '*') {
46 u8 *ptr;
47
48 ptr = (u8 *)simple_strtoul(verify_str + 1, NULL, 16);
49 memcpy(vsum, ptr, 16);
50 } else {
51 unsigned int i;
52 char *vsum_str;
53
54 if (strlen(verify_str) == 32)
55 vsum_str = verify_str;
56 else {
Simon Glass64b723f2017-08-03 12:22:12 -060057 vsum_str = env_get(verify_str);
Joe Hershbergercd39f642012-08-17 11:00:47 +000058 if (vsum_str == NULL || strlen(vsum_str) != 32)
59 return 1;
60 }
61
62 for (i = 0; i < 16; i++) {
63 char *nullp = vsum_str + (i + 1) * 2;
64 char end = *nullp;
65
66 *nullp = '\0';
67 *(u8 *)(vsum + i) =
68 simple_strtoul(vsum_str + (i * 2), NULL, 16);
69 *nullp = end;
70 }
71 }
72 return 0;
73}
74
75int do_md5sum(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
76{
77 ulong addr, len;
78 unsigned int i;
79 u8 output[16];
80 u8 vsum[16];
81 int verify = 0;
82 int ac;
83 char * const *av;
Suriyan Ramasami95bfacb2014-11-17 14:39:34 -080084 void *buf;
Joe Hershbergercd39f642012-08-17 11:00:47 +000085
86 if (argc < 3)
87 return CMD_RET_USAGE;
88
89 av = argv + 1;
90 ac = argc - 1;
91 if (strcmp(*av, "-v") == 0) {
92 verify = 1;
93 av++;
94 ac--;
95 if (ac < 3)
96 return CMD_RET_USAGE;
97 }
98
99 addr = simple_strtoul(*av++, NULL, 16);
100 len = simple_strtoul(*av++, NULL, 16);
101
Suriyan Ramasami95bfacb2014-11-17 14:39:34 -0800102 buf = map_sysmem(addr, len);
103 md5_wd(buf, len, output, CHUNKSZ_MD5);
104 unmap_sysmem(buf);
Joe Hershbergercd39f642012-08-17 11:00:47 +0000105
106 if (!verify) {
107 printf("md5 for %08lx ... %08lx ==> ", addr, addr + len - 1);
108 for (i = 0; i < 16; i++)
109 printf("%02x", output[i]);
110 printf("\n");
Joe Hershberger47685cf2012-08-17 11:00:48 +0000111
112 if (ac > 2)
113 store_result(output, *av);
Joe Hershbergercd39f642012-08-17 11:00:47 +0000114 } else {
115 char *verify_str = *av++;
116
117 if (parse_verify_sum(verify_str, vsum)) {
118 printf("ERROR: %s does not contain a valid md5 sum\n",
119 verify_str);
120 return 1;
121 }
122 if (memcmp(output, vsum, 16) != 0) {
123 printf("md5 for %08lx ... %08lx ==> ", addr,
124 addr + len - 1);
125 for (i = 0; i < 16; i++)
126 printf("%02x", output[i]);
127 printf(" != ");
128 for (i = 0; i < 16; i++)
129 printf("%02x", vsum[i]);
130 printf(" ** ERROR **\n");
131 return 1;
132 }
133 }
134
135 return 0;
136}
137#else
Mike Frysingera2274902011-04-02 21:40:19 -0400138static int do_md5sum(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
139{
140 unsigned long addr, len;
141 unsigned int i;
142 u8 output[16];
Suriyan Ramasami95bfacb2014-11-17 14:39:34 -0800143 void *buf;
Mike Frysingera2274902011-04-02 21:40:19 -0400144
145 if (argc < 3)
Simon Glassa06dfc72011-12-10 08:44:01 +0000146 return CMD_RET_USAGE;
Mike Frysingera2274902011-04-02 21:40:19 -0400147
148 addr = simple_strtoul(argv[1], NULL, 16);
149 len = simple_strtoul(argv[2], NULL, 16);
150
Suriyan Ramasami95bfacb2014-11-17 14:39:34 -0800151 buf = map_sysmem(addr, len);
152 md5_wd(buf, len, output, CHUNKSZ_MD5);
153 unmap_sysmem(buf);
154
Mike Frysingera2274902011-04-02 21:40:19 -0400155 printf("md5 for %08lx ... %08lx ==> ", addr, addr + len - 1);
156 for (i = 0; i < 16; i++)
157 printf("%02x", output[i]);
158 printf("\n");
159
Joe Hershberger47685cf2012-08-17 11:00:48 +0000160 if (argc > 3)
161 store_result(output, argv[3]);
162
Mike Frysingera2274902011-04-02 21:40:19 -0400163 return 0;
164}
Joe Hershbergercd39f642012-08-17 11:00:47 +0000165#endif
Mike Frysingera2274902011-04-02 21:40:19 -0400166
Joe Hershbergercd39f642012-08-17 11:00:47 +0000167#ifdef CONFIG_MD5SUM_VERIFY
168U_BOOT_CMD(
169 md5sum, 5, 1, do_md5sum,
170 "compute MD5 message digest",
Joe Hershberger47685cf2012-08-17 11:00:48 +0000171 "address count [[*]sum]\n"
172 " - compute MD5 message digest [save to sum]\n"
Joe Hershbergercd39f642012-08-17 11:00:47 +0000173 "md5sum -v address count [*]sum\n"
174 " - verify md5sum of memory area"
175);
176#else
Mike Frysingera2274902011-04-02 21:40:19 -0400177U_BOOT_CMD(
Joe Hershberger47685cf2012-08-17 11:00:48 +0000178 md5sum, 4, 1, do_md5sum,
Mike Frysingera2274902011-04-02 21:40:19 -0400179 "compute MD5 message digest",
Joe Hershberger47685cf2012-08-17 11:00:48 +0000180 "address count [[*]sum]\n"
181 " - compute MD5 message digest [save to sum]"
Mike Frysingera2274902011-04-02 21:40:19 -0400182);
Joe Hershbergercd39f642012-08-17 11:00:47 +0000183#endif