]> git.baikalelectronics.ru Git - uboot.git/commitdiff
test: blkmap: Add test suite
authorTobias Waldekranz <tobias@waldekranz.com>
Thu, 16 Feb 2023 15:33:53 +0000 (16:33 +0100)
committerTom Rini <trini@konsulko.com>
Wed, 5 Apr 2023 14:54:47 +0000 (10:54 -0400)
Verify that:

- Block maps can be created and destroyed
- Mappings aren't allowed to overlap
- Multiple mappings can be attached and be read/written from/to

Signed-off-by: Tobias Waldekranz <tobias@waldekranz.com>
Reviewed-by: Simon Glass <sjg@chromium.org>
MAINTAINERS
configs/sandbox_defconfig
test/dm/Makefile
test/dm/blkmap.c [new file with mode: 0644]

index e0a1f8cbc9774b5cad6256411293970ee1cb90c3..ec8b149ce7460918d5999f2e4e20160d65d9ff21 100644 (file)
@@ -799,6 +799,7 @@ S:  Maintained
 F:     cmd/blkmap.c
 F:     drivers/block/blkmap.c
 F:     include/blkmap.h
+F:     test/dm/blkmap.c
 
 BOOTDEVICE
 M:     Simon Glass <sjg@chromium.org>
index cbace259f80331b20927c47a7ab09f6b5ea0a838..3a1f14c60fdafef77bbb5c87bb4fe7c8e395ab1c 100644 (file)
@@ -147,6 +147,7 @@ CONFIG_ADC=y
 CONFIG_ADC_SANDBOX=y
 CONFIG_AXI=y
 CONFIG_AXI_SANDBOX=y
+CONFIG_BLKMAP=y
 CONFIG_SYS_IDE_MAXBUS=1
 CONFIG_SYS_ATA_BASE_ADDR=0x100
 CONFIG_SYS_ATA_STRIDE=4
index 7a79b6e1a25d8e92b73f1163c12c58a475453856..e15bdbf04bc32f12469115c041f8fc2ec0b77487 100644 (file)
@@ -29,6 +29,7 @@ obj-$(CONFIG_ADC) += adc.o
 obj-$(CONFIG_SOUND) += audio.o
 obj-$(CONFIG_AXI) += axi.o
 obj-$(CONFIG_BLK) += blk.o
+obj-$(CONFIG_BLKMAP) += blkmap.o
 obj-$(CONFIG_BUTTON) += button.o
 obj-$(CONFIG_DM_BOOTCOUNT) += bootcount.o
 obj-$(CONFIG_DM_REBOOT_MODE) += reboot-mode.o
