blob: d6e4345812748f6d12e85db8061dce47a2b9c7cd [file] [log] [blame]
Sean Anderson535e7002021-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
6#include <common.h>
7#include <dm.h>
8#include <mmc.h>
9#include <part.h>
10#include <part_efi.h>
11#include <dm/test.h>
12#include <test/ut.h>
13
Simon Glassa0fb9de2023-01-17 10:47:13 -070014static int do_test(struct unit_test_state *uts, int expected,
15 const char *part_str, bool whole)
Sean Anderson535e7002021-04-12 18:53:07 -040016{
Sean Anderson535e7002021-04-12 18:53:07 -040017 struct blk_desc *mmc_dev_desc;
18 struct disk_partition part_info;
Sean Anderson26de4292021-05-15 14:13:54 -040019
Joshua Watt44ef2852023-07-03 08:39:53 -050020 int ret = part_get_info_by_dev_and_name_or_num("mmc", part_str,
21 &mmc_dev_desc,
22 &part_info, whole);
23
24 ut_assertf(expected == ret, "test(%d, \"%s\", %d) == %d", expected,
25 part_str, whole, ret);
Sean Anderson26de4292021-05-15 14:13:54 -040026 return 0;
27}
28
29static int dm_test_part(struct unit_test_state *uts)
30{
31 char *oldbootdevice;
32 char str_disk_guid[UUID_STR_LEN + 1];
33 int ret;
34 struct blk_desc *mmc_dev_desc;
Sean Anderson535e7002021-04-12 18:53:07 -040035 struct disk_partition parts[2] = {
36 {
37 .start = 48, /* GPT data takes up the first 34 blocks or so */
38 .size = 1,
39 .name = "test1",
40 },
41 {
42 .start = 49,
43 .size = 1,
44 .name = "test2",
45 },
46 };
47
Simon Glassd94d9842023-01-17 10:47:22 -070048 ut_asserteq(2, blk_get_device_by_str("mmc", "2", &mmc_dev_desc));
Sean Anderson535e7002021-04-12 18:53:07 -040049 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);
53 }
54 ut_assertok(gpt_restore(mmc_dev_desc, str_disk_guid, parts,
55 ARRAY_SIZE(parts)));
56
Sean Anderson26de4292021-05-15 14:13:54 -040057 oldbootdevice = env_get("bootdevice");
Sean Anderson535e7002021-04-12 18:53:07 -040058
Simon Glassa0fb9de2023-01-17 10:47:13 -070059#define test(expected, part_str, whole) \
60 ut_assertok(do_test(uts, expected, part_str, whole))
Sean Anderson26de4292021-05-15 14:13:54 -040061
62 env_set("bootdevice", NULL);
63 test(-ENODEV, NULL, true);
Sean Anderson535e7002021-04-12 18:53:07 -040064 test(-ENODEV, "", true);
65 env_set("bootdevice", "0");
Sean Anderson26de4292021-05-15 14:13:54 -040066 test(0, NULL, true);
Sean Anderson535e7002021-04-12 18:53:07 -040067 test(0, "", true);
Simon Glassd94d9842023-01-17 10:47:22 -070068 env_set("bootdevice", "2");
Sean Anderson26de4292021-05-15 14:13:54 -040069 test(1, NULL, false);
Sean Anderson535e7002021-04-12 18:53:07 -040070 test(1, "", false);
71 test(1, "-", false);
72 env_set("bootdevice", "");
73 test(-EPROTONOSUPPORT, "0", false);
74 test(0, "0", true);
75 test(0, ":0", true);
76 test(0, ".0", true);
77 test(0, ".0:0", true);
78 test(-EINVAL, "#test1", true);
Simon Glassd94d9842023-01-17 10:47:22 -070079 test(1, "2", false);
80 test(1, "2", true);
Joshua Watt19c961e2023-07-03 08:39:52 -050081 test(-ENOENT, "2:0", false);
82 test(0, "2:0", true);
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);
Simon Glassd94d9842023-01-17 10:47:22 -070090 test(1, "2#test1", false);
91 test(2, "2#test2", false);
Sean Anderson26de4292021-05-15 14:13:54 -040092 ret = 0;
Sean Anderson535e7002021-04-12 18:53:07 -040093
Sean Anderson26de4292021-05-15 14:13:54 -040094 env_set("bootdevice", oldbootdevice);
95 return ret;
Sean Anderson535e7002021-04-12 18:53:07 -040096}
97DM_TEST(dm_test_part, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassdcffa442023-01-17 10:47:41 -070098
99static int dm_test_part_bootable(struct unit_test_state *uts)
100{
101 struct blk_desc *desc;
102 struct udevice *dev;
103
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));
107
108 return 0;
109}
110DM_TEST(dm_test_part_bootable, UT_TESTF_SCAN_FDT);
Joshua Watt95811662023-07-03 08:39:55 -0500111
112static 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)
115{
116 struct disk_partition p;
117 int ret;
118
119 memset(&p, 0, sizeof(p));
120
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)) {
124 return 0;
125 }
126
127 ut_asserteq(reference->start, p.start);
128 ut_asserteq(reference->size, p.size);
129 ut_asserteq(reference->sys_ind, p.sys_ind);
130
131 return 0;
132}
133
134static int dm_test_part_get_info_by_type(struct unit_test_state *uts)
135{
136 char str_disk_guid[UUID_STR_LEN + 1];
137 struct blk_desc *mmc_dev_desc;
138 struct disk_partition gpt_parts[] = {
139 {
140 .start = 48, /* GPT data takes up the first 34 blocks or so */
141 .size = 1,
142 .name = "test1",
143 .sys_ind = 0,
144 },
145 {
146 .start = 49,
147 .size = 1,
148 .name = "test2",
149 .sys_ind = 0,
150 },
151 };
152 struct disk_partition mbr_parts[] = {
153 {
154 .start = 1,
155 .size = 33,
156 .name = "gpt",
157 .sys_ind = EFI_PMBR_OSTYPE_EFI_GPT,
158 },
159 {
160 .start = 48,
161 .size = 1,
162 .name = "test1",
163 .sys_ind = 0x83,
164 },
165 };
166
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);
172 }
173 ut_assertok(gpt_restore(mmc_dev_desc, str_disk_guid, gpt_parts,
174 ARRAY_SIZE(gpt_parts)));
175
176 ut_assertok(write_mbr_partitions(mmc_dev_desc, mbr_parts,
177 ARRAY_SIZE(mbr_parts), 0));
178
179#define get_info_test(_part, _part_type, _reference) \
180 ut_assertok(do_get_info_test(uts, mmc_dev_desc, _part, _part_type, \
181 _reference))
182
183 for (int i = 0; i < ARRAY_SIZE(gpt_parts); i++) {
184 get_info_test(i + 1, PART_TYPE_UNKNOWN, &gpt_parts[i]);
185 }
186
187 for (int i = 0; i < ARRAY_SIZE(mbr_parts); i++) {
188 get_info_test(i + 1, PART_TYPE_DOS, &mbr_parts[i]);
189 }
190
191 for (int i = 0; i < ARRAY_SIZE(gpt_parts); i++) {
192 get_info_test(i + 1, PART_TYPE_EFI, &gpt_parts[i]);
193 }
194
195 return 0;
196}
197DM_TEST(dm_test_part_get_info_by_type, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);