xref: /freebsd/sys/dev/mlxfw/mlxfw_mfa2.c (revision 95ee2897e98f5d444f26ed2334cc7c439f9c16c6)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2017-2019 Mellanox Technologies.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of Mellanox nor the names of contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #define pr_fmt(fmt) "mlxfw_mfa2: " fmt
33 
34 #include <linux/kernel.h>
35 #include <linux/module.h>
36 #include <linux/printk.h>
37 #include <contrib/xz-embedded/linux/include/linux/xz.h>
38 
39 #include "mlxfw_mfa2.h"
40 #include "mlxfw_mfa2_file.h"
41 #include "mlxfw_mfa2_tlv.h"
42 #include "mlxfw_mfa2_format.h"
43 #include "mlxfw_mfa2_tlv_multi.h"
44 
45 /*               MFA2 FILE
46  *  +----------------------------------+
47  *  |        MFA2 finger print         |
48  *  +----------------------------------+
49  *  |   package descriptor multi_tlv   |
50  *  | +------------------------------+ |     +-----------------+
51  *  | |    package descriptor tlv    +-----> |num_devices=n    |
52  *  | +------------------------------+ |     |num_components=m |
53  *  +----------------------------------+     |CB offset        |
54  *  |    device descriptor multi_tlv   |     |...              |
55  *  | +------------------------------+ |     |                 |
56  *  | |           PSID tlv           | |     +-----------------+
57  *  | +------------------------------+ |
58  *  | |     component index tlv      | |
59  *  | +------------------------------+ |
60  *  +----------------------------------+
61  *  |  component descriptor multi_tlv  |
62  *  | +------------------------------+ |     +-----------------+
63  *  | |  component descriptor tlv    +-----> |Among others:    |
64  *  | +------------------------------+ |     |CB offset=o      |
65  *  +----------------------------------+     |comp index=i     |
66  *  |                                  |     |...              |
67  *  |                                  |     |                 |
68  *  |                                  |     +-----------------+
69  *  |        COMPONENT BLOCK (CB)      |
70  *  |                                  |
71  *  |                                  |
72  *  |                                  |
73  *  +----------------------------------+
74  *
75  * On the top level, an MFA2 file contains:
76  *  - Fingerprint
77  *  - Several multi_tlvs (TLVs of type MLXFW_MFA2_TLV_MULTI, as defined in
78  *    mlxfw_mfa2_format.h)
79  *  - Compresses content block
80  *
81  * The first multi_tlv
82  * -------------------
83  * The first multi TLV is treated as package descriptor, and expected to have a
84  * first TLV child of type MLXFW_MFA2_TLV_PACKAGE_DESCRIPTOR which contains all
85  * the global information needed to parse the file. Among others, it contains
86  * the number of device descriptors and component descriptor following this
87  * multi TLV.
88  *
89  * The device descriptor multi_tlv
90  * -------------------------------
91  * The multi TLVs following the package descriptor are treated as device
92  * descriptor, and are expected to have the following children:
93  *  - PSID TLV child of type MLXFW_MFA2_TLV_PSID containing that device PSID.
94  *  - Component index of type MLXFW_MFA2_TLV_COMPONENT_PTR that contains that
95  *    device component index.
96  *
97  * The component descriptor multi_tlv
98  * ----------------------------------
99  * The multi TLVs following the device descriptor multi TLVs are treated as
100  * component descriptor, and are expected to have a first child of type
101  * MLXFW_MFA2_TLV_COMPONENT_DESCRIPTOR that contains mostly the component index,
102  * needed for the flash process and the offset to the binary within the
103  * component block.
104  */
105 
106 static const u8 mlxfw_mfa2_fingerprint[] = "MLNX.MFA2.XZ.00!";
107 static const int mlxfw_mfa2_fingerprint_len =
108 			sizeof(mlxfw_mfa2_fingerprint) - 1;
109 
110 static const u8 mlxfw_mfa2_comp_magic[] = "#BIN.COMPONENT!#";
111 static const int mlxfw_mfa2_comp_magic_len = sizeof(mlxfw_mfa2_comp_magic) - 1;
112 
mlxfw_mfa2_check(const struct firmware * fw)113 bool mlxfw_mfa2_check(const struct firmware *fw)
114 {
115 	if (fw->datasize < sizeof(mlxfw_mfa2_fingerprint))
116 		return false;
117 
118 	return memcmp(fw->data, mlxfw_mfa2_fingerprint,
119 		      mlxfw_mfa2_fingerprint_len) == 0;
120 }
121 
122 static bool
mlxfw_mfa2_tlv_multi_validate(const struct mlxfw_mfa2_file * mfa2_file,const struct mlxfw_mfa2_tlv_multi * multi)123 mlxfw_mfa2_tlv_multi_validate(const struct mlxfw_mfa2_file *mfa2_file,
124 			      const struct mlxfw_mfa2_tlv_multi *multi)
125 {
126 	const struct mlxfw_mfa2_tlv *tlv;
127 	u16 idx;
128 
129 	/* Check that all children are valid */
130 	mlxfw_mfa2_tlv_multi_foreach(mfa2_file, tlv, idx, multi) {
131 		if (!tlv) {
132 			pr_err("Multi has invalid child");
133 			return false;
134 		}
135 	}
136 	return true;
137 }
138 
139 static bool
mlxfw_mfa2_file_dev_validate(const struct mlxfw_mfa2_file * mfa2_file,const struct mlxfw_mfa2_tlv * dev_tlv,u16 dev_idx)140 mlxfw_mfa2_file_dev_validate(const struct mlxfw_mfa2_file *mfa2_file,
141 			     const struct mlxfw_mfa2_tlv *dev_tlv,
142 			     u16 dev_idx)
143 {
144 	const struct mlxfw_mfa2_tlv_component_ptr *cptr;
145 	const struct mlxfw_mfa2_tlv_multi *multi;
146 	const struct mlxfw_mfa2_tlv_psid *psid;
147 	const struct mlxfw_mfa2_tlv *tlv;
148 	u16 cptr_count;
149 	u16 cptr_idx;
150 	int err;
151 
152 	pr_debug("Device %d\n", dev_idx);
153 
154 	multi = mlxfw_mfa2_tlv_multi_get(mfa2_file, dev_tlv);
155 	if (!multi) {
156 		pr_err("Device %d is not a valid TLV error\n", dev_idx);
157 		return false;
158 	}
159 
160 	if (!mlxfw_mfa2_tlv_multi_validate(mfa2_file, multi))
161 		return false;
162 
163 	/* Validate the device has PSID tlv */
164 	tlv = mlxfw_mfa2_tlv_multi_child_find(mfa2_file, multi,
165 					      MLXFW_MFA2_TLV_PSID, 0);
166 	if (!tlv) {
167 		pr_err("Device %d does not have PSID\n", dev_idx);
168 		return false;
169 	}
170 
171 	psid = mlxfw_mfa2_tlv_psid_get(mfa2_file, tlv);
172 	if (!psid) {
173 		pr_err("Device %d PSID TLV is not valid\n", dev_idx);
174 		return false;
175 	}
176 
177 	print_hex_dump_debug("  -- Device PSID ", DUMP_PREFIX_NONE, 16, 16,
178 			     psid->psid, be16_to_cpu(tlv->len), true);
179 
180 	/* Validate the device has COMPONENT_PTR */
181 	err = mlxfw_mfa2_tlv_multi_child_count(mfa2_file, multi,
182 					       MLXFW_MFA2_TLV_COMPONENT_PTR,
183 					       &cptr_count);
184 	if (err)
185 		return false;
186 
187 	if (cptr_count == 0) {
188 		pr_err("Device %d has no components\n", dev_idx);
189 		return false;
190 	}
191 
192 	for (cptr_idx = 0; cptr_idx < cptr_count; cptr_idx++) {
193 		tlv = mlxfw_mfa2_tlv_multi_child_find(mfa2_file, multi,
194 						      MLXFW_MFA2_TLV_COMPONENT_PTR,
195 						      cptr_idx);
196 		if (!tlv)
197 			return false;
198 
199 		cptr = mlxfw_mfa2_tlv_component_ptr_get(mfa2_file, tlv);
200 		if (!cptr) {
201 			pr_err("Device %d COMPONENT_PTR TLV is not valid\n",
202 			       dev_idx);
203 			return false;
204 		}
205 
206 		pr_debug("  -- Component index %d\n",
207 			 be16_to_cpu(cptr->component_index));
208 	}
209 	return true;
210 }
211 
212 static bool
mlxfw_mfa2_file_comp_validate(const struct mlxfw_mfa2_file * mfa2_file,const struct mlxfw_mfa2_tlv * comp_tlv,u16 comp_idx)213 mlxfw_mfa2_file_comp_validate(const struct mlxfw_mfa2_file *mfa2_file,
214 			      const struct mlxfw_mfa2_tlv *comp_tlv,
215 			      u16 comp_idx)
216 {
217 	const struct mlxfw_mfa2_tlv_component_descriptor *cdesc;
218 	const struct mlxfw_mfa2_tlv_multi *multi;
219 	const struct mlxfw_mfa2_tlv *tlv;
220 
221 	pr_debug("Component %d\n", comp_idx);
222 
223 	multi = mlxfw_mfa2_tlv_multi_get(mfa2_file, comp_tlv);
224 	if (!multi) {
225 		pr_err("Component %d is not a valid TLV error\n", comp_idx);
226 		return false;
227 	}
228 
229 	if (!mlxfw_mfa2_tlv_multi_validate(mfa2_file, multi))
230 		return false;
231 
232 	/* Check that component have COMPONENT_DESCRIPTOR as first child */
233 	tlv = mlxfw_mfa2_tlv_multi_child(mfa2_file, multi);
234 	if (!tlv) {
235 		pr_err("Component descriptor %d multi TLV error\n", comp_idx);
236 		return false;
237 	}
238 
239 	cdesc = mlxfw_mfa2_tlv_component_descriptor_get(mfa2_file, tlv);
240 	if (!cdesc) {
241 		pr_err("Component %d does not have a valid descriptor\n",
242 		       comp_idx);
243 		return false;
244 	}
245 	pr_debug("  -- Component type %d\n", be16_to_cpu(cdesc->identifier));
246 	pr_debug("  -- Offset %#jx and size %d\n",
247 		 (uintmax_t)((u64) be32_to_cpu(cdesc->cb_offset_h) << 32)
248 		 | be32_to_cpu(cdesc->cb_offset_l), be32_to_cpu(cdesc->size));
249 
250 	return true;
251 }
252 
mlxfw_mfa2_file_validate(const struct mlxfw_mfa2_file * mfa2_file)253 static bool mlxfw_mfa2_file_validate(const struct mlxfw_mfa2_file *mfa2_file)
254 {
255 	const struct mlxfw_mfa2_tlv *tlv;
256 	u16 idx;
257 
258 	pr_debug("Validating file\n");
259 
260 	/* check that all the devices exist */
261 	mlxfw_mfa2_tlv_foreach(mfa2_file, tlv, idx, mfa2_file->first_dev,
262 			       mfa2_file->dev_count) {
263 		if (!tlv) {
264 			pr_err("Device TLV error\n");
265 			return false;
266 		}
267 
268 		/* Check each device */
269 		if (!mlxfw_mfa2_file_dev_validate(mfa2_file, tlv, idx))
270 			return false;
271 	}
272 
273 	/* check that all the components exist */
274 	mlxfw_mfa2_tlv_foreach(mfa2_file, tlv, idx, mfa2_file->first_component,
275 			       mfa2_file->component_count) {
276 		if (!tlv) {
277 			pr_err("Device TLV error\n");
278 			return false;
279 		}
280 
281 		/* Check each component */
282 		if (!mlxfw_mfa2_file_comp_validate(mfa2_file, tlv, idx))
283 			return false;
284 	}
285 	return true;
286 }
287 
mlxfw_mfa2_file_init(const struct firmware * fw)288 struct mlxfw_mfa2_file *mlxfw_mfa2_file_init(const struct firmware *fw)
289 {
290 	const struct mlxfw_mfa2_tlv_package_descriptor *pd;
291 	const struct mlxfw_mfa2_tlv_multi *multi;
292 	const struct mlxfw_mfa2_tlv *multi_child;
293 	const struct mlxfw_mfa2_tlv *first_tlv;
294 	struct mlxfw_mfa2_file *mfa2_file;
295 	const u8 *first_tlv_ptr;
296 	const u8 *cb_top_ptr;
297 
298 	mfa2_file = kcalloc(1, sizeof(*mfa2_file), GFP_KERNEL);
299 	if (!mfa2_file)
300 		return ERR_PTR(-ENOMEM);
301 
302 	mfa2_file->fw = fw;
303 	first_tlv_ptr = (const u8 *) fw->data + NLA_ALIGN(mlxfw_mfa2_fingerprint_len);
304 	first_tlv = mlxfw_mfa2_tlv_get(mfa2_file, first_tlv_ptr);
305 	if (!first_tlv) {
306 		pr_err("Could not parse package descriptor TLV\n");
307 		goto err_out;
308 	}
309 
310 	multi = mlxfw_mfa2_tlv_multi_get(mfa2_file, first_tlv);
311 	if (!multi) {
312 		pr_err("First TLV is not of valid multi type\n");
313 		goto err_out;
314 	}
315 
316 	multi_child = mlxfw_mfa2_tlv_multi_child(mfa2_file, multi);
317 	if (!multi_child)
318 		goto err_out;
319 
320 	pd = mlxfw_mfa2_tlv_package_descriptor_get(mfa2_file, multi_child);
321 	if (!pd) {
322 		pr_err("Could not parse package descriptor TLV\n");
323 		goto err_out;
324 	}
325 
326 	mfa2_file->first_dev = mlxfw_mfa2_tlv_next(mfa2_file, first_tlv);
327 	if (!mfa2_file->first_dev) {
328 		pr_err("First device TLV is not valid\n");
329 		goto err_out;
330 	}
331 
332 	mfa2_file->dev_count = be16_to_cpu(pd->num_devices);
333 	mfa2_file->first_component = mlxfw_mfa2_tlv_advance(mfa2_file,
334 							    mfa2_file->first_dev,
335 							    mfa2_file->dev_count);
336 	mfa2_file->component_count = be16_to_cpu(pd->num_components);
337 	mfa2_file->cb = (const u8 *) fw->data + NLA_ALIGN(be32_to_cpu(pd->cb_offset));
338 	if (!mlxfw_mfa2_valid_ptr(mfa2_file, mfa2_file->cb)) {
339 		pr_err("Component block is out side the file\n");
340 		goto err_out;
341 	}
342 	mfa2_file->cb_archive_size = be32_to_cpu(pd->cb_archive_size);
343 	cb_top_ptr = (const u8 *) mfa2_file->cb + mfa2_file->cb_archive_size - 1;
344 	if (!mlxfw_mfa2_valid_ptr(mfa2_file, cb_top_ptr)) {
345 		pr_err("Component block size is too big\n");
346 		goto err_out;
347 	}
348 
349 	if (!mlxfw_mfa2_file_validate(mfa2_file))
350 		goto err_out;
351 	return mfa2_file;
352 err_out:
353 	kfree(mfa2_file);
354 	return ERR_PTR(-EINVAL);
355 }
356 
357 static const struct mlxfw_mfa2_tlv_multi *
mlxfw_mfa2_tlv_dev_get(const struct mlxfw_mfa2_file * mfa2_file,const char * psid,u16 psid_size)358 mlxfw_mfa2_tlv_dev_get(const struct mlxfw_mfa2_file *mfa2_file,
359 		       const char *psid, u16 psid_size)
360 {
361 	const struct mlxfw_mfa2_tlv_psid *tlv_psid;
362 	const struct mlxfw_mfa2_tlv_multi *dev_multi;
363 	const struct mlxfw_mfa2_tlv *dev_tlv;
364 	const struct mlxfw_mfa2_tlv *tlv;
365 	u32 idx;
366 
367 	/* for each device tlv */
368 	mlxfw_mfa2_tlv_foreach(mfa2_file, dev_tlv, idx, mfa2_file->first_dev,
369 			       mfa2_file->dev_count) {
370 		if (!dev_tlv)
371 			return NULL;
372 
373 		dev_multi = mlxfw_mfa2_tlv_multi_get(mfa2_file, dev_tlv);
374 		if (!dev_multi)
375 			return NULL;
376 
377 		/* find psid child and compare */
378 		tlv = mlxfw_mfa2_tlv_multi_child_find(mfa2_file, dev_multi,
379 						      MLXFW_MFA2_TLV_PSID, 0);
380 		if (!tlv)
381 			return NULL;
382 		if (be16_to_cpu(tlv->len) != psid_size)
383 			continue;
384 
385 		tlv_psid = mlxfw_mfa2_tlv_psid_get(mfa2_file, tlv);
386 		if (!tlv_psid)
387 			return NULL;
388 
389 		if (memcmp(psid, tlv_psid->psid, psid_size) == 0)
390 			return dev_multi;
391 	}
392 
393 	return NULL;
394 }
395 
mlxfw_mfa2_file_component_count(const struct mlxfw_mfa2_file * mfa2_file,const char * psid,u32 psid_size,u32 * p_count)396 int mlxfw_mfa2_file_component_count(const struct mlxfw_mfa2_file *mfa2_file,
397 				    const char *psid, u32 psid_size,
398 				    u32 *p_count)
399 {
400 	const struct mlxfw_mfa2_tlv_multi *dev_multi;
401 	u16 count;
402 	int err;
403 
404 	dev_multi = mlxfw_mfa2_tlv_dev_get(mfa2_file, psid, psid_size);
405 	if (!dev_multi)
406 		return -EINVAL;
407 
408 	err = mlxfw_mfa2_tlv_multi_child_count(mfa2_file, dev_multi,
409 					       MLXFW_MFA2_TLV_COMPONENT_PTR,
410 					       &count);
411 	if (err)
412 		return err;
413 
414 	*p_count = count;
415 	return 0;
416 }
417 
mlxfw_mfa2_xz_dec_run(struct xz_dec * xz_dec,struct xz_buf * xz_buf,bool * finished)418 static int mlxfw_mfa2_xz_dec_run(struct xz_dec *xz_dec, struct xz_buf *xz_buf,
419 				 bool *finished)
420 {
421 	enum xz_ret xz_ret;
422 
423 	xz_ret = xz_dec_run(xz_dec, xz_buf);
424 
425 	switch (xz_ret) {
426 	case XZ_STREAM_END:
427 		*finished = true;
428 		return 0;
429 	case XZ_OK:
430 		*finished = false;
431 		return 0;
432 	case XZ_MEM_ERROR:
433 		pr_err("xz no memory\n");
434 		return -ENOMEM;
435 	case XZ_DATA_ERROR:
436 		pr_err("xz file corrupted\n");
437 		return -EINVAL;
438 	case XZ_FORMAT_ERROR:
439 		pr_err("xz format not found\n");
440 		return -EINVAL;
441 	case XZ_OPTIONS_ERROR:
442 		pr_err("unsupported xz option\n");
443 		return -EINVAL;
444 	case XZ_MEMLIMIT_ERROR:
445 		pr_err("xz dictionary too small\n");
446 		return -EINVAL;
447 	default:
448 		pr_err("xz error %d\n", xz_ret);
449 		return -EINVAL;
450 	}
451 }
452 
mlxfw_mfa2_file_cb_offset_xz(const struct mlxfw_mfa2_file * mfa2_file,off_t off,size_t size,u8 * buf)453 static int mlxfw_mfa2_file_cb_offset_xz(const struct mlxfw_mfa2_file *mfa2_file,
454 					off_t off, size_t size, u8 *buf)
455 {
456 	struct xz_dec *xz_dec;
457 	struct xz_buf dec_buf;
458 	off_t curr_off = 0;
459 	bool finished;
460 	int err;
461 
462 	xz_dec = xz_dec_init(XZ_DYNALLOC, (u32) -1);
463 	if (!xz_dec)
464 		return -EINVAL;
465 
466 	dec_buf.in_size = mfa2_file->cb_archive_size;
467 	dec_buf.in = mfa2_file->cb;
468 	dec_buf.in_pos = 0;
469 	dec_buf.out = buf;
470 
471 	/* decode up to the offset */
472 	do {
473 		dec_buf.out_pos = 0;
474 		dec_buf.out_size = min_t(size_t, size, off - curr_off);
475 		if (dec_buf.out_size == 0)
476 			break;
477 
478 		err = mlxfw_mfa2_xz_dec_run(xz_dec, &dec_buf, &finished);
479 		if (err)
480 			goto out;
481 		if (finished) {
482 			pr_err("xz section too short\n");
483 			err = -EINVAL;
484 			goto out;
485 		}
486 		curr_off += dec_buf.out_pos;
487 	} while (curr_off != off);
488 
489 	/* decode the needed section */
490 	dec_buf.out_pos = 0;
491 	dec_buf.out_size = size;
492 	err = mlxfw_mfa2_xz_dec_run(xz_dec, &dec_buf, &finished);
493 out:
494 	xz_dec_end(xz_dec);
495 	return err;
496 }
497 
498 static const struct mlxfw_mfa2_tlv_component_descriptor *
mlxfw_mfa2_file_component_tlv_get(const struct mlxfw_mfa2_file * mfa2_file,u16 comp_index)499 mlxfw_mfa2_file_component_tlv_get(const struct mlxfw_mfa2_file *mfa2_file,
500 				  u16 comp_index)
501 {
502 	const struct mlxfw_mfa2_tlv_multi *multi;
503 	const struct mlxfw_mfa2_tlv *multi_child;
504 	const struct mlxfw_mfa2_tlv *comp_tlv;
505 
506 	if (comp_index > mfa2_file->component_count)
507 		return NULL;
508 
509 	comp_tlv = mlxfw_mfa2_tlv_advance(mfa2_file, mfa2_file->first_component,
510 					  comp_index);
511 	if (!comp_tlv)
512 		return NULL;
513 
514 	multi = mlxfw_mfa2_tlv_multi_get(mfa2_file, comp_tlv);
515 	if (!multi)
516 		return NULL;
517 
518 	multi_child = mlxfw_mfa2_tlv_multi_child(mfa2_file, multi);
519 	if (!multi_child)
520 		return NULL;
521 
522 	return mlxfw_mfa2_tlv_component_descriptor_get(mfa2_file, multi_child);
523 }
524 
525 struct mlxfw_mfa2_comp_data {
526 	struct mlxfw_mfa2_component comp;
527 	u8 buff[0];
528 };
529 
530 static const struct mlxfw_mfa2_tlv_component_descriptor *
mlxfw_mfa2_file_component_find(const struct mlxfw_mfa2_file * mfa2_file,const char * psid,int psid_size,int component_index)531 mlxfw_mfa2_file_component_find(const struct mlxfw_mfa2_file *mfa2_file,
532 			       const char *psid, int psid_size,
533 			       int component_index)
534 {
535 	const struct mlxfw_mfa2_tlv_component_ptr *cptr;
536 	const struct mlxfw_mfa2_tlv_multi *dev_multi;
537 	const struct mlxfw_mfa2_tlv *cptr_tlv;
538 	u16 comp_idx;
539 
540 	dev_multi = mlxfw_mfa2_tlv_dev_get(mfa2_file, psid, psid_size);
541 	if (!dev_multi)
542 		return NULL;
543 
544 	cptr_tlv = mlxfw_mfa2_tlv_multi_child_find(mfa2_file, dev_multi,
545 						   MLXFW_MFA2_TLV_COMPONENT_PTR,
546 						   component_index);
547 	if (!cptr_tlv)
548 		return NULL;
549 
550 	cptr = mlxfw_mfa2_tlv_component_ptr_get(mfa2_file, cptr_tlv);
551 	if (!cptr)
552 		return NULL;
553 
554 	comp_idx = be16_to_cpu(cptr->component_index);
555 	return mlxfw_mfa2_file_component_tlv_get(mfa2_file, comp_idx);
556 }
557 
558 struct mlxfw_mfa2_component *
mlxfw_mfa2_file_component_get(const struct mlxfw_mfa2_file * mfa2_file,const char * psid,int psid_size,int component_index)559 mlxfw_mfa2_file_component_get(const struct mlxfw_mfa2_file *mfa2_file,
560 			      const char *psid, int psid_size,
561 			      int component_index)
562 {
563 	const struct mlxfw_mfa2_tlv_component_descriptor *comp;
564 	struct mlxfw_mfa2_comp_data *comp_data;
565 	u32 comp_buf_size;
566 	off_t cb_offset;
567 	u32 comp_size;
568 	int err;
569 
570 	comp = mlxfw_mfa2_file_component_find(mfa2_file, psid, psid_size,
571 					      component_index);
572 	if (!comp)
573 		return ERR_PTR(-EINVAL);
574 
575 	cb_offset = (u64) be32_to_cpu(comp->cb_offset_h) << 32 |
576 		    be32_to_cpu(comp->cb_offset_l);
577 	comp_size = be32_to_cpu(comp->size);
578 	comp_buf_size = comp_size + mlxfw_mfa2_comp_magic_len;
579 
580 	comp_data = kmalloc(sizeof(*comp_data) + comp_buf_size, GFP_KERNEL);
581 	if (!comp_data)
582 		return ERR_PTR(-ENOMEM);
583 	comp_data->comp.data_size = comp_size;
584 	comp_data->comp.index = be16_to_cpu(comp->identifier);
585 	err = mlxfw_mfa2_file_cb_offset_xz(mfa2_file, cb_offset, comp_buf_size,
586 					   comp_data->buff);
587 	if (err) {
588 		pr_err("Component could not be reached in CB\n");
589 		goto err_out;
590 	}
591 
592 	if (memcmp(comp_data->buff, mlxfw_mfa2_comp_magic,
593 		   mlxfw_mfa2_comp_magic_len) != 0) {
594 		pr_err("Component has wrong magic\n");
595 		err = -EINVAL;
596 		goto err_out;
597 	}
598 
599 	comp_data->comp.data = comp_data->buff + mlxfw_mfa2_comp_magic_len;
600 	return &comp_data->comp;
601 err_out:
602 	kfree(comp_data);
603 	return ERR_PTR(err);
604 }
605 
mlxfw_mfa2_file_component_put(struct mlxfw_mfa2_component * comp)606 void mlxfw_mfa2_file_component_put(struct mlxfw_mfa2_component *comp)
607 {
608 	const struct mlxfw_mfa2_comp_data *comp_data;
609 
610 	comp_data = container_of(comp, struct mlxfw_mfa2_comp_data, comp);
611 	kfree(comp_data);
612 }
613 
mlxfw_mfa2_file_fini(struct mlxfw_mfa2_file * mfa2_file)614 void mlxfw_mfa2_file_fini(struct mlxfw_mfa2_file *mfa2_file)
615 {
616 	kfree(mfa2_file);
617 }
618