blob: be4e02cb601a67d871070dbf2e42daacd5c16761 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Henrik Nordström26f9a6c2013-11-10 10:26:56 -07002/*
3 * Copyright (C) 2013 Henrik Nordstrom <henrik@henriknordstrom.net>
Henrik Nordström26f9a6c2013-11-10 10:26:56 -07004 */
5
Henrik Nordström26f9a6c2013-11-10 10:26:56 -07006#include <common.h>
Simon Glass2c2075a2016-02-29 15:25:57 -07007#include <blk.h>
8#include <dm.h>
9#include <fdtdec.h>
Henrik Nordström26f9a6c2013-11-10 10:26:56 -070010#include <part.h>
11#include <os.h>
12#include <malloc.h>
Simon Glasse57f8d42022-10-29 19:47:17 -060013#include <sandbox_host.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060014#include <asm/global_data.h>
Simon Glass9bc15642020-02-03 07:36:16 -070015#include <dm/device_compat.h>
Simon Glass2c2075a2016-02-29 15:25:57 -070016#include <dm/device-internal.h>
Simon Glasse57f8d42022-10-29 19:47:17 -060017#include <linux/errno.h>
Henrik Nordström26f9a6c2013-11-10 10:26:56 -070018
Simon Glass2c2075a2016-02-29 15:25:57 -070019DECLARE_GLOBAL_DATA_PTR;
20
Simon Glass2c2075a2016-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 Glasse57f8d42022-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 Glass2c2075a2016-02-29 15:25:57 -070028
Simon Glasse57f8d42022-10-29 19:47:17 -060029 if (os_lseek(plat->fd, start * desc->blksz, OS_SEEK_SET) == -1) {
Simon Glass52e85ac2016-02-29 15:25:56 -070030 printf("ERROR: Invalid block %lx\n", start);
Henrik Nordström26f9a6c2013-11-10 10:26:56 -070031 return -1;
32 }
Simon Glasse57f8d42022-10-29 19:47:17 -060033 ssize_t len = os_read(plat->fd, buffer, blkcnt * desc->blksz);
Henrik Nordström26f9a6c2013-11-10 10:26:56 -070034 if (len >= 0)
Simon Glasse57f8d42022-10-29 19:47:17 -060035 return len / desc->blksz;
36
37 return -EIO;
Henrik Nordström26f9a6c2013-11-10 10:26:56 -070038}
39
Simon Glass2c2075a2016-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 Glasse57f8d42022-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 Glass52e85ac2016-02-29 15:25:56 -070047
Simon Glasse57f8d42022-10-29 19:47:17 -060048 if (os_lseek(plat->fd, start * desc->blksz, OS_SEEK_SET) == -1) {
Simon Glass52e85ac2016-02-29 15:25:56 -070049 printf("ERROR: Invalid block %lx\n", start);
Henrik Nordström26f9a6c2013-11-10 10:26:56 -070050 return -1;
51 }
Simon Glasse57f8d42022-10-29 19:47:17 -060052 ssize_t len = os_write(plat->fd, buffer, blkcnt * desc->blksz);
Henrik Nordström26f9a6c2013-11-10 10:26:56 -070053 if (len >= 0)
Simon Glasse57f8d42022-10-29 19:47:17 -060054 return len / desc->blksz;
Bin Meng311f8002018-10-15 02:21:05 -070055
Simon Glasse57f8d42022-10-29 19:47:17 -060056 return -EIO;
Heinrich Schuchardt49b5e2e2021-02-03 00:21:56 +010057}
58
Simon Glass2c2075a2016-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 Glass2c2075a2016-02-29 15:25:57 -070068};