vbe: Move reading the version into the common file
authorSimon Glass <sjg@chromium.org>
Thu, 16 Jan 2025 01:27:08 +0000 (18:27 -0700)
committerTom Rini <trini@konsulko.com>
Wed, 22 Jan 2025 15:47:49 +0000 (09:47 -0600)
All VBE methods read a version string, so move this function into a
common file.

Signed-off-by: Simon Glass <sjg@chromium.org>
boot/vbe_common.c
boot/vbe_common.h
boot/vbe_simple.c

index ede452b..8bbcc37 100644 (file)
@@ -6,8 +6,9 @@
  * Written by Simon Glass <sjg@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;
+}
index 0cd9617..a122bea 100644 (file)
@@ -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 */
index 8a370cf..ad98ac3 100644 (file)
 #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);