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