1 // SPDX-License-Identifier: GPL-2.0+
13 #include <sandboxblockdev.h>
14 #include <asm/global_data.h>
15 #include <dm/device_compat.h>
16 #include <linux/errno.h>
17 #include <dm/device-internal.h>
19 DECLARE_GLOBAL_DATA_PTR;
22 static struct host_block_dev host_devices[SANDBOX_HOST_MAX_DEVICES];
24 static struct host_block_dev *find_host_device(int dev)
26 if (dev >= 0 && dev < SANDBOX_HOST_MAX_DEVICES)
27 return &host_devices[dev];
34 static unsigned long host_block_read(struct udevice *dev,
35 unsigned long start, lbaint_t blkcnt,
38 struct host_block_dev *host_dev = dev_get_plat(dev);
39 struct blk_desc *block_dev = dev_get_uclass_plat(dev);
42 static unsigned long host_block_read(struct blk_desc *block_dev,
43 unsigned long start, lbaint_t blkcnt,
46 int dev = block_dev->devnum;
47 struct host_block_dev *host_dev = find_host_device(dev);
53 if (os_lseek(host_dev->fd, start * block_dev->blksz, OS_SEEK_SET) ==
55 printf("ERROR: Invalid block %lx\n", start);
58 ssize_t len = os_read(host_dev->fd, buffer, blkcnt * block_dev->blksz);
60 return len / block_dev->blksz;
65 static unsigned long host_block_write(struct udevice *dev,
66 unsigned long start, lbaint_t blkcnt,
69 struct host_block_dev *host_dev = dev_get_plat(dev);
70 struct blk_desc *block_dev = dev_get_uclass_plat(dev);
72 static unsigned long host_block_write(struct blk_desc *block_dev,
73 unsigned long start, lbaint_t blkcnt,
76 int dev = block_dev->devnum;
77 struct host_block_dev *host_dev = find_host_device(dev);
80 if (os_lseek(host_dev->fd, start * block_dev->blksz, OS_SEEK_SET) ==
82 printf("ERROR: Invalid block %lx\n", start);
85 ssize_t len = os_write(host_dev->fd, buffer, blkcnt * block_dev->blksz);
87 return len / block_dev->blksz;
92 int host_dev_bind(int devnum, char *filename, bool removable)
94 struct host_block_dev *host_dev;
96 struct blk_desc *desc;
97 char dev_name[20], *str, *fname;
100 /* Remove and unbind the old device, if any */
101 ret = blk_get_device(UCLASS_ROOT, devnum, &dev);
103 ret = device_remove(dev, DM_REMOVE_NORMAL);
106 ret = device_unbind(dev);
109 } else if (ret != -ENODEV) {
116 snprintf(dev_name, sizeof(dev_name), "host%d", devnum);
117 str = strdup(dev_name);
120 fname = strdup(filename);
126 fd = os_open(filename, OS_O_RDWR);
128 printf("Failed to access host backing file '%s', trying read-only\n",
130 fd = os_open(filename, OS_O_RDONLY);
132 printf("- still failed\n");
137 ret = blk_create_device(gd->dm_root, "sandbox_host_blk", str,
138 UCLASS_ROOT, devnum, 512,
139 os_lseek(fd, 0, OS_SEEK_END) / 512, &dev);
143 host_dev = dev_get_plat(dev);
145 host_dev->filename = fname;
147 ret = device_probe(dev);
153 desc = blk_get_devnum_by_type(UCLASS_ROOT, devnum);
154 desc->removable = removable;
155 snprintf(desc->vendor, BLK_VEN_SIZE, "U-Boot");
156 snprintf(desc->product, BLK_PRD_SIZE, "hostfile");
157 snprintf(desc->revision, BLK_REV_SIZE, "1.0");
168 int host_dev_bind(int dev, char *filename, bool removable)
170 struct host_block_dev *host_dev = find_host_device(dev);
174 if (host_dev->blk_dev.priv) {
175 os_close(host_dev->fd);
176 host_dev->blk_dev.priv = NULL;
178 if (host_dev->filename)
179 free(host_dev->filename);
180 if (filename && *filename) {
181 host_dev->filename = strdup(filename);
183 host_dev->filename = NULL;
187 host_dev->fd = os_open(host_dev->filename, OS_O_RDWR);
188 if (host_dev->fd == -1) {
189 printf("Failed to access host backing file '%s'\n",
194 struct blk_desc *blk_dev = &host_dev->blk_dev;
195 blk_dev->if_type = UCLASS_ROOT;
196 blk_dev->priv = host_dev;
197 blk_dev->blksz = 512;
198 blk_dev->lba = os_lseek(host_dev->fd, 0, OS_SEEK_END) / blk_dev->blksz;
199 blk_dev->block_read = host_block_read;
200 blk_dev->block_write = host_block_write;
201 blk_dev->devnum = dev;
202 blk_dev->part_type = PART_TYPE_UNKNOWN;
203 blk_dev->removable = removable;
204 snprintf(blk_dev->vendor, BLK_VEN_SIZE, "U-Boot");
205 snprintf(blk_dev->product, BLK_PRD_SIZE, "hostfile");
206 snprintf(blk_dev->revision, BLK_REV_SIZE, "1.0");
213 int host_get_dev_err(int devnum, struct blk_desc **blk_devp)
219 ret = blk_get_device(UCLASS_ROOT, devnum, &dev);
222 *blk_devp = dev_get_uclass_plat(dev);
224 struct host_block_dev *host_dev = find_host_device(devnum);
229 if (!host_dev->blk_dev.priv)
232 *blk_devp = &host_dev->blk_dev;
240 int sandbox_host_unbind(struct udevice *dev)
242 struct host_block_dev *host_dev;
244 /* Data validity is checked in host_dev_bind() */
245 host_dev = dev_get_plat(dev);
246 os_close(host_dev->fd);
251 static const struct blk_ops sandbox_host_blk_ops = {
252 .read = host_block_read,
253 .write = host_block_write,
256 U_BOOT_DRIVER(sandbox_host_blk) = {
257 .name = "sandbox_host_blk",
259 .ops = &sandbox_host_blk_ops,
260 .unbind = sandbox_host_unbind,
261 .plat_auto = sizeof(struct host_block_dev),
264 U_BOOT_LEGACY_BLK(sandbox_host) = {
265 .if_typename = "host",
266 .if_type = UCLASS_ROOT,
267 .max_devs = SANDBOX_HOST_MAX_DEVICES,
268 .get_dev = host_get_dev_err,