aboutsummaryrefslogtreecommitdiff
path: root/drivers/block/sandbox.c
blob: ec34f1ad8c2e9471723dd6485563b514c3bf546b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright (C) 2013 Henrik Nordstrom <henrik@henriknordstrom.net>
 */

#include <blk.h>
#include <dm.h>
#include <fdtdec.h>
#include <part.h>
#include <os.h>
#include <malloc.h>
#include <sandbox_host.h>
#include <asm/global_data.h>
#include <dm/device_compat.h>
#include <dm/device-internal.h>
#include <linux/errno.h>

DECLARE_GLOBAL_DATA_PTR;

static unsigned long host_block_read(struct udevice *dev,
				     unsigned long start, lbaint_t blkcnt,
				     void *buffer)
{
	struct blk_desc *desc = dev_get_uclass_plat(dev);
	struct udevice *host_dev = dev_get_parent(dev);
	struct host_sb_plat *plat = dev_get_plat(host_dev);

	if (os_lseek(plat->fd, start * desc->blksz, OS_SEEK_SET) == -1) {
		printf("ERROR: Invalid block %lx\n", start);
		return -1;
	}
	ssize_t len = os_read(plat->fd, buffer, blkcnt * desc->blksz);
	if (len >= 0)
		return len / desc->blksz;

	return -EIO;
}

static unsigned long host_block_write(struct udevice *dev,
				      unsigned long start, lbaint_t blkcnt,
				      const void *buffer)
{
	struct blk_desc *desc = dev_get_uclass_plat(dev);
	struct udevice *host_dev = dev_get_parent(dev);
	struct host_sb_plat *plat = dev_get_plat(host_dev);

	if (os_lseek(plat->fd, start * desc->blksz, OS_SEEK_SET) == -1) {
		printf("ERROR: Invalid block %lx\n", start);
		return -1;
	}
	ssize_t len = os_write(plat->fd, buffer, blkcnt * desc->blksz);
	if (len >= 0)
		return len / desc->blksz;

	return -EIO;
}

static const struct blk_ops sandbox_host_blk_ops = {
	.read	= host_block_read,
	.write	= host_block_write,
};

U_BOOT_DRIVER(sandbox_host_blk) = {
	.name		= "sandbox_host_blk",
	.id		= UCLASS_BLK,
	.ops		= &sandbox_host_blk_ops,
};