qmi_encdec.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2012-2015, The Linux Foundation. All rights reserved.
  4. * Copyright (C) 2017 Linaro Ltd.
  5. */
  6. #include <linux/slab.h>
  7. #include <linux/uaccess.h>
  8. #include <linux/module.h>
  9. #include <linux/kernel.h>
  10. #include <linux/errno.h>
  11. #include <linux/string.h>
  12. #include <linux/soc/qcom/qmi.h>
  13. #define QMI_ENCDEC_ENCODE_TLV(type, length, p_dst) do { \
  14. *p_dst++ = type; \
  15. *p_dst++ = ((u8)((length) & 0xFF)); \
  16. *p_dst++ = ((u8)(((length) >> 8) & 0xFF)); \
  17. } while (0)
  18. #define QMI_ENCDEC_DECODE_TLV(p_type, p_length, p_src) do { \
  19. *p_type = (u8)*p_src++; \
  20. *p_length = (u8)*p_src++; \
  21. *p_length |= ((u8)*p_src) << 8; \
  22. } while (0)
  23. #define QMI_ENCDEC_ENCODE_N_BYTES(p_dst, p_src, size) \
  24. do { \
  25. memcpy(p_dst, p_src, size); \
  26. p_dst = (u8 *)p_dst + size; \
  27. p_src = (u8 *)p_src + size; \
  28. } while (0)
  29. #define QMI_ENCDEC_DECODE_N_BYTES(p_dst, p_src, size) \
  30. do { \
  31. memcpy(p_dst, p_src, size); \
  32. p_dst = (u8 *)p_dst + size; \
  33. p_src = (u8 *)p_src + size; \
  34. } while (0)
  35. #define UPDATE_ENCODE_VARIABLES(temp_si, buf_dst, \
  36. encoded_bytes, tlv_len, encode_tlv, rc) \
  37. do { \
  38. buf_dst = (u8 *)buf_dst + rc; \
  39. encoded_bytes += rc; \
  40. tlv_len += rc; \
  41. temp_si = temp_si + 1; \
  42. encode_tlv = 1; \
  43. } while (0)
  44. #define UPDATE_DECODE_VARIABLES(buf_src, decoded_bytes, rc) \
  45. do { \
  46. buf_src = (u8 *)buf_src + rc; \
  47. decoded_bytes += rc; \
  48. } while (0)
  49. #define TLV_LEN_SIZE sizeof(u16)
  50. #define TLV_TYPE_SIZE sizeof(u8)
  51. #define OPTIONAL_TLV_TYPE_START 0x10
  52. static int qmi_encode(const struct qmi_elem_info *ei_array, void *out_buf,
  53. const void *in_c_struct, u32 out_buf_len,
  54. int enc_level);
  55. static int qmi_decode(const struct qmi_elem_info *ei_array, void *out_c_struct,
  56. const void *in_buf, u32 in_buf_len, int dec_level);
  57. /**
  58. * skip_to_next_elem() - Skip to next element in the structure to be encoded
  59. * @ei_array: Struct info describing the element to be skipped.
  60. * @level: Depth level of encoding/decoding to identify nested structures.
  61. *
  62. * This function is used while encoding optional elements. If the flag
  63. * corresponding to an optional element is not set, then encoding the
  64. * optional element can be skipped. This function can be used to perform
  65. * that operation.
  66. *
  67. * Return: struct info of the next element that can be encoded.
  68. */
  69. static const struct qmi_elem_info *
  70. skip_to_next_elem(const struct qmi_elem_info *ei_array, int level)
  71. {
  72. const struct qmi_elem_info *temp_ei = ei_array;
  73. u8 tlv_type;
  74. if (level > 1) {
  75. temp_ei = temp_ei + 1;
  76. } else {
  77. do {
  78. tlv_type = temp_ei->tlv_type;
  79. temp_ei = temp_ei + 1;
  80. } while (tlv_type == temp_ei->tlv_type);
  81. }
  82. return temp_ei;
  83. }
  84. /**
  85. * qmi_calc_min_msg_len() - Calculate the minimum length of a QMI message
  86. * @ei_array: Struct info array describing the structure.
  87. * @level: Level to identify the depth of the nested structures.
  88. *
  89. * Return: Expected minimum length of the QMI message or 0 on error.
  90. */
  91. static int qmi_calc_min_msg_len(const struct qmi_elem_info *ei_array,
  92. int level)
  93. {
  94. int min_msg_len = 0;
  95. const struct qmi_elem_info *temp_ei = ei_array;
  96. if (!ei_array)
  97. return min_msg_len;
  98. while (temp_ei->data_type != QMI_EOTI) {
  99. /* Optional elements do not count in minimum length */
  100. if (temp_ei->data_type == QMI_OPT_FLAG) {
  101. temp_ei = skip_to_next_elem(temp_ei, level);
  102. continue;
  103. }
  104. if (temp_ei->data_type == QMI_DATA_LEN) {
  105. min_msg_len += (temp_ei->elem_size == sizeof(u8) ?
  106. sizeof(u8) : sizeof(u16));
  107. temp_ei++;
  108. continue;
  109. } else if (temp_ei->data_type == QMI_STRUCT) {
  110. min_msg_len += qmi_calc_min_msg_len(temp_ei->ei_array,
  111. (level + 1));
  112. temp_ei++;
  113. } else if (temp_ei->data_type == QMI_STRING) {
  114. if (level > 1)
  115. min_msg_len += temp_ei->elem_len <= U8_MAX ?
  116. sizeof(u8) : sizeof(u16);
  117. min_msg_len += temp_ei->elem_len * temp_ei->elem_size;
  118. temp_ei++;
  119. } else {
  120. min_msg_len += (temp_ei->elem_len * temp_ei->elem_size);
  121. temp_ei++;
  122. }
  123. /*
  124. * Type & Length info. not prepended for elements in the
  125. * nested structure.
  126. */
  127. if (level == 1)
  128. min_msg_len += (TLV_TYPE_SIZE + TLV_LEN_SIZE);
  129. }
  130. return min_msg_len;
  131. }
  132. /**
  133. * qmi_encode_basic_elem() - Encodes elements of basic/primary data type
  134. * @buf_dst: Buffer to store the encoded information.
  135. * @buf_src: Buffer containing the elements to be encoded.
  136. * @elem_len: Number of elements, in the buf_src, to be encoded.
  137. * @elem_size: Size of a single instance of the element to be encoded.
  138. *
  139. * This function encodes the "elem_len" number of data elements, each of
  140. * size "elem_size" bytes from the source buffer "buf_src" and stores the
  141. * encoded information in the destination buffer "buf_dst". The elements are
  142. * of primary data type which include u8 - u64 or similar. This
  143. * function returns the number of bytes of encoded information.
  144. *
  145. * Return: The number of bytes of encoded information.
  146. */
  147. static int qmi_encode_basic_elem(void *buf_dst, const void *buf_src,
  148. u32 elem_len, u32 elem_size)
  149. {
  150. u32 i, rc = 0;
  151. for (i = 0; i < elem_len; i++) {
  152. QMI_ENCDEC_ENCODE_N_BYTES(buf_dst, buf_src, elem_size);
  153. rc += elem_size;
  154. }
  155. return rc;
  156. }
  157. /**
  158. * qmi_encode_struct_elem() - Encodes elements of struct data type
  159. * @ei_array: Struct info array descibing the struct element.
  160. * @buf_dst: Buffer to store the encoded information.
  161. * @buf_src: Buffer containing the elements to be encoded.
  162. * @elem_len: Number of elements, in the buf_src, to be encoded.
  163. * @out_buf_len: Available space in the encode buffer.
  164. * @enc_level: Depth of the nested structure from the main structure.
  165. *
  166. * This function encodes the "elem_len" number of struct elements, each of
  167. * size "ei_array->elem_size" bytes from the source buffer "buf_src" and
  168. * stores the encoded information in the destination buffer "buf_dst". The
  169. * elements are of struct data type which includes any C structure. This
  170. * function returns the number of bytes of encoded information.
  171. *
  172. * Return: The number of bytes of encoded information on success or negative
  173. * errno on error.
  174. */
  175. static int qmi_encode_struct_elem(const struct qmi_elem_info *ei_array,
  176. void *buf_dst, const void *buf_src,
  177. u32 elem_len, u32 out_buf_len,
  178. int enc_level)
  179. {
  180. int i, rc, encoded_bytes = 0;
  181. const struct qmi_elem_info *temp_ei = ei_array;
  182. for (i = 0; i < elem_len; i++) {
  183. rc = qmi_encode(temp_ei->ei_array, buf_dst, buf_src,
  184. out_buf_len - encoded_bytes, enc_level);
  185. if (rc < 0) {
  186. pr_err("%s: STRUCT Encode failure\n", __func__);
  187. return rc;
  188. }
  189. buf_dst = buf_dst + rc;
  190. buf_src = buf_src + temp_ei->elem_size;
  191. encoded_bytes += rc;
  192. }
  193. return encoded_bytes;
  194. }
  195. /**
  196. * qmi_encode_string_elem() - Encodes elements of string data type
  197. * @ei_array: Struct info array descibing the string element.
  198. * @buf_dst: Buffer to store the encoded information.
  199. * @buf_src: Buffer containing the elements to be encoded.
  200. * @out_buf_len: Available space in the encode buffer.
  201. * @enc_level: Depth of the string element from the main structure.
  202. *
  203. * This function encodes a string element of maximum length "ei_array->elem_len"
  204. * bytes from the source buffer "buf_src" and stores the encoded information in
  205. * the destination buffer "buf_dst". This function returns the number of bytes
  206. * of encoded information.
  207. *
  208. * Return: The number of bytes of encoded information on success or negative
  209. * errno on error.
  210. */
  211. static int qmi_encode_string_elem(const struct qmi_elem_info *ei_array,
  212. void *buf_dst, const void *buf_src,
  213. u32 out_buf_len, int enc_level)
  214. {
  215. int rc;
  216. int encoded_bytes = 0;
  217. const struct qmi_elem_info *temp_ei = ei_array;
  218. u32 string_len = 0;
  219. u32 string_len_sz = 0;
  220. string_len = strlen(buf_src);
  221. string_len_sz = temp_ei->elem_len <= U8_MAX ?
  222. sizeof(u8) : sizeof(u16);
  223. if (string_len > temp_ei->elem_len) {
  224. pr_err("%s: String to be encoded is longer - %d > %d\n",
  225. __func__, string_len, temp_ei->elem_len);
  226. return -EINVAL;
  227. }
  228. if (enc_level == 1) {
  229. if (string_len + TLV_LEN_SIZE + TLV_TYPE_SIZE >
  230. out_buf_len) {
  231. pr_err("%s: Output len %d > Out Buf len %d\n",
  232. __func__, string_len, out_buf_len);
  233. return -ETOOSMALL;
  234. }
  235. } else {
  236. if (string_len + string_len_sz > out_buf_len) {
  237. pr_err("%s: Output len %d > Out Buf len %d\n",
  238. __func__, string_len, out_buf_len);
  239. return -ETOOSMALL;
  240. }
  241. rc = qmi_encode_basic_elem(buf_dst, &string_len,
  242. 1, string_len_sz);
  243. encoded_bytes += rc;
  244. }
  245. rc = qmi_encode_basic_elem(buf_dst + encoded_bytes, buf_src,
  246. string_len, temp_ei->elem_size);
  247. encoded_bytes += rc;
  248. return encoded_bytes;
  249. }
  250. /**
  251. * qmi_encode() - Core Encode Function
  252. * @ei_array: Struct info array describing the structure to be encoded.
  253. * @out_buf: Buffer to hold the encoded QMI message.
  254. * @in_c_struct: Pointer to the C structure to be encoded.
  255. * @out_buf_len: Available space in the encode buffer.
  256. * @enc_level: Encode level to indicate the depth of the nested structure,
  257. * within the main structure, being encoded.
  258. *
  259. * Return: The number of bytes of encoded information on success or negative
  260. * errno on error.
  261. */
  262. static int qmi_encode(const struct qmi_elem_info *ei_array, void *out_buf,
  263. const void *in_c_struct, u32 out_buf_len,
  264. int enc_level)
  265. {
  266. const struct qmi_elem_info *temp_ei = ei_array;
  267. u8 opt_flag_value = 0;
  268. u32 data_len_value = 0, data_len_sz;
  269. u8 *buf_dst = (u8 *)out_buf;
  270. u8 *tlv_pointer;
  271. u32 tlv_len;
  272. u8 tlv_type;
  273. u32 encoded_bytes = 0;
  274. const void *buf_src;
  275. int encode_tlv = 0;
  276. int rc;
  277. u8 val8;
  278. u16 val16;
  279. if (!ei_array)
  280. return 0;
  281. tlv_pointer = buf_dst;
  282. tlv_len = 0;
  283. if (enc_level == 1)
  284. buf_dst = buf_dst + (TLV_LEN_SIZE + TLV_TYPE_SIZE);
  285. while (temp_ei->data_type != QMI_EOTI) {
  286. buf_src = in_c_struct + temp_ei->offset;
  287. tlv_type = temp_ei->tlv_type;
  288. if (temp_ei->array_type == NO_ARRAY) {
  289. data_len_value = 1;
  290. } else if (temp_ei->array_type == STATIC_ARRAY) {
  291. data_len_value = temp_ei->elem_len;
  292. } else if (data_len_value <= 0 ||
  293. temp_ei->elem_len < data_len_value) {
  294. pr_err("%s: Invalid data length\n", __func__);
  295. return -EINVAL;
  296. }
  297. switch (temp_ei->data_type) {
  298. case QMI_OPT_FLAG:
  299. rc = qmi_encode_basic_elem(&opt_flag_value, buf_src,
  300. 1, sizeof(u8));
  301. if (opt_flag_value)
  302. temp_ei = temp_ei + 1;
  303. else
  304. temp_ei = skip_to_next_elem(temp_ei, enc_level);
  305. break;
  306. case QMI_DATA_LEN:
  307. data_len_sz = temp_ei->elem_size == sizeof(u8) ?
  308. sizeof(u8) : sizeof(u16);
  309. /* Check to avoid out of range buffer access */
  310. if ((data_len_sz + encoded_bytes + TLV_LEN_SIZE +
  311. TLV_TYPE_SIZE) > out_buf_len) {
  312. pr_err("%s: Too Small Buffer @DATA_LEN\n",
  313. __func__);
  314. return -ETOOSMALL;
  315. }
  316. if (data_len_sz == sizeof(u8)) {
  317. val8 = *(u8 *)buf_src;
  318. data_len_value = (u32)val8;
  319. rc = qmi_encode_basic_elem(buf_dst, &val8,
  320. 1, data_len_sz);
  321. } else {
  322. val16 = *(u16 *)buf_src;
  323. data_len_value = (u32)le16_to_cpu(val16);
  324. rc = qmi_encode_basic_elem(buf_dst, &val16,
  325. 1, data_len_sz);
  326. }
  327. UPDATE_ENCODE_VARIABLES(temp_ei, buf_dst,
  328. encoded_bytes, tlv_len,
  329. encode_tlv, rc);
  330. if (!data_len_value)
  331. temp_ei = skip_to_next_elem(temp_ei, enc_level);
  332. else
  333. encode_tlv = 0;
  334. break;
  335. case QMI_UNSIGNED_1_BYTE:
  336. case QMI_UNSIGNED_2_BYTE:
  337. case QMI_UNSIGNED_4_BYTE:
  338. case QMI_UNSIGNED_8_BYTE:
  339. case QMI_SIGNED_2_BYTE_ENUM:
  340. case QMI_SIGNED_4_BYTE_ENUM:
  341. /* Check to avoid out of range buffer access */
  342. if (((data_len_value * temp_ei->elem_size) +
  343. encoded_bytes + TLV_LEN_SIZE + TLV_TYPE_SIZE) >
  344. out_buf_len) {
  345. pr_err("%s: Too Small Buffer @data_type:%d\n",
  346. __func__, temp_ei->data_type);
  347. return -ETOOSMALL;
  348. }
  349. rc = qmi_encode_basic_elem(buf_dst, buf_src,
  350. data_len_value,
  351. temp_ei->elem_size);
  352. UPDATE_ENCODE_VARIABLES(temp_ei, buf_dst,
  353. encoded_bytes, tlv_len,
  354. encode_tlv, rc);
  355. break;
  356. case QMI_STRUCT:
  357. rc = qmi_encode_struct_elem(temp_ei, buf_dst, buf_src,
  358. data_len_value,
  359. out_buf_len - encoded_bytes,
  360. enc_level + 1);
  361. if (rc < 0)
  362. return rc;
  363. UPDATE_ENCODE_VARIABLES(temp_ei, buf_dst,
  364. encoded_bytes, tlv_len,
  365. encode_tlv, rc);
  366. break;
  367. case QMI_STRING:
  368. rc = qmi_encode_string_elem(temp_ei, buf_dst, buf_src,
  369. out_buf_len - encoded_bytes,
  370. enc_level);
  371. if (rc < 0)
  372. return rc;
  373. UPDATE_ENCODE_VARIABLES(temp_ei, buf_dst,
  374. encoded_bytes, tlv_len,
  375. encode_tlv, rc);
  376. break;
  377. default:
  378. pr_err("%s: Unrecognized data type\n", __func__);
  379. return -EINVAL;
  380. }
  381. if (encode_tlv && enc_level == 1) {
  382. QMI_ENCDEC_ENCODE_TLV(tlv_type, tlv_len, tlv_pointer);
  383. encoded_bytes += (TLV_TYPE_SIZE + TLV_LEN_SIZE);
  384. tlv_pointer = buf_dst;
  385. tlv_len = 0;
  386. buf_dst = buf_dst + TLV_LEN_SIZE + TLV_TYPE_SIZE;
  387. encode_tlv = 0;
  388. }
  389. }
  390. return encoded_bytes;
  391. }
  392. /**
  393. * qmi_decode_basic_elem() - Decodes elements of basic/primary data type
  394. * @buf_dst: Buffer to store the decoded element.
  395. * @buf_src: Buffer containing the elements in QMI wire format.
  396. * @elem_len: Number of elements to be decoded.
  397. * @elem_size: Size of a single instance of the element to be decoded.
  398. *
  399. * This function decodes the "elem_len" number of elements in QMI wire format,
  400. * each of size "elem_size" bytes from the source buffer "buf_src" and stores
  401. * the decoded elements in the destination buffer "buf_dst". The elements are
  402. * of primary data type which include u8 - u64 or similar. This
  403. * function returns the number of bytes of decoded information.
  404. *
  405. * Return: The total size of the decoded data elements, in bytes.
  406. */
  407. static int qmi_decode_basic_elem(void *buf_dst, const void *buf_src,
  408. u32 elem_len, u32 elem_size)
  409. {
  410. u32 i, rc = 0;
  411. for (i = 0; i < elem_len; i++) {
  412. QMI_ENCDEC_DECODE_N_BYTES(buf_dst, buf_src, elem_size);
  413. rc += elem_size;
  414. }
  415. return rc;
  416. }
  417. /**
  418. * qmi_decode_struct_elem() - Decodes elements of struct data type
  419. * @ei_array: Struct info array describing the struct element.
  420. * @buf_dst: Buffer to store the decoded element.
  421. * @buf_src: Buffer containing the elements in QMI wire format.
  422. * @elem_len: Number of elements to be decoded.
  423. * @tlv_len: Total size of the encoded information corresponding to
  424. * this struct element.
  425. * @dec_level: Depth of the nested structure from the main structure.
  426. *
  427. * This function decodes the "elem_len" number of elements in QMI wire format,
  428. * each of size "(tlv_len/elem_len)" bytes from the source buffer "buf_src"
  429. * and stores the decoded elements in the destination buffer "buf_dst". The
  430. * elements are of struct data type which includes any C structure. This
  431. * function returns the number of bytes of decoded information.
  432. *
  433. * Return: The total size of the decoded data elements on success, negative
  434. * errno on error.
  435. */
  436. static int qmi_decode_struct_elem(const struct qmi_elem_info *ei_array,
  437. void *buf_dst, const void *buf_src,
  438. u32 elem_len, u32 tlv_len,
  439. int dec_level)
  440. {
  441. int i, rc, decoded_bytes = 0;
  442. const struct qmi_elem_info *temp_ei = ei_array;
  443. for (i = 0; i < elem_len && decoded_bytes < tlv_len; i++) {
  444. rc = qmi_decode(temp_ei->ei_array, buf_dst, buf_src,
  445. tlv_len - decoded_bytes, dec_level);
  446. if (rc < 0)
  447. return rc;
  448. buf_src = buf_src + rc;
  449. buf_dst = buf_dst + temp_ei->elem_size;
  450. decoded_bytes += rc;
  451. }
  452. if ((dec_level <= 2 && decoded_bytes != tlv_len) ||
  453. (dec_level > 2 && (i < elem_len || decoded_bytes > tlv_len))) {
  454. pr_err("%s: Fault in decoding: dl(%d), db(%d), tl(%d), i(%d), el(%d)\n",
  455. __func__, dec_level, decoded_bytes, tlv_len,
  456. i, elem_len);
  457. return -EFAULT;
  458. }
  459. return decoded_bytes;
  460. }
  461. /**
  462. * qmi_decode_string_elem() - Decodes elements of string data type
  463. * @ei_array: Struct info array describing the string element.
  464. * @buf_dst: Buffer to store the decoded element.
  465. * @buf_src: Buffer containing the elements in QMI wire format.
  466. * @tlv_len: Total size of the encoded information corresponding to
  467. * this string element.
  468. * @dec_level: Depth of the string element from the main structure.
  469. *
  470. * This function decodes the string element of maximum length
  471. * "ei_array->elem_len" from the source buffer "buf_src" and puts it into
  472. * the destination buffer "buf_dst". This function returns number of bytes
  473. * decoded from the input buffer.
  474. *
  475. * Return: The total size of the decoded data elements on success, negative
  476. * errno on error.
  477. */
  478. static int qmi_decode_string_elem(const struct qmi_elem_info *ei_array,
  479. void *buf_dst, const void *buf_src,
  480. u32 tlv_len, int dec_level)
  481. {
  482. int rc;
  483. int decoded_bytes = 0;
  484. u32 string_len = 0;
  485. u32 string_len_sz = 0;
  486. const struct qmi_elem_info *temp_ei = ei_array;
  487. u8 val8;
  488. u16 val16;
  489. if (dec_level == 1) {
  490. string_len = tlv_len;
  491. } else {
  492. string_len_sz = temp_ei->elem_len <= U8_MAX ?
  493. sizeof(u8) : sizeof(u16);
  494. if (string_len_sz == sizeof(u8)) {
  495. rc = qmi_decode_basic_elem(&val8, buf_src,
  496. 1, string_len_sz);
  497. string_len = (u32)val8;
  498. } else {
  499. rc = qmi_decode_basic_elem(&val16, buf_src,
  500. 1, string_len_sz);
  501. string_len = (u32)val16;
  502. }
  503. decoded_bytes += rc;
  504. }
  505. if (string_len >= temp_ei->elem_len) {
  506. pr_err("%s: String len %d >= Max Len %d\n",
  507. __func__, string_len, temp_ei->elem_len);
  508. return -ETOOSMALL;
  509. } else if (string_len > tlv_len) {
  510. pr_err("%s: String len %d > Input Buffer Len %d\n",
  511. __func__, string_len, tlv_len);
  512. return -EFAULT;
  513. }
  514. rc = qmi_decode_basic_elem(buf_dst, buf_src + decoded_bytes,
  515. string_len, temp_ei->elem_size);
  516. *((char *)buf_dst + string_len) = '\0';
  517. decoded_bytes += rc;
  518. return decoded_bytes;
  519. }
  520. /**
  521. * find_ei() - Find element info corresponding to TLV Type
  522. * @ei_array: Struct info array of the message being decoded.
  523. * @type: TLV Type of the element being searched.
  524. *
  525. * Every element that got encoded in the QMI message will have a type
  526. * information associated with it. While decoding the QMI message,
  527. * this function is used to find the struct info regarding the element
  528. * that corresponds to the type being decoded.
  529. *
  530. * Return: Pointer to struct info, if found
  531. */
  532. static const struct qmi_elem_info *find_ei(const struct qmi_elem_info *ei_array,
  533. u32 type)
  534. {
  535. const struct qmi_elem_info *temp_ei = ei_array;
  536. while (temp_ei->data_type != QMI_EOTI) {
  537. if (temp_ei->tlv_type == (u8)type)
  538. return temp_ei;
  539. temp_ei = temp_ei + 1;
  540. }
  541. return NULL;
  542. }
  543. /**
  544. * qmi_decode() - Core Decode Function
  545. * @ei_array: Struct info array describing the structure to be decoded.
  546. * @out_c_struct: Buffer to hold the decoded C struct
  547. * @in_buf: Buffer containing the QMI message to be decoded
  548. * @in_buf_len: Length of the QMI message to be decoded
  549. * @dec_level: Decode level to indicate the depth of the nested structure,
  550. * within the main structure, being decoded
  551. *
  552. * Return: The number of bytes of decoded information on success, negative
  553. * errno on error.
  554. */
  555. static int qmi_decode(const struct qmi_elem_info *ei_array, void *out_c_struct,
  556. const void *in_buf, u32 in_buf_len,
  557. int dec_level)
  558. {
  559. const struct qmi_elem_info *temp_ei = ei_array;
  560. u8 opt_flag_value = 1;
  561. u32 data_len_value = 0, data_len_sz = 0;
  562. u8 *buf_dst = out_c_struct;
  563. const u8 *tlv_pointer;
  564. u32 tlv_len = 0;
  565. u32 tlv_type;
  566. u32 decoded_bytes = 0;
  567. const void *buf_src = in_buf;
  568. int rc;
  569. u8 val8;
  570. u16 val16;
  571. u32 val32;
  572. while (decoded_bytes < in_buf_len) {
  573. if (dec_level >= 2 && temp_ei->data_type == QMI_EOTI)
  574. return decoded_bytes;
  575. if (dec_level == 1) {
  576. tlv_pointer = buf_src;
  577. QMI_ENCDEC_DECODE_TLV(&tlv_type,
  578. &tlv_len, tlv_pointer);
  579. buf_src += (TLV_TYPE_SIZE + TLV_LEN_SIZE);
  580. decoded_bytes += (TLV_TYPE_SIZE + TLV_LEN_SIZE);
  581. temp_ei = find_ei(ei_array, tlv_type);
  582. if (!temp_ei && tlv_type < OPTIONAL_TLV_TYPE_START) {
  583. pr_err("%s: Inval element info\n", __func__);
  584. return -EINVAL;
  585. } else if (!temp_ei) {
  586. UPDATE_DECODE_VARIABLES(buf_src,
  587. decoded_bytes, tlv_len);
  588. continue;
  589. }
  590. } else {
  591. /*
  592. * No length information for elements in nested
  593. * structures. So use remaining decodable buffer space.
  594. */
  595. tlv_len = in_buf_len - decoded_bytes;
  596. }
  597. buf_dst = out_c_struct + temp_ei->offset;
  598. if (temp_ei->data_type == QMI_OPT_FLAG) {
  599. memcpy(buf_dst, &opt_flag_value, sizeof(u8));
  600. temp_ei = temp_ei + 1;
  601. buf_dst = out_c_struct + temp_ei->offset;
  602. }
  603. if (temp_ei->data_type == QMI_DATA_LEN) {
  604. data_len_sz = temp_ei->elem_size == sizeof(u8) ?
  605. sizeof(u8) : sizeof(u16);
  606. if (data_len_sz == sizeof(u8)) {
  607. rc = qmi_decode_basic_elem(&val8, buf_src,
  608. 1, data_len_sz);
  609. data_len_value = (u32)val8;
  610. } else {
  611. rc = qmi_decode_basic_elem(&val16, buf_src,
  612. 1, data_len_sz);
  613. data_len_value = (u32)val16;
  614. }
  615. val32 = cpu_to_le32(data_len_value);
  616. memcpy(buf_dst, &val32, sizeof(u32));
  617. temp_ei = temp_ei + 1;
  618. buf_dst = out_c_struct + temp_ei->offset;
  619. tlv_len -= data_len_sz;
  620. UPDATE_DECODE_VARIABLES(buf_src, decoded_bytes, rc);
  621. }
  622. if (temp_ei->array_type == NO_ARRAY) {
  623. data_len_value = 1;
  624. } else if (temp_ei->array_type == STATIC_ARRAY) {
  625. data_len_value = temp_ei->elem_len;
  626. } else if (data_len_value > temp_ei->elem_len) {
  627. pr_err("%s: Data len %d > max spec %d\n",
  628. __func__, data_len_value, temp_ei->elem_len);
  629. return -ETOOSMALL;
  630. }
  631. switch (temp_ei->data_type) {
  632. case QMI_UNSIGNED_1_BYTE:
  633. case QMI_UNSIGNED_2_BYTE:
  634. case QMI_UNSIGNED_4_BYTE:
  635. case QMI_UNSIGNED_8_BYTE:
  636. case QMI_SIGNED_2_BYTE_ENUM:
  637. case QMI_SIGNED_4_BYTE_ENUM:
  638. rc = qmi_decode_basic_elem(buf_dst, buf_src,
  639. data_len_value,
  640. temp_ei->elem_size);
  641. UPDATE_DECODE_VARIABLES(buf_src, decoded_bytes, rc);
  642. break;
  643. case QMI_STRUCT:
  644. rc = qmi_decode_struct_elem(temp_ei, buf_dst, buf_src,
  645. data_len_value, tlv_len,
  646. dec_level + 1);
  647. if (rc < 0)
  648. return rc;
  649. UPDATE_DECODE_VARIABLES(buf_src, decoded_bytes, rc);
  650. break;
  651. case QMI_STRING:
  652. rc = qmi_decode_string_elem(temp_ei, buf_dst, buf_src,
  653. tlv_len, dec_level);
  654. if (rc < 0)
  655. return rc;
  656. UPDATE_DECODE_VARIABLES(buf_src, decoded_bytes, rc);
  657. break;
  658. default:
  659. pr_err("%s: Unrecognized data type\n", __func__);
  660. return -EINVAL;
  661. }
  662. temp_ei = temp_ei + 1;
  663. }
  664. return decoded_bytes;
  665. }
  666. /**
  667. * qmi_encode_message() - Encode C structure as QMI encoded message
  668. * @type: Type of QMI message
  669. * @msg_id: Message ID of the message
  670. * @len: Passed as max length of the message, updated to actual size
  671. * @txn_id: Transaction ID
  672. * @ei: QMI message descriptor
  673. * @c_struct: Reference to structure to encode
  674. *
  675. * Return: Buffer with encoded message, or negative ERR_PTR() on error
  676. */
  677. void *qmi_encode_message(int type, unsigned int msg_id, size_t *len,
  678. unsigned int txn_id, const struct qmi_elem_info *ei,
  679. const void *c_struct)
  680. {
  681. struct qmi_header *hdr;
  682. ssize_t msglen = 0;
  683. void *msg;
  684. int ret;
  685. /* Check the possibility of a zero length QMI message */
  686. if (!c_struct) {
  687. ret = qmi_calc_min_msg_len(ei, 1);
  688. if (ret) {
  689. pr_err("%s: Calc. len %d != 0, but NULL c_struct\n",
  690. __func__, ret);
  691. return ERR_PTR(-EINVAL);
  692. }
  693. }
  694. msg = kzalloc(sizeof(*hdr) + *len, GFP_KERNEL);
  695. if (!msg)
  696. return ERR_PTR(-ENOMEM);
  697. /* Encode message, if we have a message */
  698. if (c_struct) {
  699. msglen = qmi_encode(ei, msg + sizeof(*hdr), c_struct, *len, 1);
  700. if (msglen < 0) {
  701. kfree(msg);
  702. return ERR_PTR(msglen);
  703. }
  704. }
  705. hdr = msg;
  706. hdr->type = type;
  707. hdr->txn_id = txn_id;
  708. hdr->msg_id = msg_id;
  709. hdr->msg_len = msglen;
  710. *len = sizeof(*hdr) + msglen;
  711. return msg;
  712. }
  713. EXPORT_SYMBOL_GPL(qmi_encode_message);
  714. /**
  715. * qmi_decode_message() - Decode QMI encoded message to C structure
  716. * @buf: Buffer with encoded message
  717. * @len: Amount of data in @buf
  718. * @ei: QMI message descriptor
  719. * @c_struct: Reference to structure to decode into
  720. *
  721. * Return: The number of bytes of decoded information on success, negative
  722. * errno on error.
  723. */
  724. int qmi_decode_message(const void *buf, size_t len,
  725. const struct qmi_elem_info *ei, void *c_struct)
  726. {
  727. if (!ei)
  728. return -EINVAL;
  729. if (!c_struct || !buf || !len)
  730. return -EINVAL;
  731. return qmi_decode(ei, c_struct, buf + sizeof(struct qmi_header),
  732. len - sizeof(struct qmi_header), 1);
  733. }
  734. EXPORT_SYMBOL_GPL(qmi_decode_message);
  735. /* Common header in all QMI responses */
  736. const struct qmi_elem_info qmi_response_type_v01_ei[] = {
  737. {
  738. .data_type = QMI_SIGNED_2_BYTE_ENUM,
  739. .elem_len = 1,
  740. .elem_size = sizeof(u16),
  741. .array_type = NO_ARRAY,
  742. .tlv_type = QMI_COMMON_TLV_TYPE,
  743. .offset = offsetof(struct qmi_response_type_v01, result),
  744. .ei_array = NULL,
  745. },
  746. {
  747. .data_type = QMI_SIGNED_2_BYTE_ENUM,
  748. .elem_len = 1,
  749. .elem_size = sizeof(u16),
  750. .array_type = NO_ARRAY,
  751. .tlv_type = QMI_COMMON_TLV_TYPE,
  752. .offset = offsetof(struct qmi_response_type_v01, error),
  753. .ei_array = NULL,
  754. },
  755. {
  756. .data_type = QMI_EOTI,
  757. .elem_len = 0,
  758. .elem_size = 0,
  759. .array_type = NO_ARRAY,
  760. .tlv_type = QMI_COMMON_TLV_TYPE,
  761. .offset = 0,
  762. .ei_array = NULL,
  763. },
  764. };
  765. EXPORT_SYMBOL_GPL(qmi_response_type_v01_ei);
  766. MODULE_DESCRIPTION("QMI encoder/decoder helper");
  767. MODULE_LICENSE("GPL v2");