blob: 7b9a99c1a387b116c04ae59d461b08e10acfd7a7 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassb1446442015-03-25 12:22:39 -06002/*
3 * (C) Copyright 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glassb1446442015-03-25 12:22:39 -06005 */
6
7#include <common.h>
8#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -06009#include <log.h>
Simon Glass293c43f2022-09-21 16:21:37 +020010#include <malloc.h>
Simon Glassb1446442015-03-25 12:22:39 -060011#include <os.h>
12#include <scsi.h>
Simon Glass4c92a4d2022-09-21 16:21:36 +020013#include <scsi_emul.h>
Simon Glassb1446442015-03-25 12:22:39 -060014#include <usb.h>
15
Simon Glassb1446442015-03-25 12:22:39 -060016/*
17 * This driver emulates a flash stick using the UFI command specification and
18 * the BBB (bulk/bulk/bulk) protocol. It supports only a single logical unit
19 * number (LUN 0).
20 */
21
22enum {
23 SANDBOX_FLASH_EP_OUT = 1, /* endpoints */
24 SANDBOX_FLASH_EP_IN = 2,
25 SANDBOX_FLASH_BLOCK_LEN = 512,
Simon Glass293c43f2022-09-21 16:21:37 +020026 SANDBOX_FLASH_BUF_SIZE = 512,
Simon Glassb1446442015-03-25 12:22:39 -060027};
28
Simon Glass31d78162015-11-08 23:47:53 -070029enum {
30 STRINGID_MANUFACTURER = 1,
31 STRINGID_PRODUCT,
32 STRINGID_SERIAL,
33
34 STRINGID_COUNT,
35};
36
Simon Glassb1446442015-03-25 12:22:39 -060037/**
38 * struct sandbox_flash_priv - private state for this driver
39 *
Simon Glass4c92a4d2022-09-21 16:21:36 +020040 * @eminfo: emulator state
Simon Glassb1446442015-03-25 12:22:39 -060041 * @error: true if there is an error condition
Simon Glassb1446442015-03-25 12:22:39 -060042 * @tag: Tag value from last command
43 * @fd: File descriptor of backing file
44 * @file_size: Size of file in bytes
45 * @status_buff: Data buffer for outgoing status
Simon Glassb1446442015-03-25 12:22:39 -060046 */
47struct sandbox_flash_priv {
Simon Glass4c92a4d2022-09-21 16:21:36 +020048 struct scsi_emul_info eminfo;
Simon Glassb1446442015-03-25 12:22:39 -060049 bool error;
Simon Glassb1446442015-03-25 12:22:39 -060050 u32 tag;
51 int fd;
Simon Glassb1446442015-03-25 12:22:39 -060052 struct umass_bbb_csw status;
Simon Glassb1446442015-03-25 12:22:39 -060053};
54
55struct sandbox_flash_plat {
56 const char *pathname;
Simon Glass31d78162015-11-08 23:47:53 -070057 struct usb_string flash_strings[STRINGID_COUNT];
Simon Glassb1446442015-03-25 12:22:39 -060058};
59
60struct scsi_inquiry_resp {
61 u8 type;
62 u8 flags;
63 u8 version;
64 u8 data_format;
65 u8 additional_len;
66 u8 spare[3];
67 char vendor[8];
68 char product[16];
69 char revision[4];
70};
71
72struct scsi_read_capacity_resp {
73 u32 last_block_addr;
74 u32 block_len;
75};
76
77struct __packed scsi_read10_req {
78 u8 cmd;
79 u8 lun_flags;
80 u32 lba;
81 u8 spare;
Simon Glasse241fc32022-09-21 16:21:34 +020082 u16 xfer_len;
Simon Glassb1446442015-03-25 12:22:39 -060083 u8 spare2[3];
84};
85
Simon Glassb1446442015-03-25 12:22:39 -060086static struct usb_device_descriptor flash_device_desc = {
87 .bLength = sizeof(flash_device_desc),
88 .bDescriptorType = USB_DT_DEVICE,
89
90 .bcdUSB = __constant_cpu_to_le16(0x0200),
91
92 .bDeviceClass = 0,
93 .bDeviceSubClass = 0,
94 .bDeviceProtocol = 0,
95
96 .idVendor = __constant_cpu_to_le16(0x1234),
97 .idProduct = __constant_cpu_to_le16(0x5678),
98 .iManufacturer = STRINGID_MANUFACTURER,
99 .iProduct = STRINGID_PRODUCT,
100 .iSerialNumber = STRINGID_SERIAL,
101 .bNumConfigurations = 1,
102};
103
104static struct usb_config_descriptor flash_config0 = {
105 .bLength = sizeof(flash_config0),
106 .bDescriptorType = USB_DT_CONFIG,
107
108 /* wTotalLength is set up by usb-emul-uclass */
109 .bNumInterfaces = 1,
110 .bConfigurationValue = 0,
111 .iConfiguration = 0,
112 .bmAttributes = 1 << 7,
113 .bMaxPower = 50,
114};
115
116static struct usb_interface_descriptor flash_interface0 = {
117 .bLength = sizeof(flash_interface0),
118 .bDescriptorType = USB_DT_INTERFACE,
119
120 .bInterfaceNumber = 0,
121 .bAlternateSetting = 0,
122 .bNumEndpoints = 2,
123 .bInterfaceClass = USB_CLASS_MASS_STORAGE,
124 .bInterfaceSubClass = US_SC_UFI,
125 .bInterfaceProtocol = US_PR_BULK,
126 .iInterface = 0,
127};
128
129static struct usb_endpoint_descriptor flash_endpoint0_out = {
130 .bLength = USB_DT_ENDPOINT_SIZE,
131 .bDescriptorType = USB_DT_ENDPOINT,
132
133 .bEndpointAddress = SANDBOX_FLASH_EP_OUT,
134 .bmAttributes = USB_ENDPOINT_XFER_BULK,
135 .wMaxPacketSize = __constant_cpu_to_le16(1024),
136 .bInterval = 0,
137};
138
139static struct usb_endpoint_descriptor flash_endpoint1_in = {
140 .bLength = USB_DT_ENDPOINT_SIZE,
141 .bDescriptorType = USB_DT_ENDPOINT,
142
143 .bEndpointAddress = SANDBOX_FLASH_EP_IN | USB_ENDPOINT_DIR_MASK,
144 .bmAttributes = USB_ENDPOINT_XFER_BULK,
145 .wMaxPacketSize = __constant_cpu_to_le16(1024),
146 .bInterval = 0,
147};
148
149static void *flash_desc_list[] = {
150 &flash_device_desc,
151 &flash_config0,
152 &flash_interface0,
153 &flash_endpoint0_out,
154 &flash_endpoint1_in,
155 NULL,
156};
157
158static int sandbox_flash_control(struct udevice *dev, struct usb_device *udev,
159 unsigned long pipe, void *buff, int len,
160 struct devrequest *setup)
161{
162 struct sandbox_flash_priv *priv = dev_get_priv(dev);
163
164 if (pipe == usb_rcvctrlpipe(udev, 0)) {
165 switch (setup->request) {
166 case US_BBB_RESET:
167 priv->error = false;
168 return 0;
169 case US_BBB_GET_MAX_LUN:
170 *(char *)buff = '\0';
171 return 1;
172 default:
173 debug("request=%x\n", setup->request);
174 break;
175 }
176 }
177 debug("pipe=%lx\n", pipe);
178
179 return -EIO;
180}
181
182static void setup_fail_response(struct sandbox_flash_priv *priv)
183{
184 struct umass_bbb_csw *csw = &priv->status;
185
186 csw->dCSWSignature = CSWSIGNATURE;
187 csw->dCSWTag = priv->tag;
188 csw->dCSWDataResidue = 0;
189 csw->bCSWStatus = CSWSTATUS_FAILED;
Simon Glassb1446442015-03-25 12:22:39 -0600190}
191
192/**
193 * setup_response() - set up a response to send back to the host
194 *
195 * @priv: Sandbox flash private data
196 * @resp: Response to send, or NULL if none
197 * @size: Size of response
198 */
Simon Glass8cfdea72022-09-21 16:21:41 +0200199static void setup_response(struct sandbox_flash_priv *priv)
Simon Glassb1446442015-03-25 12:22:39 -0600200{
201 struct umass_bbb_csw *csw = &priv->status;
202
203 csw->dCSWSignature = CSWSIGNATURE;
204 csw->dCSWTag = priv->tag;
205 csw->dCSWDataResidue = 0;
206 csw->bCSWStatus = CSWSTATUS_GOOD;
Simon Glassb1446442015-03-25 12:22:39 -0600207}
208
Simon Glass657efaa2022-09-21 16:21:42 +0200209/**
210 * handle_read() - prepare for reading data from the backing file
211 *
212 * This seeks to the correct file position and sets info->buff_used to the
213 * correct size.
214 *
215 * @priv: Private information
216 * @lba: Start block to read from
217 * @transfer_length: Number of blocks to read
218 * @return 0 if OK, -EIO on failure
219 */
220static int handle_read(struct sandbox_flash_priv *priv, ulong lba,
221 ulong transfer_len)
Simon Glassb1446442015-03-25 12:22:39 -0600222{
Simon Glass4c92a4d2022-09-21 16:21:36 +0200223 struct scsi_emul_info *info = &priv->eminfo;
224
Simon Glassb1446442015-03-25 12:22:39 -0600225 debug("%s: lba=%lx, transfer_len=%lx\n", __func__, lba, transfer_len);
Simon Glass4c92a4d2022-09-21 16:21:36 +0200226 info->read_len = transfer_len;
Simon Glassb1446442015-03-25 12:22:39 -0600227 if (priv->fd != -1) {
Simon Glassd0962c92022-09-21 16:21:39 +0200228 os_lseek(priv->fd, lba * info->block_size, OS_SEEK_SET);
Simon Glass8cfdea72022-09-21 16:21:41 +0200229 info->buff_used = transfer_len * info->block_size;
Simon Glass657efaa2022-09-21 16:21:42 +0200230 return 0;
Simon Glassb1446442015-03-25 12:22:39 -0600231 }
Simon Glass657efaa2022-09-21 16:21:42 +0200232
233 return -EIO;
Simon Glassb1446442015-03-25 12:22:39 -0600234}
235
Simon Glass31d78162015-11-08 23:47:53 -0700236static int handle_ufi_command(struct sandbox_flash_plat *plat,
237 struct sandbox_flash_priv *priv, const void *buff,
Simon Glassb1446442015-03-25 12:22:39 -0600238 int len)
239{
Simon Glass4c92a4d2022-09-21 16:21:36 +0200240 struct scsi_emul_info *info = &priv->eminfo;
Simon Glassa4eff9f2017-06-14 21:28:29 -0600241 const struct scsi_cmd *req = buff;
Simon Glassb1446442015-03-25 12:22:39 -0600242
Simon Glass8cfdea72022-09-21 16:21:41 +0200243 info->buff_used = 0;
Simon Glassb1446442015-03-25 12:22:39 -0600244 switch (*req->cmd) {
245 case SCSI_INQUIRY: {
Simon Glass293c43f2022-09-21 16:21:37 +0200246 struct scsi_inquiry_resp *resp = (void *)info->buff;
Simon Glassb1446442015-03-25 12:22:39 -0600247
Simon Glass4c92a4d2022-09-21 16:21:36 +0200248 info->alloc_len = req->cmd[4];
Simon Glassb1446442015-03-25 12:22:39 -0600249 memset(resp, '\0', sizeof(*resp));
250 resp->data_format = 1;
251 resp->additional_len = 0x1f;
Simon Glass37884f02022-09-21 16:21:38 +0200252 strncpy(resp->vendor, info->vendor, sizeof(resp->vendor));
253 strncpy(resp->product, info->product, sizeof(resp->product));
Simon Glassb1446442015-03-25 12:22:39 -0600254 strncpy(resp->revision, "1.0", sizeof(resp->revision));
Simon Glass8cfdea72022-09-21 16:21:41 +0200255 info->buff_used = sizeof(*resp);
256 setup_response(priv);
Simon Glassb1446442015-03-25 12:22:39 -0600257 break;
258 }
259 case SCSI_TST_U_RDY:
Simon Glass8cfdea72022-09-21 16:21:41 +0200260 setup_response(priv);
Simon Glassb1446442015-03-25 12:22:39 -0600261 break;
262 case SCSI_RD_CAPAC: {
Simon Glass293c43f2022-09-21 16:21:37 +0200263 struct scsi_read_capacity_resp *resp = (void *)info->buff;
Simon Glassb1446442015-03-25 12:22:39 -0600264 uint blocks;
265
Simon Glass4313c3e2022-09-21 16:21:40 +0200266 if (info->file_size)
267 blocks = info->file_size / info->block_size - 1;
Simon Glassb1446442015-03-25 12:22:39 -0600268 else
269 blocks = 0;
270 resp->last_block_addr = cpu_to_be32(blocks);
Simon Glassd0962c92022-09-21 16:21:39 +0200271 resp->block_len = cpu_to_be32(info->block_size);
Simon Glass8cfdea72022-09-21 16:21:41 +0200272 info->buff_used = sizeof(*resp);
273 setup_response(priv);
Simon Glassb1446442015-03-25 12:22:39 -0600274 break;
275 }
276 case SCSI_READ10: {
277 struct scsi_read10_req *req = (void *)buff;
278
Simon Glass657efaa2022-09-21 16:21:42 +0200279 if (!handle_read(priv, be32_to_cpu(req->lba),
280 be16_to_cpu(req->xfer_len)))
281 setup_response(priv);
282 else
283 setup_fail_response(priv);
284
Simon Glassb1446442015-03-25 12:22:39 -0600285 break;
286 }
287 default:
288 debug("Command not supported: %x\n", req->cmd[0]);
289 return -EPROTONOSUPPORT;
290 }
291
Simon Glass4c92a4d2022-09-21 16:21:36 +0200292 info->phase = info->transfer_len ? SCSIPH_DATA : SCSIPH_STATUS;
Simon Glassb1446442015-03-25 12:22:39 -0600293 return 0;
294}
295
296static int sandbox_flash_bulk(struct udevice *dev, struct usb_device *udev,
297 unsigned long pipe, void *buff, int len)
298{
Simon Glassfa20e932020-12-03 16:55:20 -0700299 struct sandbox_flash_plat *plat = dev_get_plat(dev);
Simon Glassb1446442015-03-25 12:22:39 -0600300 struct sandbox_flash_priv *priv = dev_get_priv(dev);
Simon Glass4c92a4d2022-09-21 16:21:36 +0200301 struct scsi_emul_info *info = &priv->eminfo;
Simon Glassb1446442015-03-25 12:22:39 -0600302 int ep = usb_pipeendpoint(pipe);
303 struct umass_bbb_cbw *cbw = buff;
304
305 debug("%s: dev=%s, pipe=%lx, ep=%x, len=%x, phase=%d\n", __func__,
Simon Glass4c92a4d2022-09-21 16:21:36 +0200306 dev->name, pipe, ep, len, info->phase);
Simon Glassb1446442015-03-25 12:22:39 -0600307 switch (ep) {
308 case SANDBOX_FLASH_EP_OUT:
Simon Glass4c92a4d2022-09-21 16:21:36 +0200309 switch (info->phase) {
Simon Glasse6e05ad2022-09-21 16:21:35 +0200310 case SCSIPH_START:
Simon Glass4c92a4d2022-09-21 16:21:36 +0200311 info->alloc_len = 0;
312 info->read_len = 0;
Simon Glassb1446442015-03-25 12:22:39 -0600313 if (priv->error || len != UMASS_BBB_CBW_SIZE ||
314 cbw->dCBWSignature != CBWSIGNATURE)
315 goto err;
316 if ((cbw->bCBWFlags & CBWFLAGS_SBZ) ||
317 cbw->bCBWLUN != 0)
318 goto err;
319 if (cbw->bCDBLength < 1 || cbw->bCDBLength >= 0x10)
320 goto err;
Simon Glass4c92a4d2022-09-21 16:21:36 +0200321 info->transfer_len = cbw->dCBWDataTransferLength;
Simon Glassb1446442015-03-25 12:22:39 -0600322 priv->tag = cbw->dCBWTag;
Simon Glass31d78162015-11-08 23:47:53 -0700323 return handle_ufi_command(plat, priv, cbw->CBWCDB,
Simon Glassb1446442015-03-25 12:22:39 -0600324 cbw->bCDBLength);
Simon Glasse6e05ad2022-09-21 16:21:35 +0200325 case SCSIPH_DATA:
Simon Glassb1446442015-03-25 12:22:39 -0600326 debug("data out\n");
327 break;
328 default:
329 break;
330 }
331 case SANDBOX_FLASH_EP_IN:
Simon Glass4c92a4d2022-09-21 16:21:36 +0200332 switch (info->phase) {
Simon Glasse6e05ad2022-09-21 16:21:35 +0200333 case SCSIPH_DATA:
Simon Glass4c92a4d2022-09-21 16:21:36 +0200334 debug("data in, len=%x, alloc_len=%x, info->read_len=%x\n",
335 len, info->alloc_len, info->read_len);
336 if (info->read_len) {
Simon Glassb1446442015-03-25 12:22:39 -0600337 ulong bytes_read;
338
Sean Anderson03831852022-03-23 18:24:38 -0400339 if (priv->fd == -1)
340 return -EIO;
341
Simon Glassb1446442015-03-25 12:22:39 -0600342 bytes_read = os_read(priv->fd, buff, len);
343 if (bytes_read != len)
344 return -EIO;
Simon Glassd0962c92022-09-21 16:21:39 +0200345 info->read_len -= len / info->block_size;
Simon Glass4c92a4d2022-09-21 16:21:36 +0200346 if (!info->read_len)
347 info->phase = SCSIPH_STATUS;
Simon Glassb1446442015-03-25 12:22:39 -0600348 } else {
Simon Glass4c92a4d2022-09-21 16:21:36 +0200349 if (info->alloc_len && len > info->alloc_len)
350 len = info->alloc_len;
Simon Glass293c43f2022-09-21 16:21:37 +0200351 if (len > SANDBOX_FLASH_BUF_SIZE)
352 len = SANDBOX_FLASH_BUF_SIZE;
353 memcpy(buff, info->buff, len);
Simon Glass4c92a4d2022-09-21 16:21:36 +0200354 info->phase = SCSIPH_STATUS;
Simon Glassb1446442015-03-25 12:22:39 -0600355 }
356 return len;
Simon Glasse6e05ad2022-09-21 16:21:35 +0200357 case SCSIPH_STATUS:
Simon Glassb1446442015-03-25 12:22:39 -0600358 debug("status in, len=%x\n", len);
359 if (len > sizeof(priv->status))
360 len = sizeof(priv->status);
361 memcpy(buff, &priv->status, len);
Simon Glass4c92a4d2022-09-21 16:21:36 +0200362 info->phase = SCSIPH_START;
Simon Glassb1446442015-03-25 12:22:39 -0600363 return len;
364 default:
365 break;
366 }
367 }
368err:
369 priv->error = true;
370 debug("%s: Detected transfer error\n", __func__);
371 return 0;
372}
373
Simon Glassaad29ae2020-12-03 16:55:21 -0700374static int sandbox_flash_of_to_plat(struct udevice *dev)
Simon Glassb1446442015-03-25 12:22:39 -0600375{
Simon Glassfa20e932020-12-03 16:55:20 -0700376 struct sandbox_flash_plat *plat = dev_get_plat(dev);
Simon Glassb1446442015-03-25 12:22:39 -0600377
Simon Glass82bf00e2017-05-18 20:09:39 -0600378 plat->pathname = dev_read_string(dev, "sandbox,filepath");
Simon Glassb1446442015-03-25 12:22:39 -0600379
380 return 0;
381}
382
383static int sandbox_flash_bind(struct udevice *dev)
384{
Simon Glassfa20e932020-12-03 16:55:20 -0700385 struct sandbox_flash_plat *plat = dev_get_plat(dev);
Simon Glass31d78162015-11-08 23:47:53 -0700386 struct usb_string *fs;
387
388 fs = plat->flash_strings;
389 fs[0].id = STRINGID_MANUFACTURER;
390 fs[0].s = "sandbox";
391 fs[1].id = STRINGID_PRODUCT;
392 fs[1].s = "flash";
393 fs[2].id = STRINGID_SERIAL;
394 fs[2].s = dev->name;
395
Bin Mengd502cf12017-10-01 06:19:36 -0700396 return usb_emul_setup_device(dev, plat->flash_strings, flash_desc_list);
Simon Glassb1446442015-03-25 12:22:39 -0600397}
398
399static int sandbox_flash_probe(struct udevice *dev)
400{
Simon Glassfa20e932020-12-03 16:55:20 -0700401 struct sandbox_flash_plat *plat = dev_get_plat(dev);
Simon Glassb1446442015-03-25 12:22:39 -0600402 struct sandbox_flash_priv *priv = dev_get_priv(dev);
Simon Glass293c43f2022-09-21 16:21:37 +0200403 struct scsi_emul_info *info = &priv->eminfo;
404 int ret;
Simon Glassb1446442015-03-25 12:22:39 -0600405
406 priv->fd = os_open(plat->pathname, OS_O_RDONLY);
Simon Glass293c43f2022-09-21 16:21:37 +0200407 if (priv->fd != -1) {
Simon Glass4313c3e2022-09-21 16:21:40 +0200408 ret = os_get_filesize(plat->pathname, &info->file_size);
Simon Glass293c43f2022-09-21 16:21:37 +0200409 if (ret)
410 return log_msg_ret("sz", ret);
411 }
412 info->buff = malloc(SANDBOX_FLASH_BUF_SIZE);
413 if (!info->buff)
414 return log_ret(-ENOMEM);
Simon Glass37884f02022-09-21 16:21:38 +0200415 info->vendor = plat->flash_strings[STRINGID_MANUFACTURER - 1].s;
416 info->product = plat->flash_strings[STRINGID_PRODUCT - 1].s;
Simon Glassd0962c92022-09-21 16:21:39 +0200417 info->block_size = SANDBOX_FLASH_BLOCK_LEN;
Simon Glass293c43f2022-09-21 16:21:37 +0200418
419 return 0;
420}
421
422static int sandbox_flash_remove(struct udevice *dev)
423{
424 struct sandbox_flash_priv *priv = dev_get_priv(dev);
425 struct scsi_emul_info *info = &priv->eminfo;
426
427 free(info->buff);
Simon Glassb1446442015-03-25 12:22:39 -0600428
429 return 0;
430}
431
432static const struct dm_usb_ops sandbox_usb_flash_ops = {
433 .control = sandbox_flash_control,
434 .bulk = sandbox_flash_bulk,
435};
436
437static const struct udevice_id sandbox_usb_flash_ids[] = {
438 { .compatible = "sandbox,usb-flash" },
439 { }
440};
441
442U_BOOT_DRIVER(usb_sandbox_flash) = {
443 .name = "usb_sandbox_flash",
444 .id = UCLASS_USB_EMUL,
445 .of_match = sandbox_usb_flash_ids,
446 .bind = sandbox_flash_bind,
447 .probe = sandbox_flash_probe,
Simon Glass293c43f2022-09-21 16:21:37 +0200448 .remove = sandbox_flash_remove,
Simon Glassaad29ae2020-12-03 16:55:21 -0700449 .of_to_plat = sandbox_flash_of_to_plat,
Simon Glassb1446442015-03-25 12:22:39 -0600450 .ops = &sandbox_usb_flash_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700451 .priv_auto = sizeof(struct sandbox_flash_priv),
Simon Glass71fa5b42020-12-03 16:55:18 -0700452 .plat_auto = sizeof(struct sandbox_flash_plat),
Simon Glassb1446442015-03-25 12:22:39 -0600453};