xref: /openbmc/u-boot/drivers/block/sandbox.c (revision ee943655)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2013 Henrik Nordstrom <henrik@henriknordstrom.net>
4  */
5 
6 #include <common.h>
7 #include <blk.h>
8 #include <dm.h>
9 #include <fdtdec.h>
10 #include <part.h>
11 #include <os.h>
12 #include <malloc.h>
13 #include <sandboxblockdev.h>
14 #include <linux/errno.h>
15 #include <dm/device-internal.h>
16 
17 DECLARE_GLOBAL_DATA_PTR;
18 
19 #ifndef CONFIG_BLK
20 static struct host_block_dev host_devices[CONFIG_HOST_MAX_DEVICES];
21 
22 static struct host_block_dev *find_host_device(int dev)
23 {
24 	if (dev >= 0 && dev < CONFIG_HOST_MAX_DEVICES)
25 		return &host_devices[dev];
26 
27 	return NULL;
28 }
29 #endif
30 
31 #ifdef CONFIG_BLK
32 static unsigned long host_block_read(struct udevice *dev,
33 				     unsigned long start, lbaint_t blkcnt,
34 				     void *buffer)
35 {
36 	struct host_block_dev *host_dev = dev_get_priv(dev);
37 	struct blk_desc *block_dev = dev_get_uclass_platdata(dev);
38 
39 #else
40 static unsigned long host_block_read(struct blk_desc *block_dev,
41 				     unsigned long start, lbaint_t blkcnt,
42 				     void *buffer)
43 {
44 	int dev = block_dev->devnum;
45 	struct host_block_dev *host_dev = find_host_device(dev);
46 
47 	if (!host_dev)
48 		return -1;
49 #endif
50 
51 	if (os_lseek(host_dev->fd, start * block_dev->blksz, OS_SEEK_SET) ==
52 			-1) {
53 		printf("ERROR: Invalid block %lx\n", start);
54 		return -1;
55 	}
56 	ssize_t len = os_read(host_dev->fd, buffer, blkcnt * block_dev->blksz);
57 	if (len >= 0)
58 		return len / block_dev->blksz;
59 	return -1;
60 }
61 
62 #ifdef CONFIG_BLK
63 static unsigned long host_block_write(struct udevice *dev,
64 				      unsigned long start, lbaint_t blkcnt,
65 				      const void *buffer)
66 {
67 	struct host_block_dev *host_dev = dev_get_priv(dev);
68 	struct blk_desc *block_dev = dev_get_uclass_platdata(dev);
69 #else
70 static unsigned long host_block_write(struct blk_desc *block_dev,
71 				      unsigned long start, lbaint_t blkcnt,
72 				      const void *buffer)
73 {
74 	int dev = block_dev->devnum;
75 	struct host_block_dev *host_dev = find_host_device(dev);
76 #endif
77 
78 	if (os_lseek(host_dev->fd, start * block_dev->blksz, OS_SEEK_SET) ==
79 			-1) {
80 		printf("ERROR: Invalid block %lx\n", start);
81 		return -1;
82 	}
83 	ssize_t len = os_write(host_dev->fd, buffer, blkcnt * block_dev->blksz);
84 	if (len >= 0)
85 		return len / block_dev->blksz;
86 	return -1;
87 }
88 
89 #ifdef CONFIG_BLK
90 int host_dev_bind(int devnum, char *filename)
91 {
92 	struct host_block_dev *host_dev;
93 	struct udevice *dev;
94 	char dev_name[20], *str, *fname;
95 	int ret, fd;
96 
97 	/* Remove and unbind the old device, if any */
98 	ret = blk_get_device(IF_TYPE_HOST, devnum, &dev);
99 	if (ret == 0) {
100 		ret = device_remove(dev, DM_REMOVE_NORMAL);
101 		if (ret)
102 			return ret;
103 		ret = device_unbind(dev);
104 		if (ret)
105 			return ret;
106 	} else if (ret != -ENODEV) {
107 		return ret;
108 	}
109 
110 	if (!filename)
111 		return 0;
112 
113 	snprintf(dev_name, sizeof(dev_name), "host%d", devnum);
114 	str = strdup(dev_name);
115 	if (!str)
116 		return -ENOMEM;
117 	fname = strdup(filename);
118 	if (!fname) {
119 		free(str);
120 		return -ENOMEM;
121 	}
122 
123 	fd = os_open(filename, OS_O_RDWR);
124 	if (fd == -1) {
125 		printf("Failed to access host backing file '%s'\n", filename);
126 		ret = -ENOENT;
127 		goto err;
128 	}
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);
132 	if (ret)
133 		goto err_file;
134 	ret = device_probe(dev);
135 	if (ret) {
136 		device_unbind(dev);
137 		goto err_file;
138 	}
139 
140 	host_dev = dev_get_priv(dev);
141 	host_dev->fd = fd;
142 	host_dev->filename = fname;
143 
144 	return blk_prepare_device(dev);
145 err_file:
146 	os_close(fd);
147 err:
148 	free(fname);
149 	free(str);
150 	return ret;
151 }
152 #else
153 int host_dev_bind(int dev, char *filename)
154 {
155 	struct host_block_dev *host_dev = find_host_device(dev);
156 
157 	if (!host_dev)
158 		return -1;
159 	if (host_dev->blk_dev.priv) {
160 		os_close(host_dev->fd);
161 		host_dev->blk_dev.priv = NULL;
162 	}
163 	if (host_dev->filename)
164 		free(host_dev->filename);
165 	if (filename && *filename) {
166 		host_dev->filename = strdup(filename);
167 	} else {
168 		host_dev->filename = NULL;
169 		return 0;
170 	}
171 
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",
175 		       host_dev->filename);
176 		return 1;
177 	}
178 
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;
188 	part_init(blk_dev);
189 
190 	return 0;
191 }
192 #endif
193 
194 int host_get_dev_err(int devnum, struct blk_desc **blk_devp)
195 {
196 #ifdef CONFIG_BLK
197 	struct udevice *dev;
198 	int ret;
199 
200 	ret = blk_get_device(IF_TYPE_HOST, devnum, &dev);
201 	if (ret)
202 		return ret;
203 	*blk_devp = dev_get_uclass_platdata(dev);
204 #else
205 	struct host_block_dev *host_dev = find_host_device(devnum);
206 
207 	if (!host_dev)
208 		return -ENODEV;
209 
210 	if (!host_dev->blk_dev.priv)
211 		return -ENOENT;
212 
213 	*blk_devp = &host_dev->blk_dev;
214 #endif
215 
216 	return 0;
217 }
218 
219 #ifdef CONFIG_BLK
220 static const struct blk_ops sandbox_host_blk_ops = {
221 	.read	= host_block_read,
222 	.write	= host_block_write,
223 };
224 
225 U_BOOT_DRIVER(sandbox_host_blk) = {
226 	.name		= "sandbox_host_blk",
227 	.id		= UCLASS_BLK,
228 	.ops		= &sandbox_host_blk_ops,
229 	.priv_auto_alloc_size	= sizeof(struct host_block_dev),
230 };
231 #else
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,
237 };
238 #endif
239