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 | |
| 20 | ut_asserteq(expected, |
| 21 | part_get_info_by_dev_and_name_or_num("mmc", part_str, |
| 22 | &mmc_dev_desc, |
| 23 | &part_info, whole)); |
| 24 | return 0; |
| 25 | } |
| 26 | |
Sean Anderson | 619a182 | 2021-04-12 18:53:07 -0400 | [diff] [blame] | 27 | static int dm_test_part(struct unit_test_state *uts) |
| 28 | { |
Sean Anderson | cecd47c | 2021-05-15 14:13:54 -0400 | [diff] [blame] | 29 | char *oldbootdevice; |
Sean Anderson | 619a182 | 2021-04-12 18:53:07 -0400 | [diff] [blame] | 30 | char str_disk_guid[UUID_STR_LEN + 1]; |
Sean Anderson | cecd47c | 2021-05-15 14:13:54 -0400 | [diff] [blame] | 31 | int ret; |
Sean Anderson | 619a182 | 2021-04-12 18:53:07 -0400 | [diff] [blame] | 32 | struct blk_desc *mmc_dev_desc; |
Sean Anderson | 619a182 | 2021-04-12 18:53:07 -0400 | [diff] [blame] | 33 | struct disk_partition parts[2] = { |
| 34 | { |
| 35 | .start = 48, /* GPT data takes up the first 34 blocks or so */ |
| 36 | .size = 1, |
| 37 | .name = "test1", |
| 38 | }, |
| 39 | { |
| 40 | .start = 49, |
| 41 | .size = 1, |
| 42 | .name = "test2", |
| 43 | }, |
| 44 | }; |
| 45 | |
Simon Glass | 29bfcd6 | 2023-01-17 10:47:22 -0700 | [diff] [blame] | 46 | 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] | 47 | if (CONFIG_IS_ENABLED(RANDOM_UUID)) { |
| 48 | gen_rand_uuid_str(parts[0].uuid, UUID_STR_FORMAT_STD); |
| 49 | gen_rand_uuid_str(parts[1].uuid, UUID_STR_FORMAT_STD); |
| 50 | gen_rand_uuid_str(str_disk_guid, UUID_STR_FORMAT_STD); |
| 51 | } |
| 52 | ut_assertok(gpt_restore(mmc_dev_desc, str_disk_guid, parts, |
| 53 | ARRAY_SIZE(parts))); |
| 54 | |
Sean Anderson | cecd47c | 2021-05-15 14:13:54 -0400 | [diff] [blame] | 55 | oldbootdevice = env_get("bootdevice"); |
Sean Anderson | 619a182 | 2021-04-12 18:53:07 -0400 | [diff] [blame] | 56 | |
Simon Glass | 68a56ba | 2023-01-17 10:47:13 -0700 | [diff] [blame] | 57 | #define test(expected, part_str, whole) \ |
| 58 | ut_assertok(do_test(uts, expected, part_str, whole)) |
Sean Anderson | cecd47c | 2021-05-15 14:13:54 -0400 | [diff] [blame] | 59 | |
| 60 | env_set("bootdevice", NULL); |
| 61 | test(-ENODEV, NULL, true); |
Sean Anderson | 619a182 | 2021-04-12 18:53:07 -0400 | [diff] [blame] | 62 | test(-ENODEV, "", true); |
| 63 | env_set("bootdevice", "0"); |
Sean Anderson | cecd47c | 2021-05-15 14:13:54 -0400 | [diff] [blame] | 64 | test(0, NULL, true); |
Sean Anderson | 619a182 | 2021-04-12 18:53:07 -0400 | [diff] [blame] | 65 | test(0, "", true); |
Simon Glass | 29bfcd6 | 2023-01-17 10:47:22 -0700 | [diff] [blame] | 66 | env_set("bootdevice", "2"); |
Sean Anderson | cecd47c | 2021-05-15 14:13:54 -0400 | [diff] [blame] | 67 | test(1, NULL, false); |
Sean Anderson | 619a182 | 2021-04-12 18:53:07 -0400 | [diff] [blame] | 68 | test(1, "", false); |
| 69 | test(1, "-", false); |
| 70 | env_set("bootdevice", ""); |
| 71 | test(-EPROTONOSUPPORT, "0", false); |
| 72 | test(0, "0", true); |
| 73 | test(0, ":0", true); |
| 74 | test(0, ".0", true); |
| 75 | test(0, ".0:0", true); |
| 76 | test(-EINVAL, "#test1", true); |
Simon Glass | 29bfcd6 | 2023-01-17 10:47:22 -0700 | [diff] [blame] | 77 | test(1, "2", false); |
| 78 | test(1, "2", true); |
Sean Anderson | 619a182 | 2021-04-12 18:53:07 -0400 | [diff] [blame] | 79 | test(-ENOENT, "1:0", false); |
| 80 | test(0, "1:0", true); |
| 81 | test(1, "1:1", false); |
| 82 | test(2, "1:2", false); |
| 83 | test(1, "1.0", false); |
| 84 | test(0, "1.0:0", true); |
| 85 | test(1, "1.0:1", false); |
| 86 | test(2, "1.0:2", false); |
| 87 | test(-EINVAL, "1#bogus", false); |
Simon Glass | 29bfcd6 | 2023-01-17 10:47:22 -0700 | [diff] [blame] | 88 | test(1, "2#test1", false); |
| 89 | test(2, "2#test2", false); |
Sean Anderson | cecd47c | 2021-05-15 14:13:54 -0400 | [diff] [blame] | 90 | ret = 0; |
Sean Anderson | 619a182 | 2021-04-12 18:53:07 -0400 | [diff] [blame] | 91 | |
Sean Anderson | cecd47c | 2021-05-15 14:13:54 -0400 | [diff] [blame] | 92 | env_set("bootdevice", oldbootdevice); |
| 93 | return ret; |
Sean Anderson | 619a182 | 2021-04-12 18:53:07 -0400 | [diff] [blame] | 94 | } |
| 95 | 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] | 96 | |
| 97 | static int dm_test_part_bootable(struct unit_test_state *uts) |
| 98 | { |
| 99 | struct blk_desc *desc; |
| 100 | struct udevice *dev; |
| 101 | |
| 102 | ut_assertok(uclass_get_device_by_name(UCLASS_BLK, "mmc1.blk", &dev)); |
| 103 | desc = dev_get_uclass_plat(dev); |
| 104 | ut_asserteq(1, part_get_bootable(desc)); |
| 105 | |
| 106 | return 0; |
| 107 | } |
| 108 | DM_TEST(dm_test_part_bootable, UT_TESTF_SCAN_FDT); |