usbms.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /* usbms.c - USB Mass Storage Support. */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2008 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <grub/dl.h>
  20. #include <grub/mm.h>
  21. #include <grub/usb.h>
  22. #include <grub/scsi.h>
  23. #include <grub/scsicmd.h>
  24. #include <grub/misc.h>
  25. #define GRUB_USBMS_DIRECTION_BIT 7
  26. /* The USB Mass Storage Command Block Wrapper. */
  27. struct grub_usbms_cbw
  28. {
  29. grub_uint32_t signature;
  30. grub_uint32_t tag;
  31. grub_uint32_t transfer_length;
  32. grub_uint8_t flags;
  33. grub_uint8_t lun;
  34. grub_uint8_t length;
  35. grub_uint8_t cbwcb[16];
  36. } __attribute__ ((packed));
  37. struct grub_usbms_csw
  38. {
  39. grub_uint32_t signature;
  40. grub_uint32_t tag;
  41. grub_uint32_t residue;
  42. grub_uint8_t status;
  43. } __attribute__ ((packed));
  44. struct grub_usbms_dev
  45. {
  46. struct grub_usb_device *dev;
  47. int luns;
  48. int interface;
  49. struct grub_usb_desc_endp *in;
  50. struct grub_usb_desc_endp *out;
  51. int in_maxsz;
  52. int out_maxsz;
  53. struct grub_usbms_dev *next;
  54. };
  55. typedef struct grub_usbms_dev *grub_usbms_dev_t;
  56. static grub_usbms_dev_t grub_usbms_dev_list;
  57. static int devcnt;
  58. static grub_err_t
  59. grub_usbms_reset (grub_usb_device_t dev, int interface)
  60. {
  61. return grub_usb_control_msg (dev, 0x21, 255, 0, interface, 0, 0);
  62. }
  63. static int
  64. usb_iterate (grub_usb_device_t usbdev,
  65. void *closure __attribute__ ((unused)))
  66. {
  67. grub_usb_err_t err;
  68. struct grub_usb_desc_device *descdev = &usbdev->descdev;
  69. int i;
  70. if (descdev->class != 0 || descdev->subclass || descdev->protocol != 0)
  71. return 0;
  72. /* XXX: Just check configuration 0 for now. */
  73. for (i = 0; i < usbdev->config[0].descconf->numif; i++)
  74. {
  75. struct grub_usbms_dev *usbms;
  76. struct grub_usb_desc_if *interf;
  77. int j;
  78. grub_uint8_t luns;
  79. interf = usbdev->config[0].interf[i].descif;
  80. /* If this is not a USB Mass Storage device with a supported
  81. protocol, just skip it. */
  82. if (interf->class != GRUB_USB_CLASS_MASS_STORAGE
  83. || interf->subclass != GRUB_USBMS_SUBCLASS_BULK
  84. || interf->protocol != GRUB_USBMS_PROTOCOL_BULK)
  85. {
  86. continue;
  87. }
  88. devcnt++;
  89. usbms = grub_zalloc (sizeof (struct grub_usbms_dev));
  90. if (! usbms)
  91. return 1;
  92. usbms->dev = usbdev;
  93. usbms->interface = i;
  94. /* Iterate over all endpoints of this interface, at least a
  95. IN and OUT bulk endpoint are required. */
  96. for (j = 0; j < interf->endpointcnt; j++)
  97. {
  98. struct grub_usb_desc_endp *endp;
  99. endp = &usbdev->config[0].interf[i].descendp[j];
  100. if ((endp->endp_addr & 128) && (endp->attrib & 3) == 2)
  101. {
  102. /* Bulk IN endpoint. */
  103. usbms->in = endp;
  104. grub_usb_clear_halt (usbdev, endp->endp_addr & 128);
  105. usbms->in_maxsz = endp->maxpacket;
  106. }
  107. else if (!(endp->endp_addr & 128) && (endp->attrib & 3) == 2)
  108. {
  109. /* Bulk OUT endpoint. */
  110. usbms->out = endp;
  111. grub_usb_clear_halt (usbdev, endp->endp_addr & 128);
  112. usbms->out_maxsz = endp->maxpacket;
  113. }
  114. }
  115. if (!usbms->in || !usbms->out)
  116. {
  117. grub_free (usbms);
  118. return 0;
  119. }
  120. /* Query the amount of LUNs. */
  121. err = grub_usb_control_msg (usbdev, 0xA1, 254,
  122. 0, i, 1, (char *) &luns);
  123. if (err)
  124. {
  125. /* In case of a stall, clear the stall. */
  126. if (err == GRUB_USB_ERR_STALL)
  127. {
  128. grub_usb_clear_halt (usbdev, usbms->in->endp_addr & 3);
  129. grub_usb_clear_halt (usbdev, usbms->out->endp_addr & 3);
  130. }
  131. /* Just set the amount of LUNs to one. */
  132. grub_errno = GRUB_ERR_NONE;
  133. usbms->luns = 1;
  134. }
  135. else
  136. usbms->luns = luns;
  137. /* XXX: Check the magic values, does this really make
  138. sense? */
  139. grub_usb_control_msg (usbdev, (1 << 6) | 1, 255,
  140. 0, i, 0, 0);
  141. /* XXX: To make Qemu work? */
  142. if (usbms->luns == 0)
  143. usbms->luns = 1;
  144. usbms->next = grub_usbms_dev_list;
  145. grub_usbms_dev_list = usbms;
  146. /* XXX: Activate the first configuration. */
  147. grub_usb_set_configuration (usbdev, 1);
  148. /* Bulk-Only Mass Storage Reset, after the reset commands
  149. will be accepted. */
  150. grub_usbms_reset (usbdev, i);
  151. return 0;
  152. }
  153. return 0;
  154. }
  155. static void
  156. grub_usbms_finddevs (void)
  157. {
  158. grub_usb_iterate (usb_iterate, 0);
  159. }
  160. static int
  161. grub_usbms_iterate (int (*hook) (const char *name, int luns, void *closure),
  162. void *closure)
  163. {
  164. grub_usbms_dev_t p;
  165. int cnt = 0;
  166. for (p = grub_usbms_dev_list; p; p = p->next)
  167. {
  168. char *devname;
  169. devname = grub_xasprintf ("usb%d", cnt);
  170. if (hook (devname, p->luns, closure))
  171. {
  172. grub_free (devname);
  173. return 1;
  174. }
  175. grub_free (devname);
  176. cnt++;
  177. }
  178. return 0;
  179. }
  180. static grub_err_t
  181. grub_usbms_transfer (struct grub_scsi *scsi, grub_size_t cmdsize, char *cmd,
  182. grub_size_t size, char *buf, int read_write)
  183. {
  184. struct grub_usbms_cbw cbw;
  185. grub_usbms_dev_t dev = (grub_usbms_dev_t) scsi->data;
  186. struct grub_usbms_csw status;
  187. static grub_uint32_t tag = 0;
  188. grub_usb_err_t err = GRUB_USB_ERR_NONE;
  189. int retrycnt = 3 + 1;
  190. retry:
  191. retrycnt--;
  192. if (retrycnt == 0)
  193. return grub_error (GRUB_ERR_IO, "USB Mass Storage stalled");
  194. /* Setup the request. */
  195. grub_memset (&cbw, 0, sizeof (cbw));
  196. cbw.signature = grub_cpu_to_le32 (0x43425355);
  197. cbw.tag = tag++;
  198. cbw.transfer_length = grub_cpu_to_le32 (size);
  199. cbw.flags = (!read_write) << GRUB_USBMS_DIRECTION_BIT;
  200. cbw.lun = scsi->lun << GRUB_SCSI_LUN_SHIFT;
  201. cbw.length = cmdsize;
  202. grub_memcpy (cbw.cbwcb, cmd, cmdsize);
  203. /* Write the request. */
  204. err = grub_usb_bulk_write (dev->dev, dev->out->endp_addr & 15,
  205. sizeof (cbw), (char *) &cbw);
  206. if (err)
  207. {
  208. if (err == GRUB_USB_ERR_STALL)
  209. {
  210. grub_usb_clear_halt (dev->dev, dev->out->endp_addr);
  211. goto retry;
  212. }
  213. return grub_error (GRUB_ERR_IO, "USB Mass Storage request failed");
  214. }
  215. /* Read/write the data. */
  216. if (read_write == 0)
  217. {
  218. err = grub_usb_bulk_read (dev->dev, dev->in->endp_addr & 15, size, buf);
  219. grub_dprintf ("usb", "read: %d %d\n", err, GRUB_USB_ERR_STALL);
  220. if (err)
  221. {
  222. if (err == GRUB_USB_ERR_STALL)
  223. {
  224. grub_usb_clear_halt (dev->dev, dev->in->endp_addr);
  225. goto retry;
  226. }
  227. return grub_error (GRUB_ERR_READ_ERROR,
  228. "can't read from USB Mass Storage device");
  229. }
  230. }
  231. else
  232. {
  233. err = grub_usb_bulk_write (dev->dev, dev->in->endp_addr & 15, size, buf);
  234. grub_dprintf ("usb", "write: %d %d\n", err, GRUB_USB_ERR_STALL);
  235. if (err)
  236. {
  237. if (err == GRUB_USB_ERR_STALL)
  238. {
  239. grub_usb_clear_halt (dev->dev, dev->out->endp_addr);
  240. goto retry;
  241. }
  242. return grub_error (GRUB_ERR_WRITE_ERROR,
  243. "can't write to USB Mass Storage device");
  244. }
  245. }
  246. /* Read the status. */
  247. err = grub_usb_bulk_read (dev->dev, dev->in->endp_addr & 15,
  248. sizeof (status), (char *) &status);
  249. if (err)
  250. {
  251. if (err == GRUB_USB_ERR_STALL)
  252. {
  253. grub_usb_clear_halt (dev->dev, dev->in->endp_addr);
  254. goto retry;
  255. }
  256. return grub_error (GRUB_ERR_READ_ERROR,
  257. "can't read status from USB Mass Storage device");
  258. }
  259. /* XXX: Magic and check this code. */
  260. if (status.status == 2)
  261. {
  262. /* XXX: Phase error, reset device. */
  263. grub_usbms_reset (dev->dev, dev->interface);
  264. grub_usb_clear_halt (dev->dev, dev->in->endp_addr);
  265. grub_usb_clear_halt (dev->dev, dev->out->endp_addr);
  266. goto retry;
  267. }
  268. if (status.status)
  269. return grub_error (GRUB_ERR_READ_ERROR,
  270. "error communication with USB Mass Storage device");
  271. return GRUB_ERR_NONE;
  272. }
  273. static grub_err_t
  274. grub_usbms_read (struct grub_scsi *scsi, grub_size_t cmdsize, char *cmd,
  275. grub_size_t size, char *buf)
  276. {
  277. return grub_usbms_transfer (scsi, cmdsize, cmd, size, buf, 0);
  278. }
  279. static grub_err_t
  280. grub_usbms_write (struct grub_scsi *scsi, grub_size_t cmdsize, char *cmd,
  281. grub_size_t size, char *buf)
  282. {
  283. return grub_usbms_transfer (scsi, cmdsize, cmd, size, buf, 1);
  284. }
  285. static grub_err_t
  286. grub_usbms_open (const char *name, struct grub_scsi *scsi)
  287. {
  288. grub_usbms_dev_t p;
  289. int devnum;
  290. int i = 0;
  291. if (grub_strncmp (name, "usb", 3))
  292. return grub_error (GRUB_ERR_UNKNOWN_DEVICE,
  293. "not a USB Mass Storage device");
  294. devnum = grub_strtoul (name + 3, NULL, 10);
  295. for (p = grub_usbms_dev_list; p; p = p->next)
  296. {
  297. /* Check if this is the devnumth device. */
  298. if (devnum == i)
  299. {
  300. scsi->data = p;
  301. scsi->name = grub_strdup (name);
  302. scsi->luns = p->luns;
  303. if (! scsi->name)
  304. return grub_errno;
  305. return GRUB_ERR_NONE;
  306. }
  307. i++;
  308. }
  309. return grub_error (GRUB_ERR_UNKNOWN_DEVICE,
  310. "not a USB Mass Storage device");
  311. }
  312. static void
  313. grub_usbms_close (struct grub_scsi *scsi)
  314. {
  315. grub_free (scsi->name);
  316. }
  317. static struct grub_scsi_dev grub_usbms_dev =
  318. {
  319. .name = "usb",
  320. .iterate = grub_usbms_iterate,
  321. .open = grub_usbms_open,
  322. .close = grub_usbms_close,
  323. .read = grub_usbms_read,
  324. .write = grub_usbms_write
  325. };
  326. GRUB_MOD_INIT(usbms)
  327. {
  328. grub_usbms_finddevs ();
  329. grub_scsi_dev_register (&grub_usbms_dev);
  330. }
  331. GRUB_MOD_FINI(usbms)
  332. {
  333. grub_scsi_dev_unregister (&grub_usbms_dev);
  334. }