IDEDiskDevice.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. #include "IDEDiskDevice.h"
  2. #include "types.h"
  3. #include "Process.h"
  4. #include "StdLib.h"
  5. #include "IO.h"
  6. #include "Scheduler.h"
  7. #include "PIC.h"
  8. #include <AK/Lock.h>
  9. //#define DISK_DEBUG
  10. #define IRQ_FIXED_DISK 14
  11. #define IDE0_DATA 0x1F0
  12. #define IDE0_STATUS 0x1F7
  13. #define IDE0_COMMAND 0x1F7
  14. enum IDECommand : byte {
  15. IDENTIFY_DRIVE = 0xEC,
  16. READ_SECTORS = 0x21,
  17. WRITE_SECTORS = 0x30,
  18. };
  19. enum IDEStatus : byte {
  20. BUSY = (1 << 7),
  21. DRDY = (1 << 6),
  22. DF = (1 << 5),
  23. SRV = (1 << 4),
  24. DRQ = (1 << 3),
  25. CORR = (1 << 2),
  26. IDX = (1 << 1),
  27. ERR = (1 << 0),
  28. };
  29. RetainPtr<IDEDiskDevice> IDEDiskDevice::create()
  30. {
  31. return adopt(*new IDEDiskDevice);
  32. }
  33. IDEDiskDevice::IDEDiskDevice()
  34. : IRQHandler(IRQ_FIXED_DISK)
  35. {
  36. initialize();
  37. }
  38. IDEDiskDevice::~IDEDiskDevice()
  39. {
  40. }
  41. const char* IDEDiskDevice::class_name() const
  42. {
  43. return "IDEDiskDevice";
  44. }
  45. unsigned IDEDiskDevice::block_size() const
  46. {
  47. return 512;
  48. }
  49. bool IDEDiskDevice::read_block(unsigned index, byte* out) const
  50. {
  51. const_cast<IDEDiskDevice&>(*this).read_sectors(index, 1, out);
  52. return true;
  53. }
  54. bool IDEDiskDevice::write_block(unsigned index, const byte* data)
  55. {
  56. write_sectors(index, 1, data);
  57. return true;
  58. }
  59. #ifdef DISK_DEBUG
  60. static void print_ide_status(byte status)
  61. {
  62. kprintf("DRQ=%u BUSY=%u DRDY=%u SRV=%u DF=%u CORR=%u IDX=%u ERR=%u\n",
  63. (status & DRQ) != 0,
  64. (status & BUSY) != 0,
  65. (status & DRDY) != 0,
  66. (status & SRV) != 0,
  67. (status & DF) != 0,
  68. (status & CORR) != 0,
  69. (status & IDX) != 0,
  70. (status & ERR) != 0);
  71. }
  72. #endif
  73. bool IDEDiskDevice::wait_for_irq()
  74. {
  75. #ifdef DISK_DEBUG
  76. kprintf("disk: waiting for interrupt...\n");
  77. #endif
  78. // FIXME: Add timeout.
  79. while (!m_interrupted) {
  80. // FIXME: Put this process into a Blocked state instead, it's stupid to wake up just to check a flag.
  81. Scheduler::yield();
  82. }
  83. #ifdef DISK_DEBUG
  84. kprintf("disk: got interrupt!\n");
  85. #endif
  86. return true;
  87. }
  88. void IDEDiskDevice::handle_irq()
  89. {
  90. #ifdef DISK_DEBUG
  91. byte status = IO::in8(0x1f7);
  92. kprintf("disk:interrupt: DRQ=%u BUSY=%u DRDY=%u\n", (status & DRQ) != 0, (status & BUSY) != 0, (status & DRDY) != 0);
  93. #endif
  94. m_interrupted = true;
  95. }
  96. void IDEDiskDevice::initialize()
  97. {
  98. byte status;
  99. status = IO::in8(IDE0_STATUS);
  100. #ifdef DISK_DEBUG
  101. kprintf("initial status: ");
  102. print_ide_status(status);
  103. #endif
  104. m_interrupted = false;
  105. while (IO::in8(IDE0_STATUS) & BUSY);
  106. enable_irq();
  107. IO::out8(0x1F6, 0xA0); // 0xB0 for 2nd device
  108. IO::out8(0x3F6, 0xA0); // 0xB0 for 2nd device
  109. IO::out8(IDE0_COMMAND, IDENTIFY_DRIVE);
  110. enable_irq();
  111. wait_for_irq();
  112. ByteBuffer wbuf = ByteBuffer::create_uninitialized(512);
  113. ByteBuffer bbuf = ByteBuffer::create_uninitialized(512);
  114. byte* b = bbuf.pointer();
  115. word* w = (word*)wbuf.pointer();
  116. const word* wbufbase = (word*)wbuf.pointer();
  117. for (dword i = 0; i < 256; ++i) {
  118. word data = IO::in16(IDE0_DATA);
  119. *(w++) = data;
  120. *(b++) = MSB(data);
  121. *(b++) = LSB(data);
  122. }
  123. // "Unpad" the device name string.
  124. for (dword i = 93; i > 54 && bbuf[i] == ' '; --i)
  125. bbuf[i] = 0;
  126. m_cylinders = wbufbase[1];
  127. m_heads = wbufbase[3];
  128. m_sectors_per_track = wbufbase[6];
  129. kprintf(
  130. "ide0: Master=\"%s\", C/H/Spt=%u/%u/%u\n",
  131. bbuf.pointer() + 54,
  132. m_cylinders,
  133. m_heads,
  134. m_sectors_per_track
  135. );
  136. }
  137. IDEDiskDevice::CHS IDEDiskDevice::lba_to_chs(dword lba) const
  138. {
  139. CHS chs;
  140. chs.cylinder = lba / (m_sectors_per_track * m_heads);
  141. chs.head = (lba / m_sectors_per_track) % m_heads;
  142. chs.sector = (lba % m_sectors_per_track) + 1;
  143. return chs;
  144. }
  145. bool IDEDiskDevice::read_sectors(dword start_sector, word count, byte* outbuf)
  146. {
  147. LOCKER(m_lock);
  148. #ifdef DISK_DEBUG
  149. kprintf("%s: Disk::read_sectors request (%u sector(s) @ %u)\n",
  150. current->name().characters(),
  151. count,
  152. start_sector);
  153. #endif
  154. disable_irq();
  155. auto chs = lba_to_chs(start_sector);
  156. while (IO::in8(IDE0_STATUS) & BUSY);
  157. #ifdef DISK_DEBUG
  158. kprintf("ide0: Reading %u sector(s) @ LBA %u (%u/%u/%u)\n", count, start_sector, chs.cylinder, chs.head, chs.sector);
  159. #endif
  160. IO::out8(0x1F2, count == 256 ? 0 : LSB(count));
  161. IO::out8(0x1F3, chs.sector);
  162. IO::out8(0x1F4, LSB(chs.cylinder));
  163. IO::out8(0x1F5, MSB(chs.cylinder));
  164. IO::out8(0x1F6, 0xA0 | chs.head); /* 0xB0 for 2nd device */
  165. IO::out8(0x3F6, 0x08);
  166. while (!(IO::in8(IDE0_STATUS) & DRDY));
  167. IO::out8(IDE0_COMMAND, READ_SECTORS);
  168. m_interrupted = false;
  169. enable_irq();
  170. wait_for_irq();
  171. byte status = IO::in8(0x1f7);
  172. if (status & DRQ) {
  173. #ifdef DISK_DEBUG
  174. kprintf("Retrieving %u bytes (status=%b), outbuf=%p...\n", count * 512, status, outbuf);
  175. #endif
  176. for (dword i = 0; i < (count * 512); i += 2) {
  177. word w = IO::in16(IDE0_DATA);
  178. outbuf[i] = LSB(w);
  179. outbuf[i+1] = MSB(w);
  180. }
  181. }
  182. return true;
  183. }
  184. bool IDEDiskDevice::write_sectors(dword start_sector, word count, const byte* data)
  185. {
  186. LOCKER(m_lock);
  187. #ifdef DISK_DEBUG
  188. dbgprintf("%s(%u): IDEDiskDevice::write_sectors request (%u sector(s) @ %u)\n",
  189. current->name().characters(),
  190. current->pid(),
  191. count,
  192. start_sector);
  193. #endif
  194. disable_irq();
  195. auto chs = lba_to_chs(start_sector);
  196. while (IO::in8(IDE0_STATUS) & BUSY);
  197. //dbgprintf("IDEDiskDevice: Writing %u sector(s) @ LBA %u (%u/%u/%u)\n", count, start_sector, chs.cylinder, chs.head, chs.sector);
  198. IO::out8(0x1F2, count == 256 ? 0 : LSB(count));
  199. IO::out8(0x1F3, chs.sector);
  200. IO::out8(0x1F4, LSB(chs.cylinder));
  201. IO::out8(0x1F5, MSB(chs.cylinder));
  202. IO::out8(0x1F6, 0xA0 | chs.head); /* 0xB0 for 2nd device */
  203. IO::out8(0x3F6, 0x08);
  204. IO::out8(IDE0_COMMAND, WRITE_SECTORS);
  205. while (!(IO::in8(IDE0_STATUS) & DRQ));
  206. byte status = IO::in8(0x1f7);
  207. if (status & DRQ) {
  208. //dbgprintf("Sending %u bytes (status=%b), data=%p...\n", count * 512, status, data);
  209. auto* data_as_words = (const word*)data;
  210. for (dword i = 0; i < (count * 512) / 2; ++i) {
  211. IO::out16(IDE0_DATA, data_as_words[i]);
  212. }
  213. }
  214. m_interrupted = false;
  215. enable_irq();
  216. wait_for_irq();
  217. return true;
  218. }