blob: be4e02cb601a67d871070dbf2e42daacd5c16761 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Henrik Nordströmf4d8de42013-11-10 10:26:56 -07002/*
3 * Copyright (C) 2013 Henrik Nordstrom <henrik@henriknordstrom.net>
Henrik Nordströmf4d8de42013-11-10 10:26:56 -07004 */
5
Henrik Nordströmf4d8de42013-11-10 10:26:56 -07006#include <common.h>
Simon Glass40fd0502016-02-29 15:25:57 -07007#include <blk.h>
8#include <dm.h>
9#include <fdtdec.h>
Henrik Nordströmf4d8de42013-11-10 10:26:56 -070010#include <part.h>
11#include <os.h>
12#include <malloc.h>
Simon Glass95201812022-10-29 19:47:17 -060013#include <sandbox_host.h>
Simon Glass401d1c42020-10-30 21:38:53 -060014#include <asm/global_data.h>
Simon Glass336d4612020-02-03 07:36:16 -070015#include <dm/device_compat.h>
Simon Glass40fd0502016-02-29 15:25:57 -070016#include <dm/device-internal.h>
Simon Glass95201812022-10-29 19:47:17 -060017#include <linux/errno.h>
Henrik Nordströmf4d8de42013-11-10 10:26:56 -070018
Simon Glass40fd0502016-02-29 15:25:57 -070019DECLARE_GLOBAL_DATA_PTR;
20
Simon Glass40fd0502016-02-29 15:25:57 -070021static unsigned long host_block_read(struct udevice *dev,
22 unsigned long start, lbaint_t blkcnt,
23 void *buffer)
24{
Simon Glass95201812022-10-29 19:47:17 -060025 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 Glass40fd0502016-02-29 15:25:57 -070028
Simon Glass95201812022-10-29 19:47:17 -060029 if (os_lseek(plat->fd, start * desc->blksz, OS_SEEK_SET) == -1) {
Simon Glass7ded9592016-02-29 15:25:56 -070030 printf("ERROR: Invalid block %lx\n", start);
Henrik Nordströmf4d8de42013-11-10 10:26:56 -070031 return -1;
32 }
Simon Glass95201812022-10-29 19:47:17 -060033 ssize_t len = os_read(plat->fd, buffer, blkcnt * desc->blksz);
Henrik Nordströmf4d8de42013-11-10 10:26:56 -070034 if (len >= 0)
Simon Glass95201812022-10-29 19:47:17 -060035 return len / desc->blksz;
36
37 return -EIO;
Henrik Nordströmf4d8de42013-11-10 10:26:56 -070038}
39
Simon Glass40fd0502016-02-29 15:25:57 -070040static unsigned long host_block_write(struct udevice *dev,
41 unsigned long start, lbaint_t blkcnt,
42 const void *buffer)
43{
Simon Glass95201812022-10-29 19:47:17 -060044 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 Glass7ded9592016-02-29 15:25:56 -070047
Simon Glass95201812022-10-29 19:47:17 -060048 if (os_lseek(plat->fd, start * desc->blksz, OS_SEEK_SET) == -1) {
Simon Glass7ded9592016-02-29 15:25:56 -070049 printf("ERROR: Invalid block %lx\n", start);
Henrik Nordströmf4d8de42013-11-10 10:26:56 -070050 return -1;
51 }
Simon Glass95201812022-10-29 19:47:17 -060052 ssize_t len = os_write(plat->fd, buffer, blkcnt * desc->blksz);
Henrik Nordströmf4d8de42013-11-10 10:26:56 -070053 if (len >= 0)
Simon Glass95201812022-10-29 19:47:17 -060054 return len / desc->blksz;
Henrik Nordströmf4d8de42013-11-10 10:26:56 -070055
Simon Glass95201812022-10-29 19:47:17 -060056 return -EIO;
Heinrich Schuchardt297b8b32021-02-03 00:21:56 +010057}
58
Simon Glass40fd0502016-02-29 15:25:57 -070059static const struct blk_ops sandbox_host_blk_ops = {
60 .read = host_block_read,
61 .write = host_block_write,
62};
63
64U_BOOT_DRIVER(sandbox_host_blk) = {
65 .name = "sandbox_host_blk",
66 .id = UCLASS_BLK,
67 .ops = &sandbox_host_blk_ops,
Simon Glass40fd0502016-02-29 15:25:57 -070068};