@@ -210,5 +210,191 @@ int packing(void *pbuf, u64 *uval, int startbit, int endbit, size_t pbuflen,
}
EXPORT_SYMBOL(packing);
+#if IS_ENABLED(CONFIG_PACKING_SELFTESTS)
+
+#define PBUF_LEN 16
+
+/* These selftests pack and unpack a magic 64-bit value (0xcafedeadbeefcafe) at
+ * a fixed logical offset (32) within an otherwise zero array of 128 bits
+ * (16 bytes). They test all possible bit layouts of the 128 bit buffer.
+ */
+static bool test_pack(u8 expected_pbuf[PBUF_LEN], u8 quirks)
+{
+ u64 uval = 0xcafedeadbeefcafe;
+ u8 pbuf[PBUF_LEN];
+ int err, i;
+
+ memset(pbuf, 0, PBUF_LEN);
+ err = packing(pbuf, &uval, 95, 32, PBUF_LEN, PACK, quirks);
+ if (err) {
+ pr_err("packing() returned %pe\n", ERR_PTR(err));
+ return false;
+ }
+
+ for (i = 0; i < PBUF_LEN; i++) {
+ if (pbuf[i] != expected_pbuf[i]) {
+ print_hex_dump(KERN_ERR, "pbuf: ", DUMP_PREFIX_NONE,
+ 16, 1, pbuf, PBUF_LEN, false);
+ print_hex_dump(KERN_ERR, "expected: ", DUMP_PREFIX_NONE,
+ 16, 1, expected_pbuf, PBUF_LEN, false);
+ return false;
+ }
+ }
+
+ return true;
+}
+
+static bool test_unpack(u8 pbuf[PBUF_LEN], u8 quirks)
+{
+ u64 uval, expected_uval = 0xcafedeadbeefcafe;
+ int err;
+
+ err = packing(pbuf, &uval, 95, 32, PBUF_LEN, UNPACK, quirks);
+ if (err) {
+ pr_err("packing() returned %pe\n", ERR_PTR(err));
+ return false;
+ }
+
+ if (uval != expected_uval) {
+ pr_err("uval: 0x%llx expected 0x%llx\n", uval, expected_uval);
+ return false;
+ }
+
+ return true;
+}
+
+static void test_no_quirks(void)
+{
+ u8 pbuf[PBUF_LEN] = {0x00, 0x00, 0x00, 0x00, 0xca, 0xfe, 0xde, 0xad,
+ 0xbe, 0xef, 0xca, 0xfe, 0x00, 0x00, 0x00, 0x00};
+ bool ret;
+
+ ret = test_pack(pbuf, 0);
+ pr_info("packing with no quirks: %s\n", ret ? "OK" : "FAIL");
+
+ ret = test_unpack(pbuf, 0);
+ pr_info("unpacking with no quirks: %s\n", ret ? "OK" : "FAIL");
+}
+
+static void test_msb_right(void)
+{
+ u8 pbuf[PBUF_LEN] = {0x00, 0x00, 0x00, 0x00, 0x53, 0x7f, 0x7b, 0xb5,
+ 0x7d, 0xf7, 0x53, 0x7f, 0x00, 0x00, 0x00, 0x00};
+ bool ret;
+
+ ret = test_pack(pbuf, QUIRK_MSB_ON_THE_RIGHT);
+ pr_info("packing with QUIRK_MSB_ON_THE_RIGHT: %s\n",
+ ret ? "OK" : "FAIL");
+
+ ret = test_unpack(pbuf, QUIRK_MSB_ON_THE_RIGHT);
+ pr_info("unpacking with QUIRK_MSB_ON_THE_RIGHT: %s\n",
+ ret ? "OK" : "FAIL");
+}
+
+static void test_le(void)
+{
+ u8 pbuf[PBUF_LEN] = {0x00, 0x00, 0x00, 0x00, 0xad, 0xde, 0xfe, 0xca,
+ 0xfe, 0xca, 0xef, 0xbe, 0x00, 0x00, 0x00, 0x00};
+ bool ret;
+
+ ret = test_pack(pbuf, QUIRK_LITTLE_ENDIAN);
+ pr_info("packing with QUIRK_LITTLE_ENDIAN: %s\n", ret ? "OK" : "FAIL");
+
+ ret = test_unpack(pbuf, QUIRK_LITTLE_ENDIAN);
+ pr_info("unpacking with QUIRK_LITTLE_ENDIAN: %s\n",
+ ret ? "OK" : "FAIL");
+}
+
+static void test_le_msb_right(void)
+{
+ u8 pbuf[PBUF_LEN] = {0x00, 0x00, 0x00, 0x00, 0xb5, 0x7b, 0x7f, 0x53,
+ 0x7f, 0x53, 0xf7, 0x7d, 0x00, 0x00, 0x00, 0x00};
+ bool ret;
+
+ ret = test_pack(pbuf, QUIRK_LITTLE_ENDIAN | QUIRK_MSB_ON_THE_RIGHT);
+ pr_info("packing with QUIRK_LITTLE_ENDIAN | QUIRK_MSB_ON_THE_RIGHT: %s\n",
+ ret ? "OK" : "FAIL");
+
+ ret = test_unpack(pbuf, QUIRK_LITTLE_ENDIAN | QUIRK_MSB_ON_THE_RIGHT);
+ pr_info("unpacking with QUIRK_LITTLE_ENDIAN | QUIRK_MSB_ON_THE_RIGHT: %s\n",
+ ret ? "OK" : "FAIL");
+}
+
+static void test_lsw32_first(void)
+{
+ u8 pbuf[PBUF_LEN] = {0x00, 0x00, 0x00, 0x00, 0xbe, 0xef, 0xca, 0xfe,
+ 0xca, 0xfe, 0xde, 0xad, 0x00, 0x00, 0x00, 0x00};
+ bool ret;
+
+ ret = test_pack(pbuf, QUIRK_LSW32_IS_FIRST);
+ pr_info("packing with QUIRK_LSW32_IS_FIRST: %s\n", ret ? "OK" : "FAIL");
+
+ ret = test_unpack(pbuf, QUIRK_LSW32_IS_FIRST);
+ pr_info("unpacking with QUIRK_LSW32_IS_FIRST: %s\n", ret ? "OK" : "FAIL");
+}
+
+static void test_lsw32_first_msb_right(void)
+{
+ u8 pbuf[PBUF_LEN] = {0x00, 0x00, 0x00, 0x00, 0x7d, 0xf7, 0x53, 0x7f,
+ 0x53, 0x7f, 0x7b, 0xb5, 0x00, 0x00, 0x00, 0x00};
+ bool ret;
+
+ ret = test_pack(pbuf, QUIRK_LSW32_IS_FIRST | QUIRK_MSB_ON_THE_RIGHT);
+ pr_info("packing with QUIRK_LSW32_IS_FIRST | QUIRK_MSB_ON_THE_RIGHT: %s\n",
+ ret ? "OK" : "FAIL");
+
+ ret = test_unpack(pbuf, QUIRK_LSW32_IS_FIRST | QUIRK_MSB_ON_THE_RIGHT);
+ pr_info("unpacking with QUIRK_LSW32_IS_FIRST | QUIRK_MSB_ON_THE_RIGHT: %s\n",
+ ret ? "OK" : "FAIL");
+}
+
+static void test_lsw32_first_le(void)
+{
+ u8 pbuf[PBUF_LEN] = {0x00, 0x00, 0x00, 0x00, 0xfe, 0xca, 0xef, 0xbe,
+ 0xad, 0xde, 0xfe, 0xca, 0x00, 0x00, 0x00, 0x00};
+ bool ret;
+
+ ret = test_pack(pbuf, QUIRK_LSW32_IS_FIRST | QUIRK_LITTLE_ENDIAN);
+ pr_info("packing with QUIRK_LSW32_IS_FIRST | QUIRK_LITTLE_ENDIAN: %s\n",
+ ret ? "OK" : "FAIL");
+
+ ret = test_unpack(pbuf, QUIRK_LSW32_IS_FIRST | QUIRK_LITTLE_ENDIAN);
+ pr_info("unpacking with QUIRK_LSW32_IS_FIRST | QUIRK_LITTLE_ENDIAN: %s\n",
+ ret ? "OK" : "FAIL");
+}
+
+static void test_lsw32_first_le_msb_right(void)
+{
+ u8 pbuf[PBUF_LEN] = {0x00, 0x00, 0x00, 0x00, 0x7f, 0x53, 0xf7, 0x7d,
+ 0xb5, 0x7b, 0x7f, 0x53, 0x00, 0x00, 0x00, 0x00};
+ bool ret;
+
+ ret = test_pack(pbuf, QUIRK_LSW32_IS_FIRST | QUIRK_LITTLE_ENDIAN |
+ QUIRK_MSB_ON_THE_RIGHT);
+ pr_info("packing with QUIRK_LSW32_IS_FIRST | QUIRK_LITTLE_ENDIAN | QUIRK_MSB_ON_THE_RIGHT: %s\n",
+ ret ? "OK" : "FAIL");
+
+ ret = test_unpack(pbuf, QUIRK_LSW32_IS_FIRST | QUIRK_LITTLE_ENDIAN |
+ QUIRK_MSB_ON_THE_RIGHT);
+ pr_info("unpacking with QUIRK_LSW32_IS_FIRST | QUIRK_LITTLE_ENDIAN | QUIRK_MSB_ON_THE_RIGHT: %s\n",
+ ret ? "OK" : "FAIL");
+}
+
+static int __init packing_init(void)
+{
+ test_no_quirks();
+ test_msb_right();
+ test_le();
+ test_le_msb_right();
+ test_lsw32_first();
+ test_lsw32_first_msb_right();
+ test_lsw32_first_le();
+ test_lsw32_first_le_msb_right();
+
+ return 0;
+}
+module_init(packing_init);
+#endif
+
MODULE_LICENSE("GPL v2");
MODULE_DESCRIPTION("Generic bitfield packing and unpacking");