sandbox_flash.c 10 KB

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