@@ -58,19 +58,31 @@ static struct hash_algo hash_algo[] = {
58
58
* @algo: Hash algorithm being used
59
59
* @sum: Hash digest (algo->digest_size bytes)
60
60
* @dest: Destination, interpreted as a hex address if it starts
61
- * with * or otherwise as an environment variable.
61
+ * with * (or allow_env_vars is 0) or otherwise as an
62
+ * environment variable.
63
+ * @allow_env_vars: non-zero to permit storing the result to an
64
+ * variable environment
62
65
*/
63
66
static void store_result (struct hash_algo * algo , const u8 * sum ,
64
- const char * dest )
67
+ const char * dest , int allow_env_vars )
65
68
{
66
69
unsigned int i ;
70
+ int env_var = 0 ;
67
71
68
- if (* dest == '*' ) {
69
- u8 * ptr ;
72
+ /*
73
+ * If environment variables are allowed, then we assume that 'dest'
74
+ * is an environment variable, unless it starts with *, in which
75
+ * case we assume it is an address. If not allowed, it is always an
76
+ * address. This is to support the crc32 command.
77
+ */
78
+ if (allow_env_vars ) {
79
+ if (* dest == '*' )
80
+ dest ++ ;
81
+ else
82
+ env_var = 1 ;
83
+ }
70
84
71
- ptr = (u8 * )simple_strtoul (dest + 1 , NULL , 16 );
72
- memcpy (ptr , sum , algo -> digest_size );
73
- } else {
85
+ if (env_var ) {
74
86
char str_output [HASH_MAX_DIGEST_SIZE * 2 + 1 ];
75
87
char * str_ptr = str_output ;
76
88
@@ -80,6 +92,11 @@ static void store_result(struct hash_algo *algo, const u8 *sum,
80
92
}
81
93
str_ptr = '\0' ;
82
94
setenv (dest , str_output );
95
+ } else {
96
+ u8 * ptr ;
97
+
98
+ ptr = (u8 * )simple_strtoul (dest , NULL , 16 );
99
+ memcpy (ptr , sum , algo -> digest_size );
83
100
}
84
101
}
85
102
@@ -94,14 +111,28 @@ static void store_result(struct hash_algo *algo, const u8 *sum,
94
111
* Otherwise we assume it is an environment variable, and
95
112
* look up its value (it must contain a hex digest).
96
113
* @vsum: Returns binary digest value (algo->digest_size bytes)
114
+ * @allow_env_vars: non-zero to permit storing the result to an environment
115
+ * variable. If 0 then verify_str is assumed to be an
116
+ * address, and the * prefix is not expected.
97
117
* @return 0 if ok, non-zero on error
98
118
*/
99
- static int parse_verify_sum (struct hash_algo * algo , char * verify_str , u8 * vsum )
119
+ static int parse_verify_sum (struct hash_algo * algo , char * verify_str , u8 * vsum ,
120
+ int allow_env_vars )
100
121
{
101
- if (* verify_str == '*' ) {
122
+ int env_var = 0 ;
123
+
124
+ /* See comment above in store_result() */
125
+ if (allow_env_vars ) {
126
+ if (* verify_str == '*' )
127
+ verify_str ++ ;
128
+ else
129
+ env_var = 1 ;
130
+ }
131
+
132
+ if (env_var ) {
102
133
u8 * ptr ;
103
134
104
- ptr = (u8 * )simple_strtoul (verify_str + 1 , NULL , 16 );
135
+ ptr = (u8 * )simple_strtoul (verify_str , NULL , 16 );
105
136
memcpy (vsum , ptr , algo -> digest_size );
106
137
} else {
107
138
unsigned int i ;
@@ -158,7 +189,7 @@ static void show_hash(struct hash_algo *algo, ulong addr, ulong len,
158
189
printf ("%02x" , output [i ]);
159
190
}
160
191
161
- int hash_command (const char * algo_name , int verify , cmd_tbl_t * cmdtp , int flag ,
192
+ int hash_command (const char * algo_name , int flags , cmd_tbl_t * cmdtp , int flag ,
162
193
int argc , char * const argv [])
163
194
{
164
195
struct hash_algo * algo ;
@@ -169,13 +200,14 @@ int hash_command(const char *algo_name, int verify, cmd_tbl_t *cmdtp, int flag,
169
200
if (argc < 2 )
170
201
return CMD_RET_USAGE ;
171
202
203
+ addr = simple_strtoul (* argv ++ , NULL , 16 );
204
+ len = simple_strtoul (* argv ++ , NULL , 16 );
205
+
172
206
algo = find_hash_algo (algo_name );
173
207
if (!algo ) {
174
208
printf ("Unknown hash algorithm '%s'\n" , algo_name );
175
209
return CMD_RET_USAGE ;
176
210
}
177
- addr = simple_strtoul (* argv ++ , NULL , 16 );
178
- len = simple_strtoul (* argv ++ , NULL , 16 );
179
211
argc -= 2 ;
180
212
181
213
if (algo -> digest_size > HASH_MAX_DIGEST_SIZE ) {
@@ -188,13 +220,14 @@ int hash_command(const char *algo_name, int verify, cmd_tbl_t *cmdtp, int flag,
188
220
189
221
/* Try to avoid code bloat when verify is not needed */
190
222
#ifdef CONFIG_HASH_VERIFY
191
- if (verify ) {
223
+ if (flags & HASH_FLAG_VERIFY ) {
192
224
#else
193
225
if (0 ) {
194
226
#endif
195
227
if (!argc )
196
228
return CMD_RET_USAGE ;
197
- if (parse_verify_sum (algo , * argv , vsum )) {
229
+ if (parse_verify_sum (algo , * argv , vsum ,
230
+ flags & HASH_FLAG_ENV )) {
198
231
printf ("ERROR: %s does not contain a valid %s sum\n" ,
199
232
* argv , algo -> name );
200
233
return 1 ;
@@ -213,8 +246,10 @@ int hash_command(const char *algo_name, int verify, cmd_tbl_t *cmdtp, int flag,
213
246
show_hash (algo , addr , len , output );
214
247
printf ("\n" );
215
248
216
- if (argc )
217
- store_result (algo , output , * argv );
249
+ if (argc ) {
250
+ store_result (algo , output , * argv ,
251
+ flags & HASH_FLAG_ENV );
252
+ }
218
253
}
219
254
220
255
return 0 ;
0 commit comments