diff --git a/test/dm/blkmap.c b/test/dm/blkmap.c
new file mode 100644 (file)
index 0000000..7a163d6
--- /dev/null
@@ -0,0 +1,201 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (c) 2023 Addiva Elektronik
+ * Author: Tobias Waldekranz <tobias@waldekranz.com>
+ */
+
+#include <common.h>
+#include <blk.h>
+#include <blkmap.h>
+#include <dm.h>
+#include <asm/test.h>
+#include <dm/test.h>
+#include <test/test.h>
+#include <test/ut.h>
+
+#define BLKSZ 0x200
+
+struct mapping {
+       int src;
+       int cnt;
+       int dst;
+};
+
+const struct mapping unordered_mapping[] = {
+       { 0, 1, 3 },
+       { 1, 3, 0 },
+       { 4, 2, 6 },
+       { 6, 2, 4 },
+
+       { 0, 0, 0 }
+};
+
+const struct mapping identity_mapping[] = {
+       { 0, 8, 0 },
+
+       { 0, 0, 0 }
+};
+
+static char identity[8 * BLKSZ];
+static char unordered[8 * BLKSZ];
+static char buffer[8 * BLKSZ];
+
+static void mkblob(void *base, const struct mapping *m)
+{
+       int nr;
+
+       for (; m->cnt; m++) {
+               for (nr = 0; nr < m->cnt; nr++) {
+                       memset(base + (m->dst + nr) * BLKSZ,
+                              m->src + nr, BLKSZ);
+               }
+       }
+}
+
+static int dm_test_blkmap_read(struct unit_test_state *uts)
+{
+       struct udevice *dev, *blk;
+       const struct mapping *m;
+
+       ut_assertok(blkmap_create("rdtest", &dev));
+       ut_assertok(blk_get_from_parent(dev, &blk));
+
+       /* Generate an ordered and an unordered pattern in memory */
+       mkblob(unordered, unordered_mapping);
+       mkblob(identity, identity_mapping);
+
+       /* Create a blkmap that cancels out the disorder */
+       for (m = unordered_mapping; m->cnt; m++) {
+               ut_assertok(blkmap_map_mem(dev, m->src, m->cnt,
+                                          unordered + m->dst * BLKSZ));
+       }
+
+       /* Read out the data via the blkmap device to another area,
+        * and verify that it matches the ordered pattern.
+        */
+       ut_asserteq(8, blk_read(blk, 0, 8, buffer));
+       ut_assertok(memcmp(buffer, identity, sizeof(buffer)));
+
+       ut_assertok(blkmap_destroy(dev));
+       return 0;
+}
+DM_TEST(dm_test_blkmap_read, 0);
+
+static int dm_test_blkmap_write(struct unit_test_state *uts)
+{
+       struct udevice *dev, *blk;
+       const struct mapping *m;
+
+       ut_assertok(blkmap_create("wrtest", &dev));
+       ut_assertok(blk_get_from_parent(dev, &blk));
+
+       /* Generate an ordered and an unordered pattern in memory */
+       mkblob(unordered, unordered_mapping);
+       mkblob(identity, identity_mapping);
+
+       /* Create a blkmap that mimics the disorder */
+       for (m = unordered_mapping; m->cnt; m++) {
+               ut_assertok(blkmap_map_mem(dev, m->src, m->cnt,
+                                          buffer + m->dst * BLKSZ));
+       }
+
+       /* Write the ordered data via the blkmap device to another
+        * area, and verify that the result matches the unordered
+        * pattern.
+        */
+       ut_asserteq(8, blk_write(blk, 0, 8, identity));
+       ut_assertok(memcmp(buffer, unordered, sizeof(buffer)));
+
+       ut_assertok(blkmap_destroy(dev));
+       return 0;
+}
+DM_TEST(dm_test_blkmap_write, 0);
+
+static int dm_test_blkmap_slicing(struct unit_test_state *uts)
+{
+       struct udevice *dev;
+
+       ut_assertok(blkmap_create("slicetest", &dev));
+
+       ut_assertok(blkmap_map_mem(dev, 8, 8, NULL));
+
+       /* Can't overlap on the low end */
+       ut_asserteq(-EBUSY, blkmap_map_mem(dev,  4, 5, NULL));
+       /* Can't be inside */
+       ut_asserteq(-EBUSY, blkmap_map_mem(dev, 10, 2, NULL));
+       /* Can't overlap on the high end */
+       ut_asserteq(-EBUSY, blkmap_map_mem(dev, 15, 4, NULL));
+
+       /* But we should be able to add slices right before and
+        * after
+        */
+       ut_assertok(blkmap_map_mem(dev,  4, 4, NULL));
+       ut_assertok(blkmap_map_mem(dev, 16, 4, NULL));
+
+       ut_assertok(blkmap_destroy(dev));
+       return 0;
+}
+DM_TEST(dm_test_blkmap_slicing, 0);
+
+static int dm_test_blkmap_creation(struct unit_test_state *uts)
+{
+       struct udevice *first, *second;
+
+       ut_assertok(blkmap_create("first", &first));
+
+       /* Can't have two "first"s */
+       ut_asserteq(-EBUSY, blkmap_create("first", &second));
+
+       /* But "second" should be fine */
+       ut_assertok(blkmap_create("second", &second));
+
+       /* Once "first" is destroyed, we should be able to create it
+        * again
+        */
+       ut_assertok(blkmap_destroy(first));
+       ut_assertok(blkmap_create("first", &first));
+
+       ut_assertok(blkmap_destroy(first));
+       ut_assertok(blkmap_destroy(second));
+       return 0;
+}
+DM_TEST(dm_test_blkmap_creation, 0);
+
+static int dm_test_cmd_blkmap(struct unit_test_state *uts)
+{
+       ulong loadaddr = env_get_hex("loadaddr", 0);
+       struct udevice *dev;
+
+       console_record_reset();
+
+       ut_assertok(run_command("blkmap info", 0));
+       ut_assert_console_end();
+
+       ut_assertok(run_command("blkmap create ramdisk", 0));
+       ut_assert_nextline("Created \"ramdisk\"");
+       ut_assert_console_end();
+
+       ut_assertnonnull((dev = blkmap_from_label("ramdisk")));
+
+       ut_assertok(run_commandf("blkmap map ramdisk 0 800 mem 0x%lx", loadaddr));
+       ut_assert_nextline("Block 0x0+0x800 mapped to 0x%lx", loadaddr);
+       ut_assert_console_end();
+
+       ut_assertok(run_command("blkmap info", 0));
+       ut_assert_nextline("Device 0: Vendor: U-Boot Rev: 1.0 Prod: blkmap");
+       ut_assert_nextline("            Type: Hard Disk");
+       ut_assert_nextline("            Capacity: 1.0 MB = 0.0 GB (2048 x 512)");
+       ut_assert_console_end();
+
+       ut_assertok(run_command("blkmap get ramdisk dev devnum", 0));
+       ut_asserteq(dev_seq(dev), env_get_hex("devnum", 0xdeadbeef));
+
+       ut_assertok(run_command("blkmap destroy ramdisk", 0));
+       ut_assert_nextline("Destroyed \"ramdisk\"");
+       ut_assert_console_end();
+
+       ut_assertok(run_command("blkmap info", 0));
+       ut_assert_console_end();
+       return 0;
+}
+DM_TEST(dm_test_cmd_blkmap, 0);