1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (C) 2013 Henrik Nordstrom <henrik@henriknordstrom.net>
4 */
5
6 #include <common.h>
7 #include <blk.h>
8 #include <dm.h>
9 #include <fdtdec.h>
10 #include <part.h>
11 #include <os.h>
12 #include <malloc.h>
13 #include <sandbox_host.h>
14 #include <asm/global_data.h>
15 #include <dm/device_compat.h>
16 #include <dm/device-internal.h>
17 #include <linux/errno.h>
18
19 DECLARE_GLOBAL_DATA_PTR;
20
host_block_read(struct udevice * dev,unsigned long start,lbaint_t blkcnt,void * buffer)21 static unsigned long host_block_read(struct udevice *dev,
22 unsigned long start, lbaint_t blkcnt,
23 void *buffer)
24 {
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);
28
29 if (os_lseek(plat->fd, start * desc->blksz, OS_SEEK_SET) == -1) {
30 printf("ERROR: Invalid block %lx\n", start);
31 return -1;
32 }
33 ssize_t len = os_read(plat->fd, buffer, blkcnt * desc->blksz);
34 if (len >= 0)
35 return len / desc->blksz;
36
37 return -EIO;
38 }
39
host_block_write(struct udevice * dev,unsigned long start,lbaint_t blkcnt,const void * buffer)40 static unsigned long host_block_write(struct udevice *dev,
41 unsigned long start, lbaint_t blkcnt,
42 const void *buffer)
43 {
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);
47
48 if (os_lseek(plat->fd, start * desc->blksz, OS_SEEK_SET) == -1) {
49 printf("ERROR: Invalid block %lx\n", start);
50 return -1;
51 }
52 ssize_t len = os_write(plat->fd, buffer, blkcnt * desc->blksz);
53 if (len >= 0)
54 return len / desc->blksz;
55
56 return -EIO;
57 }
58
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,
68 };
69