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