vbe: Move reading the version into the common file

All VBE methods read a version string, so move this function into a
common file.

Signed-off-by: Simon Glass <sjg@chromium.org>
This commit is contained in:
Simon Glass
2025-01-15 18:27:08 -07:00
parent 6390fc0866
commit 7573cc498e
3 changed files with 46 additions and 30 deletions

View 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;
}

View 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 */

View File

@@ -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);