1 /*
2 Copyright(c) 2019 Red Hat Inc.
3 Copyright(c) 2014-2015 Intel Corporation
4 Copyright(c) 2010-2011 Texas Instruments Incorporated,
5 All rights reserved.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of version 2 of the GNU General Public License as
9 published by the Free Software Foundation.
10
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
19 The full GNU General Public License is included in this distribution
20 in the file called LICENSE.GPL.
21 */
22
23 #include <stdbool.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <stdint.h>
27 #include <fcntl.h>
28 #include <unistd.h>
29 #include <errno.h>
30 #include <string.h>
31 #include <sys/stat.h>
32 #include <getopt.h>
33 #include <assert.h>
34
35 #include <alsa/asoundlib.h>
36 #include <alsa/topology.h>
37 #include "gettext.h"
38 #include "version.h"
39 #include "topology.h"
40
41 bool pre_process_config = false;
42
43 static snd_output_t *log;
44
usage(const char * name)45 static void usage(const char *name)
46 {
47 printf(
48 _("Usage: %s [OPTIONS]...\n"
49 "\n"
50 "-h, --help help\n"
51 "-c, --compile=FILE compile configuration file\n"
52 "-p, --pre-process pre-process Topology2.0 configuration file before compilation\n"
53 "-P, --pre-process=FILE pre-process Topology2.0 configuration file\n"
54 "-d, --decode=FILE decode binary topology file\n"
55 "-n, --normalize=FILE normalize configuration file\n"
56 "-u, --dump=FILE dump (reparse) configuration file\n"
57 "-v, --verbose=LEVEL set verbosity level (0...1)\n"
58 "-o, --output=FILE set output file\n"
59 #if SND_LIB_VER(1, 2, 5) < SND_LIB_VERSION
60 "-D, --define=ARGS define variables (VAR1=VAL1[,VAR2=VAL2] ...)\n"
61 #endif
62 "-s, --sort sort the identifiers in the normalized output\n"
63 "-g, --group save configuration by group indexes\n"
64 "-x, --nocheck save configuration without additional integrity checks\n"
65 "-z, --dapm-nosort do not sort the DAPM widgets\n"
66 "-V, --version print version\n"
67 ), name);
68 }
69
version(const char * name)70 static void version(const char *name)
71 {
72 printf(
73 _("%s version %s\n"
74 "libasound version %s\n"
75 "libatopology version %s\n"
76 ), name, SND_UTIL_VERSION_STR,
77 snd_asoundlib_version(), snd_tplg_version());
78 }
79
load(const char * source_file,void ** dst,size_t * dst_size)80 static int load(const char *source_file, void **dst, size_t *dst_size)
81 {
82 int fd;
83 void *buf, *buf2;
84 size_t size, pos;
85 ssize_t r;
86
87 if (strcmp(source_file, "-") == 0) {
88 fd = fileno(stdin);
89 } else {
90 fd = open(source_file, O_RDONLY);
91 if (fd < 0) {
92 fprintf(stderr, _("Unable to open input file '%s': %s\n"),
93 source_file, strerror(-errno));
94 return 1;
95 }
96 }
97
98 size = 16*1024;
99 pos = 0;
100 buf = malloc(size);
101 if (buf == NULL)
102 goto _nomem;
103 while (1) {
104 r = read(fd, buf + pos, size - pos);
105 if (r < 0 && (errno == EAGAIN || errno == EINTR))
106 continue;
107 if (r <= 0)
108 break;
109 pos += r;
110 size += 8*1024;
111 buf2 = realloc(buf, size);
112 if (buf2 == NULL)
113 goto _nomem;
114 buf = buf2;
115 }
116 if (r < 0) {
117 fprintf(stderr, _("Read error: %s\n"), strerror(-errno));
118 goto _err;
119 }
120
121 if (fd != fileno(stdin))
122 close(fd);
123
124 *dst = buf;
125 *dst_size = pos;
126 return 0;
127
128 _nomem:
129 fprintf(stderr, _("No enough memory\n"));
130 _err:
131 if (fd != fileno(stdin))
132 close(fd);
133 free(buf);
134 return 1;
135 }
136
load_topology(snd_tplg_t ** tplg,char * config,size_t config_size,int cflags)137 static int load_topology(snd_tplg_t **tplg, char *config,
138 size_t config_size, int cflags)
139 {
140 int err;
141
142 *tplg = snd_tplg_create(cflags);
143 if (*tplg == NULL) {
144 fprintf(stderr, _("failed to create new topology context\n"));
145 return 1;
146 }
147
148 err = snd_tplg_load(*tplg, config, config_size);
149 if (err < 0) {
150 fprintf(stderr, _("Unable to load configuration: %s\n"),
151 snd_strerror(-err));
152 snd_tplg_free(*tplg);
153 return 1;
154 }
155
156 return 0;
157 }
158
save(const char * output_file,void * buf,size_t size)159 static int save(const char *output_file, void *buf, size_t size)
160 {
161 char *fname = NULL;
162 int fd;
163 ssize_t r;
164
165 if (strcmp(output_file, "-") == 0) {
166 fd = fileno(stdout);
167 } else {
168 fname = alloca(strlen(output_file) + 5);
169 strcpy(fname, output_file);
170 strcat(fname, ".new");
171 fd = open(fname, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
172 if (fd < 0) {
173 fprintf(stderr, _("Unable to open output file '%s': %s\n"),
174 fname, strerror(-errno));
175 return 1;
176 }
177 }
178
179 r = 0;
180 while (size > 0) {
181 r = write(fd, buf, size);
182 if (r < 0 && (errno == EAGAIN || errno == EINTR))
183 continue;
184 if (r < 0)
185 break;
186 size -= r;
187 buf += r;
188 }
189
190 if (r < 0) {
191 fprintf(stderr, _("Write error: %s\n"), strerror(-errno));
192 if (fd != fileno(stdout)) {
193 if (fname && remove(fname))
194 fprintf(stderr, _("Unable to remove file %s: %s\n"),
195 fname, strerror(-errno));
196 close(fd);
197 }
198 return 1;
199 }
200
201 if (fd != fileno(stdout))
202 close(fd);
203
204 if (fname && rename(fname, output_file)) {
205 fprintf(stderr, _("Unable to rename file '%s' to '%s': %s\n"),
206 fname, output_file, strerror(-errno));
207 return 1;
208 }
209
210 return 0;
211 }
212
dump(const char * source_file,const char * output_file,int cflags,int sflags)213 static int dump(const char *source_file, const char *output_file, int cflags, int sflags)
214 {
215 snd_tplg_t *tplg;
216 char *config, *text;
217 size_t size;
218 int err;
219
220 err = load(source_file, (void **)&config, &size);
221 if (err)
222 return err;
223 err = load_topology(&tplg, config, size, cflags);
224 free(config);
225 if (err)
226 return err;
227 err = snd_tplg_save(tplg, &text, sflags);
228 snd_tplg_free(tplg);
229 if (err < 0) {
230 fprintf(stderr, _("Unable to save parsed configuration: %s\n"),
231 snd_strerror(-err));
232 return 1;
233 }
234 err = save(output_file, text, strlen(text));
235 free(text);
236 return err;
237 }
238
get_inc_path(const char * filename)239 static char *get_inc_path(const char *filename)
240 {
241 const char *s = strrchr(filename, '/');
242 char *r = strdup(filename);
243 if (r && s)
244 r[s - filename] = '\0';
245 return r;
246 }
247
248 /* Convert Topology2.0 conf to the existing conf syntax */
pre_process_conf(const char * source_file,const char * output_file,const char * pre_processor_defs)249 static int pre_process_conf(const char *source_file, const char *output_file,
250 const char *pre_processor_defs)
251 {
252 struct tplg_pre_processor *tplg_pp;
253 size_t config_size;
254 char *config, *inc_path;
255 int err;
256
257 err = load(source_file, (void **)&config, &config_size);
258 if (err)
259 return err;
260
261 /* init pre-processor */
262 err = init_pre_processor(&tplg_pp, SND_OUTPUT_STDIO, output_file);
263 if (err < 0) {
264 fprintf(stderr, _("failed to init pre-processor for Topology2.0\n"));
265 free(config);
266 return err;
267 }
268
269 /* pre-process conf file */
270 inc_path = get_inc_path(source_file);
271 err = pre_process(tplg_pp, config, config_size, pre_processor_defs, inc_path);
272 free(inc_path);
273
274 /* free pre-processor */
275 free_pre_preprocessor(tplg_pp);
276 free(config);
277 return err;
278 }
279
compile(const char * source_file,const char * output_file,int cflags,const char * pre_processor_defs)280 static int compile(const char *source_file, const char *output_file, int cflags,
281 const char *pre_processor_defs)
282 {
283 struct tplg_pre_processor *tplg_pp = NULL;
284 snd_tplg_t *tplg;
285 char *config, *inc_path;
286 void *bin;
287 size_t config_size, size;
288 int err;
289
290 err = load(source_file, (void **)&config, &config_size);
291 if (err)
292 return err;
293
294 /* pre-process before compiling */
295 if (pre_process_config) {
296 char *pconfig;
297 size_t size;
298
299 /* init pre-processor */
300 init_pre_processor(&tplg_pp, SND_OUTPUT_BUFFER, NULL);
301
302 /* pre-process conf file */
303 inc_path = get_inc_path(source_file);
304 err = pre_process(tplg_pp, config, config_size, pre_processor_defs, inc_path);
305 free(inc_path);
306 if (err) {
307 free_pre_preprocessor(tplg_pp);
308 free(config);
309 return err;
310 }
311
312 /* load topology */
313 size = snd_output_buffer_string(tplg_pp->output, &pconfig);
314 err = load_topology(&tplg, pconfig, size, cflags);
315
316 /* free pre-processor */
317 free_pre_preprocessor(tplg_pp);
318 } else {
319 err = load_topology(&tplg, config, config_size, cflags);
320 }
321 free(config);
322 if (err)
323 return err;
324 err = snd_tplg_build_bin(tplg, &bin, &size);
325 snd_tplg_free(tplg);
326 if (err < 0 || size == 0) {
327 fprintf(stderr, _("failed to compile context %s: %s\n"),
328 source_file, snd_strerror(-err));
329 return 1;
330 }
331 err = save(output_file, bin, size);
332 free(bin);
333 return err;
334 }
335
decode(const char * source_file,const char * output_file,int cflags,int dflags,int sflags)336 static int decode(const char *source_file, const char *output_file,
337 int cflags, int dflags, int sflags)
338 {
339 snd_tplg_t *tplg;
340 void *bin;
341 char *text;
342 size_t size;
343 int err;
344
345 if (load(source_file, &bin, &size))
346 return 1;
347 tplg = snd_tplg_create(cflags);
348 if (tplg == NULL) {
349 fprintf(stderr, _("failed to create new topology context\n"));
350 return 1;
351 }
352 err = snd_tplg_decode(tplg, bin, size, dflags);
353 free(bin);
354 if (err < 0) {
355 snd_tplg_free(tplg);
356 fprintf(stderr, _("failed to decode context %s: %s\n"),
357 source_file, snd_strerror(-err));
358 return 1;
359 }
360 err = snd_tplg_save(tplg, &text, sflags);
361 snd_tplg_free(tplg);
362 if (err < 0) {
363 fprintf(stderr, _("Unable to save parsed configuration: %s\n"),
364 snd_strerror(-err));
365 return 1;
366 }
367 err = save(output_file, text, strlen(text));
368 free(text);
369 return err;
370 }
371
main(int argc,char * argv[])372 int main(int argc, char *argv[])
373 {
374 static const char short_options[] = "hc:d:n:u:v:o:pP:sgxzV"
375 #if SND_LIB_VER(1, 2, 5) < SND_LIB_VERSION
376 "D:"
377 #endif
378 ;
379 static const struct option long_options[] = {
380 {"help", 0, NULL, 'h'},
381 {"verbose", 1, NULL, 'v'},
382 {"compile", 1, NULL, 'c'},
383 {"pre-process", 1, NULL, 'p'},
384 {"decode", 1, NULL, 'd'},
385 {"normalize", 1, NULL, 'n'},
386 {"dump", 1, NULL, 'u'},
387 {"output", 1, NULL, 'o'},
388 #if SND_LIB_VER(1, 2, 5) < SND_LIB_VERSION
389 {"define", 1, NULL, 'D'},
390 #endif
391 {"sort", 0, NULL, 's'},
392 {"group", 0, NULL, 'g'},
393 {"nocheck", 0, NULL, 'x'},
394 {"dapm-nosort", 0, NULL, 'z'},
395 {"version", 0, NULL, 'V'},
396 {0, 0, 0, 0},
397 };
398 char *source_file = NULL;
399 char *output_file = NULL;
400 const char *pre_processor_defs = NULL;
401 int c, err, op = 'c', cflags = 0, dflags = 0, sflags = 0, option_index;
402
403 #ifdef ENABLE_NLS
404 setlocale(LC_ALL, "");
405 textdomain(PACKAGE);
406 #endif
407
408 err = snd_output_stdio_attach(&log, stderr, 0);
409 assert(err >= 0);
410
411 while ((c = getopt_long(argc, argv, short_options, long_options, &option_index)) != -1) {
412 switch (c) {
413 case 'h':
414 usage(argv[0]);
415 return 0;
416 case 'v':
417 cflags |= SND_TPLG_CREATE_VERBOSE;
418 break;
419 case 'z':
420 cflags |= SND_TPLG_CREATE_DAPM_NOSORT;
421 break;
422 case 'c':
423 case 'd':
424 case 'n':
425 case 'u':
426 if (source_file) {
427 fprintf(stderr, _("Cannot combine operations (compile, normalize, pre-process, dump)\n"));
428 return 1;
429 }
430 source_file = optarg;
431 op = c;
432 break;
433 case 'o':
434 output_file = optarg;
435 break;
436 case 's':
437 sflags |= SND_TPLG_SAVE_SORT;
438 break;
439 case 'P':
440 op = 'P';
441 source_file = optarg;
442 break;
443 case 'p':
444 pre_process_config = true;
445 break;
446 case 'g':
447 sflags |= SND_TPLG_SAVE_GROUPS;
448 break;
449 case 'x':
450 sflags |= SND_TPLG_SAVE_NOCHECK;
451 break;
452 #if SND_LIB_VER(1, 2, 5) < SND_LIB_VERSION
453 case 'D':
454 pre_processor_defs = optarg;
455 break;
456 #endif
457 case 'V':
458 version(argv[0]);
459 return 0;
460 default:
461 fprintf(stderr, _("Try `%s --help' for more information.\n"), argv[0]);
462 return 1;
463 }
464 }
465
466 if (source_file == NULL || output_file == NULL) {
467 usage(argv[0]);
468 return 1;
469 }
470
471 if (op == 'n') {
472 if (sflags != 0 && sflags != SND_TPLG_SAVE_SORT) {
473 fprintf(stderr, _("Wrong parameters for the normalize operation!\n"));
474 return 1;
475 }
476 /* normalize has predefined output */
477 sflags = SND_TPLG_SAVE_SORT;
478 }
479
480 switch (op) {
481 case 'c':
482 err = compile(source_file, output_file, cflags, pre_processor_defs);
483 break;
484 case 'd':
485 err = decode(source_file, output_file, cflags, dflags, sflags);
486 break;
487 case 'P':
488 err = pre_process_conf(source_file, output_file, pre_processor_defs);
489 break;
490 default:
491 err = dump(source_file, output_file, cflags, sflags);
492 break;
493 }
494
495 snd_output_close(log);
496 return err ? 1 : 0;
497 }
498