scsi.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /* scsi.c - scsi support. */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2008,2009 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/disk.h>
  20. #include <grub/dl.h>
  21. #include <grub/kernel.h>
  22. #include <grub/misc.h>
  23. #include <grub/mm.h>
  24. #include <grub/types.h>
  25. #include <grub/scsi.h>
  26. #include <grub/scsicmd.h>
  27. GRUB_EXPORT(grub_scsi_dev_register);
  28. GRUB_EXPORT(grub_scsi_dev_unregister);
  29. static grub_scsi_dev_t grub_scsi_dev_list;
  30. void
  31. grub_scsi_dev_register (grub_scsi_dev_t dev)
  32. {
  33. dev->next = grub_scsi_dev_list;
  34. grub_scsi_dev_list = dev;
  35. }
  36. void
  37. grub_scsi_dev_unregister (grub_scsi_dev_t dev)
  38. {
  39. grub_scsi_dev_t *p, q;
  40. for (p = &grub_scsi_dev_list, q = *p; q; p = &(q->next), q = q->next)
  41. if (q == dev)
  42. {
  43. *p = q->next;
  44. break;
  45. }
  46. }
  47. /* Determine the the device is removable and the type of the device
  48. SCSI. */
  49. static grub_err_t
  50. grub_scsi_inquiry (grub_scsi_t scsi)
  51. {
  52. struct grub_scsi_inquiry iq;
  53. struct grub_scsi_inquiry_data iqd;
  54. grub_err_t err;
  55. iq.opcode = grub_scsi_cmd_inquiry;
  56. iq.lun = scsi->lun << GRUB_SCSI_LUN_SHIFT;
  57. iq.reserved = 0;
  58. iq.alloc_length = 0x24; /* XXX: Hardcoded for now */
  59. iq.reserved2 = 0;
  60. err = scsi->dev->read (scsi, sizeof (iq), (char *) &iq,
  61. sizeof (iqd), (char *) &iqd);
  62. if (err)
  63. return err;
  64. scsi->devtype = iqd.devtype & GRUB_SCSI_DEVTYPE_MASK;
  65. scsi->removable = iqd.rmb >> GRUB_SCSI_REMOVABLE_BIT;
  66. return GRUB_ERR_NONE;
  67. }
  68. /* Read the capacity and block size of SCSI. */
  69. static grub_err_t
  70. grub_scsi_read_capacity (grub_scsi_t scsi)
  71. {
  72. struct grub_scsi_read_capacity rc;
  73. struct grub_scsi_read_capacity_data rcd;
  74. grub_err_t err;
  75. rc.opcode = grub_scsi_cmd_read_capacity;
  76. rc.lun = scsi->lun << GRUB_SCSI_LUN_SHIFT;
  77. grub_memset (rc.reserved, 0, sizeof (rc.reserved));
  78. err = scsi->dev->read (scsi, sizeof (rc), (char *) &rc,
  79. sizeof (rcd), (char *) &rcd);
  80. if (err)
  81. return err;
  82. scsi->size = grub_be_to_cpu32 (rcd.size);
  83. scsi->blocksize = grub_be_to_cpu32 (rcd.blocksize);
  84. return GRUB_ERR_NONE;
  85. }
  86. /* Send a SCSI request for DISK: read SIZE sectors starting with
  87. sector SECTOR to BUF. */
  88. static grub_err_t
  89. grub_scsi_read10 (grub_disk_t disk, grub_disk_addr_t sector,
  90. grub_size_t size, char *buf)
  91. {
  92. grub_scsi_t scsi;
  93. struct grub_scsi_read10 rd;
  94. scsi = disk->data;
  95. rd.opcode = grub_scsi_cmd_read10;
  96. rd.lun = scsi->lun << GRUB_SCSI_LUN_SHIFT;
  97. rd.lba = grub_cpu_to_be32 (sector);
  98. rd.reserved = 0;
  99. rd.size = grub_cpu_to_be16 (size);
  100. rd.reserved2 = 0;
  101. rd.pad = 0;
  102. return scsi->dev->read (scsi, sizeof (rd), (char *) &rd, size * scsi->blocksize, buf);
  103. }
  104. /* Send a SCSI request for DISK: read SIZE sectors starting with
  105. sector SECTOR to BUF. */
  106. static grub_err_t
  107. grub_scsi_read12 (grub_disk_t disk, grub_disk_addr_t sector,
  108. grub_size_t size, char *buf)
  109. {
  110. grub_scsi_t scsi;
  111. struct grub_scsi_read12 rd;
  112. scsi = disk->data;
  113. rd.opcode = grub_scsi_cmd_read12;
  114. rd.lun = scsi->lun << GRUB_SCSI_LUN_SHIFT;
  115. rd.lba = grub_cpu_to_be32 (sector);
  116. rd.size = grub_cpu_to_be32 (size);
  117. rd.reserved = 0;
  118. rd.control = 0;
  119. return scsi->dev->read (scsi, sizeof (rd), (char *) &rd, size * scsi->blocksize, buf);
  120. }
  121. #if 0
  122. /* Send a SCSI request for DISK: write the data stored in BUF to SIZE
  123. sectors starting with SECTOR. */
  124. static grub_err_t
  125. grub_scsi_write10 (grub_disk_t disk, grub_disk_addr_t sector,
  126. grub_size_t size, char *buf)
  127. {
  128. grub_scsi_t scsi;
  129. struct grub_scsi_write10 wr;
  130. scsi = disk->data;
  131. wr.opcode = grub_scsi_cmd_write10;
  132. wr.lun = scsi->lun << GRUB_SCSI_LUN_SHIFT;
  133. wr.lba = grub_cpu_to_be32 (sector);
  134. wr.reserved = 0;
  135. wr.size = grub_cpu_to_be16 (size);
  136. wr.reserved2 = 0;
  137. wr.pad = 0;
  138. return scsi->dev->write (scsi, sizeof (wr), (char *) &wr, size * scsi->blocksize, buf);
  139. }
  140. /* Send a SCSI request for DISK: write the data stored in BUF to SIZE
  141. sectors starting with SECTOR. */
  142. static grub_err_t
  143. grub_scsi_write12 (grub_disk_t disk, grub_disk_addr_t sector,
  144. grub_size_t size, char *buf)
  145. {
  146. grub_scsi_t scsi;
  147. struct grub_scsi_write10 wr;
  148. scsi = disk->data;
  149. wr.opcode = grub_scsi_cmd_write12;
  150. wr.lun = scsi->lun << GRUB_SCSI_LUN_SHIFT;
  151. wr.lba = grub_cpu_to_be32 (sector);
  152. wr.size = grub_cpu_to_be32 (size);
  153. wr.reserved = 0;
  154. wr.pad = 0;
  155. return scsi->dev->write (scsi, sizeof (wr), (char *) &wr, size * scsi->blocksize, buf);
  156. }
  157. #endif
  158. struct grub_scsi_iterate_closure
  159. {
  160. int (*hook) (const char *name, void *closure);
  161. void *closure;
  162. };
  163. static int
  164. scsi_iterate (const char *name, int luns, void *closure)
  165. {
  166. struct grub_scsi_iterate_closure *c = closure;
  167. int i;
  168. /* In case of a single LUN, just return `usbX'. */
  169. if (luns == 1)
  170. return c->hook (name, c->closure);
  171. /* In case of multiple LUNs, every LUN will get a prefix to
  172. distinguish it. */
  173. for (i = 0; i < luns; i++)
  174. {
  175. char *sname;
  176. int ret;
  177. sname = grub_xasprintf ("%s%c", name, 'a' + i);
  178. if (!sname)
  179. return 1;
  180. ret = c->hook (sname, c->closure);
  181. grub_free (sname);
  182. if (ret)
  183. return 1;
  184. }
  185. return 0;
  186. }
  187. static int
  188. grub_scsi_iterate (int (*hook) (const char *name, void *closure),
  189. void *closure)
  190. {
  191. grub_scsi_dev_t p;
  192. struct grub_scsi_iterate_closure c;
  193. c.hook = hook;
  194. c.closure = closure;
  195. for (p = grub_scsi_dev_list; p; p = p->next)
  196. if (p->iterate && (p->iterate) (scsi_iterate, &c))
  197. return 1;
  198. return 0;
  199. }
  200. static grub_err_t
  201. grub_scsi_open (const char *name, grub_disk_t disk)
  202. {
  203. grub_scsi_dev_t p;
  204. grub_scsi_t scsi;
  205. grub_err_t err;
  206. int len;
  207. int lun;
  208. scsi = grub_malloc (sizeof (*scsi));
  209. if (! scsi)
  210. return grub_errno;
  211. len = grub_strlen (name);
  212. lun = name[len - 1] - 'a';
  213. /* Try to detect a LUN ('a'-'z'), otherwise just use the first
  214. LUN. */
  215. if (lun < 0 || lun > 26)
  216. lun = 0;
  217. for (p = grub_scsi_dev_list; p; p = p->next)
  218. {
  219. if (p->open (name, scsi))
  220. continue;
  221. disk->id = (unsigned long) "scsi"; /* XXX */
  222. disk->data = scsi;
  223. scsi->dev = p;
  224. scsi->lun = lun;
  225. scsi->name = grub_strdup (name);
  226. if (! scsi->name)
  227. {
  228. grub_free (scsi);
  229. return grub_errno;
  230. }
  231. grub_dprintf ("scsi", "dev opened\n");
  232. err = grub_scsi_inquiry (scsi);
  233. if (err)
  234. {
  235. grub_free (scsi);
  236. grub_dprintf ("scsi", "inquiry failed\n");
  237. return err;
  238. }
  239. grub_dprintf ("scsi", "inquiry: devtype=0x%02x removable=%d\n",
  240. scsi->devtype, scsi->removable);
  241. /* Try to be conservative about the device types
  242. supported. */
  243. if (scsi->devtype != grub_scsi_devtype_direct
  244. && scsi->devtype != grub_scsi_devtype_cdrom)
  245. {
  246. grub_free (scsi);
  247. return grub_error (GRUB_ERR_UNKNOWN_DEVICE,
  248. "unknown SCSI device");
  249. }
  250. if (scsi->devtype == grub_scsi_devtype_cdrom)
  251. disk->has_partitions = 0;
  252. else
  253. disk->has_partitions = 1;
  254. err = grub_scsi_read_capacity (scsi);
  255. if (err)
  256. {
  257. grub_free (scsi);
  258. grub_dprintf ("scsi", "READ CAPACITY failed\n");
  259. return err;
  260. }
  261. /* SCSI blocks can be something else than 512, although GRUB
  262. wants 512 byte blocks. */
  263. disk->total_sectors = ((scsi->size * scsi->blocksize)
  264. << GRUB_DISK_SECTOR_BITS);
  265. grub_dprintf ("scsi", "capacity=%llu, blksize=%d\n",
  266. (unsigned long long) disk->total_sectors,
  267. scsi->blocksize);
  268. return GRUB_ERR_NONE;
  269. }
  270. grub_free (scsi);
  271. return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "not a SCSI disk");
  272. }
  273. static void
  274. grub_scsi_close (grub_disk_t disk)
  275. {
  276. grub_scsi_t scsi;
  277. scsi = disk->data;
  278. scsi->dev->close (scsi);
  279. grub_free (scsi);
  280. }
  281. static grub_err_t
  282. grub_scsi_read (grub_disk_t disk, grub_disk_addr_t sector,
  283. grub_size_t size, char *buf)
  284. {
  285. grub_scsi_t scsi;
  286. scsi = disk->data;
  287. /* SCSI sectors are variable in size. GRUB uses 512 byte
  288. sectors. */
  289. if (scsi->blocksize != GRUB_DISK_SECTOR_SIZE)
  290. {
  291. unsigned spb = scsi->blocksize >> GRUB_DISK_SECTOR_BITS;
  292. if (! (spb != 0 && (scsi->blocksize & GRUB_DISK_SECTOR_SIZE) == 0))
  293. return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  294. "unsupported SCSI block size");
  295. grub_uint32_t sector_mod = 0;
  296. sector = grub_divmod64 (sector, spb, &sector_mod);
  297. if (! (sector_mod == 0 && size % spb == 0))
  298. return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  299. "unaligned SCSI read not supported");
  300. size /= spb;
  301. }
  302. /* Depending on the type, select a read function. */
  303. switch (scsi->devtype)
  304. {
  305. case grub_scsi_devtype_direct:
  306. return grub_scsi_read10 (disk, sector, size, buf);
  307. case grub_scsi_devtype_cdrom:
  308. return grub_scsi_read12 (disk, sector, size, buf);
  309. }
  310. /* XXX: Never reached. */
  311. return GRUB_ERR_NONE;
  312. }
  313. static grub_err_t
  314. grub_scsi_write (grub_disk_t disk __attribute((unused)),
  315. grub_disk_addr_t sector __attribute((unused)),
  316. grub_size_t size __attribute((unused)),
  317. const char *buf __attribute((unused)))
  318. {
  319. #if 0
  320. /* XXX: Not tested yet! */
  321. /* XXX: This should depend on the device type? */
  322. return grub_scsi_write10 (disk, sector, size, buf);
  323. #endif
  324. return GRUB_ERR_NOT_IMPLEMENTED_YET;
  325. }
  326. static struct grub_disk_dev grub_scsi_dev =
  327. {
  328. .name = "scsi",
  329. .id = GRUB_DISK_DEVICE_SCSI_ID,
  330. .iterate = grub_scsi_iterate,
  331. .open = grub_scsi_open,
  332. .close = grub_scsi_close,
  333. .read = grub_scsi_read,
  334. .write = grub_scsi_write,
  335. .next = 0
  336. };
  337. GRUB_MOD_INIT(scsi)
  338. {
  339. grub_disk_dev_register (&grub_scsi_dev);
  340. }
  341. GRUB_MOD_FINI(scsi)
  342. {
  343. grub_disk_dev_unregister (&grub_scsi_dev);
  344. }