···99#include <blk.h>
1010#include <memalign.h>
1111#include <spl.h>
1212+#include <u-boot/crc.h>
1213#include "vbe_common.h"
13141415int vbe_get_blk(const char *storage, struct udevice **blkp)
···59606061 return 0;
6162}
6363+6464+int vbe_read_nvdata(struct udevice *blk, ulong offset, ulong size, u8 *buf)
6565+{
6666+ uint hdr_ver, hdr_size, data_size, crc;
6767+ const struct vbe_nvdata *nvd;
6868+6969+ /* we can use an assert() here since we already read only one block */
7070+ assert(size <= MMC_MAX_BLOCK_LEN);
7171+7272+ /*
7373+ * We can use an assert() here since reading the wrong block will just
7474+ * cause invalid state to be (safely) read. If the crc passes, then we
7575+ * obtain invalid state and it will likely cause booting to fail.
7676+ *
7777+ * VBE relies on valid values being in U-Boot's devicetree, so this
7878+ * should not every be wrong on a production device.
7979+ */
8080+ assert(!(offset & (MMC_MAX_BLOCK_LEN - 1)));
8181+8282+ if (offset & (MMC_MAX_BLOCK_LEN - 1))
8383+ return log_msg_ret("get", -EBADF);
8484+ offset /= MMC_MAX_BLOCK_LEN;
8585+8686+ if (blk_read(blk, offset, 1, buf) != 1)
8787+ return log_msg_ret("read", -EIO);
8888+ nvd = (struct vbe_nvdata *)buf;
8989+ hdr_ver = (nvd->hdr & NVD_HDR_VER_MASK) >> NVD_HDR_VER_SHIFT;
9090+ hdr_size = (nvd->hdr & NVD_HDR_SIZE_MASK) >> NVD_HDR_SIZE_SHIFT;
9191+ if (hdr_ver != NVD_HDR_VER_CUR)
9292+ return log_msg_ret("hdr", -EPERM);
9393+ data_size = 1 << hdr_size;
9494+ if (!data_size || data_size > sizeof(*nvd))
9595+ return log_msg_ret("sz", -EPERM);
9696+9797+ crc = crc8(0, buf + 1, data_size - 1);
9898+ if (crc != nvd->crc8)
9999+ return log_msg_ret("crc", -EPERM);
100100+101101+ return 0;
102102+}
+16
boot/vbe_common.h
···7878int vbe_read_version(struct udevice *blk, ulong offset, char *version,
7979 int max_size);
80808181+/**
8282+ * vbe_read_nvdata() - Read non-volatile data from a block device
8383+ *
8484+ * Reads the VBE nvdata from a device. This function reads a single block from
8585+ * the device, so the nvdata cannot be larger than that.
8686+ *
8787+ * @blk: Device to read from
8888+ * @offset: Offset to read, in bytes
8989+ * @size: Number of bytes to read
9090+ * @buf: Buffer to hold the data
9191+ * Return: 0 if OK, -E2BIG if @size > block size, -EBADF if the offset is not
9292+ * block-aligned, -EIO if an I/O error occurred, -EPERM if the header version is
9393+ * incorrect, the header size is invalid or the data fails its CRC check
9494+ */
9595+int vbe_read_nvdata(struct udevice *blk, ulong offset, ulong size, u8 *buf);
9696+8197#endif /* __VBE_ABREC_H */
+8-36
boot/vbe_simple.c
···1818#include <vbe.h>
1919#include <dm/device-internal.h>
2020#include <dm/ofnode.h>
2121-#include <u-boot/crc.h>
2221#include "vbe_simple.h"
23222423static int simple_read_nvdata(const struct simple_priv *priv,
2525- struct udevice *blk, u8 *buf,
2626- struct simple_state *state)
2424+ struct udevice *blk, struct simple_state *state)
2725{
2828- uint hdr_ver, hdr_size, size, crc;
2626+ ALLOC_CACHE_ALIGN_BUFFER(u8, buf, MMC_MAX_BLOCK_LEN);
2927 const struct vbe_nvdata *nvd;
3030- int start;
3131-3232- /* we can use an assert() here since we already read only one block */
3333- assert(priv->state_size <= MMC_MAX_BLOCK_LEN);
3434-3535- start = priv->area_start + priv->state_offset;
3636-3737- /*
3838- * We can use an assert() here since reading the wrong block will just
3939- * cause invalid state to be (safely) read. If the crc passes, then we
4040- * obtain invalid state and it will likely cause booting to fail.
4141- *
4242- * VBE relies on valid values being in U-Boot's devicetree, so this
4343- * should not every be wrong on a production device.
4444- */
4545- assert(!(start & (MMC_MAX_BLOCK_LEN - 1)));
2828+ int ret;
46294747- start /= MMC_MAX_BLOCK_LEN;
3030+ ret = vbe_read_nvdata(blk, priv->area_start + priv->state_offset,
3131+ priv->state_size, buf);
3232+ if (ret)
3333+ return log_msg_ret("nv", ret);
48344949- if (blk_read(blk, start, 1, buf) != 1)
5050- return log_msg_ret("read", -EIO);
5135 nvd = (struct vbe_nvdata *)buf;
5252- hdr_ver = (nvd->hdr & NVD_HDR_VER_MASK) >> NVD_HDR_VER_SHIFT;
5353- hdr_size = (nvd->hdr & NVD_HDR_SIZE_MASK) >> NVD_HDR_SIZE_SHIFT;
5454- if (hdr_ver != NVD_HDR_VER_CUR)
5555- return log_msg_ret("hdr", -EPERM);
5656- size = 1 << hdr_size;
5757- if (!size || size > sizeof(*nvd))
5858- return log_msg_ret("sz", -ENOEXEC);
5959-6060- crc = crc8(0, buf + 1, size - 1);
6161- if (crc != nvd->crc8)
6262- return log_msg_ret("crc", -EPERM);
6336 state->fw_vernum = nvd->fw_vernum;
64376538 log_debug("version=%s\n", state->fw_version);
···69427043int vbe_simple_read_state(struct udevice *dev, struct simple_state *state)
7144{
7272- ALLOC_CACHE_ALIGN_BUFFER(u8, buf, MMC_MAX_BLOCK_LEN);
7345 struct simple_priv *priv = dev_get_priv(dev);
7446 struct udevice *blk;
7547 int ret;
···8355 if (ret)
8456 return log_msg_ret("ver", ret);
85578686- ret = simple_read_nvdata(priv, blk, buf, state);
5858+ ret = simple_read_nvdata(priv, blk, state);
8759 if (ret)
8860 return log_msg_ret("nvd", ret);
8961