~funderscore blog cgit wiki get in touch
aboutsummaryrefslogtreecommitdiff
blob: 6b8468f79947ff43b5f7d93df350daacdbee06b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// 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;
	}
	case SCSI_WRITE10: {
		const struct scsi_write10_req *write_req = (void *)req;

		info->seek_block = be32_to_cpu(write_req->lba);
		info->write_len = be16_to_cpu(write_req->xfer_len);
		info->buff_used = info->write_len * info->block_size;
		ret = SCSI_EMUL_DO_WRITE;
		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;
}