On 02/12/2020 13:21, Jan Kiszka wrote:
> On 01.12.20 16:01, [ext] Silvano Cirujano Cuesta wrote:
>> The tools bg_setenv and bg_printenv are mostly oriented at running systems
>> that have the partitions providing BG environments already mounted.
>>
>> This patch extends bg_printenv so that the environment can be read directly
>> from whatever valid BG environment file. This usage is very usefull in
>> development, diagnose, testing,... scenarios.
>>
>> Signed-off-by: Silvano Cirujano Cuesta <[email protected]>
>> ---
>> env/env_api_fat.c | 4 +--
>> env/env_config_file.c | 18 +++++++---
>> include/env_config_file.h | 3 +-
>> tools/bg_setenv.c | 69 +++++++++++++++++++++++++++++++++++----
>> 4 files changed, 80 insertions(+), 14 deletions(-)
>>
>> diff --git a/env/env_api_fat.c b/env/env_api_fat.c
>> index c443408..130c161 100644
>> --- a/env/env_api_fat.c
>> +++ b/env/env_api_fat.c
>> @@ -67,7 +67,7 @@ bool read_env(CONFIG_PART *part, BG_ENVDATA *env)
>> part->mountpoint);
>> }
>> FILE *config;
>> - config = open_config_file(part, "rb");
>> + config = open_config_file_from_part(part, "rb");
>> if (!config) {
>> return false;
>> }
>> @@ -105,7 +105,7 @@ bool write_env(CONFIG_PART *part, BG_ENVDATA *env)
>> part->mountpoint);
>> }
>> FILE *config;
>> - config = open_config_file(part, "wb");
>> + config = open_config_file_from_part(part, "wb");
>> if (!config) {
>> VERBOSE(stderr, "Could not open config file for writing.\n");
>> return false;
>> diff --git a/env/env_config_file.c b/env/env_config_file.c
>> index 873fe10..e270fbb 100644
>> --- a/env/env_config_file.c
>> +++ b/env/env_config_file.c
>> @@ -18,7 +18,18 @@
>> #include "env_disk_utils.h"
>> #include "env_config_file.h"
>>
>> -FILE *open_config_file(CONFIG_PART *cfgpart, char *mode)
>> +FILE *open_config_file(char *configfilepath, char *mode)
>> +{
>> + VERBOSE(stdout, "Probing config file at %s.\n", configfilepath);
>> + FILE *config = fopen(configfilepath, mode);
>> + if (config) {
>> + return config;
>> + } else {
>> + return NULL;
>> + }
>> +}
>> +
>> +FILE *open_config_file_from_part(CONFIG_PART *cfgpart, char *mode)
>> {
>> char *configfilepath;
>>
>> @@ -33,8 +44,7 @@ FILE *open_config_file(CONFIG_PART *cfgpart, char *mode)
>> strcpy(configfilepath, cfgpart->mountpoint);
>> strcat(configfilepath, "/");
>> strcat(configfilepath, FAT_ENV_FILENAME);
>> - VERBOSE(stdout, "Probing config file at %s.\n", configfilepath);
>> - FILE *config = fopen(configfilepath, mode);
>> + FILE *config = open_config_file(configfilepath, mode);
>> free(configfilepath);
>> return config;
>> }
>> @@ -74,7 +84,7 @@ bool probe_config_file(CONFIG_PART *cfgpart)
>> cfgpart->devpath, cfgpart->mountpoint);
>> bool result = false;
>> FILE *config;
>> - if (!(config = open_config_file(cfgpart, "rb"))) {
>> + if (!(config = open_config_file_from_part(cfgpart, "rb"))) {
>> printf_debug(
>> "Could not open config file on partition %s.\n",
>> FAT_ENV_FILENAME);
>> diff --git a/include/env_config_file.h b/include/env_config_file.h
>> index 8fbe8fa..2679174 100644
>> --- a/include/env_config_file.h
>> +++ b/include/env_config_file.h
>> @@ -15,7 +15,8 @@
>> #ifndef __ENV_CONFIG_FILE_H__
>> #define __ENV_CONFIG_FILE_H__
>>
>> -FILE *open_config_file(CONFIG_PART *cfgpart, char *mode);
>> +FILE *open_config_file_from_part(CONFIG_PART *cfgpart, char *mode);
>> +FILE *open_config_file(char *configfilepath, char *mode);
>> int close_config_file(FILE *config_file_handle);
>> bool probe_config_file(CONFIG_PART *cfgpart);
>>
>> diff --git a/tools/bg_setenv.c b/tools/bg_setenv.c
>> index d068a2b..53c004a 100644
>> --- a/tools/bg_setenv.c
>> +++ b/tools/bg_setenv.c
>> @@ -18,6 +18,7 @@
>> #include "ebgenv.h"
>> #include "uservars.h"
>> #include "version.h"
>> +#include "env_config_file.h"
>>
>> static char doc[] =
>> "bg_setenv/bg_printenv - Environment tool for the EFI Boot Guard";
>> @@ -47,12 +48,14 @@ static struct argp_option options_setenv[] = {
>> };
>>
>> static struct argp_option options_printenv[] = {
>> + {"filepath", 'f', "ENVFILE", 0, "Read environment from file. Expects "
>> + "a valid EFI Boot Guard environment
>> file."},
>> {"verbose", 'v', 0, 0, "Be verbose"},
>> {"version", 'V', 0, 0, "Print version"},
>> - {}
>> -};
>> + {0}};
>>
>> struct arguments {
>> + bool printenv;
>> int which_part;
>> };
>>
>> @@ -350,9 +353,16 @@ static error_t parse_opt(int key, char *arg, struct
>> argp_state *state)
>> (uint8_t *)arg, strlen(arg) + 1);
>> break;
>> case 'f':
>> - res = asprintf(&envfilepath, "%s/%s", arg, FAT_ENV_FILENAME);
>> - if (res == -1) {
>> - return ENOMEM;
>> + if (arguments->printenv) {
>> + res = asprintf(&envfilepath, "%s", arg);
>> + if (res == -1) {
>> + return ENOMEM;
>> + }
>> + } else {
>> + res = asprintf(&envfilepath, "%s/%s", arg,
>> FAT_ENV_FILENAME);
>> + if (res == -1) {
>> + return ENOMEM;
>> + }
>> }
>> break;
>> case 'c':
>> @@ -531,6 +541,46 @@ static void dump_envs(void)
>> }
>> }
>>
>> +static bool get_env(char *configfilepath, BG_ENVDATA *data)
>> +{
>> + FILE *config;
>> + char buffer[ENV_STRING_LENGTH];
> cppcheck found this as unused - forgotten or hidden issue?
>
> Jan
Left over of a first draft where I was reading and dumping the fields
individually instead of using "dump_env". Can be safely removed.
Silvano
>
>> + bool result = true;
>> +
>> + if (!(config = open_config_file(configfilepath, "rb"))) {
>> + return false;
>> + }
>> +
>> + if (!(fread(data, sizeof(BG_ENVDATA), 1, config) == 1)) {
>> + VERBOSE(stderr, "Error reading environment data from %s\n",
>> + configfilepath);
>> + if (feof(config)) {
>> + VERBOSE(stderr, "End of file encountered.\n");
>> + }
>> + result = false;
>> + }
>> +
>> + if (close_config_file(config)) {
>> + VERBOSE(stderr,
>> + "Error closing environment file after reading.\n");
>> + };
>> + return result;
>> +}
>> +
>> +static int printenv_from_file(char *envfilepath) {
>> + int success = 0;
>> + BG_ENVDATA data;
>> +
>> + success = get_env(envfilepath, &data);
>> + if (success) {
>> + dump_env(&data);
>> + return 0;
>> + } else {
>> + fprintf(stderr, "Error reading environment file.\n");
>> + return 1;
>> + }
>> +}
>> +
>> static int dumpenv_to_file(char *envfilepath) {
>> /* execute journal and write to file */
>> int result = 0;
>> @@ -591,6 +641,7 @@ int main(int argc, char **argv)
>> }
>>
>> struct arguments arguments;
>> + arguments.printenv = !write_mode;
>> arguments.which_part = 0;
>>
>> STAILQ_INIT(&head);
>> @@ -605,9 +656,13 @@ int main(int argc, char **argv)
>>
>> /* arguments are parsed, journal is filled */
>>
>> - /* is output to file ? */
>> + /* is output to file or input from file ? */
>> if (envfilepath) {
>> - result = dumpenv_to_file(envfilepath);
>> + if (write_mode) {
>> + result = dumpenv_to_file(envfilepath);
>> + } else {
>> + result = printenv_from_file(envfilepath);
>> + }
>> free(envfilepath);
>> return result;
>> }
>>
--
You received this message because you are subscribed to the Google Groups "EFI
Boot Guard" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/efibootguard-dev/4403b478-0b84-550c-4f99-a2bfb064ff48%40siemens.com.