packing.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
  2. /* Copyright 2016-2018 NXP
  3. * Copyright (c) 2018-2019, Vladimir Oltean <olteanv@gmail.com>
  4. */
  5. #include <linux/packing.h>
  6. #include <linux/module.h>
  7. #include <linux/bitops.h>
  8. #include <linux/bits.h>
  9. #include <linux/errno.h>
  10. #include <linux/types.h>
  11. #include <linux/bitrev.h>
  12. #define __pack_fields(pbuf, pbuflen, ustruct, fields, num_fields, quirks) \
  13. ({ \
  14. for (size_t i = 0; i < (num_fields); i++) { \
  15. typeof(&(fields)[0]) field = &(fields)[i]; \
  16. u64 uval; \
  17. \
  18. uval = ustruct_field_to_u64(ustruct, field->offset, field->size); \
  19. \
  20. __pack(pbuf, uval, field->startbit, field->endbit, \
  21. pbuflen, quirks); \
  22. } \
  23. })
  24. #define __unpack_fields(pbuf, pbuflen, ustruct, fields, num_fields, quirks) \
  25. ({ \
  26. for (size_t i = 0; i < (num_fields); i++) { \
  27. typeof(&(fields)[0]) field = &fields[i]; \
  28. u64 uval; \
  29. \
  30. __unpack(pbuf, &uval, field->startbit, field->endbit, \
  31. pbuflen, quirks); \
  32. \
  33. u64_to_ustruct_field(ustruct, field->offset, field->size, uval); \
  34. } \
  35. })
  36. /**
  37. * calculate_box_addr - Determine physical location of byte in buffer
  38. * @box: Index of byte within buffer seen as a logical big-endian big number
  39. * @len: Size of buffer in bytes
  40. * @quirks: mask of QUIRK_LSW32_IS_FIRST and QUIRK_LITTLE_ENDIAN
  41. *
  42. * Function interprets the buffer as a @len byte sized big number, and returns
  43. * the physical offset of the @box logical octet within it. Internally, it
  44. * treats the big number as groups of 4 bytes. If @len is not a multiple of 4,
  45. * the last group may be shorter.
  46. *
  47. * @QUIRK_LSW32_IS_FIRST gives the ordering of groups of 4 octets relative to
  48. * each other. If set, the most significant group of 4 octets is last in the
  49. * buffer (and may be truncated if @len is not a multiple of 4).
  50. *
  51. * @QUIRK_LITTLE_ENDIAN gives the ordering of bytes within each group of 4.
  52. * If set, the most significant byte is last in the group. If @len takes the
  53. * form of 4k+3, the last group will only be able to represent 24 bits, and its
  54. * most significant octet is byte 2.
  55. *
  56. * Return: the physical offset into the buffer corresponding to the logical box.
  57. */
  58. static size_t calculate_box_addr(size_t box, size_t len, u8 quirks)
  59. {
  60. size_t offset_of_group, offset_in_group, this_group = box / 4;
  61. size_t group_size;
  62. if (quirks & QUIRK_LSW32_IS_FIRST)
  63. offset_of_group = this_group * 4;
  64. else
  65. offset_of_group = len - ((this_group + 1) * 4);
  66. group_size = min(4, len - offset_of_group);
  67. if (quirks & QUIRK_LITTLE_ENDIAN)
  68. offset_in_group = box - this_group * 4;
  69. else
  70. offset_in_group = group_size - (box - this_group * 4) - 1;
  71. return offset_of_group + offset_in_group;
  72. }
  73. static void __pack(void *pbuf, u64 uval, size_t startbit, size_t endbit,
  74. size_t pbuflen, u8 quirks)
  75. {
  76. /* Logical byte indices corresponding to the
  77. * start and end of the field.
  78. */
  79. int plogical_first_u8 = startbit / BITS_PER_BYTE;
  80. int plogical_last_u8 = endbit / BITS_PER_BYTE;
  81. int value_width = startbit - endbit + 1;
  82. int box;
  83. /* Check if "uval" fits in "value_width" bits.
  84. * The test only works for value_width < 64, but in the latter case,
  85. * any 64-bit uval will surely fit.
  86. */
  87. WARN(value_width < 64 && uval >= (1ull << value_width),
  88. "Cannot store 0x%llx inside bits %zu-%zu - will truncate\n",
  89. uval, startbit, endbit);
  90. /* Iterate through an idealistic view of the pbuf as an u64 with
  91. * no quirks, u8 by u8 (aligned at u8 boundaries), from high to low
  92. * logical bit significance. "box" denotes the current logical u8.
  93. */
  94. for (box = plogical_first_u8; box >= plogical_last_u8; box--) {
  95. /* Bit indices into the currently accessed 8-bit box */
  96. size_t box_start_bit, box_end_bit, box_addr;
  97. u8 box_mask;
  98. /* Corresponding bits from the unpacked u64 parameter */
  99. size_t proj_start_bit, proj_end_bit;
  100. u64 proj_mask;
  101. u64 pval;
  102. /* This u8 may need to be accessed in its entirety
  103. * (from bit 7 to bit 0), or not, depending on the
  104. * input arguments startbit and endbit.
  105. */
  106. if (box == plogical_first_u8)
  107. box_start_bit = startbit % BITS_PER_BYTE;
  108. else
  109. box_start_bit = 7;
  110. if (box == plogical_last_u8)
  111. box_end_bit = endbit % BITS_PER_BYTE;
  112. else
  113. box_end_bit = 0;
  114. /* We have determined the box bit start and end.
  115. * Now we calculate where this (masked) u8 box would fit
  116. * in the unpacked (CPU-readable) u64 - the u8 box's
  117. * projection onto the unpacked u64. Though the
  118. * box is u8, the projection is u64 because it may fall
  119. * anywhere within the unpacked u64.
  120. */
  121. proj_start_bit = ((box * BITS_PER_BYTE) + box_start_bit) - endbit;
  122. proj_end_bit = ((box * BITS_PER_BYTE) + box_end_bit) - endbit;
  123. proj_mask = GENMASK_ULL(proj_start_bit, proj_end_bit);
  124. box_mask = GENMASK(box_start_bit, box_end_bit);
  125. /* Determine the offset of the u8 box inside the pbuf,
  126. * adjusted for quirks. The adjusted box_addr will be used for
  127. * effective addressing inside the pbuf (so it's not
  128. * logical any longer).
  129. */
  130. box_addr = calculate_box_addr(box, pbuflen, quirks);
  131. /* Write to pbuf, read from uval */
  132. pval = uval & proj_mask;
  133. pval >>= proj_end_bit;
  134. pval <<= box_end_bit;
  135. if (quirks & QUIRK_MSB_ON_THE_RIGHT) {
  136. pval = bitrev8(pval);
  137. box_mask = bitrev8(box_mask);
  138. }
  139. ((u8 *)pbuf)[box_addr] &= ~box_mask;
  140. ((u8 *)pbuf)[box_addr] |= pval;
  141. }
  142. }
  143. /**
  144. * pack - Pack u64 number into bitfield of buffer.
  145. *
  146. * @pbuf: Pointer to a buffer holding the packed value.
  147. * @uval: CPU-readable unpacked value to pack.
  148. * @startbit: The index (in logical notation, compensated for quirks) where
  149. * the packed value starts within pbuf. Must be larger than, or
  150. * equal to, endbit.
  151. * @endbit: The index (in logical notation, compensated for quirks) where
  152. * the packed value ends within pbuf. Must be smaller than, or equal
  153. * to, startbit.
  154. * @pbuflen: The length in bytes of the packed buffer pointed to by @pbuf.
  155. * @quirks: A bit mask of QUIRK_LITTLE_ENDIAN, QUIRK_LSW32_IS_FIRST and
  156. * QUIRK_MSB_ON_THE_RIGHT.
  157. *
  158. * Return: 0 on success, EINVAL or ERANGE if called incorrectly. Assuming
  159. * correct usage, return code may be discarded. The @pbuf memory will
  160. * be modified on success.
  161. */
  162. int pack(void *pbuf, u64 uval, size_t startbit, size_t endbit, size_t pbuflen,
  163. u8 quirks)
  164. {
  165. /* startbit is expected to be larger than endbit, and both are
  166. * expected to be within the logically addressable range of the buffer.
  167. */
  168. if (unlikely(startbit < endbit || startbit >= BITS_PER_BYTE * pbuflen))
  169. /* Invalid function call */
  170. return -EINVAL;
  171. if (unlikely(startbit - endbit >= 64))
  172. return -ERANGE;
  173. __pack(pbuf, uval, startbit, endbit, pbuflen, quirks);
  174. return 0;
  175. }
  176. EXPORT_SYMBOL(pack);
  177. static void __unpack(const void *pbuf, u64 *uval, size_t startbit, size_t endbit,
  178. size_t pbuflen, u8 quirks)
  179. {
  180. /* Logical byte indices corresponding to the
  181. * start and end of the field.
  182. */
  183. int plogical_first_u8 = startbit / BITS_PER_BYTE;
  184. int plogical_last_u8 = endbit / BITS_PER_BYTE;
  185. int box;
  186. /* Initialize parameter */
  187. *uval = 0;
  188. /* Iterate through an idealistic view of the pbuf as an u64 with
  189. * no quirks, u8 by u8 (aligned at u8 boundaries), from high to low
  190. * logical bit significance. "box" denotes the current logical u8.
  191. */
  192. for (box = plogical_first_u8; box >= plogical_last_u8; box--) {
  193. /* Bit indices into the currently accessed 8-bit box */
  194. size_t box_start_bit, box_end_bit, box_addr;
  195. u8 box_mask;
  196. /* Corresponding bits from the unpacked u64 parameter */
  197. size_t proj_start_bit, proj_end_bit;
  198. u64 proj_mask;
  199. u64 pval;
  200. /* This u8 may need to be accessed in its entirety
  201. * (from bit 7 to bit 0), or not, depending on the
  202. * input arguments startbit and endbit.
  203. */
  204. if (box == plogical_first_u8)
  205. box_start_bit = startbit % BITS_PER_BYTE;
  206. else
  207. box_start_bit = 7;
  208. if (box == plogical_last_u8)
  209. box_end_bit = endbit % BITS_PER_BYTE;
  210. else
  211. box_end_bit = 0;
  212. /* We have determined the box bit start and end.
  213. * Now we calculate where this (masked) u8 box would fit
  214. * in the unpacked (CPU-readable) u64 - the u8 box's
  215. * projection onto the unpacked u64. Though the
  216. * box is u8, the projection is u64 because it may fall
  217. * anywhere within the unpacked u64.
  218. */
  219. proj_start_bit = ((box * BITS_PER_BYTE) + box_start_bit) - endbit;
  220. proj_end_bit = ((box * BITS_PER_BYTE) + box_end_bit) - endbit;
  221. proj_mask = GENMASK_ULL(proj_start_bit, proj_end_bit);
  222. box_mask = GENMASK(box_start_bit, box_end_bit);
  223. /* Determine the offset of the u8 box inside the pbuf,
  224. * adjusted for quirks. The adjusted box_addr will be used for
  225. * effective addressing inside the pbuf (so it's not
  226. * logical any longer).
  227. */
  228. box_addr = calculate_box_addr(box, pbuflen, quirks);
  229. /* Read from pbuf, write to uval */
  230. pval = ((u8 *)pbuf)[box_addr];
  231. if (quirks & QUIRK_MSB_ON_THE_RIGHT)
  232. pval = bitrev8(pval);
  233. pval &= box_mask;
  234. pval >>= box_end_bit;
  235. pval <<= proj_end_bit;
  236. *uval &= ~proj_mask;
  237. *uval |= pval;
  238. }
  239. }
  240. /**
  241. * unpack - Unpack u64 number from packed buffer.
  242. *
  243. * @pbuf: Pointer to a buffer holding the packed value.
  244. * @uval: Pointer to an u64 holding the unpacked value.
  245. * @startbit: The index (in logical notation, compensated for quirks) where
  246. * the packed value starts within pbuf. Must be larger than, or
  247. * equal to, endbit.
  248. * @endbit: The index (in logical notation, compensated for quirks) where
  249. * the packed value ends within pbuf. Must be smaller than, or equal
  250. * to, startbit.
  251. * @pbuflen: The length in bytes of the packed buffer pointed to by @pbuf.
  252. * @quirks: A bit mask of QUIRK_LITTLE_ENDIAN, QUIRK_LSW32_IS_FIRST and
  253. * QUIRK_MSB_ON_THE_RIGHT.
  254. *
  255. * Return: 0 on success, EINVAL or ERANGE if called incorrectly. Assuming
  256. * correct usage, return code may be discarded. The @uval will be
  257. * modified on success.
  258. */
  259. int unpack(const void *pbuf, u64 *uval, size_t startbit, size_t endbit,
  260. size_t pbuflen, u8 quirks)
  261. {
  262. /* width of the field to access in the pbuf */
  263. u64 value_width;
  264. /* startbit is expected to be larger than endbit, and both are
  265. * expected to be within the logically addressable range of the buffer.
  266. */
  267. if (startbit < endbit || startbit >= BITS_PER_BYTE * pbuflen)
  268. /* Invalid function call */
  269. return -EINVAL;
  270. value_width = startbit - endbit + 1;
  271. if (value_width > 64)
  272. return -ERANGE;
  273. __unpack(pbuf, uval, startbit, endbit, pbuflen, quirks);
  274. return 0;
  275. }
  276. EXPORT_SYMBOL(unpack);
  277. /**
  278. * packing - Convert numbers (currently u64) between a packed and an unpacked
  279. * format. Unpacked means laid out in memory in the CPU's native
  280. * understanding of integers, while packed means anything else that
  281. * requires translation.
  282. *
  283. * @pbuf: Pointer to a buffer holding the packed value.
  284. * @uval: Pointer to an u64 holding the unpacked value.
  285. * @startbit: The index (in logical notation, compensated for quirks) where
  286. * the packed value starts within pbuf. Must be larger than, or
  287. * equal to, endbit.
  288. * @endbit: The index (in logical notation, compensated for quirks) where
  289. * the packed value ends within pbuf. Must be smaller than, or equal
  290. * to, startbit.
  291. * @pbuflen: The length in bytes of the packed buffer pointed to by @pbuf.
  292. * @op: If PACK, then uval will be treated as const pointer and copied (packed)
  293. * into pbuf, between startbit and endbit.
  294. * If UNPACK, then pbuf will be treated as const pointer and the logical
  295. * value between startbit and endbit will be copied (unpacked) to uval.
  296. * @quirks: A bit mask of QUIRK_LITTLE_ENDIAN, QUIRK_LSW32_IS_FIRST and
  297. * QUIRK_MSB_ON_THE_RIGHT.
  298. *
  299. * Note: this is deprecated, prefer to use pack() or unpack() in new code.
  300. *
  301. * Return: 0 on success, EINVAL or ERANGE if called incorrectly. Assuming
  302. * correct usage, return code may be discarded.
  303. * If op is PACK, pbuf is modified.
  304. * If op is UNPACK, uval is modified.
  305. */
  306. int packing(void *pbuf, u64 *uval, int startbit, int endbit, size_t pbuflen,
  307. enum packing_op op, u8 quirks)
  308. {
  309. if (op == PACK)
  310. return pack(pbuf, *uval, startbit, endbit, pbuflen, quirks);
  311. return unpack(pbuf, uval, startbit, endbit, pbuflen, quirks);
  312. }
  313. EXPORT_SYMBOL(packing);
  314. static u64 ustruct_field_to_u64(const void *ustruct, size_t field_offset,
  315. size_t field_size)
  316. {
  317. switch (field_size) {
  318. case 1:
  319. return *((u8 *)(ustruct + field_offset));
  320. case 2:
  321. return *((u16 *)(ustruct + field_offset));
  322. case 4:
  323. return *((u32 *)(ustruct + field_offset));
  324. default:
  325. return *((u64 *)(ustruct + field_offset));
  326. }
  327. }
  328. static void u64_to_ustruct_field(void *ustruct, size_t field_offset,
  329. size_t field_size, u64 uval)
  330. {
  331. switch (field_size) {
  332. case 1:
  333. *((u8 *)(ustruct + field_offset)) = uval;
  334. break;
  335. case 2:
  336. *((u16 *)(ustruct + field_offset)) = uval;
  337. break;
  338. case 4:
  339. *((u32 *)(ustruct + field_offset)) = uval;
  340. break;
  341. default:
  342. *((u64 *)(ustruct + field_offset)) = uval;
  343. break;
  344. }
  345. }
  346. /**
  347. * pack_fields_u8 - Pack array of fields
  348. *
  349. * @pbuf: Pointer to a buffer holding the packed value.
  350. * @pbuflen: The length in bytes of the packed buffer pointed to by @pbuf.
  351. * @ustruct: Pointer to CPU-readable structure holding the unpacked value.
  352. * It is expected (but not checked) that this has the same data type
  353. * as all struct packed_field_u8 definitions.
  354. * @fields: Array of packed_field_u8 field definition. They must not overlap.
  355. * @num_fields: Length of @fields array.
  356. * @quirks: A bit mask of QUIRK_LITTLE_ENDIAN, QUIRK_LSW32_IS_FIRST and
  357. * QUIRK_MSB_ON_THE_RIGHT.
  358. *
  359. * Use the pack_fields() macro instead of calling this directly.
  360. */
  361. void pack_fields_u8(void *pbuf, size_t pbuflen, const void *ustruct,
  362. const struct packed_field_u8 *fields, size_t num_fields,
  363. u8 quirks)
  364. {
  365. __pack_fields(pbuf, pbuflen, ustruct, fields, num_fields, quirks);
  366. }
  367. EXPORT_SYMBOL(pack_fields_u8);
  368. /**
  369. * pack_fields_u16 - Pack array of fields
  370. *
  371. * @pbuf: Pointer to a buffer holding the packed value.
  372. * @pbuflen: The length in bytes of the packed buffer pointed to by @pbuf.
  373. * @ustruct: Pointer to CPU-readable structure holding the unpacked value.
  374. * It is expected (but not checked) that this has the same data type
  375. * as all struct packed_field_u16 definitions.
  376. * @fields: Array of packed_field_u16 field definitions. They must not overlap.
  377. * @num_fields: Length of @fields array.
  378. * @quirks: A bit mask of QUIRK_LITTLE_ENDIAN, QUIRK_LSW32_IS_FIRST and
  379. * QUIRK_MSB_ON_THE_RIGHT.
  380. *
  381. * Use the pack_fields() macro instead of calling this directly.
  382. */
  383. void pack_fields_u16(void *pbuf, size_t pbuflen, const void *ustruct,
  384. const struct packed_field_u16 *fields, size_t num_fields,
  385. u8 quirks)
  386. {
  387. __pack_fields(pbuf, pbuflen, ustruct, fields, num_fields, quirks);
  388. }
  389. EXPORT_SYMBOL(pack_fields_u16);
  390. /**
  391. * unpack_fields_u8 - Unpack array of fields
  392. *
  393. * @pbuf: Pointer to a buffer holding the packed value.
  394. * @pbuflen: The length in bytes of the packed buffer pointed to by @pbuf.
  395. * @ustruct: Pointer to CPU-readable structure holding the unpacked value.
  396. * It is expected (but not checked) that this has the same data type
  397. * as all struct packed_field_u8 definitions.
  398. * @fields: Array of packed_field_u8 field definitions. They must not overlap.
  399. * @num_fields: Length of @fields array.
  400. * @quirks: A bit mask of QUIRK_LITTLE_ENDIAN, QUIRK_LSW32_IS_FIRST and
  401. * QUIRK_MSB_ON_THE_RIGHT.
  402. *
  403. * Use the unpack_fields() macro instead of calling this directly.
  404. */
  405. void unpack_fields_u8(const void *pbuf, size_t pbuflen, void *ustruct,
  406. const struct packed_field_u8 *fields, size_t num_fields,
  407. u8 quirks)
  408. {
  409. __unpack_fields(pbuf, pbuflen, ustruct, fields, num_fields, quirks);
  410. }
  411. EXPORT_SYMBOL(unpack_fields_u8);
  412. /**
  413. * unpack_fields_u16 - Unpack array of fields
  414. *
  415. * @pbuf: Pointer to a buffer holding the packed value.
  416. * @pbuflen: The length in bytes of the packed buffer pointed to by @pbuf.
  417. * @ustruct: Pointer to CPU-readable structure holding the unpacked value.
  418. * It is expected (but not checked) that this has the same data type
  419. * as all struct packed_field_u16 definitions.
  420. * @fields: Array of packed_field_u16 field definitions. They must not overlap.
  421. * @num_fields: Length of @fields array.
  422. * @quirks: A bit mask of QUIRK_LITTLE_ENDIAN, QUIRK_LSW32_IS_FIRST and
  423. * QUIRK_MSB_ON_THE_RIGHT.
  424. *
  425. * Use the unpack_fields() macro instead of calling this directly.
  426. */
  427. void unpack_fields_u16(const void *pbuf, size_t pbuflen, void *ustruct,
  428. const struct packed_field_u16 *fields, size_t num_fields,
  429. u8 quirks)
  430. {
  431. __unpack_fields(pbuf, pbuflen, ustruct, fields, num_fields, quirks);
  432. }
  433. EXPORT_SYMBOL(unpack_fields_u16);
  434. MODULE_DESCRIPTION("Generic bitfield packing and unpacking");