blob: 34d1c638bc7788755f10b76154e513ae1ec3c806 [file] [log] [blame]
Henrik Nordströmf4d8de42013-11-10 10:26:56 -07001/*
2 * Copyright (C) 2013 Henrik Nordstrom <henrik@henriknordstrom.net>
3 *
4 * SPDX-License-Identifier: GPL-2.0+
5 */
6
Henrik Nordströmf4d8de42013-11-10 10:26:56 -07007#include <common.h>
Simon Glass40fd0502016-02-29 15:25:57 -07008#include <blk.h>
9#include <dm.h>
10#include <fdtdec.h>
Henrik Nordströmf4d8de42013-11-10 10:26:56 -070011#include <part.h>
12#include <os.h>
13#include <malloc.h>
14#include <sandboxblockdev.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +090015#include <linux/errno.h>
Simon Glass40fd0502016-02-29 15:25:57 -070016#include <dm/device-internal.h>
Henrik Nordströmf4d8de42013-11-10 10:26:56 -070017
Simon Glass40fd0502016-02-29 15:25:57 -070018DECLARE_GLOBAL_DATA_PTR;
19
Simon Glasse1613562016-05-01 11:35:49 -060020#ifndef CONFIG_BLK
21static struct host_block_dev host_devices[CONFIG_HOST_MAX_DEVICES];
22
23static 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
Simon Glass40fd0502016-02-29 15:25:57 -070033static 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_priv(dev);
38 struct blk_desc *block_dev = dev_get_uclass_platdata(dev);
39
Simon Glasse1613562016-05-01 11:35:49 -060040#else
41static 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
Simon Glass7ded9592016-02-29 15:25:56 -070052 if (os_lseek(host_dev->fd, start * block_dev->blksz, OS_SEEK_SET) ==
53 -1) {
54 printf("ERROR: Invalid block %lx\n", start);
Henrik Nordströmf4d8de42013-11-10 10:26:56 -070055 return -1;
56 }
Simon Glass7ded9592016-02-29 15:25:56 -070057 ssize_t len = os_read(host_dev->fd, buffer, blkcnt * block_dev->blksz);
Henrik Nordströmf4d8de42013-11-10 10:26:56 -070058 if (len >= 0)
Simon Glass7ded9592016-02-29 15:25:56 -070059 return len / block_dev->blksz;
Henrik Nordströmf4d8de42013-11-10 10:26:56 -070060 return -1;
61}
62
Simon Glasse1613562016-05-01 11:35:49 -060063#ifdef CONFIG_BLK
Simon Glass40fd0502016-02-29 15:25:57 -070064static 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_priv(dev);
69 struct blk_desc *block_dev = dev_get_uclass_platdata(dev);
Simon Glasse1613562016-05-01 11:35:49 -060070#else
71static 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
Simon Glass7ded9592016-02-29 15:25:56 -070078
79 if (os_lseek(host_dev->fd, start * block_dev->blksz, OS_SEEK_SET) ==
80 -1) {
81 printf("ERROR: Invalid block %lx\n", start);
Henrik Nordströmf4d8de42013-11-10 10:26:56 -070082 return -1;
83 }
Simon Glass7ded9592016-02-29 15:25:56 -070084 ssize_t len = os_write(host_dev->fd, buffer, blkcnt * block_dev->blksz);
Henrik Nordströmf4d8de42013-11-10 10:26:56 -070085 if (len >= 0)
Simon Glass7ded9592016-02-29 15:25:56 -070086 return len / block_dev->blksz;
Henrik Nordströmf4d8de42013-11-10 10:26:56 -070087 return -1;
88}
89
Simon Glasse1613562016-05-01 11:35:49 -060090#ifdef CONFIG_BLK
Simon Glass40fd0502016-02-29 15:25:57 -070091int host_dev_bind(int devnum, char *filename)
92{
93 struct host_block_dev *host_dev;
94 struct udevice *dev;
95 char dev_name[20], *str, *fname;
96 int ret, fd;
97
98 /* Remove and unbind the old device, if any */
99 ret = blk_get_device(IF_TYPE_HOST, devnum, &dev);
100 if (ret == 0) {
Stefan Roese706865a2017-03-20 12:51:48 +0100101 ret = device_remove(dev, DM_REMOVE_NORMAL);
Simon Glass40fd0502016-02-29 15:25:57 -0700102 if (ret)
103 return ret;
104 ret = device_unbind(dev);
105 if (ret)
106 return ret;
107 } else if (ret != -ENODEV) {
108 return ret;
109 }
110
111 if (!filename)
112 return 0;
113
114 snprintf(dev_name, sizeof(dev_name), "host%d", devnum);
115 str = strdup(dev_name);
116 if (!str)
117 return -ENOMEM;
118 fname = strdup(filename);
119 if (!fname) {
120 free(str);
121 return -ENOMEM;
122 }
123
124 fd = os_open(filename, OS_O_RDWR);
125 if (fd == -1) {
126 printf("Failed to access host backing file '%s'\n", filename);
127 ret = -ENOENT;
128 goto err;
129 }
130 ret = blk_create_device(gd->dm_root, "sandbox_host_blk", str,
131 IF_TYPE_HOST, devnum, 512,
132 os_lseek(fd, 0, OS_SEEK_END), &dev);
133 if (ret)
134 goto err_file;
135 ret = device_probe(dev);
136 if (ret) {
137 device_unbind(dev);
138 goto err_file;
139 }
140
141 host_dev = dev_get_priv(dev);
142 host_dev->fd = fd;
143 host_dev->filename = fname;
144
145 return blk_prepare_device(dev);
146err_file:
147 os_close(fd);
148err:
149 free(fname);
150 free(str);
151 return ret;
152}
Simon Glasse1613562016-05-01 11:35:49 -0600153#else
154int 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
Henrik Nordströmf4d8de42013-11-10 10:26:56 -0700194
Simon Glass7ded9592016-02-29 15:25:56 -0700195int host_get_dev_err(int devnum, struct blk_desc **blk_devp)
Henrik Nordströmf4d8de42013-11-10 10:26:56 -0700196{
Simon Glasse1613562016-05-01 11:35:49 -0600197#ifdef CONFIG_BLK
Simon Glass40fd0502016-02-29 15:25:57 -0700198 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);
Simon Glasse1613562016-05-01 11:35:49 -0600205#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
Simon Glass40fd0502016-02-29 15:25:57 -0700216
Henrik Nordströmf4d8de42013-11-10 10:26:56 -0700217 return 0;
218}
219
Simon Glasse1613562016-05-01 11:35:49 -0600220#ifdef CONFIG_BLK
Simon Glass40fd0502016-02-29 15:25:57 -0700221static const struct blk_ops sandbox_host_blk_ops = {
222 .read = host_block_read,
223 .write = host_block_write,
224};
225
226U_BOOT_DRIVER(sandbox_host_blk) = {
227 .name = "sandbox_host_blk",
228 .id = UCLASS_BLK,
229 .ops = &sandbox_host_blk_ops,
230 .priv_auto_alloc_size = sizeof(struct host_block_dev),
231};
Simon Glass0cc65a72016-05-01 11:36:05 -0600232#else
233U_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};
Simon Glasse1613562016-05-01 11:35:49 -0600239#endif