rw.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Tests for read and write commands
  4. */
  5. #include <common.h>
  6. #include <dm/test.h>
  7. #include <mapmem.h>
  8. #include <part.h>
  9. #include <test/test.h>
  10. #include <test/ut.h>
  11. static int setup_partitions(struct unit_test_state *uts, struct blk_desc **mmc_dev_desc)
  12. {
  13. char str_disk_guid[UUID_STR_LEN + 1];
  14. struct disk_partition parts[2] = {
  15. {
  16. .start = 48, /* GPT data takes up the first 34 blocks or so */
  17. .size = 4,
  18. .name = "data",
  19. },
  20. {
  21. .start = 52,
  22. .size = 10,
  23. .name = "log",
  24. },
  25. };
  26. ut_asserteq(2, blk_get_device_by_str("mmc", "2", mmc_dev_desc));
  27. if (CONFIG_IS_ENABLED(RANDOM_UUID)) {
  28. gen_rand_uuid_str(parts[0].uuid, UUID_STR_FORMAT_STD);
  29. gen_rand_uuid_str(parts[1].uuid, UUID_STR_FORMAT_STD);
  30. gen_rand_uuid_str(str_disk_guid, UUID_STR_FORMAT_STD);
  31. }
  32. ut_assertok(gpt_restore(*mmc_dev_desc, str_disk_guid, parts,
  33. ARRAY_SIZE(parts)));
  34. return 0;
  35. }
  36. /* Fill the write buffer with pseudo-random data, clear the read buffer. */
  37. static void init_buffers(char *rb, char *wb, size_t size, unsigned seed)
  38. {
  39. memset(rb, 0, size);
  40. while (size--) {
  41. *wb++ = seed;
  42. seed *= 43;
  43. seed += 17 + size/4;
  44. }
  45. }
  46. static int dm_test_read_write(struct unit_test_state *uts)
  47. {
  48. struct blk_desc *dev_desc;
  49. char wbuf[1024], rbuf[1024];
  50. ulong wa, ra;
  51. #define INIT_BUFFERS() init_buffers(rbuf, wbuf, sizeof(rbuf), __LINE__)
  52. ut_assertok(setup_partitions(uts, &dev_desc));
  53. wa = map_to_sysmem(wbuf);
  54. ra = map_to_sysmem(rbuf);
  55. /* Simple test, write to/read from same partition. */
  56. INIT_BUFFERS();
  57. ut_assertok(run_commandf("write mmc 2:1 0x%lx 0 2", wa));
  58. ut_assertok(run_commandf("read mmc 2:1 0x%lx 0 2", ra));
  59. ut_assertok(memcmp(wbuf, rbuf, sizeof(wbuf)));
  60. ut_assertok(run_commandf("read mmc 2:1 0x%lx 1 1", ra));
  61. ut_assertok(memcmp(&wbuf[512], rbuf, 512));
  62. /* Use name for write, number for read. */
  63. INIT_BUFFERS();
  64. ut_assertok(run_commandf("write mmc 2#log 0x%lx 0 2", wa));
  65. ut_assertok(run_commandf("read mmc 2:2 0x%lx 0 2", ra));
  66. ut_assertok(memcmp(wbuf, rbuf, sizeof(wbuf)));
  67. /* Use full device for write, name for read. */
  68. INIT_BUFFERS();
  69. ut_assertok(run_commandf("write mmc 2:0 0x%lx 0x30 2", wa));
  70. ut_assertok(run_commandf("read mmc 2#data 0x%lx 0 2", ra));
  71. ut_assertok(memcmp(wbuf, rbuf, sizeof(wbuf)));
  72. /* Use name for write, full device for read */
  73. INIT_BUFFERS();
  74. ut_assertok(run_commandf("write mmc 2#log 0x%lx 1 2", wa));
  75. ut_assertok(run_commandf("read mmc 2:0 0x%lx 0x35 2", ra));
  76. ut_assertok(memcmp(wbuf, rbuf, sizeof(wbuf)));
  77. /* Read/write outside partition bounds should be rejected upfront. */
  78. console_record_reset_enable();
  79. ut_asserteq(1, run_commandf("read mmc 2#data 0x%lx 3 2", ra));
  80. ut_assert_nextlinen("read out of range");
  81. ut_assert_console_end();
  82. console_record_reset_enable();
  83. ut_asserteq(1, run_commandf("write mmc 2#log 0x%lx 9 2", wa));
  84. ut_assert_nextlinen("write out of range");
  85. ut_assert_console_end();
  86. return 0;
  87. }
  88. DM_TEST(dm_test_read_write, UT_TESTF_SCAN_FDT | UT_TESTF_CONSOLE_REC);