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 
find_host_device(int dev)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
host_block_read(struct udevice * dev,unsigned long start,lbaint_t blkcnt,void * buffer)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_platdata(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_platdata(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 
135 	host_dev = dev_get_platdata(dev);
136 	host_dev->fd = fd;
137 	host_dev->filename = fname;
138 
139 	ret = device_probe(dev);
140 	if (ret) {
141 		device_unbind(dev);
142 		goto err_file;
143 	}
144 
145 	return 0;
146 err_file:
147 	os_close(fd);
148 err:
149 	free(fname);
150 	free(str);
151 	return ret;
152 }
153 #else
154 int host_dev_bind(int dev, char *filename)
155 {
156 	struct host_block_dev *host_dev = find_host_device(dev);
157 
158 	if (!host_dev)
159 		return -1;
160 	if (host_dev->blk_dev.priv) {
161 		os_close(host_dev->fd);
162 		host_dev->blk_dev.priv = NULL;
163 	}
164 	if (host_dev->filename)
165 		free(host_dev->filename);
166 	if (filename && *filename) {
167 		host_dev->filename = strdup(filename);
168 	} else {
169 		host_dev->filename = NULL;
170 		return 0;
171 	}
172 
173 	host_dev->fd = os_open(host_dev->filename, OS_O_RDWR);
174 	if (host_dev->fd == -1) {
175 		printf("Failed to access host backing file '%s'\n",
176 		       host_dev->filename);
177 		return 1;
178 	}
179 
180 	struct blk_desc *blk_dev = &host_dev->blk_dev;
181 	blk_dev->if_type = IF_TYPE_HOST;
182 	blk_dev->priv = host_dev;
183 	blk_dev->blksz = 512;
184 	blk_dev->lba = os_lseek(host_dev->fd, 0, OS_SEEK_END) / blk_dev->blksz;
185 	blk_dev->block_read = host_block_read;
186 	blk_dev->block_write = host_block_write;
187 	blk_dev->devnum = dev;
188 	blk_dev->part_type = PART_TYPE_UNKNOWN;
189 	part_init(blk_dev);
190 
191 	return 0;
192 }
193 #endif
194 
195 int host_get_dev_err(int devnum, struct blk_desc **blk_devp)
196 {
197 #ifdef CONFIG_BLK
198 	struct udevice *dev;
199 	int ret;
200 
201 	ret = blk_get_device(IF_TYPE_HOST, devnum, &dev);
202 	if (ret)
203 		return ret;
204 	*blk_devp = dev_get_uclass_platdata(dev);
205 #else
206 	struct host_block_dev *host_dev = find_host_device(devnum);
207 
208 	if (!host_dev)
209 		return -ENODEV;
210 
211 	if (!host_dev->blk_dev.priv)
212 		return -ENOENT;
213 
214 	*blk_devp = &host_dev->blk_dev;
215 #endif
216 
217 	return 0;
218 }
219 
220 #ifdef CONFIG_BLK
221 static const struct blk_ops sandbox_host_blk_ops = {
222 	.read	= host_block_read,
223 	.write	= host_block_write,
224 };
225 
226 U_BOOT_DRIVER(sandbox_host_blk) = {
227 	.name		= "sandbox_host_blk",
228 	.id		= UCLASS_BLK,
229 	.ops		= &sandbox_host_blk_ops,
230 	.platdata_auto_alloc_size = sizeof(struct host_block_dev),
231 };
232 #else
233 U_BOOT_LEGACY_BLK(sandbox_host) = {
234 	.if_typename	= "host",
235 	.if_type	= IF_TYPE_HOST,
236 	.max_devs	= CONFIG_HOST_MAX_DEVICES,
237 	.get_dev	= host_get_dev_err,
238 };
239 #endif
240