1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2015 Google, Inc
4  */
5 
6 #include <common.h>
7 #include <dm.h>
8 #include <fastboot.h>
9 #include <fb_mmc.h>
10 #include <mmc.h>
11 #include <part.h>
12 #include <part_efi.h>
13 #include <dm/test.h>
14 #include <test/ut.h>
15 #include <linux/stringify.h>
16 
17 #define FB_ALIAS_PREFIX "fastboot_partition_alias_"
18 
dm_test_fastboot_mmc_part(struct unit_test_state * uts)19 static int dm_test_fastboot_mmc_part(struct unit_test_state *uts)
20 {
21 	char response[FASTBOOT_RESPONSE_LEN] = {0};
22 	char str_disk_guid[UUID_STR_LEN + 1];
23 	struct blk_desc *mmc_dev_desc, *fb_dev_desc;
24 	struct disk_partition part_info;
25 	struct disk_partition parts[2] = {
26 		{
27 			.start = 48, /* GPT data takes up the first 34 blocks or so */
28 			.size = 1,
29 			.name = "test1",
30 		},
31 		{
32 			.start = 49,
33 			.size = 1,
34 			.name = "test2",
35 		},
36 	};
37 
38 	/*
39 	 * There are a lot of literal 0s I don't want to have to construct from
40 	 * MMC_DEV.
41 	 */
42 	ut_asserteq(0, CONFIG_FASTBOOT_FLASH_MMC_DEV);
43 	ut_assertok(blk_get_device_by_str("mmc", "0", &mmc_dev_desc));
44 	if (CONFIG_IS_ENABLED(RANDOM_UUID)) {
45 		gen_rand_uuid_str(parts[0].uuid, UUID_STR_FORMAT_STD);
46 		gen_rand_uuid_str(parts[1].uuid, UUID_STR_FORMAT_STD);
47 		gen_rand_uuid_str(str_disk_guid, UUID_STR_FORMAT_STD);
48 	}
49 	ut_assertok(gpt_restore(mmc_dev_desc, str_disk_guid, parts,
50 				ARRAY_SIZE(parts)));
51 
52 	/* "Classic" partition labels */
53 	ut_asserteq(1, fastboot_mmc_get_part_info("test1", &fb_dev_desc,
54 						  &part_info, response));
55 	ut_asserteq(2, fastboot_mmc_get_part_info("test2", &fb_dev_desc,
56 						  &part_info, response));
57 
58 	/* Test aliases */
59 	ut_assertnull(env_get(FB_ALIAS_PREFIX "test3"));
60 	ut_assertok(env_set(FB_ALIAS_PREFIX "test3", "test1"));
61 	ut_asserteq(1, fastboot_mmc_get_part_info("test3", &fb_dev_desc,
62 						  &part_info, response));
63 	ut_assertok(env_set(FB_ALIAS_PREFIX "test3", NULL));
64 
65 	/* "New" partition labels */
66 	ut_asserteq(1, fastboot_mmc_get_part_info("#test1", &fb_dev_desc,
67 						  &part_info, response));
68 	ut_asserteq(1, fastboot_mmc_get_part_info("0#test1", &fb_dev_desc,
69 						  &part_info, response));
70 	ut_asserteq(1, fastboot_mmc_get_part_info("0.0#test1", &fb_dev_desc,
71 						  &part_info, response));
72 	ut_asserteq(1, fastboot_mmc_get_part_info("0:1", &fb_dev_desc,
73 						  &part_info, response));
74 	ut_asserteq(1, fastboot_mmc_get_part_info("0.0:1", &fb_dev_desc,
75 						  &part_info, response));
76 	ut_asserteq(1, fastboot_mmc_get_part_info("0", &fb_dev_desc,
77 						  &part_info, response));
78 	ut_asserteq(1, fastboot_mmc_get_part_info("0.0", &fb_dev_desc,
79 						  &part_info, response));
80 	ut_asserteq(0, fastboot_mmc_get_part_info("0:0", &fb_dev_desc,
81 						  &part_info, response));
82 	ut_asserteq(0, fastboot_mmc_get_part_info("0.0:0", &fb_dev_desc,
83 						  &part_info, response));
84 	ut_asserteq(0, fastboot_mmc_get_part_info("1", &fb_dev_desc,
85 						  &part_info, response));
86 	ut_asserteq(0, fastboot_mmc_get_part_info("1.0", &fb_dev_desc,
87 						  &part_info, response));
88 	ut_asserteq(1, fastboot_mmc_get_part_info(":1", &fb_dev_desc,
89 						  &part_info, response));
90 	ut_asserteq(0, fastboot_mmc_get_part_info(":0", &fb_dev_desc,
91 						  &part_info, response));
92 
93 	return 0;
94 }
95 DM_TEST(dm_test_fastboot_mmc_part, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
96