blob: 0392437309e71f5dc0077bf90c5a8e7f8507a307 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Henrik Nordströmf4d8de42013-11-10 10:26:56 -07002/*
3 * Copyright (C) 2013 Henrik Nordstrom <henrik@henriknordstrom.net>
Henrik Nordströmf4d8de42013-11-10 10:26:56 -07004 */
5
Henrik Nordströmf4d8de42013-11-10 10:26:56 -07006#include <common.h>
Simon Glass40fd0502016-02-29 15:25:57 -07007#include <blk.h>
8#include <dm.h>
9#include <fdtdec.h>
Henrik Nordströmf4d8de42013-11-10 10:26:56 -070010#include <part.h>
11#include <os.h>
12#include <malloc.h>
13#include <sandboxblockdev.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +090014#include <linux/errno.h>
Simon Glass40fd0502016-02-29 15:25:57 -070015#include <dm/device-internal.h>
Henrik Nordströmf4d8de42013-11-10 10:26:56 -070016
Simon Glass40fd0502016-02-29 15:25:57 -070017DECLARE_GLOBAL_DATA_PTR;
18
Simon Glasse1613562016-05-01 11:35:49 -060019#ifndef CONFIG_BLK
20static struct host_block_dev host_devices[CONFIG_HOST_MAX_DEVICES];
21
22static 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
Simon Glass40fd0502016-02-29 15:25:57 -070032static 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
Simon Glasse1613562016-05-01 11:35:49 -060039#else
40static 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
Simon Glass7ded9592016-02-29 15:25:56 -070051 if (os_lseek(host_dev->fd, start * block_dev->blksz, OS_SEEK_SET) ==
52 -1) {
53 printf("ERROR: Invalid block %lx\n", start);
Henrik Nordströmf4d8de42013-11-10 10:26:56 -070054 return -1;
55 }
Simon Glass7ded9592016-02-29 15:25:56 -070056 ssize_t len = os_read(host_dev->fd, buffer, blkcnt * block_dev->blksz);
Henrik Nordströmf4d8de42013-11-10 10:26:56 -070057 if (len >= 0)
Simon Glass7ded9592016-02-29 15:25:56 -070058 return len / block_dev->blksz;
Henrik Nordströmf4d8de42013-11-10 10:26:56 -070059 return -1;
60}
61
Simon Glasse1613562016-05-01 11:35:49 -060062#ifdef CONFIG_BLK
Simon Glass40fd0502016-02-29 15:25:57 -070063static 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);
Simon Glasse1613562016-05-01 11:35:49 -060069#else
70static 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
Simon Glass7ded9592016-02-29 15:25:56 -070077
78 if (os_lseek(host_dev->fd, start * block_dev->blksz, OS_SEEK_SET) ==
79 -1) {
80 printf("ERROR: Invalid block %lx\n", start);
Henrik Nordströmf4d8de42013-11-10 10:26:56 -070081 return -1;
82 }
Simon Glass7ded9592016-02-29 15:25:56 -070083 ssize_t len = os_write(host_dev->fd, buffer, blkcnt * block_dev->blksz);
Henrik Nordströmf4d8de42013-11-10 10:26:56 -070084 if (len >= 0)
Simon Glass7ded9592016-02-29 15:25:56 -070085 return len / block_dev->blksz;
Henrik Nordströmf4d8de42013-11-10 10:26:56 -070086 return -1;
87}
88
Simon Glasse1613562016-05-01 11:35:49 -060089#ifdef CONFIG_BLK
Simon Glass40fd0502016-02-29 15:25:57 -070090int 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) {
Stefan Roese706865a2017-03-20 12:51:48 +0100100 ret = device_remove(dev, DM_REMOVE_NORMAL);
Simon Glass40fd0502016-02-29 15:25:57 -0700101 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,
Jean-Jacques Hiblot5fe77022017-06-09 16:45:18 +0200131 os_lseek(fd, 0, OS_SEEK_END) / 512, &dev);
Simon Glass40fd0502016-02-29 15:25:57 -0700132 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);
145err_file:
146 os_close(fd);
147err:
148 free(fname);
149 free(str);
150 return ret;
151}
Simon Glasse1613562016-05-01 11:35:49 -0600152#else
153int 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
Henrik Nordströmf4d8de42013-11-10 10:26:56 -0700193
Simon Glass7ded9592016-02-29 15:25:56 -0700194int host_get_dev_err(int devnum, struct blk_desc **blk_devp)
Henrik Nordströmf4d8de42013-11-10 10:26:56 -0700195{
Simon Glasse1613562016-05-01 11:35:49 -0600196#ifdef CONFIG_BLK
Simon Glass40fd0502016-02-29 15:25:57 -0700197 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);
Simon Glasse1613562016-05-01 11:35:49 -0600204#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
Simon Glass40fd0502016-02-29 15:25:57 -0700215
Henrik Nordströmf4d8de42013-11-10 10:26:56 -0700216 return 0;
217}
218
Simon Glasse1613562016-05-01 11:35:49 -0600219#ifdef CONFIG_BLK
Simon Glass40fd0502016-02-29 15:25:57 -0700220static const struct blk_ops sandbox_host_blk_ops = {
221 .read = host_block_read,
222 .write = host_block_write,
223};
224
225U_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};
Simon Glass0cc65a72016-05-01 11:36:05 -0600231#else
232U_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};
Simon Glasse1613562016-05-01 11:35:49 -0600238#endif