2 * drivers/net/ethernet/mellanox/mlxfw/mlxfw_mfa2.c
3 * Copyright (c) 2017 Mellanox Technologies. All rights reserved.
4 * Copyright (c) 2017 Yotam Gigi <yotamg@mellanox.com>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the names of the copyright holders nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * Alternatively, this software may be distributed under the terms of the
19 * GNU General Public License ("GPL") version 2 as published by the Free
20 * Software Foundation.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
35 #define pr_fmt(fmt) "mlxfw_mfa2: " fmt
37 #include <linux/kernel.h>
38 #include <linux/module.h>
39 #include <linux/netlink.h>
40 #include <linux/vmalloc.h>
42 #include "mlxfw_mfa2.h"
43 #include "mlxfw_mfa2_file.h"
44 #include "mlxfw_mfa2_tlv.h"
45 #include "mlxfw_mfa2_format.h"
46 #include "mlxfw_mfa2_tlv_multi.h"
49 * +----------------------------------+
50 * | MFA2 finger print |
51 * +----------------------------------+
52 * | package descriptor multi_tlv |
53 * | +------------------------------+ | +-----------------+
54 * | | package descriptor tlv +-----> |num_devices=n |
55 * | +------------------------------+ | |num_components=m |
56 * +----------------------------------+ |CB offset |
57 * | device descriptor multi_tlv | |... |
58 * | +------------------------------+ | | |
59 * | | PSID tlv | | +-----------------+
60 * | +------------------------------+ |
61 * | | component index tlv | |
62 * | +------------------------------+ |
63 * +----------------------------------+
64 * | component descriptor multi_tlv |
65 * | +------------------------------+ | +-----------------+
66 * | | component descriptor tlv +-----> |Among others: |
67 * | +------------------------------+ | |CB offset=o |
68 * +----------------------------------+ |comp index=i |
71 * | | +-----------------+
72 * | COMPONENT BLOCK (CB) |
76 * +----------------------------------+
78 * On the top level, an MFA2 file contains:
80 * - Several multi_tlvs (TLVs of type MLXFW_MFA2_TLV_MULTI, as defined in
81 * mlxfw_mfa2_format.h)
82 * - Compresses content block
86 * The first multi TLV is treated as package descriptor, and expected to have a
87 * first TLV child of type MLXFW_MFA2_TLV_PACKAGE_DESCRIPTOR which contains all
88 * the global information needed to parse the file. Among others, it contains
89 * the number of device descriptors and component descriptor following this
92 * The device descriptor multi_tlv
93 * -------------------------------
94 * The multi TLVs following the package descriptor are treated as device
95 * descriptor, and are expected to have the following children:
96 * - PSID TLV child of type MLXFW_MFA2_TLV_PSID containing that device PSID.
97 * - Component index of type MLXFW_MFA2_TLV_COMPONENT_PTR that contains that
98 * device component index.
100 * The component descriptor multi_tlv
101 * ----------------------------------
102 * The multi TLVs following the device descriptor multi TLVs are treated as
103 * component descriptor, and are expected to have a first child of type
104 * MLXFW_MFA2_TLV_COMPONENT_DESCRIPTOR that contains mostly the component index,
105 * needed for the flash process and the offset to the binary within the
109 static const u8 mlxfw_mfa2_fingerprint
[] = "MLNX.MFA2.XZ.00!";
110 static const int mlxfw_mfa2_fingerprint_len
=
111 sizeof(mlxfw_mfa2_fingerprint
) - 1;
113 static const u8 mlxfw_mfa2_comp_magic
[] = "#BIN.COMPONENT!#";
114 static const int mlxfw_mfa2_comp_magic_len
= sizeof(mlxfw_mfa2_comp_magic
) - 1;
116 bool mlxfw_mfa2_check(const struct firmware
*fw
)
118 if (fw
->size
< sizeof(mlxfw_mfa2_fingerprint
))
121 return memcmp(fw
->data
, mlxfw_mfa2_fingerprint
,
122 mlxfw_mfa2_fingerprint_len
) == 0;
126 mlxfw_mfa2_tlv_multi_validate(const struct mlxfw_mfa2_file
*mfa2_file
,
127 const struct mlxfw_mfa2_tlv_multi
*multi
)
129 const struct mlxfw_mfa2_tlv
*tlv
;
132 /* Check that all children are valid */
133 mlxfw_mfa2_tlv_multi_foreach(mfa2_file
, tlv
, idx
, multi
) {
135 pr_err("Multi has invalid child");
143 mlxfw_mfa2_file_dev_validate(const struct mlxfw_mfa2_file
*mfa2_file
,
144 const struct mlxfw_mfa2_tlv
*dev_tlv
,
147 const struct mlxfw_mfa2_tlv_component_ptr
*cptr
;
148 const struct mlxfw_mfa2_tlv_multi
*multi
;
149 const struct mlxfw_mfa2_tlv_psid
*psid
;
150 const struct mlxfw_mfa2_tlv
*tlv
;
155 pr_debug("Device %d\n", dev_idx
);
157 multi
= mlxfw_mfa2_tlv_multi_get(mfa2_file
, dev_tlv
);
159 pr_err("Device %d is not a valid TLV error\n", dev_idx
);
163 if (!mlxfw_mfa2_tlv_multi_validate(mfa2_file
, multi
))
166 /* Validate the device has PSID tlv */
167 tlv
= mlxfw_mfa2_tlv_multi_child_find(mfa2_file
, multi
,
168 MLXFW_MFA2_TLV_PSID
, 0);
170 pr_err("Device %d does not have PSID\n", dev_idx
);
174 psid
= mlxfw_mfa2_tlv_psid_get(mfa2_file
, tlv
);
176 pr_err("Device %d PSID TLV is not valid\n", dev_idx
);
180 print_hex_dump_debug(" -- Device PSID ", DUMP_PREFIX_NONE
, 16, 16,
181 psid
->psid
, be16_to_cpu(tlv
->len
), true);
183 /* Validate the device has COMPONENT_PTR */
184 err
= mlxfw_mfa2_tlv_multi_child_count(mfa2_file
, multi
,
185 MLXFW_MFA2_TLV_COMPONENT_PTR
,
190 if (cptr_count
== 0) {
191 pr_err("Device %d has no components\n", dev_idx
);
195 for (cptr_idx
= 0; cptr_idx
< cptr_count
; cptr_idx
++) {
196 tlv
= mlxfw_mfa2_tlv_multi_child_find(mfa2_file
, multi
,
197 MLXFW_MFA2_TLV_COMPONENT_PTR
,
202 cptr
= mlxfw_mfa2_tlv_component_ptr_get(mfa2_file
, tlv
);
204 pr_err("Device %d COMPONENT_PTR TLV is not valid\n",
209 pr_debug(" -- Component index %d\n",
210 be16_to_cpu(cptr
->component_index
));
216 mlxfw_mfa2_file_comp_validate(const struct mlxfw_mfa2_file
*mfa2_file
,
217 const struct mlxfw_mfa2_tlv
*comp_tlv
,
220 const struct mlxfw_mfa2_tlv_component_descriptor
*cdesc
;
221 const struct mlxfw_mfa2_tlv_multi
*multi
;
222 const struct mlxfw_mfa2_tlv
*tlv
;
224 pr_debug("Component %d\n", comp_idx
);
226 multi
= mlxfw_mfa2_tlv_multi_get(mfa2_file
, comp_tlv
);
228 pr_err("Component %d is not a valid TLV error\n", comp_idx
);
232 if (!mlxfw_mfa2_tlv_multi_validate(mfa2_file
, multi
))
235 /* Check that component have COMPONENT_DESCRIPTOR as first child */
236 tlv
= mlxfw_mfa2_tlv_multi_child(mfa2_file
, multi
);
238 pr_err("Component descriptor %d multi TLV error\n", comp_idx
);
242 cdesc
= mlxfw_mfa2_tlv_component_descriptor_get(mfa2_file
, tlv
);
244 pr_err("Component %d does not have a valid descriptor\n",
248 pr_debug(" -- Component type %d\n", be16_to_cpu(cdesc
->identifier
));
249 pr_debug(" -- Offset 0x%llx and size %d\n",
250 ((u64
) be32_to_cpu(cdesc
->cb_offset_h
) << 32)
251 | be32_to_cpu(cdesc
->cb_offset_l
), be32_to_cpu(cdesc
->size
));
256 static bool mlxfw_mfa2_file_validate(const struct mlxfw_mfa2_file
*mfa2_file
)
258 const struct mlxfw_mfa2_tlv
*tlv
;
261 pr_debug("Validating file\n");
263 /* check that all the devices exist */
264 mlxfw_mfa2_tlv_foreach(mfa2_file
, tlv
, idx
, mfa2_file
->first_dev
,
265 mfa2_file
->dev_count
) {
267 pr_err("Device TLV error\n");
271 /* Check each device */
272 if (!mlxfw_mfa2_file_dev_validate(mfa2_file
, tlv
, idx
))
276 /* check that all the components exist */
277 mlxfw_mfa2_tlv_foreach(mfa2_file
, tlv
, idx
, mfa2_file
->first_component
,
278 mfa2_file
->component_count
) {
280 pr_err("Device TLV error\n");
284 /* Check each component */
285 if (!mlxfw_mfa2_file_comp_validate(mfa2_file
, tlv
, idx
))
291 struct mlxfw_mfa2_file
*mlxfw_mfa2_file_init(const struct firmware
*fw
)
293 const struct mlxfw_mfa2_tlv_package_descriptor
*pd
;
294 const struct mlxfw_mfa2_tlv_multi
*multi
;
295 const struct mlxfw_mfa2_tlv
*multi_child
;
296 const struct mlxfw_mfa2_tlv
*first_tlv
;
297 struct mlxfw_mfa2_file
*mfa2_file
;
298 const void *first_tlv_ptr
;
299 const void *cb_top_ptr
;
301 mfa2_file
= kcalloc(1, sizeof(*mfa2_file
), GFP_KERNEL
);
303 return ERR_PTR(-ENOMEM
);
306 first_tlv_ptr
= fw
->data
+ NLA_ALIGN(mlxfw_mfa2_fingerprint_len
);
307 first_tlv
= mlxfw_mfa2_tlv_get(mfa2_file
, first_tlv_ptr
);
309 pr_err("Could not parse package descriptor TLV\n");
313 multi
= mlxfw_mfa2_tlv_multi_get(mfa2_file
, first_tlv
);
315 pr_err("First TLV is not of valid multi type\n");
319 multi_child
= mlxfw_mfa2_tlv_multi_child(mfa2_file
, multi
);
323 pd
= mlxfw_mfa2_tlv_package_descriptor_get(mfa2_file
, multi_child
);
325 pr_err("Could not parse package descriptor TLV\n");
329 mfa2_file
->first_dev
= mlxfw_mfa2_tlv_next(mfa2_file
, first_tlv
);
330 if (!mfa2_file
->first_dev
) {
331 pr_err("First device TLV is not valid\n");
335 mfa2_file
->dev_count
= be16_to_cpu(pd
->num_devices
);
336 mfa2_file
->first_component
= mlxfw_mfa2_tlv_advance(mfa2_file
,
337 mfa2_file
->first_dev
,
338 mfa2_file
->dev_count
);
339 mfa2_file
->component_count
= be16_to_cpu(pd
->num_components
);
340 mfa2_file
->cb
= fw
->data
+ NLA_ALIGN(be32_to_cpu(pd
->cb_offset
));
341 if (!mlxfw_mfa2_valid_ptr(mfa2_file
, mfa2_file
->cb
)) {
342 pr_err("Component block is out side the file\n");
345 mfa2_file
->cb_archive_size
= be32_to_cpu(pd
->cb_archive_size
);
346 cb_top_ptr
= mfa2_file
->cb
+ mfa2_file
->cb_archive_size
- 1;
347 if (!mlxfw_mfa2_valid_ptr(mfa2_file
, cb_top_ptr
)) {
348 pr_err("Component block size is too big\n");
352 if (!mlxfw_mfa2_file_validate(mfa2_file
))
357 return ERR_PTR(-EINVAL
);
360 static const struct mlxfw_mfa2_tlv_multi
*
361 mlxfw_mfa2_tlv_dev_get(const struct mlxfw_mfa2_file
*mfa2_file
,
362 const char *psid
, u16 psid_size
)
364 const struct mlxfw_mfa2_tlv_psid
*tlv_psid
;
365 const struct mlxfw_mfa2_tlv_multi
*dev_multi
;
366 const struct mlxfw_mfa2_tlv
*dev_tlv
;
367 const struct mlxfw_mfa2_tlv
*tlv
;
370 /* for each device tlv */
371 mlxfw_mfa2_tlv_foreach(mfa2_file
, dev_tlv
, idx
, mfa2_file
->first_dev
,
372 mfa2_file
->dev_count
) {
376 dev_multi
= mlxfw_mfa2_tlv_multi_get(mfa2_file
, dev_tlv
);
380 /* find psid child and compare */
381 tlv
= mlxfw_mfa2_tlv_multi_child_find(mfa2_file
, dev_multi
,
382 MLXFW_MFA2_TLV_PSID
, 0);
385 if (be16_to_cpu(tlv
->len
) != psid_size
)
388 tlv_psid
= mlxfw_mfa2_tlv_psid_get(mfa2_file
, tlv
);
392 if (memcmp(psid
, tlv_psid
->psid
, psid_size
) == 0)
399 int mlxfw_mfa2_file_component_count(const struct mlxfw_mfa2_file
*mfa2_file
,
400 const char *psid
, u32 psid_size
,
403 const struct mlxfw_mfa2_tlv_multi
*dev_multi
;
407 dev_multi
= mlxfw_mfa2_tlv_dev_get(mfa2_file
, psid
, psid_size
);
411 err
= mlxfw_mfa2_tlv_multi_child_count(mfa2_file
, dev_multi
,
412 MLXFW_MFA2_TLV_COMPONENT_PTR
,
421 static int mlxfw_mfa2_xz_dec_run(struct xz_dec
*xz_dec
, struct xz_buf
*xz_buf
,
426 xz_ret
= xz_dec_run(xz_dec
, xz_buf
);
436 pr_err("xz no memory\n");
439 pr_err("xz file corrupted\n");
441 case XZ_FORMAT_ERROR
:
442 pr_err("xz format not found\n");
444 case XZ_OPTIONS_ERROR
:
445 pr_err("unsupported xz option\n");
447 case XZ_MEMLIMIT_ERROR
:
448 pr_err("xz dictionary too small\n");
451 pr_err("xz error %d\n", xz_ret
);
456 static int mlxfw_mfa2_file_cb_offset_xz(const struct mlxfw_mfa2_file
*mfa2_file
,
457 off_t off
, size_t size
, u8
*buf
)
459 struct xz_dec
*xz_dec
;
460 struct xz_buf dec_buf
;
465 xz_dec
= xz_dec_init(XZ_DYNALLOC
, (u32
) -1);
469 dec_buf
.in_size
= mfa2_file
->cb_archive_size
;
470 dec_buf
.in
= mfa2_file
->cb
;
474 /* decode up to the offset */
477 dec_buf
.out_size
= min_t(size_t, size
, off
- curr_off
);
478 if (dec_buf
.out_size
== 0)
481 err
= mlxfw_mfa2_xz_dec_run(xz_dec
, &dec_buf
, &finished
);
485 pr_err("xz section too short\n");
489 curr_off
+= dec_buf
.out_pos
;
490 } while (curr_off
!= off
);
492 /* decode the needed section */
494 dec_buf
.out_size
= size
;
495 err
= mlxfw_mfa2_xz_dec_run(xz_dec
, &dec_buf
, &finished
);
501 static const struct mlxfw_mfa2_tlv_component_descriptor
*
502 mlxfw_mfa2_file_component_tlv_get(const struct mlxfw_mfa2_file
*mfa2_file
,
505 const struct mlxfw_mfa2_tlv_multi
*multi
;
506 const struct mlxfw_mfa2_tlv
*multi_child
;
507 const struct mlxfw_mfa2_tlv
*comp_tlv
;
509 if (comp_index
> mfa2_file
->component_count
)
512 comp_tlv
= mlxfw_mfa2_tlv_advance(mfa2_file
, mfa2_file
->first_component
,
517 multi
= mlxfw_mfa2_tlv_multi_get(mfa2_file
, comp_tlv
);
521 multi_child
= mlxfw_mfa2_tlv_multi_child(mfa2_file
, multi
);
525 return mlxfw_mfa2_tlv_component_descriptor_get(mfa2_file
, multi_child
);
528 struct mlxfw_mfa2_comp_data
{
529 struct mlxfw_mfa2_component comp
;
533 static const struct mlxfw_mfa2_tlv_component_descriptor
*
534 mlxfw_mfa2_file_component_find(const struct mlxfw_mfa2_file
*mfa2_file
,
535 const char *psid
, int psid_size
,
538 const struct mlxfw_mfa2_tlv_component_ptr
*cptr
;
539 const struct mlxfw_mfa2_tlv_multi
*dev_multi
;
540 const struct mlxfw_mfa2_tlv
*cptr_tlv
;
543 dev_multi
= mlxfw_mfa2_tlv_dev_get(mfa2_file
, psid
, psid_size
);
547 cptr_tlv
= mlxfw_mfa2_tlv_multi_child_find(mfa2_file
, dev_multi
,
548 MLXFW_MFA2_TLV_COMPONENT_PTR
,
553 cptr
= mlxfw_mfa2_tlv_component_ptr_get(mfa2_file
, cptr_tlv
);
557 comp_idx
= be16_to_cpu(cptr
->component_index
);
558 return mlxfw_mfa2_file_component_tlv_get(mfa2_file
, comp_idx
);
561 struct mlxfw_mfa2_component
*
562 mlxfw_mfa2_file_component_get(const struct mlxfw_mfa2_file
*mfa2_file
,
563 const char *psid
, int psid_size
,
566 const struct mlxfw_mfa2_tlv_component_descriptor
*comp
;
567 struct mlxfw_mfa2_comp_data
*comp_data
;
573 comp
= mlxfw_mfa2_file_component_find(mfa2_file
, psid
, psid_size
,
576 return ERR_PTR(-EINVAL
);
578 cb_offset
= (u64
) be32_to_cpu(comp
->cb_offset_h
) << 32 |
579 be32_to_cpu(comp
->cb_offset_l
);
580 comp_size
= be32_to_cpu(comp
->size
);
581 comp_buf_size
= comp_size
+ mlxfw_mfa2_comp_magic_len
;
583 comp_data
= vzalloc(sizeof(*comp_data
) + comp_buf_size
);
585 return ERR_PTR(-ENOMEM
);
586 comp_data
->comp
.data_size
= comp_size
;
587 comp_data
->comp
.index
= be16_to_cpu(comp
->identifier
);
588 err
= mlxfw_mfa2_file_cb_offset_xz(mfa2_file
, cb_offset
, comp_buf_size
,
591 pr_err("Component could not be reached in CB\n");
595 if (memcmp(comp_data
->buff
, mlxfw_mfa2_comp_magic
,
596 mlxfw_mfa2_comp_magic_len
) != 0) {
597 pr_err("Component has wrong magic\n");
602 comp_data
->comp
.data
= comp_data
->buff
+ mlxfw_mfa2_comp_magic_len
;
603 return &comp_data
->comp
;
609 void mlxfw_mfa2_file_component_put(struct mlxfw_mfa2_component
*comp
)
611 const struct mlxfw_mfa2_comp_data
*comp_data
;
613 comp_data
= container_of(comp
, struct mlxfw_mfa2_comp_data
, comp
);
617 void mlxfw_mfa2_file_fini(struct mlxfw_mfa2_file
*mfa2_file
)