Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Henrik Nordström | 26f9a6c | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2013 Henrik Nordstrom <henrik@henriknordstrom.net> |
Henrik Nordström | 26f9a6c | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
Henrik Nordström | 26f9a6c | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 6 | #include <common.h> |
Simon Glass | 2c2075a | 2016-02-29 15:25:57 -0700 | [diff] [blame] | 7 | #include <blk.h> |
| 8 | #include <dm.h> |
| 9 | #include <fdtdec.h> |
Henrik Nordström | 26f9a6c | 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 | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 14 | #include <asm/global_data.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 15 | #include <dm/device_compat.h> |
Masahiro Yamada | 56a931c | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 16 | #include <linux/errno.h> |
Simon Glass | 2c2075a | 2016-02-29 15:25:57 -0700 | [diff] [blame] | 17 | #include <dm/device-internal.h> |
Henrik Nordström | 26f9a6c | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 18 | |
Simon Glass | 2c2075a | 2016-02-29 15:25:57 -0700 | [diff] [blame] | 19 | DECLARE_GLOBAL_DATA_PTR; |
| 20 | |
Simon Glass | 86bc14f | 2016-05-01 11:35:49 -0600 | [diff] [blame] | 21 | #ifndef CONFIG_BLK |
Simon Glass | c6d7681 | 2021-11-24 09:26:40 -0700 | [diff] [blame] | 22 | static struct host_block_dev host_devices[SANDBOX_HOST_MAX_DEVICES]; |
Simon Glass | 86bc14f | 2016-05-01 11:35:49 -0600 | [diff] [blame] | 23 | |
| 24 | static struct host_block_dev *find_host_device(int dev) |
| 25 | { |
Simon Glass | c6d7681 | 2021-11-24 09:26:40 -0700 | [diff] [blame] | 26 | if (dev >= 0 && dev < SANDBOX_HOST_MAX_DEVICES) |
Simon Glass | 86bc14f | 2016-05-01 11:35:49 -0600 | [diff] [blame] | 27 | return &host_devices[dev]; |
| 28 | |
| 29 | return NULL; |
| 30 | } |
| 31 | #endif |
| 32 | |
| 33 | #ifdef CONFIG_BLK |
Simon Glass | 2c2075a | 2016-02-29 15:25:57 -0700 | [diff] [blame] | 34 | static unsigned long host_block_read(struct udevice *dev, |
| 35 | unsigned long start, lbaint_t blkcnt, |
| 36 | void *buffer) |
| 37 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 38 | struct host_block_dev *host_dev = dev_get_plat(dev); |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 39 | struct blk_desc *block_dev = dev_get_uclass_plat(dev); |
Simon Glass | 2c2075a | 2016-02-29 15:25:57 -0700 | [diff] [blame] | 40 | |
Simon Glass | 86bc14f | 2016-05-01 11:35:49 -0600 | [diff] [blame] | 41 | #else |
| 42 | static 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 Glass | 52e85ac | 2016-02-29 15:25:56 -0700 | [diff] [blame] | 53 | 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öm | 26f9a6c | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 56 | return -1; |
| 57 | } |
Simon Glass | 52e85ac | 2016-02-29 15:25:56 -0700 | [diff] [blame] | 58 | ssize_t len = os_read(host_dev->fd, buffer, blkcnt * block_dev->blksz); |
Henrik Nordström | 26f9a6c | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 59 | if (len >= 0) |
Simon Glass | 52e85ac | 2016-02-29 15:25:56 -0700 | [diff] [blame] | 60 | return len / block_dev->blksz; |
Henrik Nordström | 26f9a6c | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 61 | return -1; |
| 62 | } |
| 63 | |
Simon Glass | 86bc14f | 2016-05-01 11:35:49 -0600 | [diff] [blame] | 64 | #ifdef CONFIG_BLK |
Simon Glass | 2c2075a | 2016-02-29 15:25:57 -0700 | [diff] [blame] | 65 | static unsigned long host_block_write(struct udevice *dev, |
| 66 | unsigned long start, lbaint_t blkcnt, |
| 67 | const void *buffer) |
| 68 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 69 | struct host_block_dev *host_dev = dev_get_plat(dev); |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 70 | struct blk_desc *block_dev = dev_get_uclass_plat(dev); |
Simon Glass | 86bc14f | 2016-05-01 11:35:49 -0600 | [diff] [blame] | 71 | #else |
| 72 | static 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 Glass | 52e85ac | 2016-02-29 15:25:56 -0700 | [diff] [blame] | 79 | |
| 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öm | 26f9a6c | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 83 | return -1; |
| 84 | } |
Simon Glass | 52e85ac | 2016-02-29 15:25:56 -0700 | [diff] [blame] | 85 | ssize_t len = os_write(host_dev->fd, buffer, blkcnt * block_dev->blksz); |
Henrik Nordström | 26f9a6c | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 86 | if (len >= 0) |
Simon Glass | 52e85ac | 2016-02-29 15:25:56 -0700 | [diff] [blame] | 87 | return len / block_dev->blksz; |
Henrik Nordström | 26f9a6c | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 88 | return -1; |
| 89 | } |
| 90 | |
Simon Glass | 86bc14f | 2016-05-01 11:35:49 -0600 | [diff] [blame] | 91 | #ifdef CONFIG_BLK |
Simon Glass | b4d0077 | 2021-03-15 18:11:11 +1300 | [diff] [blame] | 92 | int host_dev_bind(int devnum, char *filename, bool removable) |
Simon Glass | 2c2075a | 2016-02-29 15:25:57 -0700 | [diff] [blame] | 93 | { |
| 94 | struct host_block_dev *host_dev; |
| 95 | struct udevice *dev; |
Heinrich Schuchardt | e3cfde9 | 2021-01-25 12:57:14 +0100 | [diff] [blame] | 96 | struct blk_desc *desc; |
Simon Glass | 2c2075a | 2016-02-29 15:25:57 -0700 | [diff] [blame] | 97 | 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 Roese | 80b5bc9 | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 103 | ret = device_remove(dev, DM_REMOVE_NORMAL); |
Simon Glass | 2c2075a | 2016-02-29 15:25:57 -0700 | [diff] [blame] | 104 | 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) { |
Simon Glass | 0883162 | 2022-02-28 15:13:47 -0700 | [diff] [blame] | 128 | printf("Failed to access host backing file '%s', trying read-only\n", |
| 129 | filename); |
| 130 | fd = os_open(filename, OS_O_RDONLY); |
| 131 | if (fd == -1) { |
| 132 | printf("- still failed\n"); |
| 133 | ret = -ENOENT; |
| 134 | goto err; |
| 135 | } |
Simon Glass | 2c2075a | 2016-02-29 15:25:57 -0700 | [diff] [blame] | 136 | } |
| 137 | ret = blk_create_device(gd->dm_root, "sandbox_host_blk", str, |
| 138 | IF_TYPE_HOST, devnum, 512, |
Jean-Jacques Hiblot | 99b324a | 2017-06-09 16:45:18 +0200 | [diff] [blame] | 139 | os_lseek(fd, 0, OS_SEEK_END) / 512, &dev); |
Simon Glass | 2c2075a | 2016-02-29 15:25:57 -0700 | [diff] [blame] | 140 | if (ret) |
| 141 | goto err_file; |
Bin Meng | 311f800 | 2018-10-15 02:21:05 -0700 | [diff] [blame] | 142 | |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 143 | host_dev = dev_get_plat(dev); |
Bin Meng | 311f800 | 2018-10-15 02:21:05 -0700 | [diff] [blame] | 144 | host_dev->fd = fd; |
| 145 | host_dev->filename = fname; |
| 146 | |
Simon Glass | 2c2075a | 2016-02-29 15:25:57 -0700 | [diff] [blame] | 147 | ret = device_probe(dev); |
| 148 | if (ret) { |
| 149 | device_unbind(dev); |
| 150 | goto err_file; |
| 151 | } |
| 152 | |
Heinrich Schuchardt | e3cfde9 | 2021-01-25 12:57:14 +0100 | [diff] [blame] | 153 | desc = blk_get_devnum_by_type(IF_TYPE_HOST, devnum); |
Simon Glass | b4d0077 | 2021-03-15 18:11:11 +1300 | [diff] [blame] | 154 | desc->removable = removable; |
Heinrich Schuchardt | e3cfde9 | 2021-01-25 12:57:14 +0100 | [diff] [blame] | 155 | snprintf(desc->vendor, BLK_VEN_SIZE, "U-Boot"); |
| 156 | snprintf(desc->product, BLK_PRD_SIZE, "hostfile"); |
| 157 | snprintf(desc->revision, BLK_REV_SIZE, "1.0"); |
| 158 | |
Bin Meng | cbc3da8 | 2018-10-15 02:21:07 -0700 | [diff] [blame] | 159 | return 0; |
Simon Glass | 2c2075a | 2016-02-29 15:25:57 -0700 | [diff] [blame] | 160 | err_file: |
| 161 | os_close(fd); |
| 162 | err: |
| 163 | free(fname); |
| 164 | free(str); |
| 165 | return ret; |
| 166 | } |
Simon Glass | 86bc14f | 2016-05-01 11:35:49 -0600 | [diff] [blame] | 167 | #else |
Simon Glass | b4d0077 | 2021-03-15 18:11:11 +1300 | [diff] [blame] | 168 | int host_dev_bind(int dev, char *filename, bool removable) |
Simon Glass | 86bc14f | 2016-05-01 11:35:49 -0600 | [diff] [blame] | 169 | { |
| 170 | struct host_block_dev *host_dev = find_host_device(dev); |
| 171 | |
| 172 | if (!host_dev) |
| 173 | return -1; |
| 174 | if (host_dev->blk_dev.priv) { |
| 175 | os_close(host_dev->fd); |
| 176 | host_dev->blk_dev.priv = NULL; |
| 177 | } |
| 178 | if (host_dev->filename) |
| 179 | free(host_dev->filename); |
| 180 | if (filename && *filename) { |
| 181 | host_dev->filename = strdup(filename); |
| 182 | } else { |
| 183 | host_dev->filename = NULL; |
| 184 | return 0; |
| 185 | } |
| 186 | |
| 187 | host_dev->fd = os_open(host_dev->filename, OS_O_RDWR); |
| 188 | if (host_dev->fd == -1) { |
| 189 | printf("Failed to access host backing file '%s'\n", |
| 190 | host_dev->filename); |
| 191 | return 1; |
| 192 | } |
| 193 | |
| 194 | struct blk_desc *blk_dev = &host_dev->blk_dev; |
| 195 | blk_dev->if_type = IF_TYPE_HOST; |
| 196 | blk_dev->priv = host_dev; |
| 197 | blk_dev->blksz = 512; |
| 198 | blk_dev->lba = os_lseek(host_dev->fd, 0, OS_SEEK_END) / blk_dev->blksz; |
| 199 | blk_dev->block_read = host_block_read; |
| 200 | blk_dev->block_write = host_block_write; |
| 201 | blk_dev->devnum = dev; |
| 202 | blk_dev->part_type = PART_TYPE_UNKNOWN; |
Simon Glass | b4d0077 | 2021-03-15 18:11:11 +1300 | [diff] [blame] | 203 | blk_dev->removable = removable; |
Heinrich Schuchardt | e3cfde9 | 2021-01-25 12:57:14 +0100 | [diff] [blame] | 204 | snprintf(blk_dev->vendor, BLK_VEN_SIZE, "U-Boot"); |
| 205 | snprintf(blk_dev->product, BLK_PRD_SIZE, "hostfile"); |
| 206 | snprintf(blk_dev->revision, BLK_REV_SIZE, "1.0"); |
Simon Glass | 86bc14f | 2016-05-01 11:35:49 -0600 | [diff] [blame] | 207 | part_init(blk_dev); |
| 208 | |
| 209 | return 0; |
| 210 | } |
| 211 | #endif |
Henrik Nordström | 26f9a6c | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 212 | |
Simon Glass | 52e85ac | 2016-02-29 15:25:56 -0700 | [diff] [blame] | 213 | int host_get_dev_err(int devnum, struct blk_desc **blk_devp) |
Henrik Nordström | 26f9a6c | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 214 | { |
Simon Glass | 86bc14f | 2016-05-01 11:35:49 -0600 | [diff] [blame] | 215 | #ifdef CONFIG_BLK |
Simon Glass | 2c2075a | 2016-02-29 15:25:57 -0700 | [diff] [blame] | 216 | struct udevice *dev; |
| 217 | int ret; |
| 218 | |
| 219 | ret = blk_get_device(IF_TYPE_HOST, devnum, &dev); |
| 220 | if (ret) |
| 221 | return ret; |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 222 | *blk_devp = dev_get_uclass_plat(dev); |
Simon Glass | 86bc14f | 2016-05-01 11:35:49 -0600 | [diff] [blame] | 223 | #else |
| 224 | struct host_block_dev *host_dev = find_host_device(devnum); |
| 225 | |
| 226 | if (!host_dev) |
| 227 | return -ENODEV; |
| 228 | |
| 229 | if (!host_dev->blk_dev.priv) |
| 230 | return -ENOENT; |
| 231 | |
| 232 | *blk_devp = &host_dev->blk_dev; |
| 233 | #endif |
Simon Glass | 2c2075a | 2016-02-29 15:25:57 -0700 | [diff] [blame] | 234 | |
Henrik Nordström | 26f9a6c | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 235 | return 0; |
| 236 | } |
| 237 | |
Simon Glass | 86bc14f | 2016-05-01 11:35:49 -0600 | [diff] [blame] | 238 | #ifdef CONFIG_BLK |
Heinrich Schuchardt | 49b5e2e | 2021-02-03 00:21:56 +0100 | [diff] [blame] | 239 | |
| 240 | int sandbox_host_unbind(struct udevice *dev) |
| 241 | { |
| 242 | struct host_block_dev *host_dev; |
| 243 | |
| 244 | /* Data validity is checked in host_dev_bind() */ |
| 245 | host_dev = dev_get_plat(dev); |
| 246 | os_close(host_dev->fd); |
| 247 | |
| 248 | return 0; |
| 249 | } |
| 250 | |
Simon Glass | 2c2075a | 2016-02-29 15:25:57 -0700 | [diff] [blame] | 251 | static const struct blk_ops sandbox_host_blk_ops = { |
| 252 | .read = host_block_read, |
| 253 | .write = host_block_write, |
| 254 | }; |
| 255 | |
| 256 | U_BOOT_DRIVER(sandbox_host_blk) = { |
| 257 | .name = "sandbox_host_blk", |
| 258 | .id = UCLASS_BLK, |
| 259 | .ops = &sandbox_host_blk_ops, |
Heinrich Schuchardt | 49b5e2e | 2021-02-03 00:21:56 +0100 | [diff] [blame] | 260 | .unbind = sandbox_host_unbind, |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 261 | .plat_auto = sizeof(struct host_block_dev), |
Simon Glass | 2c2075a | 2016-02-29 15:25:57 -0700 | [diff] [blame] | 262 | }; |
Simon Glass | a57299e | 2016-05-01 11:36:05 -0600 | [diff] [blame] | 263 | #else |
| 264 | U_BOOT_LEGACY_BLK(sandbox_host) = { |
| 265 | .if_typename = "host", |
| 266 | .if_type = IF_TYPE_HOST, |
Simon Glass | c6d7681 | 2021-11-24 09:26:40 -0700 | [diff] [blame] | 267 | .max_devs = SANDBOX_HOST_MAX_DEVICES, |
Simon Glass | a57299e | 2016-05-01 11:36:05 -0600 | [diff] [blame] | 268 | .get_dev = host_get_dev_err, |
| 269 | }; |
Simon Glass | 86bc14f | 2016-05-01 11:35:49 -0600 | [diff] [blame] | 270 | #endif |