Merge https://gitlab.denx.de/u-boot/custodians/u-boot-usb
[platform/kernel/u-boot.git] / drivers / block / sandbox.c
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 <dm/device_compat.h>
15 #include <linux/errno.h>
16 #include <dm/device-internal.h>
17
18 DECLARE_GLOBAL_DATA_PTR;
19
20 #ifndef CONFIG_BLK
21 static struct host_block_dev host_devices[CONFIG_HOST_MAX_DEVICES];
22
23 static struct host_block_dev *find_host_device(int dev)
24 {
25         if (dev >= 0 && dev < CONFIG_HOST_MAX_DEVICES)
26                 return &host_devices[dev];
27
28         return NULL;
29 }
30 #endif
31
32 #ifdef CONFIG_BLK
33 static unsigned long host_block_read(struct udevice *dev,
34                                      unsigned long start, lbaint_t blkcnt,
35                                      void *buffer)
36 {
37         struct host_block_dev *host_dev = dev_get_plat(dev);
38         struct blk_desc *block_dev = dev_get_uclass_plat(dev);
39
40 #else
41 static unsigned long host_block_read(struct blk_desc *block_dev,
42                                      unsigned long start, lbaint_t blkcnt,
43                                      void *buffer)
44 {
45         int dev = block_dev->devnum;
46         struct host_block_dev *host_dev = find_host_device(dev);
47
48         if (!host_dev)
49                 return -1;
50 #endif
51
52         if (os_lseek(host_dev->fd, start * block_dev->blksz, OS_SEEK_SET) ==
53                         -1) {
54                 printf("ERROR: Invalid block %lx\n", start);
55                 return -1;
56         }
57         ssize_t len = os_read(host_dev->fd, buffer, blkcnt * block_dev->blksz);
58         if (len >= 0)
59                 return len / block_dev->blksz;
60         return -1;
61 }
62
63 #ifdef CONFIG_BLK
64 static unsigned long host_block_write(struct udevice *dev,
65                                       unsigned long start, lbaint_t blkcnt,
66                                       const void *buffer)
67 {
68         struct host_block_dev *host_dev = dev_get_plat(dev);
69         struct blk_desc *block_dev = dev_get_uclass_plat(dev);
70 #else
71 static unsigned long host_block_write(struct blk_desc *block_dev,
72                                       unsigned long start, lbaint_t blkcnt,
73                                       const void *buffer)
74 {
75         int dev = block_dev->devnum;
76         struct host_block_dev *host_dev = find_host_device(dev);
77 #endif
78
79         if (os_lseek(host_dev->fd, start * block_dev->blksz, OS_SEEK_SET) ==
80                         -1) {
81                 printf("ERROR: Invalid block %lx\n", start);
82                 return -1;
83         }
84         ssize_t len = os_write(host_dev->fd, buffer, blkcnt * block_dev->blksz);
85         if (len >= 0)
86                 return len / block_dev->blksz;
87         return -1;
88 }
89
90 #ifdef CONFIG_BLK
91 int host_dev_bind(int devnum, char *filename)
92 {
93         struct host_block_dev *host_dev;
94         struct udevice *dev;
95         struct blk_desc *desc;
96         char dev_name[20], *str, *fname;
97         int ret, fd;
98
99         /* Remove and unbind the old device, if any */
100         ret = blk_get_device(IF_TYPE_HOST, devnum, &dev);
101         if (ret == 0) {
102                 ret = device_remove(dev, DM_REMOVE_NORMAL);
103                 if (ret)
104                         return ret;
105                 ret = device_unbind(dev);
106                 if (ret)
107                         return ret;
108         } else if (ret != -ENODEV) {
109                 return ret;
110         }
111
112         if (!filename)
113                 return 0;
114
115         snprintf(dev_name, sizeof(dev_name), "host%d", devnum);
116         str = strdup(dev_name);
117         if (!str)
118                 return -ENOMEM;
119         fname = strdup(filename);
120         if (!fname) {
121                 free(str);
122                 return -ENOMEM;
123         }
124
125         fd = os_open(filename, OS_O_RDWR);
126         if (fd == -1) {
127                 printf("Failed to access host backing file '%s'\n", filename);
128                 ret = -ENOENT;
129                 goto err;
130         }
131         ret = blk_create_device(gd->dm_root, "sandbox_host_blk", str,
132                                 IF_TYPE_HOST, devnum, 512,
133                                 os_lseek(fd, 0, OS_SEEK_END) / 512, &dev);
134         if (ret)
135                 goto err_file;
136
137         host_dev = dev_get_plat(dev);
138         host_dev->fd = fd;
139         host_dev->filename = fname;
140
141         ret = device_probe(dev);
142         if (ret) {
143                 device_unbind(dev);
144                 goto err_file;
145         }
146
147         desc = blk_get_devnum_by_type(IF_TYPE_HOST, devnum);
148         desc->removable = 1;
149         snprintf(desc->vendor, BLK_VEN_SIZE, "U-Boot");
150         snprintf(desc->product, BLK_PRD_SIZE, "hostfile");
151         snprintf(desc->revision, BLK_REV_SIZE, "1.0");
152
153         return 0;
154 err_file:
155         os_close(fd);
156 err:
157         free(fname);
158         free(str);
159         return ret;
160 }
161 #else
162 int host_dev_bind(int dev, char *filename)
163 {
164         struct host_block_dev *host_dev = find_host_device(dev);
165
166         if (!host_dev)
167                 return -1;
168         if (host_dev->blk_dev.priv) {
169                 os_close(host_dev->fd);
170                 host_dev->blk_dev.priv = NULL;
171         }
172         if (host_dev->filename)
173                 free(host_dev->filename);
174         if (filename && *filename) {
175                 host_dev->filename = strdup(filename);
176         } else {
177                 host_dev->filename = NULL;
178                 return 0;
179         }
180
181         host_dev->fd = os_open(host_dev->filename, OS_O_RDWR);
182         if (host_dev->fd == -1) {
183                 printf("Failed to access host backing file '%s'\n",
184                        host_dev->filename);
185                 return 1;
186         }
187
188         struct blk_desc *blk_dev = &host_dev->blk_dev;
189         blk_dev->if_type = IF_TYPE_HOST;
190         blk_dev->priv = host_dev;
191         blk_dev->blksz = 512;
192         blk_dev->lba = os_lseek(host_dev->fd, 0, OS_SEEK_END) / blk_dev->blksz;
193         blk_dev->block_read = host_block_read;
194         blk_dev->block_write = host_block_write;
195         blk_dev->devnum = dev;
196         blk_dev->part_type = PART_TYPE_UNKNOWN;
197         blk_dev->removable = 1;
198         snprintf(blk_dev->vendor, BLK_VEN_SIZE, "U-Boot");
199         snprintf(blk_dev->product, BLK_PRD_SIZE, "hostfile");
200         snprintf(blk_dev->revision, BLK_REV_SIZE, "1.0");
201         part_init(blk_dev);
202
203         return 0;
204 }
205 #endif
206
207 int host_get_dev_err(int devnum, struct blk_desc **blk_devp)
208 {
209 #ifdef CONFIG_BLK
210         struct udevice *dev;
211         int ret;
212
213         ret = blk_get_device(IF_TYPE_HOST, devnum, &dev);
214         if (ret)
215                 return ret;
216         *blk_devp = dev_get_uclass_plat(dev);
217 #else
218         struct host_block_dev *host_dev = find_host_device(devnum);
219
220         if (!host_dev)
221                 return -ENODEV;
222
223         if (!host_dev->blk_dev.priv)
224                 return -ENOENT;
225
226         *blk_devp = &host_dev->blk_dev;
227 #endif
228
229         return 0;
230 }
231
232 #ifdef CONFIG_BLK
233 static const struct blk_ops sandbox_host_blk_ops = {
234         .read   = host_block_read,
235         .write  = host_block_write,
236 };
237
238 U_BOOT_DRIVER(sandbox_host_blk) = {
239         .name           = "sandbox_host_blk",
240         .id             = UCLASS_BLK,
241         .ops            = &sandbox_host_blk_ops,
242         .plat_auto      = sizeof(struct host_block_dev),
243 };
244 #else
245 U_BOOT_LEGACY_BLK(sandbox_host) = {
246         .if_typename    = "host",
247         .if_type        = IF_TYPE_HOST,
248         .max_devs       = CONFIG_HOST_MAX_DEVICES,
249         .get_dev        = host_get_dev_err,
250 };
251 #endif