sandbox: scsi: Move request-handling code to scsi_emul

Move this code into the emulator file so it can be used by multiple
drivers.

Signed-off-by: Simon Glass <sjg@chromium.org>
diff --git a/drivers/scsi/Makefile b/drivers/scsi/Makefile
index 25194ee..d1b40c6 100644
--- a/drivers/scsi/Makefile
+++ b/drivers/scsi/Makefile
@@ -17,4 +17,5 @@
 
 ifdef CONFIG_SCSI
 obj-$(CONFIG_SANDBOX) += sandbox_scsi.o
+obj-$(CONFIG_SANDBOX) += scsi_emul.o
 endif
diff --git a/drivers/scsi/scsi_emul.c b/drivers/scsi/scsi_emul.c
new file mode 100644
index 0000000..5ba364b
--- /dev/null
+++ b/drivers/scsi/scsi_emul.c
@@ -0,0 +1,74 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Emulation of enough SCSI commands to find and read from a unit
+ *
+ * Copyright 2022 Google LLC
+ * Written by Simon Glass <sjg@chromium.org>
+ *
+ * implementation of SCSI functions required so that CONFIG_SCSI can be enabled
+ * for sandbox.
+ */
+
+#define LOG_CATEGORY UCLASS_SCSI
+
+#include <common.h>
+#include <dm.h>
+#include <log.h>
+#include <scsi.h>
+#include <scsi_emul.h>
+
+int sb_scsi_emul_command(struct scsi_emul_info *info,
+			 const struct scsi_cmd *req, int len)
+{
+	int ret = 0;
+
+	info->buff_used = 0;
+	log_debug("emul %x\n", *req->cmd);
+	switch (*req->cmd) {
+	case SCSI_INQUIRY: {
+		struct scsi_inquiry_resp *resp = (void *)info->buff;
+
+		info->alloc_len = req->cmd[4];
+		memset(resp, '\0', sizeof(*resp));
+		resp->data_format = 1;
+		resp->additional_len = 0x1f;
+		strncpy(resp->vendor, info->vendor, sizeof(resp->vendor));
+		strncpy(resp->product, info->product, sizeof(resp->product));
+		strncpy(resp->revision, "1.0", sizeof(resp->revision));
+		info->buff_used = sizeof(*resp);
+		break;
+	}
+	case SCSI_TST_U_RDY:
+		break;
+	case SCSI_RD_CAPAC: {
+		struct scsi_read_capacity_resp *resp = (void *)info->buff;
+		uint blocks;
+
+		if (info->file_size)
+			blocks = info->file_size / info->block_size - 1;
+		else
+			blocks = 0;
+		resp->last_block_addr = cpu_to_be32(blocks);
+		resp->block_len = cpu_to_be32(info->block_size);
+		info->buff_used = sizeof(*resp);
+		break;
+	}
+	case SCSI_READ10: {
+		const struct scsi_read10_req *read_req = (void *)req;
+
+		info->seek_block = be32_to_cpu(read_req->lba);
+		info->read_len = be16_to_cpu(read_req->xfer_len);
+		info->buff_used = info->read_len * info->block_size;
+		ret = SCSI_EMUL_DO_READ;
+		break;
+	}
+	default:
+		debug("Command not supported: %x\n", req->cmd[0]);
+		ret = -EPROTONOSUPPORT;
+	}
+	if (ret >= 0)
+		info->phase = info->transfer_len ? SCSIPH_DATA : SCSIPH_STATUS;
+	log_debug("   - done %x: ret=%d\n", *req->cmd, ret);
+
+	return ret;
+}