blob: c6766c532f2d4f5caee83704c59d295b74e66e56 [file] [log] [blame]
Simon Glassd0af04c2022-07-30 15:52:33 -06001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Verified Boot for Embedded (VBE) 'simple' method
4 *
5 * Copyright 2022 Google LLC
6 * Written by Simon Glass <sjg@chromium.org>
7 */
8
Simon Glass571707b2022-10-11 09:47:16 -06009#define LOG_CATEGORY LOGC_BOOT
10
Simon Glassd78aa752022-10-20 18:23:10 -060011#include <bootdev.h>
Simon Glassd0af04c2022-07-30 15:52:33 -060012#include <bootflow.h>
13#include <bootmeth.h>
14#include <dm.h>
Simon Glassd78aa752022-10-20 18:23:10 -060015#include <log.h>
16#include <memalign.h>
Simon Glassd0af04c2022-07-30 15:52:33 -060017#include <mmc.h>
18#include <vbe.h>
Simon Glassd0af04c2022-07-30 15:52:33 -060019#include <dm/device-internal.h>
20#include <dm/ofnode.h>
Simon Glassd78aa752022-10-20 18:23:10 -060021#include "vbe_simple.h"
Simon Glassd0af04c2022-07-30 15:52:33 -060022
Simon Glass1833c262025-01-15 18:27:05 -070023static int simple_read_nvdata(const struct simple_priv *priv,
Simon Glass72424b82025-01-15 18:27:09 -070024 struct udevice *blk, struct simple_state *state)
Simon Glassd0af04c2022-07-30 15:52:33 -060025{
Simon Glass72424b82025-01-15 18:27:09 -070026 ALLOC_CACHE_ALIGN_BUFFER(u8, buf, MMC_MAX_BLOCK_LEN);
Simon Glassfdacd502025-01-15 18:27:03 -070027 const struct vbe_nvdata *nvd;
Simon Glass72424b82025-01-15 18:27:09 -070028 int ret;
Simon Glassd0af04c2022-07-30 15:52:33 -060029
Simon Glass72424b82025-01-15 18:27:09 -070030 ret = vbe_read_nvdata(blk, priv->area_start + priv->state_offset,
31 priv->state_size, buf);
32 if (ret)
33 return log_msg_ret("nv", ret);
Simon Glassd0af04c2022-07-30 15:52:33 -060034
Simon Glassfdacd502025-01-15 18:27:03 -070035 nvd = (struct vbe_nvdata *)buf;
Simon Glassd0af04c2022-07-30 15:52:33 -060036 state->fw_vernum = nvd->fw_vernum;
37
38 log_debug("version=%s\n", state->fw_version);
39
40 return 0;
41}
42
Simon Glass39db1992022-10-20 18:23:11 -060043int vbe_simple_read_state(struct udevice *dev, struct simple_state *state)
Simon Glassd0af04c2022-07-30 15:52:33 -060044{
Simon Glassd0af04c2022-07-30 15:52:33 -060045 struct simple_priv *priv = dev_get_priv(dev);
Simon Glasse06c2542025-01-15 18:27:04 -070046 struct udevice *blk;
Simon Glassd0af04c2022-07-30 15:52:33 -060047 int ret;
48
Simon Glass15d320d2025-01-15 18:27:07 -070049 ret = vbe_get_blk(priv->storage, &blk);
50 if (ret)
51 return log_msg_ret("blk", ret);
Simon Glassd0af04c2022-07-30 15:52:33 -060052
Simon Glass9846e5f2025-01-15 18:27:08 -070053 ret = vbe_read_version(blk, priv->area_start + priv->version_offset,
54 state->fw_version, MAX_VERSION_LEN);
Simon Glassd0af04c2022-07-30 15:52:33 -060055 if (ret)
56 return log_msg_ret("ver", ret);
57
Simon Glass72424b82025-01-15 18:27:09 -070058 ret = simple_read_nvdata(priv, blk, state);
Simon Glassd0af04c2022-07-30 15:52:33 -060059 if (ret)
60 return log_msg_ret("nvd", ret);
61
62 return 0;
63}
64
65static int vbe_simple_get_state_desc(struct udevice *dev, char *buf,
66 int maxsize)
67{
68 struct simple_state state;
69 int ret;
70
Simon Glass39db1992022-10-20 18:23:11 -060071 ret = vbe_simple_read_state(dev, &state);
Simon Glassd0af04c2022-07-30 15:52:33 -060072 if (ret)
73 return log_msg_ret("read", ret);
74
75 if (maxsize < 30)
76 return -ENOSPC;
77 snprintf(buf, maxsize, "Version: %s\nVernum: %x/%x", state.fw_version,
78 state.fw_vernum >> FWVER_KEY_SHIFT,
79 state.fw_vernum & FWVER_FW_MASK);
80
81 return 0;
82}
83
84static int vbe_simple_read_bootflow(struct udevice *dev, struct bootflow *bflow)
85{
Simon Glassd78aa752022-10-20 18:23:10 -060086 int ret;
87
Bin Meng45736cd2023-04-03 11:40:26 +080088 if (CONFIG_IS_ENABLED(BOOTMETH_VBE_SIMPLE_FW)) {
89 if (vbe_phase() == VBE_PHASE_FIRMWARE) {
90 ret = vbe_simple_read_bootflow_fw(dev, bflow);
91 if (ret)
92 return log_msg_ret("fw", ret);
93 return 0;
94 }
Simon Glassd78aa752022-10-20 18:23:10 -060095 }
Simon Glassd0af04c2022-07-30 15:52:33 -060096
97 return -EINVAL;
98}
99
Simon Glassd78aa752022-10-20 18:23:10 -0600100static int vbe_simple_read_file(struct udevice *dev, struct bootflow *bflow,
Simon Glassf39b5592024-11-15 16:19:17 -0700101 const char *file_path, ulong addr,
102 enum bootflow_img_t type, ulong *sizep)
Simon Glassd78aa752022-10-20 18:23:10 -0600103{
104 int ret;
105
106 if (vbe_phase() == VBE_PHASE_OS) {
107 ret = bootmeth_common_read_file(dev, bflow, file_path, addr,
Simon Glassf39b5592024-11-15 16:19:17 -0700108 type, sizep);
Simon Glassd78aa752022-10-20 18:23:10 -0600109 if (ret)
110 return log_msg_ret("os", ret);
111 }
112
113 /* To be implemented */
114 return -EINVAL;
115}
116
Simon Glassd0af04c2022-07-30 15:52:33 -0600117static struct bootmeth_ops bootmeth_vbe_simple_ops = {
118 .get_state_desc = vbe_simple_get_state_desc,
119 .read_bootflow = vbe_simple_read_bootflow,
Simon Glassd78aa752022-10-20 18:23:10 -0600120 .read_file = vbe_simple_read_file,
Simon Glassd0af04c2022-07-30 15:52:33 -0600121};
122
Simon Glassd0af04c2022-07-30 15:52:33 -0600123static int bootmeth_vbe_simple_probe(struct udevice *dev)
124{
125 struct simple_priv *priv = dev_get_priv(dev);
126
127 memset(priv, '\0', sizeof(*priv));
128 if (dev_read_u32(dev, "area-start", &priv->area_start) ||
129 dev_read_u32(dev, "area-size", &priv->area_size) ||
130 dev_read_u32(dev, "version-offset", &priv->version_offset) ||
131 dev_read_u32(dev, "version-size", &priv->version_size) ||
132 dev_read_u32(dev, "state-offset", &priv->state_offset) ||
133 dev_read_u32(dev, "state-size", &priv->state_size))
134 return log_msg_ret("read", -EINVAL);
135 dev_read_u32(dev, "skip-offset", &priv->skip_offset);
136 priv->storage = strdup(dev_read_string(dev, "storage"));
137 if (!priv->storage)
138 return log_msg_ret("str", -EINVAL);
139
140 return 0;
141}
142
143static int bootmeth_vbe_simple_bind(struct udevice *dev)
144{
145 struct bootmeth_uc_plat *plat = dev_get_uclass_plat(dev);
146
147 plat->desc = IS_ENABLED(CONFIG_BOOTSTD_FULL) ?
148 "VBE simple" : "vbe-simple";
149 plat->flags = BOOTMETHF_GLOBAL;
150
151 return 0;
152}
153
154#if CONFIG_IS_ENABLED(OF_REAL)
155static const struct udevice_id generic_simple_vbe_simple_ids[] = {
156 { .compatible = "fwupd,vbe-simple" },
157 { }
158};
159#endif
160
161U_BOOT_DRIVER(vbe_simple) = {
162 .name = "vbe_simple",
163 .id = UCLASS_BOOTMETH,
164 .of_match = of_match_ptr(generic_simple_vbe_simple_ids),
165 .ops = &bootmeth_vbe_simple_ops,
166 .bind = bootmeth_vbe_simple_bind,
167 .probe = bootmeth_vbe_simple_probe,
168 .flags = DM_FLAG_PRE_RELOC,
169 .priv_auto = sizeof(struct simple_priv),
170};