"Das U-Boot" Source Tree
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

vbe: Move reading the nvdata into the common file

All VBE methods read non-volatile data, so move this function into a
common file.

Signed-off-by: Simon Glass <sjg@chromium.org>

authored by

Simon Glass and committed by
Tom Rini
0a59dc41 47e56185

+65 -36
+41
boot/vbe_common.c
··· 9 9 #include <blk.h> 10 10 #include <memalign.h> 11 11 #include <spl.h> 12 + #include <u-boot/crc.h> 12 13 #include "vbe_common.h" 13 14 14 15 int vbe_get_blk(const char *storage, struct udevice **blkp) ··· 59 60 60 61 return 0; 61 62 } 63 + 64 + int vbe_read_nvdata(struct udevice *blk, ulong offset, ulong size, u8 *buf) 65 + { 66 + uint hdr_ver, hdr_size, data_size, crc; 67 + const struct vbe_nvdata *nvd; 68 + 69 + /* we can use an assert() here since we already read only one block */ 70 + assert(size <= MMC_MAX_BLOCK_LEN); 71 + 72 + /* 73 + * We can use an assert() here since reading the wrong block will just 74 + * cause invalid state to be (safely) read. If the crc passes, then we 75 + * obtain invalid state and it will likely cause booting to fail. 76 + * 77 + * VBE relies on valid values being in U-Boot's devicetree, so this 78 + * should not every be wrong on a production device. 79 + */ 80 + assert(!(offset & (MMC_MAX_BLOCK_LEN - 1))); 81 + 82 + if (offset & (MMC_MAX_BLOCK_LEN - 1)) 83 + return log_msg_ret("get", -EBADF); 84 + offset /= MMC_MAX_BLOCK_LEN; 85 + 86 + if (blk_read(blk, offset, 1, buf) != 1) 87 + return log_msg_ret("read", -EIO); 88 + nvd = (struct vbe_nvdata *)buf; 89 + hdr_ver = (nvd->hdr & NVD_HDR_VER_MASK) >> NVD_HDR_VER_SHIFT; 90 + hdr_size = (nvd->hdr & NVD_HDR_SIZE_MASK) >> NVD_HDR_SIZE_SHIFT; 91 + if (hdr_ver != NVD_HDR_VER_CUR) 92 + return log_msg_ret("hdr", -EPERM); 93 + data_size = 1 << hdr_size; 94 + if (!data_size || data_size > sizeof(*nvd)) 95 + return log_msg_ret("sz", -EPERM); 96 + 97 + crc = crc8(0, buf + 1, data_size - 1); 98 + if (crc != nvd->crc8) 99 + return log_msg_ret("crc", -EPERM); 100 + 101 + return 0; 102 + }
+16
boot/vbe_common.h
··· 78 78 int vbe_read_version(struct udevice *blk, ulong offset, char *version, 79 79 int max_size); 80 80 81 + /** 82 + * vbe_read_nvdata() - Read non-volatile data from a block device 83 + * 84 + * Reads the VBE nvdata from a device. This function reads a single block from 85 + * the device, so the nvdata cannot be larger than that. 86 + * 87 + * @blk: Device to read from 88 + * @offset: Offset to read, in bytes 89 + * @size: Number of bytes to read 90 + * @buf: Buffer to hold the data 91 + * Return: 0 if OK, -E2BIG if @size > block size, -EBADF if the offset is not 92 + * block-aligned, -EIO if an I/O error occurred, -EPERM if the header version is 93 + * incorrect, the header size is invalid or the data fails its CRC check 94 + */ 95 + int vbe_read_nvdata(struct udevice *blk, ulong offset, ulong size, u8 *buf); 96 + 81 97 #endif /* __VBE_ABREC_H */
+8 -36
boot/vbe_simple.c
··· 18 18 #include <vbe.h> 19 19 #include <dm/device-internal.h> 20 20 #include <dm/ofnode.h> 21 - #include <u-boot/crc.h> 22 21 #include "vbe_simple.h" 23 22 24 23 static int simple_read_nvdata(const struct simple_priv *priv, 25 - struct udevice *blk, u8 *buf, 26 - struct simple_state *state) 24 + struct udevice *blk, struct simple_state *state) 27 25 { 28 - uint hdr_ver, hdr_size, size, crc; 26 + ALLOC_CACHE_ALIGN_BUFFER(u8, buf, MMC_MAX_BLOCK_LEN); 29 27 const struct vbe_nvdata *nvd; 30 - int start; 31 - 32 - /* we can use an assert() here since we already read only one block */ 33 - assert(priv->state_size <= MMC_MAX_BLOCK_LEN); 34 - 35 - start = priv->area_start + priv->state_offset; 36 - 37 - /* 38 - * We can use an assert() here since reading the wrong block will just 39 - * cause invalid state to be (safely) read. If the crc passes, then we 40 - * obtain invalid state and it will likely cause booting to fail. 41 - * 42 - * VBE relies on valid values being in U-Boot's devicetree, so this 43 - * should not every be wrong on a production device. 44 - */ 45 - assert(!(start & (MMC_MAX_BLOCK_LEN - 1))); 28 + int ret; 46 29 47 - start /= MMC_MAX_BLOCK_LEN; 30 + ret = vbe_read_nvdata(blk, priv->area_start + priv->state_offset, 31 + priv->state_size, buf); 32 + if (ret) 33 + return log_msg_ret("nv", ret); 48 34 49 - if (blk_read(blk, start, 1, buf) != 1) 50 - return log_msg_ret("read", -EIO); 51 35 nvd = (struct vbe_nvdata *)buf; 52 - hdr_ver = (nvd->hdr & NVD_HDR_VER_MASK) >> NVD_HDR_VER_SHIFT; 53 - hdr_size = (nvd->hdr & NVD_HDR_SIZE_MASK) >> NVD_HDR_SIZE_SHIFT; 54 - if (hdr_ver != NVD_HDR_VER_CUR) 55 - return log_msg_ret("hdr", -EPERM); 56 - size = 1 << hdr_size; 57 - if (!size || size > sizeof(*nvd)) 58 - return log_msg_ret("sz", -ENOEXEC); 59 - 60 - crc = crc8(0, buf + 1, size - 1); 61 - if (crc != nvd->crc8) 62 - return log_msg_ret("crc", -EPERM); 63 36 state->fw_vernum = nvd->fw_vernum; 64 37 65 38 log_debug("version=%s\n", state->fw_version); ··· 69 42 70 43 int vbe_simple_read_state(struct udevice *dev, struct simple_state *state) 71 44 { 72 - ALLOC_CACHE_ALIGN_BUFFER(u8, buf, MMC_MAX_BLOCK_LEN); 73 45 struct simple_priv *priv = dev_get_priv(dev); 74 46 struct udevice *blk; 75 47 int ret; ··· 83 55 if (ret) 84 56 return log_msg_ret("ver", ret); 85 57 86 - ret = simple_read_nvdata(priv, blk, buf, state); 58 + ret = simple_read_nvdata(priv, blk, state); 87 59 if (ret) 88 60 return log_msg_ret("nvd", ret); 89 61