1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /* Copyright (c) 2017-2019 Mellanox Technologies. All rights reserved */
4 #define pr_fmt(fmt) "mlxfw_mfa2: " fmt
6 #include <linux/kernel.h>
7 #include <linux/module.h>
8 #include <linux/netlink.h>
10 #include "mlxfw_mfa2.h"
11 #include "mlxfw_mfa2_file.h"
12 #include "mlxfw_mfa2_tlv.h"
13 #include "mlxfw_mfa2_format.h"
14 #include "mlxfw_mfa2_tlv_multi.h"
17 * +----------------------------------+
18 * | MFA2 finger print |
19 * +----------------------------------+
20 * | package descriptor multi_tlv |
21 * | +------------------------------+ | +-----------------+
22 * | | package descriptor tlv +-----> |num_devices=n |
23 * | +------------------------------+ | |num_components=m |
24 * +----------------------------------+ |CB offset |
25 * | device descriptor multi_tlv | |... |
26 * | +------------------------------+ | | |
27 * | | PSID tlv | | +-----------------+
28 * | +------------------------------+ |
29 * | | component index tlv | |
30 * | +------------------------------+ |
31 * +----------------------------------+
32 * | component descriptor multi_tlv |
33 * | +------------------------------+ | +-----------------+
34 * | | component descriptor tlv +-----> |Among others: |
35 * | +------------------------------+ | |CB offset=o |
36 * +----------------------------------+ |comp index=i |
39 * | | +-----------------+
40 * | COMPONENT BLOCK (CB) |
44 * +----------------------------------+
46 * On the top level, an MFA2 file contains:
48 * - Several multi_tlvs (TLVs of type MLXFW_MFA2_TLV_MULTI, as defined in
49 * mlxfw_mfa2_format.h)
50 * - Compresses content block
54 * The first multi TLV is treated as package descriptor, and expected to have a
55 * first TLV child of type MLXFW_MFA2_TLV_PACKAGE_DESCRIPTOR which contains all
56 * the global information needed to parse the file. Among others, it contains
57 * the number of device descriptors and component descriptor following this
60 * The device descriptor multi_tlv
61 * -------------------------------
62 * The multi TLVs following the package descriptor are treated as device
63 * descriptor, and are expected to have the following children:
64 * - PSID TLV child of type MLXFW_MFA2_TLV_PSID containing that device PSID.
65 * - Component index of type MLXFW_MFA2_TLV_COMPONENT_PTR that contains that
66 * device component index.
68 * The component descriptor multi_tlv
69 * ----------------------------------
70 * The multi TLVs following the device descriptor multi TLVs are treated as
71 * component descriptor, and are expected to have a first child of type
72 * MLXFW_MFA2_TLV_COMPONENT_DESCRIPTOR that contains mostly the component index,
73 * needed for the flash process and the offset to the binary within the
77 static const u8 mlxfw_mfa2_fingerprint[] = "MLNX.MFA2.XZ.00!";
78 static const int mlxfw_mfa2_fingerprint_len =
79 sizeof(mlxfw_mfa2_fingerprint) - 1;
81 static const u8 mlxfw_mfa2_comp_magic[] = "#BIN.COMPONENT!#";
82 static const int mlxfw_mfa2_comp_magic_len = sizeof(mlxfw_mfa2_comp_magic) - 1;
84 bool mlxfw_mfa2_check(const struct firmware *fw)
86 if (fw->size < sizeof(mlxfw_mfa2_fingerprint))
89 return memcmp(fw->data, mlxfw_mfa2_fingerprint,
90 mlxfw_mfa2_fingerprint_len) == 0;
94 mlxfw_mfa2_tlv_multi_validate(const struct mlxfw_mfa2_file *mfa2_file,
95 const struct mlxfw_mfa2_tlv_multi *multi)
97 const struct mlxfw_mfa2_tlv *tlv;
100 /* Check that all children are valid */
101 mlxfw_mfa2_tlv_multi_foreach(mfa2_file, tlv, idx, multi) {
103 pr_err("Multi has invalid child");
111 mlxfw_mfa2_file_dev_validate(const struct mlxfw_mfa2_file *mfa2_file,
112 const struct mlxfw_mfa2_tlv *dev_tlv,
115 const struct mlxfw_mfa2_tlv_component_ptr *cptr;
116 const struct mlxfw_mfa2_tlv_multi *multi;
117 const struct mlxfw_mfa2_tlv_psid *psid;
118 const struct mlxfw_mfa2_tlv *tlv;
123 pr_debug("Device %d\n", dev_idx);
125 multi = mlxfw_mfa2_tlv_multi_get(mfa2_file, dev_tlv);
127 pr_err("Device %d is not a valid TLV error\n", dev_idx);
131 if (!mlxfw_mfa2_tlv_multi_validate(mfa2_file, multi))
134 /* Validate the device has PSID tlv */
135 tlv = mlxfw_mfa2_tlv_multi_child_find(mfa2_file, multi,
136 MLXFW_MFA2_TLV_PSID, 0);
138 pr_err("Device %d does not have PSID\n", dev_idx);
142 psid = mlxfw_mfa2_tlv_psid_get(mfa2_file, tlv);
144 pr_err("Device %d PSID TLV is not valid\n", dev_idx);
148 print_hex_dump_debug(" -- Device PSID ", DUMP_PREFIX_NONE, 16, 16,
149 psid->psid, be16_to_cpu(tlv->len), true);
151 /* Validate the device has COMPONENT_PTR */
152 err = mlxfw_mfa2_tlv_multi_child_count(mfa2_file, multi,
153 MLXFW_MFA2_TLV_COMPONENT_PTR,
158 if (cptr_count == 0) {
159 pr_err("Device %d has no components\n", dev_idx);
163 for (cptr_idx = 0; cptr_idx < cptr_count; cptr_idx++) {
164 tlv = mlxfw_mfa2_tlv_multi_child_find(mfa2_file, multi,
165 MLXFW_MFA2_TLV_COMPONENT_PTR,
170 cptr = mlxfw_mfa2_tlv_component_ptr_get(mfa2_file, tlv);
172 pr_err("Device %d COMPONENT_PTR TLV is not valid\n",
177 pr_debug(" -- Component index %d\n",
178 be16_to_cpu(cptr->component_index));
184 mlxfw_mfa2_file_comp_validate(const struct mlxfw_mfa2_file *mfa2_file,
185 const struct mlxfw_mfa2_tlv *comp_tlv,
188 const struct mlxfw_mfa2_tlv_component_descriptor *cdesc;
189 const struct mlxfw_mfa2_tlv_multi *multi;
190 const struct mlxfw_mfa2_tlv *tlv;
192 pr_debug("Component %d\n", comp_idx);
194 multi = mlxfw_mfa2_tlv_multi_get(mfa2_file, comp_tlv);
196 pr_err("Component %d is not a valid TLV error\n", comp_idx);
200 if (!mlxfw_mfa2_tlv_multi_validate(mfa2_file, multi))
203 /* Check that component have COMPONENT_DESCRIPTOR as first child */
204 tlv = mlxfw_mfa2_tlv_multi_child(mfa2_file, multi);
206 pr_err("Component descriptor %d multi TLV error\n", comp_idx);
210 cdesc = mlxfw_mfa2_tlv_component_descriptor_get(mfa2_file, tlv);
212 pr_err("Component %d does not have a valid descriptor\n",
216 pr_debug(" -- Component type %d\n", be16_to_cpu(cdesc->identifier));
217 pr_debug(" -- Offset 0x%llx and size %d\n",
218 ((u64) be32_to_cpu(cdesc->cb_offset_h) << 32)
219 | be32_to_cpu(cdesc->cb_offset_l), be32_to_cpu(cdesc->size));
224 static bool mlxfw_mfa2_file_validate(const struct mlxfw_mfa2_file *mfa2_file)
226 const struct mlxfw_mfa2_tlv *tlv;
229 pr_debug("Validating file\n");
231 /* check that all the devices exist */
232 mlxfw_mfa2_tlv_foreach(mfa2_file, tlv, idx, mfa2_file->first_dev,
233 mfa2_file->dev_count) {
235 pr_err("Device TLV error\n");
239 /* Check each device */
240 if (!mlxfw_mfa2_file_dev_validate(mfa2_file, tlv, idx))
244 /* check that all the components exist */
245 mlxfw_mfa2_tlv_foreach(mfa2_file, tlv, idx, mfa2_file->first_component,
246 mfa2_file->component_count) {
248 pr_err("Device TLV error\n");
252 /* Check each component */
253 if (!mlxfw_mfa2_file_comp_validate(mfa2_file, tlv, idx))
259 struct mlxfw_mfa2_file *mlxfw_mfa2_file_init(const struct firmware *fw)
261 const struct mlxfw_mfa2_tlv_package_descriptor *pd;
262 const struct mlxfw_mfa2_tlv_multi *multi;
263 const struct mlxfw_mfa2_tlv *multi_child;
264 const struct mlxfw_mfa2_tlv *first_tlv;
265 struct mlxfw_mfa2_file *mfa2_file;
266 const void *first_tlv_ptr;
267 const void *cb_top_ptr;
269 mfa2_file = kcalloc(1, sizeof(*mfa2_file), GFP_KERNEL);
271 return ERR_PTR(-ENOMEM);
274 first_tlv_ptr = fw->data + NLA_ALIGN(mlxfw_mfa2_fingerprint_len);
275 first_tlv = mlxfw_mfa2_tlv_get(mfa2_file, first_tlv_ptr);
277 pr_err("Could not parse package descriptor TLV\n");
281 multi = mlxfw_mfa2_tlv_multi_get(mfa2_file, first_tlv);
283 pr_err("First TLV is not of valid multi type\n");
287 multi_child = mlxfw_mfa2_tlv_multi_child(mfa2_file, multi);
291 pd = mlxfw_mfa2_tlv_package_descriptor_get(mfa2_file, multi_child);
293 pr_err("Could not parse package descriptor TLV\n");
297 mfa2_file->first_dev = mlxfw_mfa2_tlv_next(mfa2_file, first_tlv);
298 if (!mfa2_file->first_dev) {
299 pr_err("First device TLV is not valid\n");
303 mfa2_file->dev_count = be16_to_cpu(pd->num_devices);
304 mfa2_file->first_component = mlxfw_mfa2_tlv_advance(mfa2_file,
305 mfa2_file->first_dev,
306 mfa2_file->dev_count);
307 mfa2_file->component_count = be16_to_cpu(pd->num_components);
308 mfa2_file->cb = fw->data + NLA_ALIGN(be32_to_cpu(pd->cb_offset));
309 if (!mlxfw_mfa2_valid_ptr(mfa2_file, mfa2_file->cb)) {
310 pr_err("Component block is out side the file\n");
313 mfa2_file->cb_archive_size = be32_to_cpu(pd->cb_archive_size);
314 cb_top_ptr = mfa2_file->cb + mfa2_file->cb_archive_size - 1;
315 if (!mlxfw_mfa2_valid_ptr(mfa2_file, cb_top_ptr)) {
316 pr_err("Component block size is too big\n");
320 if (!mlxfw_mfa2_file_validate(mfa2_file))
325 return ERR_PTR(-EINVAL);
328 static const struct mlxfw_mfa2_tlv_multi *
329 mlxfw_mfa2_tlv_dev_get(const struct mlxfw_mfa2_file *mfa2_file,
330 const char *psid, u16 psid_size)
332 const struct mlxfw_mfa2_tlv_psid *tlv_psid;
333 const struct mlxfw_mfa2_tlv_multi *dev_multi;
334 const struct mlxfw_mfa2_tlv *dev_tlv;
335 const struct mlxfw_mfa2_tlv *tlv;
338 /* for each device tlv */
339 mlxfw_mfa2_tlv_foreach(mfa2_file, dev_tlv, idx, mfa2_file->first_dev,
340 mfa2_file->dev_count) {
344 dev_multi = mlxfw_mfa2_tlv_multi_get(mfa2_file, dev_tlv);
348 /* find psid child and compare */
349 tlv = mlxfw_mfa2_tlv_multi_child_find(mfa2_file, dev_multi,
350 MLXFW_MFA2_TLV_PSID, 0);
353 if (be16_to_cpu(tlv->len) != psid_size)
356 tlv_psid = mlxfw_mfa2_tlv_psid_get(mfa2_file, tlv);
360 if (memcmp(psid, tlv_psid->psid, psid_size) == 0)
367 int mlxfw_mfa2_file_component_count(const struct mlxfw_mfa2_file *mfa2_file,
368 const char *psid, u32 psid_size,
371 const struct mlxfw_mfa2_tlv_multi *dev_multi;
375 dev_multi = mlxfw_mfa2_tlv_dev_get(mfa2_file, psid, psid_size);
379 err = mlxfw_mfa2_tlv_multi_child_count(mfa2_file, dev_multi,
380 MLXFW_MFA2_TLV_COMPONENT_PTR,
389 static int mlxfw_mfa2_xz_dec_run(struct xz_dec *xz_dec, struct xz_buf *xz_buf,
394 xz_ret = xz_dec_run(xz_dec, xz_buf);
404 pr_err("xz no memory\n");
407 pr_err("xz file corrupted\n");
409 case XZ_FORMAT_ERROR:
410 pr_err("xz format not found\n");
412 case XZ_OPTIONS_ERROR:
413 pr_err("unsupported xz option\n");
415 case XZ_MEMLIMIT_ERROR:
416 pr_err("xz dictionary too small\n");
419 pr_err("xz error %d\n", xz_ret);
424 static int mlxfw_mfa2_file_cb_offset_xz(const struct mlxfw_mfa2_file *mfa2_file,
425 off_t off, size_t size, u8 *buf)
427 struct xz_dec *xz_dec;
428 struct xz_buf dec_buf;
433 xz_dec = xz_dec_init(XZ_DYNALLOC, (u32) -1);
437 dec_buf.in_size = mfa2_file->cb_archive_size;
438 dec_buf.in = mfa2_file->cb;
442 /* decode up to the offset */
445 dec_buf.out_size = min_t(size_t, size, off - curr_off);
446 if (dec_buf.out_size == 0)
449 err = mlxfw_mfa2_xz_dec_run(xz_dec, &dec_buf, &finished);
453 pr_err("xz section too short\n");
457 curr_off += dec_buf.out_pos;
458 } while (curr_off != off);
460 /* decode the needed section */
462 dec_buf.out_size = size;
463 err = mlxfw_mfa2_xz_dec_run(xz_dec, &dec_buf, &finished);
469 static const struct mlxfw_mfa2_tlv_component_descriptor *
470 mlxfw_mfa2_file_component_tlv_get(const struct mlxfw_mfa2_file *mfa2_file,
473 const struct mlxfw_mfa2_tlv_multi *multi;
474 const struct mlxfw_mfa2_tlv *multi_child;
475 const struct mlxfw_mfa2_tlv *comp_tlv;
477 if (comp_index > mfa2_file->component_count)
480 comp_tlv = mlxfw_mfa2_tlv_advance(mfa2_file, mfa2_file->first_component,
485 multi = mlxfw_mfa2_tlv_multi_get(mfa2_file, comp_tlv);
489 multi_child = mlxfw_mfa2_tlv_multi_child(mfa2_file, multi);
493 return mlxfw_mfa2_tlv_component_descriptor_get(mfa2_file, multi_child);
496 struct mlxfw_mfa2_comp_data {
497 struct mlxfw_mfa2_component comp;
501 static const struct mlxfw_mfa2_tlv_component_descriptor *
502 mlxfw_mfa2_file_component_find(const struct mlxfw_mfa2_file *mfa2_file,
503 const char *psid, int psid_size,
506 const struct mlxfw_mfa2_tlv_component_ptr *cptr;
507 const struct mlxfw_mfa2_tlv_multi *dev_multi;
508 const struct mlxfw_mfa2_tlv *cptr_tlv;
511 dev_multi = mlxfw_mfa2_tlv_dev_get(mfa2_file, psid, psid_size);
515 cptr_tlv = mlxfw_mfa2_tlv_multi_child_find(mfa2_file, dev_multi,
516 MLXFW_MFA2_TLV_COMPONENT_PTR,
521 cptr = mlxfw_mfa2_tlv_component_ptr_get(mfa2_file, cptr_tlv);
525 comp_idx = be16_to_cpu(cptr->component_index);
526 return mlxfw_mfa2_file_component_tlv_get(mfa2_file, comp_idx);
529 struct mlxfw_mfa2_component *
530 mlxfw_mfa2_file_component_get(const struct mlxfw_mfa2_file *mfa2_file,
531 const char *psid, int psid_size,
534 const struct mlxfw_mfa2_tlv_component_descriptor *comp;
535 struct mlxfw_mfa2_comp_data *comp_data;
541 comp = mlxfw_mfa2_file_component_find(mfa2_file, psid, psid_size,
544 return ERR_PTR(-EINVAL);
546 cb_offset = (u64) be32_to_cpu(comp->cb_offset_h) << 32 |
547 be32_to_cpu(comp->cb_offset_l);
548 comp_size = be32_to_cpu(comp->size);
549 comp_buf_size = comp_size + mlxfw_mfa2_comp_magic_len;
551 comp_data = kmalloc(sizeof(*comp_data) + comp_buf_size, GFP_KERNEL);
553 return ERR_PTR(-ENOMEM);
554 comp_data->comp.data_size = comp_size;
555 comp_data->comp.index = be16_to_cpu(comp->identifier);
556 err = mlxfw_mfa2_file_cb_offset_xz(mfa2_file, cb_offset, comp_buf_size,
559 pr_err("Component could not be reached in CB\n");
563 if (memcmp(comp_data->buff, mlxfw_mfa2_comp_magic,
564 mlxfw_mfa2_comp_magic_len) != 0) {
565 pr_err("Component has wrong magic\n");
570 comp_data->comp.data = comp_data->buff + mlxfw_mfa2_comp_magic_len;
571 return &comp_data->comp;
577 void mlxfw_mfa2_file_component_put(struct mlxfw_mfa2_component *comp)
579 const struct mlxfw_mfa2_comp_data *comp_data;
581 comp_data = container_of(comp, struct mlxfw_mfa2_comp_data, comp);
585 void mlxfw_mfa2_file_fini(struct mlxfw_mfa2_file *mfa2_file)