diff options
author | Tom Rini | 2023-10-10 21:47:50 -0400 |
---|---|---|
committer | Tom Rini | 2023-10-10 21:47:50 -0400 |
commit | be98a786b67dfcccde1f18ec7cbfe1584e03cebe (patch) | |
tree | 8a5006ab4a8b4c96494c0973706e0fe8eb738aed | |
parent | 833ff23047c50e4053fb1bda21f4e2c9f6a3aa6a (diff) | |
parent | 5fdd48066e8a055576294c3846be8ea3914e7902 (diff) |
Merge branch '2023-10-10-blk-sandbox-support-binding-a-device-with-a-given-logical-block-size'
To quote the author:
At present on Sandbox when binding to a host backing file, the host
block device is created with a hard-coded 512 bytes block size.
Such assumption works for most cases, but for situation that with a raw
image file dump from a pre-formatted GPT partitioned disk image from a
4KiB block size device, when binding this file to a host device and mapping
this device to a blkmap, "blkmap" command like "blkmap part" won't work
correctly, due to block size mismatch during parsing the partition table.
This series updates Sandbox block driver, as well as the blkmap driver,
to get rid of the hard-coded 512 bytes block size assumption.
This series is available at u-boot-x86/blk for testing.
Test log (512 block size):
=> host bind 0 test.img
=> host info
dev blocks blksz label path
0 262144 512 0 test.img
=> blkmap create 0
Created "0"
=> blkmap map 0 0 40000 linear host 0 0
Block 0x0+0x40000 mapped to block 0x0 of "host 0"
=> blkmap info
Device 0: Vendor: U-Boot Rev: 1.0 Prod: blkmap
Type: Hard Disk
Capacity: 128.0 MB = 0.1 GB (262144 x 512)
=> blkmap part
Partition Map for BLKMAP device 0 -- Partition Type: EFI
Part Start LBA End LBA Name
Attributes
Type GUID
Partition GUID
1 0x00000022 0x000000bd "u-boot-spl"
attrs: 0x0000000000000000
type: 5b193300-fc78-40cd-8002-e86c45580b47
(5b193300-fc78-40cd-8002-e86c45580b47)
guid: 0bb6bb6e-4aac-4c27-be03-016b01e7b941
2 0x00000822 0x00000c84 "u-boot"
attrs: 0x0000000000000000
type: 2e54b353-1271-4842-806f-e436d6af6985
(2e54b353-1271-4842-806f-e436d6af6985)
guid: 91d50814-8e31-4cc0-97dc-779e1dc59056
3 0x00000c85 0x0000cc84 "rootfs"
attrs: 0x0000000000000004
type: 0fc63daf-8483-4772-8e79-3d69d8477de4
(linux)
guid: 42799722-6e55-46e6-afa9-529e7af3f03b
Test log (4096 block size):
=> host bind 0 test.img 4096
=> host info
dev blocks blksz label path
0 32768 4096 0 test.img
=> blkmap create 0
Created "0"
=> blkmap map 0 0 8000 linear host 0 0
Block 0x0+0x8000 mapped to block 0x0 of "host 0"
=> blkmap info
Device 0: Vendor: U-Boot Rev: 1.0 Prod: blkmap
Type: Hard Disk
Capacity: 128.0 MB = 0.1 GB (32768 x 4096)
=> blkmap part
Partition Map for BLKMAP device 0 -- Partition Type: EFI
Part Start LBA End LBA Name
Attributes
Type GUID
Partition GUID
1 0x00000100 0x00001fff "primary"
attrs: 0x0000000000000000
type: 0fc63daf-8483-4772-8e79-3d69d8477de4
(linux)
guid: eba904d7-72c1-4dbd-bb4e-36be49cba5e3
2 0x00002000 0x00007ffa "primary"
attrs: 0x0000000000000000
type: 0fc63daf-8483-4772-8e79-3d69d8477de4
(linux)
guid: c48c360e-db47-46da-ab87-26416fad3cd3
-rw-r--r-- | cmd/blk_common.c | 34 | ||||
-rw-r--r-- | cmd/blkmap.c | 7 | ||||
-rw-r--r-- | cmd/host.c | 25 | ||||
-rw-r--r-- | common/usb_storage.c | 4 | ||||
-rw-r--r-- | disk/part.c | 5 | ||||
-rw-r--r-- | drivers/ata/dwc_ahsata.c | 3 | ||||
-rw-r--r-- | drivers/ata/fsl_sata.c | 3 | ||||
-rw-r--r-- | drivers/ata/sata_mv.c | 3 | ||||
-rw-r--r-- | drivers/ata/sata_sil.c | 3 | ||||
-rw-r--r-- | drivers/block/blk-uclass.c | 51 | ||||
-rw-r--r-- | drivers/block/blkmap.c | 16 | ||||
-rw-r--r-- | drivers/block/host-uclass.c | 15 | ||||
-rw-r--r-- | drivers/block/host_dev.c | 11 | ||||
-rw-r--r-- | drivers/mmc/mmc-uclass.c | 2 | ||||
-rw-r--r-- | drivers/nvme/nvme.c | 2 | ||||
-rw-r--r-- | include/blk.h | 40 | ||||
-rw-r--r-- | include/sandbox_host.h | 7 | ||||
-rw-r--r-- | test/dm/blk.c | 7 | ||||
-rw-r--r-- | test/dm/host.c | 26 |
19 files changed, 129 insertions, 135 deletions
diff --git a/cmd/blk_common.c b/cmd/blk_common.c index 9f9d4327a99..02ac92837b6 100644 --- a/cmd/blk_common.c +++ b/cmd/blk_common.c @@ -25,18 +25,18 @@ int blk_common_cmd(int argc, char *const argv[], enum uclass_id uclass_id, case 2: if (strncmp(argv[1], "inf", 3) == 0) { blk_list_devices(uclass_id); - return 0; + return CMD_RET_SUCCESS; } else if (strncmp(argv[1], "dev", 3) == 0) { if (blk_print_device_num(uclass_id, *cur_devnump)) { printf("\nno %s devices available\n", if_name); return CMD_RET_FAILURE; } - return 0; + return CMD_RET_SUCCESS; } else if (strncmp(argv[1], "part", 4) == 0) { if (blk_list_part(uclass_id)) printf("\nno %s partition table available\n", if_name); - return 0; + return CMD_RET_SUCCESS; } return CMD_RET_USAGE; case 3: @@ -49,7 +49,7 @@ int blk_common_cmd(int argc, char *const argv[], enum uclass_id uclass_id, } else { return CMD_RET_FAILURE; } - return 0; + return CMD_RET_SUCCESS; } else if (strncmp(argv[1], "part", 4) == 0) { int dev = (int)dectoul(argv[2], NULL); @@ -58,7 +58,7 @@ int blk_common_cmd(int argc, char *const argv[], enum uclass_id uclass_id, if_name, dev); return CMD_RET_FAILURE; } - return 0; + return CMD_RET_SUCCESS; } return CMD_RET_USAGE; @@ -67,38 +67,46 @@ int blk_common_cmd(int argc, char *const argv[], enum uclass_id uclass_id, phys_addr_t paddr = hextoul(argv[2], NULL); lbaint_t blk = hextoul(argv[3], NULL); ulong cnt = hextoul(argv[4], NULL); + struct blk_desc *desc; void *vaddr; ulong n; + int ret; printf("\n%s read: device %d block # "LBAFU", count %lu ... ", if_name, *cur_devnump, blk, cnt); - vaddr = map_sysmem(paddr, 512 * cnt); - n = blk_read_devnum(uclass_id, *cur_devnump, blk, cnt, - vaddr); + ret = blk_get_desc(uclass_id, *cur_devnump, &desc); + if (ret) + return CMD_RET_FAILURE; + vaddr = map_sysmem(paddr, desc->blksz * cnt); + n = blk_dread(desc, blk, cnt, vaddr); unmap_sysmem(vaddr); printf("%ld blocks read: %s\n", n, n == cnt ? "OK" : "ERROR"); - return n == cnt ? 0 : 1; + return n == cnt ? CMD_RET_SUCCESS : CMD_RET_FAILURE; } else if (strcmp(argv[1], "write") == 0) { phys_addr_t paddr = hextoul(argv[2], NULL); lbaint_t blk = hextoul(argv[3], NULL); ulong cnt = hextoul(argv[4], NULL); + struct blk_desc *desc; void *vaddr; ulong n; + int ret; printf("\n%s write: device %d block # "LBAFU", count %lu ... ", if_name, *cur_devnump, blk, cnt); - vaddr = map_sysmem(paddr, 512 * cnt); - n = blk_write_devnum(uclass_id, *cur_devnump, blk, cnt, - vaddr); + ret = blk_get_desc(uclass_id, *cur_devnump, &desc); + if (ret) + return CMD_RET_FAILURE; + vaddr = map_sysmem(paddr, desc->blksz * cnt); + n = blk_dwrite(desc, blk, cnt, vaddr); unmap_sysmem(vaddr); printf("%ld blocks written: %s\n", n, n == cnt ? "OK" : "ERROR"); - return n == cnt ? 0 : 1; + return n == cnt ? CMD_RET_SUCCESS : CMD_RET_FAILURE; } else { return CMD_RET_USAGE; } diff --git a/cmd/blkmap.c b/cmd/blkmap.c index b34c0130728..ef74ebc0036 100644 --- a/cmd/blkmap.c +++ b/cmd/blkmap.c @@ -25,7 +25,8 @@ struct map_handler { map_parser_fn fn; }; -int do_blkmap_map_linear(struct map_ctx *ctx, int argc, char *const argv[]) +static int do_blkmap_map_linear(struct map_ctx *ctx, int argc, + char *const argv[]) { struct blk_desc *lbd; int err, ldevnum; @@ -58,7 +59,7 @@ int do_blkmap_map_linear(struct map_ctx *ctx, int argc, char *const argv[]) return CMD_RET_SUCCESS; } -int do_blkmap_map_mem(struct map_ctx *ctx, int argc, char *const argv[]) +static int do_blkmap_map_mem(struct map_ctx *ctx, int argc, char *const argv[]) { phys_addr_t addr; int err; @@ -80,7 +81,7 @@ int do_blkmap_map_mem(struct map_ctx *ctx, int argc, char *const argv[]) return CMD_RET_SUCCESS; } -struct map_handler map_handlers[] = { +static struct map_handler map_handlers[] = { { .name = "linear", .fn = do_blkmap_map_linear }, { .name = "mem", .fn = do_blkmap_map_mem }, diff --git a/cmd/host.c b/cmd/host.c index fb1cb1fdd1a..c33c2a9787e 100644 --- a/cmd/host.c +++ b/cmd/host.c @@ -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 = argc > 1 ? argv[1] : NULL; + 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; @@ -151,8 +160,8 @@ static void show_host_dev(struct udevice *dev) return; desc = dev_get_uclass_plat(blk); - printf("%12lu %-15s %s\n", (unsigned long)desc->lba, plat->label, - plat->filename); + printf("%12lu %6lu %-15s %s\n", (unsigned long)desc->lba, desc->blksz, + plat->label, plat->filename); } static int do_host_info(struct cmd_tbl *cmdtp, int flag, int argc, @@ -170,7 +179,8 @@ static int do_host_info(struct cmd_tbl *cmdtp, int flag, int argc, return CMD_RET_FAILURE; } - printf("%3s %12s %-15s %s\n", "dev", "blocks", "label", "path"); + printf("%3s %12s %6s %-15s %s\n", + "dev", "blocks", "blksz", "label", "path"); if (dev) { show_host_dev(dev); } else { @@ -253,7 +263,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" diff --git a/common/usb_storage.c b/common/usb_storage.c index 85774220ef2..35c656db0dc 100644 --- a/common/usb_storage.c +++ b/common/usb_storage.c @@ -219,8 +219,8 @@ static int usb_stor_probe_device(struct usb_device *udev) snprintf(str, sizeof(str), "lun%d", lun); ret = blk_create_devicef(udev->dev, "usb_storage_blk", str, - UCLASS_USB, usb_max_devs, 512, 0, - &dev); + UCLASS_USB, usb_max_devs, + DEFAULT_BLKSZ, 0, &dev); if (ret) { debug("Cannot bind driver\n"); return ret; diff --git a/disk/part.c b/disk/part.c index 72241b7b232..85244b09f35 100644 --- a/disk/part.c +++ b/disk/part.c @@ -336,8 +336,11 @@ static void print_part_header(const char *type, struct blk_desc *desc) case UCLASS_EFI_MEDIA: puts("EFI"); break; + case UCLASS_BLKMAP: + puts("BLKMAP"); + break; default: - puts("UNKNOWN"); + printf("UNKNOWN(%d)", desc->uclass_id); break; } printf (" device %d -- Partition Type: %s\n\n", diff --git a/drivers/ata/dwc_ahsata.c b/drivers/ata/dwc_ahsata.c index 6a4d861bf1e..b4d4e39c9b3 100644 --- a/drivers/ata/dwc_ahsata.c +++ b/drivers/ata/dwc_ahsata.c @@ -880,7 +880,8 @@ int dwc_ahsata_scan(struct udevice *dev) device_find_first_child(dev, &blk); if (!blk) { ret = blk_create_devicef(dev, "dwc_ahsata_blk", "blk", - UCLASS_AHCI, -1, 512, 0, &blk); + UCLASS_AHCI, -1, DEFAULT_BLKSZ, + 0, &blk); if (ret) { debug("Can't create device\n"); return ret; diff --git a/drivers/ata/fsl_sata.c b/drivers/ata/fsl_sata.c index 972101b29ce..969bc191f8e 100644 --- a/drivers/ata/fsl_sata.c +++ b/drivers/ata/fsl_sata.c @@ -888,7 +888,8 @@ static int fsl_ata_probe(struct udevice *dev) for (i = 0; i < nr_ports; i++) { snprintf(sata_name, sizeof(sata_name), "fsl_sata%d", i); ret = blk_create_devicef(dev, "sata_fsl_blk", sata_name, - UCLASS_AHCI, -1, 512, 0, &blk); + UCLASS_AHCI, -1, DEFAULT_BLKSZ, + 0, &blk); if (ret) { debug("Can't create device\n"); return ret; diff --git a/drivers/ata/sata_mv.c b/drivers/ata/sata_mv.c index 18c7a66db1b..1abea0b3093 100644 --- a/drivers/ata/sata_mv.c +++ b/drivers/ata/sata_mv.c @@ -1076,7 +1076,8 @@ static int sata_mv_probe(struct udevice *dev) for (i = 0; i < nr_ports; i++) { ret = blk_create_devicef(dev, "sata_mv_blk", "blk", - UCLASS_AHCI, -1, 512, 0, &blk); + UCLASS_AHCI, -1, DEFAULT_BLKSZ, + 0, &blk); if (ret) { debug("Can't create device\n"); continue; diff --git a/drivers/ata/sata_sil.c b/drivers/ata/sata_sil.c index b5e150d568b..43a91a79120 100644 --- a/drivers/ata/sata_sil.c +++ b/drivers/ata/sata_sil.c @@ -730,7 +730,8 @@ static int sil_pci_probe(struct udevice *dev) for (i = sata_info.portbase; i < sata_info.maxport; i++) { snprintf(sata_name, sizeof(sata_name), "sil_sata%d", i); ret = blk_create_devicef(dev, "sata_sil_blk", sata_name, - UCLASS_AHCI, -1, 512, 0, &blk); + UCLASS_AHCI, -1, DEFAULT_BLKSZ, + 0, &blk); if (ret) { debug("Can't create device\n"); return ret; diff --git a/drivers/block/blk-uclass.c b/drivers/block/blk-uclass.c index 885513893f6..f126547cc7e 100644 --- a/drivers/block/blk-uclass.c +++ b/drivers/block/blk-uclass.c @@ -178,17 +178,7 @@ struct blk_desc *blk_get_by_device(struct udevice *dev) return NULL; } -/** - * get_desc() - Get the block device descriptor for the given device number - * - * @uclass_id: Interface type - * @devnum: Device number (0 = first) - * @descp: Returns block device descriptor on success - * Return: 0 on success, -ENODEV if there is no such device and no device - * with a higher device number, -ENOENT if there is no such device but there - * is one with a higher number, or other -ve on other error. - */ -static int get_desc(enum uclass_id uclass_id, int devnum, struct blk_desc **descp) +int blk_get_desc(enum uclass_id uclass_id, int devnum, struct blk_desc **descp) { bool found_more = false; struct udevice *dev; @@ -240,7 +230,7 @@ int blk_list_part(enum uclass_id uclass_id) int ret; for (ok = 0, devnum = 0;; ++devnum) { - ret = get_desc(uclass_id, devnum, &desc); + ret = blk_get_desc(uclass_id, devnum, &desc); if (ret == -ENODEV) break; else if (ret) @@ -263,7 +253,7 @@ int blk_print_part_devnum(enum uclass_id uclass_id, int devnum) struct blk_desc *desc; int ret; - ret = get_desc(uclass_id, devnum, &desc); + ret = blk_get_desc(uclass_id, devnum, &desc); if (ret) return ret; if (desc->type == DEV_TYPE_UNKNOWN) @@ -280,7 +270,7 @@ void blk_list_devices(enum uclass_id uclass_id) int i; for (i = 0;; ++i) { - ret = get_desc(uclass_id, i, &desc); + ret = blk_get_desc(uclass_id, i, &desc); if (ret == -ENODEV) break; else if (ret) @@ -297,7 +287,7 @@ int blk_print_device_num(enum uclass_id uclass_id, int devnum) struct blk_desc *desc; int ret; - ret = get_desc(uclass_id, devnum, &desc); + ret = blk_get_desc(uclass_id, devnum, &desc); if (ret) return ret; printf("\nIDE device %d: ", devnum); @@ -312,7 +302,7 @@ int blk_show_device(enum uclass_id uclass_id, int devnum) int ret; printf("\nDevice %d: ", devnum); - ret = get_desc(uclass_id, devnum, &desc); + ret = blk_get_desc(uclass_id, devnum, &desc); if (ret == -ENODEV || ret == -ENOENT) { printf("unknown device\n"); return -ENODEV; @@ -327,35 +317,6 @@ int blk_show_device(enum uclass_id uclass_id, int devnum) return 0; } -ulong blk_read_devnum(enum uclass_id uclass_id, int devnum, lbaint_t start, - lbaint_t blkcnt, void *buffer) -{ - struct blk_desc *desc; - ulong n; - int ret; - - ret = get_desc(uclass_id, devnum, &desc); - if (ret) - return ret; - n = blk_dread(desc, start, blkcnt, buffer); - if (IS_ERR_VALUE(n)) - return n; - - return n; -} - -ulong blk_write_devnum(enum uclass_id uclass_id, int devnum, lbaint_t start, - lbaint_t blkcnt, const void *buffer) -{ - struct blk_desc *desc; - int ret; - - ret = get_desc(uclass_id, devnum, &desc); - if (ret) - return ret; - return blk_dwrite(desc, start, blkcnt, buffer); -} - int blk_select_hwpart(struct udevice *dev, int hwpart) { const struct blk_ops *ops = blk_get_ops(dev); diff --git a/drivers/block/blkmap.c b/drivers/block/blkmap.c index 2bb0acc20f2..149a4cac3ea 100644 --- a/drivers/block/blkmap.c +++ b/drivers/block/blkmap.c @@ -171,11 +171,11 @@ int blkmap_map_linear(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt, bd = dev_get_uclass_plat(bm->blk); lbd = dev_get_uclass_plat(lblk); - if (lbd->blksz != bd->blksz) - /* We could support block size translation, but we - * don't yet. - */ - return -EINVAL; + if (lbd->blksz != bd->blksz) { + /* update to match the mapped device */ + bd->blksz = lbd->blksz; + bd->log2blksz = LOG2(bd->blksz); + } linear = malloc(sizeof(*linear)); if (!linear) @@ -383,14 +383,14 @@ U_BOOT_DRIVER(blkmap_blk) = { .ops = &blkmap_blk_ops, }; -int blkmap_dev_bind(struct udevice *dev) +static int blkmap_dev_bind(struct udevice *dev) { struct blkmap *bm = dev_get_plat(dev); struct blk_desc *bd; int err; err = blk_create_devicef(dev, "blkmap_blk", "blk", UCLASS_BLKMAP, - dev_seq(dev), 512, 0, &bm->blk); + dev_seq(dev), DEFAULT_BLKSZ, 0, &bm->blk); if (err) return log_msg_ret("blk", err); @@ -410,7 +410,7 @@ int blkmap_dev_bind(struct udevice *dev) return 0; } -int blkmap_dev_unbind(struct udevice *dev) +static int blkmap_dev_unbind(struct udevice *dev) { struct blkmap *bm = dev_get_plat(dev); struct blkmap_slice *bms, *tmp; diff --git a/drivers/block/host-uclass.c b/drivers/block/host-uclass.c index 6460d968c23..b3647e3ce33 100644 --- a/drivers/block/host-uclass.c +++ b/drivers/block/host-uclass.c @@ -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); diff --git a/drivers/block/host_dev.c b/drivers/block/host_dev.c index 64422417b74..30c74157934 100644 --- a/drivers/block/host_dev.c +++ b/drivers/block/host_dev.c @@ -58,6 +58,11 @@ static int host_sb_attach_file(struct udevice *dev, const char *filename) size = os_filesize(fd); desc = dev_get_uclass_plat(blk); + if (size % desc->blksz) { + printf("The size of host backing file '%s' is not multiple of " + "the device block size\n", filename); + goto err_fname; + } desc->lba = size / desc->blksz; /* write this in last, when nothing can go wrong */ @@ -73,7 +78,7 @@ err_fname: return ret; } -int host_sb_detach_file(struct udevice *dev) +static int host_sb_detach_file(struct udevice *dev) { struct host_sb_plat *plat = dev_get_plat(dev); int ret; @@ -105,7 +110,7 @@ static int host_sb_bind(struct udevice *dev) int ret; ret = blk_create_devicef(dev, "sandbox_host_blk", "blk", UCLASS_HOST, - dev_seq(dev), 512, 0, &blk); + dev_seq(dev), DEFAULT_BLKSZ, 0, &blk); if (ret) return log_msg_ret("blk", ret); @@ -123,7 +128,7 @@ static int host_sb_bind(struct udevice *dev) return 0; } -struct host_ops host_sb_ops = { +static struct host_ops host_sb_ops = { .attach_file = host_sb_attach_file, .detach_file = host_sb_detach_file, }; diff --git a/drivers/mmc/mmc-uclass.c b/drivers/mmc/mmc-uclass.c index 0e157672eae..328456831dd 100644 --- a/drivers/mmc/mmc-uclass.c +++ b/drivers/mmc/mmc-uclass.c @@ -412,7 +412,7 @@ int mmc_bind(struct udevice *dev, struct mmc *mmc, const struct mmc_config *cfg) debug("%s: alias devnum=%d\n", __func__, dev_seq(dev)); ret = blk_create_devicef(dev, "mmc_blk", "blk", UCLASS_MMC, - dev_seq(dev), 512, 0, &bdev); + dev_seq(dev), DEFAULT_BLKSZ, 0, &bdev); if (ret) { debug("Cannot create block device\n"); return ret; diff --git a/drivers/nvme/nvme.c b/drivers/nvme/nvme.c index 20dc910d8a3..c39cd41aa38 100644 --- a/drivers/nvme/nvme.c +++ b/drivers/nvme/nvme.c @@ -906,7 +906,7 @@ int nvme_init(struct udevice *udev) /* The real blksz and size will be set by nvme_blk_probe() */ ret = blk_create_devicef(udev, "nvme-blk", name, UCLASS_NVME, - -1, 512, 0, &ns_udev); + -1, DEFAULT_BLKSZ, 0, &ns_udev); if (ret) goto free_id; diff --git a/include/blk.h b/include/blk.h index 95e86e2d5d1..76bd5baf995 100644 --- a/include/blk.h +++ b/include/blk.h @@ -21,6 +21,8 @@ typedef ulong lbaint_t; #define LBAF "%" LBAFlength "x" #define LBAFU "%" LBAFlength "u" +#define DEFAULT_BLKSZ 512 + struct udevice; static inline bool blk_enabled(void) @@ -513,6 +515,18 @@ const char *blk_get_devtype(struct udevice *dev); */ struct blk_desc *blk_get_by_device(struct udevice *dev); +/** + * blk_get_desc() - Get the block device descriptor for the given device number + * + * @uclass_id: Interface type + * @devnum: Device number (0 = first) + * @descp: Returns block device descriptor on success + * Return: 0 on success, -ENODEV if there is no such device and no device + * with a higher device number, -ENOENT if there is no such device but there + * is one with a higher number, or other -ve on other error. + */ +int blk_get_desc(enum uclass_id uclass_id, int devnum, struct blk_desc **descp); + #else #include <errno.h> /* @@ -716,32 +730,6 @@ int blk_print_device_num(enum uclass_id uclass_id, int devnum); int blk_print_part_devnum(enum uclass_id uclass_id, int devnum); /** - * blk_read_devnum() - read blocks from a device - * - * @uclass_id: Block device type - * @devnum: Device number - * @start: Start block number to read (0=first) - * @blkcnt: Number of blocks to read - * @buffer: Address to write data to - * Return: number of blocks read, or -ve error number on error - */ -ulong blk_read_devnum(enum uclass_id uclass_id, int devnum, lbaint_t start, - lbaint_t blkcnt, void *buffer); - -/** - * blk_write_devnum() - write blocks to a device - * - * @uclass_id: Block device type - * @devnum: Device number - * @start: Start block number to write (0=first) - * @blkcnt: Number of blocks to write - * @buffer: Address to read data from - * Return: number of blocks written, or -ve error number on error - */ -ulong blk_write_devnum(enum uclass_id uclass_id, int devnum, lbaint_t start, - lbaint_t blkcnt, const void *buffer); - -/** * blk_select_hwpart_devnum() - select a hardware partition * * This is similar to blk_dselect_hwpart() but it looks up the interface and diff --git a/include/sandbox_host.h b/include/sandbox_host.h index ebd7d99b473..f7a5fc67230 100644 --- a/include/sandbox_host.h +++ b/include/sandbox_host.h @@ -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 diff --git a/test/dm/blk.c b/test/dm/blk.c index 446c4423e6f..799f1e4dc75 100644 --- a/test/dm/blk.c +++ b/test/dm/blk.c @@ -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)); diff --git a/test/dm/host.c b/test/dm/host.c index 85f21f9839e..ca05a36b313 100644 --- a/test/dm/host.c +++ b/test/dm/host.c @@ -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")); @@ -120,7 +120,7 @@ static int dm_test_cmd_host(struct unit_test_state *uts) /* first check 'host info' with binding */ ut_assertok(run_command("host info", 0)); - ut_assert_nextline("dev blocks label path"); + ut_assert_nextline("dev blocks blksz label path"); ut_assert_console_end(); ut_assertok(os_persistent_file(fname, sizeof(fname), "2MB.ext2.img")); @@ -133,8 +133,8 @@ static int dm_test_cmd_host(struct unit_test_state *uts) ut_asserteq(true, desc->removable); ut_assertok(run_command("host info", 0)); - ut_assert_nextline("dev blocks label path"); - ut_assert_nextlinen(" 0 4096 test2"); + ut_assert_nextline("dev blocks blksz label path"); + ut_assert_nextlinen(" 0 4096 512 test2"); ut_assert_console_end(); ut_assertok(os_persistent_file(fname, sizeof(fname), "1MB.fat32.img")); @@ -147,9 +147,9 @@ static int dm_test_cmd_host(struct unit_test_state *uts) ut_asserteq(false, desc->removable); ut_assertok(run_command("host info", 0)); - ut_assert_nextline("dev blocks label path"); - ut_assert_nextlinen(" 0 4096 test2"); - ut_assert_nextlinen(" 1 2048 fat"); + ut_assert_nextline("dev blocks blksz label path"); + ut_assert_nextlinen(" 0 4096 512 test2"); + ut_assert_nextlinen(" 1 2048 512 fat"); ut_assert_console_end(); ut_asserteq(1, run_command("host info test", 0)); @@ -157,8 +157,8 @@ static int dm_test_cmd_host(struct unit_test_state *uts) ut_assert_console_end(); ut_assertok(run_command("host info fat", 0)); - ut_assert_nextline("dev blocks label path"); - ut_assert_nextlinen(" 1 2048 fat"); + ut_assert_nextline("dev blocks blksz label path"); + ut_assert_nextlinen(" 1 2048 512 fat"); ut_assert_console_end(); /* check 'host dev' */ @@ -194,8 +194,8 @@ static int dm_test_cmd_host(struct unit_test_state *uts) ut_assert_console_end(); ut_assertok(run_command("host info", 0)); - ut_assert_nextline("dev blocks label path"); - ut_assert_nextlinen(" 1 2048 fat"); + ut_assert_nextline("dev blocks blksz label path"); + ut_assert_nextlinen(" 1 2048 512 fat"); ut_assert_console_end(); return 0; |