Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Henrik Nordström | f4d8de4 | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2013 Henrik Nordstrom <henrik@henriknordstrom.net> |
Henrik Nordström | f4d8de4 | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
Henrik Nordström | f4d8de4 | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 6 | #include <common.h> |
Simon Glass | 40fd050 | 2016-02-29 15:25:57 -0700 | [diff] [blame] | 7 | #include <blk.h> |
| 8 | #include <dm.h> |
| 9 | #include <fdtdec.h> |
Henrik Nordström | f4d8de4 | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 10 | #include <part.h> |
| 11 | #include <os.h> |
| 12 | #include <malloc.h> |
| 13 | #include <sandboxblockdev.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 14 | #include <dm/device_compat.h> |
Masahiro Yamada | 1221ce4 | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 15 | #include <linux/errno.h> |
Simon Glass | 40fd050 | 2016-02-29 15:25:57 -0700 | [diff] [blame] | 16 | #include <dm/device-internal.h> |
Henrik Nordström | f4d8de4 | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 17 | |
Simon Glass | 40fd050 | 2016-02-29 15:25:57 -0700 | [diff] [blame] | 18 | DECLARE_GLOBAL_DATA_PTR; |
| 19 | |
Simon Glass | e161356 | 2016-05-01 11:35:49 -0600 | [diff] [blame] | 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 |
Simon Glass | 40fd050 | 2016-02-29 15:25:57 -0700 | [diff] [blame] | 33 | static unsigned long host_block_read(struct udevice *dev, |
| 34 | unsigned long start, lbaint_t blkcnt, |
| 35 | void *buffer) |
| 36 | { |
Bin Meng | 8f994c8 | 2018-10-15 02:21:05 -0700 | [diff] [blame] | 37 | struct host_block_dev *host_dev = dev_get_platdata(dev); |
Simon Glass | 40fd050 | 2016-02-29 15:25:57 -0700 | [diff] [blame] | 38 | struct blk_desc *block_dev = dev_get_uclass_platdata(dev); |
| 39 | |
Simon Glass | e161356 | 2016-05-01 11:35:49 -0600 | [diff] [blame] | 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 | |
Simon Glass | 7ded959 | 2016-02-29 15:25:56 -0700 | [diff] [blame] | 52 | 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öm | f4d8de4 | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 55 | return -1; |
| 56 | } |
Simon Glass | 7ded959 | 2016-02-29 15:25:56 -0700 | [diff] [blame] | 57 | ssize_t len = os_read(host_dev->fd, buffer, blkcnt * block_dev->blksz); |
Henrik Nordström | f4d8de4 | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 58 | if (len >= 0) |
Simon Glass | 7ded959 | 2016-02-29 15:25:56 -0700 | [diff] [blame] | 59 | return len / block_dev->blksz; |
Henrik Nordström | f4d8de4 | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 60 | return -1; |
| 61 | } |
| 62 | |
Simon Glass | e161356 | 2016-05-01 11:35:49 -0600 | [diff] [blame] | 63 | #ifdef CONFIG_BLK |
Simon Glass | 40fd050 | 2016-02-29 15:25:57 -0700 | [diff] [blame] | 64 | static unsigned long host_block_write(struct udevice *dev, |
| 65 | unsigned long start, lbaint_t blkcnt, |
| 66 | const void *buffer) |
| 67 | { |
Bin Meng | 8f994c8 | 2018-10-15 02:21:05 -0700 | [diff] [blame] | 68 | struct host_block_dev *host_dev = dev_get_platdata(dev); |
Simon Glass | 40fd050 | 2016-02-29 15:25:57 -0700 | [diff] [blame] | 69 | struct blk_desc *block_dev = dev_get_uclass_platdata(dev); |
Simon Glass | e161356 | 2016-05-01 11:35:49 -0600 | [diff] [blame] | 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 |
Simon Glass | 7ded959 | 2016-02-29 15:25:56 -0700 | [diff] [blame] | 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); |
Henrik Nordström | f4d8de4 | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 82 | return -1; |
| 83 | } |
Simon Glass | 7ded959 | 2016-02-29 15:25:56 -0700 | [diff] [blame] | 84 | ssize_t len = os_write(host_dev->fd, buffer, blkcnt * block_dev->blksz); |
Henrik Nordström | f4d8de4 | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 85 | if (len >= 0) |
Simon Glass | 7ded959 | 2016-02-29 15:25:56 -0700 | [diff] [blame] | 86 | return len / block_dev->blksz; |
Henrik Nordström | f4d8de4 | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 87 | return -1; |
| 88 | } |
| 89 | |
Simon Glass | e161356 | 2016-05-01 11:35:49 -0600 | [diff] [blame] | 90 | #ifdef CONFIG_BLK |
Simon Glass | 40fd050 | 2016-02-29 15:25:57 -0700 | [diff] [blame] | 91 | int 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 Roese | 706865a | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 101 | ret = device_remove(dev, DM_REMOVE_NORMAL); |
Simon Glass | 40fd050 | 2016-02-29 15:25:57 -0700 | [diff] [blame] | 102 | 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, |
Jean-Jacques Hiblot | 5fe7702 | 2017-06-09 16:45:18 +0200 | [diff] [blame] | 132 | os_lseek(fd, 0, OS_SEEK_END) / 512, &dev); |
Simon Glass | 40fd050 | 2016-02-29 15:25:57 -0700 | [diff] [blame] | 133 | if (ret) |
| 134 | goto err_file; |
Bin Meng | 8f994c8 | 2018-10-15 02:21:05 -0700 | [diff] [blame] | 135 | |
| 136 | host_dev = dev_get_platdata(dev); |
| 137 | host_dev->fd = fd; |
| 138 | host_dev->filename = fname; |
| 139 | |
Simon Glass | 40fd050 | 2016-02-29 15:25:57 -0700 | [diff] [blame] | 140 | ret = device_probe(dev); |
| 141 | if (ret) { |
| 142 | device_unbind(dev); |
| 143 | goto err_file; |
| 144 | } |
| 145 | |
Bin Meng | d0851c8 | 2018-10-15 02:21:07 -0700 | [diff] [blame] | 146 | return 0; |
Simon Glass | 40fd050 | 2016-02-29 15:25:57 -0700 | [diff] [blame] | 147 | err_file: |
| 148 | os_close(fd); |
| 149 | err: |
| 150 | free(fname); |
| 151 | free(str); |
| 152 | return ret; |
| 153 | } |
Simon Glass | e161356 | 2016-05-01 11:35:49 -0600 | [diff] [blame] | 154 | #else |
| 155 | int host_dev_bind(int dev, char *filename) |
| 156 | { |
| 157 | struct host_block_dev *host_dev = find_host_device(dev); |
| 158 | |
| 159 | if (!host_dev) |
| 160 | return -1; |
| 161 | if (host_dev->blk_dev.priv) { |
| 162 | os_close(host_dev->fd); |
| 163 | host_dev->blk_dev.priv = NULL; |
| 164 | } |
| 165 | if (host_dev->filename) |
| 166 | free(host_dev->filename); |
| 167 | if (filename && *filename) { |
| 168 | host_dev->filename = strdup(filename); |
| 169 | } else { |
| 170 | host_dev->filename = NULL; |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | host_dev->fd = os_open(host_dev->filename, OS_O_RDWR); |
| 175 | if (host_dev->fd == -1) { |
| 176 | printf("Failed to access host backing file '%s'\n", |
| 177 | host_dev->filename); |
| 178 | return 1; |
| 179 | } |
| 180 | |
| 181 | struct blk_desc *blk_dev = &host_dev->blk_dev; |
| 182 | blk_dev->if_type = IF_TYPE_HOST; |
| 183 | blk_dev->priv = host_dev; |
| 184 | blk_dev->blksz = 512; |
| 185 | blk_dev->lba = os_lseek(host_dev->fd, 0, OS_SEEK_END) / blk_dev->blksz; |
| 186 | blk_dev->block_read = host_block_read; |
| 187 | blk_dev->block_write = host_block_write; |
| 188 | blk_dev->devnum = dev; |
| 189 | blk_dev->part_type = PART_TYPE_UNKNOWN; |
| 190 | part_init(blk_dev); |
| 191 | |
| 192 | return 0; |
| 193 | } |
| 194 | #endif |
Henrik Nordström | f4d8de4 | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 195 | |
Simon Glass | 7ded959 | 2016-02-29 15:25:56 -0700 | [diff] [blame] | 196 | int host_get_dev_err(int devnum, struct blk_desc **blk_devp) |
Henrik Nordström | f4d8de4 | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 197 | { |
Simon Glass | e161356 | 2016-05-01 11:35:49 -0600 | [diff] [blame] | 198 | #ifdef CONFIG_BLK |
Simon Glass | 40fd050 | 2016-02-29 15:25:57 -0700 | [diff] [blame] | 199 | struct udevice *dev; |
| 200 | int ret; |
| 201 | |
| 202 | ret = blk_get_device(IF_TYPE_HOST, devnum, &dev); |
| 203 | if (ret) |
| 204 | return ret; |
| 205 | *blk_devp = dev_get_uclass_platdata(dev); |
Simon Glass | e161356 | 2016-05-01 11:35:49 -0600 | [diff] [blame] | 206 | #else |
| 207 | struct host_block_dev *host_dev = find_host_device(devnum); |
| 208 | |
| 209 | if (!host_dev) |
| 210 | return -ENODEV; |
| 211 | |
| 212 | if (!host_dev->blk_dev.priv) |
| 213 | return -ENOENT; |
| 214 | |
| 215 | *blk_devp = &host_dev->blk_dev; |
| 216 | #endif |
Simon Glass | 40fd050 | 2016-02-29 15:25:57 -0700 | [diff] [blame] | 217 | |
Henrik Nordström | f4d8de4 | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 218 | return 0; |
| 219 | } |
| 220 | |
Simon Glass | e161356 | 2016-05-01 11:35:49 -0600 | [diff] [blame] | 221 | #ifdef CONFIG_BLK |
Simon Glass | 40fd050 | 2016-02-29 15:25:57 -0700 | [diff] [blame] | 222 | static const struct blk_ops sandbox_host_blk_ops = { |
| 223 | .read = host_block_read, |
| 224 | .write = host_block_write, |
| 225 | }; |
| 226 | |
| 227 | U_BOOT_DRIVER(sandbox_host_blk) = { |
| 228 | .name = "sandbox_host_blk", |
| 229 | .id = UCLASS_BLK, |
| 230 | .ops = &sandbox_host_blk_ops, |
Bin Meng | 8f994c8 | 2018-10-15 02:21:05 -0700 | [diff] [blame] | 231 | .platdata_auto_alloc_size = sizeof(struct host_block_dev), |
Simon Glass | 40fd050 | 2016-02-29 15:25:57 -0700 | [diff] [blame] | 232 | }; |
Simon Glass | 0cc65a7 | 2016-05-01 11:36:05 -0600 | [diff] [blame] | 233 | #else |
| 234 | U_BOOT_LEGACY_BLK(sandbox_host) = { |
| 235 | .if_typename = "host", |
| 236 | .if_type = IF_TYPE_HOST, |
| 237 | .max_devs = CONFIG_HOST_MAX_DEVICES, |
| 238 | .get_dev = host_get_dev_err, |
| 239 | }; |
Simon Glass | e161356 | 2016-05-01 11:35:49 -0600 | [diff] [blame] | 240 | #endif |