sandbox_flash.c 10 KB

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