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