Simon Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 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 | |
Simon Glass | de567b1 | 2023-01-17 10:48:09 -0700 | [diff] [blame] | 9 | #include <bootdev.h> |
Simon Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 10 | #include <bootstd.h> |
| 11 | #include <dm.h> |
Simon Glass | 8025346 | 2022-10-11 09:47:13 -0600 | [diff] [blame] | 12 | #include <memalign.h> |
| 13 | #include <mmc.h> |
Simon Glass | 22c93da | 2024-09-01 16:26:16 -0600 | [diff] [blame] | 14 | #include <usb.h> |
Simon Glass | 8025346 | 2022-10-11 09:47:13 -0600 | [diff] [blame] | 15 | #include <linux/log2.h> |
Simon Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 16 | #include <test/suites.h> |
| 17 | #include <test/ut.h> |
Simon Glass | 8025346 | 2022-10-11 09:47:13 -0600 | [diff] [blame] | 18 | #include <u-boot/crc.h> |
Simon Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 19 | #include "bootstd_common.h" |
| 20 | |
Simon Glass | 1b035c1 | 2022-10-11 09:47:17 -0600 | [diff] [blame] | 21 | /* tracks whether bootstd_setup_for_tests() has been run yet */ |
Simon Glass | 8025346 | 2022-10-11 09:47:13 -0600 | [diff] [blame] | 22 | bool vbe_setup_done; |
| 23 | |
Simon Glass | 13b8f4f | 2025-02-07 11:30:48 -0700 | [diff] [blame^] | 24 | /** |
| 25 | * bootstd_setup_for_tests() - Set up MMC data for VBE tests |
| 26 | * |
| 27 | * Some data is needed for VBE tests to work. This function sets that up. |
| 28 | * |
| 29 | * @return 0 if OK, -ve on error |
| 30 | */ |
| 31 | static int bootstd_setup_for_tests(struct unit_test_state *uts) |
Simon Glass | 8025346 | 2022-10-11 09:47:13 -0600 | [diff] [blame] | 32 | { |
| 33 | ALLOC_CACHE_ALIGN_BUFFER(u8, buf, MMC_MAX_BLOCK_LEN); |
| 34 | struct udevice *mmc; |
| 35 | struct blk_desc *desc; |
| 36 | int ret; |
| 37 | |
Simon Glass | 1b035c1 | 2022-10-11 09:47:17 -0600 | [diff] [blame] | 38 | if (vbe_setup_done) |
| 39 | return 0; |
| 40 | |
Simon Glass | 8025346 | 2022-10-11 09:47:13 -0600 | [diff] [blame] | 41 | /* Set up the version string */ |
| 42 | ret = uclass_get_device(UCLASS_MMC, 1, &mmc); |
| 43 | if (ret) |
| 44 | return log_msg_ret("mmc", -EIO); |
| 45 | desc = blk_get_by_device(mmc); |
| 46 | |
| 47 | memset(buf, '\0', MMC_MAX_BLOCK_LEN); |
| 48 | strcpy(buf, TEST_VERSION); |
| 49 | if (blk_dwrite(desc, VERSION_START_BLK, 1, buf) != 1) |
| 50 | return log_msg_ret("wr1", -EIO); |
| 51 | |
| 52 | /* Set up the nvdata */ |
| 53 | memset(buf, '\0', MMC_MAX_BLOCK_LEN); |
| 54 | buf[1] = ilog2(0x40) << 4 | 1; |
| 55 | *(u32 *)(buf + 4) = TEST_VERNUM; |
| 56 | buf[0] = crc8(0, buf + 1, 0x3f); |
| 57 | if (blk_dwrite(desc, NVDATA_START_BLK, 1, buf) != 1) |
| 58 | return log_msg_ret("wr2", -EIO); |
| 59 | |
Simon Glass | 1b035c1 | 2022-10-11 09:47:17 -0600 | [diff] [blame] | 60 | vbe_setup_done = true; |
| 61 | |
Simon Glass | 8025346 | 2022-10-11 09:47:13 -0600 | [diff] [blame] | 62 | return 0; |
| 63 | } |
Simon Glass | 13b8f4f | 2025-02-07 11:30:48 -0700 | [diff] [blame^] | 64 | BOOTSTD_TEST_INIT(bootstd_setup_for_tests, 0); |
Simon Glass | 8025346 | 2022-10-11 09:47:13 -0600 | [diff] [blame] | 65 | |
Simon Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 66 | int bootstd_test_drop_bootdev_order(struct unit_test_state *uts) |
| 67 | { |
| 68 | struct bootstd_priv *priv; |
| 69 | struct udevice *bootstd; |
| 70 | |
| 71 | ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd)); |
| 72 | priv = dev_get_priv(bootstd); |
| 73 | priv->bootdev_order = NULL; |
| 74 | |
| 75 | return 0; |
| 76 | } |
| 77 | |
Simon Glass | de567b1 | 2023-01-17 10:48:09 -0700 | [diff] [blame] | 78 | int bootstd_test_check_mmc_hunter(struct unit_test_state *uts) |
| 79 | { |
| 80 | struct bootdev_hunter *start, *mmc; |
| 81 | struct bootstd_priv *std; |
| 82 | uint seq; |
| 83 | |
Dan Carpenter | fb3ad54 | 2024-02-21 09:26:21 +0300 | [diff] [blame] | 84 | if (!IS_ENABLED(CONFIG_MMC)) |
| 85 | return 0; |
| 86 | |
Simon Glass | de567b1 | 2023-01-17 10:48:09 -0700 | [diff] [blame] | 87 | /* get access to the used hunters */ |
| 88 | ut_assertok(bootstd_get_priv(&std)); |
| 89 | |
| 90 | /* check that the hunter was used */ |
| 91 | start = ll_entry_start(struct bootdev_hunter, bootdev_hunter); |
| 92 | mmc = BOOTDEV_HUNTER_GET(mmc_bootdev_hunter); |
| 93 | seq = mmc - start; |
| 94 | ut_asserteq(BIT(seq), std->hunters_used); |
| 95 | |
| 96 | return 0; |
| 97 | } |
| 98 | |
Simon Glass | 22c93da | 2024-09-01 16:26:16 -0600 | [diff] [blame] | 99 | void bootstd_reset_usb(void) |
| 100 | { |
| 101 | usb_started = false; |
| 102 | } |
| 103 | |
Simon Glass | 6685ece | 2025-01-20 14:25:58 -0700 | [diff] [blame] | 104 | int do_ut_bootstd(struct unit_test_state *uts, struct cmd_tbl *cmdtp, int flag, |
| 105 | int argc, char *const argv[]) |
Simon Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 106 | { |
Simon Glass | b15512c | 2025-01-20 14:25:32 -0700 | [diff] [blame] | 107 | struct unit_test *tests = UNIT_TEST_SUITE_START(bootstd); |
| 108 | const int n_ents = UNIT_TEST_SUITE_COUNT(bootstd); |
Simon Glass | 8025346 | 2022-10-11 09:47:13 -0600 | [diff] [blame] | 109 | |
Simon Glass | 6685ece | 2025-01-20 14:25:58 -0700 | [diff] [blame] | 110 | return cmd_ut_category(uts, "bootstd", "bootstd_", |
| 111 | tests, n_ents, argc, argv); |
Simon Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 112 | } |