Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- enum{
- ATA_PRIMARY=0x1F0,
- ATA_SECONDARY=0x170,
- ATA_MASTER=0,
- ATA_SLAVE=1<<4,
- ATA_DATA=0,
- ATA_ERROR=1,
- ATA_FEATURE=1,
- ATA_SECTOR_COUNT=2,
- ATA_LBA_LOW=3,
- ATA_LBA_MID=4,
- ATA_LBA_HIGH=5,
- ATA_DEV=6,
- ATA_STATUS=7,
- ATA_COMMAND=7,
- ATA_ALT_STATUS=0x206,
- ATA_DEV_CTRL=0x206,
- };
- static void ATA_delay(int bus, int delay){
- for(;delay;delay--)
- ASM_inb(bus+ATA_ALT_STATUS);
- }
- static void ATA_select(int bus, int dev){
- ASM_outb(bus+ATA_DEV, dev);
- ATA_delay(bus, 4);
- }
- static int ATA_waitDevice(int bus){
- while((ASM_inb(bus+ATA_STATUS)&(1<<7))!=0);
- uint8_t status;
- while(true){
- status=ASM_inb(bus+ATA_STATUS);
- if((status&(1<<3))!=0){
- return 1;
- }else if((status&1)!=0){
- return 0;
- }
- }
- }
- static bool ATAPI_identify(int bus){
- ASM_outb(bus+ATA_COMMAND, 0xA1);
- if(ASM_inb(bus+ATA_STATUS)==0){
- return false;
- }
- if(!ATA_waitDevice(bus)){
- return false;
- }
- uint16_t buf[256];
- ASM_repInsw(bus+ATA_DATA, buf, 512);
- // Maybe we can do something here.
- return true;
- }
- static bool ATAPI_readCapacity(int bus){
- ASM_outb(bus+ATA_FEATURE, 0);
- ASM_outb(bus+ATA_DEV_CTRL, 2);
- ASM_outb(bus+ATA_LBA_MID, 8&0xFF);
- ASM_outb(bus+ATA_LBA_HIGH, 8>>8);
- ASM_outb(bus+ATA_COMMAND, 0xA0);
- if(!ATA_waitDevice(bus)){
- printf("Error happened when sent PACKET command");
- }
- char packet[10]={0x25};
- ASM_repOutsw(bus+ATA_DATA, packet, 12);//<-Notice here
- if(!ATA_waitDevice(bus)){
- printf("Error happended when packet sent");
- printf("(%x)", ASM_inb(bus+ATA_ERROR));
- return;
- }
- int count=ASM_inb(bus+ATA_LBA_MID)|(ASM_inb(bus+ATA_LBA_HIGH)<<8);
- printf("%d", count);
- uint32_t buffer[2];
- ASM_repInsw(bus+ATA_DATA, buffer, count);
- printf("%x ", Util_endian(buffer[0]));//change endian
- printf("%x", Util_endian(buffer[1]));
- }
- void ATAPI_init(){
- ATA_select(ATA_PRIMARY, ATA_MASTER);
- if(ATAPI_identify(ATA_PRIMARY)){
- printf("ATAPI: Primary Master");
- ATAPI_readCapacity(ATA_PRIMARY);
- }
- ATA_select(ATA_PRIMARY, ATA_SLAVE);
- if(ATAPI_identify(ATA_PRIMARY)){
- printf("ATAPI: Primary Slave");
- ATAPI_readCapacity(ATA_PRIMARY);
- }
- ATA_select(ATA_SECONDARY, ATA_MASTER);
- if(ATAPI_identify(ATA_SECONDARY)){
- printf("ATAPI: Secondary Master");
- ATAPI_readCapacity(ATA_SECONDARY);
- }
- ATA_select(ATA_SECONDARY, ATA_SLAVE);
- if(ATAPI_identify(ATA_SECONDARY)){
- printf("ATAPI: Secondary Slave");
- ATAPI_readCapacity(ATA_SECONDARY);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment