blk: sandbox: Support binding a device with a given logical block size

Allow optionally set the logical block size of the host device to
bind in the "host bind" command. If not given, defaults to 512.

Signed-off-by: Bin Meng <bmeng@tinylab.org>
This commit is contained in:
Bin Meng 2023-09-26 16:43:33 +08:00 committed by Tom Rini
parent 77ca9d7457
commit 8897faba2d
5 changed files with 37 additions and 14 deletions

View file

@ -13,6 +13,7 @@
#include <dm/device-internal.h>
#include <dm/uclass-internal.h>
#include <linux/errno.h>
#include <linux/log2.h>
static int do_host_load(struct cmd_tbl *cmdtp, int flag, int argc,
char *const argv[])
@ -45,6 +46,7 @@ static int do_host_bind(struct cmd_tbl *cmdtp, int flag, int argc,
struct udevice *dev;
const char *label;
char *file;
unsigned long blksz = DEFAULT_BLKSZ;
int ret;
/* Skip 'bind' */
@ -59,12 +61,19 @@ static int do_host_bind(struct cmd_tbl *cmdtp, int flag, int argc,
argv++;
}
if (argc != 2)
if (argc < 2 || argc > 3)
return CMD_RET_USAGE;
label = argv[0];
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");
return CMD_RET_FAILURE;
}
}
ret = host_create_attach_file(label, file, removable, &dev);
ret = host_create_attach_file(label, file, removable, blksz, &dev);
if (ret) {
printf("Cannot create device / bind file\n");
return CMD_RET_FAILURE;
@ -253,7 +262,8 @@ U_BOOT_CMD(
"host save hostfs - <addr> <filename> <bytes> [<offset>] - "
"save a file to host\n"
"host size hostfs - <filename> - determine size of file on host\n"
"host bind [-r] <label> <filename> - bind \"host\" device to file\n"
"host bind [-r] <label> <filename> [<blksz>] - bind \"host\" device to file,\n"
" and optionally set the device's logical block size\n"
" -r = mark as removable\n"
"host unbind <label> - unbind file from \"host\" device\n"
"host info [<label>] - show device binding & info\n"

View file

@ -13,6 +13,7 @@
#include <blk.h>
#include <dm.h>
#include <malloc.h>
#include <part.h>
#include <sandbox_host.h>
#include <dm/device-internal.h>
#include <dm/lists.h>
@ -29,7 +30,8 @@ struct host_priv {
struct udevice *cur_dev;
};
int host_create_device(const char *label, bool removable, struct udevice **devp)
int host_create_device(const char *label, bool removable, unsigned long blksz,
struct udevice **devp)
{
char dev_name[30], *str, *label_new;
struct host_sb_plat *plat;
@ -68,6 +70,12 @@ int host_create_device(const char *label, bool removable, struct udevice **devp)
struct blk_desc *desc = dev_get_uclass_plat(blk);
desc->removable = removable;
/* update blk device's block size with the provided one */
if (blksz != desc->blksz) {
desc->blksz = blksz;
desc->log2blksz = LOG2(desc->blksz);
}
}
plat = dev_get_plat(dev);
@ -95,12 +103,13 @@ int host_attach_file(struct udevice *dev, const char *filename)
}
int host_create_attach_file(const char *label, const char *filename,
bool removable, struct udevice **devp)
bool removable, unsigned long blksz,
struct udevice **devp)
{
struct udevice *dev;
int ret;
ret = host_create_device(label, removable, &dev);
ret = host_create_device(label, removable, blksz, &dev);
if (ret)
return log_msg_ret("cre", ret);

View file

@ -74,10 +74,11 @@ int host_detach_file(struct udevice *dev);
* @label: Label of the attachment, e.g. "test1"
* @removable: true if the device should be marked as removable, false
* if it is fixed. See enum blk_flag_t
* @blksz: logical block size of the device
* @devp: Returns the device created, on success
* Returns: 0 if OK, -ve on error
*/
int host_create_device(const char *label, bool removable,
int host_create_device(const char *label, bool removable, unsigned long blksz,
struct udevice **devp);
/**
@ -87,11 +88,13 @@ int host_create_device(const char *label, bool removable,
* @filename: Name of the file, e.g. "/path/to/disk.img"
* @removable: true if the device should be marked as removable, false
* if it is fixed. See enum blk_flag_t
* @blksz: logical block size of the device
* @devp: Returns the device created, on success
* Returns: 0 if OK, -ve on error
*/
int host_create_attach_file(const char *label, const char *filename,
bool removable, struct udevice **devp);
bool removable, unsigned long blksz,
struct udevice **devp);
/**
* host_find_by_label() - Find a host by label

View file

@ -4,6 +4,7 @@
*/
#include <common.h>
#include <blk.h>
#include <dm.h>
#include <part.h>
#include <sandbox_host.h>
@ -22,8 +23,8 @@ static int dm_test_blk_base(struct unit_test_state *uts)
struct udevice *blk0, *blk1, *dev0, *dev1, *dev, *chk0, *chk1;
/* Create two, one the parent of the other */
ut_assertok(host_create_device("test0", false, &dev0));
ut_assertok(host_create_device("test1", false, &dev1));
ut_assertok(host_create_device("test0", false, DEFAULT_BLKSZ, &dev0));
ut_assertok(host_create_device("test1", false, DEFAULT_BLKSZ, &dev1));
/* Check we can find them */
ut_assertok(blk_get_device(UCLASS_HOST, 0, &blk0));
@ -99,7 +100,7 @@ static int dm_test_blk_find(struct unit_test_state *uts)
{
struct udevice *blk, *chk, *dev;
ut_assertok(host_create_device("test0", false, &dev));
ut_assertok(host_create_device("test0", false, DEFAULT_BLKSZ, &dev));
ut_assertok(blk_find_device(UCLASS_HOST, 0, &chk));
ut_assertok(device_find_first_child_by_uclass(dev, UCLASS_BLK, &blk));

View file

@ -31,7 +31,7 @@ static int dm_test_host(struct unit_test_state *uts)
ut_asserteq(-ENODEV, uclass_first_device_err(UCLASS_PARTITION, &part));
mem_start = ut_check_delta(0);
ut_assertok(host_create_device(label, true, &dev));
ut_assertok(host_create_device(label, true, DEFAULT_BLKSZ, &dev));
/* Check that the plat data has been allocated */
plat = dev_get_plat(dev);
@ -83,7 +83,7 @@ static int dm_test_host_dup(struct unit_test_state *uts)
char fname[256];
ut_asserteq(0, uclass_id_count(UCLASS_HOST));
ut_assertok(host_create_device(label, true, &dev));
ut_assertok(host_create_device(label, true, DEFAULT_BLKSZ, &dev));
/* Attach a file created in test_ut_dm_init */
ut_assertok(os_persistent_file(fname, sizeof(fname), "2MB.ext2.img"));
@ -93,7 +93,7 @@ static int dm_test_host_dup(struct unit_test_state *uts)
ut_asserteq(1, uclass_id_count(UCLASS_HOST));
/* Create another device with the same label (should remove old one) */
ut_assertok(host_create_device(label, true, &dev));
ut_assertok(host_create_device(label, true, DEFAULT_BLKSZ, &dev));
/* Attach a different file created in test_ut_dm_init */
ut_assertok(os_persistent_file(fname, sizeof(fname), "1MB.fat32.img"));