1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2013 Henrik Nordstrom <henrik@henriknordstrom.net>
13 #include <sandboxblockdev.h>
14 #include <linux/errno.h>
15 #include <dm/device-internal.h>
17 DECLARE_GLOBAL_DATA_PTR;
20 static struct host_block_dev host_devices[CONFIG_HOST_MAX_DEVICES];
22 static struct host_block_dev *find_host_device(int dev)
24 if (dev >= 0 && dev < CONFIG_HOST_MAX_DEVICES)
25 return &host_devices[dev];
32 static unsigned long host_block_read(struct udevice *dev,
33 unsigned long start, lbaint_t blkcnt,
36 struct host_block_dev *host_dev = dev_get_priv(dev);
37 struct blk_desc *block_dev = dev_get_uclass_platdata(dev);
40 static unsigned long host_block_read(struct blk_desc *block_dev,
41 unsigned long start, lbaint_t blkcnt,
44 int dev = block_dev->devnum;
45 struct host_block_dev *host_dev = find_host_device(dev);
51 if (os_lseek(host_dev->fd, start * block_dev->blksz, OS_SEEK_SET) ==
53 printf("ERROR: Invalid block %lx\n", start);
56 ssize_t len = os_read(host_dev->fd, buffer, blkcnt * block_dev->blksz);
58 return len / block_dev->blksz;
63 static unsigned long host_block_write(struct udevice *dev,
64 unsigned long start, lbaint_t blkcnt,
67 struct host_block_dev *host_dev = dev_get_priv(dev);
68 struct blk_desc *block_dev = dev_get_uclass_platdata(dev);
70 static unsigned long host_block_write(struct blk_desc *block_dev,
71 unsigned long start, lbaint_t blkcnt,
74 int dev = block_dev->devnum;
75 struct host_block_dev *host_dev = find_host_device(dev);
78 if (os_lseek(host_dev->fd, start * block_dev->blksz, OS_SEEK_SET) ==
80 printf("ERROR: Invalid block %lx\n", start);
83 ssize_t len = os_write(host_dev->fd, buffer, blkcnt * block_dev->blksz);
85 return len / block_dev->blksz;
90 int host_dev_bind(int devnum, char *filename)
92 struct host_block_dev *host_dev;
94 char dev_name[20], *str, *fname;
97 /* Remove and unbind the old device, if any */
98 ret = blk_get_device(IF_TYPE_HOST, devnum, &dev);
100 ret = device_remove(dev, DM_REMOVE_NORMAL);
103 ret = device_unbind(dev);
106 } else if (ret != -ENODEV) {
113 snprintf(dev_name, sizeof(dev_name), "host%d", devnum);
114 str = strdup(dev_name);
117 fname = strdup(filename);
123 fd = os_open(filename, OS_O_RDWR);
125 printf("Failed to access host backing file '%s'\n", filename);
129 ret = blk_create_device(gd->dm_root, "sandbox_host_blk", str,
130 IF_TYPE_HOST, devnum, 512,
131 os_lseek(fd, 0, OS_SEEK_END) / 512, &dev);
134 ret = device_probe(dev);
140 host_dev = dev_get_priv(dev);
142 host_dev->filename = fname;
144 return blk_prepare_device(dev);
153 int host_dev_bind(int dev, char *filename)
155 struct host_block_dev *host_dev = find_host_device(dev);
159 if (host_dev->blk_dev.priv) {
160 os_close(host_dev->fd);
161 host_dev->blk_dev.priv = NULL;
163 if (host_dev->filename)
164 free(host_dev->filename);
165 if (filename && *filename) {
166 host_dev->filename = strdup(filename);
168 host_dev->filename = NULL;
172 host_dev->fd = os_open(host_dev->filename, OS_O_RDWR);
173 if (host_dev->fd == -1) {
174 printf("Failed to access host backing file '%s'\n",
179 struct blk_desc *blk_dev = &host_dev->blk_dev;
180 blk_dev->if_type = IF_TYPE_HOST;
181 blk_dev->priv = host_dev;
182 blk_dev->blksz = 512;
183 blk_dev->lba = os_lseek(host_dev->fd, 0, OS_SEEK_END) / blk_dev->blksz;
184 blk_dev->block_read = host_block_read;
185 blk_dev->block_write = host_block_write;
186 blk_dev->devnum = dev;
187 blk_dev->part_type = PART_TYPE_UNKNOWN;
194 int host_get_dev_err(int devnum, struct blk_desc **blk_devp)
200 ret = blk_get_device(IF_TYPE_HOST, devnum, &dev);
203 *blk_devp = dev_get_uclass_platdata(dev);
205 struct host_block_dev *host_dev = find_host_device(devnum);
210 if (!host_dev->blk_dev.priv)
213 *blk_devp = &host_dev->blk_dev;
220 static const struct blk_ops sandbox_host_blk_ops = {
221 .read = host_block_read,
222 .write = host_block_write,
225 U_BOOT_DRIVER(sandbox_host_blk) = {
226 .name = "sandbox_host_blk",
228 .ops = &sandbox_host_blk_ops,
229 .priv_auto_alloc_size = sizeof(struct host_block_dev),
232 U_BOOT_LEGACY_BLK(sandbox_host) = {
233 .if_typename = "host",
234 .if_type = IF_TYPE_HOST,
235 .max_devs = CONFIG_HOST_MAX_DEVICES,
236 .get_dev = host_get_dev_err,