sandbox_flash.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /*
  2. * (C) Copyright 2015 Google, Inc
  3. * Written by Simon Glass <sjg@chromium.org>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <dm.h>
  9. #include <os.h>
  10. #include <scsi.h>
  11. #include <usb.h>
  12. DECLARE_GLOBAL_DATA_PTR;
  13. /*
  14. * This driver emulates a flash stick using the UFI command specification and
  15. * the BBB (bulk/bulk/bulk) protocol. It supports only a single logical unit
  16. * number (LUN 0).
  17. */
  18. enum {
  19. SANDBOX_FLASH_EP_OUT = 1, /* endpoints */
  20. SANDBOX_FLASH_EP_IN = 2,
  21. SANDBOX_FLASH_BLOCK_LEN = 512,
  22. };
  23. enum cmd_phase {
  24. PHASE_START,
  25. PHASE_DATA,
  26. PHASE_STATUS,
  27. };
  28. enum {
  29. STRINGID_MANUFACTURER = 1,
  30. STRINGID_PRODUCT,
  31. STRINGID_SERIAL,
  32. STRINGID_COUNT,
  33. };
  34. /**
  35. * struct sandbox_flash_priv - private state for this driver
  36. *
  37. * @error: true if there is an error condition
  38. * @alloc_len: Allocation length from the last incoming command
  39. * @transfer_len: Transfer length from CBW header
  40. * @read_len: Number of blocks of data left in the current read command
  41. * @tag: Tag value from last command
  42. * @fd: File descriptor of backing file
  43. * @file_size: Size of file in bytes
  44. * @status_buff: Data buffer for outgoing status
  45. * @buff_used: Number of bytes ready to transfer back to host
  46. * @buff: Data buffer for outgoing data
  47. */
  48. struct sandbox_flash_priv {
  49. bool error;
  50. int alloc_len;
  51. int transfer_len;
  52. int read_len;
  53. enum cmd_phase phase;
  54. u32 tag;
  55. int fd;
  56. loff_t file_size;
  57. struct umass_bbb_csw status;
  58. int buff_used;
  59. u8 buff[512];
  60. };
  61. struct sandbox_flash_plat {
  62. const char *pathname;
  63. struct usb_string flash_strings[STRINGID_COUNT];
  64. };
  65. struct scsi_inquiry_resp {
  66. u8 type;
  67. u8 flags;
  68. u8 version;
  69. u8 data_format;
  70. u8 additional_len;
  71. u8 spare[3];
  72. char vendor[8];
  73. char product[16];
  74. char revision[4];
  75. };
  76. struct scsi_read_capacity_resp {
  77. u32 last_block_addr;
  78. u32 block_len;
  79. };
  80. struct __packed scsi_read10_req {
  81. u8 cmd;
  82. u8 lun_flags;
  83. u32 lba;
  84. u8 spare;
  85. u16 transfer_len;
  86. u8 spare2[3];
  87. };
  88. static struct usb_device_descriptor flash_device_desc = {
  89. .bLength = sizeof(flash_device_desc),
  90. .bDescriptorType = USB_DT_DEVICE,
  91. .bcdUSB = __constant_cpu_to_le16(0x0200),
  92. .bDeviceClass = 0,
  93. .bDeviceSubClass = 0,
  94. .bDeviceProtocol = 0,
  95. .idVendor = __constant_cpu_to_le16(0x1234),
  96. .idProduct = __constant_cpu_to_le16(0x5678),
  97. .iManufacturer = STRINGID_MANUFACTURER,
  98. .iProduct = STRINGID_PRODUCT,
  99. .iSerialNumber = STRINGID_SERIAL,
  100. .bNumConfigurations = 1,
  101. };
  102. static struct usb_config_descriptor flash_config0 = {
  103. .bLength = sizeof(flash_config0),
  104. .bDescriptorType = USB_DT_CONFIG,
  105. /* wTotalLength is set up by usb-emul-uclass */
  106. .bNumInterfaces = 1,
  107. .bConfigurationValue = 0,
  108. .iConfiguration = 0,
  109. .bmAttributes = 1 << 7,
  110. .bMaxPower = 50,
  111. };
  112. static struct usb_interface_descriptor flash_interface0 = {
  113. .bLength = sizeof(flash_interface0),
  114. .bDescriptorType = USB_DT_INTERFACE,
  115. .bInterfaceNumber = 0,
  116. .bAlternateSetting = 0,
  117. .bNumEndpoints = 2,
  118. .bInterfaceClass = USB_CLASS_MASS_STORAGE,
  119. .bInterfaceSubClass = US_SC_UFI,
  120. .bInterfaceProtocol = US_PR_BULK,
  121. .iInterface = 0,
  122. };
  123. static struct usb_endpoint_descriptor flash_endpoint0_out = {
  124. .bLength = USB_DT_ENDPOINT_SIZE,
  125. .bDescriptorType = USB_DT_ENDPOINT,
  126. .bEndpointAddress = SANDBOX_FLASH_EP_OUT,
  127. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  128. .wMaxPacketSize = __constant_cpu_to_le16(1024),
  129. .bInterval = 0,
  130. };
  131. static struct usb_endpoint_descriptor flash_endpoint1_in = {
  132. .bLength = USB_DT_ENDPOINT_SIZE,
  133. .bDescriptorType = USB_DT_ENDPOINT,
  134. .bEndpointAddress = SANDBOX_FLASH_EP_IN | USB_ENDPOINT_DIR_MASK,
  135. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  136. .wMaxPacketSize = __constant_cpu_to_le16(1024),
  137. .bInterval = 0,
  138. };
  139. static void *flash_desc_list[] = {
  140. &flash_device_desc,
  141. &flash_config0,
  142. &flash_interface0,
  143. &flash_endpoint0_out,
  144. &flash_endpoint1_in,
  145. NULL,
  146. };
  147. static int sandbox_flash_control(struct udevice *dev, struct usb_device *udev,
  148. unsigned long pipe, void *buff, int len,
  149. struct devrequest *setup)
  150. {
  151. struct sandbox_flash_priv *priv = dev_get_priv(dev);
  152. if (pipe == usb_rcvctrlpipe(udev, 0)) {
  153. switch (setup->request) {
  154. case US_BBB_RESET:
  155. priv->error = false;
  156. return 0;
  157. case US_BBB_GET_MAX_LUN:
  158. *(char *)buff = '\0';
  159. return 1;
  160. default:
  161. debug("request=%x\n", setup->request);
  162. break;
  163. }
  164. }
  165. debug("pipe=%lx\n", pipe);
  166. return -EIO;
  167. }
  168. static void setup_fail_response(struct sandbox_flash_priv *priv)
  169. {
  170. struct umass_bbb_csw *csw = &priv->status;
  171. csw->dCSWSignature = CSWSIGNATURE;
  172. csw->dCSWTag = priv->tag;
  173. csw->dCSWDataResidue = 0;
  174. csw->bCSWStatus = CSWSTATUS_FAILED;
  175. priv->buff_used = 0;
  176. }
  177. /**
  178. * setup_response() - set up a response to send back to the host
  179. *
  180. * @priv: Sandbox flash private data
  181. * @resp: Response to send, or NULL if none
  182. * @size: Size of response
  183. */
  184. static void setup_response(struct sandbox_flash_priv *priv, void *resp,
  185. int size)
  186. {
  187. struct umass_bbb_csw *csw = &priv->status;
  188. csw->dCSWSignature = CSWSIGNATURE;
  189. csw->dCSWTag = priv->tag;
  190. csw->dCSWDataResidue = 0;
  191. csw->bCSWStatus = CSWSTATUS_GOOD;
  192. assert(!resp || resp == priv->buff);
  193. priv->buff_used = size;
  194. }
  195. static void handle_read(struct sandbox_flash_priv *priv, ulong lba,
  196. ulong transfer_len)
  197. {
  198. debug("%s: lba=%lx, transfer_len=%lx\n", __func__, lba, transfer_len);
  199. if (priv->fd != -1) {
  200. os_lseek(priv->fd, lba * SANDBOX_FLASH_BLOCK_LEN, OS_SEEK_SET);
  201. priv->read_len = transfer_len;
  202. setup_response(priv, priv->buff,
  203. transfer_len * SANDBOX_FLASH_BLOCK_LEN);
  204. } else {
  205. setup_fail_response(priv);
  206. }
  207. }
  208. static int handle_ufi_command(struct sandbox_flash_plat *plat,
  209. struct sandbox_flash_priv *priv, const void *buff,
  210. int len)
  211. {
  212. const struct SCSI_cmd_block *req = buff;
  213. switch (*req->cmd) {
  214. case SCSI_INQUIRY: {
  215. struct scsi_inquiry_resp *resp = (void *)priv->buff;
  216. priv->alloc_len = req->cmd[4];
  217. memset(resp, '\0', sizeof(*resp));
  218. resp->data_format = 1;
  219. resp->additional_len = 0x1f;
  220. strncpy(resp->vendor,
  221. plat->flash_strings[STRINGID_MANUFACTURER - 1].s,
  222. sizeof(resp->vendor));
  223. strncpy(resp->product,
  224. plat->flash_strings[STRINGID_PRODUCT - 1].s,
  225. sizeof(resp->product));
  226. strncpy(resp->revision, "1.0", sizeof(resp->revision));
  227. setup_response(priv, resp, sizeof(*resp));
  228. break;
  229. }
  230. case SCSI_TST_U_RDY:
  231. setup_response(priv, NULL, 0);
  232. break;
  233. case SCSI_RD_CAPAC: {
  234. struct scsi_read_capacity_resp *resp = (void *)priv->buff;
  235. uint blocks;
  236. if (priv->file_size)
  237. blocks = priv->file_size / SANDBOX_FLASH_BLOCK_LEN - 1;
  238. else
  239. blocks = 0;
  240. resp->last_block_addr = cpu_to_be32(blocks);
  241. resp->block_len = cpu_to_be32(SANDBOX_FLASH_BLOCK_LEN);
  242. setup_response(priv, resp, sizeof(*resp));
  243. break;
  244. }
  245. case SCSI_READ10: {
  246. struct scsi_read10_req *req = (void *)buff;
  247. handle_read(priv, be32_to_cpu(req->lba),
  248. be16_to_cpu(req->transfer_len));
  249. break;
  250. }
  251. default:
  252. debug("Command not supported: %x\n", req->cmd[0]);
  253. return -EPROTONOSUPPORT;
  254. }
  255. priv->phase = priv->transfer_len ? PHASE_DATA : PHASE_STATUS;
  256. return 0;
  257. }
  258. static int sandbox_flash_bulk(struct udevice *dev, struct usb_device *udev,
  259. unsigned long pipe, void *buff, int len)
  260. {
  261. struct sandbox_flash_plat *plat = dev_get_platdata(dev);
  262. struct sandbox_flash_priv *priv = dev_get_priv(dev);
  263. int ep = usb_pipeendpoint(pipe);
  264. struct umass_bbb_cbw *cbw = buff;
  265. debug("%s: dev=%s, pipe=%lx, ep=%x, len=%x, phase=%d\n", __func__,
  266. dev->name, pipe, ep, len, priv->phase);
  267. switch (ep) {
  268. case SANDBOX_FLASH_EP_OUT:
  269. switch (priv->phase) {
  270. case PHASE_START:
  271. priv->alloc_len = 0;
  272. priv->read_len = 0;
  273. if (priv->error || len != UMASS_BBB_CBW_SIZE ||
  274. cbw->dCBWSignature != CBWSIGNATURE)
  275. goto err;
  276. if ((cbw->bCBWFlags & CBWFLAGS_SBZ) ||
  277. cbw->bCBWLUN != 0)
  278. goto err;
  279. if (cbw->bCDBLength < 1 || cbw->bCDBLength >= 0x10)
  280. goto err;
  281. priv->transfer_len = cbw->dCBWDataTransferLength;
  282. priv->tag = cbw->dCBWTag;
  283. return handle_ufi_command(plat, priv, cbw->CBWCDB,
  284. cbw->bCDBLength);
  285. case PHASE_DATA:
  286. debug("data out\n");
  287. break;
  288. default:
  289. break;
  290. }
  291. case SANDBOX_FLASH_EP_IN:
  292. switch (priv->phase) {
  293. case PHASE_DATA:
  294. debug("data in, len=%x, alloc_len=%x, priv->read_len=%x\n",
  295. len, priv->alloc_len, priv->read_len);
  296. if (priv->read_len) {
  297. ulong bytes_read;
  298. bytes_read = os_read(priv->fd, buff, len);
  299. if (bytes_read != len)
  300. return -EIO;
  301. priv->read_len -= len / SANDBOX_FLASH_BLOCK_LEN;
  302. if (!priv->read_len)
  303. priv->phase = PHASE_STATUS;
  304. } else {
  305. if (priv->alloc_len && len > priv->alloc_len)
  306. len = priv->alloc_len;
  307. memcpy(buff, priv->buff, len);
  308. priv->phase = PHASE_STATUS;
  309. }
  310. return len;
  311. case PHASE_STATUS:
  312. debug("status in, len=%x\n", len);
  313. if (len > sizeof(priv->status))
  314. len = sizeof(priv->status);
  315. memcpy(buff, &priv->status, len);
  316. priv->phase = PHASE_START;
  317. return len;
  318. default:
  319. break;
  320. }
  321. }
  322. err:
  323. priv->error = true;
  324. debug("%s: Detected transfer error\n", __func__);
  325. return 0;
  326. }
  327. static int sandbox_flash_ofdata_to_platdata(struct udevice *dev)
  328. {
  329. struct sandbox_flash_plat *plat = dev_get_platdata(dev);
  330. const void *blob = gd->fdt_blob;
  331. plat->pathname = fdt_getprop(blob, dev->of_offset, "sandbox,filepath",
  332. NULL);
  333. return 0;
  334. }
  335. static int sandbox_flash_bind(struct udevice *dev)
  336. {
  337. struct sandbox_flash_plat *plat = dev_get_platdata(dev);
  338. struct usb_string *fs;
  339. fs = plat->flash_strings;
  340. fs[0].id = STRINGID_MANUFACTURER;
  341. fs[0].s = "sandbox";
  342. fs[1].id = STRINGID_PRODUCT;
  343. fs[1].s = "flash";
  344. fs[2].id = STRINGID_SERIAL;
  345. fs[2].s = dev->name;
  346. return usb_emul_setup_device(dev, PACKET_SIZE_64, plat->flash_strings,
  347. flash_desc_list);
  348. }
  349. static int sandbox_flash_probe(struct udevice *dev)
  350. {
  351. struct sandbox_flash_plat *plat = dev_get_platdata(dev);
  352. struct sandbox_flash_priv *priv = dev_get_priv(dev);
  353. priv->fd = os_open(plat->pathname, OS_O_RDONLY);
  354. if (priv->fd != -1)
  355. return os_get_filesize(plat->pathname, &priv->file_size);
  356. return 0;
  357. }
  358. static const struct dm_usb_ops sandbox_usb_flash_ops = {
  359. .control = sandbox_flash_control,
  360. .bulk = sandbox_flash_bulk,
  361. };
  362. static const struct udevice_id sandbox_usb_flash_ids[] = {
  363. { .compatible = "sandbox,usb-flash" },
  364. { }
  365. };
  366. U_BOOT_DRIVER(usb_sandbox_flash) = {
  367. .name = "usb_sandbox_flash",
  368. .id = UCLASS_USB_EMUL,
  369. .of_match = sandbox_usb_flash_ids,
  370. .bind = sandbox_flash_bind,
  371. .probe = sandbox_flash_probe,
  372. .ofdata_to_platdata = sandbox_flash_ofdata_to_platdata,
  373. .ops = &sandbox_usb_flash_ops,
  374. .priv_auto_alloc_size = sizeof(struct sandbox_flash_priv),
  375. .platdata_auto_alloc_size = sizeof(struct sandbox_flash_plat),
  376. };