blob: 2059fc7fe426312f688b2997a689073636cb5612 [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
Simon Glassb1446442015-03-25 12:22:39 -060060static struct usb_device_descriptor flash_device_desc = {
61 .bLength = sizeof(flash_device_desc),
62 .bDescriptorType = USB_DT_DEVICE,
63
64 .bcdUSB = __constant_cpu_to_le16(0x0200),
65
66 .bDeviceClass = 0,
67 .bDeviceSubClass = 0,
68 .bDeviceProtocol = 0,
69
70 .idVendor = __constant_cpu_to_le16(0x1234),
71 .idProduct = __constant_cpu_to_le16(0x5678),
72 .iManufacturer = STRINGID_MANUFACTURER,
73 .iProduct = STRINGID_PRODUCT,
74 .iSerialNumber = STRINGID_SERIAL,
75 .bNumConfigurations = 1,
76};
77
78static struct usb_config_descriptor flash_config0 = {
79 .bLength = sizeof(flash_config0),
80 .bDescriptorType = USB_DT_CONFIG,
81
82 /* wTotalLength is set up by usb-emul-uclass */
83 .bNumInterfaces = 1,
84 .bConfigurationValue = 0,
85 .iConfiguration = 0,
86 .bmAttributes = 1 << 7,
87 .bMaxPower = 50,
88};
89
90static struct usb_interface_descriptor flash_interface0 = {
91 .bLength = sizeof(flash_interface0),
92 .bDescriptorType = USB_DT_INTERFACE,
93
94 .bInterfaceNumber = 0,
95 .bAlternateSetting = 0,
96 .bNumEndpoints = 2,
97 .bInterfaceClass = USB_CLASS_MASS_STORAGE,
98 .bInterfaceSubClass = US_SC_UFI,
99 .bInterfaceProtocol = US_PR_BULK,
100 .iInterface = 0,
101};
102
103static struct usb_endpoint_descriptor flash_endpoint0_out = {
104 .bLength = USB_DT_ENDPOINT_SIZE,
105 .bDescriptorType = USB_DT_ENDPOINT,
106
107 .bEndpointAddress = SANDBOX_FLASH_EP_OUT,
108 .bmAttributes = USB_ENDPOINT_XFER_BULK,
109 .wMaxPacketSize = __constant_cpu_to_le16(1024),
110 .bInterval = 0,
111};
112
113static struct usb_endpoint_descriptor flash_endpoint1_in = {
114 .bLength = USB_DT_ENDPOINT_SIZE,
115 .bDescriptorType = USB_DT_ENDPOINT,
116
117 .bEndpointAddress = SANDBOX_FLASH_EP_IN | USB_ENDPOINT_DIR_MASK,
118 .bmAttributes = USB_ENDPOINT_XFER_BULK,
119 .wMaxPacketSize = __constant_cpu_to_le16(1024),
120 .bInterval = 0,
121};
122
123static void *flash_desc_list[] = {
124 &flash_device_desc,
125 &flash_config0,
126 &flash_interface0,
127 &flash_endpoint0_out,
128 &flash_endpoint1_in,
129 NULL,
130};
131
132static int sandbox_flash_control(struct udevice *dev, struct usb_device *udev,
133 unsigned long pipe, void *buff, int len,
134 struct devrequest *setup)
135{
136 struct sandbox_flash_priv *priv = dev_get_priv(dev);
137
138 if (pipe == usb_rcvctrlpipe(udev, 0)) {
139 switch (setup->request) {
140 case US_BBB_RESET:
141 priv->error = false;
142 return 0;
143 case US_BBB_GET_MAX_LUN:
144 *(char *)buff = '\0';
145 return 1;
146 default:
147 debug("request=%x\n", setup->request);
148 break;
149 }
150 }
151 debug("pipe=%lx\n", pipe);
152
153 return -EIO;
154}
155
156static void setup_fail_response(struct sandbox_flash_priv *priv)
157{
158 struct umass_bbb_csw *csw = &priv->status;
159
160 csw->dCSWSignature = CSWSIGNATURE;
161 csw->dCSWTag = priv->tag;
162 csw->dCSWDataResidue = 0;
163 csw->bCSWStatus = CSWSTATUS_FAILED;
Simon Glassb1446442015-03-25 12:22:39 -0600164}
165
166/**
167 * setup_response() - set up a response to send back to the host
168 *
169 * @priv: Sandbox flash private data
170 * @resp: Response to send, or NULL if none
171 * @size: Size of response
172 */
Simon Glass8cfdea72022-09-21 16:21:41 +0200173static void setup_response(struct sandbox_flash_priv *priv)
Simon Glassb1446442015-03-25 12:22:39 -0600174{
175 struct umass_bbb_csw *csw = &priv->status;
176
177 csw->dCSWSignature = CSWSIGNATURE;
178 csw->dCSWTag = priv->tag;
179 csw->dCSWDataResidue = 0;
180 csw->bCSWStatus = CSWSTATUS_GOOD;
Simon Glassb1446442015-03-25 12:22:39 -0600181}
182
Simon Glass657efaa2022-09-21 16:21:42 +0200183/**
184 * handle_read() - prepare for reading data from the backing file
185 *
186 * This seeks to the correct file position and sets info->buff_used to the
187 * correct size.
188 *
189 * @priv: Private information
190 * @lba: Start block to read from
191 * @transfer_length: Number of blocks to read
192 * @return 0 if OK, -EIO on failure
193 */
194static int handle_read(struct sandbox_flash_priv *priv, ulong lba,
195 ulong transfer_len)
Simon Glassb1446442015-03-25 12:22:39 -0600196{
Simon Glass4c92a4d2022-09-21 16:21:36 +0200197 struct scsi_emul_info *info = &priv->eminfo;
198
Simon Glassb1446442015-03-25 12:22:39 -0600199 debug("%s: lba=%lx, transfer_len=%lx\n", __func__, lba, transfer_len);
Simon Glass4c92a4d2022-09-21 16:21:36 +0200200 info->read_len = transfer_len;
Simon Glassb1446442015-03-25 12:22:39 -0600201 if (priv->fd != -1) {
Simon Glassd0962c92022-09-21 16:21:39 +0200202 os_lseek(priv->fd, lba * info->block_size, OS_SEEK_SET);
Simon Glass8cfdea72022-09-21 16:21:41 +0200203 info->buff_used = transfer_len * info->block_size;
Simon Glass657efaa2022-09-21 16:21:42 +0200204 return 0;
Simon Glassb1446442015-03-25 12:22:39 -0600205 }
Simon Glass657efaa2022-09-21 16:21:42 +0200206
207 return -EIO;
Simon Glassb1446442015-03-25 12:22:39 -0600208}
209
Simon Glass31d78162015-11-08 23:47:53 -0700210static int handle_ufi_command(struct sandbox_flash_plat *plat,
211 struct sandbox_flash_priv *priv, const void *buff,
Simon Glassb1446442015-03-25 12:22:39 -0600212 int len)
213{
Simon Glass4c92a4d2022-09-21 16:21:36 +0200214 struct scsi_emul_info *info = &priv->eminfo;
Simon Glassa4eff9f2017-06-14 21:28:29 -0600215 const struct scsi_cmd *req = buff;
Simon Glassb1446442015-03-25 12:22:39 -0600216
Simon Glass8cfdea72022-09-21 16:21:41 +0200217 info->buff_used = 0;
Simon Glassb1446442015-03-25 12:22:39 -0600218 switch (*req->cmd) {
219 case SCSI_INQUIRY: {
Simon Glass293c43f2022-09-21 16:21:37 +0200220 struct scsi_inquiry_resp *resp = (void *)info->buff;
Simon Glassb1446442015-03-25 12:22:39 -0600221
Simon Glass4c92a4d2022-09-21 16:21:36 +0200222 info->alloc_len = req->cmd[4];
Simon Glassb1446442015-03-25 12:22:39 -0600223 memset(resp, '\0', sizeof(*resp));
224 resp->data_format = 1;
225 resp->additional_len = 0x1f;
Simon Glass37884f02022-09-21 16:21:38 +0200226 strncpy(resp->vendor, info->vendor, sizeof(resp->vendor));
227 strncpy(resp->product, info->product, sizeof(resp->product));
Simon Glassb1446442015-03-25 12:22:39 -0600228 strncpy(resp->revision, "1.0", sizeof(resp->revision));
Simon Glass8cfdea72022-09-21 16:21:41 +0200229 info->buff_used = sizeof(*resp);
230 setup_response(priv);
Simon Glassb1446442015-03-25 12:22:39 -0600231 break;
232 }
233 case SCSI_TST_U_RDY:
Simon Glass8cfdea72022-09-21 16:21:41 +0200234 setup_response(priv);
Simon Glassb1446442015-03-25 12:22:39 -0600235 break;
236 case SCSI_RD_CAPAC: {
Simon Glass293c43f2022-09-21 16:21:37 +0200237 struct scsi_read_capacity_resp *resp = (void *)info->buff;
Simon Glassb1446442015-03-25 12:22:39 -0600238 uint blocks;
239
Simon Glass4313c3e2022-09-21 16:21:40 +0200240 if (info->file_size)
241 blocks = info->file_size / info->block_size - 1;
Simon Glassb1446442015-03-25 12:22:39 -0600242 else
243 blocks = 0;
244 resp->last_block_addr = cpu_to_be32(blocks);
Simon Glassd0962c92022-09-21 16:21:39 +0200245 resp->block_len = cpu_to_be32(info->block_size);
Simon Glass8cfdea72022-09-21 16:21:41 +0200246 info->buff_used = sizeof(*resp);
247 setup_response(priv);
Simon Glassb1446442015-03-25 12:22:39 -0600248 break;
249 }
250 case SCSI_READ10: {
251 struct scsi_read10_req *req = (void *)buff;
252
Simon Glass657efaa2022-09-21 16:21:42 +0200253 if (!handle_read(priv, be32_to_cpu(req->lba),
254 be16_to_cpu(req->xfer_len)))
255 setup_response(priv);
256 else
257 setup_fail_response(priv);
258
Simon Glassb1446442015-03-25 12:22:39 -0600259 break;
260 }
261 default:
262 debug("Command not supported: %x\n", req->cmd[0]);
263 return -EPROTONOSUPPORT;
264 }
265
Simon Glass4c92a4d2022-09-21 16:21:36 +0200266 info->phase = info->transfer_len ? SCSIPH_DATA : SCSIPH_STATUS;
Simon Glassb1446442015-03-25 12:22:39 -0600267 return 0;
268}
269
270static int sandbox_flash_bulk(struct udevice *dev, struct usb_device *udev,
271 unsigned long pipe, void *buff, int len)
272{
Simon Glassfa20e932020-12-03 16:55:20 -0700273 struct sandbox_flash_plat *plat = dev_get_plat(dev);
Simon Glassb1446442015-03-25 12:22:39 -0600274 struct sandbox_flash_priv *priv = dev_get_priv(dev);
Simon Glass4c92a4d2022-09-21 16:21:36 +0200275 struct scsi_emul_info *info = &priv->eminfo;
Simon Glassb1446442015-03-25 12:22:39 -0600276 int ep = usb_pipeendpoint(pipe);
277 struct umass_bbb_cbw *cbw = buff;
278
279 debug("%s: dev=%s, pipe=%lx, ep=%x, len=%x, phase=%d\n", __func__,
Simon Glass4c92a4d2022-09-21 16:21:36 +0200280 dev->name, pipe, ep, len, info->phase);
Simon Glassb1446442015-03-25 12:22:39 -0600281 switch (ep) {
282 case SANDBOX_FLASH_EP_OUT:
Simon Glass4c92a4d2022-09-21 16:21:36 +0200283 switch (info->phase) {
Simon Glasse6e05ad2022-09-21 16:21:35 +0200284 case SCSIPH_START:
Simon Glass4c92a4d2022-09-21 16:21:36 +0200285 info->alloc_len = 0;
286 info->read_len = 0;
Simon Glassb1446442015-03-25 12:22:39 -0600287 if (priv->error || len != UMASS_BBB_CBW_SIZE ||
288 cbw->dCBWSignature != CBWSIGNATURE)
289 goto err;
290 if ((cbw->bCBWFlags & CBWFLAGS_SBZ) ||
291 cbw->bCBWLUN != 0)
292 goto err;
293 if (cbw->bCDBLength < 1 || cbw->bCDBLength >= 0x10)
294 goto err;
Simon Glass4c92a4d2022-09-21 16:21:36 +0200295 info->transfer_len = cbw->dCBWDataTransferLength;
Simon Glassb1446442015-03-25 12:22:39 -0600296 priv->tag = cbw->dCBWTag;
Simon Glass31d78162015-11-08 23:47:53 -0700297 return handle_ufi_command(plat, priv, cbw->CBWCDB,
Simon Glassb1446442015-03-25 12:22:39 -0600298 cbw->bCDBLength);
Simon Glasse6e05ad2022-09-21 16:21:35 +0200299 case SCSIPH_DATA:
Simon Glassb1446442015-03-25 12:22:39 -0600300 debug("data out\n");
301 break;
302 default:
303 break;
304 }
305 case SANDBOX_FLASH_EP_IN:
Simon Glass4c92a4d2022-09-21 16:21:36 +0200306 switch (info->phase) {
Simon Glasse6e05ad2022-09-21 16:21:35 +0200307 case SCSIPH_DATA:
Simon Glass4c92a4d2022-09-21 16:21:36 +0200308 debug("data in, len=%x, alloc_len=%x, info->read_len=%x\n",
309 len, info->alloc_len, info->read_len);
310 if (info->read_len) {
Simon Glassb1446442015-03-25 12:22:39 -0600311 ulong bytes_read;
312
Sean Anderson03831852022-03-23 18:24:38 -0400313 if (priv->fd == -1)
314 return -EIO;
315
Simon Glassb1446442015-03-25 12:22:39 -0600316 bytes_read = os_read(priv->fd, buff, len);
317 if (bytes_read != len)
318 return -EIO;
Simon Glassd0962c92022-09-21 16:21:39 +0200319 info->read_len -= len / info->block_size;
Simon Glass4c92a4d2022-09-21 16:21:36 +0200320 if (!info->read_len)
321 info->phase = SCSIPH_STATUS;
Simon Glassb1446442015-03-25 12:22:39 -0600322 } else {
Simon Glass4c92a4d2022-09-21 16:21:36 +0200323 if (info->alloc_len && len > info->alloc_len)
324 len = info->alloc_len;
Simon Glass293c43f2022-09-21 16:21:37 +0200325 if (len > SANDBOX_FLASH_BUF_SIZE)
326 len = SANDBOX_FLASH_BUF_SIZE;
327 memcpy(buff, info->buff, len);
Simon Glass4c92a4d2022-09-21 16:21:36 +0200328 info->phase = SCSIPH_STATUS;
Simon Glassb1446442015-03-25 12:22:39 -0600329 }
330 return len;
Simon Glasse6e05ad2022-09-21 16:21:35 +0200331 case SCSIPH_STATUS:
Simon Glassb1446442015-03-25 12:22:39 -0600332 debug("status in, len=%x\n", len);
333 if (len > sizeof(priv->status))
334 len = sizeof(priv->status);
335 memcpy(buff, &priv->status, len);
Simon Glass4c92a4d2022-09-21 16:21:36 +0200336 info->phase = SCSIPH_START;
Simon Glassb1446442015-03-25 12:22:39 -0600337 return len;
338 default:
339 break;
340 }
341 }
342err:
343 priv->error = true;
344 debug("%s: Detected transfer error\n", __func__);
345 return 0;
346}
347
Simon Glassaad29ae2020-12-03 16:55:21 -0700348static int sandbox_flash_of_to_plat(struct udevice *dev)
Simon Glassb1446442015-03-25 12:22:39 -0600349{
Simon Glassfa20e932020-12-03 16:55:20 -0700350 struct sandbox_flash_plat *plat = dev_get_plat(dev);
Simon Glassb1446442015-03-25 12:22:39 -0600351
Simon Glass82bf00e2017-05-18 20:09:39 -0600352 plat->pathname = dev_read_string(dev, "sandbox,filepath");
Simon Glassb1446442015-03-25 12:22:39 -0600353
354 return 0;
355}
356
357static int sandbox_flash_bind(struct udevice *dev)
358{
Simon Glassfa20e932020-12-03 16:55:20 -0700359 struct sandbox_flash_plat *plat = dev_get_plat(dev);
Simon Glass31d78162015-11-08 23:47:53 -0700360 struct usb_string *fs;
361
362 fs = plat->flash_strings;
363 fs[0].id = STRINGID_MANUFACTURER;
364 fs[0].s = "sandbox";
365 fs[1].id = STRINGID_PRODUCT;
366 fs[1].s = "flash";
367 fs[2].id = STRINGID_SERIAL;
368 fs[2].s = dev->name;
369
Bin Mengd502cf12017-10-01 06:19:36 -0700370 return usb_emul_setup_device(dev, plat->flash_strings, flash_desc_list);
Simon Glassb1446442015-03-25 12:22:39 -0600371}
372
373static int sandbox_flash_probe(struct udevice *dev)
374{
Simon Glassfa20e932020-12-03 16:55:20 -0700375 struct sandbox_flash_plat *plat = dev_get_plat(dev);
Simon Glassb1446442015-03-25 12:22:39 -0600376 struct sandbox_flash_priv *priv = dev_get_priv(dev);
Simon Glass293c43f2022-09-21 16:21:37 +0200377 struct scsi_emul_info *info = &priv->eminfo;
378 int ret;
Simon Glassb1446442015-03-25 12:22:39 -0600379
380 priv->fd = os_open(plat->pathname, OS_O_RDONLY);
Simon Glass293c43f2022-09-21 16:21:37 +0200381 if (priv->fd != -1) {
Simon Glass4313c3e2022-09-21 16:21:40 +0200382 ret = os_get_filesize(plat->pathname, &info->file_size);
Simon Glass293c43f2022-09-21 16:21:37 +0200383 if (ret)
384 return log_msg_ret("sz", ret);
385 }
386 info->buff = malloc(SANDBOX_FLASH_BUF_SIZE);
387 if (!info->buff)
388 return log_ret(-ENOMEM);
Simon Glass37884f02022-09-21 16:21:38 +0200389 info->vendor = plat->flash_strings[STRINGID_MANUFACTURER - 1].s;
390 info->product = plat->flash_strings[STRINGID_PRODUCT - 1].s;
Simon Glassd0962c92022-09-21 16:21:39 +0200391 info->block_size = SANDBOX_FLASH_BLOCK_LEN;
Simon Glass293c43f2022-09-21 16:21:37 +0200392
393 return 0;
394}
395
396static int sandbox_flash_remove(struct udevice *dev)
397{
398 struct sandbox_flash_priv *priv = dev_get_priv(dev);
399 struct scsi_emul_info *info = &priv->eminfo;
400
401 free(info->buff);
Simon Glassb1446442015-03-25 12:22:39 -0600402
403 return 0;
404}
405
406static const struct dm_usb_ops sandbox_usb_flash_ops = {
407 .control = sandbox_flash_control,
408 .bulk = sandbox_flash_bulk,
409};
410
411static const struct udevice_id sandbox_usb_flash_ids[] = {
412 { .compatible = "sandbox,usb-flash" },
413 { }
414};
415
416U_BOOT_DRIVER(usb_sandbox_flash) = {
417 .name = "usb_sandbox_flash",
418 .id = UCLASS_USB_EMUL,
419 .of_match = sandbox_usb_flash_ids,
420 .bind = sandbox_flash_bind,
421 .probe = sandbox_flash_probe,
Simon Glass293c43f2022-09-21 16:21:37 +0200422 .remove = sandbox_flash_remove,
Simon Glassaad29ae2020-12-03 16:55:21 -0700423 .of_to_plat = sandbox_flash_of_to_plat,
Simon Glassb1446442015-03-25 12:22:39 -0600424 .ops = &sandbox_usb_flash_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700425 .priv_auto = sizeof(struct sandbox_flash_priv),
Simon Glass71fa5b42020-12-03 16:55:18 -0700426 .plat_auto = sizeof(struct sandbox_flash_plat),
Simon Glassb1446442015-03-25 12:22:39 -0600427};