public static void DetectATADrive(ATAControllerIdEnum aControllerID, ATABusPositionEnum aBusPosition) { bool primary = aControllerID == ATAControllerIdEnum.Primary; var xBAR0 = (ushort)(!primary ? 0x0170 : 0x01F0); var xBAR1 = (ushort)(!primary ? 0x0374 : 0x03F4); ushort IOControl = (ushort)(xBAR1 + 2); ushort IOCommand = (ushort)(xBAR0 + 7); ushort IOData = (ushort)xBAR0; ushort IOStatus = (ushort)(xBAR0 + 7); ushort IODeviceSelect = (ushort)(xBAR0 + 6); ushort IOLBA0 = (ushort)(xBAR0 + 3); ushort IOLBA1 = (ushort)(xBAR0 + 4); ushort IOLBA2 = (ushort)(xBAR0 + 5); ushort IOSectorCount = (ushort)(xBAR0 + 2); IOPort.outb(IOControl, 0x02); ATASpecLevel DriveType = DiscoverATADrive(IOLBA1, IOLBA2, IOCommand, IOStatus, IODeviceSelect, aBusPosition); if (DriveType == ATASpecLevel.Null) { return; } if (DriveType != ATASpecLevel.ATA) { return; } DetectedDiskDrives.Add(new ATADiskDrive(primary, aControllerID, aBusPosition, DriveType)); }
public ATADiskDrive(bool primary, ATAControllerIdEnum aControllerId, ATABusPositionEnum aBusPosition, ATASpecLevel mDriveType) { var xBAR0 = (ushort)(!primary ? 0x0170 : 0x01F0); var xBAR1 = (ushort)(!primary ? 0x0374 : 0x03F4); IOControl = (ushort)(xBAR1 + 2); IOCommand = (ushort)(xBAR0 + 7); IOData = (ushort)xBAR0; IOStatus = (ushort)(xBAR0 + 7); IODeviceSelect = (ushort)(xBAR0 + 6); IOLBA0 = (ushort)(xBAR0 + 3); IOLBA1 = (ushort)(xBAR0 + 4); IOLBA2 = (ushort)(xBAR0 + 5); IOSectorCount = (ushort)(xBAR0 + 2); mControllerID = aControllerId; mBusPosition = aBusPosition; DriveType = mDriveType; if (DriveType == ATASpecLevel.ATA) { SendATACmd(ATACmd.Identify, IOCommand, IOStatus); } else { SendATACmd(ATACmd.IdentifyPacket, IOCommand, IOStatus); } var xBuff = (ushort *)Heap.alloc(512); for (int i = 0; i < 256; i++) { ushort read = IOPort.inw(IOData); byte upper = (byte)(read >> 8); byte lower = (byte)read; xBuff[i] = (ushort)((lower << 8) | upper); } SerialNo = GetString(xBuff, 10, 20); FirmwareRev = GetString(xBuff, 23, 8); ModelNo = GetString(xBuff, 27, 40); DeviceLa = GetString(xBuff, 54, 40); uint l = 0; byte *ptr = DeviceLa; while (*ptr != 0) { ptr++; l++; } char[] tmp = new char[l]; for (int i = 0; i < l; i++) { tmp[i] = (char)DeviceLa[i]; } Label = new string(tmp); BlockCount = ((uint)xBuff[61] << 16 | xBuff[60]) - 1; LBA48Bit = (xBuff[83] & 0x40) != 0; if (LBA48Bit) { BlockCount = ((ulong)xBuff[102] << 32 | (ulong)xBuff[101] << 16 | (ulong)xBuff[100]) - 1; } byte *xMbrData = (byte *)Heap.alloc(512); ReadBlock(0, 1, xMbrData); MBR xMBR = new MBR(); xMBR.Setup(this, xMbrData); for (int i = 0; i < xMBR.partitions.Count; i++) { DetectedPartitions.Add(xMBR.partitions[i]); } }