]> Git Repo - u-boot.git/blob - test/dm/part.c
Revert "Merge patch series "arm: dts: am62-beagleplay: Fix Beagleplay Ethernet""
[u-boot.git] / test / dm / part.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2020 Sean Anderson <[email protected]>
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
14 static int do_test(struct unit_test_state *uts, int expected,
15                    const char *part_str, bool whole)
16 {
17         struct blk_desc *mmc_dev_desc;
18         struct disk_partition part_info;
19
20         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);
26         return 0;
27 }
28
29 static 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;
35         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
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);
53         }
54         ut_assertok(gpt_restore(mmc_dev_desc, str_disk_guid, parts,
55                                 ARRAY_SIZE(parts)));
56
57         oldbootdevice = env_get("bootdevice");
58
59 #define test(expected, part_str, whole) \
60         ut_assertok(do_test(uts, expected, part_str, whole))
61
62         env_set("bootdevice", NULL);
63         test(-ENODEV, NULL, true);
64         test(-ENODEV, "", true);
65         env_set("bootdevice", "0");
66         test(0, NULL, true);
67         test(0, "", true);
68         env_set("bootdevice", "2");
69         test(1, NULL, false);
70         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);
79         test(1, "2", false);
80         test(1, "2", true);
81         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);
90         test(1, "2#test1", false);
91         test(2, "2#test2", false);
92         ret = 0;
93
94         env_set("bootdevice", oldbootdevice);
95         return ret;
96 }
97 DM_TEST(dm_test_part, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
98
99 static 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 }
110 DM_TEST(dm_test_part_bootable, UT_TESTF_SCAN_FDT);
111
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)
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
134 static 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 }
197 DM_TEST(dm_test_part_get_info_by_type, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
This page took 0.034868 seconds and 4 git commands to generate.