mlxfw_mfa2.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. /*
  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>
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  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.
  17. *
  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.
  21. *
  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.
  33. */
  34. #define pr_fmt(fmt) "mlxfw_mfa2: " fmt
  35. #include <linux/kernel.h>
  36. #include <linux/module.h>
  37. #include <linux/netlink.h>
  38. #include <linux/xz.h>
  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. /* MFA2 FILE
  45. * +----------------------------------+
  46. * | MFA2 finger print |
  47. * +----------------------------------+
  48. * | package descriptor multi_tlv |
  49. * | +------------------------------+ | +-----------------+
  50. * | | package descriptor tlv +-----> |num_devices=n |
  51. * | +------------------------------+ | |num_components=m |
  52. * +----------------------------------+ |CB offset |
  53. * | device descriptor multi_tlv | |... |
  54. * | +------------------------------+ | | |
  55. * | | PSID tlv | | +-----------------+
  56. * | +------------------------------+ |
  57. * | | component index tlv | |
  58. * | +------------------------------+ |
  59. * +----------------------------------+
  60. * | component descriptor multi_tlv |
  61. * | +------------------------------+ | +-----------------+
  62. * | | component descriptor tlv +-----> |Among others: |
  63. * | +------------------------------+ | |CB offset=o |
  64. * +----------------------------------+ |comp index=i |
  65. * | | |... |
  66. * | | | |
  67. * | | +-----------------+
  68. * | COMPONENT BLOCK (CB) |
  69. * | |
  70. * | |
  71. * | |
  72. * +----------------------------------+
  73. *
  74. * On the top level, an MFA2 file contains:
  75. * - Fingerprint
  76. * - Several multi_tlvs (TLVs of type MLXFW_MFA2_TLV_MULTI, as defined in
  77. * mlxfw_mfa2_format.h)
  78. * - Compresses content block
  79. *
  80. * The first multi_tlv
  81. * -------------------
  82. * The first multi TLV is treated as package descriptor, and expected to have a
  83. * first TLV child of type MLXFW_MFA2_TLV_PACKAGE_DESCRIPTOR which contains all
  84. * the global information needed to parse the file. Among others, it contains
  85. * the number of device descriptors and component descriptor following this
  86. * multi TLV.
  87. *
  88. * The device descriptor multi_tlv
  89. * -------------------------------
  90. * The multi TLVs following the package descriptor are treated as device
  91. * descriptor, and are expected to have the following children:
  92. * - PSID TLV child of type MLXFW_MFA2_TLV_PSID containing that device PSID.
  93. * - Component index of type MLXFW_MFA2_TLV_COMPONENT_PTR that contains that
  94. * device component index.
  95. *
  96. * The component descriptor multi_tlv
  97. * ----------------------------------
  98. * The multi TLVs following the device descriptor multi TLVs are treated as
  99. * component descriptor, and are expected to have a first child of type
  100. * MLXFW_MFA2_TLV_COMPONENT_DESCRIPTOR that contains mostly the component index,
  101. * needed for the flash process and the offset to the binary within the
  102. * component block.
  103. */
  104. static const u8 mlxfw_mfa2_fingerprint[] = "MLNX.MFA2.XZ.00!";
  105. static const int mlxfw_mfa2_fingerprint_len =
  106. sizeof(mlxfw_mfa2_fingerprint) - 1;
  107. static const u8 mlxfw_mfa2_comp_magic[] = "#BIN.COMPONENT!#";
  108. static const int mlxfw_mfa2_comp_magic_len = sizeof(mlxfw_mfa2_comp_magic) - 1;
  109. bool mlxfw_mfa2_check(const struct firmware *fw)
  110. {
  111. if (fw->size < sizeof(mlxfw_mfa2_fingerprint))
  112. return false;
  113. return memcmp(fw->data, mlxfw_mfa2_fingerprint,
  114. mlxfw_mfa2_fingerprint_len) == 0;
  115. }
  116. static bool
  117. mlxfw_mfa2_tlv_multi_validate(const struct mlxfw_mfa2_file *mfa2_file,
  118. const struct mlxfw_mfa2_tlv_multi *multi)
  119. {
  120. const struct mlxfw_mfa2_tlv *tlv;
  121. u16 idx;
  122. /* Check that all children are valid */
  123. mlxfw_mfa2_tlv_multi_foreach(mfa2_file, tlv, idx, multi) {
  124. if (!tlv) {
  125. pr_err("Multi has invalid child");
  126. return false;
  127. }
  128. }
  129. return true;
  130. }
  131. static bool
  132. mlxfw_mfa2_file_dev_validate(const struct mlxfw_mfa2_file *mfa2_file,
  133. const struct mlxfw_mfa2_tlv *dev_tlv,
  134. u16 dev_idx)
  135. {
  136. const struct mlxfw_mfa2_tlv_component_ptr *cptr;
  137. const struct mlxfw_mfa2_tlv_multi *multi;
  138. const struct mlxfw_mfa2_tlv_psid *psid;
  139. const struct mlxfw_mfa2_tlv *tlv;
  140. u16 cptr_count;
  141. u16 cptr_idx;
  142. int err;
  143. pr_debug("Device %d\n", dev_idx);
  144. multi = mlxfw_mfa2_tlv_multi_get(mfa2_file, dev_tlv);
  145. if (!multi) {
  146. pr_err("Device %d is not a valid TLV error\n", dev_idx);
  147. return false;
  148. }
  149. if (!mlxfw_mfa2_tlv_multi_validate(mfa2_file, multi))
  150. return false;
  151. /* Validate the device has PSID tlv */
  152. tlv = mlxfw_mfa2_tlv_multi_child_find(mfa2_file, multi,
  153. MLXFW_MFA2_TLV_PSID, 0);
  154. if (!tlv) {
  155. pr_err("Device %d does not have PSID\n", dev_idx);
  156. return false;
  157. }
  158. psid = mlxfw_mfa2_tlv_psid_get(mfa2_file, tlv);
  159. if (!psid) {
  160. pr_err("Device %d PSID TLV is not valid\n", dev_idx);
  161. return false;
  162. }
  163. print_hex_dump_debug(" -- Device PSID ", DUMP_PREFIX_NONE, 16, 16,
  164. psid->psid, be16_to_cpu(tlv->len), true);
  165. /* Validate the device has COMPONENT_PTR */
  166. err = mlxfw_mfa2_tlv_multi_child_count(mfa2_file, multi,
  167. MLXFW_MFA2_TLV_COMPONENT_PTR,
  168. &cptr_count);
  169. if (err)
  170. return false;
  171. if (cptr_count == 0) {
  172. pr_err("Device %d has no components\n", dev_idx);
  173. return false;
  174. }
  175. for (cptr_idx = 0; cptr_idx < cptr_count; cptr_idx++) {
  176. tlv = mlxfw_mfa2_tlv_multi_child_find(mfa2_file, multi,
  177. MLXFW_MFA2_TLV_COMPONENT_PTR,
  178. cptr_idx);
  179. if (!tlv)
  180. return false;
  181. cptr = mlxfw_mfa2_tlv_component_ptr_get(mfa2_file, tlv);
  182. if (!cptr) {
  183. pr_err("Device %d COMPONENT_PTR TLV is not valid\n",
  184. dev_idx);
  185. return false;
  186. }
  187. pr_debug(" -- Component index %d\n",
  188. be16_to_cpu(cptr->component_index));
  189. }
  190. return true;
  191. }
  192. static bool
  193. mlxfw_mfa2_file_comp_validate(const struct mlxfw_mfa2_file *mfa2_file,
  194. const struct mlxfw_mfa2_tlv *comp_tlv,
  195. u16 comp_idx)
  196. {
  197. const struct mlxfw_mfa2_tlv_component_descriptor *cdesc;
  198. const struct mlxfw_mfa2_tlv_multi *multi;
  199. const struct mlxfw_mfa2_tlv *tlv;
  200. pr_debug("Component %d\n", comp_idx);
  201. multi = mlxfw_mfa2_tlv_multi_get(mfa2_file, comp_tlv);
  202. if (!multi) {
  203. pr_err("Component %d is not a valid TLV error\n", comp_idx);
  204. return false;
  205. }
  206. if (!mlxfw_mfa2_tlv_multi_validate(mfa2_file, multi))
  207. return false;
  208. /* Check that component have COMPONENT_DESCRIPTOR as first child */
  209. tlv = mlxfw_mfa2_tlv_multi_child(mfa2_file, multi);
  210. if (!tlv) {
  211. pr_err("Component descriptor %d multi TLV error\n", comp_idx);
  212. return false;
  213. }
  214. cdesc = mlxfw_mfa2_tlv_component_descriptor_get(mfa2_file, tlv);
  215. if (!cdesc) {
  216. pr_err("Component %d does not have a valid descriptor\n",
  217. comp_idx);
  218. return false;
  219. }
  220. pr_debug(" -- Component type %d\n", be16_to_cpu(cdesc->identifier));
  221. pr_debug(" -- Offset 0x%llx and size %d\n",
  222. ((u64) be32_to_cpu(cdesc->cb_offset_h) << 32)
  223. | be32_to_cpu(cdesc->cb_offset_l), be32_to_cpu(cdesc->size));
  224. return true;
  225. }
  226. static bool mlxfw_mfa2_file_validate(const struct mlxfw_mfa2_file *mfa2_file)
  227. {
  228. const struct mlxfw_mfa2_tlv *tlv;
  229. u16 idx;
  230. 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) {
  234. if (!tlv) {
  235. pr_err("Device TLV error\n");
  236. return false;
  237. }
  238. /* Check each device */
  239. if (!mlxfw_mfa2_file_dev_validate(mfa2_file, tlv, idx))
  240. return false;
  241. }
  242. /* check that all the components exist */
  243. mlxfw_mfa2_tlv_foreach(mfa2_file, tlv, idx, mfa2_file->first_component,
  244. mfa2_file->component_count) {
  245. if (!tlv) {
  246. pr_err("Device TLV error\n");
  247. return false;
  248. }
  249. /* Check each component */
  250. if (!mlxfw_mfa2_file_comp_validate(mfa2_file, tlv, idx))
  251. return false;
  252. }
  253. return true;
  254. }
  255. struct mlxfw_mfa2_file *mlxfw_mfa2_file_init(const struct firmware *fw)
  256. {
  257. const struct mlxfw_mfa2_tlv_package_descriptor *pd;
  258. const struct mlxfw_mfa2_tlv_multi *multi;
  259. const struct mlxfw_mfa2_tlv *multi_child;
  260. const struct mlxfw_mfa2_tlv *first_tlv;
  261. struct mlxfw_mfa2_file *mfa2_file;
  262. const void *first_tlv_ptr;
  263. const void *cb_top_ptr;
  264. mfa2_file = kcalloc(1, sizeof(*mfa2_file), GFP_KERNEL);
  265. if (!mfa2_file)
  266. return ERR_PTR(-ENOMEM);
  267. mfa2_file->fw = fw;
  268. first_tlv_ptr = fw->data + NLA_ALIGN(mlxfw_mfa2_fingerprint_len);
  269. first_tlv = mlxfw_mfa2_tlv_get(mfa2_file, first_tlv_ptr);
  270. if (!first_tlv) {
  271. pr_err("Could not parse package descriptor TLV\n");
  272. goto err_out;
  273. }
  274. multi = mlxfw_mfa2_tlv_multi_get(mfa2_file, first_tlv);
  275. if (!multi) {
  276. pr_err("First TLV is not of valid multi type\n");
  277. goto err_out;
  278. }
  279. multi_child = mlxfw_mfa2_tlv_multi_child(mfa2_file, multi);
  280. if (!multi_child)
  281. goto err_out;
  282. pd = mlxfw_mfa2_tlv_package_descriptor_get(mfa2_file, multi_child);
  283. if (!pd) {
  284. pr_err("Could not parse package descriptor TLV\n");
  285. goto err_out;
  286. }
  287. mfa2_file->first_dev = mlxfw_mfa2_tlv_next(mfa2_file, first_tlv);
  288. if (!mfa2_file->first_dev) {
  289. pr_err("First device TLV is not valid\n");
  290. goto err_out;
  291. }
  292. mfa2_file->dev_count = be16_to_cpu(pd->num_devices);
  293. mfa2_file->first_component = mlxfw_mfa2_tlv_advance(mfa2_file,
  294. mfa2_file->first_dev,
  295. mfa2_file->dev_count);
  296. mfa2_file->component_count = be16_to_cpu(pd->num_components);
  297. mfa2_file->cb = fw->data + NLA_ALIGN(be32_to_cpu(pd->cb_offset));
  298. if (!mlxfw_mfa2_valid_ptr(mfa2_file, mfa2_file->cb)) {
  299. pr_err("Component block is out side the file\n");
  300. goto err_out;
  301. }
  302. mfa2_file->cb_archive_size = be32_to_cpu(pd->cb_archive_size);
  303. cb_top_ptr = mfa2_file->cb + mfa2_file->cb_archive_size - 1;
  304. if (!mlxfw_mfa2_valid_ptr(mfa2_file, cb_top_ptr)) {
  305. pr_err("Component block size is too big\n");
  306. goto err_out;
  307. }
  308. if (!mlxfw_mfa2_file_validate(mfa2_file))
  309. goto err_out;
  310. return mfa2_file;
  311. err_out:
  312. kfree(mfa2_file);
  313. return ERR_PTR(-EINVAL);
  314. }
  315. static const struct mlxfw_mfa2_tlv_multi *
  316. mlxfw_mfa2_tlv_dev_get(const struct mlxfw_mfa2_file *mfa2_file,
  317. const char *psid, u16 psid_size)
  318. {
  319. const struct mlxfw_mfa2_tlv_psid *tlv_psid;
  320. const struct mlxfw_mfa2_tlv_multi *dev_multi;
  321. const struct mlxfw_mfa2_tlv *dev_tlv;
  322. const struct mlxfw_mfa2_tlv *tlv;
  323. u32 idx;
  324. /* for each device tlv */
  325. mlxfw_mfa2_tlv_foreach(mfa2_file, dev_tlv, idx, mfa2_file->first_dev,
  326. mfa2_file->dev_count) {
  327. if (!dev_tlv)
  328. return NULL;
  329. dev_multi = mlxfw_mfa2_tlv_multi_get(mfa2_file, dev_tlv);
  330. if (!dev_multi)
  331. return NULL;
  332. /* find psid child and compare */
  333. tlv = mlxfw_mfa2_tlv_multi_child_find(mfa2_file, dev_multi,
  334. MLXFW_MFA2_TLV_PSID, 0);
  335. if (!tlv)
  336. return NULL;
  337. if (be16_to_cpu(tlv->len) != psid_size)
  338. continue;
  339. tlv_psid = mlxfw_mfa2_tlv_psid_get(mfa2_file, tlv);
  340. if (!tlv_psid)
  341. return NULL;
  342. if (memcmp(psid, tlv_psid->psid, psid_size) == 0)
  343. return dev_multi;
  344. }
  345. return NULL;
  346. }
  347. int mlxfw_mfa2_file_component_count(const struct mlxfw_mfa2_file *mfa2_file,
  348. const char *psid, u32 psid_size,
  349. u32 *p_count)
  350. {
  351. const struct mlxfw_mfa2_tlv_multi *dev_multi;
  352. u16 count;
  353. int err;
  354. dev_multi = mlxfw_mfa2_tlv_dev_get(mfa2_file, psid, psid_size);
  355. if (!dev_multi)
  356. return -EINVAL;
  357. err = mlxfw_mfa2_tlv_multi_child_count(mfa2_file, dev_multi,
  358. MLXFW_MFA2_TLV_COMPONENT_PTR,
  359. &count);
  360. if (err)
  361. return err;
  362. *p_count = count;
  363. return 0;
  364. }
  365. static int mlxfw_mfa2_xz_dec_run(struct xz_dec *xz_dec, struct xz_buf *xz_buf,
  366. bool *finished)
  367. {
  368. enum xz_ret xz_ret;
  369. xz_ret = xz_dec_run(xz_dec, xz_buf);
  370. switch (xz_ret) {
  371. case XZ_STREAM_END:
  372. *finished = true;
  373. return 0;
  374. case XZ_OK:
  375. *finished = false;
  376. return 0;
  377. case XZ_MEM_ERROR:
  378. pr_err("xz no memory\n");
  379. return -ENOMEM;
  380. case XZ_DATA_ERROR:
  381. pr_err("xz file corrupted\n");
  382. return -EINVAL;
  383. case XZ_FORMAT_ERROR:
  384. pr_err("xz format not found\n");
  385. return -EINVAL;
  386. case XZ_OPTIONS_ERROR:
  387. pr_err("unsupported xz option\n");
  388. return -EINVAL;
  389. case XZ_MEMLIMIT_ERROR:
  390. pr_err("xz dictionary too small\n");
  391. return -EINVAL;
  392. default:
  393. pr_err("xz error %d\n", xz_ret);
  394. return -EINVAL;
  395. }
  396. }
  397. static int mlxfw_mfa2_file_cb_offset_xz(const struct mlxfw_mfa2_file *mfa2_file,
  398. off_t off, size_t size, u8 *buf)
  399. {
  400. struct xz_dec *xz_dec;
  401. struct xz_buf dec_buf;
  402. off_t curr_off = 0;
  403. bool finished;
  404. int err;
  405. xz_dec = xz_dec_init(XZ_DYNALLOC, (u32) -1);
  406. if (!xz_dec)
  407. return -EINVAL;
  408. dec_buf.in_size = mfa2_file->cb_archive_size;
  409. dec_buf.in = mfa2_file->cb;
  410. dec_buf.in_pos = 0;
  411. dec_buf.out = buf;
  412. /* decode up to the offset */
  413. do {
  414. dec_buf.out_pos = 0;
  415. dec_buf.out_size = min_t(size_t, size, off - curr_off);
  416. if (dec_buf.out_size == 0)
  417. break;
  418. err = mlxfw_mfa2_xz_dec_run(xz_dec, &dec_buf, &finished);
  419. if (err)
  420. goto out;
  421. if (finished) {
  422. pr_err("xz section too short\n");
  423. err = -EINVAL;
  424. goto out;
  425. }
  426. curr_off += dec_buf.out_pos;
  427. } while (curr_off != off);
  428. /* decode the needed section */
  429. dec_buf.out_pos = 0;
  430. dec_buf.out_size = size;
  431. err = mlxfw_mfa2_xz_dec_run(xz_dec, &dec_buf, &finished);
  432. out:
  433. xz_dec_end(xz_dec);
  434. return err;
  435. }
  436. static const struct mlxfw_mfa2_tlv_component_descriptor *
  437. mlxfw_mfa2_file_component_tlv_get(const struct mlxfw_mfa2_file *mfa2_file,
  438. u16 comp_index)
  439. {
  440. const struct mlxfw_mfa2_tlv_multi *multi;
  441. const struct mlxfw_mfa2_tlv *multi_child;
  442. const struct mlxfw_mfa2_tlv *comp_tlv;
  443. if (comp_index > mfa2_file->component_count)
  444. return NULL;
  445. comp_tlv = mlxfw_mfa2_tlv_advance(mfa2_file, mfa2_file->first_component,
  446. comp_index);
  447. if (!comp_tlv)
  448. return NULL;
  449. multi = mlxfw_mfa2_tlv_multi_get(mfa2_file, comp_tlv);
  450. if (!multi)
  451. return NULL;
  452. multi_child = mlxfw_mfa2_tlv_multi_child(mfa2_file, multi);
  453. if (!multi_child)
  454. return NULL;
  455. return mlxfw_mfa2_tlv_component_descriptor_get(mfa2_file, multi_child);
  456. }
  457. struct mlxfw_mfa2_comp_data {
  458. struct mlxfw_mfa2_component comp;
  459. u8 buff[0];
  460. };
  461. static const struct mlxfw_mfa2_tlv_component_descriptor *
  462. mlxfw_mfa2_file_component_find(const struct mlxfw_mfa2_file *mfa2_file,
  463. const char *psid, int psid_size,
  464. int component_index)
  465. {
  466. const struct mlxfw_mfa2_tlv_component_ptr *cptr;
  467. const struct mlxfw_mfa2_tlv_multi *dev_multi;
  468. const struct mlxfw_mfa2_tlv *cptr_tlv;
  469. u16 comp_idx;
  470. dev_multi = mlxfw_mfa2_tlv_dev_get(mfa2_file, psid, psid_size);
  471. if (!dev_multi)
  472. return NULL;
  473. cptr_tlv = mlxfw_mfa2_tlv_multi_child_find(mfa2_file, dev_multi,
  474. MLXFW_MFA2_TLV_COMPONENT_PTR,
  475. component_index);
  476. if (!cptr_tlv)
  477. return NULL;
  478. cptr = mlxfw_mfa2_tlv_component_ptr_get(mfa2_file, cptr_tlv);
  479. if (!cptr)
  480. return NULL;
  481. comp_idx = be16_to_cpu(cptr->component_index);
  482. return mlxfw_mfa2_file_component_tlv_get(mfa2_file, comp_idx);
  483. }
  484. struct mlxfw_mfa2_component *
  485. mlxfw_mfa2_file_component_get(const struct mlxfw_mfa2_file *mfa2_file,
  486. const char *psid, int psid_size,
  487. int component_index)
  488. {
  489. const struct mlxfw_mfa2_tlv_component_descriptor *comp;
  490. struct mlxfw_mfa2_comp_data *comp_data;
  491. u32 comp_buf_size;
  492. off_t cb_offset;
  493. u32 comp_size;
  494. int err;
  495. comp = mlxfw_mfa2_file_component_find(mfa2_file, psid, psid_size,
  496. component_index);
  497. if (!comp)
  498. return ERR_PTR(-EINVAL);
  499. cb_offset = (u64) be32_to_cpu(comp->cb_offset_h) << 32 |
  500. be32_to_cpu(comp->cb_offset_l);
  501. comp_size = be32_to_cpu(comp->size);
  502. comp_buf_size = comp_size + mlxfw_mfa2_comp_magic_len;
  503. comp_data = kmalloc(sizeof(*comp_data) + comp_buf_size, GFP_KERNEL);
  504. if (!comp_data)
  505. return ERR_PTR(-ENOMEM);
  506. comp_data->comp.data_size = comp_size;
  507. comp_data->comp.index = be16_to_cpu(comp->identifier);
  508. err = mlxfw_mfa2_file_cb_offset_xz(mfa2_file, cb_offset, comp_buf_size,
  509. comp_data->buff);
  510. if (err) {
  511. pr_err("Component could not be reached in CB\n");
  512. goto err_out;
  513. }
  514. if (memcmp(comp_data->buff, mlxfw_mfa2_comp_magic,
  515. mlxfw_mfa2_comp_magic_len) != 0) {
  516. pr_err("Component has wrong magic\n");
  517. err = -EINVAL;
  518. goto err_out;
  519. }
  520. comp_data->comp.data = comp_data->buff + mlxfw_mfa2_comp_magic_len;
  521. return &comp_data->comp;
  522. err_out:
  523. kfree(comp_data);
  524. return ERR_PTR(err);
  525. }
  526. void mlxfw_mfa2_file_component_put(struct mlxfw_mfa2_component *comp)
  527. {
  528. const struct mlxfw_mfa2_comp_data *comp_data;
  529. comp_data = container_of(comp, struct mlxfw_mfa2_comp_data, comp);
  530. kfree(comp_data);
  531. }
  532. void mlxfw_mfa2_file_fini(struct mlxfw_mfa2_file *mfa2_file)
  533. {
  534. kfree(mfa2_file);
  535. }