diff --git a/src/include/kernel/tokens.h b/src/include/kernel/tokens.h index e09178d722ac..9290fa5e2a0a 100644 --- a/src/include/kernel/tokens.h +++ b/src/include/kernel/tokens.h @@ -71,6 +71,7 @@ /* Token retired with ABI 3.2, do not use for new capabilities * #define SOF_TKN_COMP_PRELOAD_COUNT 403 */ +#define SOF_TKN_COMP_CORE_ID 404 /* SSP */ #define SOF_TKN_INTEL_SSP_CLKS_CONTROL 500 diff --git a/tools/testbench/topology.c b/tools/testbench/topology.c index a647b99aee21..b216e6ab98f6 100644 --- a/tools/testbench/topology.c +++ b/tools/testbench/topology.c @@ -7,16 +7,17 @@ /* Topology loader to set up components and pipeline */ +#include +#include #include -#include -#include #include +#include #include -#include #include -#include "testbench/file.h" +#include #include #include "testbench/common_test.h" +#include "testbench/file.h" FILE *file; char pipeline_string[DEBUG_MSG_LEN]; @@ -168,7 +169,7 @@ static int tplg_load_fileread(int comp_id, int pipeline_id, int size, array = (struct snd_soc_tplg_vendor_array *)malloc(size); if (!array) { fprintf(stderr, "error: mem alloc\n"); - return -EINVAL; + return -errno; } /* read vendor tokens */ @@ -226,7 +227,7 @@ static int tplg_load_filewrite(int comp_id, int pipeline_id, int size, array = (struct snd_soc_tplg_vendor_array *)malloc(size); if (!array) { fprintf(stderr, "error: mem alloc\n"); - return -EINVAL; + return -errno; } /* read vendor tokens */ @@ -572,7 +573,7 @@ static int process_append_data(struct sof_ipc_comp_process **process_ipc, *process_ipc = malloc(ipc_size); if (!(*process_ipc)) { fprintf(stderr, "error: Failed to allocate IPC\n"); - return -ENOMEM; + return -errno; } /* Copy header */ @@ -689,16 +690,22 @@ int parse_topology(struct sof *sof, struct shared_lib_table *library_table, lib_table = library_table; /* file size */ - fseek(file, 0, SEEK_END); + if (fseek(file, 0, SEEK_END)) { + fprintf(stderr, "error: seek to end of topology\n"); + return -errno; + } file_size = ftell(file); - fseek(file, 0, SEEK_SET); + if (fseek(file, 0, SEEK_SET)) { + fprintf(stderr, "error: seek to beginning of topology\n"); + return -errno; + } /* allocate memory */ size = sizeof(struct snd_soc_tplg_hdr); hdr = (struct snd_soc_tplg_hdr *)malloc(size); if (!hdr) { fprintf(stderr, "error: mem alloc\n"); - return -EINVAL; + return -errno; } debug_print("topology parsing start\n"); @@ -750,10 +757,16 @@ int parse_topology(struct sof *sof, struct shared_lib_table *library_table, if (ftell(file) == file_size) goto finish; break; + default: - fseek(file, hdr->payload_size, SEEK_CUR); + if (fseek(file, hdr->payload_size, SEEK_CUR)) { + fprintf(stderr, "error: fseek payload size\n"); + return -errno; + } + if (ftell(file) == file_size) goto finish; + break; } } diff --git a/tools/tplg_parser/include/tplg_parser/topology.h b/tools/tplg_parser/include/tplg_parser/topology.h index d04ffb25320f..d98acdb71496 100644 --- a/tools/tplg_parser/include/tplg_parser/topology.h +++ b/tools/tplg_parser/include/tplg_parser/topology.h @@ -80,6 +80,11 @@ static const struct sof_topology_token buffer_tokens[] = { offsetof(struct sof_ipc_buffer, caps), 0}, }; +static const struct sof_topology_token buffer_comp_tokens[] = { + {SOF_TKN_COMP_CORE_ID, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_uint32_t, + offsetof(struct sof_ipc_comp, core), 0}, +}; + /* scheduling */ static const struct sof_topology_token sched_tokens[] = { {SOF_TKN_SCHED_PERIOD, SND_SOC_TPLG_TUPLE_TYPE_WORD, diff --git a/tools/tplg_parser/tplg_parser.c b/tools/tplg_parser/tplg_parser.c index bc2b6c96df02..b0a85199bd80 100644 --- a/tools/tplg_parser/tplg_parser.c +++ b/tools/tplg_parser/tplg_parser.c @@ -117,6 +117,8 @@ int tplg_load_buffer(int comp_id, int pipeline_id, int size, struct sof_ipc_buffer *buffer, FILE *file) { struct snd_soc_tplg_vendor_array *array; + size_t read_size; + size_t parsed_size = 0; int ret = 0; /* configure buffer */ @@ -131,22 +133,51 @@ int tplg_load_buffer(int comp_id, int pipeline_id, int size, array = (struct snd_soc_tplg_vendor_array *)malloc(size); if (!array) { fprintf(stderr, "error: malloc fail during load_buffer\n"); - free(array); - return -EINVAL; + return -errno; } - ret = fread(array, sizeof(struct snd_soc_tplg_vendor_array), 1, file); - if (ret != 1) { - fprintf(stderr, "error: fread fail during load_buffer\n"); - free(array); - return -EINVAL; - } + /* read vendor tokens */ + while (parsed_size < size) { + read_size = sizeof(struct snd_soc_tplg_vendor_array); + ret = fread(array, read_size, 1, file); + if (ret != 1) { + fprintf(stderr, + "error: fread fail during load_buffer\n"); + free(array); + return -EINVAL; + } - tplg_read_array(array, file); + ret = tplg_read_array(array, file); + if (ret) { + fprintf(stderr, "error: read array fail\n"); + free(array); + return ret; + } - /* parse buffer tokens */ - ret = sof_parse_tokens(buffer, buffer_tokens, - ARRAY_SIZE(buffer_tokens), array, size); + /* parse buffer comp tokens */ + ret = sof_parse_tokens(&buffer->comp, buffer_comp_tokens, + ARRAY_SIZE(buffer_comp_tokens), array, + array->size); + if (ret) { + fprintf(stderr, "error: parse buffer comp tokens %d\n", + size); + free(array); + return -EINVAL; + } + + /* parse buffer tokens */ + ret = sof_parse_tokens(buffer, buffer_tokens, + ARRAY_SIZE(buffer_tokens), array, + array->size); + if (ret) { + fprintf(stderr, "error: parse buffer tokens %d\n", + size); + free(array); + return -EINVAL; + } + + parsed_size += array->size; + } free(array); return 0; @@ -172,7 +203,7 @@ int tplg_load_pcm(int comp_id, int pipeline_id, int size, int dir, array = (struct snd_soc_tplg_vendor_array *)malloc(size); if (!array) { fprintf(stderr, "error: mem alloc\n"); - return -EINVAL; + return -errno; } /* read vendor tokens */ @@ -181,7 +212,14 @@ int tplg_load_pcm(int comp_id, int pipeline_id, int size, int dir, ret = fread(array, read_size, 1, file); if (ret != 1) return -EINVAL; - tplg_read_array(array, file); + + ret = tplg_read_array(array, file); + if (ret) { + fprintf(stderr, "error: read array fail\n"); + free(array); + return ret; + } + /* parse comp tokens */ ret = sof_parse_tokens(&host->config, comp_tokens, @@ -231,7 +269,7 @@ int tplg_load_dai(int comp_id, int pipeline_id, int size, array = (struct snd_soc_tplg_vendor_array *)malloc(size); if (!array) { fprintf(stderr, "error: mem alloc\n"); - return -EINVAL; + return -errno; } /* read vendor tokens */ @@ -243,7 +281,12 @@ int tplg_load_dai(int comp_id, int pipeline_id, int size, return -EINVAL; } - tplg_read_array(array, file); + ret = tplg_read_array(array, file); + if (ret) { + fprintf(stderr, "error: read array fail\n"); + free(array); + return ret; + } ret = sof_parse_tokens(comp_dai, dai_tokens, ARRAY_SIZE(dai_tokens), array, @@ -284,7 +327,7 @@ int tplg_load_pga(int comp_id, int pipeline_id, int size, array = (struct snd_soc_tplg_vendor_array *)malloc(size); if (!array) { fprintf(stderr, "error: mem alloc\n"); - return -EINVAL; + return -errno; } /* read vendor tokens */ @@ -295,7 +338,13 @@ int tplg_load_pga(int comp_id, int pipeline_id, int size, free(array); return -EINVAL; } - tplg_read_array(array, file); + + ret = tplg_read_array(array, file); + if (ret) { + fprintf(stderr, "error: read array fail\n"); + free(array); + return ret; + } /* parse comp tokens */ ret = sof_parse_tokens(&volume->config, comp_tokens, @@ -353,7 +402,7 @@ int tplg_load_pipeline(int comp_id, int pipeline_id, int size, array = (struct snd_soc_tplg_vendor_array *)malloc(size); if (!array) { fprintf(stderr, "error: mem alloc\n"); - return -EINVAL; + return -errno; } /* read vendor arrays */ @@ -366,7 +415,8 @@ int tplg_load_pipeline(int comp_id, int pipeline_id, int size, } ret = tplg_read_array(array, file); - if (ret < 0) { + if (ret) { + fprintf(stderr, "error: read array fail\n"); free(array); return -EINVAL; } @@ -415,7 +465,7 @@ int tplg_load_one_control(struct snd_soc_tplg_ctl_hdr **ctl, char **priv_data, ctl_hdr = (struct snd_soc_tplg_ctl_hdr *)malloc(hdr_size); if (!ctl_hdr) { fprintf(stderr, "error: mem alloc\n"); - return -EINVAL; + return -errno; } /* read control header */ @@ -435,14 +485,19 @@ int tplg_load_one_control(struct snd_soc_tplg_ctl_hdr **ctl, char **priv_data, case SND_SOC_TPLG_DAPM_CTL_VOLSW: /* load mixer type control */ rewind_size = sizeof(struct snd_soc_tplg_ctl_hdr); - fseek(file, rewind_size * -1, SEEK_CUR); + if (fseek(file, rewind_size * -1, SEEK_CUR)) { + ret = -errno; + goto err; + } + read_size = sizeof(struct snd_soc_tplg_mixer_control); mixer_ctl = malloc(read_size); if (!mixer_ctl) { fprintf(stderr, "error: mem alloc\n"); - ret = -EINVAL; + ret = -errno; goto err; } + ret = fread(mixer_ctl, read_size, 1, file); if (ret != 1) { ret = -EINVAL; @@ -450,7 +505,11 @@ int tplg_load_one_control(struct snd_soc_tplg_ctl_hdr **ctl, char **priv_data, } /* skip mixer private data */ - fseek(file, mixer_ctl->priv.size, SEEK_CUR); + if (fseek(file, mixer_ctl->priv.size, SEEK_CUR)) { + ret = -errno; + goto err; + } + *ctl = (struct snd_soc_tplg_ctl_hdr *)mixer_ctl; break; @@ -461,14 +520,19 @@ int tplg_load_one_control(struct snd_soc_tplg_ctl_hdr **ctl, char **priv_data, case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE: /* load enum type control */ rewind_size = sizeof(struct snd_soc_tplg_ctl_hdr); - fseek(file, rewind_size * -1, SEEK_CUR); + if (fseek(file, rewind_size * -1, SEEK_CUR)) { + ret = -errno; + goto err; + } + read_size = sizeof(struct snd_soc_tplg_enum_control); enum_ctl = malloc(read_size); if (!enum_ctl) { fprintf(stderr, "error: mem alloc\n"); - ret = -EINVAL; + ret = -errno; goto err; } + ret = fread(enum_ctl, read_size, 1, file); if (ret != 1) { ret = -EINVAL; @@ -476,19 +540,27 @@ int tplg_load_one_control(struct snd_soc_tplg_ctl_hdr **ctl, char **priv_data, } /* skip enum private data */ - fseek(file, enum_ctl->priv.size, SEEK_CUR); + if (fseek(file, enum_ctl->priv.size, SEEK_CUR)) { + ret = -errno; + goto err; + } + *ctl = (struct snd_soc_tplg_ctl_hdr *)enum_ctl; break; case SND_SOC_TPLG_CTL_BYTES: /* load bytes type controls */ rewind_size = sizeof(struct snd_soc_tplg_ctl_hdr); - fseek(file, rewind_size * -1, SEEK_CUR); + if (fseek(file, rewind_size * -1, SEEK_CUR)) { + ret = -errno; + goto err; + } + read_size = sizeof(struct snd_soc_tplg_bytes_control); bytes_ctl = malloc(read_size); if (!bytes_ctl) { fprintf(stderr, "error: mem alloc\n"); - ret = -EINVAL; + ret = -errno; goto err; } @@ -503,7 +575,7 @@ int tplg_load_one_control(struct snd_soc_tplg_ctl_hdr **ctl, char **priv_data, *priv_data = malloc(read_size); if (!(*priv_data)) { fprintf(stderr, "error: mem alloc\n"); - ret = -EINVAL; + ret = -errno; goto err; } @@ -552,7 +624,7 @@ int tplg_load_controls(int num_kcontrols, FILE *file) ctl_hdr = (struct snd_soc_tplg_ctl_hdr *)malloc(size); if (!ctl_hdr) { fprintf(stderr, "error: mem alloc\n"); - return -EINVAL; + return -errno; } size = sizeof(struct snd_soc_tplg_mixer_control); @@ -560,7 +632,7 @@ int tplg_load_controls(int num_kcontrols, FILE *file) if (!mixer_ctl) { fprintf(stderr, "error: mem alloc\n"); free(ctl_hdr); - return -EINVAL; + return -errno; } size = sizeof(struct snd_soc_tplg_enum_control); @@ -569,7 +641,7 @@ int tplg_load_controls(int num_kcontrols, FILE *file) fprintf(stderr, "error: mem alloc\n"); free(ctl_hdr); free(mixer_ctl); - return -EINVAL; + return -errno; } size = sizeof(struct snd_soc_tplg_bytes_control); @@ -579,7 +651,7 @@ int tplg_load_controls(int num_kcontrols, FILE *file) free(ctl_hdr); free(mixer_ctl); free(enum_ctl); - return -EINVAL; + return -errno; } for (j = 0; j < num_kcontrols; j++) { @@ -599,10 +671,13 @@ int tplg_load_controls(int num_kcontrols, FILE *file) case SND_SOC_TPLG_CTL_VOLSW_XR_SX: case SND_SOC_TPLG_CTL_RANGE: case SND_SOC_TPLG_DAPM_CTL_VOLSW: - /* load mixer type control */ read_size = sizeof(struct snd_soc_tplg_ctl_hdr); - fseek(file, read_size * -1, SEEK_CUR); + if (fseek(file, read_size * -1, SEEK_CUR)) { + return -errno; + goto err; + } + read_size = sizeof(struct snd_soc_tplg_mixer_control); ret = fread(mixer_ctl, read_size, 1, file); if (ret != 1) { @@ -611,17 +686,24 @@ int tplg_load_controls(int num_kcontrols, FILE *file) } /* skip mixer private data */ - fseek(file, mixer_ctl->priv.size, SEEK_CUR); + if (fseek(file, mixer_ctl->priv.size, SEEK_CUR)) { + ret = -errno; + goto err; + } break; + case SND_SOC_TPLG_CTL_ENUM: case SND_SOC_TPLG_CTL_ENUM_VALUE: case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE: case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT: case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE: - /* load enum type control */ read_size = sizeof(struct snd_soc_tplg_ctl_hdr); - fseek(file, read_size * -1, SEEK_CUR); + if (fseek(file, read_size * -1, SEEK_CUR)) { + ret = -errno; + goto err; + } + read_size = sizeof(struct snd_soc_tplg_enum_control); ret = fread(enum_ctl, read_size, 1, file); if (ret != 1) { @@ -630,13 +712,20 @@ int tplg_load_controls(int num_kcontrols, FILE *file) } /* skip enum private data */ - fseek(file, enum_ctl->priv.size, SEEK_CUR); + if (fseek(file, enum_ctl->priv.size, SEEK_CUR)) { + ret = -errno; + goto err; + } break; - case SND_SOC_TPLG_CTL_BYTES: + case SND_SOC_TPLG_CTL_BYTES: /* load bytes type controls */ read_size = sizeof(struct snd_soc_tplg_ctl_hdr); - fseek(file, read_size * -1, SEEK_CUR); + if (fseek(file, read_size * -1, SEEK_CUR)) { + ret = -errno; + goto err; + } + read_size = sizeof(struct snd_soc_tplg_bytes_control); ret = fread(bytes_ctl, read_size, 1, file); if (ret != 1) { @@ -645,8 +734,12 @@ int tplg_load_controls(int num_kcontrols, FILE *file) } /* skip bytes private data */ - fseek(file, bytes_ctl->priv.size, SEEK_CUR); + if (fseek(file, bytes_ctl->priv.size, SEEK_CUR)) { + ret = -errno; + goto err; + } break; + default: printf("info: control type not supported\n"); return -EINVAL; @@ -674,7 +767,7 @@ int tplg_load_src(int comp_id, int pipeline_id, int size, array = (struct snd_soc_tplg_vendor_array *)malloc(size); if (!array) { fprintf(stderr, "error: mem alloc for src vendor array\n"); - return -EINVAL; + return -errno; } /* read vendor tokens */ @@ -686,7 +779,12 @@ int tplg_load_src(int comp_id, int pipeline_id, int size, return -EINVAL; } - tplg_read_array(array, file); + ret = tplg_read_array(array, file); + if (ret) { + fprintf(stderr, "error: read array fail\n"); + free(array); + return ret; + } /* parse comp tokens */ ret = sof_parse_tokens(&src->config, comp_tokens, @@ -741,7 +839,7 @@ int tplg_load_asrc(int comp_id, int pipeline_id, int size, array = (struct snd_soc_tplg_vendor_array *)malloc(size); if (!array) { fprintf(stderr, "error: mem alloc for asrc vendor array\n"); - return -EINVAL; + return -errno; } /* read vendor tokens */ @@ -753,7 +851,12 @@ int tplg_load_asrc(int comp_id, int pipeline_id, int size, return -EINVAL; } - tplg_read_array(array, file); + ret = tplg_read_array(array, file); + if (ret) { + fprintf(stderr, "error: read array fail\n"); + free(array); + return ret; + } /* parse comp tokens */ ret = sof_parse_tokens(&asrc->config, comp_tokens, @@ -809,7 +912,7 @@ int tplg_load_process(int comp_id, int pipeline_id, int size, array = (struct snd_soc_tplg_vendor_array *)malloc(size); if (!array) { fprintf(stderr, "error: mem alloc for asrc vendor array\n"); - return -EINVAL; + return -errno; } /* read vendor tokens */ @@ -821,7 +924,12 @@ int tplg_load_process(int comp_id, int pipeline_id, int size, return -EINVAL; } - tplg_read_array(array, file); + ret = tplg_read_array(array, file); + if (ret) { + fprintf(stderr, "error: read array fail\n"); + free(array); + return ret; + } /* parse comp tokens */ ret = sof_parse_tokens(&process->config, comp_tokens, @@ -877,7 +985,7 @@ int tplg_load_mixer(int comp_id, int pipeline_id, int size, array = (struct snd_soc_tplg_vendor_array *)malloc(size); if (!array) { fprintf(stderr, "error: mem alloc for src vendor array\n"); - return -EINVAL; + return -errno; } /* read vendor tokens */ @@ -889,7 +997,12 @@ int tplg_load_mixer(int comp_id, int pipeline_id, int size, return -EINVAL; } - tplg_read_array(array, file); + ret = tplg_read_array(array, file); + if (ret) { + fprintf(stderr, "error: read array fail\n"); + free(array); + return ret; + } /* parse comp tokens */ ret = sof_parse_tokens(&mixer->config, comp_tokens, @@ -943,7 +1056,7 @@ int tplg_load_graph(int num_comps, int pipeline_id, graph_elem = (struct snd_soc_tplg_dapm_graph_elem *)malloc(size); if (!graph_elem) { fprintf(stderr, "error: mem alloc\n"); - return -EINVAL; + return -errno; } /* set up component connections */ @@ -1006,7 +1119,7 @@ int load_widget(void *dev, int dev_type, struct comp_info *temp_comp_list, widget = (struct snd_soc_tplg_dapm_widget *)malloc(size); if (!widget) { fprintf(stderr, "error: mem alloc\n"); - return -EINVAL; + return -errno; } /* read widget data */ @@ -1109,7 +1222,11 @@ int load_widget(void *dev, int dev_type, struct comp_info *temp_comp_list, break; /* unsupported widgets */ default: - fseek(file, widget->priv.size, SEEK_CUR); + if (fseek(file, widget->priv.size, SEEK_CUR)) { + fprintf(stderr, "error: fseek unsupported widget\n"); + return -errno; + } + printf("info: Widget type not supported %d\n", widget->id); ret = tplg_load_controls(widget->num_kcontrols, file); if (ret < 0) { @@ -1144,7 +1261,7 @@ int sof_parse_tokens(void *object, const struct sof_topology_token *tokens, priv_size -= asize; if (priv_size < 0) { - fprintf(stderr, "error: invalid array size 0x%x\n", + fprintf(stderr, "error: invalid priv size 0x%x\n", asize); return -EINVAL; }