Error using LBA 28

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
armizh
Posts: 3
Joined: Sun Jan 29, 2012 2:54 pm

Error using LBA 28

Post by armizh »

Hello guys, i'm new in this forum. I'm trying to implement ATA/ATAPI or IDE, and i wrote two functions and the polling function but them doesn't work yet :( Can you help me?
I'm not sure what's the problem but when I call the function with:

Code: Select all

ide_initialization(0x1F0,0x170,unused_ch,unused_ch);
ide_write((word *)3,1,1,1);
printk("%x\n",ide_read(1,1,1));
The output is: '2'
Thanks.
Another question: how i can debug my os?
PS: Sorry if my english isn't good, i'm chilean and i learn english yet. Thanks.
All code of the file ata.c:

Code: Select all

//Define los dispositivos maestros y esclavos, suponiendo su uso
//con el metodo de acceso LBA
#define MASTER			0x00
#define SLAVE			0x01

#define READ_SECTORS	0x20
#define WRITE_SECTORS	0x30

#define STATUS			0x0C
#define BSY				0x80

#define unused_ch		0

struct ide_device {
	byte Reserved;		//0 = Instalado 1 = No Instalado
	byte Channel;
	byte Drive;			//Indica si es maestro o esclavo
} ide_devices[4];

byte ide_polling(byte ch)
{
	int i;
	for(i = 0; i < 5; i++)
		inb(ide_devices[ch].Channel + STATUS - 0x0A);
	while (inb(ide_devices[ch].Channel + 0x07) >> 7);
}

//Por defecto, el canal 1 es 0x1F0 y el 2 es 0x170
//Los canales 3 y 4 generalmente no estan presentes, pero cuando estan,
//por lo general el 3 es 0x1E8 y el 4 es 0x168
void ide_initialization(word d1, word d2, word d3, word d4)
{
	int device_n = 0;
	if(d1!=unused_ch) {
		ide_devices[1].Reserved = 1;
		ide_devices[1].Channel = d1;
		ide_devices[1].Drive = MASTER;
		device_n++;
		ide_devices[2].Reserved = 1;
		ide_devices[2].Channel = d2;
		ide_devices[2].Drive = SLAVE;
		device_n++;
	} else if(d2==unused_ch) {
		ide_devices[1].Reserved = 0;
		ide_devices[2].Reserved = 0;
	}
	if(d2!=unused_ch) {
		ide_devices[3].Reserved = 1;
		ide_devices[3].Channel = d2;
		ide_devices[3].Drive = MASTER;
		device_n++;
		ide_devices[4].Reserved = 1;
		ide_devices[4].Channel = d2;
		ide_devices[4].Drive = SLAVE;
		device_n++;
	} else if(d3==unused_ch) {
		ide_devices[3].Reserved = 0;
		ide_devices[4].Reserved = 0;
	}
	printk("Se han inicializado %i dispositivos",device_n);
}

//En el argumento 'device' son validos los numeros 1, 2, 3 y 4.
//Cero provocara un error.
//LBA no debe superar 0xFFFFFFF debido a que estamos utilizando LBA 28
//el cual utiliza valores de hasta 28 bits.
word *ide_read(int device, word LBA, byte count)
{
	byte slavebit;
	word *buffer;
	if(device!=0) {
		if(ide_devices[device].Reserved!=0) {
			if((ide_devices[device].Channel+6)==MASTER) slavebit = 0;
			else if((ide_devices[device].Channel+6)==SLAVE) slavebit = 1;
			LBA = 0xFFFFFFF & LBA;
			outb((0xE0 | slavebit << 4) | ((LBA >> 24) & 0x0F),ide_devices[device].Channel+6);
			outb(0x00,ide_devices[device].Channel+1);
			outb(count,ide_devices[device].Channel+2);
			outb((byte)LBA,ide_devices[device].Channel+3);
			outb((byte)(LBA >> 8),ide_devices[device].Channel+4);
			outb((byte)(LBA >> 16),ide_devices[device].Channel+5);
			outb(READ_SECTORS,ide_devices[device].Channel+7);
			ide_polling(ide_devices[device].Channel);
			buffer=insw(ide_devices[device].Channel,count);
			ide_polling(ide_devices[device].Channel);
			return buffer;
		} else if(ide_devices[device].Reserved==0) {
			printk("Error: El dispositivo %i no ha sido inicializado y es probable que no este conectado\n",device);
		}
	} else if(device==0) printk("Error: El dispositivo 0 es NULL\n");
}

int ide_write(word *data, int device, word LBA, byte count)
{
	byte slavebit;
	byte n;
	if(device!=0) {
		if(ide_devices[device].Reserved!=0) {
			if((ide_devices[device].Channel+6)==MASTER) slavebit = 0;
			else if((ide_devices[device].Channel+6)==SLAVE) slavebit = 1;
			LBA = 0xFFFFFFF & LBA;
			outb((0xE0 | slavebit << 4) | ((LBA >> 24) & 0x0F),ide_devices[device].Channel+6);
			outb(0x00,ide_devices[device].Channel+1);
			outb(count,ide_devices[device].Channel+2);
			outb((byte)LBA,ide_devices[device].Channel+3);
			outb((byte)(LBA >> 8),ide_devices[device].Channel+4);
			outb((byte)(LBA >> 16),ide_devices[device].Channel+5);
			outb(WRITE_SECTORS,ide_devices[device].Channel+7);
			ide_polling(ide_devices[device].Channel);
			while(n!=count) {
				outw(*data,ide_devices[device].Channel);
				*data++;
				n++;
				asm(".intel_syntax noprefix");
				asm("jmp $+2");
				asm(".att_syntax prefix");
			}
			outb(0xE7,ide_devices[device].Channel+7);
		} else if(ide_devices[device].Reserved==0) {
			printk("Error: El dispositivo %i no ha sido inicializado y es probable que no este conectado\n",device);
		}
	} else if(device==0) printk("Error: El dispositivo 0 es NULL\n");
}
armizh
Posts: 3
Joined: Sun Jan 29, 2012 2:54 pm

Re: Error using LBA 28

Post by armizh »

Thank you so much. But now i have another question, ok i initialised the array buffer, but, i can use 'strcpy' to copy words? or i must create another function but specifical for words? Thanks
armizh
Posts: 3
Joined: Sun Jan 29, 2012 2:54 pm

Re: Error using LBA 28

Post by armizh »

Hey man, I know about memcpy but i ignored it, sorry for the problems. Don't say about my knowledge, please. I started my OS one month ago and i have achieved good things since i started the os. Only i had a question and i don't understand you perfectly, but now i understand you. Thank you and sorry. Maybe i don't meet required knowledge of the language.
Thanks.
Post Reply