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 | |
| 9 | #include <common.h> |
| 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 | 8025346 | 2022-10-11 09:47:13 -0600 | [diff] [blame^] | 20 | bool vbe_setup_done; |
| 21 | |
| 22 | /* set up MMC for VBE tests */ |
| 23 | int 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 Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 52 | int 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 | |
| 64 | int 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 Glass | 8025346 | 2022-10-11 09:47:13 -0600 | [diff] [blame^] | 69 | 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 Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 81 | return cmd_ut_category("bootstd", "bootstd_test_", |
| 82 | tests, n_ents, argc, argv); |
| 83 | } |