1 // SPDX-License-Identifier: GPL-2.0+
3 * Verified Boot for Embedded (VBE) 'simple' method
5 * Copyright 2022 Google LLC
9 #define LOG_CATEGORY LOGC_BOOT
20 #include <dm/device-internal.h>
21 #include <dm/ofnode.h>
22 #include <u-boot/crc.h>
23 #include "vbe_simple.h"
25 /** struct simple_nvdata - storage format for non-volatile data */
26 struct simple_nvdata {
34 static int simple_read_version(struct udevice *dev, struct blk_desc *desc,
35 u8 *buf, struct simple_state *state)
37 struct simple_priv *priv = dev_get_priv(dev);
40 if (priv->version_size > MMC_MAX_BLOCK_LEN)
41 return log_msg_ret("ver", -E2BIG);
43 start = priv->area_start + priv->version_offset;
44 if (start & (MMC_MAX_BLOCK_LEN - 1))
45 return log_msg_ret("get", -EBADF);
46 start /= MMC_MAX_BLOCK_LEN;
48 if (blk_dread(desc, start, 1, buf) != 1)
49 return log_msg_ret("read", -EIO);
50 strlcpy(state->fw_version, buf, MAX_VERSION_LEN);
51 log_debug("version=%s\n", state->fw_version);
56 static int simple_read_nvdata(struct udevice *dev, struct blk_desc *desc,
57 u8 *buf, struct simple_state *state)
59 struct simple_priv *priv = dev_get_priv(dev);
60 uint hdr_ver, hdr_size, size, crc;
61 const struct simple_nvdata *nvd;
64 if (priv->state_size > MMC_MAX_BLOCK_LEN)
65 return log_msg_ret("state", -E2BIG);
67 start = priv->area_start + priv->state_offset;
68 if (start & (MMC_MAX_BLOCK_LEN - 1))
69 return log_msg_ret("get", -EBADF);
70 start /= MMC_MAX_BLOCK_LEN;
72 if (blk_dread(desc, start, 1, buf) != 1)
73 return log_msg_ret("read", -EIO);
74 nvd = (struct simple_nvdata *)buf;
75 hdr_ver = (nvd->hdr & NVD_HDR_VER_MASK) >> NVD_HDR_VER_SHIFT;
76 hdr_size = (nvd->hdr & NVD_HDR_SIZE_MASK) >> NVD_HDR_SIZE_SHIFT;
77 if (hdr_ver != NVD_HDR_VER_CUR)
78 return log_msg_ret("hdr", -EPERM);
80 if (size > sizeof(*nvd))
81 return log_msg_ret("sz", -ENOEXEC);
83 crc = crc8(0, buf + 1, size - 1);
85 return log_msg_ret("crc", -EPERM);
86 state->fw_vernum = nvd->fw_vernum;
88 log_debug("version=%s\n", state->fw_version);
93 int vbe_simple_read_state(struct udevice *dev, struct simple_state *state)
95 ALLOC_CACHE_ALIGN_BUFFER(u8, buf, MMC_MAX_BLOCK_LEN);
96 struct simple_priv *priv = dev_get_priv(dev);
97 struct blk_desc *desc;
103 /* First figure out the block device */
104 log_debug("storage=%s\n", priv->storage);
105 devnum = trailing_strtoln_end(priv->storage, NULL, &end);
107 return log_msg_ret("num", -ENODEV);
108 if (end - priv->storage >= sizeof(devname))
109 return log_msg_ret("end", -E2BIG);
110 strlcpy(devname, priv->storage, end - priv->storage + 1);
111 log_debug("dev=%s, %x\n", devname, devnum);
113 desc = blk_get_dev(devname, devnum);
115 return log_msg_ret("get", -ENXIO);
117 ret = simple_read_version(dev, desc, buf, state);
119 return log_msg_ret("ver", ret);
121 ret = simple_read_nvdata(dev, desc, buf, state);
123 return log_msg_ret("nvd", ret);
128 static int vbe_simple_get_state_desc(struct udevice *dev, char *buf,
131 struct simple_state state;
134 ret = vbe_simple_read_state(dev, &state);
136 return log_msg_ret("read", ret);
140 snprintf(buf, maxsize, "Version: %s\nVernum: %x/%x", state.fw_version,
141 state.fw_vernum >> FWVER_KEY_SHIFT,
142 state.fw_vernum & FWVER_FW_MASK);
147 static int vbe_simple_read_bootflow(struct udevice *dev, struct bootflow *bflow)
151 if (CONFIG_IS_ENABLED(BOOTMETH_VBE_SIMPLE_FW)) {
152 if (vbe_phase() == VBE_PHASE_FIRMWARE) {
153 ret = vbe_simple_read_bootflow_fw(dev, bflow);
155 return log_msg_ret("fw", ret);
163 static int vbe_simple_read_file(struct udevice *dev, struct bootflow *bflow,
164 const char *file_path, ulong addr, ulong *sizep)
168 if (vbe_phase() == VBE_PHASE_OS) {
169 ret = bootmeth_common_read_file(dev, bflow, file_path, addr,
172 return log_msg_ret("os", ret);
175 /* To be implemented */
179 static struct bootmeth_ops bootmeth_vbe_simple_ops = {
180 .get_state_desc = vbe_simple_get_state_desc,
181 .read_bootflow = vbe_simple_read_bootflow,
182 .read_file = vbe_simple_read_file,
185 static int bootmeth_vbe_simple_probe(struct udevice *dev)
187 struct simple_priv *priv = dev_get_priv(dev);
189 memset(priv, '\0', sizeof(*priv));
190 if (dev_read_u32(dev, "area-start", &priv->area_start) ||
191 dev_read_u32(dev, "area-size", &priv->area_size) ||
192 dev_read_u32(dev, "version-offset", &priv->version_offset) ||
193 dev_read_u32(dev, "version-size", &priv->version_size) ||
194 dev_read_u32(dev, "state-offset", &priv->state_offset) ||
195 dev_read_u32(dev, "state-size", &priv->state_size))
196 return log_msg_ret("read", -EINVAL);
197 dev_read_u32(dev, "skip-offset", &priv->skip_offset);
198 priv->storage = strdup(dev_read_string(dev, "storage"));
200 return log_msg_ret("str", -EINVAL);
205 static int bootmeth_vbe_simple_bind(struct udevice *dev)
207 struct bootmeth_uc_plat *plat = dev_get_uclass_plat(dev);
209 plat->desc = IS_ENABLED(CONFIG_BOOTSTD_FULL) ?
210 "VBE simple" : "vbe-simple";
211 plat->flags = BOOTMETHF_GLOBAL;
216 #if CONFIG_IS_ENABLED(OF_REAL)
217 static const struct udevice_id generic_simple_vbe_simple_ids[] = {
218 { .compatible = "fwupd,vbe-simple" },
223 U_BOOT_DRIVER(vbe_simple) = {
224 .name = "vbe_simple",
225 .id = UCLASS_BOOTMETH,
226 .of_match = of_match_ptr(generic_simple_vbe_simple_ids),
227 .ops = &bootmeth_vbe_simple_ops,
228 .bind = bootmeth_vbe_simple_bind,
229 .probe = bootmeth_vbe_simple_probe,
230 .flags = DM_FLAG_PRE_RELOC,
231 .priv_auto = sizeof(struct simple_priv),