blob: e7abc0d5c9f3cb2e693532a1ec249866543a22e6 [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 Glassb1446442015-03-25 12:22:39 -060010#include <os.h>
11#include <scsi.h>
Simon Glass4c92a4d2022-09-21 16:21:36 +020012#include <scsi_emul.h>
Simon Glassb1446442015-03-25 12:22:39 -060013#include <usb.h>
14
Simon Glassb1446442015-03-25 12:22:39 -060015/*
16 * This driver emulates a flash stick using the UFI command specification and
17 * the BBB (bulk/bulk/bulk) protocol. It supports only a single logical unit
18 * number (LUN 0).
19 */
20
21enum {
22 SANDBOX_FLASH_EP_OUT = 1, /* endpoints */
23 SANDBOX_FLASH_EP_IN = 2,
24 SANDBOX_FLASH_BLOCK_LEN = 512,
25};
26
Simon Glass31d78162015-11-08 23:47:53 -070027enum {
28 STRINGID_MANUFACTURER = 1,
29 STRINGID_PRODUCT,
30 STRINGID_SERIAL,
31
32 STRINGID_COUNT,
33};
34
Simon Glassb1446442015-03-25 12:22:39 -060035/**
36 * struct sandbox_flash_priv - private state for this driver
37 *
Simon Glass4c92a4d2022-09-21 16:21:36 +020038 * @eminfo: emulator state
Simon Glassb1446442015-03-25 12:22:39 -060039 * @error: true if there is an error condition
Simon Glassb1446442015-03-25 12:22:39 -060040 * @tag: Tag value from last command
41 * @fd: File descriptor of backing file
42 * @file_size: Size of file in bytes
43 * @status_buff: Data buffer for outgoing status
Simon Glassb1446442015-03-25 12:22:39 -060044 * @buff: Data buffer for outgoing data
45 */
46struct sandbox_flash_priv {
Simon Glass4c92a4d2022-09-21 16:21:36 +020047 struct scsi_emul_info eminfo;
Simon Glassb1446442015-03-25 12:22:39 -060048 bool error;
Simon Glassb1446442015-03-25 12:22:39 -060049 u32 tag;
50 int fd;
51 loff_t file_size;
52 struct umass_bbb_csw status;
Simon Glassb1446442015-03-25 12:22:39 -060053 u8 buff[512];
54};
55
56struct sandbox_flash_plat {
57 const char *pathname;
Simon Glass31d78162015-11-08 23:47:53 -070058 struct usb_string flash_strings[STRINGID_COUNT];
Simon Glassb1446442015-03-25 12:22:39 -060059};
60
61struct scsi_inquiry_resp {
62 u8 type;
63 u8 flags;
64 u8 version;
65 u8 data_format;
66 u8 additional_len;
67 u8 spare[3];
68 char vendor[8];
69 char product[16];
70 char revision[4];
71};
72
73struct scsi_read_capacity_resp {
74 u32 last_block_addr;
75 u32 block_len;
76};
77
78struct __packed scsi_read10_req {
79 u8 cmd;
80 u8 lun_flags;
81 u32 lba;
82 u8 spare;
Simon Glasse241fc32022-09-21 16:21:34 +020083 u16 xfer_len;
Simon Glassb1446442015-03-25 12:22:39 -060084 u8 spare2[3];
85};
86
Simon Glassb1446442015-03-25 12:22:39 -060087static struct usb_device_descriptor flash_device_desc = {
88 .bLength = sizeof(flash_device_desc),
89 .bDescriptorType = USB_DT_DEVICE,
90
91 .bcdUSB = __constant_cpu_to_le16(0x0200),
92
93 .bDeviceClass = 0,
94 .bDeviceSubClass = 0,
95 .bDeviceProtocol = 0,
96
97 .idVendor = __constant_cpu_to_le16(0x1234),
98 .idProduct = __constant_cpu_to_le16(0x5678),
99 .iManufacturer = STRINGID_MANUFACTURER,
100 .iProduct = STRINGID_PRODUCT,
101 .iSerialNumber = STRINGID_SERIAL,
102 .bNumConfigurations = 1,
103};
104
105static struct usb_config_descriptor flash_config0 = {
106 .bLength = sizeof(flash_config0),
107 .bDescriptorType = USB_DT_CONFIG,
108
109 /* wTotalLength is set up by usb-emul-uclass */
110 .bNumInterfaces = 1,
111 .bConfigurationValue = 0,
112 .iConfiguration = 0,
113 .bmAttributes = 1 << 7,
114 .bMaxPower = 50,
115};
116
117static struct usb_interface_descriptor flash_interface0 = {
118 .bLength = sizeof(flash_interface0),
119 .bDescriptorType = USB_DT_INTERFACE,
120
121 .bInterfaceNumber = 0,
122 .bAlternateSetting = 0,
123 .bNumEndpoints = 2,
124 .bInterfaceClass = USB_CLASS_MASS_STORAGE,
125 .bInterfaceSubClass = US_SC_UFI,
126 .bInterfaceProtocol = US_PR_BULK,
127 .iInterface = 0,
128};
129
130static struct usb_endpoint_descriptor flash_endpoint0_out = {
131 .bLength = USB_DT_ENDPOINT_SIZE,
132 .bDescriptorType = USB_DT_ENDPOINT,
133
134 .bEndpointAddress = SANDBOX_FLASH_EP_OUT,
135 .bmAttributes = USB_ENDPOINT_XFER_BULK,
136 .wMaxPacketSize = __constant_cpu_to_le16(1024),
137 .bInterval = 0,
138};
139
140static struct usb_endpoint_descriptor flash_endpoint1_in = {
141 .bLength = USB_DT_ENDPOINT_SIZE,
142 .bDescriptorType = USB_DT_ENDPOINT,
143
144 .bEndpointAddress = SANDBOX_FLASH_EP_IN | USB_ENDPOINT_DIR_MASK,
145 .bmAttributes = USB_ENDPOINT_XFER_BULK,
146 .wMaxPacketSize = __constant_cpu_to_le16(1024),
147 .bInterval = 0,
148};
149
150static void *flash_desc_list[] = {
151 &flash_device_desc,
152 &flash_config0,
153 &flash_interface0,
154 &flash_endpoint0_out,
155 &flash_endpoint1_in,
156 NULL,
157};
158
159static int sandbox_flash_control(struct udevice *dev, struct usb_device *udev,
160 unsigned long pipe, void *buff, int len,
161 struct devrequest *setup)
162{
163 struct sandbox_flash_priv *priv = dev_get_priv(dev);
164
165 if (pipe == usb_rcvctrlpipe(udev, 0)) {
166 switch (setup->request) {
167 case US_BBB_RESET:
168 priv->error = false;
169 return 0;
170 case US_BBB_GET_MAX_LUN:
171 *(char *)buff = '\0';
172 return 1;
173 default:
174 debug("request=%x\n", setup->request);
175 break;
176 }
177 }
178 debug("pipe=%lx\n", pipe);
179
180 return -EIO;
181}
182
183static void setup_fail_response(struct sandbox_flash_priv *priv)
184{
Simon Glass4c92a4d2022-09-21 16:21:36 +0200185 struct scsi_emul_info *info = &priv->eminfo;
Simon Glassb1446442015-03-25 12:22:39 -0600186 struct umass_bbb_csw *csw = &priv->status;
187
188 csw->dCSWSignature = CSWSIGNATURE;
189 csw->dCSWTag = priv->tag;
190 csw->dCSWDataResidue = 0;
191 csw->bCSWStatus = CSWSTATUS_FAILED;
Simon Glass4c92a4d2022-09-21 16:21:36 +0200192 info->buff_used = 0;
Simon Glassb1446442015-03-25 12:22:39 -0600193}
194
195/**
196 * setup_response() - set up a response to send back to the host
197 *
198 * @priv: Sandbox flash private data
199 * @resp: Response to send, or NULL if none
200 * @size: Size of response
201 */
202static void setup_response(struct sandbox_flash_priv *priv, void *resp,
203 int size)
204{
Simon Glass4c92a4d2022-09-21 16:21:36 +0200205 struct scsi_emul_info *info = &priv->eminfo;
Simon Glassb1446442015-03-25 12:22:39 -0600206 struct umass_bbb_csw *csw = &priv->status;
207
208 csw->dCSWSignature = CSWSIGNATURE;
209 csw->dCSWTag = priv->tag;
210 csw->dCSWDataResidue = 0;
211 csw->bCSWStatus = CSWSTATUS_GOOD;
212
213 assert(!resp || resp == priv->buff);
Simon Glass4c92a4d2022-09-21 16:21:36 +0200214 info->buff_used = size;
Simon Glassb1446442015-03-25 12:22:39 -0600215}
216
217static void handle_read(struct sandbox_flash_priv *priv, ulong lba,
218 ulong transfer_len)
219{
Simon Glass4c92a4d2022-09-21 16:21:36 +0200220 struct scsi_emul_info *info = &priv->eminfo;
221
Simon Glassb1446442015-03-25 12:22:39 -0600222 debug("%s: lba=%lx, transfer_len=%lx\n", __func__, lba, transfer_len);
Simon Glass4c92a4d2022-09-21 16:21:36 +0200223 info->read_len = transfer_len;
Simon Glassb1446442015-03-25 12:22:39 -0600224 if (priv->fd != -1) {
225 os_lseek(priv->fd, lba * SANDBOX_FLASH_BLOCK_LEN, OS_SEEK_SET);
Simon Glassb1446442015-03-25 12:22:39 -0600226 setup_response(priv, priv->buff,
227 transfer_len * SANDBOX_FLASH_BLOCK_LEN);
228 } else {
229 setup_fail_response(priv);
230 }
231}
232
Simon Glass31d78162015-11-08 23:47:53 -0700233static int handle_ufi_command(struct sandbox_flash_plat *plat,
234 struct sandbox_flash_priv *priv, const void *buff,
Simon Glassb1446442015-03-25 12:22:39 -0600235 int len)
236{
Simon Glass4c92a4d2022-09-21 16:21:36 +0200237 struct scsi_emul_info *info = &priv->eminfo;
Simon Glassa4eff9f2017-06-14 21:28:29 -0600238 const struct scsi_cmd *req = buff;
Simon Glassb1446442015-03-25 12:22:39 -0600239
240 switch (*req->cmd) {
241 case SCSI_INQUIRY: {
242 struct scsi_inquiry_resp *resp = (void *)priv->buff;
243
Simon Glass4c92a4d2022-09-21 16:21:36 +0200244 info->alloc_len = req->cmd[4];
Simon Glassb1446442015-03-25 12:22:39 -0600245 memset(resp, '\0', sizeof(*resp));
246 resp->data_format = 1;
247 resp->additional_len = 0x1f;
248 strncpy(resp->vendor,
Simon Glass31d78162015-11-08 23:47:53 -0700249 plat->flash_strings[STRINGID_MANUFACTURER - 1].s,
Simon Glassb1446442015-03-25 12:22:39 -0600250 sizeof(resp->vendor));
Simon Glass31d78162015-11-08 23:47:53 -0700251 strncpy(resp->product,
252 plat->flash_strings[STRINGID_PRODUCT - 1].s,
Simon Glassb1446442015-03-25 12:22:39 -0600253 sizeof(resp->product));
254 strncpy(resp->revision, "1.0", sizeof(resp->revision));
255 setup_response(priv, resp, sizeof(*resp));
256 break;
257 }
258 case SCSI_TST_U_RDY:
259 setup_response(priv, NULL, 0);
260 break;
261 case SCSI_RD_CAPAC: {
262 struct scsi_read_capacity_resp *resp = (void *)priv->buff;
263 uint blocks;
264
265 if (priv->file_size)
266 blocks = priv->file_size / SANDBOX_FLASH_BLOCK_LEN - 1;
267 else
268 blocks = 0;
269 resp->last_block_addr = cpu_to_be32(blocks);
270 resp->block_len = cpu_to_be32(SANDBOX_FLASH_BLOCK_LEN);
271 setup_response(priv, resp, sizeof(*resp));
272 break;
273 }
274 case SCSI_READ10: {
275 struct scsi_read10_req *req = (void *)buff;
276
277 handle_read(priv, be32_to_cpu(req->lba),
Simon Glasse241fc32022-09-21 16:21:34 +0200278 be16_to_cpu(req->xfer_len));
Simon Glassb1446442015-03-25 12:22:39 -0600279 break;
280 }
281 default:
282 debug("Command not supported: %x\n", req->cmd[0]);
283 return -EPROTONOSUPPORT;
284 }
285
Simon Glass4c92a4d2022-09-21 16:21:36 +0200286 info->phase = info->transfer_len ? SCSIPH_DATA : SCSIPH_STATUS;
Simon Glassb1446442015-03-25 12:22:39 -0600287 return 0;
288}
289
290static int sandbox_flash_bulk(struct udevice *dev, struct usb_device *udev,
291 unsigned long pipe, void *buff, int len)
292{
Simon Glassfa20e932020-12-03 16:55:20 -0700293 struct sandbox_flash_plat *plat = dev_get_plat(dev);
Simon Glassb1446442015-03-25 12:22:39 -0600294 struct sandbox_flash_priv *priv = dev_get_priv(dev);
Simon Glass4c92a4d2022-09-21 16:21:36 +0200295 struct scsi_emul_info *info = &priv->eminfo;
Simon Glassb1446442015-03-25 12:22:39 -0600296 int ep = usb_pipeendpoint(pipe);
297 struct umass_bbb_cbw *cbw = buff;
298
299 debug("%s: dev=%s, pipe=%lx, ep=%x, len=%x, phase=%d\n", __func__,
Simon Glass4c92a4d2022-09-21 16:21:36 +0200300 dev->name, pipe, ep, len, info->phase);
Simon Glassb1446442015-03-25 12:22:39 -0600301 switch (ep) {
302 case SANDBOX_FLASH_EP_OUT:
Simon Glass4c92a4d2022-09-21 16:21:36 +0200303 switch (info->phase) {
Simon Glasse6e05ad2022-09-21 16:21:35 +0200304 case SCSIPH_START:
Simon Glass4c92a4d2022-09-21 16:21:36 +0200305 info->alloc_len = 0;
306 info->read_len = 0;
Simon Glassb1446442015-03-25 12:22:39 -0600307 if (priv->error || len != UMASS_BBB_CBW_SIZE ||
308 cbw->dCBWSignature != CBWSIGNATURE)
309 goto err;
310 if ((cbw->bCBWFlags & CBWFLAGS_SBZ) ||
311 cbw->bCBWLUN != 0)
312 goto err;
313 if (cbw->bCDBLength < 1 || cbw->bCDBLength >= 0x10)
314 goto err;
Simon Glass4c92a4d2022-09-21 16:21:36 +0200315 info->transfer_len = cbw->dCBWDataTransferLength;
Simon Glassb1446442015-03-25 12:22:39 -0600316 priv->tag = cbw->dCBWTag;
Simon Glass31d78162015-11-08 23:47:53 -0700317 return handle_ufi_command(plat, priv, cbw->CBWCDB,
Simon Glassb1446442015-03-25 12:22:39 -0600318 cbw->bCDBLength);
Simon Glasse6e05ad2022-09-21 16:21:35 +0200319 case SCSIPH_DATA:
Simon Glassb1446442015-03-25 12:22:39 -0600320 debug("data out\n");
321 break;
322 default:
323 break;
324 }
325 case SANDBOX_FLASH_EP_IN:
Simon Glass4c92a4d2022-09-21 16:21:36 +0200326 switch (info->phase) {
Simon Glasse6e05ad2022-09-21 16:21:35 +0200327 case SCSIPH_DATA:
Simon Glass4c92a4d2022-09-21 16:21:36 +0200328 debug("data in, len=%x, alloc_len=%x, info->read_len=%x\n",
329 len, info->alloc_len, info->read_len);
330 if (info->read_len) {
Simon Glassb1446442015-03-25 12:22:39 -0600331 ulong bytes_read;
332
Sean Anderson03831852022-03-23 18:24:38 -0400333 if (priv->fd == -1)
334 return -EIO;
335
Simon Glassb1446442015-03-25 12:22:39 -0600336 bytes_read = os_read(priv->fd, buff, len);
337 if (bytes_read != len)
338 return -EIO;
Simon Glass4c92a4d2022-09-21 16:21:36 +0200339 info->read_len -= len / SANDBOX_FLASH_BLOCK_LEN;
340 if (!info->read_len)
341 info->phase = SCSIPH_STATUS;
Simon Glassb1446442015-03-25 12:22:39 -0600342 } else {
Simon Glass4c92a4d2022-09-21 16:21:36 +0200343 if (info->alloc_len && len > info->alloc_len)
344 len = info->alloc_len;
Andrew Scull50456c92022-04-03 10:39:12 +0000345 if (len > sizeof(priv->buff))
346 len = sizeof(priv->buff);
Simon Glassb1446442015-03-25 12:22:39 -0600347 memcpy(buff, priv->buff, len);
Simon Glass4c92a4d2022-09-21 16:21:36 +0200348 info->phase = SCSIPH_STATUS;
Simon Glassb1446442015-03-25 12:22:39 -0600349 }
350 return len;
Simon Glasse6e05ad2022-09-21 16:21:35 +0200351 case SCSIPH_STATUS:
Simon Glassb1446442015-03-25 12:22:39 -0600352 debug("status in, len=%x\n", len);
353 if (len > sizeof(priv->status))
354 len = sizeof(priv->status);
355 memcpy(buff, &priv->status, len);
Simon Glass4c92a4d2022-09-21 16:21:36 +0200356 info->phase = SCSIPH_START;
Simon Glassb1446442015-03-25 12:22:39 -0600357 return len;
358 default:
359 break;
360 }
361 }
362err:
363 priv->error = true;
364 debug("%s: Detected transfer error\n", __func__);
365 return 0;
366}
367
Simon Glassaad29ae2020-12-03 16:55:21 -0700368static int sandbox_flash_of_to_plat(struct udevice *dev)
Simon Glassb1446442015-03-25 12:22:39 -0600369{
Simon Glassfa20e932020-12-03 16:55:20 -0700370 struct sandbox_flash_plat *plat = dev_get_plat(dev);
Simon Glassb1446442015-03-25 12:22:39 -0600371
Simon Glass82bf00e2017-05-18 20:09:39 -0600372 plat->pathname = dev_read_string(dev, "sandbox,filepath");
Simon Glassb1446442015-03-25 12:22:39 -0600373
374 return 0;
375}
376
377static int sandbox_flash_bind(struct udevice *dev)
378{
Simon Glassfa20e932020-12-03 16:55:20 -0700379 struct sandbox_flash_plat *plat = dev_get_plat(dev);
Simon Glass31d78162015-11-08 23:47:53 -0700380 struct usb_string *fs;
381
382 fs = plat->flash_strings;
383 fs[0].id = STRINGID_MANUFACTURER;
384 fs[0].s = "sandbox";
385 fs[1].id = STRINGID_PRODUCT;
386 fs[1].s = "flash";
387 fs[2].id = STRINGID_SERIAL;
388 fs[2].s = dev->name;
389
Bin Mengd502cf12017-10-01 06:19:36 -0700390 return usb_emul_setup_device(dev, plat->flash_strings, flash_desc_list);
Simon Glassb1446442015-03-25 12:22:39 -0600391}
392
393static int sandbox_flash_probe(struct udevice *dev)
394{
Simon Glassfa20e932020-12-03 16:55:20 -0700395 struct sandbox_flash_plat *plat = dev_get_plat(dev);
Simon Glassb1446442015-03-25 12:22:39 -0600396 struct sandbox_flash_priv *priv = dev_get_priv(dev);
397
398 priv->fd = os_open(plat->pathname, OS_O_RDONLY);
399 if (priv->fd != -1)
400 return os_get_filesize(plat->pathname, &priv->file_size);
401
402 return 0;
403}
404
405static const struct dm_usb_ops sandbox_usb_flash_ops = {
406 .control = sandbox_flash_control,
407 .bulk = sandbox_flash_bulk,
408};
409
410static const struct udevice_id sandbox_usb_flash_ids[] = {
411 { .compatible = "sandbox,usb-flash" },
412 { }
413};
414
415U_BOOT_DRIVER(usb_sandbox_flash) = {
416 .name = "usb_sandbox_flash",
417 .id = UCLASS_USB_EMUL,
418 .of_match = sandbox_usb_flash_ids,
419 .bind = sandbox_flash_bind,
420 .probe = sandbox_flash_probe,
Simon Glassaad29ae2020-12-03 16:55:21 -0700421 .of_to_plat = sandbox_flash_of_to_plat,
Simon Glassb1446442015-03-25 12:22:39 -0600422 .ops = &sandbox_usb_flash_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700423 .priv_auto = sizeof(struct sandbox_flash_priv),
Simon Glass71fa5b42020-12-03 16:55:18 -0700424 .plat_auto = sizeof(struct sandbox_flash_plat),
Simon Glassb1446442015-03-25 12:22:39 -0600425};