1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
|
// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
/* Copyright (c) 2017-2019 Mellanox Technologies. All rights reserved */
#define pr_fmt(fmt) "mlxfw_mfa2: " fmt
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/netlink.h>
#include <linux/vmalloc.h>
#include <linux/xz.h>
#include "mlxfw_mfa2.h"
#include "mlxfw_mfa2_file.h"
#include "mlxfw_mfa2_tlv.h"
#include "mlxfw_mfa2_format.h"
#include "mlxfw_mfa2_tlv_multi.h"
/* MFA2 FILE
* +----------------------------------+
* | MFA2 finger print |
* +----------------------------------+
* | package descriptor multi_tlv |
* | +------------------------------+ | +-----------------+
* | | package descriptor tlv +-----> |num_devices=n |
* | +------------------------------+ | |num_components=m |
* +----------------------------------+ |CB offset |
* | device descriptor multi_tlv | |... |
* | +------------------------------+ | | |
* | | PSID tlv | | +-----------------+
* | +------------------------------+ |
* | | component index tlv | |
* | +------------------------------+ |
* +----------------------------------+
* | component descriptor multi_tlv |
* | +------------------------------+ | +-----------------+
* | | component descriptor tlv +-----> |Among others: |
* | +------------------------------+ | |CB offset=o |
* +----------------------------------+ |comp index=i |
* | | |... |
* | | | |
* | | +-----------------+
* | COMPONENT BLOCK (CB) |
* | |
* | |
* | |
* +----------------------------------+
*
* On the top level, an MFA2 file contains:
* - Fingerprint
* - Several multi_tlvs (TLVs of type MLXFW_MFA2_TLV_MULTI, as defined in
* mlxfw_mfa2_format.h)
* - Compresses content block
*
* The first multi_tlv
* -------------------
* The first multi TLV is treated as package descriptor, and expected to have a
* first TLV child of type MLXFW_MFA2_TLV_PACKAGE_DESCRIPTOR which contains all
* the global information needed to parse the file. Among others, it contains
* the number of device descriptors and component descriptor following this
* multi TLV.
*
* The device descriptor multi_tlv
* -------------------------------
* The multi TLVs following the package descriptor are treated as device
* descriptor, and are expected to have the following children:
* - PSID TLV child of type MLXFW_MFA2_TLV_PSID containing that device PSID.
* - Component index of type MLXFW_MFA2_TLV_COMPONENT_PTR that contains that
* device component index.
*
* The component descriptor multi_tlv
* ----------------------------------
* The multi TLVs following the device descriptor multi TLVs are treated as
* component descriptor, and are expected to have a first child of type
* MLXFW_MFA2_TLV_COMPONENT_DESCRIPTOR that contains mostly the component index,
* needed for the flash process and the offset to the binary within the
* component block.
*/
static const u8 mlxfw_mfa2_fingerprint[] = "MLNX.MFA2.XZ.00!";
static const int mlxfw_mfa2_fingerprint_len =
sizeof(mlxfw_mfa2_fingerprint) - 1;
static const u8 mlxfw_mfa2_comp_magic[] = "#BIN.COMPONENT!#";
static const int mlxfw_mfa2_comp_magic_len = sizeof(mlxfw_mfa2_comp_magic) - 1;
bool mlxfw_mfa2_check(const struct firmware *fw)
{
if (fw->size < sizeof(mlxfw_mfa2_fingerprint))
return false;
return memcmp(fw->data, mlxfw_mfa2_fingerprint,
mlxfw_mfa2_fingerprint_len) == 0;
}
static bool
mlxfw_mfa2_tlv_multi_validate(const struct mlxfw_mfa2_file *mfa2_file,
const struct mlxfw_mfa2_tlv_multi *multi)
{
const struct mlxfw_mfa2_tlv *tlv;
u16 idx;
/* Check that all children are valid */
mlxfw_mfa2_tlv_multi_foreach(mfa2_file, tlv, idx, multi) {
if (!tlv) {
pr_err("Multi has invalid child");
return false;
}
}
return true;
}
static bool
mlxfw_mfa2_file_dev_validate(const struct mlxfw_mfa2_file *mfa2_file,
const struct mlxfw_mfa2_tlv *dev_tlv,
u16 dev_idx)
{
const struct mlxfw_mfa2_tlv_component_ptr *cptr;
const struct mlxfw_mfa2_tlv_multi *multi;
const struct mlxfw_mfa2_tlv_psid *psid;
const struct mlxfw_mfa2_tlv *tlv;
u16 cptr_count;
u16 cptr_idx;
int err;
pr_debug("Device %d\n", dev_idx);
multi = mlxfw_mfa2_tlv_multi_get(mfa2_file, dev_tlv);
if (!multi) {
pr_err("Device %d is not a valid TLV error\n", dev_idx);
return false;
}
if (!mlxfw_mfa2_tlv_multi_validate(mfa2_file, multi))
return false;
/* Validate the device has PSID tlv */
tlv = mlxfw_mfa2_tlv_multi_child_find(mfa2_file, multi,
MLXFW_MFA2_TLV_PSID, 0);
if (!tlv) {
pr_err("Device %d does not have PSID\n", dev_idx);
return false;
}
psid = mlxfw_mfa2_tlv_psid_get(mfa2_file, tlv);
if (!psid) {
pr_err("Device %d PSID TLV is not valid\n", dev_idx);
return false;
}
print_hex_dump_debug(" -- Device PSID ", DUMP_PREFIX_NONE, 16, 16,
psid->psid, be16_to_cpu(tlv->len), true);
/* Validate the device has COMPONENT_PTR */
err = mlxfw_mfa2_tlv_multi_child_count(mfa2_file, multi,
MLXFW_MFA2_TLV_COMPONENT_PTR,
&cptr_count);
if (err)
return false;
if (cptr_count == 0) {
pr_err("Device %d has no components\n", dev_idx);
return false;
}
for (cptr_idx = 0; cptr_idx < cptr_count; cptr_idx++) {
tlv = mlxfw_mfa2_tlv_multi_child_find(mfa2_file, multi,
MLXFW_MFA2_TLV_COMPONENT_PTR,
cptr_idx);
if (!tlv)
return false;
cptr = mlxfw_mfa2_tlv_component_ptr_get(mfa2_file, tlv);
if (!cptr) {
pr_err("Device %d COMPONENT_PTR TLV is not valid\n",
dev_idx);
return false;
}
pr_debug(" -- Component index %d\n",
be16_to_cpu(cptr->component_index));
}
return true;
}
static bool
mlxfw_mfa2_file_comp_validate(const struct mlxfw_mfa2_file *mfa2_file,
const struct mlxfw_mfa2_tlv *comp_tlv,
u16 comp_idx)
{
const struct mlxfw_mfa2_tlv_component_descriptor *cdesc;
const struct mlxfw_mfa2_tlv_multi *multi;
const struct mlxfw_mfa2_tlv *tlv;
pr_debug("Component %d\n", comp_idx);
multi = mlxfw_mfa2_tlv_multi_get(mfa2_file, comp_tlv);
if (!multi) {
pr_err("Component %d is not a valid TLV error\n", comp_idx);
return false;
}
if (!mlxfw_mfa2_tlv_multi_validate(mfa2_file, multi))
return false;
/* Check that component have COMPONENT_DESCRIPTOR as first child */
tlv = mlxfw_mfa2_tlv_multi_child(mfa2_file, multi);
if (!tlv) {
pr_err("Component descriptor %d multi TLV error\n", comp_idx);
return false;
}
cdesc = mlxfw_mfa2_tlv_component_descriptor_get(mfa2_file, tlv);
if (!cdesc) {
pr_err("Component %d does not have a valid descriptor\n",
comp_idx);
return false;
}
pr_debug(" -- Component type %d\n", be16_to_cpu(cdesc->identifier));
pr_debug(" -- Offset 0x%llx and size %d\n",
((u64) be32_to_cpu(cdesc->cb_offset_h) << 32)
| be32_to_cpu(cdesc->cb_offset_l), be32_to_cpu(cdesc->size));
return true;
}
static bool mlxfw_mfa2_file_validate(const struct mlxfw_mfa2_file *mfa2_file)
{
const struct mlxfw_mfa2_tlv *tlv;
u16 idx;
pr_debug("Validating file\n");
/* check that all the devices exist */
mlxfw_mfa2_tlv_foreach(mfa2_file, tlv, idx, mfa2_file->first_dev,
mfa2_file->dev_count) {
if (!tlv) {
pr_err("Device TLV error\n");
return false;
}
/* Check each device */
if (!mlxfw_mfa2_file_dev_validate(mfa2_file, tlv, idx))
return false;
}
/* check that all the components exist */
mlxfw_mfa2_tlv_foreach(mfa2_file, tlv, idx, mfa2_file->first_component,
mfa2_file->component_count) {
if (!tlv) {
pr_err("Device TLV error\n");
return false;
}
/* Check each component */
if (!mlxfw_mfa2_file_comp_validate(mfa2_file, tlv, idx))
return false;
}
return true;
}
struct mlxfw_mfa2_file *mlxfw_mfa2_file_init(const struct firmware *fw)
{
const <
|