1 // SPDX-License-Identifier: GPL-2.0+
14 static int do_test(struct unit_test_state *uts, int expected,
15 const char *part_str, bool whole)
17 struct blk_desc *mmc_dev_desc;
18 struct disk_partition part_info;
20 int ret = part_get_info_by_dev_and_name_or_num("mmc", part_str,
24 ut_assertf(expected == ret, "test(%d, \"%s\", %d) == %d", expected,
25 part_str, whole, ret);
29 static int dm_test_part(struct unit_test_state *uts)
32 char str_disk_guid[UUID_STR_LEN + 1];
34 struct blk_desc *mmc_dev_desc;
35 struct disk_partition parts[2] = {
37 .start = 48, /* GPT data takes up the first 34 blocks or so */
48 ut_asserteq(2, blk_get_device_by_str("mmc", "2", &mmc_dev_desc));
49 if (CONFIG_IS_ENABLED(RANDOM_UUID)) {
50 gen_rand_uuid_str(parts[0].uuid, UUID_STR_FORMAT_STD);
51 gen_rand_uuid_str(parts[1].uuid, UUID_STR_FORMAT_STD);
52 gen_rand_uuid_str(str_disk_guid, UUID_STR_FORMAT_STD);
54 ut_assertok(gpt_restore(mmc_dev_desc, str_disk_guid, parts,
57 oldbootdevice = env_get("bootdevice");
59 #define test(expected, part_str, whole) \
60 ut_assertok(do_test(uts, expected, part_str, whole))
62 env_set("bootdevice", NULL);
63 test(-ENODEV, NULL, true);
64 test(-ENODEV, "", true);
65 env_set("bootdevice", "0");
68 env_set("bootdevice", "2");
72 env_set("bootdevice", "");
73 test(-EPROTONOSUPPORT, "0", false);
77 test(0, ".0:0", true);
78 test(-EINVAL, "#test1", true);
81 test(-ENOENT, "2:0", false);
83 test(1, "2:1", false);
84 test(2, "2:2", false);
85 test(1, "2.0", false);
86 test(0, "2.0:0", true);
87 test(1, "2.0:1", false);
88 test(2, "2.0:2", false);
89 test(-EINVAL, "2#bogus", false);
90 test(1, "2#test1", false);
91 test(2, "2#test2", false);
94 env_set("bootdevice", oldbootdevice);
97 DM_TEST(dm_test_part, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
99 static int dm_test_part_bootable(struct unit_test_state *uts)
101 struct blk_desc *desc;
104 ut_assertok(uclass_get_device_by_name(UCLASS_BLK, "mmc1.blk", &dev));
105 desc = dev_get_uclass_plat(dev);
106 ut_asserteq(1, part_get_bootable(desc));
110 DM_TEST(dm_test_part_bootable, UT_TESTF_SCAN_FDT);
112 static int do_get_info_test(struct unit_test_state *uts,
113 struct blk_desc *dev_desc, int part, int part_type,
114 struct disk_partition const *reference)
116 struct disk_partition p;
119 memset(&p, 0, sizeof(p));
121 ret = part_get_info_by_type(dev_desc, part, part_type, &p);
122 printf("part_get_info_by_type(%d, 0x%x) = %d\n", part, part_type, ret);
123 if (ut_assertok(ret)) {
127 ut_asserteq(reference->start, p.start);
128 ut_asserteq(reference->size, p.size);
129 ut_asserteq(reference->sys_ind, p.sys_ind);
134 static int dm_test_part_get_info_by_type(struct unit_test_state *uts)
136 char str_disk_guid[UUID_STR_LEN + 1];
137 struct blk_desc *mmc_dev_desc;
138 struct disk_partition gpt_parts[] = {
140 .start = 48, /* GPT data takes up the first 34 blocks or so */
152 struct disk_partition mbr_parts[] = {
157 .sys_ind = EFI_PMBR_OSTYPE_EFI_GPT,
167 ut_asserteq(2, blk_get_device_by_str("mmc", "2", &mmc_dev_desc));
168 if (CONFIG_IS_ENABLED(RANDOM_UUID)) {
169 gen_rand_uuid_str(gpt_parts[0].uuid, UUID_STR_FORMAT_STD);
170 gen_rand_uuid_str(gpt_parts[1].uuid, UUID_STR_FORMAT_STD);
171 gen_rand_uuid_str(str_disk_guid, UUID_STR_FORMAT_STD);
173 ut_assertok(gpt_restore(mmc_dev_desc, str_disk_guid, gpt_parts,
174 ARRAY_SIZE(gpt_parts)));
176 ut_assertok(write_mbr_partitions(mmc_dev_desc, mbr_parts,
177 ARRAY_SIZE(mbr_parts), 0));
179 #define get_info_test(_part, _part_type, _reference) \
180 ut_assertok(do_get_info_test(uts, mmc_dev_desc, _part, _part_type, \
183 for (int i = 0; i < ARRAY_SIZE(gpt_parts); i++) {
184 get_info_test(i + 1, PART_TYPE_UNKNOWN, &gpt_parts[i]);
187 for (int i = 0; i < ARRAY_SIZE(mbr_parts); i++) {
188 get_info_test(i + 1, PART_TYPE_DOS, &mbr_parts[i]);
191 for (int i = 0; i < ARRAY_SIZE(gpt_parts); i++) {
192 get_info_test(i + 1, PART_TYPE_EFI, &gpt_parts[i]);
197 DM_TEST(dm_test_part_get_info_by_type, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);