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 | |
Simon Glass | 2c2075a | 2016-02-29 15:25:57 -0700 | [diff] [blame] | 6 | #include <blk.h> |
| 7 | #include <dm.h> |
| 8 | #include <fdtdec.h> |
Henrik Nordström | 26f9a6c | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 9 | #include <part.h> |
| 10 | #include <os.h> |
| 11 | #include <malloc.h> |
Simon Glass | e57f8d4 | 2022-10-29 19:47:17 -0600 | [diff] [blame] | 12 | #include <sandbox_host.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 13 | #include <asm/global_data.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 14 | #include <dm/device_compat.h> |
Simon Glass | 2c2075a | 2016-02-29 15:25:57 -0700 | [diff] [blame] | 15 | #include <dm/device-internal.h> |
Simon Glass | e57f8d4 | 2022-10-29 19:47:17 -0600 | [diff] [blame] | 16 | #include <linux/errno.h> |
Henrik Nordström | 26f9a6c | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 17 | |
Simon Glass | 2c2075a | 2016-02-29 15:25:57 -0700 | [diff] [blame] | 18 | DECLARE_GLOBAL_DATA_PTR; |
| 19 | |
Simon Glass | 2c2075a | 2016-02-29 15:25:57 -0700 | [diff] [blame] | 20 | static unsigned long host_block_read(struct udevice *dev, |
| 21 | unsigned long start, lbaint_t blkcnt, |
| 22 | void *buffer) |
| 23 | { |
Simon Glass | e57f8d4 | 2022-10-29 19:47:17 -0600 | [diff] [blame] | 24 | struct blk_desc *desc = dev_get_uclass_plat(dev); |
| 25 | struct udevice *host_dev = dev_get_parent(dev); |
| 26 | struct host_sb_plat *plat = dev_get_plat(host_dev); |
Simon Glass | 2c2075a | 2016-02-29 15:25:57 -0700 | [diff] [blame] | 27 | |
Simon Glass | e57f8d4 | 2022-10-29 19:47:17 -0600 | [diff] [blame] | 28 | if (os_lseek(plat->fd, start * desc->blksz, OS_SEEK_SET) == -1) { |
Simon Glass | 52e85ac | 2016-02-29 15:25:56 -0700 | [diff] [blame] | 29 | printf("ERROR: Invalid block %lx\n", start); |
Henrik Nordström | 26f9a6c | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 30 | return -1; |
| 31 | } |
Simon Glass | e57f8d4 | 2022-10-29 19:47:17 -0600 | [diff] [blame] | 32 | ssize_t len = os_read(plat->fd, buffer, blkcnt * desc->blksz); |
Henrik Nordström | 26f9a6c | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 33 | if (len >= 0) |
Simon Glass | e57f8d4 | 2022-10-29 19:47:17 -0600 | [diff] [blame] | 34 | return len / desc->blksz; |
| 35 | |
| 36 | return -EIO; |
Henrik Nordström | 26f9a6c | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 37 | } |
| 38 | |
Simon Glass | 2c2075a | 2016-02-29 15:25:57 -0700 | [diff] [blame] | 39 | static unsigned long host_block_write(struct udevice *dev, |
| 40 | unsigned long start, lbaint_t blkcnt, |
| 41 | const void *buffer) |
| 42 | { |
Simon Glass | e57f8d4 | 2022-10-29 19:47:17 -0600 | [diff] [blame] | 43 | struct blk_desc *desc = dev_get_uclass_plat(dev); |
| 44 | struct udevice *host_dev = dev_get_parent(dev); |
| 45 | struct host_sb_plat *plat = dev_get_plat(host_dev); |
Simon Glass | 52e85ac | 2016-02-29 15:25:56 -0700 | [diff] [blame] | 46 | |
Simon Glass | e57f8d4 | 2022-10-29 19:47:17 -0600 | [diff] [blame] | 47 | if (os_lseek(plat->fd, start * desc->blksz, OS_SEEK_SET) == -1) { |
Simon Glass | 52e85ac | 2016-02-29 15:25:56 -0700 | [diff] [blame] | 48 | printf("ERROR: Invalid block %lx\n", start); |
Henrik Nordström | 26f9a6c | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 49 | return -1; |
| 50 | } |
Simon Glass | e57f8d4 | 2022-10-29 19:47:17 -0600 | [diff] [blame] | 51 | ssize_t len = os_write(plat->fd, buffer, blkcnt * desc->blksz); |
Henrik Nordström | 26f9a6c | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 52 | if (len >= 0) |
Simon Glass | e57f8d4 | 2022-10-29 19:47:17 -0600 | [diff] [blame] | 53 | return len / desc->blksz; |
Bin Meng | 311f800 | 2018-10-15 02:21:05 -0700 | [diff] [blame] | 54 | |
Simon Glass | e57f8d4 | 2022-10-29 19:47:17 -0600 | [diff] [blame] | 55 | return -EIO; |
Heinrich Schuchardt | 49b5e2e | 2021-02-03 00:21:56 +0100 | [diff] [blame] | 56 | } |
| 57 | |
Simon Glass | 2c2075a | 2016-02-29 15:25:57 -0700 | [diff] [blame] | 58 | static const struct blk_ops sandbox_host_blk_ops = { |
| 59 | .read = host_block_read, |
| 60 | .write = host_block_write, |
| 61 | }; |
| 62 | |
| 63 | U_BOOT_DRIVER(sandbox_host_blk) = { |
| 64 | .name = "sandbox_host_blk", |
| 65 | .id = UCLASS_BLK, |
| 66 | .ops = &sandbox_host_blk_ops, |
Simon Glass | 2c2075a | 2016-02-29 15:25:57 -0700 | [diff] [blame] | 67 | }; |