Guest User

Untitled

a guest
Jul 10th, 2014
353
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.64 KB | None | 0 0
  1. enum{
  2.    ATA_PRIMARY=0x1F0,
  3.    ATA_SECONDARY=0x170,
  4.    ATA_MASTER=0,
  5.    ATA_SLAVE=1<<4,
  6.  
  7.    ATA_DATA=0,
  8.    ATA_ERROR=1,
  9.    ATA_FEATURE=1,
  10.    ATA_SECTOR_COUNT=2,
  11.    ATA_LBA_LOW=3,
  12.    ATA_LBA_MID=4,
  13.    ATA_LBA_HIGH=5,
  14.    ATA_DEV=6,
  15.    ATA_STATUS=7,
  16.    ATA_COMMAND=7,
  17.    ATA_ALT_STATUS=0x206,
  18.    ATA_DEV_CTRL=0x206,
  19. };
  20.  
  21. static void ATA_delay(int bus, int delay){
  22.    for(;delay;delay--)
  23.       ASM_inb(bus+ATA_ALT_STATUS);
  24. }
  25.  
  26. static void ATA_select(int bus, int dev){
  27.    ASM_outb(bus+ATA_DEV, dev);
  28.    ATA_delay(bus, 4);
  29. }
  30.  
  31. static int ATA_waitDevice(int bus){
  32.    while((ASM_inb(bus+ATA_STATUS)&(1<<7))!=0);
  33.    uint8_t status;
  34.    while(true){
  35.       status=ASM_inb(bus+ATA_STATUS);
  36.       if((status&(1<<3))!=0){
  37.          return 1;
  38.       }else if((status&1)!=0){
  39.          return 0;
  40.       }
  41.    }
  42. }
  43.  
  44. static bool ATAPI_identify(int bus){
  45.    ASM_outb(bus+ATA_COMMAND, 0xA1);
  46.    if(ASM_inb(bus+ATA_STATUS)==0){
  47.       return false;
  48.    }
  49.    if(!ATA_waitDevice(bus)){
  50.       return false;
  51.    }
  52.    uint16_t buf[256];  
  53.    ASM_repInsw(bus+ATA_DATA, buf, 512);
  54.  
  55.    // Maybe we can do something here.
  56.    
  57.    return true;
  58. }
  59.  
  60. static bool ATAPI_readCapacity(int bus){
  61.    ASM_outb(bus+ATA_FEATURE, 0);  
  62.    ASM_outb(bus+ATA_DEV_CTRL, 2);
  63.    ASM_outb(bus+ATA_LBA_MID, 8&0xFF);
  64.    ASM_outb(bus+ATA_LBA_HIGH, 8>>8);  
  65.    ASM_outb(bus+ATA_COMMAND, 0xA0);
  66.    if(!ATA_waitDevice(bus)){
  67.       printf("Error happened when sent PACKET command");
  68.    }
  69.    
  70.    char packet[10]={0x25};
  71.    ASM_repOutsw(bus+ATA_DATA, packet, 12);//<-Notice here
  72.  
  73.    if(!ATA_waitDevice(bus)){
  74.       printf("Error happended when packet sent");
  75.       printf("(%x)", ASM_inb(bus+ATA_ERROR));
  76.       return;
  77.    }
  78.  
  79.    int count=ASM_inb(bus+ATA_LBA_MID)|(ASM_inb(bus+ATA_LBA_HIGH)<<8);
  80.    printf("%d", count);
  81.  
  82.    uint32_t buffer[2];
  83.    ASM_repInsw(bus+ATA_DATA, buffer, count);
  84.    printf("%x ", Util_endian(buffer[0]));//change endian
  85.    printf("%x", Util_endian(buffer[1]));
  86. }
  87.  
  88. void ATAPI_init(){
  89.    ATA_select(ATA_PRIMARY, ATA_MASTER);
  90.    if(ATAPI_identify(ATA_PRIMARY)){
  91.       printf("ATAPI: Primary Master");
  92.       ATAPI_readCapacity(ATA_PRIMARY);
  93.    }
  94.  
  95.    ATA_select(ATA_PRIMARY, ATA_SLAVE);
  96.    if(ATAPI_identify(ATA_PRIMARY)){
  97.       printf("ATAPI: Primary Slave");
  98.       ATAPI_readCapacity(ATA_PRIMARY);
  99.    }
  100.  
  101.    ATA_select(ATA_SECONDARY, ATA_MASTER);
  102.    if(ATAPI_identify(ATA_SECONDARY)){
  103.       printf("ATAPI: Secondary Master");
  104.       ATAPI_readCapacity(ATA_SECONDARY);
  105.    }
  106.  
  107.    ATA_select(ATA_SECONDARY, ATA_SLAVE);
  108.    if(ATAPI_identify(ATA_SECONDARY)){
  109.       printf("ATAPI: Secondary Slave");
  110.       ATAPI_readCapacity(ATA_SECONDARY);
  111.    }
  112.    
  113. }
Advertisement
Add Comment
Please, Sign In to add comment