mlxfw_mfa2.c 18 KB

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