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> |
Simon Glass | 9520181 | 2022-10-29 19:47:17 -0600 | [diff] [blame] | 13 | #include <sandbox_host.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 14 | #include <asm/global_data.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 15 | #include <dm/device_compat.h> |
Simon Glass | 40fd050 | 2016-02-29 15:25:57 -0700 | [diff] [blame] | 16 | #include <dm/device-internal.h> |
Simon Glass | 9520181 | 2022-10-29 19:47:17 -0600 | [diff] [blame] | 17 | #include <linux/errno.h> |
Henrik Nordström | f4d8de4 | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 18 | |
Simon Glass | 40fd050 | 2016-02-29 15:25:57 -0700 | [diff] [blame] | 19 | DECLARE_GLOBAL_DATA_PTR; |
| 20 | |
Simon Glass | 40fd050 | 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 | 9520181 | 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 | 40fd050 | 2016-02-29 15:25:57 -0700 | [diff] [blame] | 28 | |
Simon Glass | 9520181 | 2022-10-29 19:47:17 -0600 | [diff] [blame] | 29 | if (os_lseek(plat->fd, start * desc->blksz, OS_SEEK_SET) == -1) { |
Simon Glass | 7ded959 | 2016-02-29 15:25:56 -0700 | [diff] [blame] | 30 | printf("ERROR: Invalid block %lx\n", start); |
Henrik Nordström | f4d8de4 | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 31 | return -1; |
| 32 | } |
Simon Glass | 9520181 | 2022-10-29 19:47:17 -0600 | [diff] [blame] | 33 | ssize_t len = os_read(plat->fd, buffer, blkcnt * desc->blksz); |
Henrik Nordström | f4d8de4 | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 34 | if (len >= 0) |
Simon Glass | 9520181 | 2022-10-29 19:47:17 -0600 | [diff] [blame] | 35 | return len / desc->blksz; |
| 36 | |
| 37 | return -EIO; |
Henrik Nordström | f4d8de4 | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 38 | } |
| 39 | |
Simon Glass | 40fd050 | 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 | 9520181 | 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 | 7ded959 | 2016-02-29 15:25:56 -0700 | [diff] [blame] | 47 | |
Simon Glass | 9520181 | 2022-10-29 19:47:17 -0600 | [diff] [blame] | 48 | if (os_lseek(plat->fd, start * desc->blksz, OS_SEEK_SET) == -1) { |
Simon Glass | 7ded959 | 2016-02-29 15:25:56 -0700 | [diff] [blame] | 49 | printf("ERROR: Invalid block %lx\n", start); |
Henrik Nordström | f4d8de4 | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 50 | return -1; |
| 51 | } |
Simon Glass | 9520181 | 2022-10-29 19:47:17 -0600 | [diff] [blame] | 52 | ssize_t len = os_write(plat->fd, buffer, blkcnt * desc->blksz); |
Henrik Nordström | f4d8de4 | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 53 | if (len >= 0) |
Simon Glass | 9520181 | 2022-10-29 19:47:17 -0600 | [diff] [blame] | 54 | return len / desc->blksz; |
Henrik Nordström | f4d8de4 | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 55 | |
Simon Glass | 9520181 | 2022-10-29 19:47:17 -0600 | [diff] [blame] | 56 | return -EIO; |
Heinrich Schuchardt | 297b8b3 | 2021-02-03 00:21:56 +0100 | [diff] [blame] | 57 | } |
| 58 | |
Simon Glass | 40fd050 | 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 | 40fd050 | 2016-02-29 15:25:57 -0700 | [diff] [blame] | 68 | }; |