blob: 6c74d66037e74706956e54803468cb15e61fcf39 [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
Simon Glass2c2075a2016-02-29 15:25:57 -07006#include <blk.h>
7#include <dm.h>
8#include <fdtdec.h>
Henrik Nordström26f9a6c2013-11-10 10:26:56 -07009#include <part.h>
10#include <os.h>
11#include <malloc.h>
Simon Glasse57f8d42022-10-29 19:47:17 -060012#include <sandbox_host.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060013#include <asm/global_data.h>
Simon Glass9bc15642020-02-03 07:36:16 -070014#include <dm/device_compat.h>
Simon Glass2c2075a2016-02-29 15:25:57 -070015#include <dm/device-internal.h>
Simon Glasse57f8d42022-10-29 19:47:17 -060016#include <linux/errno.h>
Henrik Nordström26f9a6c2013-11-10 10:26:56 -070017
Simon Glass2c2075a2016-02-29 15:25:57 -070018DECLARE_GLOBAL_DATA_PTR;
19
Simon Glass2c2075a2016-02-29 15:25:57 -070020static unsigned long host_block_read(struct udevice *dev,
21 unsigned long start, lbaint_t blkcnt,
22 void *buffer)
23{
Simon Glasse57f8d42022-10-29 19:47:17 -060024 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);
Simon Glass2c2075a2016-02-29 15:25:57 -070027
Simon Glass3dbd4ae2024-08-07 16:47:24 -060028 if (os_lseek(plat->fd, start * desc->blksz, OS_SEEK_SET) < 0) {
Simon Glass52e85ac2016-02-29 15:25:56 -070029 printf("ERROR: Invalid block %lx\n", start);
Henrik Nordström26f9a6c2013-11-10 10:26:56 -070030 return -1;
31 }
Simon Glasse57f8d42022-10-29 19:47:17 -060032 ssize_t len = os_read(plat->fd, buffer, blkcnt * desc->blksz);
Henrik Nordström26f9a6c2013-11-10 10:26:56 -070033 if (len >= 0)
Simon Glasse57f8d42022-10-29 19:47:17 -060034 return len / desc->blksz;
35
36 return -EIO;
Henrik Nordström26f9a6c2013-11-10 10:26:56 -070037}
38
Simon Glass2c2075a2016-02-29 15:25:57 -070039static unsigned long host_block_write(struct udevice *dev,
40 unsigned long start, lbaint_t blkcnt,
41 const void *buffer)
42{
Simon Glasse57f8d42022-10-29 19:47:17 -060043 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);
Simon Glass52e85ac2016-02-29 15:25:56 -070046
Simon Glass3dbd4ae2024-08-07 16:47:24 -060047 if (os_lseek(plat->fd, start * desc->blksz, OS_SEEK_SET) < 0) {
Simon Glass52e85ac2016-02-29 15:25:56 -070048 printf("ERROR: Invalid block %lx\n", start);
Henrik Nordström26f9a6c2013-11-10 10:26:56 -070049 return -1;
50 }
Simon Glasse57f8d42022-10-29 19:47:17 -060051 ssize_t len = os_write(plat->fd, buffer, blkcnt * desc->blksz);
Henrik Nordström26f9a6c2013-11-10 10:26:56 -070052 if (len >= 0)
Simon Glasse57f8d42022-10-29 19:47:17 -060053 return len / desc->blksz;
Bin Meng311f8002018-10-15 02:21:05 -070054
Simon Glasse57f8d42022-10-29 19:47:17 -060055 return -EIO;
Heinrich Schuchardt49b5e2e2021-02-03 00:21:56 +010056}
57
Simon Glass2c2075a2016-02-29 15:25:57 -070058static const struct blk_ops sandbox_host_blk_ops = {
59 .read = host_block_read,
60 .write = host_block_write,
61};
62
63U_BOOT_DRIVER(sandbox_host_blk) = {
64 .name = "sandbox_host_blk",
65 .id = UCLASS_BLK,
66 .ops = &sandbox_host_blk_ops,
Simon Glass2c2075a2016-02-29 15:25:57 -070067};