blob: eaa7f1e7ff67540248a5b10d5af1b6f2750f66b4 [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;
52 loff_t file_size;
53 struct umass_bbb_csw status;
Simon Glassb1446442015-03-25 12:22:39 -060054};
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
Simon Glass293c43f2022-09-21 16:21:37 +0200213 assert(!resp || resp == info->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 Glass293c43f2022-09-21 16:21:37 +0200226 setup_response(priv, info->buff,
Simon Glassb1446442015-03-25 12:22:39 -0600227 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: {
Simon Glass293c43f2022-09-21 16:21:37 +0200242 struct scsi_inquiry_resp *resp = (void *)info->buff;
Simon Glassb1446442015-03-25 12:22:39 -0600243
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;
Simon Glass37884f02022-09-21 16:21:38 +0200248 strncpy(resp->vendor, info->vendor, sizeof(resp->vendor));
249 strncpy(resp->product, info->product, sizeof(resp->product));
Simon Glassb1446442015-03-25 12:22:39 -0600250 strncpy(resp->revision, "1.0", sizeof(resp->revision));
251 setup_response(priv, resp, sizeof(*resp));
252 break;
253 }
254 case SCSI_TST_U_RDY:
255 setup_response(priv, NULL, 0);
256 break;
257 case SCSI_RD_CAPAC: {
Simon Glass293c43f2022-09-21 16:21:37 +0200258 struct scsi_read_capacity_resp *resp = (void *)info->buff;
Simon Glassb1446442015-03-25 12:22:39 -0600259 uint blocks;
260
261 if (priv->file_size)
262 blocks = priv->file_size / SANDBOX_FLASH_BLOCK_LEN - 1;
263 else
264 blocks = 0;
265 resp->last_block_addr = cpu_to_be32(blocks);
266 resp->block_len = cpu_to_be32(SANDBOX_FLASH_BLOCK_LEN);
267 setup_response(priv, resp, sizeof(*resp));
268 break;
269 }
270 case SCSI_READ10: {
271 struct scsi_read10_req *req = (void *)buff;
272
273 handle_read(priv, be32_to_cpu(req->lba),
Simon Glasse241fc32022-09-21 16:21:34 +0200274 be16_to_cpu(req->xfer_len));
Simon Glassb1446442015-03-25 12:22:39 -0600275 break;
276 }
277 default:
278 debug("Command not supported: %x\n", req->cmd[0]);
279 return -EPROTONOSUPPORT;
280 }
281
Simon Glass4c92a4d2022-09-21 16:21:36 +0200282 info->phase = info->transfer_len ? SCSIPH_DATA : SCSIPH_STATUS;
Simon Glassb1446442015-03-25 12:22:39 -0600283 return 0;
284}
285
286static int sandbox_flash_bulk(struct udevice *dev, struct usb_device *udev,
287 unsigned long pipe, void *buff, int len)
288{
Simon Glassfa20e932020-12-03 16:55:20 -0700289 struct sandbox_flash_plat *plat = dev_get_plat(dev);
Simon Glassb1446442015-03-25 12:22:39 -0600290 struct sandbox_flash_priv *priv = dev_get_priv(dev);
Simon Glass4c92a4d2022-09-21 16:21:36 +0200291 struct scsi_emul_info *info = &priv->eminfo;
Simon Glassb1446442015-03-25 12:22:39 -0600292 int ep = usb_pipeendpoint(pipe);
293 struct umass_bbb_cbw *cbw = buff;
294
295 debug("%s: dev=%s, pipe=%lx, ep=%x, len=%x, phase=%d\n", __func__,
Simon Glass4c92a4d2022-09-21 16:21:36 +0200296 dev->name, pipe, ep, len, info->phase);
Simon Glassb1446442015-03-25 12:22:39 -0600297 switch (ep) {
298 case SANDBOX_FLASH_EP_OUT:
Simon Glass4c92a4d2022-09-21 16:21:36 +0200299 switch (info->phase) {
Simon Glasse6e05ad2022-09-21 16:21:35 +0200300 case SCSIPH_START:
Simon Glass4c92a4d2022-09-21 16:21:36 +0200301 info->alloc_len = 0;
302 info->read_len = 0;
Simon Glassb1446442015-03-25 12:22:39 -0600303 if (priv->error || len != UMASS_BBB_CBW_SIZE ||
304 cbw->dCBWSignature != CBWSIGNATURE)
305 goto err;
306 if ((cbw->bCBWFlags & CBWFLAGS_SBZ) ||
307 cbw->bCBWLUN != 0)
308 goto err;
309 if (cbw->bCDBLength < 1 || cbw->bCDBLength >= 0x10)
310 goto err;
Simon Glass4c92a4d2022-09-21 16:21:36 +0200311 info->transfer_len = cbw->dCBWDataTransferLength;
Simon Glassb1446442015-03-25 12:22:39 -0600312 priv->tag = cbw->dCBWTag;
Simon Glass31d78162015-11-08 23:47:53 -0700313 return handle_ufi_command(plat, priv, cbw->CBWCDB,
Simon Glassb1446442015-03-25 12:22:39 -0600314 cbw->bCDBLength);
Simon Glasse6e05ad2022-09-21 16:21:35 +0200315 case SCSIPH_DATA:
Simon Glassb1446442015-03-25 12:22:39 -0600316 debug("data out\n");
317 break;
318 default:
319 break;
320 }
321 case SANDBOX_FLASH_EP_IN:
Simon Glass4c92a4d2022-09-21 16:21:36 +0200322 switch (info->phase) {
Simon Glasse6e05ad2022-09-21 16:21:35 +0200323 case SCSIPH_DATA:
Simon Glass4c92a4d2022-09-21 16:21:36 +0200324 debug("data in, len=%x, alloc_len=%x, info->read_len=%x\n",
325 len, info->alloc_len, info->read_len);
326 if (info->read_len) {
Simon Glassb1446442015-03-25 12:22:39 -0600327 ulong bytes_read;
328
Sean Anderson03831852022-03-23 18:24:38 -0400329 if (priv->fd == -1)
330 return -EIO;
331
Simon Glassb1446442015-03-25 12:22:39 -0600332 bytes_read = os_read(priv->fd, buff, len);
333 if (bytes_read != len)
334 return -EIO;
Simon Glass4c92a4d2022-09-21 16:21:36 +0200335 info->read_len -= len / SANDBOX_FLASH_BLOCK_LEN;
336 if (!info->read_len)
337 info->phase = SCSIPH_STATUS;
Simon Glassb1446442015-03-25 12:22:39 -0600338 } else {
Simon Glass4c92a4d2022-09-21 16:21:36 +0200339 if (info->alloc_len && len > info->alloc_len)
340 len = info->alloc_len;
Simon Glass293c43f2022-09-21 16:21:37 +0200341 if (len > SANDBOX_FLASH_BUF_SIZE)
342 len = SANDBOX_FLASH_BUF_SIZE;
343 memcpy(buff, info->buff, len);
Simon Glass4c92a4d2022-09-21 16:21:36 +0200344 info->phase = SCSIPH_STATUS;
Simon Glassb1446442015-03-25 12:22:39 -0600345 }
346 return len;
Simon Glasse6e05ad2022-09-21 16:21:35 +0200347 case SCSIPH_STATUS:
Simon Glassb1446442015-03-25 12:22:39 -0600348 debug("status in, len=%x\n", len);
349 if (len > sizeof(priv->status))
350 len = sizeof(priv->status);
351 memcpy(buff, &priv->status, len);
Simon Glass4c92a4d2022-09-21 16:21:36 +0200352 info->phase = SCSIPH_START;
Simon Glassb1446442015-03-25 12:22:39 -0600353 return len;
354 default:
355 break;
356 }
357 }
358err:
359 priv->error = true;
360 debug("%s: Detected transfer error\n", __func__);
361 return 0;
362}
363
Simon Glassaad29ae2020-12-03 16:55:21 -0700364static int sandbox_flash_of_to_plat(struct udevice *dev)
Simon Glassb1446442015-03-25 12:22:39 -0600365{
Simon Glassfa20e932020-12-03 16:55:20 -0700366 struct sandbox_flash_plat *plat = dev_get_plat(dev);
Simon Glassb1446442015-03-25 12:22:39 -0600367
Simon Glass82bf00e2017-05-18 20:09:39 -0600368 plat->pathname = dev_read_string(dev, "sandbox,filepath");
Simon Glassb1446442015-03-25 12:22:39 -0600369
370 return 0;
371}
372
373static int sandbox_flash_bind(struct udevice *dev)
374{
Simon Glassfa20e932020-12-03 16:55:20 -0700375 struct sandbox_flash_plat *plat = dev_get_plat(dev);
Simon Glass31d78162015-11-08 23:47:53 -0700376 struct usb_string *fs;
377
378 fs = plat->flash_strings;
379 fs[0].id = STRINGID_MANUFACTURER;
380 fs[0].s = "sandbox";
381 fs[1].id = STRINGID_PRODUCT;
382 fs[1].s = "flash";
383 fs[2].id = STRINGID_SERIAL;
384 fs[2].s = dev->name;
385
Bin Mengd502cf12017-10-01 06:19:36 -0700386 return usb_emul_setup_device(dev, plat->flash_strings, flash_desc_list);
Simon Glassb1446442015-03-25 12:22:39 -0600387}
388
389static int sandbox_flash_probe(struct udevice *dev)
390{
Simon Glassfa20e932020-12-03 16:55:20 -0700391 struct sandbox_flash_plat *plat = dev_get_plat(dev);
Simon Glassb1446442015-03-25 12:22:39 -0600392 struct sandbox_flash_priv *priv = dev_get_priv(dev);
Simon Glass293c43f2022-09-21 16:21:37 +0200393 struct scsi_emul_info *info = &priv->eminfo;
394 int ret;
Simon Glassb1446442015-03-25 12:22:39 -0600395
396 priv->fd = os_open(plat->pathname, OS_O_RDONLY);
Simon Glass293c43f2022-09-21 16:21:37 +0200397 if (priv->fd != -1) {
398 ret = os_get_filesize(plat->pathname, &priv->file_size);
399 if (ret)
400 return log_msg_ret("sz", ret);
401 }
402 info->buff = malloc(SANDBOX_FLASH_BUF_SIZE);
403 if (!info->buff)
404 return log_ret(-ENOMEM);
Simon Glass37884f02022-09-21 16:21:38 +0200405 info->vendor = plat->flash_strings[STRINGID_MANUFACTURER - 1].s;
406 info->product = plat->flash_strings[STRINGID_PRODUCT - 1].s;
Simon Glass293c43f2022-09-21 16:21:37 +0200407
408 return 0;
409}
410
411static int sandbox_flash_remove(struct udevice *dev)
412{
413 struct sandbox_flash_priv *priv = dev_get_priv(dev);
414 struct scsi_emul_info *info = &priv->eminfo;
415
416 free(info->buff);
Simon Glassb1446442015-03-25 12:22:39 -0600417
418 return 0;
419}
420
421static const struct dm_usb_ops sandbox_usb_flash_ops = {
422 .control = sandbox_flash_control,
423 .bulk = sandbox_flash_bulk,
424};
425
426static const struct udevice_id sandbox_usb_flash_ids[] = {
427 { .compatible = "sandbox,usb-flash" },
428 { }
429};
430
431U_BOOT_DRIVER(usb_sandbox_flash) = {
432 .name = "usb_sandbox_flash",
433 .id = UCLASS_USB_EMUL,
434 .of_match = sandbox_usb_flash_ids,
435 .bind = sandbox_flash_bind,
436 .probe = sandbox_flash_probe,
Simon Glass293c43f2022-09-21 16:21:37 +0200437 .remove = sandbox_flash_remove,
Simon Glassaad29ae2020-12-03 16:55:21 -0700438 .of_to_plat = sandbox_flash_of_to_plat,
Simon Glassb1446442015-03-25 12:22:39 -0600439 .ops = &sandbox_usb_flash_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700440 .priv_auto = sizeof(struct sandbox_flash_priv),
Simon Glass71fa5b42020-12-03 16:55:18 -0700441 .plat_auto = sizeof(struct sandbox_flash_plat),
Simon Glassb1446442015-03-25 12:22:39 -0600442};