Skip to content

Commit d5b7667

Browse files
committed
hash: Add a flag to support saving hashes in the environment
Some hashing commands permit saving the hash in an environment variable, and verifying a hash from there. But the crc32 command does not support this. In order to permit crc32 to use the generic hashing infrastructure, add a flag to select which behaviour to use. Signed-off-by: Simon Glass <sjg@chromium.org>
1 parent 0ccff50 commit d5b7667

File tree

4 files changed

+66
-26
lines changed

4 files changed

+66
-26
lines changed

common/cmd_hash.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,22 @@
3030
static int do_hash(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
3131
{
3232
#ifdef CONFIG_HASH_VERIFY
33-
int verify = 0;
33+
int flags = HASH_FLAG_ENV;
3434

3535
if (argc < 4)
3636
return CMD_RET_USAGE;
3737
if (!strcmp(argv[1], "-v")) {
38-
verify = 1;
38+
flags |= HASH_FLAG_VERIFY;
3939
argc--;
4040
argv++;
4141
}
4242
#else
43-
const int verify = 0;
43+
const int flags = HASH_FLAG_ENV;
4444
#endif
4545
/* Move forward to 'algorithm' parameter */
4646
argc--;
4747
argv++;
48-
return hash_command(*argv, verify, cmdtp, flag, argc - 1, argv + 1);
48+
return hash_command(*argv, flags, cmdtp, flag, argc - 1, argv + 1);
4949
}
5050

5151
#ifdef CONFIG_HASH_VERIFY

common/cmd_sha1sum.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
int do_sha1sum(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
3333
{
34-
int verify = 0;
34+
int flags = HASH_FLAG_ENV;
3535
int ac;
3636
char * const *av;
3737

@@ -42,13 +42,13 @@ int do_sha1sum(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
4242
ac = argc - 1;
4343
#ifdef CONFIG_SHA1SUM_VERIFY
4444
if (strcmp(*av, "-v") == 0) {
45-
verify = 1;
45+
flags |= HASH_FLAG_VERIFY;
4646
av++;
4747
ac--;
4848
}
4949
#endif
5050

51-
return hash_command("sha1", verify, cmdtp, flag, ac, av);
51+
return hash_command("sha1", flags, cmdtp, flag, ac, av);
5252
}
5353

5454
#ifdef CONFIG_SHA1SUM_VERIFY

common/hash.c

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -58,19 +58,31 @@ static struct hash_algo hash_algo[] = {
5858
* @algo: Hash algorithm being used
5959
* @sum: Hash digest (algo->digest_size bytes)
6060
* @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
6265
*/
6366
static void store_result(struct hash_algo *algo, const u8 *sum,
64-
const char *dest)
67+
const char *dest, int allow_env_vars)
6568
{
6669
unsigned int i;
70+
int env_var = 0;
6771

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+
}
7084

71-
ptr = (u8 *)simple_strtoul(dest + 1, NULL, 16);
72-
memcpy(ptr, sum, algo->digest_size);
73-
} else {
85+
if (env_var) {
7486
char str_output[HASH_MAX_DIGEST_SIZE * 2 + 1];
7587
char *str_ptr = str_output;
7688

@@ -80,6 +92,11 @@ static void store_result(struct hash_algo *algo, const u8 *sum,
8092
}
8193
str_ptr = '\0';
8294
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);
83100
}
84101
}
85102

@@ -94,14 +111,28 @@ static void store_result(struct hash_algo *algo, const u8 *sum,
94111
* Otherwise we assume it is an environment variable, and
95112
* look up its value (it must contain a hex digest).
96113
* @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.
97117
* @return 0 if ok, non-zero on error
98118
*/
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)
100121
{
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) {
102133
u8 *ptr;
103134

104-
ptr = (u8 *)simple_strtoul(verify_str + 1, NULL, 16);
135+
ptr = (u8 *)simple_strtoul(verify_str, NULL, 16);
105136
memcpy(vsum, ptr, algo->digest_size);
106137
} else {
107138
unsigned int i;
@@ -158,7 +189,7 @@ static void show_hash(struct hash_algo *algo, ulong addr, ulong len,
158189
printf("%02x", output[i]);
159190
}
160191

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,
162193
int argc, char * const argv[])
163194
{
164195
struct hash_algo *algo;
@@ -169,13 +200,14 @@ int hash_command(const char *algo_name, int verify, cmd_tbl_t *cmdtp, int flag,
169200
if (argc < 2)
170201
return CMD_RET_USAGE;
171202

203+
addr = simple_strtoul(*argv++, NULL, 16);
204+
len = simple_strtoul(*argv++, NULL, 16);
205+
172206
algo = find_hash_algo(algo_name);
173207
if (!algo) {
174208
printf("Unknown hash algorithm '%s'\n", algo_name);
175209
return CMD_RET_USAGE;
176210
}
177-
addr = simple_strtoul(*argv++, NULL, 16);
178-
len = simple_strtoul(*argv++, NULL, 16);
179211
argc -= 2;
180212

181213
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,
188220

189221
/* Try to avoid code bloat when verify is not needed */
190222
#ifdef CONFIG_HASH_VERIFY
191-
if (verify) {
223+
if (flags & HASH_FLAG_VERIFY) {
192224
#else
193225
if (0) {
194226
#endif
195227
if (!argc)
196228
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)) {
198231
printf("ERROR: %s does not contain a valid %s sum\n",
199232
*argv, algo->name);
200233
return 1;
@@ -213,8 +246,10 @@ int hash_command(const char *algo_name, int verify, cmd_tbl_t *cmdtp, int flag,
213246
show_hash(algo, addr, len, output);
214247
printf("\n");
215248

216-
if (argc)
217-
store_result(algo, output, *argv);
249+
if (argc) {
250+
store_result(algo, output, *argv,
251+
flags & HASH_FLAG_ENV);
252+
}
218253
}
219254

220255
return 0;

include/hash.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,24 @@ struct hash_algo {
5151
*/
5252
#define HASH_MAX_DIGEST_SIZE 32
5353

54+
enum {
55+
HASH_FLAG_VERIFY = 1 << 0, /* Enable verify mode */
56+
HASH_FLAG_ENV = 1 << 1, /* Allow env vars */
57+
};
58+
5459
/**
5560
* hash_command: Process a hash command for a particular algorithm
5661
*
5762
* This common function is used to implement specific hash commands.
5863
*
5964
* @algo_name: Hash algorithm being used
60-
* @verify: Non-zero to enable verify mode
65+
* @flags: Flags value (HASH_FLAG_...)
6166
* @cmdtp: Pointer to command table entry
6267
* @flag: Some flags normally 0 (see CMD_FLAG_.. above)
6368
* @argc: Number of arguments (arg 0 must be the command text)
6469
* @argv: Arguments
6570
*/
66-
int hash_command(const char *algo_name, int verify, cmd_tbl_t *cmdtp, int flag,
71+
int hash_command(const char *algo_name, int flags, cmd_tbl_t *cmdtp, int flag,
6772
int argc, char * const argv[]);
6873

6974
#endif

0 commit comments

Comments
 (0)