1 /*
2 Copyright(c) 2014-2015 Intel Corporation
3 All rights reserved.
4
5 This library is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation; either version 2.1 of
8 the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
14
15 Authors: Mengdong Lin <mengdong.lin@intel.com>
16 Yao Jin <yao.jin@intel.com>
17 Liam Girdwood <liam.r.girdwood@linux.intel.com>
18 */
19
20 #include "list.h"
21 #include "tplg_local.h"
22
23 /* write a block, track the position */
twrite(snd_tplg_t * tplg,void * data,size_t data_size)24 static ssize_t twrite(snd_tplg_t *tplg, void *data, size_t data_size)
25 {
26 if (tplg->bin_pos + data_size > tplg->bin_size)
27 return -EIO;
28 memcpy(tplg->bin + tplg->bin_pos, data, data_size);
29 tplg->bin_pos += data_size;
30 return data_size;
31 }
32
33 /* write out block header to output file */
write_block_header(snd_tplg_t * tplg,unsigned int type,unsigned int vendor_type,unsigned int version,unsigned int index,size_t payload_size,int count)34 static ssize_t write_block_header(snd_tplg_t *tplg, unsigned int type,
35 unsigned int vendor_type,
36 unsigned int version, unsigned int index,
37 size_t payload_size, int count)
38 {
39 struct snd_soc_tplg_hdr hdr;
40
41 memset(&hdr, 0, sizeof(hdr));
42 hdr.magic = SND_SOC_TPLG_MAGIC;
43 hdr.abi = SND_SOC_TPLG_ABI_VERSION;
44 hdr.type = type;
45 hdr.vendor_type = vendor_type;
46 hdr.version = version;
47 hdr.payload_size = payload_size;
48 hdr.index = index;
49 hdr.size = sizeof(hdr);
50 hdr.count = count;
51
52 /* make sure file offset is aligned with the calculated HDR offset */
53 if (tplg->bin_pos != tplg->next_hdr_pos) {
54 SNDERR("New header is at offset 0x%zx but file"
55 " offset 0x%zx is %s by %ld bytes",
56 tplg->next_hdr_pos, tplg->bin_pos,
57 tplg->bin_pos > tplg->next_hdr_pos ? "ahead" : "behind",
58 tplg->bin_pos - tplg->next_hdr_pos);
59 return -EINVAL;
60 }
61
62 tplg_log(tplg, 'B', tplg->bin_pos,
63 "header index %d type %d count %d size 0x%lx/%ld vendor %d "
64 "version %d", index, type, count,
65 (long unsigned int)payload_size, (long int)payload_size,
66 vendor_type, version);
67
68 tplg->next_hdr_pos += hdr.payload_size + sizeof(hdr);
69
70 return twrite(tplg, &hdr, sizeof(hdr));
71 }
72
write_elem_block(snd_tplg_t * tplg,struct list_head * base,size_t size,int tplg_type,const char * obj_name)73 static int write_elem_block(snd_tplg_t *tplg,
74 struct list_head *base, size_t size,
75 int tplg_type, const char *obj_name)
76 {
77 struct list_head *pos, *sub_pos, *sub_base;
78 struct tplg_elem *elem, *elem_next;
79 size_t total_size = 0, count = 0, block_size = 0;
80 ssize_t ret, wsize;
81
82 sub_base = base;
83 list_for_each(pos, base) {
84 /* find elems with the same index to make a block */
85 elem = list_entry(pos, struct tplg_elem, list);
86
87 if (elem->compound_elem)
88 continue;
89
90 elem_next = list_entry(pos->next, struct tplg_elem, list);
91 block_size += elem->size;
92 count++;
93
94 if ((pos->next == base) || (elem_next->index != elem->index)) {
95 /* write header for the block */
96 ret = write_block_header(tplg, tplg_type, elem->vendor_type,
97 tplg->version, elem->index, block_size, count);
98 if (ret < 0) {
99 SNDERR("failed to write %s block %d",
100 obj_name, ret);
101 return ret;
102 }
103
104 /* write elems for the block */
105 list_for_each(sub_pos, sub_base) {
106 elem = list_entry(sub_pos, struct tplg_elem, list);
107 /* compound elems have already been copied to other elems */
108 if (elem->compound_elem)
109 continue;
110
111 if (elem->type != SND_TPLG_TYPE_DAPM_GRAPH)
112 tplg_log(tplg, 'B', tplg->bin_pos,
113 "%s '%s': write %d bytes",
114 obj_name, elem->id, elem->size);
115 else
116 tplg_log(tplg, 'B', tplg->bin_pos,
117 "%s '%s -> %s -> %s': write %d bytes",
118 obj_name, elem->route->source,
119 elem->route->control,
120 elem->route->sink, elem->size);
121
122 wsize = twrite(tplg, elem->obj, elem->size);
123 if (wsize < 0)
124 return size;
125
126 total_size += wsize;
127 /* get to the end of sub list */
128 if (sub_pos == pos)
129 break;
130 }
131 /* the last elem of the current sub list as the head of
132 next sub list*/
133 sub_base = pos;
134 count = 0;
135 block_size = 0;
136 }
137 }
138
139 /* make sure we have written the correct size */
140 if (total_size != size) {
141 SNDERR("size mismatch. Expected %zu wrote %zu",
142 size, total_size);
143 return -EIO;
144 }
145
146 return 0;
147 }
148
calc_manifest_size(snd_tplg_t * tplg)149 static size_t calc_manifest_size(snd_tplg_t *tplg)
150 {
151 return sizeof(struct snd_soc_tplg_hdr) +
152 sizeof(tplg->manifest) +
153 tplg->manifest.priv.size;
154 }
155
calc_real_size(struct list_head * base)156 static size_t calc_real_size(struct list_head *base)
157 {
158 struct list_head *pos;
159 struct tplg_elem *elem, *elem_next;
160 size_t size = 0;
161
162 list_for_each(pos, base) {
163
164 elem = list_entry(pos, struct tplg_elem, list);
165
166 /* compound elems have already been copied to other elems */
167 if (elem->compound_elem)
168 continue;
169
170 if (elem->size <= 0)
171 continue;
172
173 size += elem->size;
174
175 elem_next = list_entry(pos->next, struct tplg_elem, list);
176
177 if ((pos->next == base) || (elem_next->index != elem->index))
178 size += sizeof(struct snd_soc_tplg_hdr);
179 }
180
181 return size;
182 }
183
calc_block_size(struct list_head * base)184 static size_t calc_block_size(struct list_head *base)
185 {
186 struct list_head *pos;
187 struct tplg_elem *elem;
188 size_t size = 0;
189
190 list_for_each(pos, base) {
191
192 elem = list_entry(pos, struct tplg_elem, list);
193
194 /* compound elems have already been copied to other elems */
195 if (elem->compound_elem)
196 continue;
197
198 size += elem->size;
199 }
200
201 return size;
202 }
203
204 /* write the manifest including its private data */
write_manifest_data(snd_tplg_t * tplg)205 static ssize_t write_manifest_data(snd_tplg_t *tplg)
206 {
207 ssize_t ret;
208
209 /* write the header for this block */
210 ret = write_block_header(tplg, SND_SOC_TPLG_TYPE_MANIFEST, 0,
211 tplg->version, 0,
212 sizeof(tplg->manifest) + tplg->manifest.priv.size, 1);
213 if (ret < 0) {
214 SNDERR("failed to write manifest block");
215 return ret;
216 }
217
218 tplg_log(tplg, 'B', tplg->bin_pos, "manifest: write %d bytes",
219 sizeof(tplg->manifest));
220 ret = twrite(tplg, &tplg->manifest, sizeof(tplg->manifest));
221 if (ret >= 0) {
222 tplg_log(tplg, 'B', tplg->bin_pos,
223 "manifest: write %d priv bytes",
224 tplg->manifest.priv.size);
225 ret = twrite(tplg, tplg->manifest_pdata, tplg->manifest.priv.size);
226 }
227 return ret;
228 }
229
tplg_write_data(snd_tplg_t * tplg)230 int tplg_write_data(snd_tplg_t *tplg)
231 {
232 struct tplg_table *tptr;
233 struct list_head *list;
234 ssize_t ret;
235 size_t total_size, size;
236 unsigned int index;
237
238 /* calculate total size */
239 total_size = calc_manifest_size(tplg);
240 for (index = 0; index < tplg_table_items; index++) {
241 tptr = &tplg_table[index];
242 if (!tptr->build)
243 continue;
244 list = (struct list_head *)((void *)tplg + tptr->loff);
245 size = calc_real_size(list);
246 total_size += size;
247 }
248
249 /* allocate new binary output */
250 free(tplg->bin);
251 tplg->bin = malloc(total_size);
252 tplg->bin_pos = 0;
253 tplg->bin_size = total_size;
254 if (tplg->bin == NULL) {
255 tplg->bin_size = 0;
256 return -ENOMEM;
257 }
258
259 /* write manifest */
260 ret = write_manifest_data(tplg);
261 if (ret < 0) {
262 SNDERR("failed to write manifest %d", ret);
263 return ret;
264 }
265
266 /* write all blocks */
267 for (index = 0; index < tplg_table_items; index++) {
268 tptr = &tplg_table[index];
269 if (!tptr->build)
270 continue;
271 list = (struct list_head *)((void *)tplg + tptr->loff);
272 /* calculate the block size in bytes for all elems in this list */
273 size = calc_block_size(list);
274 if (size == 0)
275 continue;
276 tplg_log(tplg, 'B', tplg->bin_pos,
277 "block size for type %s (%d:%d) is 0x%zx/%zd",
278 tptr->name, tptr->type,
279 tptr->tsoc, size, size);
280 ret = write_elem_block(tplg, list, size,
281 tptr->tsoc, tptr->name);
282 if (ret < 0) {
283 SNDERR("failed to write %s elements: %s",
284 tptr->name, snd_strerror(-ret));
285 return ret;
286 }
287 }
288
289 tplg_log(tplg, 'B', tplg->bin_pos, "total size is 0x%zx/%zd",
290 tplg->bin_pos, tplg->bin_pos);
291
292 if (total_size != tplg->bin_pos) {
293 SNDERR("total size mismatch (%zd != %zd)",
294 total_size, tplg->bin_pos);
295 return -EINVAL;
296 }
297
298 return 0;
299 }
300