part.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2020 Sean Anderson <sean.anderson@seco.com>
  4. */
  5. #include <common.h>
  6. #include <dm.h>
  7. #include <mmc.h>
  8. #include <part.h>
  9. #include <part_efi.h>
  10. #include <dm/test.h>
  11. #include <test/ut.h>
  12. static int dm_test_part(struct unit_test_state *uts)
  13. {
  14. char str_disk_guid[UUID_STR_LEN + 1];
  15. struct blk_desc *mmc_dev_desc;
  16. struct disk_partition part_info;
  17. struct disk_partition parts[2] = {
  18. {
  19. .start = 48, /* GPT data takes up the first 34 blocks or so */
  20. .size = 1,
  21. .name = "test1",
  22. },
  23. {
  24. .start = 49,
  25. .size = 1,
  26. .name = "test2",
  27. },
  28. };
  29. ut_asserteq(1, blk_get_device_by_str("mmc", "1", &mmc_dev_desc));
  30. if (CONFIG_IS_ENABLED(RANDOM_UUID)) {
  31. gen_rand_uuid_str(parts[0].uuid, UUID_STR_FORMAT_STD);
  32. gen_rand_uuid_str(parts[1].uuid, UUID_STR_FORMAT_STD);
  33. gen_rand_uuid_str(str_disk_guid, UUID_STR_FORMAT_STD);
  34. }
  35. ut_assertok(gpt_restore(mmc_dev_desc, str_disk_guid, parts,
  36. ARRAY_SIZE(parts)));
  37. #define test(expected, part_str, whole) \
  38. ut_asserteq(expected, \
  39. part_get_info_by_dev_and_name_or_num("mmc", part_str, \
  40. &mmc_dev_desc, \
  41. &part_info, whole))
  42. test(-ENODEV, "", true);
  43. env_set("bootdevice", "0");
  44. test(0, "", true);
  45. env_set("bootdevice", "1");
  46. test(1, "", false);
  47. test(1, "-", false);
  48. env_set("bootdevice", "");
  49. test(-EPROTONOSUPPORT, "0", false);
  50. test(0, "0", true);
  51. test(0, ":0", true);
  52. test(0, ".0", true);
  53. test(0, ".0:0", true);
  54. test(-EINVAL, "#test1", true);
  55. test(1, "1", false);
  56. test(1, "1", true);
  57. test(-ENOENT, "1:0", false);
  58. test(0, "1:0", true);
  59. test(1, "1:1", false);
  60. test(2, "1:2", false);
  61. test(1, "1.0", false);
  62. test(0, "1.0:0", true);
  63. test(1, "1.0:1", false);
  64. test(2, "1.0:2", false);
  65. test(-EINVAL, "1#bogus", false);
  66. test(1, "1#test1", false);
  67. test(2, "1#test2", false);
  68. return 0;
  69. }
  70. DM_TEST(dm_test_part, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);