vbe: Create a common function to get the block device
authorSimon Glass <sjg@chromium.org>
Thu, 16 Jan 2025 01:27:07 +0000 (18:27 -0700)
committerTom Rini <trini@konsulko.com>
Wed, 22 Jan 2025 15:47:49 +0000 (09:47 -0600)
Add a vbe_get_blk() function and use it to obtain the block device used
by VBE.

Signed-off-by: Simon Glass <sjg@chromium.org>
boot/Makefile
boot/vbe_common.c [new file with mode: 0644]
boot/vbe_common.h
boot/vbe_simple.c

index a24fd90..c2753de 100644 (file)
@@ -65,7 +65,7 @@ endif
 
 obj-$(CONFIG_$(PHASE_)BOOTMETH_VBE) += vbe.o
 obj-$(CONFIG_$(PHASE_)BOOTMETH_VBE_REQUEST) += vbe_request.o
-obj-$(CONFIG_$(PHASE_)BOOTMETH_VBE_SIMPLE) += vbe_simple.o
+obj-$(CONFIG_$(PHASE_)BOOTMETH_VBE_SIMPLE) += vbe_simple.o vbe_common.o
 obj-$(CONFIG_$(PHASE_)BOOTMETH_VBE_SIMPLE_FW) += vbe_simple_fw.o
 obj-$(CONFIG_$(PHASE_)BOOTMETH_VBE_SIMPLE_OS) += vbe_simple_os.o
 
diff --git a/boot/vbe_common.c b/boot/vbe_common.c
new file mode 100644 (file)
index 0000000..ede452b
--- /dev/null
@@ -0,0 +1,36 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Verified Boot for Embedded (VBE) common functions
+ *
+ * Copyright 2024 Google LLC
+ * Written by Simon Glass <sjg@chromium.org>
+ */
+
+#include <part.h>
+#include <vsprintf.h>
+#include "vbe_common.h"
+
+int vbe_get_blk(const char *storage, struct udevice **blkp)
+{
+       struct blk_desc *desc;
+       char devname[16];
+       const char *end;
+       int devnum;
+
+       /* First figure out the block device */
+       log_debug("storage=%s\n", storage);
+       devnum = trailing_strtoln_end(storage, NULL, &end);
+       if (devnum == -1)
+               return log_msg_ret("num", -ENODEV);
+       if (end - storage >= sizeof(devname))
+               return log_msg_ret("end", -E2BIG);
+       strlcpy(devname, storage, end - storage + 1);
+       log_debug("dev=%s, %x\n", devname, devnum);
+
+       desc = blk_get_dev(devname, devnum);
+       if (!desc)
+               return log_msg_ret("get", -ENXIO);
+       *blkp = desc->bdev;
+
+       return 0;
+}
index 1fc70ee..0cd9617 100644 (file)
@@ -48,4 +48,17 @@ struct vbe_nvdata {
        u8 spare2[0x34];
 };
 
+/**
+ * vbe_get_blk() - Obtain the block device to use for VBE
+ *
+ * Decodes the string to produce a block device
+ *
+ * @storage: String indicating the device to use, e.g. "mmc1"
+ * @blkp: Returns associated block device, on success
+ * Return 0 if OK, -ENODEV if @storage does not end with a number, -E2BIG if
+ * the device name is more than 15 characters, -ENXIO if the block device could
+ * not be found
+ */
+int vbe_get_blk(const char *storage, struct udevice **blkp);
+
 #endif /* __VBE_ABREC_H */
index 313f063..8a370cf 100644 (file)
@@ -98,28 +98,13 @@ int vbe_simple_read_state(struct udevice *dev, struct simple_state *state)
 {
        ALLOC_CACHE_ALIGN_BUFFER(u8, buf, MMC_MAX_BLOCK_LEN);
        struct simple_priv *priv = dev_get_priv(dev);
-       struct blk_desc *desc;
        struct udevice *blk;
-       char devname[16];
-       const char *end;
-       int devnum;
        int ret;
 
-       /* First figure out the block device */
-       log_debug("storage=%s\n", priv->storage);
-       devnum = trailing_strtoln_end(priv->storage, NULL, &end);
-       if (devnum == -1)
-               return log_msg_ret("num", -ENODEV);
-       if (end - priv->storage >= sizeof(devname))
-               return log_msg_ret("end", -E2BIG);
-       strlcpy(devname, priv->storage, end - priv->storage + 1);
-       log_debug("dev=%s, %x\n", devname, devnum);
-
-       desc = blk_get_dev(devname, devnum);
-       if (!desc)
-               return log_msg_ret("get", -ENXIO);
-
-       blk = desc->bdev;
+       ret = vbe_get_blk(priv->storage, &blk);
+       if (ret)
+               return log_msg_ret("blk", ret);
+
        ret = simple_read_version(priv, blk, buf, state);
        if (ret)
                return log_msg_ret("ver", ret);