@@ -67,8 +67,9 @@ static int do_host_bind(struct cmd_tbl *cmdtp, int flag, int argc,
file = argv[1];
if (argc > 2) {
blksz = dectoul(argv[2], NULL);
- if (blksz < DEFAULT_BLKSZ || !is_power_of_2(blksz)) {
- printf("blksz must be >= 512 and power of 2\n");
+ if (!blk_valid_blksz(blksz)) {
+ printf("blksz must be a power of 2 from %d to %d\n",
+ DEFAULT_BLKSZ, BLK_MAX_BLKSZ);
return CMD_RET_FAILURE;
}
}
@@ -258,7 +258,7 @@ static int virtio_blk_probe(struct udevice *dev)
if (virtio_has_feature(dev, VIRTIO_BLK_F_BLK_SIZE)) {
virtio_cread(dev, struct virtio_blk_config, blk_size, &blk_size);
desc->blksz = blk_size;
- if (!is_power_of_2(blk_size) || desc->blksz < 512)
+ if (!blk_valid_blksz(blk_size))
return -EIO;
} else {
desc->blksz = 512;
@@ -10,6 +10,8 @@
#include <bouncebuf.h>
#include <dm/uclass-id.h>
#include <efi.h>
+#include <linux/log2.h>
+#include <linux/sizes.h>
#ifdef CONFIG_SYS_64BIT_LBA
typedef uint64_t lbaint_t;
@@ -22,9 +24,28 @@ typedef ulong lbaint_t;
#define LBAFU "%" LBAFlength "u"
#define DEFAULT_BLKSZ 512
+/* Largest block size supported, as in Linux */
+#define BLK_MAX_BLKSZ SZ_64K
struct udevice;
+/**
+ * blk_valid_blksz() - Check that a block size is supported
+ *
+ * The block, partition and filesystem code size buffers by the block size,
+ * expect a block to hold at least a 512-byte boot sector and convert between
+ * bytes and blocks with log2(block size), so a device is only usable with a
+ * block size that is a power of two from DEFAULT_BLKSZ to BLK_MAX_BLKSZ.
+ *
+ * @blksz: Block size in bytes
+ * Return: true if @blksz is supported, false if not
+ */
+static inline bool blk_valid_blksz(unsigned long blksz)
+{
+ return blksz >= DEFAULT_BLKSZ && blksz <= BLK_MAX_BLKSZ &&
+ is_power_of_2(blksz);
+}
+
static inline bool blk_enabled(void)
{
return CONFIG_IS_ENABLED(BLK) || IS_ENABLED(CONFIG_SPL_LEGACY_BLOCK);