1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
6 * This file contains dummy implementations of SCSI functions requried so
7 * that CONFIG_SCSI can be enabled for sandbox.
10 #define LOG_CATEGORY UCLASS_SCSI
17 #include <scsi_emul.h>
20 SANDBOX_SCSI_BLOCK_LEN = 512,
21 SANDBOX_SCSI_BUF_SIZE = 512,
25 * struct sandbox_scsi_priv
27 * @eminfo: emulator state
28 * @pathanme: Path to the backing file, e.g. 'scsi.img'
29 * @fd: File descriptor of backing file
31 struct sandbox_scsi_priv {
32 struct scsi_emul_info eminfo;
37 static int sandbox_scsi_exec(struct udevice *dev, struct scsi_cmd *req)
39 struct sandbox_scsi_priv *priv = dev_get_priv(dev);
40 struct scsi_emul_info *info = &priv->eminfo;
43 if (req->lun || req->target)
45 ret = sb_scsi_emul_command(info, req, req->cmdlen);
47 log_debug("SCSI command 0x%02x ret errno %d\n", req->cmd[0],
50 } else if (ret == SCSI_EMUL_DO_READ && priv->fd != -1) {
53 log_debug("read %x %x\n", info->seek_block, info->read_len);
54 os_lseek(priv->fd, info->seek_block * info->block_size,
56 bytes_read = os_read(priv->fd, req->pdata, info->buff_used);
59 if (bytes_read != info->buff_used)
62 req->pdata = info->buff;
63 info->phase = SCSIPH_STATUS;
64 log_debug("sending buf\n");
73 static int sandbox_scsi_bus_reset(struct udevice *dev)
80 static int sandbox_scsi_of_to_plat(struct udevice *dev)
82 struct sandbox_scsi_priv *priv = dev_get_priv(dev);
84 priv->pathname = dev_read_string(dev, "sandbox,filepath");
89 static int sandbox_scsi_probe(struct udevice *dev)
91 struct scsi_plat *scsi_plat = dev_get_uclass_plat(dev);
92 struct sandbox_scsi_priv *priv = dev_get_priv(dev);
93 struct scsi_emul_info *info = &priv->eminfo;
96 scsi_plat->max_id = 2;
97 scsi_plat->max_lun = 3;
98 scsi_plat->max_bytes_per_req = 1 << 20;
100 info->vendor = "SANDBOX";
101 info->product = "FAKE DISK";
102 info->buff = malloc(SANDBOX_SCSI_BUF_SIZE);
104 return log_ret(-ENOMEM);
105 info->block_size = SANDBOX_SCSI_BLOCK_LEN;
107 if (priv->pathname) {
108 priv->fd = os_open(priv->pathname, OS_O_RDONLY);
109 if (priv->fd != -1) {
110 ret = os_get_filesize(priv->pathname, &info->file_size);
112 return log_msg_ret("sz", ret);
117 log_debug("filename: %s, fd %d\n", priv->pathname, priv->fd);
122 static int sandbox_scsi_remove(struct udevice *dev)
124 struct sandbox_scsi_priv *priv = dev_get_priv(dev);
125 struct scsi_emul_info *info = &priv->eminfo;
132 struct scsi_ops sandbox_scsi_ops = {
133 .exec = sandbox_scsi_exec,
134 .bus_reset = sandbox_scsi_bus_reset,
137 static const struct udevice_id sanbox_scsi_ids[] = {
138 { .compatible = "sandbox,scsi" },
142 U_BOOT_DRIVER(sandbox_scsi) = {
143 .name = "sandbox_scsi",
145 .ops = &sandbox_scsi_ops,
146 .of_match = sanbox_scsi_ids,
147 .of_to_plat = sandbox_scsi_of_to_plat,
148 .probe = sandbox_scsi_probe,
149 .remove = sandbox_scsi_remove,
150 .priv_auto = sizeof(struct sandbox_scsi_priv),