⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ata.c

📁 c语言编的网络操作系统。具备网络操作系统基本功能。
💻 C
字号:
/*
nexOS: nexos generic ata driver - generic ata driver
Copyright 2004 nexOS development team

This file is part of nexOS.

nexOS is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.

nexOS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with nexOS; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/ 

#include "ata.h"
#include <types.h>
#include "../../../io/io.h"
#include <libnexos-module/nexos-module.h>

const WORD ata_ctrlport[] = { 0x1F0, 0x170 };
const WORD ata_drive[] = { 0xA0, 0xB0 };

void ata_loadsectors(ATA_DRIVE *drive, void *buf, int n)
{
    int dataoffset;
    WORD *data = buf;
    for (dataoffset = 0; dataoffset < n*256; dataoffset++)
    {
        data[dataoffset] = inw (ata_ctrlport[drive->controller]+ATA_DATA_REG);
    }
}    

unsigned int ata_irqhandler(struct trap_state_t *state)
{
    ata_irq[state->irqnum - 14] = 1;        
    return 0;
}

void ata_simplecmd(ATA_DRIVE *drive, BYTE cmd)
{
    int port;
    port = ata_ctrlport[drive->controller];
    port += ATA_CMD_REG;
    outb(port,cmd);
}    
void ata_command(ATA_DRIVE *drive, BYTE cmdseq[], int portmask)
{
    int i;
    for( i=1; i<8; i++ )
        if( portmask & 1<<i )
            outb(ata_ctrlport[drive->controller]+i,cmdseq[i]);
}
int ata_readblocks(ATA_DRIVE *drive, BLOCK block)
{
    int timeout;
    BYTE cmd[8];
    if( drive->caps[0] && ATA_CAPS_LBA )
    {
        /* drive supports LBA access */
        /* we support 28-bit LBA only for now */
        /* FIXME: support for 48-bit LBA */
        /* load data in cmd array */
        cmd[0] = 0;
        cmd[1] = 0;
        cmd[2] = block.len;
        cmd[3] = ((block.lownum    ) & 0xFF); /* bits 00-07 of LBA */
        cmd[4] = ((block.lownum>> 8) & 0xFF); /* bits 08-15 of LBA */
        cmd[5] = ((block.lownum>>16) & 0xFF); /* bits 16-23 of LBA */
        cmd[6] = ((block.lownum>>24) & 0xFF)| /* bits 24-27 of LBA */
                 (drive->drive<<4)|           /* drive selection */
                 (1<<6);                      /* we have an LBA address */
        cmd[7] = ATA_CMD_READSECTS;
 
        /* issue command */
        ata_command(drive,cmd,
                ATA_PMSK_SCNT|ATA_PMSK_LBAL|ATA_PMSK_LBAM|
                ATA_PMSK_LBAH|ATA_PMSK_DRVHD|ATA_PMSK_CMD);
        
        /* wait for irq */
        timeout = multitask_get_ticks() + 5000;
        while( multitask_get_ticks() < timeout && ata_irq[drive->controller] == 0 );
        if( ata_irq[drive->controller] )
        {
            /* drive has finished read, copy data over to the buf */
            ata_loadsectors(drive,block.buf,block.len);
            ata_irq[drive->controller] = 0;
            return ATA_ERR_NONE;
        }
        return ATA_ERR_TIMEOUT;
    }
    else
    {
        /* drive does not support LBA */
        /* FIXME: we really should include CHS access */
        return ATA_ERR_NOTSUPP;
    }
    return ATA_ERR_TIMEOUT;
}    
int ata_writeblocks(ATA_DRIVE *drive, BLOCK block)
{
    if( drive->caps[0] && ATA_CAPS_LBA )
    {
        /* drive supports LBA access */
        /* FIXME: we should include LBA access */
    }
    else
    {
        /* drive does not support LBA */
        /* FIXME: we really should include CHS access */
        return ATA_ERR_NOTSUPP;
    }    
    return ATA_ERR_TIMEOUT;
}
int ata_select(ATA_DRIVE *drive)
{
    outb (ata_ctrlport[drive->controller], ata_drive[drive->drive]);
    return 0;
}    
int ata_identify(ATA_DRIVE *drive, void* buf)
{
    DWORD timeout;
    ata_simplecmd(drive,ATA_CMD_IDENTIFY);
    /* timeout after 5 sec */
    timeout = multitask_get_ticks() + 5000;
    while( multitask_get_ticks() < timeout && ata_irq[drive->controller] == 0 );
    if( ata_irq[drive->controller] != 0 )
    {
        /* we got an irq, reset it and load data */
        ata_irq[drive->controller] = 0;
        ata_loadsectors(drive, buf,1);
        /* we swap the strings here */
        ata_string_swap(buf+54,40);
        ata_string_swap(buf+20,20);
        ata_string_swap(buf+46,8);
        /* copy capabilities fields */
        kmemcpy(&(drive->caps[0]),buf+98,2);
        kmemcpy(&(drive->caps[1]),buf+100,2);
        return ATA_ERR_NONE;
    }
    /* timeout */
    return ATA_ERR_TIMEOUT;
}
void ata_string_swap(char *string, int chars)
{
    char tmp;
    int i;
    
    chars -= chars % 2;
    for( i=0 ; i<chars; i+=2 )
    {
        tmp = string[i];
        string[i] = string[i+1];
        string[i+1] = tmp;
    }    
}   

⌨️ 快捷键说明

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