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

📄 ide_access.c

📁 一个FAT32文件系统的源代码,可用于嵌入式系统,并能够直接在linux下直接验证.
💻 C
字号:
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//					    FAT32 File IO Library Linux Test Version
//									  V1.0L
// 	  								Rob Riglar
//								Copyright 2003,2004 
//
//   					  Email: admin@robs-projects.com
//
//			   Compiled and tested on Redhat 'Shrike' with GNU gcc
//-----------------------------------------------------------------------------
//
// This file is part of FAT32 File IO Library.
//
// FAT32 File IO Library is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// FAT32 File IO Library 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with FAT32 File IO Library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <assert.h>
#include <unistd.h>
#include <string.h>

#include "IDE_Access.h"

#define READ(fd,offset,item) { lseek(fd, offset, SEEK_SET);  read(fd,&item,sizeof(item)); }

int fd;

//-----------------------------------------------------------------------------
// IDE_SectorByte: Function is used to retrieve a byte from the 'currentsector'
// array. The value passed in must be between 0 - 511.
//
// Parameters: Word location within currentsector
//
// Returns: Byte of data requested from currentsector
//-----------------------------------------------------------------------------
byte IDE_SectorByte(word sublocation)
{
// NOTE: This function is to be used whereever access to currentsector is required
//  	 by all layers that are higher than the IDE Base functions.
	byte data=0;          
    data=IDE_Internal.currentsector[sublocation];	  // Read Data from specified pos
	return (data);
}

//-----------------------------------------------------------------------------
// IDE_SectorWord: Function is used to retrieve a word from the 'currentsector'
// array. The value passed in must be between 0 - 510.
//
// Parameters: Word location within currentsector
//
// Returns: Word of data requested from currentsector
//-----------------------------------------------------------------------------
word IDE_SectorWord(word sublocation) // Return Word at position specified
{
	 word data=0; 
	 word tempword=0;
	   
	 tempword = IDE_SectorByte(sublocation+1); // Get MSB from Buffer
	 tempword<<=8;							 // Make MSB word half into byte
	 data = IDE_SectorByte(sublocation) + tempword; // Combine LSB and MSB

	 return (data);					  			 // Return value
}

//-----------------------------------------------------------------------------
// IDE_SectorUI32: Function is used to retrieve a 32 bit value from the 
// 'currentsector' array. The value passed in must be between 0 - 511.
//
// Parameters: Word offset within current sector
//
// Returns: 32 bit unsigned data as requested
//
// Functions Used: None
//
//-----------------------------------------------------------------------------
UI32 IDE_SectorUI32(word sublocation) // Return UI32 at position specified
{
    UI32 data=0; 
	UI32 A,B,C,D;
	
	A= IDE_SectorByte(sublocation);		// Read the four bytes which make up
	B= IDE_SectorByte(sublocation+1);    // the 32-bit value
	C= IDE_SectorByte(sublocation+2);
	D= IDE_SectorByte(sublocation+3);         

    data=(D<<=24) + (C<<=16) + (B<<=8) + A; //Combine into correct order
	return (data);					  			 // Return value
}

//-----------------------------------------------------------------------------
// IDE_BufferSector: This function recieves the LBA address of a sector and reads into
// an array of 512 bytes (1 sector) called 'currentsector'.
//
// Parameters: 32bit Unsigned Logical Block Address
//
// Returns: An integer error code: 1 means read successfull, 0 means read fail.
//
// Functions Used: WriteReg, CheckforError, ReadErrors, Wait_DRQ, Set_Address, 
// 			 	   DataInputSetup, printf
//
//-----------------------------------------------------------------------------
int IDE_BufferSector(UI32 LBALocation)
{
    UI16    i;
	
	// Only rebuffer if sector not currently loaded
	if (IDE_Internal.SectorCurrentlyLoaded!=LBALocation) // Dont reload if already loaded
	{              
	IDE_Internal.SectorCurrentlyLoaded=LBALocation; // update current sector loaded
	
	// Loose and slow read
    for(i=0; i <= 511; i++)
		READ(fd,i+(LBALocation*512),IDE_Internal.currentsector[i]);      

	}

	// Return Success
	return 1;
}

//-----------------------------------------------------------------------------
// InitDrive: Drive Initialisation routine, required before you use the drive 
// for reading or writting to the actual media. 
//-----------------------------------------------------------------------------
void IDE_InitDrive(char *partition)
{
	  // Set to defualt value
	  IDE_Internal.SectorCurrentlyLoaded = 0xffffffff;
	  
	fd = open(partition, O_RDONLY | O_NOCTTY);

	if (fd == -1) 
	{
		printf("\r\nHALT:: Unable to open disc");
		while (1);
	}

 // assert(sizeof(byte) == 1);
 // assert(sizeof(word) == 2);
 // assert(sizeof(UI32) == 4);
}

⌨️ 快捷键说明

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