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