sandbox_flash.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2015 Google, Inc
  4. * Written by Simon Glass <sjg@chromium.org>
  5. */
  6. #define LOG_CATEGORY UCLASS_USB
  7. #include <common.h>
  8. #include <dm.h>
  9. #include <log.h>
  10. #include <malloc.h>
  11. #include <os.h>
  12. #include <scsi.h>
  13. #include <scsi_emul.h>
  14. #include <usb.h>
  15. /*
  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. enum {
  21. SANDBOX_FLASH_EP_OUT = 1, /* endpoints */
  22. SANDBOX_FLASH_EP_IN = 2,
  23. SANDBOX_FLASH_BLOCK_LEN = 512,
  24. SANDBOX_FLASH_BUF_SIZE = 512,
  25. };
  26. enum {
  27. STRINGID_MANUFACTURER = 1,
  28. STRINGID_PRODUCT,
  29. STRINGID_SERIAL,
  30. STRINGID_COUNT,
  31. };
  32. /**
  33. * struct sandbox_flash_priv - private state for this driver
  34. *
  35. * @eminfo: emulator state
  36. * @error: true if there is an error condition
  37. * @tag: Tag value from last command
  38. * @fd: File descriptor of backing file
  39. * @file_size: Size of file in bytes
  40. * @status_buff: Data buffer for outgoing status
  41. */
  42. struct sandbox_flash_priv {
  43. struct scsi_emul_info eminfo;
  44. bool error;
  45. u32 tag;
  46. int fd;
  47. struct umass_bbb_csw status;
  48. };
  49. struct sandbox_flash_plat {
  50. const char *pathname;
  51. struct usb_string flash_strings[STRINGID_COUNT];
  52. };
  53. static struct usb_device_descriptor flash_device_desc = {
  54. .bLength = sizeof(flash_device_desc),
  55. .bDescriptorType = USB_DT_DEVICE,
  56. .bcdUSB = __constant_cpu_to_le16(0x0200),
  57. .bDeviceClass = 0,
  58. .bDeviceSubClass = 0,
  59. .bDeviceProtocol = 0,
  60. .idVendor = __constant_cpu_to_le16(0x1234),
  61. .idProduct = __constant_cpu_to_le16(0x5678),
  62. .iManufacturer = STRINGID_MANUFACTURER,
  63. .iProduct = STRINGID_PRODUCT,
  64. .iSerialNumber = STRINGID_SERIAL,
  65. .bNumConfigurations = 1,
  66. };
  67. static struct usb_config_descriptor flash_config0 = {
  68. .bLength = sizeof(flash_config0),
  69. .bDescriptorType = USB_DT_CONFIG,
  70. /* wTotalLength is set up by usb-emul-uclass */
  71. .bNumInterfaces = 1,
  72. .bConfigurationValue = 0,
  73. .iConfiguration = 0,
  74. .bmAttributes = 1 << 7,
  75. .bMaxPower = 50,
  76. };
  77. static struct usb_interface_descriptor flash_interface0 = {
  78. .bLength = sizeof(flash_interface0),
  79. .bDescriptorType = USB_DT_INTERFACE,
  80. .bInterfaceNumber = 0,
  81. .bAlternateSetting = 0,
  82. .bNumEndpoints = 2,
  83. .bInterfaceClass = USB_CLASS_MASS_STORAGE,
  84. .bInterfaceSubClass = US_SC_UFI,
  85. .bInterfaceProtocol = US_PR_BULK,
  86. .iInterface = 0,
  87. };
  88. static struct usb_endpoint_descriptor flash_endpoint0_out = {
  89. .bLength = USB_DT_ENDPOINT_SIZE,
  90. .bDescriptorType = USB_DT_ENDPOINT,
  91. .bEndpointAddress = SANDBOX_FLASH_EP_OUT,
  92. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  93. .wMaxPacketSize = __constant_cpu_to_le16(1024),
  94. .bInterval = 0,
  95. };
  96. static struct usb_endpoint_descriptor flash_endpoint1_in = {
  97. .bLength = USB_DT_ENDPOINT_SIZE,
  98. .bDescriptorType = USB_DT_ENDPOINT,
  99. .bEndpointAddress = SANDBOX_FLASH_EP_IN | USB_ENDPOINT_DIR_MASK,
  100. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  101. .wMaxPacketSize = __constant_cpu_to_le16(1024),
  102. .bInterval = 0,
  103. };
  104. static void *flash_desc_list[] = {
  105. &flash_device_desc,
  106. &flash_config0,
  107. &flash_interface0,
  108. &flash_endpoint0_out,
  109. &flash_endpoint1_in,
  110. NULL,
  111. };
  112. static int sandbox_flash_control(struct udevice *dev, struct usb_device *udev,
  113. unsigned long pipe, void *buff, int len,
  114. struct devrequest *setup)
  115. {
  116. struct sandbox_flash_priv *priv = dev_get_priv(dev);
  117. if (pipe == usb_rcvctrlpipe(udev, 0)) {
  118. switch (setup->request) {
  119. case US_BBB_RESET:
  120. priv->error = false;
  121. return 0;
  122. case US_BBB_GET_MAX_LUN:
  123. *(char *)buff = '\0';
  124. return 1;
  125. default:
  126. debug("request=%x\n", setup->request);
  127. break;
  128. }
  129. }
  130. debug("pipe=%lx\n", pipe);
  131. return -EIO;
  132. }
  133. static void setup_fail_response(struct sandbox_flash_priv *priv)
  134. {
  135. struct umass_bbb_csw *csw = &priv->status;
  136. csw->dCSWSignature = CSWSIGNATURE;
  137. csw->dCSWTag = priv->tag;
  138. csw->dCSWDataResidue = 0;
  139. csw->bCSWStatus = CSWSTATUS_FAILED;
  140. }
  141. /**
  142. * setup_response() - set up a response to send back to the host
  143. *
  144. * @priv: Sandbox flash private data
  145. * @resp: Response to send, or NULL if none
  146. * @size: Size of response
  147. */
  148. static void setup_response(struct sandbox_flash_priv *priv)
  149. {
  150. struct umass_bbb_csw *csw = &priv->status;
  151. csw->dCSWSignature = CSWSIGNATURE;
  152. csw->dCSWTag = priv->tag;
  153. csw->dCSWDataResidue = 0;
  154. csw->bCSWStatus = CSWSTATUS_GOOD;
  155. }
  156. static int handle_ufi_command(struct sandbox_flash_priv *priv, const void *buff,
  157. int len)
  158. {
  159. struct scsi_emul_info *info = &priv->eminfo;
  160. const struct scsi_cmd *req = buff;
  161. int ret;
  162. off_t offset;
  163. ret = sb_scsi_emul_command(info, req, len);
  164. if (!ret) {
  165. setup_response(priv);
  166. } else if ((ret == SCSI_EMUL_DO_READ || ret == SCSI_EMUL_DO_WRITE) &&
  167. priv->fd != -1) {
  168. offset = os_lseek(priv->fd, info->seek_block * info->block_size,
  169. OS_SEEK_SET);
  170. if (offset == (off_t)-1)
  171. setup_fail_response(priv);
  172. else
  173. setup_response(priv);
  174. } else {
  175. setup_fail_response(priv);
  176. }
  177. return 0;
  178. }
  179. static int sandbox_flash_bulk(struct udevice *dev, struct usb_device *udev,
  180. unsigned long pipe, void *buff, int len)
  181. {
  182. struct sandbox_flash_priv *priv = dev_get_priv(dev);
  183. struct scsi_emul_info *info = &priv->eminfo;
  184. int ep = usb_pipeendpoint(pipe);
  185. struct umass_bbb_cbw *cbw = buff;
  186. debug("%s: dev=%s, pipe=%lx, ep=%x, len=%x, phase=%d\n", __func__,
  187. dev->name, pipe, ep, len, info->phase);
  188. switch (ep) {
  189. case SANDBOX_FLASH_EP_OUT:
  190. switch (info->phase) {
  191. case SCSIPH_START:
  192. info->alloc_len = 0;
  193. info->read_len = 0;
  194. info->write_len = 0;
  195. if (priv->error || len != UMASS_BBB_CBW_SIZE ||
  196. cbw->dCBWSignature != CBWSIGNATURE)
  197. goto err;
  198. if ((cbw->bCBWFlags & CBWFLAGS_SBZ) ||
  199. cbw->bCBWLUN != 0)
  200. goto err;
  201. if (cbw->bCDBLength < 1 || cbw->bCDBLength >= 0x10)
  202. goto err;
  203. info->transfer_len = cbw->dCBWDataTransferLength;
  204. priv->tag = cbw->dCBWTag;
  205. return handle_ufi_command(priv, cbw->CBWCDB,
  206. cbw->bCDBLength);
  207. case SCSIPH_DATA:
  208. log_debug("data out, len=%x, info->write_len=%x\n", len,
  209. info->write_len);
  210. info->transfer_len = cbw->dCBWDataTransferLength;
  211. priv->tag = cbw->dCBWTag;
  212. if (!info->write_len)
  213. return 0;
  214. if (priv->fd != -1) {
  215. ulong bytes_written;
  216. bytes_written = os_write(priv->fd, buff, len);
  217. log_debug("bytes_written=%lx", bytes_written);
  218. if (bytes_written != len)
  219. return -EIO;
  220. info->write_len -= len / info->block_size;
  221. if (!info->write_len)
  222. info->phase = SCSIPH_STATUS;
  223. } else {
  224. if (info->alloc_len && len > info->alloc_len)
  225. len = info->alloc_len;
  226. if (len > SANDBOX_FLASH_BUF_SIZE)
  227. len = SANDBOX_FLASH_BUF_SIZE;
  228. memcpy(info->buff, buff, len);
  229. info->phase = SCSIPH_STATUS;
  230. }
  231. return len;
  232. default:
  233. break;
  234. }
  235. break;
  236. case SANDBOX_FLASH_EP_IN:
  237. switch (info->phase) {
  238. case SCSIPH_DATA:
  239. debug("data in, len=%x, alloc_len=%x, info->read_len=%x\n",
  240. len, info->alloc_len, info->read_len);
  241. if (info->read_len) {
  242. ulong bytes_read;
  243. if (priv->fd == -1)
  244. return -EIO;
  245. bytes_read = os_read(priv->fd, buff, len);
  246. if (bytes_read != len)
  247. return -EIO;
  248. info->read_len -= len / info->block_size;
  249. if (!info->read_len)
  250. info->phase = SCSIPH_STATUS;
  251. } else {
  252. if (info->alloc_len && len > info->alloc_len)
  253. len = info->alloc_len;
  254. if (len > SANDBOX_FLASH_BUF_SIZE)
  255. len = SANDBOX_FLASH_BUF_SIZE;
  256. memcpy(buff, info->buff, len);
  257. info->phase = SCSIPH_STATUS;
  258. }
  259. return len;
  260. case SCSIPH_STATUS:
  261. debug("status in, len=%x\n", len);
  262. if (len > sizeof(priv->status))
  263. len = sizeof(priv->status);
  264. memcpy(buff, &priv->status, len);
  265. info->phase = SCSIPH_START;
  266. return len;
  267. default:
  268. break;
  269. }
  270. }
  271. err:
  272. priv->error = true;
  273. debug("%s: Detected transfer error\n", __func__);
  274. return 0;
  275. }
  276. static int sandbox_flash_of_to_plat(struct udevice *dev)
  277. {
  278. struct sandbox_flash_plat *plat = dev_get_plat(dev);
  279. plat->pathname = dev_read_string(dev, "sandbox,filepath");
  280. return 0;
  281. }
  282. static int sandbox_flash_bind(struct udevice *dev)
  283. {
  284. struct sandbox_flash_plat *plat = dev_get_plat(dev);
  285. struct usb_string *fs;
  286. fs = plat->flash_strings;
  287. fs[0].id = STRINGID_MANUFACTURER;
  288. fs[0].s = "sandbox";
  289. fs[1].id = STRINGID_PRODUCT;
  290. fs[1].s = "flash";
  291. fs[2].id = STRINGID_SERIAL;
  292. fs[2].s = dev->name;
  293. return usb_emul_setup_device(dev, plat->flash_strings, flash_desc_list);
  294. }
  295. static int sandbox_flash_probe(struct udevice *dev)
  296. {
  297. struct sandbox_flash_plat *plat = dev_get_plat(dev);
  298. struct sandbox_flash_priv *priv = dev_get_priv(dev);
  299. struct scsi_emul_info *info = &priv->eminfo;
  300. int ret;
  301. priv->fd = os_open(plat->pathname, OS_O_RDWR);
  302. if (priv->fd != -1) {
  303. ret = os_get_filesize(plat->pathname, &info->file_size);
  304. if (ret)
  305. return log_msg_ret("sz", ret);
  306. }
  307. info->buff = malloc(SANDBOX_FLASH_BUF_SIZE);
  308. if (!info->buff)
  309. return log_ret(-ENOMEM);
  310. info->vendor = plat->flash_strings[STRINGID_MANUFACTURER - 1].s;
  311. info->product = plat->flash_strings[STRINGID_PRODUCT - 1].s;
  312. info->block_size = SANDBOX_FLASH_BLOCK_LEN;
  313. return 0;
  314. }
  315. static int sandbox_flash_remove(struct udevice *dev)
  316. {
  317. struct sandbox_flash_priv *priv = dev_get_priv(dev);
  318. struct scsi_emul_info *info = &priv->eminfo;
  319. free(info->buff);
  320. return 0;
  321. }
  322. static const struct dm_usb_ops sandbox_usb_flash_ops = {
  323. .control = sandbox_flash_control,
  324. .bulk = sandbox_flash_bulk,
  325. };
  326. static const struct udevice_id sandbox_usb_flash_ids[] = {
  327. { .compatible = "sandbox,usb-flash" },
  328. { }
  329. };
  330. U_BOOT_DRIVER(usb_sandbox_flash) = {
  331. .name = "usb_sandbox_flash",
  332. .id = UCLASS_USB_EMUL,
  333. .of_match = sandbox_usb_flash_ids,
  334. .bind = sandbox_flash_bind,
  335. .probe = sandbox_flash_probe,
  336. .remove = sandbox_flash_remove,
  337. .of_to_plat = sandbox_flash_of_to_plat,
  338. .ops = &sandbox_usb_flash_ops,
  339. .priv_auto = sizeof(struct sandbox_flash_priv),
  340. .plat_auto = sizeof(struct sandbox_flash_plat),
  341. };