blob: 8db5456c9583ec8cf6b927f9760a748d72f4f1ed [file] [log] [blame]
Mike Frysingerc3d2a172011-04-02 21:40:19 -04001/*
Joe Hershbergere6e77d32012-08-17 11:00:45 +00002 * (C) Copyright 2011
3 * Joe Hershberger, National Instruments, joe.hershberger@ni.com
4 *
Mike Frysingerc3d2a172011-04-02 21:40:19 -04005 * (C) Copyright 2000
6 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
7 *
8 * See file CREDITS for list of people who contributed to this
9 * project.
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of
14 * the License, or (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
24 * MA 02111-1307 USA
25 */
26
27#include <common.h>
28#include <command.h>
29#include <sha1.h>
30
Joe Hershberger3c210e22012-08-17 11:00:46 +000031/*
32 * Store the resulting sum to an address or variable
33 */
34static void store_result(const u8 *sum, const char *dest)
35{
36 unsigned int i;
37
38 if (*dest == '*') {
39 u8 *ptr;
40
41 ptr = (u8 *)simple_strtoul(dest + 1, NULL, 16);
42 for (i = 0; i < 20; i++)
43 *ptr++ = sum[i];
44 } else {
45 char str_output[41];
46 char *str_ptr = str_output;
47
48 for (i = 0; i < 20; i++) {
49 sprintf(str_ptr, "%02x", sum[i]);
50 str_ptr += 2;
51 }
52 str_ptr = '\0';
53 setenv(dest, str_output);
54 }
55}
56
Joe Hershbergere6e77d32012-08-17 11:00:45 +000057#ifdef CONFIG_SHA1SUM_VERIFY
58static int parse_verify_sum(char *verify_str, u8 *vsum)
59{
60 if (*verify_str == '*') {
61 u8 *ptr;
62
63 ptr = (u8 *)simple_strtoul(verify_str + 1, NULL, 16);
64 memcpy(vsum, ptr, 20);
65 } else {
66 unsigned int i;
67 char *vsum_str;
68
69 if (strlen(verify_str) == 40)
70 vsum_str = verify_str;
71 else {
72 vsum_str = getenv(verify_str);
73 if (vsum_str == NULL || strlen(vsum_str) != 40)
74 return 1;
75 }
76
77 for (i = 0; i < 20; i++) {
78 char *nullp = vsum_str + (i + 1) * 2;
79 char end = *nullp;
80
81 *nullp = '\0';
82 *(u8 *)(vsum + i) =
83 simple_strtoul(vsum_str + (i * 2), NULL, 16);
84 *nullp = end;
85 }
86 }
87 return 0;
88}
89
90int do_sha1sum(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
91{
92 ulong addr, len;
93 unsigned int i;
94 u8 output[20];
95 u8 vsum[20];
96 int verify = 0;
97 int ac;
98 char * const *av;
99
100 if (argc < 3)
101 return CMD_RET_USAGE;
102
103 av = argv + 1;
104 ac = argc - 1;
105 if (strcmp(*av, "-v") == 0) {
106 verify = 1;
107 av++;
108 ac--;
109 if (ac < 3)
110 return CMD_RET_USAGE;
111 }
112
113 addr = simple_strtoul(*av++, NULL, 16);
114 len = simple_strtoul(*av++, NULL, 16);
115
116 sha1_csum_wd((unsigned char *) addr, len, output, CHUNKSZ_SHA1);
117
118 if (!verify) {
119 printf("SHA1 for %08lx ... %08lx ==> ", addr, addr + len - 1);
120 for (i = 0; i < 20; i++)
121 printf("%02x", output[i]);
122 printf("\n");
Joe Hershberger3c210e22012-08-17 11:00:46 +0000123
124 if (ac > 2)
125 store_result(output, *av);
Joe Hershbergere6e77d32012-08-17 11:00:45 +0000126 } else {
127 char *verify_str = *av++;
128
129 if (parse_verify_sum(verify_str, vsum)) {
130 printf("ERROR: %s does not contain a valid SHA1 sum\n",
131 verify_str);
132 return 1;
133 }
134 if (memcmp(output, vsum, 20) != 0) {
135 printf("SHA1 for %08lx ... %08lx ==> ", addr,
136 addr + len - 1);
137 for (i = 0; i < 20; i++)
138 printf("%02x", output[i]);
139 printf(" != ");
140 for (i = 0; i < 20; i++)
141 printf("%02x", vsum[i]);
142 printf(" ** ERROR **\n");
143 return 1;
144 }
145 }
146
147 return 0;
148}
149#else
Mike Frysingerc3d2a172011-04-02 21:40:19 -0400150static int do_sha1sum(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
151{
152 unsigned long addr, len;
153 unsigned int i;
154 u8 output[20];
155
156 if (argc < 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000157 return CMD_RET_USAGE;
Mike Frysingerc3d2a172011-04-02 21:40:19 -0400158
159 addr = simple_strtoul(argv[1], NULL, 16);
160 len = simple_strtoul(argv[2], NULL, 16);
161
Jens Scharsig3e499b62011-07-18 14:39:07 +0200162 sha1_csum_wd((unsigned char *) addr, len, output, CHUNKSZ_SHA1);
Mike Frysingerc3d2a172011-04-02 21:40:19 -0400163 printf("SHA1 for %08lx ... %08lx ==> ", addr, addr + len - 1);
164 for (i = 0; i < 20; i++)
165 printf("%02x", output[i]);
166 printf("\n");
167
Joe Hershberger3c210e22012-08-17 11:00:46 +0000168 if (argc > 3)
169 store_result(output, argv[3]);
170
Mike Frysingerc3d2a172011-04-02 21:40:19 -0400171 return 0;
172}
Joe Hershbergere6e77d32012-08-17 11:00:45 +0000173#endif
Mike Frysingerc3d2a172011-04-02 21:40:19 -0400174
Joe Hershbergere6e77d32012-08-17 11:00:45 +0000175#ifdef CONFIG_SHA1SUM_VERIFY
176U_BOOT_CMD(
177 sha1sum, 5, 1, do_sha1sum,
178 "compute SHA1 message digest",
Joe Hershberger3c210e22012-08-17 11:00:46 +0000179 "address count [[*]sum]\n"
180 " - compute SHA1 message digest [save to sum]\n"
Joe Hershbergere6e77d32012-08-17 11:00:45 +0000181 "sha1sum -v address count [*]sum\n"
182 " - verify sha1sum of memory area"
183);
184#else
Mike Frysingerc3d2a172011-04-02 21:40:19 -0400185U_BOOT_CMD(
Joe Hershberger3c210e22012-08-17 11:00:46 +0000186 sha1sum, 4, 1, do_sha1sum,
Mike Frysingerc3d2a172011-04-02 21:40:19 -0400187 "compute SHA1 message digest",
Joe Hershberger3c210e22012-08-17 11:00:46 +0000188 "address count [[*]sum]\n"
189 " - compute SHA1 message digest [save to sum]"
Mike Frysingerc3d2a172011-04-02 21:40:19 -0400190);
Joe Hershbergere6e77d32012-08-17 11:00:45 +0000191#endif