[PATCH v2 07/22] vbe: Move reading the version into the common file
Simon Glass
sjg at chromium.org
Thu Jan 16 02:27:08 CET 2025
All VBE methods read a version string, so move this function into a
common file.
Signed-off-by: Simon Glass <sjg at chromium.org>
---
Changes in v2:
- Split patch into several pieces
- Reduce inclusion of headers to only those necessary
boot/vbe_common.c | 29 +++++++++++++++++++++++++++--
boot/vbe_common.h | 17 +++++++++++++++++
boot/vbe_simple.c | 30 ++----------------------------
3 files changed, 46 insertions(+), 30 deletions(-)
diff --git a/boot/vbe_common.c b/boot/vbe_common.c
index ede452ba306..8bbcc37e67e 100644
--- a/boot/vbe_common.c
+++ b/boot/vbe_common.c
@@ -6,8 +6,9 @@
* Written by Simon Glass <sjg at chromium.org>
*/
-#include <part.h>
-#include <vsprintf.h>
+#include <blk.h>
+#include <memalign.h>
+#include <spl.h>
#include "vbe_common.h"
int vbe_get_blk(const char *storage, struct udevice **blkp)
@@ -34,3 +35,27 @@ int vbe_get_blk(const char *storage, struct udevice **blkp)
return 0;
}
+
+int vbe_read_version(struct udevice *blk, ulong offset, char *version,
+ int max_size)
+{
+ ALLOC_CACHE_ALIGN_BUFFER(u8, buf, MMC_MAX_BLOCK_LEN);
+
+ /* we can use an assert() here since we already read only one block */
+ assert(max_size <= MMC_MAX_BLOCK_LEN);
+
+ /*
+ * we can use an assert() here since reading the wrong block will just
+ * cause an invalid version-string to be (safely) read
+ */
+ assert(!(offset & (MMC_MAX_BLOCK_LEN - 1)));
+
+ offset /= MMC_MAX_BLOCK_LEN;
+
+ if (blk_read(blk, offset, 1, buf) != 1)
+ return log_msg_ret("read", -EIO);
+ strlcpy(version, buf, max_size);
+ log_debug("version=%s\n", version);
+
+ return 0;
+}
diff --git a/boot/vbe_common.h b/boot/vbe_common.h
index 0cd9617b5b1..a122bead93e 100644
--- a/boot/vbe_common.h
+++ b/boot/vbe_common.h
@@ -61,4 +61,21 @@ struct vbe_nvdata {
*/
int vbe_get_blk(const char *storage, struct udevice **blkp);
+/**
+ * vbe_read_version() - Read version-string from a block device
+ *
+ * Reads the VBE version-string from a device. This function reads a single
+ * block from the device, so the string cannot be larger than that. It uses a
+ * temporary buffer for the read, then copies in up to @size bytes
+ *
+ * @blk: Device to read from
+ * @offset: Offset to read, in bytes
+ * @version: Place to put the string
+ * @max_size: Maximum size of @version
+ * Return: 0 if OK, -E2BIG if @max_size > block size, -EBADF if the offset is
+ * not block-aligned, -EIO if an I/O error occurred
+ */
+int vbe_read_version(struct udevice *blk, ulong offset, char *version,
+ int max_size);
+
#endif /* __VBE_ABREC_H */
diff --git a/boot/vbe_simple.c b/boot/vbe_simple.c
index ee51804e02d..ce385065321 100644
--- a/boot/vbe_simple.c
+++ b/boot/vbe_simple.c
@@ -21,33 +21,6 @@
#include <u-boot/crc.h>
#include "vbe_simple.h"
-static int simple_read_version(const struct simple_priv *priv,
- struct udevice *blk, u8 *buf,
- struct simple_state *state)
-{
- int start;
-
- /* we can use an assert() here since we already read only one block */
- assert(priv->version_size <= MMC_MAX_BLOCK_LEN);
-
- start = priv->area_start + priv->version_offset;
-
- /*
- * we can use an assert() here since reading the wrong block will just
- * cause an invalid version-string to be (safely) read
- */
- assert(!(start & (MMC_MAX_BLOCK_LEN - 1)));
-
- start /= MMC_MAX_BLOCK_LEN;
-
- if (blk_read(blk, start, 1, buf) != 1)
- return log_msg_ret("read", -EIO);
- strlcpy(state->fw_version, buf, MAX_VERSION_LEN);
- log_debug("version=%s\n", state->fw_version);
-
- return 0;
-}
-
static int simple_read_nvdata(const struct simple_priv *priv,
struct udevice *blk, u8 *buf,
struct simple_state *state)
@@ -105,7 +78,8 @@ int vbe_simple_read_state(struct udevice *dev, struct simple_state *state)
if (ret)
return log_msg_ret("blk", ret);
- ret = simple_read_version(priv, blk, buf, state);
+ ret = vbe_read_version(blk, priv->area_start + priv->version_offset,
+ state->fw_version, MAX_VERSION_LEN);
if (ret)
return log_msg_ret("ver", ret);
--
2.34.1
More information about the U-Boot
mailing list