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