首页
社区
课程
招聘
[求助]硬盘扇区读写不了。麻烦呀。
发表于: 2010-12-28 23:25 4845

[求助]硬盘扇区读写不了。麻烦呀。

2010-12-28 23:25
4845
/*
小弟在做LBA 48bit 硬盘PIO方式读数据时。一直没有读到数据。而28bit PIO完全没有问题。
请教各位。下列代码出错在哪里。(DMA 方式读写48bit LBA没问题)。请大家帮帮呀。
*/

extern "C"
{
#include <ntddk.h>
}

/* _Wait... */
void sleep(ULONG mSecond)
{
  LARGE_INTEGER timeout;
  timeout = RtlConvertLongToLargeInteger(-10*1000*mSecond);
  KeDelayExecutionThread(KernelMode,false,&timeout);
}

/* _Read Sector
*uLba - 扇区逻辑地址 48bit.
*uCount - 要读的扇区数
*buffer - 数据缓冲区
 */
void Lba48Read(ULONGLONG uLba,USHORT uCounts,PUCHAR buffer)
{
  WRITE_PORT_UCHAR((PUCHAR)0x1f1,0);
  WRITE_PORT_UCHAR((PUCHAR)0x1f2,uCounts >> 8);
  WRITE_PORT_UCHAR((PUCHAR)0x1f3,uLba >> 24);
  WRITE_PORT_UCHAR((PUCHAR)0x1f4,uLba >> 32);
  WRITE_PORT_UCHAR((PUCHAR)0x1f5,uLba >> 40);
  
  WRITE_PORT_UCHAR((PUCHAR)0x1f1,0);
  WRITE_PORT_UCHAR((PUCHAR)0x1f2,uCounts);
  WRITE_PORT_UCHAR((PUCHAR)0x1f3,uLba);
  WRITE_PORT_UCHAR((PUCHAR)0x1f4,uLba >> 8);
  WRITE_PORT_UCHAR((PUCHAR)0x1f5,uLba >> 16);

  WRITE_PORT_UCHAR((PUCHAR)0x1f6,0x40);
  WRITE_PORT_UCHAR((PUCHAR)0x1f7,0x24);

  sleep(5000);

  // (status = 0x50 && Error = 0x0)
  KdPrint(("status = 0x%02x\n",READ_PORT_UCHAR((PUCHAR)0x3f6)));
  KdPrint(("Error = 0x%02x\n",READ_PORT_UCHAR((PUCHAR)0x1f1)));

  while (uCounts)
  {
    // 出错。读还到数据。死循环
     while((READ_PORT_UCHAR((PUCHAR)0x1f7)&0xf)!=0x08)
     {
       sleep(5);
    }

    // (status = 0x50 && Error = 0x0)但读不到数据。
    READ_PORT_BUFFER_USHORT((PUSHORT)0x1f0,(PUSHORT)buffer,256);

  buffer += 512 ;
  uCounts--;
  }
}

void UnLoad(PDRIVER_OBJECT pDrvObj)
{
  KdPrint(("UnLoad OK!\n"));
}

NTSTATUS DriverEntry(PDRIVER_OBJECT pDrvObj,PUNICODE_STRING pReg)
{
  UCHAR szText[512] = {0};
  Lba48Read(0x888888,1,szText);

  for (USHORT i = 0; i < 512; i ++)
  {
    KdPrint(("%02x ",szText[i]));
    if (i % 16 == 15) KdPrint((" --%d\n",i / 16 +1));
  }

  KdPrint(("Load OK!\n"));
  pDrvObj->DriverUnload = UnLoad;
  return STATUS_SUCCESS;
}

[课程]FART 脱壳王!加量不加价!FART作者讲授!

收藏
免费 0
支持
分享
最新回复 (3)
雪    币: 134
活跃值: (91)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
2
1F1H  错误寄存器(只读寄存器)  特征寄存器
2010-12-31 09:18
0
雪    币: 214
活跃值: (11)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
3
去掉1f1也不行呀。
这里有介绍。但是按他方法不行
http://wiki.osdev.org/ATA_PIO_Mode

48 bit PIO
Reading sectors using 48 bit PIO is very similar to the 28 bit method:

(Notes: A sector count of 0 means 65536 sectors = 32MB. Try not to send bytes to the same IO port twice in a row. Doing so is much slower than doing two outb() commands to different IO ports. The important thing is that the high byte of the sector count, and LBA bytes 4, 5, & 6 go to their respective ports before the low bytes.)

Assume you have a sectorcount word and a 6 byte LBA value. Mentally number the LBA bytes as 1 to 6, from low to high. Send the 2 byte sector count to port 0x1F2 (high byte first), and the six LBA byte pairs to ports 0x1F3 through 0x1F5 in some appropriate order.

An example:

Send 0x40 for the "master" or 0x50 for the "slave" to port 0x1F6: outb(0x1F6, 0x40 | (slavebit << 4))
outb (0x1F2, sectorcount high byte)
outb (0x1F3, LBA4)
outb (0x1F4, LBA5)
outb (0x1F5, LBA6)
outb (0x1F2, sectorcount low byte)
outb (0x1F3, LBA1)
outb (0x1F4, LBA2)
outb (0x1F5, LBA3)
Send the "READ SECTORS EXT" command (0x24) to port 0x1F7: outb(0x1F7, 0x24)
Note on the "magic bits" sent to port 0x1f6: Bit 6 (value = 0x40) is the LBA bit. This must be set for either LBA28 or LBA48 transfers. It must be clear for CHS transfers. Any drive that can support LBA48 will ignore all other bits on this port for an LBA48 command. You can set them if it will make your code cleaner (to use the same magic bits as LBA28).

To write sectors in 48 bit PIO mode, send command "WRITE SECTORS EXT" (0x34), instead. (As before, do not use REP OUTSW when writing.) And remember to do a Cache Flush after each write command completes.

After the command byte is sent, transfer each sector of data in exactly the same way as for a 28 bit PIO Read/Write command.

请高手帮帮呀。。
2011-1-1 17:27
0
雪    币: 43
活跃值: (40)
能力值: ( LV3,RANK:20 )
在线值:
发帖
回帖
粉丝
4
我也在做LBA48,但也不行,而且把虚拟机给搞崩溃了。
坐等高人。
2011-1-1 20:37
0
游客
登录 | 注册 方可回帖
返回
//