blob: d3b1aaaba36d77921ad306f39e9606821ed8b92e [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{
Bin Meng8f994c82018-10-15 02:21:05 -070036 struct host_block_dev *host_dev = dev_get_platdata(dev);
Simon Glass40fd0502016-02-29 15:25:57 -070037 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{
Bin Meng8f994c82018-10-15 02:21:05 -070067 struct host_block_dev *host_dev = dev_get_platdata(dev);
Simon Glass40fd0502016-02-29 15:25:57 -070068 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;
Bin Meng8f994c82018-10-15 02:21:05 -0700134
135 host_dev = dev_get_platdata(dev);
136 host_dev->fd = fd;
137 host_dev->filename = fname;
138
Simon Glass40fd0502016-02-29 15:25:57 -0700139 ret = device_probe(dev);
140 if (ret) {
141 device_unbind(dev);
142 goto err_file;
143 }
144
Bin Mengd0851c82018-10-15 02:21:07 -0700145 return 0;
Simon Glass40fd0502016-02-29 15:25:57 -0700146err_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,
Bin Meng8f994c82018-10-15 02:21:05 -0700230 .platdata_auto_alloc_size = sizeof(struct host_block_dev),
Simon Glass40fd0502016-02-29 15:25:57 -0700231};
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