1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Test for bootdev functions. All start with 'bootdev'
4  *
5  * Copyright 2021 Google LLC
6  * Written by Simon Glass <sjg@chromium.org>
7  */
8 
9 #include <bootdev.h>
10 #include <bootstd.h>
11 #include <dm.h>
12 #include <memalign.h>
13 #include <mmc.h>
14 #include <usb.h>
15 #include <linux/log2.h>
16 #include <test/ut.h>
17 #include <u-boot/crc.h>
18 #include "bootstd_common.h"
19 
20 /* tracks whether bootstd_setup_for_tests() has been run yet */
21 bool vbe_setup_done;
22 
23 /**
24  * bootstd_setup_for_tests() - Set up MMC data for VBE tests
25  *
26  * Some data is needed for VBE tests to work. This function sets that up.
27  *
28  * @return 0 if OK, -ve on error
29  */
bootstd_setup_for_tests(struct unit_test_state * uts)30 static int bootstd_setup_for_tests(struct unit_test_state *uts)
31 {
32 	ALLOC_CACHE_ALIGN_BUFFER(u8, buf, MMC_MAX_BLOCK_LEN);
33 	struct udevice *mmc;
34 	struct blk_desc *desc;
35 	int ret;
36 
37 	if (vbe_setup_done)
38 		return 0;
39 
40 	/* Set up the version string */
41 	ret = uclass_get_device(UCLASS_MMC, 1, &mmc);
42 	if (ret)
43 		return log_msg_ret("mmc", -EIO);
44 	desc = blk_get_by_device(mmc);
45 
46 	memset(buf, '\0', MMC_MAX_BLOCK_LEN);
47 	strcpy(buf, TEST_VERSION);
48 	if (blk_dwrite(desc, VERSION_START_BLK, 1, buf) != 1)
49 		return log_msg_ret("wr1", -EIO);
50 
51 	/* Set up the nvdata */
52 	memset(buf, '\0', MMC_MAX_BLOCK_LEN);
53 	buf[1] = ilog2(0x40) << 4 | 1;
54 	*(u32 *)(buf + 4) = TEST_VERNUM;
55 	buf[0] = crc8(0, buf + 1, 0x3f);
56 	if (blk_dwrite(desc, NVDATA_START_BLK, 1, buf) != 1)
57 		return log_msg_ret("wr2", -EIO);
58 
59 	vbe_setup_done = true;
60 
61 	return 0;
62 }
63 BOOTSTD_TEST_INIT(bootstd_setup_for_tests, 0);
64 
bootstd_test_drop_bootdev_order(struct unit_test_state * uts)65 int bootstd_test_drop_bootdev_order(struct unit_test_state *uts)
66 {
67 	struct bootstd_priv *priv;
68 	struct udevice *bootstd;
69 
70 	ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd));
71 	priv = dev_get_priv(bootstd);
72 	priv->bootdev_order = NULL;
73 
74 	return 0;
75 }
76 
bootstd_test_check_mmc_hunter(struct unit_test_state * uts)77 int bootstd_test_check_mmc_hunter(struct unit_test_state *uts)
78 {
79 	struct bootdev_hunter *start, *mmc;
80 	struct bootstd_priv *std;
81 	uint seq;
82 
83 	if (!IS_ENABLED(CONFIG_MMC))
84 		return 0;
85 
86 	/* get access to the used hunters */
87 	ut_assertok(bootstd_get_priv(&std));
88 
89 	/* check that the hunter was used */
90 	start = ll_entry_start(struct bootdev_hunter, bootdev_hunter);
91 	mmc = BOOTDEV_HUNTER_GET(mmc_bootdev_hunter);
92 	seq = mmc - start;
93 	ut_asserteq(BIT(seq), std->hunters_used);
94 
95 	return 0;
96 }
97 
bootstd_reset_usb(void)98 void bootstd_reset_usb(void)
99 {
100 	usb_started = false;
101 }
102