ata.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  1. /* ata.c - ATA disk access. */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2007, 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/ata.h>
  20. #include <grub/dl.h>
  21. #include <grub/disk.h>
  22. #include <grub/mm.h>
  23. #include <grub/time.h>
  24. #include <grub/pci.h>
  25. #include <grub/scsi.h>
  26. GRUB_EXPORT(grub_ata_pio_read);
  27. GRUB_EXPORT(grub_ata_wait_not_busy);
  28. GRUB_EXPORT(grub_ata_wait_drq);
  29. /* At the moment, only two IDE ports are supported. */
  30. static const grub_port_t grub_ata_ioaddress[] = { 0x1f0, 0x170 };
  31. static const grub_port_t grub_ata_ioaddress2[] = { 0x3f6, 0x376 };
  32. static struct grub_ata_device *grub_ata_devices;
  33. /* Wait for !BSY. */
  34. grub_err_t
  35. grub_ata_wait_not_busy (struct grub_ata_device *dev, int milliseconds)
  36. {
  37. /* ATA requires 400ns (after a write to CMD register) or
  38. 1 PIO cycle (after a DRQ block transfer) before
  39. first check of BSY. */
  40. grub_millisleep (1);
  41. int i = 1;
  42. grub_uint8_t sts;
  43. while ((sts = grub_ata_regget (dev, GRUB_ATA_REG_STATUS))
  44. & GRUB_ATA_STATUS_BUSY)
  45. {
  46. if (i >= milliseconds)
  47. {
  48. grub_dprintf ("ata", "timeout: %dms, status=0x%x\n",
  49. milliseconds, sts);
  50. return grub_error (GRUB_ERR_TIMEOUT, "ATA timeout");
  51. }
  52. grub_millisleep (1);
  53. i++;
  54. }
  55. return GRUB_ERR_NONE;
  56. }
  57. static inline void
  58. grub_ata_wait (void)
  59. {
  60. grub_millisleep (50);
  61. }
  62. /* Wait for !BSY, DRQ. */
  63. grub_err_t
  64. grub_ata_wait_drq (struct grub_ata_device *dev, int rw,
  65. int milliseconds)
  66. {
  67. if (grub_ata_wait_not_busy (dev, milliseconds))
  68. return grub_errno;
  69. /* !DRQ implies error condition. */
  70. grub_uint8_t sts = grub_ata_regget (dev, GRUB_ATA_REG_STATUS);
  71. if ((sts & (GRUB_ATA_STATUS_DRQ | GRUB_ATA_STATUS_ERR))
  72. != GRUB_ATA_STATUS_DRQ)
  73. {
  74. grub_dprintf ("ata", "ata error: status=0x%x, error=0x%x\n",
  75. sts, grub_ata_regget (dev, GRUB_ATA_REG_ERROR));
  76. if (! rw)
  77. return grub_error (GRUB_ERR_READ_ERROR, "ATA read error");
  78. else
  79. return grub_error (GRUB_ERR_WRITE_ERROR, "ATA write error");
  80. }
  81. return GRUB_ERR_NONE;
  82. }
  83. /* Byteorder has to be changed before strings can be read. */
  84. static void
  85. grub_ata_strncpy (char *dst, char *src, grub_size_t len)
  86. {
  87. grub_uint16_t *src16 = (grub_uint16_t *) src;
  88. grub_uint16_t *dst16 = (grub_uint16_t *) dst;
  89. unsigned int i;
  90. for (i = 0; i < len / 2; i++)
  91. *(dst16++) = grub_be_to_cpu16 (*(src16++));
  92. dst[len] = '\0';
  93. }
  94. void
  95. grub_ata_pio_read (struct grub_ata_device *dev, char *buf, grub_size_t size)
  96. {
  97. grub_uint16_t *buf16 = (grub_uint16_t *) buf;
  98. unsigned int i;
  99. /* Read in the data, word by word. */
  100. for (i = 0; i < size / 2; i++)
  101. buf16[i] = grub_le_to_cpu16 (grub_inw(dev->ioaddress + GRUB_ATA_REG_DATA));
  102. }
  103. static void
  104. grub_ata_pio_write (struct grub_ata_device *dev, char *buf, grub_size_t size)
  105. {
  106. grub_uint16_t *buf16 = (grub_uint16_t *) buf;
  107. unsigned int i;
  108. /* Write the data, word by word. */
  109. for (i = 0; i < size / 2; i++)
  110. grub_outw(grub_cpu_to_le16 (buf16[i]), dev->ioaddress + GRUB_ATA_REG_DATA);
  111. }
  112. static void
  113. grub_ata_dumpinfo (struct grub_ata_device *dev, char *info)
  114. {
  115. char text[41];
  116. /* The device information was read, dump it for debugging. */
  117. grub_ata_strncpy (text, info + 20, 20);
  118. grub_dprintf ("ata", "Serial: %s\n", text);
  119. grub_ata_strncpy (text, info + 46, 8);
  120. grub_dprintf ("ata", "Firmware: %s\n", text);
  121. grub_ata_strncpy (text, info + 54, 40);
  122. grub_dprintf ("ata", "Model: %s\n", text);
  123. if (! dev->atapi)
  124. {
  125. grub_dprintf ("ata", "Addressing: %d\n", dev->addr);
  126. grub_dprintf ("ata", "Sectors: %lld\n", (unsigned long long) dev->size);
  127. }
  128. }
  129. static grub_err_t
  130. grub_atapi_identify (struct grub_ata_device *dev)
  131. {
  132. char *info;
  133. info = grub_malloc (GRUB_DISK_SECTOR_SIZE);
  134. if (! info)
  135. return grub_errno;
  136. grub_ata_regset (dev, GRUB_ATA_REG_DISK, 0xE0 | dev->device << 4);
  137. grub_ata_wait ();
  138. if (grub_ata_check_ready (dev))
  139. {
  140. grub_free (info);
  141. return grub_errno;
  142. }
  143. grub_ata_regset (dev, GRUB_ATA_REG_CMD, GRUB_ATA_CMD_IDENTIFY_PACKET_DEVICE);
  144. grub_ata_wait ();
  145. if (grub_ata_wait_drq (dev, 0, GRUB_ATA_TOUT_STD))
  146. {
  147. grub_free (info);
  148. return grub_errno;
  149. }
  150. grub_ata_pio_read (dev, info, GRUB_DISK_SECTOR_SIZE);
  151. dev->atapi = 1;
  152. grub_ata_dumpinfo (dev, info);
  153. grub_free (info);
  154. return GRUB_ERR_NONE;
  155. }
  156. static grub_err_t
  157. grub_atapi_wait_drq (struct grub_ata_device *dev,
  158. grub_uint8_t ireason,
  159. int milliseconds)
  160. {
  161. /* Wait for !BSY, DRQ, ireason */
  162. if (grub_ata_wait_not_busy (dev, milliseconds))
  163. return grub_errno;
  164. grub_uint8_t sts = grub_ata_regget (dev, GRUB_ATA_REG_STATUS);
  165. grub_uint8_t irs = grub_ata_regget (dev, GRUB_ATAPI_REG_IREASON);
  166. /* OK if DRQ is asserted and interrupt reason is as expected. */
  167. if ((sts & GRUB_ATA_STATUS_DRQ)
  168. && (irs & GRUB_ATAPI_IREASON_MASK) == ireason)
  169. return GRUB_ERR_NONE;
  170. /* !DRQ implies error condition. */
  171. grub_dprintf ("ata", "atapi error: status=0x%x, ireason=0x%x, error=0x%x\n",
  172. sts, irs, grub_ata_regget (dev, GRUB_ATA_REG_ERROR));
  173. if (! (sts & GRUB_ATA_STATUS_DRQ)
  174. && (irs & GRUB_ATAPI_IREASON_MASK) == GRUB_ATAPI_IREASON_ERROR)
  175. {
  176. if (ireason == GRUB_ATAPI_IREASON_CMD_OUT)
  177. return grub_error (GRUB_ERR_READ_ERROR, "ATA PACKET command error");
  178. else
  179. return grub_error (GRUB_ERR_READ_ERROR, "ATAPI read error");
  180. }
  181. return grub_error (GRUB_ERR_READ_ERROR, "ATAPI protocol error");
  182. }
  183. static grub_err_t
  184. grub_atapi_packet (struct grub_ata_device *dev, char *packet,
  185. grub_size_t size)
  186. {
  187. grub_ata_regset (dev, GRUB_ATA_REG_DISK, dev->device << 4);
  188. if (grub_ata_check_ready (dev))
  189. return grub_errno;
  190. /* Send ATA PACKET command. */
  191. grub_ata_regset (dev, GRUB_ATA_REG_FEATURES, 0);
  192. grub_ata_regset (dev, GRUB_ATAPI_REG_IREASON, 0);
  193. grub_ata_regset (dev, GRUB_ATAPI_REG_CNTHIGH, size >> 8);
  194. grub_ata_regset (dev, GRUB_ATAPI_REG_CNTLOW, size & 0xFF);
  195. grub_ata_regset (dev, GRUB_ATA_REG_CMD, GRUB_ATA_CMD_PACKET);
  196. /* Wait for !BSY, DRQ, !I/O, C/D. */
  197. if (grub_atapi_wait_drq (dev, GRUB_ATAPI_IREASON_CMD_OUT, GRUB_ATA_TOUT_STD))
  198. return grub_errno;
  199. /* Write the packet. */
  200. grub_ata_pio_write (dev, packet, 12);
  201. return GRUB_ERR_NONE;
  202. }
  203. static grub_err_t
  204. grub_ata_identify (struct grub_ata_device *dev)
  205. {
  206. char *info;
  207. grub_uint16_t *info16;
  208. info = grub_malloc (GRUB_DISK_SECTOR_SIZE);
  209. if (! info)
  210. return grub_errno;
  211. info16 = (grub_uint16_t *) info;
  212. grub_ata_regset (dev, GRUB_ATA_REG_DISK, 0xE0 | dev->device << 4);
  213. grub_ata_wait ();
  214. if (grub_ata_check_ready (dev))
  215. {
  216. grub_free (info);
  217. return grub_errno;
  218. }
  219. grub_ata_regset (dev, GRUB_ATA_REG_CMD, GRUB_ATA_CMD_IDENTIFY_DEVICE);
  220. grub_ata_wait ();
  221. if (grub_ata_wait_drq (dev, 0, GRUB_ATA_TOUT_STD))
  222. {
  223. grub_free (info);
  224. grub_errno = GRUB_ERR_NONE;
  225. grub_uint8_t sts = grub_ata_regget (dev, GRUB_ATA_REG_STATUS);
  226. if ((sts & (GRUB_ATA_STATUS_BUSY | GRUB_ATA_STATUS_DRQ
  227. | GRUB_ATA_STATUS_ERR)) == GRUB_ATA_STATUS_ERR
  228. && (grub_ata_regget (dev, GRUB_ATA_REG_ERROR) & 0x04 /* ABRT */))
  229. /* Device without ATA IDENTIFY, try ATAPI. */
  230. return grub_atapi_identify (dev);
  231. else if (sts == 0x00)
  232. /* No device, return error but don't print message. */
  233. return GRUB_ERR_UNKNOWN_DEVICE;
  234. else
  235. /* Other Error. */
  236. return grub_error (GRUB_ERR_UNKNOWN_DEVICE,
  237. "device cannot be identified");
  238. }
  239. grub_ata_pio_read (dev, info, GRUB_DISK_SECTOR_SIZE);
  240. /* Re-check status to avoid bogus identify data due to stuck DRQ. */
  241. grub_uint8_t sts = grub_ata_regget (dev, GRUB_ATA_REG_STATUS);
  242. if (sts & (GRUB_ATA_STATUS_BUSY | GRUB_ATA_STATUS_DRQ | GRUB_ATA_STATUS_ERR))
  243. {
  244. grub_dprintf ("ata", "bad status=0x%x\n", sts);
  245. grub_free (info);
  246. /* No device, return error but don't print message. */
  247. grub_errno = GRUB_ERR_NONE;
  248. return GRUB_ERR_UNKNOWN_DEVICE;
  249. }
  250. /* Now it is certain that this is not an ATAPI device. */
  251. dev->atapi = 0;
  252. /* CHS is always supported. */
  253. dev->addr = GRUB_ATA_CHS;
  254. /* Check if LBA is supported. */
  255. if (info16[49] & (1 << 9))
  256. {
  257. /* Check if LBA48 is supported. */
  258. if (info16[83] & (1 << 10))
  259. dev->addr = GRUB_ATA_LBA48;
  260. else
  261. dev->addr = GRUB_ATA_LBA;
  262. }
  263. /* Determine the amount of sectors. */
  264. if (dev->addr != GRUB_ATA_LBA48)
  265. dev->size = grub_le_to_cpu32(*((grub_uint32_t *) &info16[60]));
  266. else
  267. dev->size = grub_le_to_cpu64(*((grub_uint64_t *) &info16[100]));
  268. /* Read CHS information. */
  269. dev->cylinders = info16[1];
  270. dev->heads = info16[3];
  271. dev->sectors_per_track = info16[6];
  272. grub_ata_dumpinfo (dev, info);
  273. grub_free(info);
  274. return 0;
  275. }
  276. static grub_err_t
  277. grub_ata_device_initialize (int port, int device, int addr, int addr2)
  278. {
  279. struct grub_ata_device *dev;
  280. struct grub_ata_device **devp;
  281. grub_dprintf ("ata", "detecting device %d,%d (0x%x, 0x%x)\n",
  282. port, device, addr, addr2);
  283. dev = grub_malloc (sizeof(*dev));
  284. if (! dev)
  285. return grub_errno;
  286. /* Setup the device information. */
  287. dev->port = port;
  288. dev->device = device;
  289. dev->ioaddress = addr + GRUB_MACHINE_PCI_IO_BASE;
  290. dev->ioaddress2 = addr2 + GRUB_MACHINE_PCI_IO_BASE;
  291. dev->next = NULL;
  292. grub_ata_regset (dev, GRUB_ATA_REG_DISK, dev->device << 4);
  293. grub_ata_wait ();
  294. /* Try to detect if the port is in use by writing to it,
  295. waiting for a while and reading it again. If the value
  296. was preserved, there is a device connected. */
  297. grub_ata_regset (dev, GRUB_ATA_REG_SECTORS, 0x5A);
  298. grub_ata_wait ();
  299. grub_uint8_t sec = grub_ata_regget (dev, GRUB_ATA_REG_SECTORS);
  300. grub_dprintf ("ata", "sectors=0x%x\n", sec);
  301. if (sec != 0x5A)
  302. {
  303. grub_free(dev);
  304. return 0;
  305. }
  306. /* The above test may detect a second (slave) device
  307. connected to a SATA controller which supports only one
  308. (master) device. It is not safe to use the status register
  309. READY bit to check for controller channel existence. Some
  310. ATAPI commands (RESET, DIAGNOSTIC) may clear this bit. */
  311. /* Use the IDENTIFY DEVICE command to query the device. */
  312. if (grub_ata_identify (dev))
  313. {
  314. grub_free (dev);
  315. return 0;
  316. }
  317. /* Register the device. */
  318. for (devp = &grub_ata_devices; *devp; devp = &(*devp)->next);
  319. *devp = dev;
  320. return 0;
  321. }
  322. static int
  323. grub_ata_pciinit (grub_pci_device_t dev, grub_pci_id_t pciid,
  324. void *closure __attribute__((unused)))
  325. {
  326. static int compat_use[2] = { 0 };
  327. grub_pci_address_t addr;
  328. grub_uint32_t class;
  329. grub_uint32_t bar1;
  330. grub_uint32_t bar2;
  331. int rega;
  332. int regb;
  333. int i;
  334. static int controller = 0;
  335. int cs5536 = 0;
  336. int nports = 2;
  337. /* Read class. */
  338. addr = grub_pci_make_address (dev, GRUB_PCI_REG_CLASS);
  339. class = grub_pci_read (addr);
  340. /* AMD CS5536 Southbridge. */
  341. if (pciid == 0x208f1022)
  342. {
  343. cs5536 = 1;
  344. nports = 1;
  345. }
  346. /* Check if this class ID matches that of a PCI IDE Controller. */
  347. if (!cs5536 && (class >> 16 != 0x0101))
  348. return 0;
  349. for (i = 0; i < nports; i++)
  350. {
  351. /* Set to 0 when the channel operated in compatibility mode. */
  352. int compat;
  353. /* We don't support non-compatibility mode for CS5536. */
  354. if (cs5536)
  355. compat = 0;
  356. else
  357. compat = (class >> (8 + 2 * i)) & 1;
  358. rega = 0;
  359. regb = 0;
  360. /* If the channel is in compatibility mode, just assign the
  361. default registers. */
  362. if (compat == 0 && !compat_use[i])
  363. {
  364. rega = grub_ata_ioaddress[i];
  365. regb = grub_ata_ioaddress2[i];
  366. compat_use[i] = 1;
  367. }
  368. else if (compat)
  369. {
  370. /* Read the BARs, which either contain a mmapped IO address
  371. or the IO port address. */
  372. addr = grub_pci_make_address (dev, GRUB_PCI_REG_ADDRESSES
  373. + sizeof (grub_uint64_t) * i);
  374. bar1 = grub_pci_read (addr);
  375. addr = grub_pci_make_address (dev, GRUB_PCI_REG_ADDRESSES
  376. + sizeof (grub_uint64_t) * i
  377. + sizeof (grub_uint32_t));
  378. bar2 = grub_pci_read (addr);
  379. /* Check if the BARs describe an IO region. */
  380. if ((bar1 & 1) && (bar2 & 1))
  381. {
  382. rega = bar1 & ~3;
  383. regb = bar2 & ~3;
  384. }
  385. }
  386. grub_dprintf ("ata",
  387. "PCI dev (%d,%d,%d) compat=%d rega=0x%x regb=0x%x\n",
  388. grub_pci_get_bus (dev), grub_pci_get_device (dev),
  389. grub_pci_get_function (dev), compat, rega, regb);
  390. if (rega && regb)
  391. {
  392. grub_errno = GRUB_ERR_NONE;
  393. grub_ata_device_initialize (controller * 2 + i, 0, rega, regb);
  394. /* Most errors raised by grub_ata_device_initialize() are harmless.
  395. They just indicate this particular drive is not responding, most
  396. likely because it doesn't exist. We might want to ignore specific
  397. error types here, instead of printing them. */
  398. if (grub_errno)
  399. {
  400. grub_print_error ();
  401. grub_errno = GRUB_ERR_NONE;
  402. }
  403. grub_ata_device_initialize (controller * 2 + i, 1, rega, regb);
  404. /* Likewise. */
  405. if (grub_errno)
  406. {
  407. grub_print_error ();
  408. grub_errno = GRUB_ERR_NONE;
  409. }
  410. }
  411. }
  412. controller++;
  413. return 0;
  414. }
  415. static grub_err_t
  416. grub_ata_initialize (void)
  417. {
  418. grub_pci_iterate (grub_ata_pciinit, 0);
  419. return 0;
  420. }
  421. static void
  422. grub_ata_setlba (struct grub_ata_device *dev, grub_disk_addr_t sector,
  423. grub_size_t size)
  424. {
  425. grub_ata_regset (dev, GRUB_ATA_REG_SECTORS, size);
  426. grub_ata_regset (dev, GRUB_ATA_REG_LBALOW, sector & 0xFF);
  427. grub_ata_regset (dev, GRUB_ATA_REG_LBAMID, (sector >> 8) & 0xFF);
  428. grub_ata_regset (dev, GRUB_ATA_REG_LBAHIGH, (sector >> 16) & 0xFF);
  429. }
  430. static grub_err_t
  431. grub_ata_setaddress (struct grub_ata_device *dev,
  432. grub_ata_addressing_t addressing,
  433. grub_disk_addr_t sector,
  434. grub_size_t size)
  435. {
  436. switch (addressing)
  437. {
  438. case GRUB_ATA_CHS:
  439. {
  440. unsigned int cylinder;
  441. unsigned int head;
  442. unsigned int sect;
  443. /* Calculate the sector, cylinder and head to use. */
  444. sect = ((grub_uint32_t) sector % dev->sectors_per_track) + 1;
  445. cylinder = (((grub_uint32_t) sector / dev->sectors_per_track)
  446. / dev->heads);
  447. head = ((grub_uint32_t) sector / dev->sectors_per_track) % dev->heads;
  448. if (sect > dev->sectors_per_track
  449. || cylinder > dev->cylinders
  450. || head > dev->heads)
  451. return grub_error (GRUB_ERR_OUT_OF_RANGE,
  452. "sector %d cannot be addressed "
  453. "using CHS addressing", sector);
  454. grub_ata_regset (dev, GRUB_ATA_REG_DISK, (dev->device << 4) | head);
  455. if (grub_ata_check_ready (dev))
  456. return grub_errno;
  457. grub_ata_regset (dev, GRUB_ATA_REG_SECTNUM, sect);
  458. grub_ata_regset (dev, GRUB_ATA_REG_CYLLSB, cylinder & 0xFF);
  459. grub_ata_regset (dev, GRUB_ATA_REG_CYLMSB, cylinder >> 8);
  460. break;
  461. }
  462. case GRUB_ATA_LBA:
  463. if (size == 256)
  464. size = 0;
  465. grub_ata_regset (dev, GRUB_ATA_REG_DISK,
  466. 0xE0 | (dev->device << 4) | ((sector >> 24) & 0x0F));
  467. if (grub_ata_check_ready (dev))
  468. return grub_errno;
  469. grub_ata_setlba (dev, sector, size);
  470. break;
  471. case GRUB_ATA_LBA48:
  472. if (size == 65536)
  473. size = 0;
  474. grub_ata_regset (dev, GRUB_ATA_REG_DISK, 0xE0 | (dev->device << 4));
  475. if (grub_ata_check_ready (dev))
  476. return grub_errno;
  477. /* Set "Previous". */
  478. grub_ata_setlba (dev, sector >> 24, size >> 8);
  479. /* Set "Current". */
  480. grub_ata_setlba (dev, sector, size);
  481. break;
  482. }
  483. return GRUB_ERR_NONE;
  484. }
  485. static grub_err_t
  486. grub_ata_readwrite (grub_disk_t disk, grub_disk_addr_t sector,
  487. grub_size_t size, char *buf, int rw)
  488. {
  489. struct grub_ata_device *dev = (struct grub_ata_device *) disk->data;
  490. grub_dprintf("ata", "grub_ata_readwrite (size=%llu, rw=%d)\n", (unsigned long long) size, rw);
  491. grub_ata_addressing_t addressing = dev->addr;
  492. grub_size_t batch;
  493. int cmd, cmd_write;
  494. if (addressing == GRUB_ATA_LBA48 && ((sector + size) >> 28) != 0)
  495. {
  496. batch = 65536;
  497. cmd = GRUB_ATA_CMD_READ_SECTORS_EXT;
  498. cmd_write = GRUB_ATA_CMD_WRITE_SECTORS_EXT;
  499. }
  500. else
  501. {
  502. if (addressing == GRUB_ATA_LBA48)
  503. addressing = GRUB_ATA_LBA;
  504. batch = 256;
  505. cmd = GRUB_ATA_CMD_READ_SECTORS;
  506. cmd_write = GRUB_ATA_CMD_WRITE_SECTORS;
  507. }
  508. grub_size_t nsectors = 0;
  509. while (nsectors < size)
  510. {
  511. if (size - nsectors < batch)
  512. batch = size - nsectors;
  513. grub_dprintf("ata", "rw=%d, sector=%llu, batch=%llu\n", rw, (unsigned long long) sector, (unsigned long long) batch);
  514. /* Send read/write command. */
  515. if (grub_ata_setaddress (dev, addressing, sector, batch))
  516. return grub_errno;
  517. grub_ata_regset (dev, GRUB_ATA_REG_CMD, (! rw ? cmd : cmd_write));
  518. unsigned sect;
  519. for (sect = 0; sect < batch; sect++)
  520. {
  521. /* Wait for !BSY, DRQ. */
  522. if (grub_ata_wait_drq (dev, rw, GRUB_ATA_TOUT_DATA))
  523. return grub_errno;
  524. /* Transfer data. */
  525. if (! rw)
  526. grub_ata_pio_read (dev, buf, GRUB_DISK_SECTOR_SIZE);
  527. else
  528. grub_ata_pio_write (dev, buf, GRUB_DISK_SECTOR_SIZE);
  529. buf += GRUB_DISK_SECTOR_SIZE;
  530. }
  531. if (rw)
  532. {
  533. /* Check for write error. */
  534. if (grub_ata_wait_not_busy (dev, GRUB_ATA_TOUT_DATA))
  535. return grub_errno;
  536. if (grub_ata_regget (dev, GRUB_ATA_REG_STATUS)
  537. & (GRUB_ATA_STATUS_DRQ | GRUB_ATA_STATUS_ERR))
  538. return grub_error (GRUB_ERR_WRITE_ERROR, "ATA write error");
  539. }
  540. sector += batch;
  541. nsectors += batch;
  542. }
  543. return GRUB_ERR_NONE;
  544. }
  545. static int
  546. grub_ata_iterate (int (*hook) (const char *name, void *closure),
  547. void *closure)
  548. {
  549. struct grub_ata_device *dev;
  550. for (dev = grub_ata_devices; dev; dev = dev->next)
  551. {
  552. char devname[10];
  553. if (dev->atapi)
  554. continue;
  555. grub_snprintf (devname, sizeof (devname),
  556. "ata%d", dev->port * 2 + dev->device);
  557. if (hook (devname, closure))
  558. return 1;
  559. }
  560. return 0;
  561. }
  562. static grub_err_t
  563. grub_ata_open (const char *name, grub_disk_t disk)
  564. {
  565. struct grub_ata_device *dev;
  566. for (dev = grub_ata_devices; dev; dev = dev->next)
  567. {
  568. char devname[10];
  569. grub_snprintf (devname, sizeof (devname),
  570. "ata%d", dev->port * 2 + dev->device);
  571. if (grub_strcmp (name, devname) == 0)
  572. break;
  573. }
  574. if (! dev)
  575. return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "can't open device");
  576. if (dev->atapi)
  577. return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "not an ATA harddisk");
  578. disk->total_sectors = dev->size;
  579. disk->id = (unsigned long) dev;
  580. disk->has_partitions = 1;
  581. disk->data = dev;
  582. return 0;
  583. }
  584. static void
  585. grub_ata_close (grub_disk_t disk __attribute__((unused)))
  586. {
  587. }
  588. static grub_err_t
  589. grub_ata_read (grub_disk_t disk, grub_disk_addr_t sector,
  590. grub_size_t size, char *buf)
  591. {
  592. return grub_ata_readwrite (disk, sector, size, buf, 0);
  593. }
  594. static grub_err_t
  595. grub_ata_write (grub_disk_t disk,
  596. grub_disk_addr_t sector,
  597. grub_size_t size,
  598. const char *buf)
  599. {
  600. return grub_ata_readwrite (disk, sector, size, (char *) buf, 1);
  601. }
  602. static struct grub_disk_dev grub_atadisk_dev =
  603. {
  604. .name = "ATA",
  605. .id = GRUB_DISK_DEVICE_ATA_ID,
  606. .iterate = grub_ata_iterate,
  607. .open = grub_ata_open,
  608. .close = grub_ata_close,
  609. .read = grub_ata_read,
  610. .write = grub_ata_write,
  611. .next = 0
  612. };
  613. /* ATAPI code. */
  614. static int
  615. grub_atapi_iterate (int (*hook) (const char *name, int luns, void *closure),
  616. void *closure)
  617. {
  618. struct grub_ata_device *dev;
  619. for (dev = grub_ata_devices; dev; dev = dev->next)
  620. {
  621. char devname[10];
  622. grub_snprintf (devname, sizeof (devname),
  623. "ata%d", dev->port * 2 + dev->device);
  624. if (! dev->atapi)
  625. continue;
  626. if (hook (devname, 1, closure))
  627. return 1;
  628. }
  629. return 0;
  630. }
  631. static grub_err_t
  632. grub_atapi_read (struct grub_scsi *scsi,
  633. grub_size_t cmdsize __attribute__((unused)),
  634. char *cmd, grub_size_t size, char *buf)
  635. {
  636. struct grub_ata_device *dev = (struct grub_ata_device *) scsi->data;
  637. grub_dprintf("ata", "grub_atapi_read (size=%llu)\n", (unsigned long long) size);
  638. if (grub_atapi_packet (dev, cmd, size))
  639. return grub_errno;
  640. grub_size_t nread = 0;
  641. while (nread < size)
  642. {
  643. /* Wait for !BSY, DRQ, I/O, !C/D. */
  644. if (grub_atapi_wait_drq (dev, GRUB_ATAPI_IREASON_DATA_IN, GRUB_ATA_TOUT_DATA))
  645. return grub_errno;
  646. /* Get byte count for this DRQ assertion. */
  647. unsigned cnt = grub_ata_regget (dev, GRUB_ATAPI_REG_CNTHIGH) << 8
  648. | grub_ata_regget (dev, GRUB_ATAPI_REG_CNTLOW);
  649. grub_dprintf("ata", "DRQ count=%u\n", cnt);
  650. /* Count of last transfer may be uneven. */
  651. if (! (0 < cnt && cnt <= size - nread && (! (cnt & 1) || cnt == size - nread)))
  652. return grub_error (GRUB_ERR_READ_ERROR, "invalid ATAPI transfer count");
  653. /* Read the data. */
  654. grub_ata_pio_read (dev, buf + nread, cnt);
  655. if (cnt & 1)
  656. buf[nread + cnt - 1] = (char) grub_le_to_cpu16 (grub_inw (dev->ioaddress + GRUB_ATA_REG_DATA));
  657. nread += cnt;
  658. }
  659. return GRUB_ERR_NONE;
  660. }
  661. static grub_err_t
  662. grub_atapi_write (struct grub_scsi *scsi __attribute__((unused)),
  663. grub_size_t cmdsize __attribute__((unused)),
  664. char *cmd __attribute__((unused)),
  665. grub_size_t size __attribute__((unused)),
  666. char *buf __attribute__((unused)))
  667. {
  668. // XXX: scsi.mod does not use write yet.
  669. return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET, "ATAPI write not implemented");
  670. }
  671. static grub_err_t
  672. grub_atapi_open (const char *name, struct grub_scsi *scsi)
  673. {
  674. struct grub_ata_device *dev;
  675. struct grub_ata_device *devfnd = 0;
  676. for (dev = grub_ata_devices; dev; dev = dev->next)
  677. {
  678. char devname[10];
  679. grub_snprintf (devname, sizeof (devname),
  680. "ata%d", dev->port * 2 + dev->device);
  681. if (!grub_strcmp (devname, name))
  682. {
  683. devfnd = dev;
  684. break;
  685. }
  686. }
  687. grub_dprintf ("ata", "opening ATAPI dev `%s'\n", name);
  688. if (! devfnd)
  689. return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "no such ATAPI device");
  690. scsi->data = devfnd;
  691. return GRUB_ERR_NONE;
  692. }
  693. static void
  694. grub_atapi_close (struct grub_scsi *scsi)
  695. {
  696. grub_free (scsi->name);
  697. }
  698. static struct grub_scsi_dev grub_atapi_dev =
  699. {
  700. .name = "ATAPI",
  701. .iterate = grub_atapi_iterate,
  702. .open = grub_atapi_open,
  703. .close = grub_atapi_close,
  704. .read = grub_atapi_read,
  705. .write = grub_atapi_write
  706. };
  707. GRUB_MOD_INIT(ata)
  708. {
  709. /* To prevent two drivers operating on the same disks. */
  710. grub_disk_firmware_is_tainted = 1;
  711. if (grub_disk_firmware_fini)
  712. {
  713. grub_disk_firmware_fini ();
  714. grub_disk_firmware_fini = NULL;
  715. }
  716. /* ATA initialization. */
  717. grub_ata_initialize ();
  718. grub_disk_dev_register (&grub_atadisk_dev);
  719. /* ATAPI devices are handled by scsi.mod. */
  720. grub_scsi_dev_register (&grub_atapi_dev);
  721. }
  722. GRUB_MOD_FINI(ata)
  723. {
  724. grub_scsi_dev_unregister (&grub_atapi_dev);
  725. grub_disk_dev_unregister (&grub_atadisk_dev);
  726. }