blob: 59d46bef0c5b94d4bceaeb7a66a02ebab7615fa4 [file] [log] [blame]
Simon Glassb255efc2022-04-24 23:31:24 -06001// 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 <common.h>
10#include <bootstd.h>
11#include <dm.h>
Simon Glass80253462022-10-11 09:47:13 -060012#include <memalign.h>
13#include <mmc.h>
14#include <linux/log2.h>
Simon Glassb255efc2022-04-24 23:31:24 -060015#include <test/suites.h>
16#include <test/ut.h>
Simon Glass80253462022-10-11 09:47:13 -060017#include <u-boot/crc.h>
Simon Glassb255efc2022-04-24 23:31:24 -060018#include "bootstd_common.h"
19
Simon Glass80253462022-10-11 09:47:13 -060020bool vbe_setup_done;
21
22/* set up MMC for VBE tests */
23int bootstd_setup_for_tests(void)
24{
25 ALLOC_CACHE_ALIGN_BUFFER(u8, buf, MMC_MAX_BLOCK_LEN);
26 struct udevice *mmc;
27 struct blk_desc *desc;
28 int ret;
29
30 /* Set up the version string */
31 ret = uclass_get_device(UCLASS_MMC, 1, &mmc);
32 if (ret)
33 return log_msg_ret("mmc", -EIO);
34 desc = blk_get_by_device(mmc);
35
36 memset(buf, '\0', MMC_MAX_BLOCK_LEN);
37 strcpy(buf, TEST_VERSION);
38 if (blk_dwrite(desc, VERSION_START_BLK, 1, buf) != 1)
39 return log_msg_ret("wr1", -EIO);
40
41 /* Set up the nvdata */
42 memset(buf, '\0', MMC_MAX_BLOCK_LEN);
43 buf[1] = ilog2(0x40) << 4 | 1;
44 *(u32 *)(buf + 4) = TEST_VERNUM;
45 buf[0] = crc8(0, buf + 1, 0x3f);
46 if (blk_dwrite(desc, NVDATA_START_BLK, 1, buf) != 1)
47 return log_msg_ret("wr2", -EIO);
48
49 return 0;
50}
51
Simon Glassb255efc2022-04-24 23:31:24 -060052int bootstd_test_drop_bootdev_order(struct unit_test_state *uts)
53{
54 struct bootstd_priv *priv;
55 struct udevice *bootstd;
56
57 ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd));
58 priv = dev_get_priv(bootstd);
59 priv->bootdev_order = NULL;
60
61 return 0;
62}
63
64int do_ut_bootstd(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
65{
66 struct unit_test *tests = UNIT_TEST_SUITE_START(bootstd_test);
67 const int n_ents = UNIT_TEST_SUITE_COUNT(bootstd_test);
68
Simon Glass80253462022-10-11 09:47:13 -060069 if (!vbe_setup_done) {
70 int ret;
71
72 ret = bootstd_setup_for_tests();
73 if (ret) {
74 printf("Failed to set up for bootstd tests (err=%d)\n",
75 ret);
76 return CMD_RET_FAILURE;
77 }
78 vbe_setup_done = true;
79 }
80
Simon Glassb255efc2022-04-24 23:31:24 -060081 return cmd_ut_category("bootstd", "bootstd_test_",
82 tests, n_ents, argc, argv);
83}