Commit e50b12c95cb12c8e8a5efcc1047747740fcf8e6a

Authored by Simon Glass
Committed by Tom Rini
1 parent 460408ef9a

sha1sum: Use generic hash layer

Update the code to use the hash layer instead of local code.

Signed-off-by: Simon Glass <sjg@chromium.org>

Showing 1 changed file with 4 additions and 125 deletions Side-by-side Diff

common/cmd_sha1sum.c
... ... @@ -26,73 +26,11 @@
26 26  
27 27 #include <common.h>
28 28 #include <command.h>
  29 +#include <hash.h>
29 30 #include <sha1.h>
30 31  
31   -/*
32   - * Store the resulting sum to an address or variable
33   - */
34   -static 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   -
57   -#ifdef CONFIG_SHA1SUM_VERIFY
58   -static 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   -
90 32 int do_sha1sum(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
91 33 {
92   - ulong addr, len;
93   - unsigned int i;
94   - u8 output[20];
95   - u8 vsum[20];
96 34 int verify = 0;
97 35 int ac;
98 36 char * const *av;
99 37  
100 38  
101 39  
102 40  
... ... @@ -102,75 +40,16 @@
102 40  
103 41 av = argv + 1;
104 42 ac = argc - 1;
  43 +#ifdef CONFIG_SHA1SUM_VERIFY
105 44 if (strcmp(*av, "-v") == 0) {
106 45 verify = 1;
107 46 av++;
108 47 ac--;
109   - if (ac < 3)
110   - return CMD_RET_USAGE;
111 48 }
  49 +#endif
112 50  
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");
123   -
124   - if (ac > 2)
125   - store_result(output, *av);
126   - } 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;
  51 + return hash_command("sha1", verify, cmdtp, flag, ac, av);
148 52 }
149   -#else
150   -static 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)
157   - return CMD_RET_USAGE;
158   -
159   - addr = simple_strtoul(argv[1], NULL, 16);
160   - len = simple_strtoul(argv[2], NULL, 16);
161   -
162   - sha1_csum_wd((unsigned char *) addr, len, output, CHUNKSZ_SHA1);
163   - 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   -
168   - if (argc > 3)
169   - store_result(output, argv[3]);
170   -
171   - return 0;
172   -}
173   -#endif
174 53  
175 54 #ifdef CONFIG_SHA1SUM_VERIFY
176 55 U_BOOT_CMD(