harddisk.c

来自「参照MINIX3写的操作系统 用GCC+NASM+BOCHS开发」· C语言 代码 · 共 216 行

C
216
字号
#include "../include/toyos.h"#include"harddisk.h"
PRIVATE void ReadFromPort(unsigned char *pbuffer)
{
#if 0
	printk(" enter ReadFromPort \n");
#endif
	BatchRead(0x1F0, pbuffer, 512/2);
	return;
}


PUBLIC void readSector(int sector, unsigned char *pbuffer)
{
#define IDE_READ_COMMAND 0x20
	int c, h, s;
	unsigned char status;

	cli();
	s = sector & 0xFF;
	c = (sector & 0xFFFF00) >> 8;
	h = (sector & 0xF000000) >> 24;

	reading = 1;
	while (inb(STATUS_REG) & 0x80)
		;
#if 0
	printk("pass STATUS_REG ! \n");
#endif
	outb(1, 0x1F2); /* the number of sectors to read */
	outb(sector, 0x1F3); /* the no of the sector to read */
	outb(c, 0x1F4); /* low 8 bits of cylinder */
	outb(c >> 8, 0x1F5); /* high 2 bits of cylinder */
	outb(0xE0 | (0 << 4) | h, 0x1F6); /* d765 = 101 , ecc, 512B/SECTOR */

	outb(IDE_READ_COMMAND, 0x1F7);

	while ((status = inb(STATUS_REG)) & 0x80)
		;
#if 0
	printk("pass inb(STATUS_REG)) IS BUSY ? \n");
#endif

	ReadFromPort(pbuffer);
	sti();
	return;
}

PRIVATE void WriteToPort(unsigned char *pBuffer)
{
	/* batch write to port */
	BatchWrite(0x1F0, pBuffer, SECTOR_SIZE/2 );
	return;
}

PUBLIC void writeSector(int sector, unsigned char *pBuffer)
{
#define IDE_WRITE_COMMAND 0x30
	int c, h, s;
#if 0
	unsigned char status;
#endif

	cli();
	s = sector & 0xFF;
	c = (sector & 0xFFFF00) >> 8;
	h = (sector & 0xF000000) >> 24;

	while(inb(STATUS_REG) & 0x80);
	/* printk("pass STATUS_REG ! \n"); */

	outb(1, 0x1F2); /* the number of sectors to read */
	outb(sector, 0x1F3); /* the no of the sector to read */
	outb(c, 0x1F4); /* low 8 bits of cylinder */
	outb(c>>8, 0x1F5); /* high 2 bits of cylinder */
	outb(0xE0 | (0 << 4) | h, 0x1F6); /* d765 = 101 , ecc, 512B/SECTOR */

	outb(IDE_WRITE_COMMAND, 0x1F7);

	if (inb(STATUS_REG & 0x01)) {
		printk(" issue write_command command error !\n");
		return;
	}
	/* wait for DRQ */
	while (!(inb(STATUS_REG) & 0x08))
		;

#if 0
	printk("pass while(!(inb(STATUS_REG) & 0x08) \n");
#endif

	/* ok , let's pass data */
	WriteToPort(pBuffer);
	sti();
	return;
}


PRIVATE void GetHDInfo(void)
{
	/* to get some info , then print it */
	outb(0xA0, DRIVE_REG);
	outb(0xEC, STATUS_REG);

	while (inb(STATUS_REG)&0x80)
		;

	BatchRead(DATA_REG, &id, sizeof(id)/2);
}


PUBLIC void ideInit(void)
{
//	put_irq_handler(14, ide_interrupt);
	GetHDInfo();
	printk("****************IDE Init**************\n");
	printk("Your Hardisk type is : %s ,total sectors: %d\n", id.model,id.lba_capacity);
	//printk("Cycls: %d Heads :%d Sector: %d sector_bytes: %d\n",id.cyls,id.heads,id.sectors ,id.sector_bytes);
//	printk("cur_cyls: %d cur_heads: %d cur_sectors: %d cur_capacity0: %d\n",id.cur_cyls,id.cur_heads,id.cur_sectors,id.cur_capacity0);
	readSector(0, ReadBuffer);
	return;
}
PUBLIC void harddiskHandler(int Irq)
{ /* the main IDE handler */
	printk(" there is a ide interrupt occuring !\n");
	if (reading) {
    /* is reading sector */
		reading = 0; /* processed */
		ReadFromPort(ReadBuffer);
		printk("read from io port !\n");
	}
	return;
}

PUBLIC void Test(void)
{
	printk("begin HD test!\n");

	/* prepare write buffer */
	WriteBuffer[510] = 0x55;
	WriteBuffer[511] = 0xAA;

	readSector(0, ReadBuffer);

	if (ReadBuffer[510] == 0x55) {
		printk(" write command taste good! \n");
		return;
	} else {
		printk(" write or read fails !\n");
		return;
	}

	return;
}




PUBLIC void HDTask()
{
/*	t_8 *Data;
 	Message *pMess;
	Message Reply;
	ide_init();
	int i=0;
//	for(i=0;i<512;i++)
	//	Data[i]=i;
	 while(1)
		{
		
			pMess=ReceiveMessage(HD,NULL);
			switch(pMess->Type)
			{
			case READ: 
			
				   // printk("Read ");
					Data=(t_8 *)pMess->FSADDR;
					for(i=0;i<pMess->SECTORS;i++)
					{
						ReadSector(pMess->STARTSECTOR+i,Data);
						Data+=512;
					}
				
				    //printk("Read End ");
					Reply.Source=HD;
					SendMessage(FS,&Reply);
					ProcTable[HD].State=SLEEP;
					ChangeProcess();
				
			
			
				break;
			case WRITE:	
			   	 //   printk("Write ");
					Data=(t_8 *)pMess->FSADDR;
					for(i=0;i<pMess->SECTORS;i++)
					{
						WriteSector(pMess->STARTSECTOR+i, Data);
						Data+=512;
					}
	
					
					Reply.Source=HD;
					SendMessage(FS,&Reply);
					ProcTable[HD].State=SLEEP;
					ChangeProcess();
				break;
			default:   break;
			}
			
		}
*/
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?