1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2015 Google, Inc
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
16 #include <scsi_emul.h>
19 SANDBOX_SCSI_BLOCK_LEN = 512,
20 SANDBOX_SCSI_BUF_SIZE = 512,
24 * struct sandbox_scsi_priv
26 * @eminfo: emulator state
27 * @pathanme: Path to the backing file, e.g. 'scsi.img'
28 * @fd: File descriptor of backing file
30 struct sandbox_scsi_priv {
31 struct scsi_emul_info eminfo;
36 static int sandbox_scsi_exec(struct udevice *dev, struct scsi_cmd *req)
38 struct sandbox_scsi_priv *priv = dev_get_priv(dev);
39 struct scsi_emul_info *info = &priv->eminfo;
42 if (req->lun || req->target)
44 ret = sb_scsi_emul_command(info, req, req->cmdlen);
46 log_debug("SCSI command 0x%02x ret errno %d\n", req->cmd[0],
49 } else if (ret == SCSI_EMUL_DO_READ && priv->fd != -1) {
52 log_debug("read %x %x\n", info->seek_block, info->read_len);
53 os_lseek(priv->fd, info->seek_block * info->block_size,
55 bytes_read = os_read(priv->fd, req->pdata, info->buff_used);
58 if (bytes_read != info->buff_used)
61 req->pdata = info->buff;
62 info->phase = SCSIPH_STATUS;
63 log_debug("sending buf\n");
72 static int sandbox_scsi_bus_reset(struct udevice *dev)
79 static int sandbox_scsi_of_to_plat(struct udevice *dev)
81 struct sandbox_scsi_priv *priv = dev_get_priv(dev);
83 priv->pathname = dev_read_string(dev, "sandbox,filepath");
88 static int sandbox_scsi_probe(struct udevice *dev)
90 struct scsi_plat *scsi_plat = dev_get_uclass_plat(dev);
91 struct sandbox_scsi_priv *priv = dev_get_priv(dev);
92 struct scsi_emul_info *info = &priv->eminfo;
95 scsi_plat->max_id = 2;
96 scsi_plat->max_lun = 3;
97 scsi_plat->max_bytes_per_req = 1 << 20;
99 info->vendor = "SANDBOX";
100 info->product = "FAKE DISK";
101 info->buff = malloc(SANDBOX_SCSI_BUF_SIZE);
103 return log_ret(-ENOMEM);
104 info->block_size = SANDBOX_SCSI_BLOCK_LEN;
106 if (priv->pathname) {
107 priv->fd = os_open(priv->pathname, OS_O_RDONLY);
108 if (priv->fd != -1) {
109 ret = os_get_filesize(priv->pathname, &info->file_size);
111 return log_msg_ret("sz", ret);
116 log_debug("filename: %s, fd %d\n", priv->pathname, priv->fd);
121 static int sandbox_scsi_remove(struct udevice *dev)
123 struct sandbox_scsi_priv *priv = dev_get_priv(dev);
124 struct scsi_emul_info *info = &priv->eminfo;
131 struct scsi_ops sandbox_scsi_ops = {
132 .exec = sandbox_scsi_exec,
133 .bus_reset = sandbox_scsi_bus_reset,
136 static const struct udevice_id sanbox_scsi_ids[] = {
137 { .compatible = "sandbox,scsi" },
141 U_BOOT_DRIVER(sandbox_scsi) = {
142 .name = "sandbox_scsi",
144 .ops = &sandbox_scsi_ops,
145 .of_match = sanbox_scsi_ids,
146 .of_to_plat = sandbox_scsi_of_to_plat,
147 .probe = sandbox_scsi_probe,
148 .remove = sandbox_scsi_remove,
149 .priv_auto = sizeof(struct sandbox_scsi_priv),