blob: 076b650c25afb591fe341ab0fe5c5a6d108c828f [file] [log] [blame]
Simon Glasscb47e212022-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 Glass87c97cb2022-10-11 09:47:16 -06009#define LOG_CATEGORY LOGC_BOOT
10
Simon Glasscb47e212022-07-30 15:52:33 -060011#include <common.h>
12#include <log.h>
13#include <memalign.h>
14#include <part.h>
15#include <bootflow.h>
16#include <bootmeth.h>
17#include <dm.h>
18#include <mmc.h>
19#include <vbe.h>
20#include <version_string.h>
21#include <dm/device-internal.h>
22#include <dm/ofnode.h>
23#include <u-boot/crc.h>
24
25enum {
26 MAX_VERSION_LEN = 256,
27
28 NVD_HDR_VER_SHIFT = 0,
29 NVD_HDR_VER_MASK = 0xf,
30 NVD_HDR_SIZE_SHIFT = 4,
31 NVD_HDR_SIZE_MASK = 0xf << NVD_HDR_SIZE_SHIFT,
32
33 /* Firmware key-version is in the top 16 bits of fw_ver */
34 FWVER_KEY_SHIFT = 16,
35 FWVER_FW_MASK = 0xffff,
36
37 NVD_HDR_VER_CUR = 1, /* current version */
38};
39
40/** struct simple_priv - information read from the device tree */
41struct simple_priv {
42 u32 area_start;
43 u32 area_size;
44 u32 skip_offset;
45 u32 state_offset;
46 u32 state_size;
47 u32 version_offset;
48 u32 version_size;
49 const char *storage;
50};
51
52/** struct simple_state - state information read from media
53 *
54 * @fw_version: Firmware version string
55 * @fw_vernum: Firmware version number
56 */
57struct simple_state {
58 char fw_version[MAX_VERSION_LEN];
59 u32 fw_vernum;
60};
61
62/** struct simple_nvdata - storage format for non-volatile data */
63struct simple_nvdata {
64 u8 crc8;
65 u8 hdr;
66 u16 spare1;
67 u32 fw_vernum;
68 u8 spare2[0x38];
69};
70
71static int simple_read_version(struct udevice *dev, struct blk_desc *desc,
72 u8 *buf, struct simple_state *state)
73{
74 struct simple_priv *priv = dev_get_priv(dev);
75 int start;
76
77 if (priv->version_size > MMC_MAX_BLOCK_LEN)
78 return log_msg_ret("ver", -E2BIG);
79
80 start = priv->area_start + priv->version_offset;
81 if (start & (MMC_MAX_BLOCK_LEN - 1))
82 return log_msg_ret("get", -EBADF);
83 start /= MMC_MAX_BLOCK_LEN;
84
85 if (blk_dread(desc, start, 1, buf) != 1)
86 return log_msg_ret("read", -EIO);
87 strlcpy(state->fw_version, buf, MAX_VERSION_LEN);
88 log_debug("version=%s\n", state->fw_version);
89
90 return 0;
91}
92
93static int simple_read_nvdata(struct udevice *dev, struct blk_desc *desc,
94 u8 *buf, struct simple_state *state)
95{
96 struct simple_priv *priv = dev_get_priv(dev);
97 uint hdr_ver, hdr_size, size, crc;
98 const struct simple_nvdata *nvd;
99 int start;
100
101 if (priv->state_size > MMC_MAX_BLOCK_LEN)
102 return log_msg_ret("state", -E2BIG);
103
104 start = priv->area_start + priv->state_offset;
105 if (start & (MMC_MAX_BLOCK_LEN - 1))
106 return log_msg_ret("get", -EBADF);
107 start /= MMC_MAX_BLOCK_LEN;
108
109 if (blk_dread(desc, start, 1, buf) != 1)
110 return log_msg_ret("read", -EIO);
111 nvd = (struct simple_nvdata *)buf;
112 hdr_ver = (nvd->hdr & NVD_HDR_VER_MASK) >> NVD_HDR_VER_SHIFT;
113 hdr_size = (nvd->hdr & NVD_HDR_SIZE_MASK) >> NVD_HDR_SIZE_SHIFT;
114 if (hdr_ver != NVD_HDR_VER_CUR)
115 return log_msg_ret("hdr", -EPERM);
116 size = 1 << hdr_size;
117 if (size > sizeof(*nvd))
118 return log_msg_ret("sz", -ENOEXEC);
119
120 crc = crc8(0, buf + 1, size - 1);
121 if (crc != nvd->crc8)
122 return log_msg_ret("crc", -EPERM);
123 state->fw_vernum = nvd->fw_vernum;
124
125 log_debug("version=%s\n", state->fw_version);
126
127 return 0;
128}
129
130static int simple_read_state(struct udevice *dev, struct simple_state *state)
131{
132 ALLOC_CACHE_ALIGN_BUFFER(u8, buf, MMC_MAX_BLOCK_LEN);
133 struct simple_priv *priv = dev_get_priv(dev);
134 struct blk_desc *desc;
135 char devname[16];
136 const char *end;
137 int devnum;
138 int ret;
139
140 /* First figure out the block device */
141 log_debug("storage=%s\n", priv->storage);
142 devnum = trailing_strtoln_end(priv->storage, NULL, &end);
143 if (devnum == -1)
144 return log_msg_ret("num", -ENODEV);
145 if (end - priv->storage >= sizeof(devname))
146 return log_msg_ret("end", -E2BIG);
147 strlcpy(devname, priv->storage, end - priv->storage + 1);
148 log_debug("dev=%s, %x\n", devname, devnum);
149
150 desc = blk_get_dev(devname, devnum);
151 if (!desc)
152 return log_msg_ret("get", -ENXIO);
153
154 ret = simple_read_version(dev, desc, buf, state);
155 if (ret)
156 return log_msg_ret("ver", ret);
157
158 ret = simple_read_nvdata(dev, desc, buf, state);
159 if (ret)
160 return log_msg_ret("nvd", ret);
161
162 return 0;
163}
164
165static int vbe_simple_get_state_desc(struct udevice *dev, char *buf,
166 int maxsize)
167{
168 struct simple_state state;
169 int ret;
170
171 ret = simple_read_state(dev, &state);
172 if (ret)
173 return log_msg_ret("read", ret);
174
175 if (maxsize < 30)
176 return -ENOSPC;
177 snprintf(buf, maxsize, "Version: %s\nVernum: %x/%x", state.fw_version,
178 state.fw_vernum >> FWVER_KEY_SHIFT,
179 state.fw_vernum & FWVER_FW_MASK);
180
181 return 0;
182}
183
184static int vbe_simple_read_bootflow(struct udevice *dev, struct bootflow *bflow)
185{
186 /* To be implemented */
187
188 return -EINVAL;
189}
190
191static struct bootmeth_ops bootmeth_vbe_simple_ops = {
192 .get_state_desc = vbe_simple_get_state_desc,
193 .read_bootflow = vbe_simple_read_bootflow,
194 .read_file = bootmeth_common_read_file,
195};
196
197int vbe_simple_fixup_node(ofnode node, struct simple_state *state)
198{
199 char *version;
200 int ret;
201
202 version = strdup(state->fw_version);
203 if (!version)
Simon Glass87c97cb2022-10-11 09:47:16 -0600204 return log_msg_ret("dup", -ENOMEM);
Simon Glasscb47e212022-07-30 15:52:33 -0600205
206 ret = ofnode_write_string(node, "cur-version", version);
207 if (ret)
208 return log_msg_ret("ver", ret);
209 ret = ofnode_write_u32(node, "cur-vernum", state->fw_vernum);
210 if (ret)
Simon Glass87c97cb2022-10-11 09:47:16 -0600211 return log_msg_ret("num", ret);
Simon Glasscb47e212022-07-30 15:52:33 -0600212 ret = ofnode_write_string(node, "bootloader-version", version_string);
213 if (ret)
Simon Glass87c97cb2022-10-11 09:47:16 -0600214 return log_msg_ret("bl", ret);
Simon Glasscb47e212022-07-30 15:52:33 -0600215
216 return 0;
217}
218
219/**
220 * bootmeth_vbe_simple_ft_fixup() - Write out all VBE simple data to the DT
221 *
222 * @ctx: Context for event
223 * @event: Event to process
224 * @return 0 if OK, -ve on error
225 */
226static int bootmeth_vbe_simple_ft_fixup(void *ctx, struct event *event)
227{
228 oftree tree = event->data.ft_fixup.tree;
229 struct udevice *dev;
Simon Glasscb47e212022-07-30 15:52:33 -0600230
231 /*
232 * Ideally we would have driver model support for fixups, but that does
233 * not exist yet. It is a step too far to try to do this before VBE is
234 * in place.
235 */
Heinrich Schuchardt49740e02022-08-23 10:31:56 +0200236 for (vbe_find_first_device(&dev); dev; vbe_find_next_device(&dev)) {
Simon Glasscb47e212022-07-30 15:52:33 -0600237 struct simple_state state;
Simon Glass87c97cb2022-10-11 09:47:16 -0600238 ofnode node, subnode;
Heinrich Schuchardt49740e02022-08-23 10:31:56 +0200239 int ret;
Simon Glasscb47e212022-07-30 15:52:33 -0600240
241 if (strcmp("vbe_simple", dev->driver->name))
242 continue;
243
244 /* Check if there is a node to fix up */
Simon Glassb7bd94f2022-09-06 20:27:24 -0600245 node = oftree_path(tree, "/chosen/fwupd");
Simon Glasscb47e212022-07-30 15:52:33 -0600246 if (!ofnode_valid(node))
247 continue;
Simon Glass87c97cb2022-10-11 09:47:16 -0600248 subnode = ofnode_find_subnode(node, dev->name);
249 if (!ofnode_valid(subnode))
Simon Glasscb47e212022-07-30 15:52:33 -0600250 continue;
251
252 log_debug("Fixing up: %s\n", dev->name);
253 ret = device_probe(dev);
254 if (ret)
255 return log_msg_ret("probe", ret);
256 ret = simple_read_state(dev, &state);
257 if (ret)
258 return log_msg_ret("read", ret);
259
Simon Glass87c97cb2022-10-11 09:47:16 -0600260 ret = vbe_simple_fixup_node(subnode, &state);
Simon Glasscb47e212022-07-30 15:52:33 -0600261 if (ret)
262 return log_msg_ret("fix", ret);
263 }
264
265 return 0;
266}
267EVENT_SPY(EVT_FT_FIXUP, bootmeth_vbe_simple_ft_fixup);
268
269static int bootmeth_vbe_simple_probe(struct udevice *dev)
270{
271 struct simple_priv *priv = dev_get_priv(dev);
272
273 memset(priv, '\0', sizeof(*priv));
274 if (dev_read_u32(dev, "area-start", &priv->area_start) ||
275 dev_read_u32(dev, "area-size", &priv->area_size) ||
276 dev_read_u32(dev, "version-offset", &priv->version_offset) ||
277 dev_read_u32(dev, "version-size", &priv->version_size) ||
278 dev_read_u32(dev, "state-offset", &priv->state_offset) ||
279 dev_read_u32(dev, "state-size", &priv->state_size))
280 return log_msg_ret("read", -EINVAL);
281 dev_read_u32(dev, "skip-offset", &priv->skip_offset);
282 priv->storage = strdup(dev_read_string(dev, "storage"));
283 if (!priv->storage)
284 return log_msg_ret("str", -EINVAL);
285
286 return 0;
287}
288
289static int bootmeth_vbe_simple_bind(struct udevice *dev)
290{
291 struct bootmeth_uc_plat *plat = dev_get_uclass_plat(dev);
292
293 plat->desc = IS_ENABLED(CONFIG_BOOTSTD_FULL) ?
294 "VBE simple" : "vbe-simple";
295 plat->flags = BOOTMETHF_GLOBAL;
296
297 return 0;
298}
299
300#if CONFIG_IS_ENABLED(OF_REAL)
301static const struct udevice_id generic_simple_vbe_simple_ids[] = {
302 { .compatible = "fwupd,vbe-simple" },
303 { }
304};
305#endif
306
307U_BOOT_DRIVER(vbe_simple) = {
308 .name = "vbe_simple",
309 .id = UCLASS_BOOTMETH,
310 .of_match = of_match_ptr(generic_simple_vbe_simple_ids),
311 .ops = &bootmeth_vbe_simple_ops,
312 .bind = bootmeth_vbe_simple_bind,
313 .probe = bootmeth_vbe_simple_probe,
314 .flags = DM_FLAG_PRE_RELOC,
315 .priv_auto = sizeof(struct simple_priv),
316};