aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSughosh Ganu <sughosh.ganu@linaro.org>2023-06-08 17:36:55 +0530
committerSughosh Ganu <sughosh.ganu@linaro.org>2023-06-08 17:36:55 +0530
commitbb35d482036811ee9c7a8e4dbff67aaec2108264 (patch)
treeb6ebde622f23af2779dfe3dcbf9901578a49d504
parent976808c25374a75ed9ad78707ad5803fc942cc37 (diff)
Temp commit with cleanupsmkeficap_parse_support
Signed-off-by: Sughosh Ganu <sughosh.ganu@linaro.org>
-rw-r--r--tools/eficapsule.h2
-rw-r--r--tools/mkeficapsule.c119
-rw-r--r--tools/mkeficapsule_parse.c185
3 files changed, 178 insertions, 128 deletions
diff --git a/tools/eficapsule.h b/tools/eficapsule.h
index 636f7876c9c1..21849918a491 100644
--- a/tools/eficapsule.h
+++ b/tools/eficapsule.h
@@ -59,7 +59,7 @@ enum capsule_type {
};
struct efi_capsule_params {
- efi_guid_t *image_guid;
+ efi_guid_t image_guid;
unsigned long image_index;
unsigned long hardware_instance;
uint64_t monotonic_count;
diff --git a/tools/mkeficapsule.c b/tools/mkeficapsule.c
index 27e0838eae2f..4f53a41b914c 100644
--- a/tools/mkeficapsule.c
+++ b/tools/mkeficapsule.c
@@ -617,36 +617,7 @@ err:
return ret;
}
-static int params_dependency_check(struct efi_capsule_params *params)
-{
-
- /* check necessary parameters */
- if ((params->capsule == CAPSULE_NORMAL_BLOB &&
- ((!params->input_file || !params->capsule_file || !guid) ||
- ((params->privkey_file && !params->cert_file) ||
- (!params->privkey_file && params->cert_file)))) ||
- (params->capsule != CAPSULE_NORMAL_BLOB &&
- ((!params->capsule_file) ||
- ((params->capsule == CAPSULE_ACCEPT) && !guid) ||
- ((params->capsule == CAPSULE_REVERT) && guid)))) {
- print_usage();
- exit(EXIT_FAILURE);
- }
-}
-
-/**
- * main - main entry function of mkeficapsule
- * @argc: Number of arguments
- * @argv: Array of pointers to arguments
- *
- * Create an uefi capsule file, optionally signing it.
- * Parse all the arguments and pass them on to create_fwbin().
- *
- * Return:
- * * 0 - on success
- * * -1 - on failure
- */
-int main(int argc, char **argv)
+static void capsule_with_cmdline_params(int argc, char **argv)
{
efi_guid_t *guid;
unsigned char uuid_buf[16];
@@ -654,11 +625,8 @@ int main(int argc, char **argv)
uint64_t mcount;
unsigned long oemflags;
char *privkey_file, *cert_file;
- char *cfg_file;
int c, idx;
- struct efi_capsule_params params = { 0 };
-
guid = NULL;
index = 0;
instance = 0;
@@ -668,15 +636,7 @@ int main(int argc, char **argv)
dump_sig = 0;
capsule_type = CAPSULE_NORMAL_BLOB;
oemflags = 0;
-
-
for (;;) {
- if (argc == 1) {
- parse_capsule_cfg_file(CONFIG_CAPSULE_CFG_FILE,
- &params);
- break;
- }
-
c = getopt_long(argc, argv, opts_short, options, &idx);
if (c == -1)
break;
@@ -693,55 +653,55 @@ int main(int argc, char **argv)
exit(EXIT_FAILURE);
}
convert_uuid_to_guid(uuid_buf);
- params.image_guid = (efi_guid_t *)uuid_buf;
+ guid = (efi_guid_t *)uuid_buf;
break;
case 'i':
- params.image_index = strtoul(optarg, NULL, 0);
+ index = strtoul(optarg, NULL, 0);
break;
case 'I':
- params.hardware_instance = strtoul(optarg, NULL, 0);
+ instance = strtoul(optarg, NULL, 0);
break;
case 'p':
- if (params.privkey_file) {
+ if (privkey_file) {
fprintf(stderr,
"Private Key already specified\n");
exit(EXIT_FAILURE);
}
- params.privkey_file = optarg;
+ privkey_file = optarg;
break;
case 'c':
- if (params.cert_file) {
+ if (cert_file) {
fprintf(stderr,
"Certificate file already specified\n");
exit(EXIT_FAILURE);
}
- params.cert_file = optarg;
+ cert_file = optarg;
break;
case 'm':
- params.monotonic_count = strtoul(optarg, NULL, 0);
+ mcount = strtoul(optarg, NULL, 0);
break;
case 'd':
dump_sig = 1;
break;
case 'A':
- if (params.capsule) {
+ if (capsule_type) {
fprintf(stderr,
"Select either of Accept or Revert capsule generation\n");
exit(1);
}
- params.capsule = CAPSULE_ACCEPT;
+ capsule_type = CAPSULE_ACCEPT;
break;
case 'R':
- if (params.capsule) {
+ if (capsule_type) {
fprintf(stderr,
"Select either of Accept or Revert capsule generation\n");
exit(1);
}
- params.capsule = CAPSULE_REVERT;
+ capsule_type = CAPSULE_REVERT;
break;
case 'o':
- params.oemflags = strtoul(optarg, NULL, 0);
- if (params.oemflags > 0xffff) {
+ oemflags = strtoul(optarg, NULL, 0);
+ if (oemflags > 0xffff) {
fprintf(stderr,
"oemflags must be between 0x0 and 0xffff\n");
exit(1);
@@ -753,25 +713,52 @@ int main(int argc, char **argv)
}
}
- param_dependency_check(&params);
+ /* check necessary parameters */
+ if ((capsule_type == CAPSULE_NORMAL_BLOB &&
+ ((argc != optind + 2) || !guid ||
+ ((privkey_file && !cert_file) ||
+ (!privkey_file && cert_file)))) ||
+ (capsule_type != CAPSULE_NORMAL_BLOB &&
+ ((argc != optind + 1) ||
+ ((capsule_type == CAPSULE_ACCEPT) && !guid) ||
+ ((capsule_type == CAPSULE_REVERT) && guid)))) {
+ print_usage();
+ exit(EXIT_FAILURE);
+ }
if (capsule_type != CAPSULE_NORMAL_BLOB) {
- if (create_empty_capsule(params.capsule_file, params.image_guid,
- params.capsule ==
- CAPSULE_ACCEPT) < 0) {
+ if (create_empty_capsule(argv[argc - 1], guid,
+ capsule_type == CAPSULE_ACCEPT) < 0) {
fprintf(stderr, "Creating empty capsule failed\n");
exit(EXIT_FAILURE);
}
- } else if (create_fwbin(params.capsule_file, params.input_file,
- params.image_guid, params.image_index,
- params.hardware_instance,
- params.monotonic_count,
- params.privkey_file,
- params.cert_file,
- (uint16_t)params.oemflags) < 0) {
+ } else if (create_fwbin(argv[argc - 1], argv[argc - 2], guid,
+ index, instance, mcount, privkey_file,
+ cert_file, (uint16_t)oemflags) < 0) {
fprintf(stderr, "Creating firmware capsule failed\n");
exit(EXIT_FAILURE);
}
+}
+
+/**
+ * main - main entry function of mkeficapsule
+ * @argc: Number of arguments
+ * @argv: Array of pointers to arguments
+ *
+ * Create an uefi capsule file, optionally signing it.
+ * Parse all the arguments and pass them on to create_fwbin().
+ *
+ * Return:
+ * * 0 - on success
+ * * -1 - on failure
+ */
+int main(int argc, char **argv)
+{
+ if (CONFIG_IS_ENABLED(CONFIG_CAPSULE_CFG_FILE)) {
+ capsule_with_cfg_file();
+ } else {
+ capsule_with_cmdline_params(argc, argv);
+ }
exit(EXIT_SUCCESS);
}
diff --git a/tools/mkeficapsule_parse.c b/tools/mkeficapsule_parse.c
index 965015f1fee7..6249f1a9a24a 100644
--- a/tools/mkeficapsule_parse.c
+++ b/tools/mkeficapsule_parse.c
@@ -36,6 +36,9 @@
#define PARAMS_START "{"
#define PARAMS_END "}"
+#define PSTART 2
+#define PEND 3
+
#define MALLOC_FAIL_STR "Unable to allocate memory\n"
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
@@ -46,8 +49,8 @@ const char *capsule_params[] = {
"hardware-instance", "monotonic-count",
"capsule-type", "oemflags" };
-static uint8_t params_start;
-static uint8_t params_end;
+static unsigned char params_start;
+static unsigned char params_end;
#if 0
static struct efi_capsule_params *params;
@@ -73,7 +76,13 @@ static void allocate_params_node(void)
}
#endif
-static int param_checks(char *line)
+static void print_and_exit(const char *str)
+{
+ fprintf(stderr, str);
+ exit(EXIT_FAILURE);
+}
+
+static int param_delim_checks(char *line, unsigned char *token)
{
if (!strcmp(line, PARAMS_START)) {
if (params_start || !params_end) {
@@ -83,7 +92,7 @@ static int param_checks(char *line)
} else {
params_start = 1;
params_end = 0;
- allocate_params_node();
+ *token = PSTART;
return 1;
}
} else if (!strcmp(line, PARAMS_END)) {
@@ -94,6 +103,7 @@ static int param_checks(char *line)
} else {
params_start = 0;
params_end = 1;
+ *token = PEND;
return 1;
}
} else if (!params_start) {
@@ -105,21 +115,26 @@ static int param_checks(char *line)
return 0;
}
-static void add_guid(efi_guid_t *guid_param, char *guid)
+static void add_guid(efi_guid_t **guid_param, char *guid)
{
unsigned char uuid[16];
- if (uuid_parse(guid, uuid)) {
- fprintf(stderr, "Wrong guid format\n");
- exit(EXIT_FAILURE);
- }
- convert_uuid_to_guid(uuid_buf);
- guidcpy(guid_param, uuid_buf);
+ uuid = malloc(sizeof(efi_guid_t));
+ if (!uuid)
+ print_and_exit(MALLOC_FAIL_STR);
+
+ if (uuid_parse(guid, uuid))
+ print_and_exit("Wrong guid format\n");
+
+ convert_uuid_to_guid(uuid);
+ *guid_param = (efi_guid_t *)uuid;
}
static void add_string(char **dst, char *val)
{
*dst = strdup(val);
+ if (!*dst)
+ print_and_exit(MALLOC_FAIL_STR);
}
static void match_and_populate_param(char *key, char *val,
@@ -135,16 +150,14 @@ static void match_and_populate_param(char *key, char *val,
return;
case 1:
param->image_index = strtoul(val, NULL, 0);
- if (param->image_index == ULONG_MAX) {
- fprintf(stderr, "Enter a valid value of index bewtween 1-255");
- exit(EXIT_FAILURE);
- }
+ if (param->image_index == ULONG_MAX)
+ print_and_exit("Enter a valid value of index bewtween 1-255");
return;
case 2:
- add_string(&param->priv_key, val);
+ add_string(&param->privkey_file, val);
return;
case 3:
- add_string(&param->pub_key_cert, val);
+ add_string(&param->cert_fie, val);
return;
case 4:
add_string(&param->input_file, val);
@@ -154,17 +167,13 @@ static void match_and_populate_param(char *key, char *val,
return;
case 6:
param->hardware_instance = strtoul(val, NULL, 0);
- if (param->hardware_instance == ULONG_MAX) {
- fprintf(stderr, "Enter a valid hardware instance value");
- exit(EXIT_FAILURE);
- }
+ if (param->hardware_instance == ULONG_MAX)
+ print_and_exit("Enter a valid hardware instance value");
return;
case 7:
param->monotonic_count = strtoull(val, NULL, 0);
- if (param->monotonic_count == ULLONG_MAX) {
- fprintf(stderr, "Enter a valid monotonic count value");
- exit(EXIT_FAILURE);
- }
+ if (param->monotonic_count == ULLONG_MAX)
+ print_and_exit("Enter a valid monotonic count value");
return;
case 8:
if (strcmp(val, "normal")) {
@@ -174,16 +183,13 @@ static void match_and_populate_param(char *key, char *val,
} else if (strcmp(val, "revert")) {
param->capsule = CAPSULE_REVERT;
} else {
- fprintf(stderr, "Invalid type of capsule");
- exit(EXIT_FAILURE);
+ print_and_exit("Invalid type of capsule");
}
return;
case 9:
param->oemflags = strtoul(val, NULL, 0);
- if (param->oemflags > 0xffff) {
- fprintf(stderr, "OemFlags must be between 0x0 and 0xffff\n");
- exit(EXIT_FAILURE);
- }
+ if (param->oemflags > 0xffff)
+ print_and_exit("OemFlags must be between 0x0 and 0xffff\n");
return;
}
}
@@ -194,23 +200,24 @@ static void match_and_populate_param(char *key, char *val,
exit(EXIT_FAILURE);
}
-static void get_capsule_params(char *line, struct efi_capsule_params *params)
+static int get_capsule_params(char *line, struct efi_capsule_params *params)
{
char *key;
char *val;
+ unsigned char token;
- if (param_checks(line))
- return;
+ if (param_delim_checks(line, &token))
+ return token;
key = strtok(line, ":");
- if (key) {
+ if (key)
val = strtok(NULL, "\0");
- } else {
- fprintf(stderr, "Expect the params in a key:value pair\n");
- exit(EXIT_FAILURE);
- }
+ else
+ print_and_exit("Expect the params in a key:value pair\n");
match_and_populate_param(key, val, params);
+
+ return 0;
}
static char *skip_whitespace(char *line, int len)
@@ -218,10 +225,8 @@ static char *skip_whitespace(char *line, int len)
char *ptr, *newline;
ptr = malloc(len);
- if (!ptr) {
- fprintf(stderr, MALLOC_FAIL_STR);
- exit(EXIT_FAILURE);
- }
+ if (!ptr)
+ print_and_exit(MALLOC_FAIL_STR);
for (newline = ptr; *line; line++)
if (!isblank(*line))
@@ -230,37 +235,95 @@ static char *skip_whitespace(char *line, int len)
return newline;
}
-int parse_capsule_cfg_file(const char *cfg_file, struct efi_capsule_params *params)
+static int parse_capsule_payload_params(FILE *fp, struct efi_capsule_params *params)
{
- FILE *fp;
char *line;
char *newline;
size_t n = 0;
ssize_t len;
- fp = fopen(cfg_file, "r");
- if (!fp) {
- fprintf(stderr, "Unable to open the capsule config file %s\n",
- cfg_file);
- exit(EXIT_FAILURE);
- }
-
- params_start = 0;
- params_end = 1;
-// qsort(capsule_params, ARRAY_SIZE(capsule_params), sizeof(char *),
-// paramcmp);
-
while ((len = getline(&line, &n, fp)) != -1) {
- line[len - 1] = '\0';
-
- if (len == 0)
+ if (len == 1 && line[len - 1] == '\n')
continue;
+ line[len - 1] = '\0';
+
newline = skip_whitespace(line, len);
if (newline[0] == '#')
continue;
- get_capsule_params(newline, params);
+ if (get_capsule_params(newline, params) == PEND)
+ return 0;
+ }
+
+ if (errno == EINVAL || errno == ENOMEM) {
+ fprintf(stderr, "getline() returned an error %s reading the line\n",
+ strerror(errno));
+ exit(EXIT_FAILURE);
+ } else if (params_start == 1 || params_end == 0) {
+ fprintf(stderr, "Params should be passed within braces. ");
+ fprintf(stderr, "Please check the documentation for reference config file syntax\n");
+ exit(EXIT_FAILURE);
+ } else {
+ return -1;
+ }
+}
+
+static void params_dependency_check(struct efi_capsule_params *params)
+{
+ /* check necessary parameters */
+ if ((params->capsule == CAPSULE_NORMAL_BLOB &&
+ ((!params->input_file || !params->capsule_file ||
+ !params->image_guid) ||
+ ((params->privkey_file && !params->cert_file) ||
+ (!params->privkey_file && params->cert_file)))) ||
+ (params->capsule != CAPSULE_NORMAL_BLOB &&
+ ((!params->capsule_file) ||
+ ((params->capsule == CAPSULE_ACCEPT) && !params->image_guid) ||
+ ((params->capsule == CAPSULE_REVERT) && params->image_guid)))) {
+ print_usage();
+ exit(EXIT_FAILURE);
+ }
+}
+
+static void generate_capsule(struct efi_capsule_params *params)
+{
+ if (params.capsule != CAPSULE_NORMAL_BLOB)
+ if (create_empty_capsule(params->capsule_file,
+ params->image_guid,
+ params->capsule ==
+ CAPSULE_ACCEPT) < 0)
+ print_and_exit("Creating empty capsule failed\n");
+ else if (create_fwbin(params->capsule_file, params->input_file,
+ params->image_guid, params->image_index,
+ params->hardware_instance,
+ params->monotonic_count,
+ params->privkey_file,
+ params->cert_file,
+ (uint16_t)params->oemflags) < 0)
+ print_and_exit("Creating firmware capsule failed\n");
+}
+
+void capsule_with_cfg_file(void)
+{
+ FILE *fp;
+ struct efi_capsule_params params = { 0 };
+
+ fp = fopen(CONFIG_CAPSULE_CFG_FILE, "r");
+ if (!fp) {
+ fprintf(stderr, "Unable to open the capsule config file %s\n",
+ CONFIG_CAPSULE_CFG_FILE);
+ exit(EXIT_FAILURE);
+ }
+
+ params_start = 0;
+ params_end = 1;
+
+ while (parse_capsule_payload_params(fp, &params) != -1) {
+ params_dependency_check(&params);
+ generate_capsule(&params);
+
+ memset(&params, 0, sizeof(struct efi_capsule_params));
}
}