blob: 2589c708d883ae26e898da92e911cbe6c638fa42 [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 Glass4d3b00d2022-09-21 16:21:45 +0200183static int handle_ufi_command(struct sandbox_flash_priv *priv, const void *buff,
Simon Glassb1446442015-03-25 12:22:39 -0600184 int len)
185{
Simon Glass4c92a4d2022-09-21 16:21:36 +0200186 struct scsi_emul_info *info = &priv->eminfo;
Simon Glassa4eff9f2017-06-14 21:28:29 -0600187 const struct scsi_cmd *req = buff;
Simon Glass4d3b00d2022-09-21 16:21:45 +0200188 int ret;
Simon Glassb1446442015-03-25 12:22:39 -0600189
Simon Glass4d3b00d2022-09-21 16:21:45 +0200190 ret = sb_scsi_emul_command(info, req, len);
191 if (!ret) {
Simon Glass8cfdea72022-09-21 16:21:41 +0200192 setup_response(priv);
Simon Glass4d3b00d2022-09-21 16:21:45 +0200193 } else if (ret == SCSI_EMUL_DO_READ && priv->fd != -1) {
194 os_lseek(priv->fd, info->seek_block * info->block_size,
195 OS_SEEK_SET);
Simon Glass8cfdea72022-09-21 16:21:41 +0200196 setup_response(priv);
Simon Glass4d3b00d2022-09-21 16:21:45 +0200197 } else {
198 setup_fail_response(priv);
Simon Glassb1446442015-03-25 12:22:39 -0600199 }
200
Simon Glassb1446442015-03-25 12:22:39 -0600201 return 0;
202}
203
204static int sandbox_flash_bulk(struct udevice *dev, struct usb_device *udev,
205 unsigned long pipe, void *buff, int len)
206{
207 struct sandbox_flash_priv *priv = dev_get_priv(dev);
Simon Glass4c92a4d2022-09-21 16:21:36 +0200208 struct scsi_emul_info *info = &priv->eminfo;
Simon Glassb1446442015-03-25 12:22:39 -0600209 int ep = usb_pipeendpoint(pipe);
210 struct umass_bbb_cbw *cbw = buff;
211
212 debug("%s: dev=%s, pipe=%lx, ep=%x, len=%x, phase=%d\n", __func__,
Simon Glass4c92a4d2022-09-21 16:21:36 +0200213 dev->name, pipe, ep, len, info->phase);
Simon Glassb1446442015-03-25 12:22:39 -0600214 switch (ep) {
215 case SANDBOX_FLASH_EP_OUT:
Simon Glass4c92a4d2022-09-21 16:21:36 +0200216 switch (info->phase) {
Simon Glasse6e05ad2022-09-21 16:21:35 +0200217 case SCSIPH_START:
Simon Glass4c92a4d2022-09-21 16:21:36 +0200218 info->alloc_len = 0;
219 info->read_len = 0;
Simon Glassb1446442015-03-25 12:22:39 -0600220 if (priv->error || len != UMASS_BBB_CBW_SIZE ||
221 cbw->dCBWSignature != CBWSIGNATURE)
222 goto err;
223 if ((cbw->bCBWFlags & CBWFLAGS_SBZ) ||
224 cbw->bCBWLUN != 0)
225 goto err;
226 if (cbw->bCDBLength < 1 || cbw->bCDBLength >= 0x10)
227 goto err;
Simon Glass4c92a4d2022-09-21 16:21:36 +0200228 info->transfer_len = cbw->dCBWDataTransferLength;
Simon Glassb1446442015-03-25 12:22:39 -0600229 priv->tag = cbw->dCBWTag;
Simon Glass4d3b00d2022-09-21 16:21:45 +0200230 return handle_ufi_command(priv, cbw->CBWCDB,
Simon Glassb1446442015-03-25 12:22:39 -0600231 cbw->bCDBLength);
Simon Glasse6e05ad2022-09-21 16:21:35 +0200232 case SCSIPH_DATA:
Simon Glassb1446442015-03-25 12:22:39 -0600233 debug("data out\n");
234 break;
235 default:
236 break;
237 }
238 case SANDBOX_FLASH_EP_IN:
Simon Glass4c92a4d2022-09-21 16:21:36 +0200239 switch (info->phase) {
Simon Glasse6e05ad2022-09-21 16:21:35 +0200240 case SCSIPH_DATA:
Simon Glass4c92a4d2022-09-21 16:21:36 +0200241 debug("data in, len=%x, alloc_len=%x, info->read_len=%x\n",
242 len, info->alloc_len, info->read_len);
243 if (info->read_len) {
Simon Glassb1446442015-03-25 12:22:39 -0600244 ulong bytes_read;
245
Sean Anderson03831852022-03-23 18:24:38 -0400246 if (priv->fd == -1)
247 return -EIO;
248
Simon Glassb1446442015-03-25 12:22:39 -0600249 bytes_read = os_read(priv->fd, buff, len);
250 if (bytes_read != len)
251 return -EIO;
Simon Glassd0962c92022-09-21 16:21:39 +0200252 info->read_len -= len / info->block_size;
Simon Glass4c92a4d2022-09-21 16:21:36 +0200253 if (!info->read_len)
254 info->phase = SCSIPH_STATUS;
Simon Glassb1446442015-03-25 12:22:39 -0600255 } else {
Simon Glass4c92a4d2022-09-21 16:21:36 +0200256 if (info->alloc_len && len > info->alloc_len)
257 len = info->alloc_len;
Simon Glass293c43f2022-09-21 16:21:37 +0200258 if (len > SANDBOX_FLASH_BUF_SIZE)
259 len = SANDBOX_FLASH_BUF_SIZE;
260 memcpy(buff, info->buff, len);
Simon Glass4c92a4d2022-09-21 16:21:36 +0200261 info->phase = SCSIPH_STATUS;
Simon Glassb1446442015-03-25 12:22:39 -0600262 }
263 return len;
Simon Glasse6e05ad2022-09-21 16:21:35 +0200264 case SCSIPH_STATUS:
Simon Glassb1446442015-03-25 12:22:39 -0600265 debug("status in, len=%x\n", len);
266 if (len > sizeof(priv->status))
267 len = sizeof(priv->status);
268 memcpy(buff, &priv->status, len);
Simon Glass4c92a4d2022-09-21 16:21:36 +0200269 info->phase = SCSIPH_START;
Simon Glassb1446442015-03-25 12:22:39 -0600270 return len;
271 default:
272 break;
273 }
274 }
275err:
276 priv->error = true;
277 debug("%s: Detected transfer error\n", __func__);
278 return 0;
279}
280
Simon Glassaad29ae2020-12-03 16:55:21 -0700281static int sandbox_flash_of_to_plat(struct udevice *dev)
Simon Glassb1446442015-03-25 12:22:39 -0600282{
Simon Glassfa20e932020-12-03 16:55:20 -0700283 struct sandbox_flash_plat *plat = dev_get_plat(dev);
Simon Glassb1446442015-03-25 12:22:39 -0600284
Simon Glass82bf00e2017-05-18 20:09:39 -0600285 plat->pathname = dev_read_string(dev, "sandbox,filepath");
Simon Glassb1446442015-03-25 12:22:39 -0600286
287 return 0;
288}
289
290static int sandbox_flash_bind(struct udevice *dev)
291{
Simon Glassfa20e932020-12-03 16:55:20 -0700292 struct sandbox_flash_plat *plat = dev_get_plat(dev);
Simon Glass31d78162015-11-08 23:47:53 -0700293 struct usb_string *fs;
294
295 fs = plat->flash_strings;
296 fs[0].id = STRINGID_MANUFACTURER;
297 fs[0].s = "sandbox";
298 fs[1].id = STRINGID_PRODUCT;
299 fs[1].s = "flash";
300 fs[2].id = STRINGID_SERIAL;
301 fs[2].s = dev->name;
302
Bin Mengd502cf12017-10-01 06:19:36 -0700303 return usb_emul_setup_device(dev, plat->flash_strings, flash_desc_list);
Simon Glassb1446442015-03-25 12:22:39 -0600304}
305
306static int sandbox_flash_probe(struct udevice *dev)
307{
Simon Glassfa20e932020-12-03 16:55:20 -0700308 struct sandbox_flash_plat *plat = dev_get_plat(dev);
Simon Glassb1446442015-03-25 12:22:39 -0600309 struct sandbox_flash_priv *priv = dev_get_priv(dev);
Simon Glass293c43f2022-09-21 16:21:37 +0200310 struct scsi_emul_info *info = &priv->eminfo;
311 int ret;
Simon Glassb1446442015-03-25 12:22:39 -0600312
313 priv->fd = os_open(plat->pathname, OS_O_RDONLY);
Simon Glass293c43f2022-09-21 16:21:37 +0200314 if (priv->fd != -1) {
Simon Glass4313c3e2022-09-21 16:21:40 +0200315 ret = os_get_filesize(plat->pathname, &info->file_size);
Simon Glass293c43f2022-09-21 16:21:37 +0200316 if (ret)
317 return log_msg_ret("sz", ret);
318 }
319 info->buff = malloc(SANDBOX_FLASH_BUF_SIZE);
320 if (!info->buff)
321 return log_ret(-ENOMEM);
Simon Glass37884f02022-09-21 16:21:38 +0200322 info->vendor = plat->flash_strings[STRINGID_MANUFACTURER - 1].s;
323 info->product = plat->flash_strings[STRINGID_PRODUCT - 1].s;
Simon Glassd0962c92022-09-21 16:21:39 +0200324 info->block_size = SANDBOX_FLASH_BLOCK_LEN;
Simon Glass293c43f2022-09-21 16:21:37 +0200325
326 return 0;
327}
328
329static int sandbox_flash_remove(struct udevice *dev)
330{
331 struct sandbox_flash_priv *priv = dev_get_priv(dev);
332 struct scsi_emul_info *info = &priv->eminfo;
333
334 free(info->buff);
Simon Glassb1446442015-03-25 12:22:39 -0600335
336 return 0;
337}
338
339static const struct dm_usb_ops sandbox_usb_flash_ops = {
340 .control = sandbox_flash_control,
341 .bulk = sandbox_flash_bulk,
342};
343
344static const struct udevice_id sandbox_usb_flash_ids[] = {
345 { .compatible = "sandbox,usb-flash" },
346 { }
347};
348
349U_BOOT_DRIVER(usb_sandbox_flash) = {
350 .name = "usb_sandbox_flash",
351 .id = UCLASS_USB_EMUL,
352 .of_match = sandbox_usb_flash_ids,
353 .bind = sandbox_flash_bind,
354 .probe = sandbox_flash_probe,
Simon Glass293c43f2022-09-21 16:21:37 +0200355 .remove = sandbox_flash_remove,
Simon Glassaad29ae2020-12-03 16:55:21 -0700356 .of_to_plat = sandbox_flash_of_to_plat,
Simon Glassb1446442015-03-25 12:22:39 -0600357 .ops = &sandbox_usb_flash_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700358 .priv_auto = sizeof(struct sandbox_flash_priv),
Simon Glass71fa5b42020-12-03 16:55:18 -0700359 .plat_auto = sizeof(struct sandbox_flash_plat),
Simon Glassb1446442015-03-25 12:22:39 -0600360};