blob: 1c2c3b4f886edc214cb2a5283a027321d02b48b6 [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>
Simon Glass401d1c42020-10-30 21:38:53 -060014#include <asm/global_data.h>
Simon Glass336d4612020-02-03 07:36:16 -070015#include <dm/device_compat.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +090016#include <linux/errno.h>
Simon Glass40fd0502016-02-29 15:25:57 -070017#include <dm/device-internal.h>
Henrik Nordströmf4d8de42013-11-10 10:26:56 -070018
Simon Glass40fd0502016-02-29 15:25:57 -070019DECLARE_GLOBAL_DATA_PTR;
20
Simon Glasse1613562016-05-01 11:35:49 -060021#ifndef CONFIG_BLK
22static struct host_block_dev host_devices[CONFIG_HOST_MAX_DEVICES];
23
24static struct host_block_dev *find_host_device(int dev)
25{
26 if (dev >= 0 && dev < CONFIG_HOST_MAX_DEVICES)
27 return &host_devices[dev];
28
29 return NULL;
30}
31#endif
32
33#ifdef CONFIG_BLK
Simon Glass40fd0502016-02-29 15:25:57 -070034static unsigned long host_block_read(struct udevice *dev,
35 unsigned long start, lbaint_t blkcnt,
36 void *buffer)
37{
Simon Glassc69cda22020-12-03 16:55:20 -070038 struct host_block_dev *host_dev = dev_get_plat(dev);
Simon Glasscaa4daa2020-12-03 16:55:18 -070039 struct blk_desc *block_dev = dev_get_uclass_plat(dev);
Simon Glass40fd0502016-02-29 15:25:57 -070040
Simon Glasse1613562016-05-01 11:35:49 -060041#else
42static unsigned long host_block_read(struct blk_desc *block_dev,
43 unsigned long start, lbaint_t blkcnt,
44 void *buffer)
45{
46 int dev = block_dev->devnum;
47 struct host_block_dev *host_dev = find_host_device(dev);
48
49 if (!host_dev)
50 return -1;
51#endif
52
Simon Glass7ded9592016-02-29 15:25:56 -070053 if (os_lseek(host_dev->fd, start * block_dev->blksz, OS_SEEK_SET) ==
54 -1) {
55 printf("ERROR: Invalid block %lx\n", start);
Henrik Nordströmf4d8de42013-11-10 10:26:56 -070056 return -1;
57 }
Simon Glass7ded9592016-02-29 15:25:56 -070058 ssize_t len = os_read(host_dev->fd, buffer, blkcnt * block_dev->blksz);
Henrik Nordströmf4d8de42013-11-10 10:26:56 -070059 if (len >= 0)
Simon Glass7ded9592016-02-29 15:25:56 -070060 return len / block_dev->blksz;
Henrik Nordströmf4d8de42013-11-10 10:26:56 -070061 return -1;
62}
63
Simon Glasse1613562016-05-01 11:35:49 -060064#ifdef CONFIG_BLK
Simon Glass40fd0502016-02-29 15:25:57 -070065static unsigned long host_block_write(struct udevice *dev,
66 unsigned long start, lbaint_t blkcnt,
67 const void *buffer)
68{
Simon Glassc69cda22020-12-03 16:55:20 -070069 struct host_block_dev *host_dev = dev_get_plat(dev);
Simon Glasscaa4daa2020-12-03 16:55:18 -070070 struct blk_desc *block_dev = dev_get_uclass_plat(dev);
Simon Glasse1613562016-05-01 11:35:49 -060071#else
72static unsigned long host_block_write(struct blk_desc *block_dev,
73 unsigned long start, lbaint_t blkcnt,
74 const void *buffer)
75{
76 int dev = block_dev->devnum;
77 struct host_block_dev *host_dev = find_host_device(dev);
78#endif
Simon Glass7ded9592016-02-29 15:25:56 -070079
80 if (os_lseek(host_dev->fd, start * block_dev->blksz, OS_SEEK_SET) ==
81 -1) {
82 printf("ERROR: Invalid block %lx\n", start);
Henrik Nordströmf4d8de42013-11-10 10:26:56 -070083 return -1;
84 }
Simon Glass7ded9592016-02-29 15:25:56 -070085 ssize_t len = os_write(host_dev->fd, buffer, blkcnt * block_dev->blksz);
Henrik Nordströmf4d8de42013-11-10 10:26:56 -070086 if (len >= 0)
Simon Glass7ded9592016-02-29 15:25:56 -070087 return len / block_dev->blksz;
Henrik Nordströmf4d8de42013-11-10 10:26:56 -070088 return -1;
89}
90
Simon Glasse1613562016-05-01 11:35:49 -060091#ifdef CONFIG_BLK
Simon Glass17585512021-03-15 18:11:11 +130092int host_dev_bind(int devnum, char *filename, bool removable)
Simon Glass40fd0502016-02-29 15:25:57 -070093{
94 struct host_block_dev *host_dev;
95 struct udevice *dev;
Heinrich Schuchardt168e3132021-01-25 12:57:14 +010096 struct blk_desc *desc;
Simon Glass40fd0502016-02-29 15:25:57 -070097 char dev_name[20], *str, *fname;
98 int ret, fd;
99
100 /* Remove and unbind the old device, if any */
101 ret = blk_get_device(IF_TYPE_HOST, devnum, &dev);
102 if (ret == 0) {
Stefan Roese706865a2017-03-20 12:51:48 +0100103 ret = device_remove(dev, DM_REMOVE_NORMAL);
Simon Glass40fd0502016-02-29 15:25:57 -0700104 if (ret)
105 return ret;
106 ret = device_unbind(dev);
107 if (ret)
108 return ret;
109 } else if (ret != -ENODEV) {
110 return ret;
111 }
112
113 if (!filename)
114 return 0;
115
116 snprintf(dev_name, sizeof(dev_name), "host%d", devnum);
117 str = strdup(dev_name);
118 if (!str)
119 return -ENOMEM;
120 fname = strdup(filename);
121 if (!fname) {
122 free(str);
123 return -ENOMEM;
124 }
125
126 fd = os_open(filename, OS_O_RDWR);
127 if (fd == -1) {
128 printf("Failed to access host backing file '%s'\n", filename);
129 ret = -ENOENT;
130 goto err;
131 }
132 ret = blk_create_device(gd->dm_root, "sandbox_host_blk", str,
133 IF_TYPE_HOST, devnum, 512,
Jean-Jacques Hiblot5fe77022017-06-09 16:45:18 +0200134 os_lseek(fd, 0, OS_SEEK_END) / 512, &dev);
Simon Glass40fd0502016-02-29 15:25:57 -0700135 if (ret)
136 goto err_file;
Bin Meng8f994c82018-10-15 02:21:05 -0700137
Simon Glassc69cda22020-12-03 16:55:20 -0700138 host_dev = dev_get_plat(dev);
Bin Meng8f994c82018-10-15 02:21:05 -0700139 host_dev->fd = fd;
140 host_dev->filename = fname;
141
Simon Glass40fd0502016-02-29 15:25:57 -0700142 ret = device_probe(dev);
143 if (ret) {
144 device_unbind(dev);
145 goto err_file;
146 }
147
Heinrich Schuchardt168e3132021-01-25 12:57:14 +0100148 desc = blk_get_devnum_by_type(IF_TYPE_HOST, devnum);
Simon Glass17585512021-03-15 18:11:11 +1300149 desc->removable = removable;
Heinrich Schuchardt168e3132021-01-25 12:57:14 +0100150 snprintf(desc->vendor, BLK_VEN_SIZE, "U-Boot");
151 snprintf(desc->product, BLK_PRD_SIZE, "hostfile");
152 snprintf(desc->revision, BLK_REV_SIZE, "1.0");
153
Bin Mengd0851c82018-10-15 02:21:07 -0700154 return 0;
Simon Glass40fd0502016-02-29 15:25:57 -0700155err_file:
156 os_close(fd);
157err:
158 free(fname);
159 free(str);
160 return ret;
161}
Simon Glasse1613562016-05-01 11:35:49 -0600162#else
Simon Glass17585512021-03-15 18:11:11 +1300163int host_dev_bind(int dev, char *filename, bool removable)
Simon Glasse1613562016-05-01 11:35:49 -0600164{
165 struct host_block_dev *host_dev = find_host_device(dev);
166
167 if (!host_dev)
168 return -1;
169 if (host_dev->blk_dev.priv) {
170 os_close(host_dev->fd);
171 host_dev->blk_dev.priv = NULL;
172 }
173 if (host_dev->filename)
174 free(host_dev->filename);
175 if (filename && *filename) {
176 host_dev->filename = strdup(filename);
177 } else {
178 host_dev->filename = NULL;
179 return 0;
180 }
181
182 host_dev->fd = os_open(host_dev->filename, OS_O_RDWR);
183 if (host_dev->fd == -1) {
184 printf("Failed to access host backing file '%s'\n",
185 host_dev->filename);
186 return 1;
187 }
188
189 struct blk_desc *blk_dev = &host_dev->blk_dev;
190 blk_dev->if_type = IF_TYPE_HOST;
191 blk_dev->priv = host_dev;
192 blk_dev->blksz = 512;
193 blk_dev->lba = os_lseek(host_dev->fd, 0, OS_SEEK_END) / blk_dev->blksz;
194 blk_dev->block_read = host_block_read;
195 blk_dev->block_write = host_block_write;
196 blk_dev->devnum = dev;
197 blk_dev->part_type = PART_TYPE_UNKNOWN;
Simon Glass17585512021-03-15 18:11:11 +1300198 blk_dev->removable = removable;
Heinrich Schuchardt168e3132021-01-25 12:57:14 +0100199 snprintf(blk_dev->vendor, BLK_VEN_SIZE, "U-Boot");
200 snprintf(blk_dev->product, BLK_PRD_SIZE, "hostfile");
201 snprintf(blk_dev->revision, BLK_REV_SIZE, "1.0");
Simon Glasse1613562016-05-01 11:35:49 -0600202 part_init(blk_dev);
203
204 return 0;
205}
206#endif
Henrik Nordströmf4d8de42013-11-10 10:26:56 -0700207
Simon Glass7ded9592016-02-29 15:25:56 -0700208int host_get_dev_err(int devnum, struct blk_desc **blk_devp)
Henrik Nordströmf4d8de42013-11-10 10:26:56 -0700209{
Simon Glasse1613562016-05-01 11:35:49 -0600210#ifdef CONFIG_BLK
Simon Glass40fd0502016-02-29 15:25:57 -0700211 struct udevice *dev;
212 int ret;
213
214 ret = blk_get_device(IF_TYPE_HOST, devnum, &dev);
215 if (ret)
216 return ret;
Simon Glasscaa4daa2020-12-03 16:55:18 -0700217 *blk_devp = dev_get_uclass_plat(dev);
Simon Glasse1613562016-05-01 11:35:49 -0600218#else
219 struct host_block_dev *host_dev = find_host_device(devnum);
220
221 if (!host_dev)
222 return -ENODEV;
223
224 if (!host_dev->blk_dev.priv)
225 return -ENOENT;
226
227 *blk_devp = &host_dev->blk_dev;
228#endif
Simon Glass40fd0502016-02-29 15:25:57 -0700229
Henrik Nordströmf4d8de42013-11-10 10:26:56 -0700230 return 0;
231}
232
Simon Glasse1613562016-05-01 11:35:49 -0600233#ifdef CONFIG_BLK
Heinrich Schuchardt297b8b32021-02-03 00:21:56 +0100234
235int sandbox_host_unbind(struct udevice *dev)
236{
237 struct host_block_dev *host_dev;
238
239 /* Data validity is checked in host_dev_bind() */
240 host_dev = dev_get_plat(dev);
241 os_close(host_dev->fd);
242
243 return 0;
244}
245
Simon Glass40fd0502016-02-29 15:25:57 -0700246static const struct blk_ops sandbox_host_blk_ops = {
247 .read = host_block_read,
248 .write = host_block_write,
249};
250
251U_BOOT_DRIVER(sandbox_host_blk) = {
252 .name = "sandbox_host_blk",
253 .id = UCLASS_BLK,
254 .ops = &sandbox_host_blk_ops,
Heinrich Schuchardt297b8b32021-02-03 00:21:56 +0100255 .unbind = sandbox_host_unbind,
Simon Glasscaa4daa2020-12-03 16:55:18 -0700256 .plat_auto = sizeof(struct host_block_dev),
Simon Glass40fd0502016-02-29 15:25:57 -0700257};
Simon Glass0cc65a72016-05-01 11:36:05 -0600258#else
259U_BOOT_LEGACY_BLK(sandbox_host) = {
260 .if_typename = "host",
261 .if_type = IF_TYPE_HOST,
262 .max_devs = CONFIG_HOST_MAX_DEVICES,
263 .get_dev = host_get_dev_err,
264};
Simon Glasse1613562016-05-01 11:35:49 -0600265#endif