blob: cabb31d18ca04236eed1ffaf4288eafdebd4f741 [file] [log] [blame]
Sean Anderson619a1822021-04-12 18:53:07 -04001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2020 Sean Anderson <sean.anderson@seco.com>
4 */
5
Sean Anderson619a1822021-04-12 18:53:07 -04006#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
Simon Glass68a56ba2023-01-17 10:47:13 -070013static int do_test(struct unit_test_state *uts, int expected,
14 const char *part_str, bool whole)
Sean Andersoncecd47c2021-05-15 14:13:54 -040015{
16 struct blk_desc *mmc_dev_desc;
17 struct disk_partition part_info;
18
Joshua Watt2c1045c2023-07-03 08:39:53 -050019 int ret = part_get_info_by_dev_and_name_or_num("mmc", part_str,
20 &mmc_dev_desc,
21 &part_info, whole);
22
23 ut_assertf(expected == ret, "test(%d, \"%s\", %d) == %d", expected,
24 part_str, whole, ret);
Sean Andersoncecd47c2021-05-15 14:13:54 -040025 return 0;
26}
27
Sean Anderson619a1822021-04-12 18:53:07 -040028static int dm_test_part(struct unit_test_state *uts)
29{
Sean Andersoncecd47c2021-05-15 14:13:54 -040030 char *oldbootdevice;
Sean Anderson619a1822021-04-12 18:53:07 -040031 char str_disk_guid[UUID_STR_LEN + 1];
Sean Andersoncecd47c2021-05-15 14:13:54 -040032 int ret;
Sean Anderson619a1822021-04-12 18:53:07 -040033 struct blk_desc *mmc_dev_desc;
Sean Anderson619a1822021-04-12 18:53:07 -040034 struct disk_partition parts[2] = {
35 {
36 .start = 48, /* GPT data takes up the first 34 blocks or so */
37 .size = 1,
38 .name = "test1",
39 },
40 {
41 .start = 49,
42 .size = 1,
43 .name = "test2",
44 },
45 };
46
Simon Glass29bfcd62023-01-17 10:47:22 -070047 ut_asserteq(2, blk_get_device_by_str("mmc", "2", &mmc_dev_desc));
Sean Anderson619a1822021-04-12 18:53:07 -040048 if (CONFIG_IS_ENABLED(RANDOM_UUID)) {
49 gen_rand_uuid_str(parts[0].uuid, UUID_STR_FORMAT_STD);
50 gen_rand_uuid_str(parts[1].uuid, UUID_STR_FORMAT_STD);
51 gen_rand_uuid_str(str_disk_guid, UUID_STR_FORMAT_STD);
52 }
53 ut_assertok(gpt_restore(mmc_dev_desc, str_disk_guid, parts,
54 ARRAY_SIZE(parts)));
55
Sean Andersoncecd47c2021-05-15 14:13:54 -040056 oldbootdevice = env_get("bootdevice");
Sean Anderson619a1822021-04-12 18:53:07 -040057
Simon Glass68a56ba2023-01-17 10:47:13 -070058#define test(expected, part_str, whole) \
59 ut_assertok(do_test(uts, expected, part_str, whole))
Sean Andersoncecd47c2021-05-15 14:13:54 -040060
61 env_set("bootdevice", NULL);
62 test(-ENODEV, NULL, true);
Sean Anderson619a1822021-04-12 18:53:07 -040063 test(-ENODEV, "", true);
64 env_set("bootdevice", "0");
Sean Andersoncecd47c2021-05-15 14:13:54 -040065 test(0, NULL, true);
Sean Anderson619a1822021-04-12 18:53:07 -040066 test(0, "", true);
Simon Glass29bfcd62023-01-17 10:47:22 -070067 env_set("bootdevice", "2");
Sean Andersoncecd47c2021-05-15 14:13:54 -040068 test(1, NULL, false);
Sean Anderson619a1822021-04-12 18:53:07 -040069 test(1, "", false);
70 test(1, "-", false);
71 env_set("bootdevice", "");
72 test(-EPROTONOSUPPORT, "0", false);
73 test(0, "0", true);
74 test(0, ":0", true);
75 test(0, ".0", true);
76 test(0, ".0:0", true);
77 test(-EINVAL, "#test1", true);
Simon Glass29bfcd62023-01-17 10:47:22 -070078 test(1, "2", false);
79 test(1, "2", true);
Joshua Watt3e1b52d2023-07-03 08:39:52 -050080 test(-ENOENT, "2:0", false);
81 test(0, "2:0", true);
82 test(1, "2:1", false);
83 test(2, "2:2", false);
84 test(1, "2.0", false);
85 test(0, "2.0:0", true);
86 test(1, "2.0:1", false);
87 test(2, "2.0:2", false);
88 test(-EINVAL, "2#bogus", false);
Simon Glass29bfcd62023-01-17 10:47:22 -070089 test(1, "2#test1", false);
90 test(2, "2#test2", false);
Sean Andersoncecd47c2021-05-15 14:13:54 -040091 ret = 0;
Sean Anderson619a1822021-04-12 18:53:07 -040092
Sean Andersoncecd47c2021-05-15 14:13:54 -040093 env_set("bootdevice", oldbootdevice);
94 return ret;
Sean Anderson619a1822021-04-12 18:53:07 -040095}
96DM_TEST(dm_test_part, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassf5e2df02023-01-17 10:47:41 -070097
98static int dm_test_part_bootable(struct unit_test_state *uts)
99{
100 struct blk_desc *desc;
101 struct udevice *dev;
102
103 ut_assertok(uclass_get_device_by_name(UCLASS_BLK, "mmc1.blk", &dev));
104 desc = dev_get_uclass_plat(dev);
105 ut_asserteq(1, part_get_bootable(desc));
106
107 return 0;
108}
109DM_TEST(dm_test_part_bootable, UT_TESTF_SCAN_FDT);
Joshua Watte40802e2023-07-03 08:39:55 -0500110
111static int do_get_info_test(struct unit_test_state *uts,
112 struct blk_desc *dev_desc, int part, int part_type,
113 struct disk_partition const *reference)
114{
115 struct disk_partition p;
116 int ret;
117
118 memset(&p, 0, sizeof(p));
119
120 ret = part_get_info_by_type(dev_desc, part, part_type, &p);
121 printf("part_get_info_by_type(%d, 0x%x) = %d\n", part, part_type, ret);
122 if (ut_assertok(ret)) {
123 return 0;
124 }
125
126 ut_asserteq(reference->start, p.start);
127 ut_asserteq(reference->size, p.size);
128 ut_asserteq(reference->sys_ind, p.sys_ind);
129
130 return 0;
131}
132
133static int dm_test_part_get_info_by_type(struct unit_test_state *uts)
134{
135 char str_disk_guid[UUID_STR_LEN + 1];
136 struct blk_desc *mmc_dev_desc;
137 struct disk_partition gpt_parts[] = {
138 {
139 .start = 48, /* GPT data takes up the first 34 blocks or so */
140 .size = 1,
141 .name = "test1",
142 .sys_ind = 0,
143 },
144 {
145 .start = 49,
146 .size = 1,
147 .name = "test2",
148 .sys_ind = 0,
149 },
150 };
151 struct disk_partition mbr_parts[] = {
152 {
153 .start = 1,
154 .size = 33,
155 .name = "gpt",
156 .sys_ind = EFI_PMBR_OSTYPE_EFI_GPT,
157 },
158 {
159 .start = 48,
160 .size = 1,
161 .name = "test1",
162 .sys_ind = 0x83,
163 },
164 };
165
166 ut_asserteq(2, blk_get_device_by_str("mmc", "2", &mmc_dev_desc));
167 if (CONFIG_IS_ENABLED(RANDOM_UUID)) {
168 gen_rand_uuid_str(gpt_parts[0].uuid, UUID_STR_FORMAT_STD);
169 gen_rand_uuid_str(gpt_parts[1].uuid, UUID_STR_FORMAT_STD);
170 gen_rand_uuid_str(str_disk_guid, UUID_STR_FORMAT_STD);
171 }
172 ut_assertok(gpt_restore(mmc_dev_desc, str_disk_guid, gpt_parts,
173 ARRAY_SIZE(gpt_parts)));
174
175 ut_assertok(write_mbr_partitions(mmc_dev_desc, mbr_parts,
176 ARRAY_SIZE(mbr_parts), 0));
177
178#define get_info_test(_part, _part_type, _reference) \
179 ut_assertok(do_get_info_test(uts, mmc_dev_desc, _part, _part_type, \
180 _reference))
181
182 for (int i = 0; i < ARRAY_SIZE(gpt_parts); i++) {
183 get_info_test(i + 1, PART_TYPE_UNKNOWN, &gpt_parts[i]);
184 }
185
186 for (int i = 0; i < ARRAY_SIZE(mbr_parts); i++) {
187 get_info_test(i + 1, PART_TYPE_DOS, &mbr_parts[i]);
188 }
189
190 for (int i = 0; i < ARRAY_SIZE(gpt_parts); i++) {
191 get_info_test(i + 1, PART_TYPE_EFI, &gpt_parts[i]);
192 }
193
194 return 0;
195}
196DM_TEST(dm_test_part_get_info_by_type, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);