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> |
Masahiro Yamada | 1221ce4 | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 14 | #include <linux/errno.h> |
Simon Glass | 40fd050 | 2016-02-29 15:25:57 -0700 | [diff] [blame] | 15 | #include <dm/device-internal.h> |
Henrik Nordström | f4d8de4 | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 16 | |
Simon Glass | 40fd050 | 2016-02-29 15:25:57 -0700 | [diff] [blame] | 17 | DECLARE_GLOBAL_DATA_PTR; |
| 18 | |
Simon Glass | e161356 | 2016-05-01 11:35:49 -0600 | [diff] [blame] | 19 | #ifndef CONFIG_BLK |
| 20 | static struct host_block_dev host_devices[CONFIG_HOST_MAX_DEVICES]; |
| 21 | |
| 22 | static 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 Glass | 40fd050 | 2016-02-29 15:25:57 -0700 | [diff] [blame] | 32 | static unsigned long host_block_read(struct udevice *dev, |
| 33 | unsigned long start, lbaint_t blkcnt, |
| 34 | void *buffer) |
| 35 | { |
Bin Meng | 8f994c8 | 2018-10-15 02:21:05 -0700 | [diff] [blame] | 36 | struct host_block_dev *host_dev = dev_get_platdata(dev); |
Simon Glass | 40fd050 | 2016-02-29 15:25:57 -0700 | [diff] [blame] | 37 | struct blk_desc *block_dev = dev_get_uclass_platdata(dev); |
| 38 | |
Simon Glass | e161356 | 2016-05-01 11:35:49 -0600 | [diff] [blame] | 39 | #else |
| 40 | static 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 Glass | 7ded959 | 2016-02-29 15:25:56 -0700 | [diff] [blame] | 51 | 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öm | f4d8de4 | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 54 | return -1; |
| 55 | } |
Simon Glass | 7ded959 | 2016-02-29 15:25:56 -0700 | [diff] [blame] | 56 | 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] | 57 | if (len >= 0) |
Simon Glass | 7ded959 | 2016-02-29 15:25:56 -0700 | [diff] [blame] | 58 | return len / block_dev->blksz; |
Henrik Nordström | f4d8de4 | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 59 | return -1; |
| 60 | } |
| 61 | |
Simon Glass | e161356 | 2016-05-01 11:35:49 -0600 | [diff] [blame] | 62 | #ifdef CONFIG_BLK |
Simon Glass | 40fd050 | 2016-02-29 15:25:57 -0700 | [diff] [blame] | 63 | static unsigned long host_block_write(struct udevice *dev, |
| 64 | unsigned long start, lbaint_t blkcnt, |
| 65 | const void *buffer) |
| 66 | { |
Bin Meng | 8f994c8 | 2018-10-15 02:21:05 -0700 | [diff] [blame] | 67 | struct host_block_dev *host_dev = dev_get_platdata(dev); |
Simon Glass | 40fd050 | 2016-02-29 15:25:57 -0700 | [diff] [blame] | 68 | struct blk_desc *block_dev = dev_get_uclass_platdata(dev); |
Simon Glass | e161356 | 2016-05-01 11:35:49 -0600 | [diff] [blame] | 69 | #else |
| 70 | static 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 Glass | 7ded959 | 2016-02-29 15:25:56 -0700 | [diff] [blame] | 77 | |
| 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öm | f4d8de4 | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 81 | return -1; |
| 82 | } |
Simon Glass | 7ded959 | 2016-02-29 15:25:56 -0700 | [diff] [blame] | 83 | 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] | 84 | if (len >= 0) |
Simon Glass | 7ded959 | 2016-02-29 15:25:56 -0700 | [diff] [blame] | 85 | return len / block_dev->blksz; |
Henrik Nordström | f4d8de4 | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 86 | return -1; |
| 87 | } |
| 88 | |
Simon Glass | e161356 | 2016-05-01 11:35:49 -0600 | [diff] [blame] | 89 | #ifdef CONFIG_BLK |
Simon Glass | 40fd050 | 2016-02-29 15:25:57 -0700 | [diff] [blame] | 90 | int 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 Roese | 706865a | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 100 | ret = device_remove(dev, DM_REMOVE_NORMAL); |
Simon Glass | 40fd050 | 2016-02-29 15:25:57 -0700 | [diff] [blame] | 101 | 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 Hiblot | 5fe7702 | 2017-06-09 16:45:18 +0200 | [diff] [blame] | 131 | os_lseek(fd, 0, OS_SEEK_END) / 512, &dev); |
Simon Glass | 40fd050 | 2016-02-29 15:25:57 -0700 | [diff] [blame] | 132 | if (ret) |
| 133 | goto err_file; |
Bin Meng | 8f994c8 | 2018-10-15 02:21:05 -0700 | [diff] [blame] | 134 | |
| 135 | host_dev = dev_get_platdata(dev); |
| 136 | host_dev->fd = fd; |
| 137 | host_dev->filename = fname; |
| 138 | |
Simon Glass | 40fd050 | 2016-02-29 15:25:57 -0700 | [diff] [blame] | 139 | ret = device_probe(dev); |
| 140 | if (ret) { |
| 141 | device_unbind(dev); |
| 142 | goto err_file; |
| 143 | } |
| 144 | |
Simon Glass | 40fd050 | 2016-02-29 15:25:57 -0700 | [diff] [blame] | 145 | return blk_prepare_device(dev); |
| 146 | err_file: |
| 147 | os_close(fd); |
| 148 | err: |
| 149 | free(fname); |
| 150 | free(str); |
| 151 | return ret; |
| 152 | } |
Simon Glass | e161356 | 2016-05-01 11:35:49 -0600 | [diff] [blame] | 153 | #else |
| 154 | int 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öm | f4d8de4 | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 194 | |
Simon Glass | 7ded959 | 2016-02-29 15:25:56 -0700 | [diff] [blame] | 195 | 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] | 196 | { |
Simon Glass | e161356 | 2016-05-01 11:35:49 -0600 | [diff] [blame] | 197 | #ifdef CONFIG_BLK |
Simon Glass | 40fd050 | 2016-02-29 15:25:57 -0700 | [diff] [blame] | 198 | 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 Glass | e161356 | 2016-05-01 11:35:49 -0600 | [diff] [blame] | 205 | #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 Glass | 40fd050 | 2016-02-29 15:25:57 -0700 | [diff] [blame] | 216 | |
Henrik Nordström | f4d8de4 | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 217 | return 0; |
| 218 | } |
| 219 | |
Simon Glass | e161356 | 2016-05-01 11:35:49 -0600 | [diff] [blame] | 220 | #ifdef CONFIG_BLK |
Simon Glass | 40fd050 | 2016-02-29 15:25:57 -0700 | [diff] [blame] | 221 | static const struct blk_ops sandbox_host_blk_ops = { |
| 222 | .read = host_block_read, |
| 223 | .write = host_block_write, |
| 224 | }; |
| 225 | |
| 226 | U_BOOT_DRIVER(sandbox_host_blk) = { |
| 227 | .name = "sandbox_host_blk", |
| 228 | .id = UCLASS_BLK, |
| 229 | .ops = &sandbox_host_blk_ops, |
Bin Meng | 8f994c8 | 2018-10-15 02:21:05 -0700 | [diff] [blame] | 230 | .platdata_auto_alloc_size = sizeof(struct host_block_dev), |
Simon Glass | 40fd050 | 2016-02-29 15:25:57 -0700 | [diff] [blame] | 231 | }; |
Simon Glass | 0cc65a7 | 2016-05-01 11:36:05 -0600 | [diff] [blame] | 232 | #else |
| 233 | U_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 Glass | e161356 | 2016-05-01 11:35:49 -0600 | [diff] [blame] | 239 | #endif |