qmi_encdec.c 27 KB

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