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