Henrik Nordström | 26f9a6c | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 Henrik Nordstrom <henrik@henriknordstrom.net> |
| 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0+ |
| 5 | */ |
| 6 | |
| 7 | #include <config.h> |
| 8 | #include <common.h> |
| 9 | #include <part.h> |
| 10 | #include <os.h> |
| 11 | #include <malloc.h> |
| 12 | #include <sandboxblockdev.h> |
| 13 | #include <asm/errno.h> |
| 14 | |
| 15 | static struct host_block_dev host_devices[CONFIG_HOST_MAX_DEVICES]; |
| 16 | |
| 17 | static struct host_block_dev *find_host_device(int dev) |
| 18 | { |
| 19 | if (dev >= 0 && dev < CONFIG_HOST_MAX_DEVICES) |
| 20 | return &host_devices[dev]; |
| 21 | |
| 22 | return NULL; |
| 23 | } |
| 24 | |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 25 | static unsigned long host_block_read(struct blk_desc *block_dev, |
Stephen Warren | e73f296 | 2015-12-07 11:38:48 -0700 | [diff] [blame] | 26 | unsigned long start, lbaint_t blkcnt, |
| 27 | void *buffer) |
Henrik Nordström | 26f9a6c | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 28 | { |
Simon Glass | 2f26fff | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 29 | int dev = block_dev->devnum; |
Henrik Nordström | 26f9a6c | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 30 | struct host_block_dev *host_dev = find_host_device(dev); |
| 31 | |
| 32 | if (!host_dev) |
| 33 | return -1; |
Simon Glass | 52e85ac | 2016-02-29 15:25:56 -0700 | [diff] [blame^] | 34 | |
| 35 | if (os_lseek(host_dev->fd, start * block_dev->blksz, OS_SEEK_SET) == |
| 36 | -1) { |
| 37 | printf("ERROR: Invalid block %lx\n", start); |
Henrik Nordström | 26f9a6c | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 38 | return -1; |
| 39 | } |
Simon Glass | 52e85ac | 2016-02-29 15:25:56 -0700 | [diff] [blame^] | 40 | 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] | 41 | if (len >= 0) |
Simon Glass | 52e85ac | 2016-02-29 15:25:56 -0700 | [diff] [blame^] | 42 | return len / block_dev->blksz; |
Henrik Nordström | 26f9a6c | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 43 | return -1; |
| 44 | } |
| 45 | |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 46 | static unsigned long host_block_write(struct blk_desc *block_dev, |
Stephen Warren | e73f296 | 2015-12-07 11:38:48 -0700 | [diff] [blame] | 47 | unsigned long start, lbaint_t blkcnt, |
| 48 | const void *buffer) |
Henrik Nordström | 26f9a6c | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 49 | { |
Simon Glass | 2f26fff | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 50 | int dev = block_dev->devnum; |
Henrik Nordström | 26f9a6c | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 51 | struct host_block_dev *host_dev = find_host_device(dev); |
Simon Glass | 52e85ac | 2016-02-29 15:25:56 -0700 | [diff] [blame^] | 52 | |
| 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_write(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 | |
| 64 | int host_dev_bind(int dev, char *filename) |
| 65 | { |
| 66 | struct host_block_dev *host_dev = find_host_device(dev); |
| 67 | |
| 68 | if (!host_dev) |
| 69 | return -1; |
| 70 | if (host_dev->blk_dev.priv) { |
| 71 | os_close(host_dev->fd); |
| 72 | host_dev->blk_dev.priv = NULL; |
| 73 | } |
| 74 | if (host_dev->filename) |
| 75 | free(host_dev->filename); |
| 76 | if (filename && *filename) { |
| 77 | host_dev->filename = strdup(filename); |
| 78 | } else { |
| 79 | host_dev->filename = NULL; |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | host_dev->fd = os_open(host_dev->filename, OS_O_RDWR); |
| 84 | if (host_dev->fd == -1) { |
| 85 | printf("Failed to access host backing file '%s'\n", |
| 86 | host_dev->filename); |
| 87 | return 1; |
| 88 | } |
| 89 | |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 90 | struct blk_desc *blk_dev = &host_dev->blk_dev; |
Henrik Nordström | 26f9a6c | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 91 | blk_dev->if_type = IF_TYPE_HOST; |
| 92 | blk_dev->priv = host_dev; |
| 93 | blk_dev->blksz = 512; |
| 94 | blk_dev->lba = os_lseek(host_dev->fd, 0, OS_SEEK_END) / blk_dev->blksz; |
| 95 | blk_dev->block_read = host_block_read; |
| 96 | blk_dev->block_write = host_block_write; |
Simon Glass | 2f26fff | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 97 | blk_dev->devnum = dev; |
Henrik Nordström | 26f9a6c | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 98 | blk_dev->part_type = PART_TYPE_UNKNOWN; |
Simon Glass | b89a844 | 2016-02-29 15:25:48 -0700 | [diff] [blame] | 99 | part_init(blk_dev); |
Henrik Nordström | 26f9a6c | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 100 | |
| 101 | return 0; |
| 102 | } |
| 103 | |
Simon Glass | 52e85ac | 2016-02-29 15:25:56 -0700 | [diff] [blame^] | 104 | 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] | 105 | { |
Simon Glass | 52e85ac | 2016-02-29 15:25:56 -0700 | [diff] [blame^] | 106 | struct host_block_dev *host_dev = find_host_device(devnum); |
Henrik Nordström | 26f9a6c | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 107 | |
| 108 | if (!host_dev) |
| 109 | return -ENODEV; |
| 110 | |
| 111 | if (!host_dev->blk_dev.priv) |
| 112 | return -ENOENT; |
| 113 | |
| 114 | *blk_devp = &host_dev->blk_dev; |
| 115 | return 0; |
| 116 | } |
| 117 | |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 118 | struct blk_desc *host_get_dev(int dev) |
Henrik Nordström | 26f9a6c | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 119 | { |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 120 | struct blk_desc *blk_dev; |
Henrik Nordström | 26f9a6c | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 121 | |
| 122 | if (host_get_dev_err(dev, &blk_dev)) |
| 123 | return NULL; |
| 124 | |
| 125 | return blk_dev; |
| 126 | } |