1
0
mirror of https://github.com/systemd/systemd.git synced 2025-03-31 14:50:15 +03:00

tpm2-util: introduce tpm2_parse_pcr_argument() helper

Add a new tpm2_parse_pcr_argument() helper that unifies how we merge PCR
masks in a single function, we can use all over the place. Previously we
had basically the same code for this at 4 places.
This commit is contained in:
Lennart Poettering 2022-08-17 17:00:27 +02:00
parent 466266c172
commit 222a951fa4
6 changed files with 36 additions and 59 deletions

View File

@ -832,25 +832,12 @@ static int parse_argv(int argc, char *argv[]) {
arg_tpm2_device = streq(optarg, "auto") ? NULL : optarg;
break;
case ARG_TPM2_PCRS: {
uint32_t mask;
if (isempty(optarg)) {
arg_tpm2_pcr_mask = 0;
break;
}
r = tpm2_parse_pcrs(optarg, &mask);
case ARG_TPM2_PCRS:
r = tpm2_parse_pcr_argument(optarg, &arg_tpm2_pcr_mask);
if (r < 0)
return r;
if (arg_tpm2_pcr_mask == UINT32_MAX)
arg_tpm2_pcr_mask = mask;
else
arg_tpm2_pcr_mask |= mask;
break;
}
case ARG_NAME:
if (isempty(optarg)) {

View File

@ -315,25 +315,12 @@ static int parse_argv(int argc, char *argv[]) {
break;
}
case ARG_TPM2_PCRS: {
uint32_t mask;
if (isempty(optarg)) {
arg_tpm2_pcr_mask = 0;
break;
}
r = tpm2_parse_pcrs(optarg, &mask);
case ARG_TPM2_PCRS:
r = tpm2_parse_pcr_argument(optarg, &arg_tpm2_pcr_mask);
if (r < 0)
return r;
if (arg_tpm2_pcr_mask == UINT32_MAX)
arg_tpm2_pcr_mask = mask;
else
arg_tpm2_pcr_mask |= mask;
break;
}
case ARG_TPM2_PIN:
r = parse_boolean_argument("--tpm2-with-pin=", optarg, &arg_tpm2_pin);

View File

@ -394,20 +394,9 @@ static int parse_one_option(const char *option) {
} else if ((val = startswith(option, "tpm2-pcrs="))) {
if (isempty(val))
arg_tpm2_pcr_mask = 0;
else {
uint32_t mask;
r = tpm2_parse_pcrs(val, &mask);
if (r < 0)
return r;
if (arg_tpm2_pcr_mask == UINT32_MAX)
arg_tpm2_pcr_mask = mask;
else
arg_tpm2_pcr_mask |= mask;
}
r = tpm2_parse_pcr_argument(val, &arg_tpm2_pcr_mask);
if (r < 0)
return r;
} else if ((val = startswith(option, "tpm2-pin="))) {

View File

@ -4359,25 +4359,12 @@ static int parse_argv(int argc, char *argv[]) {
break;
}
case ARG_TPM2_PCRS: {
uint32_t mask;
if (isempty(optarg)) {
arg_tpm2_pcr_mask = 0;
break;
}
r = tpm2_parse_pcrs(optarg, &mask);
case ARG_TPM2_PCRS:
r = tpm2_parse_pcr_argument(optarg, &arg_tpm2_pcr_mask);
if (r < 0)
return r;
if (arg_tpm2_pcr_mask == UINT32_MAX)
arg_tpm2_pcr_mask = mask;
else
arg_tpm2_pcr_mask |= mask;
break;
}
case '?':
return -EINVAL;

View File

@ -1508,3 +1508,28 @@ Tpm2Support tpm2_support(void) {
return support;
}
int tpm2_parse_pcr_argument(const char *arg, uint32_t *mask) {
uint32_t m;
int r;
assert(mask);
/* For use in getopt_long() command line parsers: merges masks specified on the command line */
if (isempty(arg)) {
*mask = 0;
return 0;
}
r = tpm2_parse_pcrs(arg, &m);
if (r < 0)
return r;
if (*mask == UINT32_MAX)
*mask = m;
else
*mask |= m;
return 0;
}

View File

@ -113,3 +113,5 @@ typedef enum Tpm2Support {
} Tpm2Support;
Tpm2Support tpm2_support(void);
int tpm2_parse_pcr_argument(const char *arg, uint32_t *mask);