Simon Glass | d0af04c | 2022-07-30 15:52:33 -0600 | [diff] [blame] | 1 | // 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 Glass | 571707b | 2022-10-11 09:47:16 -0600 | [diff] [blame] | 9 | #define LOG_CATEGORY LOGC_BOOT |
| 10 | |
Simon Glass | d0af04c | 2022-07-30 15:52:33 -0600 | [diff] [blame] | 11 | #include <common.h> |
Simon Glass | d78aa75 | 2022-10-20 18:23:10 -0600 | [diff] [blame] | 12 | #include <bootdev.h> |
Simon Glass | d0af04c | 2022-07-30 15:52:33 -0600 | [diff] [blame] | 13 | #include <bootflow.h> |
| 14 | #include <bootmeth.h> |
| 15 | #include <dm.h> |
Simon Glass | d78aa75 | 2022-10-20 18:23:10 -0600 | [diff] [blame] | 16 | #include <log.h> |
| 17 | #include <memalign.h> |
Simon Glass | d0af04c | 2022-07-30 15:52:33 -0600 | [diff] [blame] | 18 | #include <mmc.h> |
| 19 | #include <vbe.h> |
Simon Glass | d0af04c | 2022-07-30 15:52:33 -0600 | [diff] [blame] | 20 | #include <dm/device-internal.h> |
| 21 | #include <dm/ofnode.h> |
| 22 | #include <u-boot/crc.h> |
Simon Glass | d78aa75 | 2022-10-20 18:23:10 -0600 | [diff] [blame] | 23 | #include "vbe_simple.h" |
Simon Glass | d0af04c | 2022-07-30 15:52:33 -0600 | [diff] [blame] | 24 | |
Simon Glass | d0af04c | 2022-07-30 15:52:33 -0600 | [diff] [blame] | 25 | /** struct simple_nvdata - storage format for non-volatile data */ |
| 26 | struct simple_nvdata { |
| 27 | u8 crc8; |
| 28 | u8 hdr; |
| 29 | u16 spare1; |
| 30 | u32 fw_vernum; |
| 31 | u8 spare2[0x38]; |
| 32 | }; |
| 33 | |
| 34 | static int simple_read_version(struct udevice *dev, struct blk_desc *desc, |
| 35 | u8 *buf, struct simple_state *state) |
| 36 | { |
| 37 | struct simple_priv *priv = dev_get_priv(dev); |
| 38 | int start; |
| 39 | |
| 40 | if (priv->version_size > MMC_MAX_BLOCK_LEN) |
| 41 | return log_msg_ret("ver", -E2BIG); |
| 42 | |
| 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; |
| 47 | |
| 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); |
| 52 | |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | static int simple_read_nvdata(struct udevice *dev, struct blk_desc *desc, |
| 57 | u8 *buf, struct simple_state *state) |
| 58 | { |
| 59 | struct simple_priv *priv = dev_get_priv(dev); |
| 60 | uint hdr_ver, hdr_size, size, crc; |
| 61 | const struct simple_nvdata *nvd; |
| 62 | int start; |
| 63 | |
| 64 | if (priv->state_size > MMC_MAX_BLOCK_LEN) |
| 65 | return log_msg_ret("state", -E2BIG); |
| 66 | |
| 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; |
| 71 | |
| 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); |
| 79 | size = 1 << hdr_size; |
| 80 | if (size > sizeof(*nvd)) |
| 81 | return log_msg_ret("sz", -ENOEXEC); |
| 82 | |
| 83 | crc = crc8(0, buf + 1, size - 1); |
| 84 | if (crc != nvd->crc8) |
| 85 | return log_msg_ret("crc", -EPERM); |
| 86 | state->fw_vernum = nvd->fw_vernum; |
| 87 | |
| 88 | log_debug("version=%s\n", state->fw_version); |
| 89 | |
| 90 | return 0; |
| 91 | } |
| 92 | |
Simon Glass | 39db199 | 2022-10-20 18:23:11 -0600 | [diff] [blame] | 93 | int vbe_simple_read_state(struct udevice *dev, struct simple_state *state) |
Simon Glass | d0af04c | 2022-07-30 15:52:33 -0600 | [diff] [blame] | 94 | { |
| 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; |
| 98 | char devname[16]; |
| 99 | const char *end; |
| 100 | int devnum; |
| 101 | int ret; |
| 102 | |
| 103 | /* First figure out the block device */ |
| 104 | log_debug("storage=%s\n", priv->storage); |
| 105 | devnum = trailing_strtoln_end(priv->storage, NULL, &end); |
| 106 | if (devnum == -1) |
| 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); |
| 112 | |
| 113 | desc = blk_get_dev(devname, devnum); |
| 114 | if (!desc) |
| 115 | return log_msg_ret("get", -ENXIO); |
| 116 | |
| 117 | ret = simple_read_version(dev, desc, buf, state); |
| 118 | if (ret) |
| 119 | return log_msg_ret("ver", ret); |
| 120 | |
| 121 | ret = simple_read_nvdata(dev, desc, buf, state); |
| 122 | if (ret) |
| 123 | return log_msg_ret("nvd", ret); |
| 124 | |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | static int vbe_simple_get_state_desc(struct udevice *dev, char *buf, |
| 129 | int maxsize) |
| 130 | { |
| 131 | struct simple_state state; |
| 132 | int ret; |
| 133 | |
Simon Glass | 39db199 | 2022-10-20 18:23:11 -0600 | [diff] [blame] | 134 | ret = vbe_simple_read_state(dev, &state); |
Simon Glass | d0af04c | 2022-07-30 15:52:33 -0600 | [diff] [blame] | 135 | if (ret) |
| 136 | return log_msg_ret("read", ret); |
| 137 | |
| 138 | if (maxsize < 30) |
| 139 | return -ENOSPC; |
| 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); |
| 143 | |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | static int vbe_simple_read_bootflow(struct udevice *dev, struct bootflow *bflow) |
| 148 | { |
Simon Glass | d78aa75 | 2022-10-20 18:23:10 -0600 | [diff] [blame] | 149 | int ret; |
| 150 | |
| 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 Glass | d0af04c | 2022-07-30 15:52:33 -0600 | [diff] [blame] | 157 | |
| 158 | return -EINVAL; |
| 159 | } |
| 160 | |
Simon Glass | d78aa75 | 2022-10-20 18:23:10 -0600 | [diff] [blame] | 161 | static int vbe_simple_read_file(struct udevice *dev, struct bootflow *bflow, |
| 162 | const char *file_path, ulong addr, ulong *sizep) |
| 163 | { |
| 164 | int ret; |
| 165 | |
| 166 | if (vbe_phase() == VBE_PHASE_OS) { |
| 167 | ret = bootmeth_common_read_file(dev, bflow, file_path, addr, |
| 168 | sizep); |
| 169 | if (ret) |
| 170 | return log_msg_ret("os", ret); |
| 171 | } |
| 172 | |
| 173 | /* To be implemented */ |
| 174 | return -EINVAL; |
| 175 | } |
| 176 | |
Simon Glass | d0af04c | 2022-07-30 15:52:33 -0600 | [diff] [blame] | 177 | static struct bootmeth_ops bootmeth_vbe_simple_ops = { |
| 178 | .get_state_desc = vbe_simple_get_state_desc, |
| 179 | .read_bootflow = vbe_simple_read_bootflow, |
Simon Glass | d78aa75 | 2022-10-20 18:23:10 -0600 | [diff] [blame] | 180 | .read_file = vbe_simple_read_file, |
Simon Glass | d0af04c | 2022-07-30 15:52:33 -0600 | [diff] [blame] | 181 | }; |
| 182 | |
Simon Glass | d0af04c | 2022-07-30 15:52:33 -0600 | [diff] [blame] | 183 | static int bootmeth_vbe_simple_probe(struct udevice *dev) |
| 184 | { |
| 185 | struct simple_priv *priv = dev_get_priv(dev); |
| 186 | |
| 187 | memset(priv, '\0', sizeof(*priv)); |
| 188 | if (dev_read_u32(dev, "area-start", &priv->area_start) || |
| 189 | dev_read_u32(dev, "area-size", &priv->area_size) || |
| 190 | dev_read_u32(dev, "version-offset", &priv->version_offset) || |
| 191 | dev_read_u32(dev, "version-size", &priv->version_size) || |
| 192 | dev_read_u32(dev, "state-offset", &priv->state_offset) || |
| 193 | dev_read_u32(dev, "state-size", &priv->state_size)) |
| 194 | return log_msg_ret("read", -EINVAL); |
| 195 | dev_read_u32(dev, "skip-offset", &priv->skip_offset); |
| 196 | priv->storage = strdup(dev_read_string(dev, "storage")); |
| 197 | if (!priv->storage) |
| 198 | return log_msg_ret("str", -EINVAL); |
| 199 | |
| 200 | return 0; |
| 201 | } |
| 202 | |
| 203 | static int bootmeth_vbe_simple_bind(struct udevice *dev) |
| 204 | { |
| 205 | struct bootmeth_uc_plat *plat = dev_get_uclass_plat(dev); |
| 206 | |
| 207 | plat->desc = IS_ENABLED(CONFIG_BOOTSTD_FULL) ? |
| 208 | "VBE simple" : "vbe-simple"; |
| 209 | plat->flags = BOOTMETHF_GLOBAL; |
| 210 | |
| 211 | return 0; |
| 212 | } |
| 213 | |
| 214 | #if CONFIG_IS_ENABLED(OF_REAL) |
| 215 | static const struct udevice_id generic_simple_vbe_simple_ids[] = { |
| 216 | { .compatible = "fwupd,vbe-simple" }, |
| 217 | { } |
| 218 | }; |
| 219 | #endif |
| 220 | |
| 221 | U_BOOT_DRIVER(vbe_simple) = { |
| 222 | .name = "vbe_simple", |
| 223 | .id = UCLASS_BOOTMETH, |
| 224 | .of_match = of_match_ptr(generic_simple_vbe_simple_ids), |
| 225 | .ops = &bootmeth_vbe_simple_ops, |
| 226 | .bind = bootmeth_vbe_simple_bind, |
| 227 | .probe = bootmeth_vbe_simple_probe, |
| 228 | .flags = DM_FLAG_PRE_RELOC, |
| 229 | .priv_auto = sizeof(struct simple_priv), |
| 230 | }; |