Sean Anderson | 619a182 | 2021-04-12 18:53:07 -0400 | [diff] [blame] | 1 | // 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 Glass | 68a56ba | 2023-01-17 10:47:13 -0700 | [diff] [blame] | 14 | static int do_test(struct unit_test_state *uts, int expected, |
| 15 | const char *part_str, bool whole) |
Sean Anderson | cecd47c | 2021-05-15 14:13:54 -0400 | [diff] [blame] | 16 | { |
| 17 | struct blk_desc *mmc_dev_desc; |
| 18 | struct disk_partition part_info; |
| 19 | |
Joshua Watt | 2c1045c | 2023-07-03 08:39:53 -0500 | [diff] [blame] | 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); |
Sean Anderson | cecd47c | 2021-05-15 14:13:54 -0400 | [diff] [blame] | 26 | return 0; |
| 27 | } |
| 28 | |
Sean Anderson | 619a182 | 2021-04-12 18:53:07 -0400 | [diff] [blame] | 29 | static int dm_test_part(struct unit_test_state *uts) |
| 30 | { |
Sean Anderson | cecd47c | 2021-05-15 14:13:54 -0400 | [diff] [blame] | 31 | char *oldbootdevice; |
Sean Anderson | 619a182 | 2021-04-12 18:53:07 -0400 | [diff] [blame] | 32 | char str_disk_guid[UUID_STR_LEN + 1]; |
Sean Anderson | cecd47c | 2021-05-15 14:13:54 -0400 | [diff] [blame] | 33 | int ret; |
Sean Anderson | 619a182 | 2021-04-12 18:53:07 -0400 | [diff] [blame] | 34 | struct blk_desc *mmc_dev_desc; |
Sean Anderson | 619a182 | 2021-04-12 18:53:07 -0400 | [diff] [blame] | 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 | |
Simon Glass | 29bfcd6 | 2023-01-17 10:47:22 -0700 | [diff] [blame] | 48 | ut_asserteq(2, blk_get_device_by_str("mmc", "2", &mmc_dev_desc)); |
Sean Anderson | 619a182 | 2021-04-12 18:53:07 -0400 | [diff] [blame] | 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 | |
Sean Anderson | cecd47c | 2021-05-15 14:13:54 -0400 | [diff] [blame] | 57 | oldbootdevice = env_get("bootdevice"); |
Sean Anderson | 619a182 | 2021-04-12 18:53:07 -0400 | [diff] [blame] | 58 | |
Simon Glass | 68a56ba | 2023-01-17 10:47:13 -0700 | [diff] [blame] | 59 | #define test(expected, part_str, whole) \ |
| 60 | ut_assertok(do_test(uts, expected, part_str, whole)) |
Sean Anderson | cecd47c | 2021-05-15 14:13:54 -0400 | [diff] [blame] | 61 | |
| 62 | env_set("bootdevice", NULL); |
| 63 | test(-ENODEV, NULL, true); |
Sean Anderson | 619a182 | 2021-04-12 18:53:07 -0400 | [diff] [blame] | 64 | test(-ENODEV, "", true); |
| 65 | env_set("bootdevice", "0"); |
Sean Anderson | cecd47c | 2021-05-15 14:13:54 -0400 | [diff] [blame] | 66 | test(0, NULL, true); |
Sean Anderson | 619a182 | 2021-04-12 18:53:07 -0400 | [diff] [blame] | 67 | test(0, "", true); |
Simon Glass | 29bfcd6 | 2023-01-17 10:47:22 -0700 | [diff] [blame] | 68 | env_set("bootdevice", "2"); |
Sean Anderson | cecd47c | 2021-05-15 14:13:54 -0400 | [diff] [blame] | 69 | test(1, NULL, false); |
Sean Anderson | 619a182 | 2021-04-12 18:53:07 -0400 | [diff] [blame] | 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); |
Simon Glass | 29bfcd6 | 2023-01-17 10:47:22 -0700 | [diff] [blame] | 79 | test(1, "2", false); |
| 80 | test(1, "2", true); |
Joshua Watt | 3e1b52d | 2023-07-03 08:39:52 -0500 | [diff] [blame] | 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); |
Simon Glass | 29bfcd6 | 2023-01-17 10:47:22 -0700 | [diff] [blame] | 90 | test(1, "2#test1", false); |
| 91 | test(2, "2#test2", false); |
Sean Anderson | cecd47c | 2021-05-15 14:13:54 -0400 | [diff] [blame] | 92 | ret = 0; |
Sean Anderson | 619a182 | 2021-04-12 18:53:07 -0400 | [diff] [blame] | 93 | |
Sean Anderson | cecd47c | 2021-05-15 14:13:54 -0400 | [diff] [blame] | 94 | env_set("bootdevice", oldbootdevice); |
| 95 | return ret; |
Sean Anderson | 619a182 | 2021-04-12 18:53:07 -0400 | [diff] [blame] | 96 | } |
| 97 | DM_TEST(dm_test_part, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | f5e2df0 | 2023-01-17 10:47:41 -0700 | [diff] [blame] | 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); |
Joshua Watt | e40802e | 2023-07-03 08:39:55 -0500 | [diff] [blame] | 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); |