blob: 8acd777f4cd1cd090a8f50569b88c077b1cb25c0 [file] [log] [blame]
Simon Glasse5a7b772022-07-30 15:52:37 -06001// 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 */
30static 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
Simon Glasseda440b2022-09-06 20:27:31 -060077 if (of_live_active()) {
78 ut_assertok(unflatten_device_tree(fdt_buf, &np));
79 fixup.tree = oftree_from_np(np);
80 } else {
81 fixup.tree = oftree_from_fdt(fdt_buf);
82 }
Simon Glasse5a7b772022-07-30 15:52:37 -060083
84 /*
85 * It would be better to call image_setup_libfdt() here, but that
86 * function does not allow passing an ofnode. We can pass fdt_buf but
Simon Glasseda440b2022-09-06 20:27:31 -060087 * when it comes to send the event, it creates an ofnode that uses the
Simon Glasse5a7b772022-07-30 15:52:37 -060088 * control FDT, since it has no way of accessing the live tree created
89 * here.
90 *
Simon Glasseda440b2022-09-06 20:27:31 -060091 * Two fix this we need image_setup_libfdt() is updated to use ofnode
Simon Glasse5a7b772022-07-30 15:52:37 -060092 */
Simon Glasse5a7b772022-07-30 15:52:37 -060093 ut_assertok(event_notify(EVT_FT_FIXUP, &fixup, sizeof(fixup)));
94
Simon Glass45ae59d2022-09-06 20:27:24 -060095 node = oftree_path(fixup.tree, "/chosen/fwupd/firmware0");
Simon Glasse5a7b772022-07-30 15:52:37 -060096
97 version = ofnode_read_string(node, "cur-version");
98 ut_assertnonnull(version);
99 ut_asserteq_str(TEST_VERSION, version);
100
101 ut_assertok(ofnode_read_u32(node, "cur-vernum", &vernum));
102 ut_asserteq(TEST_VERNUM, vernum);
103
104 bl_version = ofnode_read_string(node, "bootloader-version");
105 ut_assertnonnull(bl_version);
106 ut_asserteq_str(version_string, bl_version);
107
108 return 0;
109}
Simon Glasseda440b2022-09-06 20:27:31 -0600110BOOTSTD_TEST(vbe_simple_test_base, UT_TESTF_DM | UT_TESTF_SCAN_FDT);