From f5da50c339e5aee4e4b491edfa5d40c3c299e77f Mon Sep 17 00:00:00 2001 From: Chao Song Date: Mon, 4 Sep 2023 14:29:40 +0800 Subject: [PATCH] rimage: Add support for multiple toml file This patch adds support for multiple toml configuration file. Signed-off-by: Chao Song --- src/adsp_config.c | 60 +++++++++++++++++++++++++++++--- src/include/rimage/adsp_config.h | 2 +- src/include/rimage/rimage.h | 7 ++++ src/rimage.c | 15 ++++---- 4 files changed, 73 insertions(+), 11 deletions(-) diff --git a/src/adsp_config.c b/src/adsp_config.c index ac19607..da7ecfe 100644 --- a/src/adsp_config.c +++ b/src/adsp_config.c @@ -25,6 +25,7 @@ #include #include #include +#include /* map memory zone string name to enum value */ static enum snd_sof_fw_blk_type zone_name_to_idx(const char *name) @@ -2337,18 +2338,69 @@ static int adsp_parse_config_fd(FILE *fd, struct image *image) return ret; } +static FILE* adsp_conf_files_open(struct adsp_conf_files *files, const char *mode) +{ + struct stat statbuf; + char *buf; + int ret; + int i; + size_t file_size[MAX_SUPPORTED_CONF_FILES]; + size_t buf_size = 0; + + for (i = 0; i < files->file_count; i++) { + ret = stat(files->file[i], &statbuf); + if (ret) { + fprintf(stderr, "error: failed to stat file %s\n", files->file[i]); + return NULL; + } + + file_size[i] = statbuf.st_size; + buf_size += statbuf.st_size; + } + /* Reserve space for adding '\n' at the end of each file */ + buf_size += files->file_count; + files->buffer = malloc(buf_size); + + buf = files->buffer; + for (i = 0; i < files->file_count; i++) { + FILE *file; + size_t read; + + file = fopen(files->file[i], mode); + if (!file) { + fprintf(stderr, "error: failed to open file %s\n", files->file[i]); + return NULL; + } + read = fread(buf, 1, file_size[i], file); + if (read != file_size[i]) { + fprintf(stderr, "error: failed to read file %s\n", files->file[i]); + return NULL; + } + fclose(file); + + buf[file_size[i]] = '\n'; + buf += file_size[i] + 1; + } + return fmemopen(files->buffer, buf_size, mode); +} + +static void adsp_conf_files_close(struct adsp_conf_files *files) +{ + free(files->buffer); +} + /* public function, fully handle parsing process */ -int adsp_parse_config(const char *file, struct image *image) +int adsp_parse_config(struct adsp_conf_files *files, struct image *image) { FILE *fd; int ret; - fd = fopen(file, "r"); + fd = adsp_conf_files_open(files, "r"); if (!fd) - return file_error("unable to open file for reading", file); + return -EINVAL; ret = adsp_parse_config_fd(fd, image); - fclose(fd); + adsp_conf_files_close(files); return ret; } diff --git a/src/include/rimage/adsp_config.h b/src/include/rimage/adsp_config.h index e87c78c..76f7aa6 100644 --- a/src/include/rimage/adsp_config.h +++ b/src/include/rimage/adsp_config.h @@ -5,5 +5,5 @@ #include #include -int adsp_parse_config(const char *file, struct image *image); +int adsp_parse_config(struct adsp_conf_files *file, struct image *image); void adsp_free(struct adsp *adsp); diff --git a/src/include/rimage/rimage.h b/src/include/rimage/rimage.h index 2b9a26e..bde81c0 100644 --- a/src/include/rimage/rimage.h +++ b/src/include/rimage/rimage.h @@ -15,6 +15,7 @@ #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0])) #define MAX_MODULES 32 +#define MAX_SUPPORTED_CONF_FILES 8 struct adsp; @@ -128,6 +129,12 @@ struct adsp { int exec_boot_ldr; }; +struct adsp_conf_files { + uint32 file_count; + char *file[MAX_SUPPORTED_CONF_FILES]; + char *buffer; +}; + int ri_manifest_sign_v1_5(struct image *image); int ri_manifest_sign_v1_8(struct image *image); int ri_manifest_sign_v2_5(struct image *image); diff --git a/src/rimage.c b/src/rimage.c index 7c89e09..746b7ee 100644 --- a/src/rimage.c +++ b/src/rimage.c @@ -40,11 +40,12 @@ int main(int argc, char *argv[]) { struct image image; struct adsp *heap_adsp; - const char *adsp_config = NULL; - int opt, ret, i, first_non_opt; + struct adsp_conf_files files; + int opt, ret, first_non_opt; int use_ext_man = 0; unsigned int pv_bit = 0; bool imr_type_override = false; + int i = 0; memset(&image, 0, sizeof(image)); @@ -84,7 +85,8 @@ int main(int argc, char *argv[]) use_ext_man = 1; break; case 'c': - adsp_config = optarg; + files.file[i] = optarg; + i++; break; case 'y': image.verify_file = optarg; @@ -110,11 +112,12 @@ int main(int argc, char *argv[]) first_non_opt = optind; /* we must have config */ - if (!adsp_config) { + if (i == 0 || i >= MAX_SUPPORTED_CONF_FILES) { usage(argv[0]); - fprintf(stderr, "error: must have adsp desc\n"); + fprintf(stderr, "error: invalid number of adsp_desc provided\n"); return -EINVAL; } + files.file_count = i; /* requires private key */ if (!image.key_name) { @@ -161,7 +164,7 @@ int main(int argc, char *argv[]) } image.adsp = heap_adsp; memset(heap_adsp, 0, sizeof(*heap_adsp)); - ret = adsp_parse_config(adsp_config, &image); + ret = adsp_parse_config(&files, &image); if (ret < 0) goto out;