blob: fbb99eaad1152075f89427976e76666dc4a27f0d [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 Hershbergere6e77d32012-08-17 11:00:45 +000031#ifdef CONFIG_SHA1SUM_VERIFY
32static int parse_verify_sum(char *verify_str, u8 *vsum)
33{
34 if (*verify_str == '*') {
35 u8 *ptr;
36
37 ptr = (u8 *)simple_strtoul(verify_str + 1, NULL, 16);
38 memcpy(vsum, ptr, 20);
39 } else {
40 unsigned int i;
41 char *vsum_str;
42
43 if (strlen(verify_str) == 40)
44 vsum_str = verify_str;
45 else {
46 vsum_str = getenv(verify_str);
47 if (vsum_str == NULL || strlen(vsum_str) != 40)
48 return 1;
49 }
50
51 for (i = 0; i < 20; i++) {
52 char *nullp = vsum_str + (i + 1) * 2;
53 char end = *nullp;
54
55 *nullp = '\0';
56 *(u8 *)(vsum + i) =
57 simple_strtoul(vsum_str + (i * 2), NULL, 16);
58 *nullp = end;
59 }
60 }
61 return 0;
62}
63
64int do_sha1sum(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
65{
66 ulong addr, len;
67 unsigned int i;
68 u8 output[20];
69 u8 vsum[20];
70 int verify = 0;
71 int ac;
72 char * const *av;
73
74 if (argc < 3)
75 return CMD_RET_USAGE;
76
77 av = argv + 1;
78 ac = argc - 1;
79 if (strcmp(*av, "-v") == 0) {
80 verify = 1;
81 av++;
82 ac--;
83 if (ac < 3)
84 return CMD_RET_USAGE;
85 }
86
87 addr = simple_strtoul(*av++, NULL, 16);
88 len = simple_strtoul(*av++, NULL, 16);
89
90 sha1_csum_wd((unsigned char *) addr, len, output, CHUNKSZ_SHA1);
91
92 if (!verify) {
93 printf("SHA1 for %08lx ... %08lx ==> ", addr, addr + len - 1);
94 for (i = 0; i < 20; i++)
95 printf("%02x", output[i]);
96 printf("\n");
97 } else {
98 char *verify_str = *av++;
99
100 if (parse_verify_sum(verify_str, vsum)) {
101 printf("ERROR: %s does not contain a valid SHA1 sum\n",
102 verify_str);
103 return 1;
104 }
105 if (memcmp(output, vsum, 20) != 0) {
106 printf("SHA1 for %08lx ... %08lx ==> ", addr,
107 addr + len - 1);
108 for (i = 0; i < 20; i++)
109 printf("%02x", output[i]);
110 printf(" != ");
111 for (i = 0; i < 20; i++)
112 printf("%02x", vsum[i]);
113 printf(" ** ERROR **\n");
114 return 1;
115 }
116 }
117
118 return 0;
119}
120#else
Mike Frysingerc3d2a172011-04-02 21:40:19 -0400121static int do_sha1sum(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
122{
123 unsigned long addr, len;
124 unsigned int i;
125 u8 output[20];
126
127 if (argc < 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000128 return CMD_RET_USAGE;
Mike Frysingerc3d2a172011-04-02 21:40:19 -0400129
130 addr = simple_strtoul(argv[1], NULL, 16);
131 len = simple_strtoul(argv[2], NULL, 16);
132
Jens Scharsig3e499b62011-07-18 14:39:07 +0200133 sha1_csum_wd((unsigned char *) addr, len, output, CHUNKSZ_SHA1);
Mike Frysingerc3d2a172011-04-02 21:40:19 -0400134 printf("SHA1 for %08lx ... %08lx ==> ", addr, addr + len - 1);
135 for (i = 0; i < 20; i++)
136 printf("%02x", output[i]);
137 printf("\n");
138
139 return 0;
140}
Joe Hershbergere6e77d32012-08-17 11:00:45 +0000141#endif
Mike Frysingerc3d2a172011-04-02 21:40:19 -0400142
Joe Hershbergere6e77d32012-08-17 11:00:45 +0000143#ifdef CONFIG_SHA1SUM_VERIFY
144U_BOOT_CMD(
145 sha1sum, 5, 1, do_sha1sum,
146 "compute SHA1 message digest",
147 "address count\n"
148 " - compute SHA1 message digest\n"
149 "sha1sum -v address count [*]sum\n"
150 " - verify sha1sum of memory area"
151);
152#else
Mike Frysingerc3d2a172011-04-02 21:40:19 -0400153U_BOOT_CMD(
154 sha1sum, 3, 1, do_sha1sum,
155 "compute SHA1 message digest",
156 "address count"
157);
Joe Hershbergere6e77d32012-08-17 11:00:45 +0000158#endif