blob: 189e86d2a223ba742f8b470123881cbe00cc8b58 [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>
21#include <u-boot/crc.h>
Simon Glassd78aa752022-10-20 18:23:10 -060022#include "vbe_simple.h"
Simon Glassd0af04c2022-07-30 15:52:33 -060023
Simon Glassd0af04c2022-07-30 15:52:33 -060024/** struct simple_nvdata - storage format for non-volatile data */
25struct simple_nvdata {
26 u8 crc8;
27 u8 hdr;
28 u16 spare1;
29 u32 fw_vernum;
30 u8 spare2[0x38];
31};
32
33static int simple_read_version(struct udevice *dev, struct blk_desc *desc,
34 u8 *buf, struct simple_state *state)
35{
36 struct simple_priv *priv = dev_get_priv(dev);
37 int start;
38
39 if (priv->version_size > MMC_MAX_BLOCK_LEN)
40 return log_msg_ret("ver", -E2BIG);
41
42 start = priv->area_start + priv->version_offset;
43 if (start & (MMC_MAX_BLOCK_LEN - 1))
44 return log_msg_ret("get", -EBADF);
45 start /= MMC_MAX_BLOCK_LEN;
46
47 if (blk_dread(desc, start, 1, buf) != 1)
48 return log_msg_ret("read", -EIO);
49 strlcpy(state->fw_version, buf, MAX_VERSION_LEN);
50 log_debug("version=%s\n", state->fw_version);
51
52 return 0;
53}
54
55static int simple_read_nvdata(struct udevice *dev, struct blk_desc *desc,
56 u8 *buf, struct simple_state *state)
57{
58 struct simple_priv *priv = dev_get_priv(dev);
59 uint hdr_ver, hdr_size, size, crc;
60 const struct simple_nvdata *nvd;
61 int start;
62
63 if (priv->state_size > MMC_MAX_BLOCK_LEN)
64 return log_msg_ret("state", -E2BIG);
65
66 start = priv->area_start + priv->state_offset;
67 if (start & (MMC_MAX_BLOCK_LEN - 1))
68 return log_msg_ret("get", -EBADF);
69 start /= MMC_MAX_BLOCK_LEN;
70
71 if (blk_dread(desc, start, 1, buf) != 1)
72 return log_msg_ret("read", -EIO);
73 nvd = (struct simple_nvdata *)buf;
74 hdr_ver = (nvd->hdr & NVD_HDR_VER_MASK) >> NVD_HDR_VER_SHIFT;
75 hdr_size = (nvd->hdr & NVD_HDR_SIZE_MASK) >> NVD_HDR_SIZE_SHIFT;
76 if (hdr_ver != NVD_HDR_VER_CUR)
77 return log_msg_ret("hdr", -EPERM);
78 size = 1 << hdr_size;
79 if (size > sizeof(*nvd))
80 return log_msg_ret("sz", -ENOEXEC);
81
82 crc = crc8(0, buf + 1, size - 1);
83 if (crc != nvd->crc8)
84 return log_msg_ret("crc", -EPERM);
85 state->fw_vernum = nvd->fw_vernum;
86
87 log_debug("version=%s\n", state->fw_version);
88
89 return 0;
90}
91
Simon Glass39db1992022-10-20 18:23:11 -060092int vbe_simple_read_state(struct udevice *dev, struct simple_state *state)
Simon Glassd0af04c2022-07-30 15:52:33 -060093{
94 ALLOC_CACHE_ALIGN_BUFFER(u8, buf, MMC_MAX_BLOCK_LEN);
95 struct simple_priv *priv = dev_get_priv(dev);
96 struct blk_desc *desc;
97 char devname[16];
98 const char *end;
99 int devnum;
100 int ret;
101
102 /* First figure out the block device */
103 log_debug("storage=%s\n", priv->storage);
104 devnum = trailing_strtoln_end(priv->storage, NULL, &end);
105 if (devnum == -1)
106 return log_msg_ret("num", -ENODEV);
107 if (end - priv->storage >= sizeof(devname))
108 return log_msg_ret("end", -E2BIG);
109 strlcpy(devname, priv->storage, end - priv->storage + 1);
110 log_debug("dev=%s, %x\n", devname, devnum);
111
112 desc = blk_get_dev(devname, devnum);
113 if (!desc)
114 return log_msg_ret("get", -ENXIO);
115
116 ret = simple_read_version(dev, desc, buf, state);
117 if (ret)
118 return log_msg_ret("ver", ret);
119
120 ret = simple_read_nvdata(dev, desc, buf, state);
121 if (ret)
122 return log_msg_ret("nvd", ret);
123
124 return 0;
125}
126
127static int vbe_simple_get_state_desc(struct udevice *dev, char *buf,
128 int maxsize)
129{
130 struct simple_state state;
131 int ret;
132
Simon Glass39db1992022-10-20 18:23:11 -0600133 ret = vbe_simple_read_state(dev, &state);
Simon Glassd0af04c2022-07-30 15:52:33 -0600134 if (ret)
135 return log_msg_ret("read", ret);
136
137 if (maxsize < 30)
138 return -ENOSPC;
139 snprintf(buf, maxsize, "Version: %s\nVernum: %x/%x", state.fw_version,
140 state.fw_vernum >> FWVER_KEY_SHIFT,
141 state.fw_vernum & FWVER_FW_MASK);
142
143 return 0;
144}
145
146static int vbe_simple_read_bootflow(struct udevice *dev, struct bootflow *bflow)
147{
Simon Glassd78aa752022-10-20 18:23:10 -0600148 int ret;
149
Bin Meng45736cd2023-04-03 11:40:26 +0800150 if (CONFIG_IS_ENABLED(BOOTMETH_VBE_SIMPLE_FW)) {
151 if (vbe_phase() == VBE_PHASE_FIRMWARE) {
152 ret = vbe_simple_read_bootflow_fw(dev, bflow);
153 if (ret)
154 return log_msg_ret("fw", ret);
155 return 0;
156 }
Simon Glassd78aa752022-10-20 18:23:10 -0600157 }
Simon Glassd0af04c2022-07-30 15:52:33 -0600158
159 return -EINVAL;
160}
161
Simon Glassd78aa752022-10-20 18:23:10 -0600162static int vbe_simple_read_file(struct udevice *dev, struct bootflow *bflow,
163 const char *file_path, ulong addr, ulong *sizep)
164{
165 int ret;
166
167 if (vbe_phase() == VBE_PHASE_OS) {
168 ret = bootmeth_common_read_file(dev, bflow, file_path, addr,
169 sizep);
170 if (ret)
171 return log_msg_ret("os", ret);
172 }
173
174 /* To be implemented */
175 return -EINVAL;
176}
177
Simon Glassd0af04c2022-07-30 15:52:33 -0600178static struct bootmeth_ops bootmeth_vbe_simple_ops = {
179 .get_state_desc = vbe_simple_get_state_desc,
180 .read_bootflow = vbe_simple_read_bootflow,
Simon Glassd78aa752022-10-20 18:23:10 -0600181 .read_file = vbe_simple_read_file,
Simon Glassd0af04c2022-07-30 15:52:33 -0600182};
183
Simon Glassd0af04c2022-07-30 15:52:33 -0600184static int bootmeth_vbe_simple_probe(struct udevice *dev)
185{
186 struct simple_priv *priv = dev_get_priv(dev);
187
188 memset(priv, '\0', sizeof(*priv));
189 if (dev_read_u32(dev, "area-start", &priv->area_start) ||
190 dev_read_u32(dev, "area-size", &priv->area_size) ||
191 dev_read_u32(dev, "version-offset", &priv->version_offset) ||
192 dev_read_u32(dev, "version-size", &priv->version_size) ||
193 dev_read_u32(dev, "state-offset", &priv->state_offset) ||
194 dev_read_u32(dev, "state-size", &priv->state_size))
195 return log_msg_ret("read", -EINVAL);
196 dev_read_u32(dev, "skip-offset", &priv->skip_offset);
197 priv->storage = strdup(dev_read_string(dev, "storage"));
198 if (!priv->storage)
199 return log_msg_ret("str", -EINVAL);
200
201 return 0;
202}
203
204static int bootmeth_vbe_simple_bind(struct udevice *dev)
205{
206 struct bootmeth_uc_plat *plat = dev_get_uclass_plat(dev);
207
208 plat->desc = IS_ENABLED(CONFIG_BOOTSTD_FULL) ?
209 "VBE simple" : "vbe-simple";
210 plat->flags = BOOTMETHF_GLOBAL;
211
212 return 0;
213}
214
215#if CONFIG_IS_ENABLED(OF_REAL)
216static const struct udevice_id generic_simple_vbe_simple_ids[] = {
217 { .compatible = "fwupd,vbe-simple" },
218 { }
219};
220#endif
221
222U_BOOT_DRIVER(vbe_simple) = {
223 .name = "vbe_simple",
224 .id = UCLASS_BOOTMETH,
225 .of_match = of_match_ptr(generic_simple_vbe_simple_ids),
226 .ops = &bootmeth_vbe_simple_ops,
227 .bind = bootmeth_vbe_simple_bind,
228 .probe = bootmeth_vbe_simple_probe,
229 .flags = DM_FLAG_PRE_RELOC,
230 .priv_auto = sizeof(struct simple_priv),
231};