Simon Glass | 5fe76d4 | 2022-07-30 15:52:37 -0600 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Test for vbe-simple bootmeth. All start with 'vbe_simple' |
| 4 | * |
| 5 | * Copyright 2023 Google LLC |
| 6 | * Written by Simon Glass <sjg@chromium.org> |
| 7 | */ |
| 8 | |
| 9 | #include <common.h> |
| 10 | #include <bootmeth.h> |
| 11 | #include <dm.h> |
| 12 | #include <image.h> |
| 13 | #include <memalign.h> |
| 14 | #include <mmc.h> |
| 15 | #include <of_live.h> |
| 16 | #include <vbe.h> |
| 17 | #include <version_string.h> |
| 18 | #include <linux/log2.h> |
| 19 | #include <test/suites.h> |
| 20 | #include <test/ut.h> |
| 21 | #include <u-boot/crc.h> |
| 22 | #include "bootstd_common.h" |
| 23 | |
| 24 | #define NVDATA_START_BLK ((0x400 + 0x400) / MMC_MAX_BLOCK_LEN) |
| 25 | #define VERSION_START_BLK ((0x400 + 0x800) / MMC_MAX_BLOCK_LEN) |
| 26 | #define TEST_VERSION "U-Boot v2022.04-local2" |
| 27 | #define TEST_VERNUM 0x00010002 |
| 28 | |
| 29 | /* Basic test of reading nvdata and updating a fwupd node in the device tree */ |
| 30 | static int vbe_simple_test_base(struct unit_test_state *uts) |
| 31 | { |
| 32 | ALLOC_CACHE_ALIGN_BUFFER(u8, buf, MMC_MAX_BLOCK_LEN); |
| 33 | const char *version, *bl_version; |
| 34 | struct event_ft_fixup fixup; |
| 35 | struct udevice *dev, *mmc; |
| 36 | struct device_node *np; |
| 37 | struct blk_desc *desc; |
| 38 | char fdt_buf[0x400]; |
| 39 | char info[100]; |
| 40 | int node_ofs; |
| 41 | ofnode node; |
| 42 | u32 vernum; |
| 43 | |
| 44 | /* Set up the version string */ |
| 45 | ut_assertok(uclass_get_device(UCLASS_MMC, 1, &mmc)); |
| 46 | desc = blk_get_by_device(mmc); |
| 47 | ut_assertnonnull(desc); |
| 48 | |
| 49 | memset(buf, '\0', MMC_MAX_BLOCK_LEN); |
| 50 | strcpy(buf, TEST_VERSION); |
| 51 | if (blk_dwrite(desc, VERSION_START_BLK, 1, buf) != 1) |
| 52 | return log_msg_ret("write", -EIO); |
| 53 | |
| 54 | /* Set up the nvdata */ |
| 55 | memset(buf, '\0', MMC_MAX_BLOCK_LEN); |
| 56 | buf[1] = ilog2(0x40) << 4 | 1; |
| 57 | *(u32 *)(buf + 4) = TEST_VERNUM; |
| 58 | buf[0] = crc8(0, buf + 1, 0x3f); |
| 59 | if (blk_dwrite(desc, NVDATA_START_BLK, 1, buf) != 1) |
| 60 | return log_msg_ret("write", -EIO); |
| 61 | |
| 62 | /* Read the version back */ |
| 63 | ut_assertok(vbe_find_by_any("firmware0", &dev)); |
| 64 | ut_assertok(bootmeth_get_state_desc(dev, info, sizeof(info))); |
| 65 | ut_asserteq_str("Version: " TEST_VERSION "\nVernum: 1/2", info); |
| 66 | |
| 67 | ut_assertok(fdt_create_empty_tree(fdt_buf, sizeof(fdt_buf))); |
| 68 | node_ofs = fdt_add_subnode(fdt_buf, 0, "chosen"); |
| 69 | ut_assert(node_ofs > 0); |
| 70 | |
| 71 | node_ofs = fdt_add_subnode(fdt_buf, node_ofs, "fwupd"); |
| 72 | ut_assert(node_ofs > 0); |
| 73 | |
| 74 | node_ofs = fdt_add_subnode(fdt_buf, node_ofs, "firmware0"); |
| 75 | ut_assert(node_ofs > 0); |
| 76 | |
| 77 | /* |
| 78 | * This can only work on the live tree, since the ofnode interface for |
| 79 | * flat tree assumes that ofnode points to the control FDT |
| 80 | */ |
| 81 | ut_assertok(unflatten_device_tree(fdt_buf, &np)); |
| 82 | |
| 83 | /* |
| 84 | * It would be better to call image_setup_libfdt() here, but that |
| 85 | * function does not allow passing an ofnode. We can pass fdt_buf but |
| 86 | * when it comes to send the evenr, it creates an ofnode that uses the |
| 87 | * control FDT, since it has no way of accessing the live tree created |
| 88 | * here. |
| 89 | * |
| 90 | * Two fix this we need: |
| 91 | * - image_setup_libfdt() is updated to use ofnode |
| 92 | * - ofnode updated to support access to an FDT other than the control |
| 93 | * FDT. This is partially implemented with live tree, but not with |
| 94 | * flat tree |
| 95 | */ |
| 96 | fixup.tree.np = np; |
| 97 | ut_assertok(event_notify(EVT_FT_FIXUP, &fixup, sizeof(fixup))); |
| 98 | |
| 99 | node = ofnode_path_root(fixup.tree, "/chosen/fwupd/firmware0"); |
| 100 | |
| 101 | version = ofnode_read_string(node, "cur-version"); |
| 102 | ut_assertnonnull(version); |
| 103 | ut_asserteq_str(TEST_VERSION, version); |
| 104 | |
| 105 | ut_assertok(ofnode_read_u32(node, "cur-vernum", &vernum)); |
| 106 | ut_asserteq(TEST_VERNUM, vernum); |
| 107 | |
| 108 | bl_version = ofnode_read_string(node, "bootloader-version"); |
| 109 | ut_assertnonnull(bl_version); |
| 110 | ut_asserteq_str(version_string, bl_version); |
| 111 | |
| 112 | return 0; |
| 113 | } |
| 114 | BOOTSTD_TEST(vbe_simple_test_base, UT_TESTF_DM | UT_TESTF_SCAN_FDT | |
| 115 | UT_TESTF_LIVE_TREE); |