FX_FR.C
上传用户:super_houu
上传日期:2008-09-21
资源大小:4099k
文件大小:21k
- /**************************************************************************/
- /* */
- /* Copyright (c) 1996-2002 by Express Logic Inc. */
- /* */
- /* This software is copyrighted by and is the sole property of Express */
- /* Logic, Inc. All rights, title, ownership, or other interests */
- /* in the software remain the property of Express Logic, Inc. This */
- /* software may only be used in accordance with the corresponding */
- /* license agreement. Any unauthorized use, duplication, transmission, */
- /* distribution, or disclosure of this software is expressly forbidden. */
- /* */
- /* This Copyright notice may not be removed or modified without prior */
- /* written consent of Express Logic, Inc. */
- /* */
- /* Express Logic, Inc. reserves the right to modify this software */
- /* without notice. */
- /* */
- /* Express Logic, Inc. info@expresslogic.com */
- /* 11423 West Bernardo Court http://www.expresslogic.com */
- /* San Diego, CA 92127 */
- /* */
- /**************************************************************************/
- /**************************************************************************/
- /**************************************************************************/
- /** */
- /** FileX Component */
- /** */
- /** File (FIL) */
- /** */
- /**************************************************************************/
- /**************************************************************************/
- #include "Config.h" // Global Configuration - do not remove!
- #ifdef ENABLE_FILEX
- #ifdef _DEBUG
- #undef IFTRACE
- #define IFTRACE if (gTraceFileSys)
- #include "DebugDbgMain.h"
- #endif //_DEBUG
- #define FX_SOURCE_CODE
- /* Include necessary system files. */
- #include "PlaycoreFileSysFileXfx_api.h"
- #include "PlaycoreFileSysFileXfx_sys.h"
- #include "PlaycoreFileSysFileXfx_fil.h"
- #include "PlaycoreFileSysFileXfx_uti.h"
- #include "DriveCardReaderCardReaderDriver.h"
- #ifdef FILEX_READ_4_SECTORS
- #include "mediacardsincluderegister.h"
- extern CARD_DETECT_TYPE card_type;
- extern int I49_BitstreamWrite8Bit(UINT8 *data,UINT16 length);
- #include "kerneluitronrtos.h"
- #endif
- /**************************************************************************/
- /* */
- /* FUNCTION RELEASE */
- /* */
- /* _fx_file_read PORTABLE C */
- /* 3.0 */
- /* AUTHOR */
- /* */
- /* William E. Lamie, Express Logic, Inc. */
- /* */
- /* DESCRIPTION */
- /* */
- /* This function reads the specified number of bytes (or as many as */
- /* possible into the buffer supplied by the caller. The actual number */
- /* of bytes and the status of the read operation is returned to the */
- /* caller. In addition, various internal file pointers in the file */
- /* control block are also updated. */
- /* */
- /* INPUT */
- /* */
- /* file_ptr File control block pointer */
- /* buffer_ptr Buffer pointer */
- /* request_size Number of bytes requested */
- /* actual_size Pointer to variable for the */
- /* number of bytes read */
- /* */
- /* OUTPUT */
- /* */
- /* return status */
- /* */
- /* CALLS */
- /* */
- /* _fx_utility_FAT_entry_read Read a FAT entry */
- /* _fx_utility_log_sector_read Read a logical sector */
- /* _fx_utility_memory_copy Fast memory copy routine */
- /* */
- /* CALLED BY */
- /* */
- /* Application Code */
- /* */
- /* RELEASE HISTORY */
- /* */
- /* DATE NAME DESCRIPTION */
- /* */
- /* 01-01-1999 William E. Lamie Initial Version 1.0 */
- /* 03-01-2000 William E. Lamie Modified comment(s), */
- /* resulting in version 1.0b. */
- /* 01-28-2001 William E. Lamie Modified comment(s) and */
- /* corrected exact end-of-file */
- /* problem that caused data */
- /* not to be copied into the */
- /* user's buffer, resulting in */
- /* version 2.0. */
- /* 03-01-2002 Mohammad N. Minhaz Changed to 32 bit variables, */
- /* consecutive sectors read */
- /* together, resulting in */
- /* version 3.0. */
- /* */
- /**************************************************************************/
- UINT _fx_file_read(FX_FILE *file_ptr, VOID *buffer_ptr, ULONG request_size, ULONG *actual_size)
- {
- UINT status;
- ULONG bytes_remaining, i;
- ULONG copy_bytes;
- UCHAR_PTR destination_ptr;
- ULONG cluster, next_cluster;
- ULONG sectors;
- FX_MEDIA *media_ptr;
- ULONG fat_reserved;
- /* First, determine if the file is still open. */
- if (file_ptr -> fx_file_id != FX_FILE_ID)
- {
- /* Return the file not open error status. */
- return(FX_NOT_OPEN);
- }
- /* Setup pointer to associated media control block. */
- media_ptr = file_ptr -> fx_file_media_ptr;
- /* Determine the type of FAT and setup variables accordingly. */
- if (media_ptr -> fx_media_32_bit_FAT) fat_reserved = FX_RESERVED_1_32;
- else fat_reserved = FX_RESERVED_1;
- /* Protect against other threads accessing the media. */
- FX_PROTECT
- /* Next, determine if there is any more bytes to read in the file. */
- if (file_ptr -> fx_file_current_file_offset >=
- file_ptr -> fx_file_current_file_size)
- {
- /* Release media protection. */
- FX_UNPROTECT
- /* The file is at the end, return the proper status and set the
- actual size to 0. */
- *actual_size = 0;
- return(FX_END_OF_FILE);
- }
- /* At this point there is something to read. */
- /* Setup local buffer pointer. */
- destination_ptr = (UCHAR_PTR) buffer_ptr;
- /* Determine if there are less bytes left in the file than that specified
- by the request. If so, ajust the requested size. */
- if (request_size >
- (file_ptr -> fx_file_current_file_size - file_ptr -> fx_file_current_file_offset))
- {
- /* Adjust the bytes remaining to what's available. */
- request_size = file_ptr -> fx_file_current_file_size - file_ptr -> fx_file_current_file_offset;
- }
- /* Setup the remaining number of bytes to read. */
- bytes_remaining = request_size;
- /* Loop to read all of the bytes. */
- while (bytes_remaining)
- {
- /* Determine if a beginning or ending partial read is required. */
- if ((file_ptr -> fx_file_current_logical_offset) ||
- (bytes_remaining < media_ptr -> fx_media_bytes_per_sector))
- {
- /* A partial sector read is required. */
- /* Read the current logical sector. */
- status = _fx_utility_log_sector_read(media_ptr,
- file_ptr -> fx_file_current_logical_sector,
- media_ptr -> fx_media_memory_buffer, (ULONG) 1);
- /* Check for good completion status. */
- if (status != FX_SUCCESS)
- {
- /* Release media protection. */
- FX_UNPROTECT
- /* Return the error status. */
- return(status);
- }
- /* Copy the appropriate number of bytes into the destination buffer. */
- copy_bytes = media_ptr -> fx_media_bytes_per_sector -
- file_ptr -> fx_file_current_logical_offset;
-
- /* Check to see if only a portion of the read sector needs to be
- copied. */
- if (copy_bytes > bytes_remaining)
- {
-
- /* Adjust the number of bytes to copy. */
- copy_bytes = bytes_remaining;
- }
- /* Actually perform the memory copy. */
- if (!IsDestDecoder(destination_ptr))
- memcpy( destination_ptr,
- ((UCHAR_PTR) media_ptr -> fx_media_memory_buffer) +
- file_ptr -> fx_file_current_logical_offset,
- copy_bytes);
- else
- printf("todo: fx_fr error!!!n");
- /* Increment the logical sector byte offset. */
- file_ptr -> fx_file_current_logical_offset =
- file_ptr -> fx_file_current_logical_offset + copy_bytes;
-
- /* Adjust the remaining bytes to read. */
- bytes_remaining = bytes_remaining - copy_bytes;
- /* Adjust the pointer to the destination buffer. */
- if (!IsDestDecoder(destination_ptr)) //jerry cai
- destination_ptr = destination_ptr + copy_bytes;
- }
- else
- {
- /* Attempt to read multiple sectors directly into the destination
- buffer. */
- /* Calculate the number of whole sectors to read directly into
- the destination buffer. */
- sectors = (UINT) (bytes_remaining/media_ptr -> fx_media_bytes_per_sector);
- next_cluster = cluster = file_ptr -> fx_file_current_physical_cluster;
- for(i = (media_ptr -> fx_media_sectors_per_cluster -
- file_ptr -> fx_file_current_relative_sector); i<sectors; i += media_ptr->fx_media_sectors_per_cluster)
- {
- status = _fx_utility_FAT_entry_read(media_ptr, cluster, &next_cluster);
- /* Determine if an error is present. */
- if ((status != FX_SUCCESS) || (next_cluster < FX_FAT_ENTRY_START) ||
- (next_cluster > fat_reserved))
- {
- /* Release media protection. */
- FX_UNPROTECT
- /* Send error message back to caller. */
- if (status != FX_SUCCESS)
- return(status);
- // else
- // return(FX_FILE_CORRUPT);
- }
- if (next_cluster != cluster + 1)
- break;
- else
- cluster = next_cluster;
- }
- if (i < sectors)
- sectors = i;
- #ifdef FILEX_READ_4_SECTORS
- if(CARD_DETECT_SSFDC == card_type || CARD_DETECT_XD == card_type){
-
- /* Perform the data read directly into the user's buffer of
- the appropriate number of sectors. */
-
- UINT remaining_sectors;
- remaining_sectors = sectors;
- while(remaining_sectors > 0){
- UINT unit = 4;
-
- if(remaining_sectors < unit)
- unit = remaining_sectors;
- if(unit > 4)
- printf("n");
- status = _fx_utility_log_sector_read(media_ptr, file_ptr -> fx_file_current_logical_sector + (sectors - remaining_sectors),
- IsDestDecoder(destination_ptr)? media_ptr -> fx_media_memory_buffer_orig:
- destination_ptr,
- (ULONG) unit);
- if(IsDestDecoder(destination_ptr)){
- I49_BitstreamWrite8Bit((UINT8 *)media_ptr -> fx_media_memory_buffer_orig,unit*512u);
- }
- remaining_sectors -= unit;
- }
- }
- else
- status = _fx_utility_log_sector_read(media_ptr, file_ptr -> fx_file_current_logical_sector,
- destination_ptr,
- (ULONG) sectors);
-
- #else
- status = _fx_utility_log_sector_read(media_ptr, file_ptr -> fx_file_current_logical_sector,
- destination_ptr, (ULONG) sectors);
- #endif
- /* Check for good completion status. */
- if (status != FX_SUCCESS)
- {
- /* Release media protection. */
- FX_UNPROTECT
- /* Return the error status. */
- return(status);
- }
- /* Now adjust the various file pointers. */
- /* Increment the current logical sector. Subtract one from
- the sector count because we are going to use the logical
- offset to do additional sector/cluster arithmetic below. */
- file_ptr -> fx_file_current_logical_sector =
- file_ptr -> fx_file_current_logical_sector +
- (sectors - 1);
- /* Move the relative sector and cluster as well. */
- file_ptr -> fx_file_current_relative_cluster = file_ptr -> fx_file_current_relative_cluster
- + (file_ptr -> fx_file_current_relative_sector + (sectors - 1))
- / media_ptr -> fx_media_sectors_per_cluster;
- file_ptr -> fx_file_current_relative_sector =
- (file_ptr -> fx_file_current_relative_sector +
- (sectors - 1)) % media_ptr -> fx_media_sectors_per_cluster;
- /* Increment the logical sector byte offset. */
- file_ptr -> fx_file_current_logical_offset =
- media_ptr -> fx_media_bytes_per_sector;
- file_ptr -> fx_file_current_physical_cluster = cluster;
- /* Adjust the remaining bytes. */
- bytes_remaining = bytes_remaining -
- (((ULONG) media_ptr -> fx_media_bytes_per_sector) * sectors);
- if (!IsDestDecoder(destination_ptr))
- {
- /* Adjust the pointer to the destination buffer. */
- destination_ptr = destination_ptr +
- (((ULONG) media_ptr -> fx_media_bytes_per_sector) * sectors);
- }
- if ((next_cluster < FX_FAT_ENTRY_START) || (next_cluster > fat_reserved))
- return(FX_END_OF_FILE);
- }
- /* At this point, we have either read a partial sector or have successfully
- read one or more whole sectors. Determine if we are at the end of
- the current logical sector. */
- if (file_ptr -> fx_file_current_logical_offset >=
- media_ptr -> fx_media_bytes_per_sector)
- {
- /* Determine if we are at the exact physical end of the file at the end of reading. */
- if ((bytes_remaining == 0) && ((file_ptr -> fx_file_current_file_offset + request_size) >=
- file_ptr -> fx_file_current_available_size))
- {
- /* Skip the following file parameter adjustments. The next write will
- detect the logical offset out of the range of the sector and reset
- all of the pertinent information. */
- break;
- }
- /* We need to move to the next logical sector, but first
- determine if the next logical sector is within the same
- cluster. */
- /* Increment the current relative sector in the cluster. */
- file_ptr -> fx_file_current_relative_sector++;
- /* Determine if this is in a new cluster. */
- if (file_ptr -> fx_file_current_relative_sector >=
- media_ptr -> fx_media_sectors_per_cluster)
- {
- /* Read the FAT entry of the current cluster to find
- the next cluster. */
- status = _fx_utility_FAT_entry_read(media_ptr,
- file_ptr -> fx_file_current_physical_cluster, &next_cluster);
- /* Determine if an error is present. */
- if ((status != FX_SUCCESS) || (next_cluster < FX_FAT_ENTRY_START) ||
- (next_cluster > fat_reserved))
- {
- /* Release media protection. */
- FX_UNPROTECT
- /* Send error message back to caller. */
- if (status != FX_SUCCESS)
- return(status);
- else
- return(FX_FILE_CORRUPT);
- }
- /* Otherwise, we have a new cluster. Save it in the file
- control block and calculate a new logical sector value. */
- file_ptr -> fx_file_current_physical_cluster = next_cluster;
- file_ptr -> fx_file_current_relative_cluster++;
- file_ptr -> fx_file_current_logical_sector = ((ULONG) media_ptr -> fx_media_data_sector_start) +
- ((((ULONG) next_cluster) - FX_FAT_ENTRY_START) *
- ((ULONG) media_ptr -> fx_media_sectors_per_cluster));
- file_ptr -> fx_file_current_relative_sector = 0;
- }
- else
- {
- /* Still within the same cluster so just increment the
- logical sector. */
- file_ptr -> fx_file_current_logical_sector++;
- }
- /* In either case, we are now positioned at a new sector so
- clear the logical sector offset. */
- file_ptr -> fx_file_current_logical_offset = 0;
- }
- }
- dbg_printf(("fx_file_read, size:%x, offset:%lxn", request_size, file_ptr -> fx_file_current_file_offset));
- /* Adjust the current file offset accordingly. */
- file_ptr -> fx_file_current_file_offset =
- file_ptr -> fx_file_current_file_offset + request_size;
- /* Store the number of bytes actually read. */
- *actual_size = request_size;
- /* Release media protection. */
- FX_UNPROTECT
- /* Return a successful status to the caller. */
- return(FX_SUCCESS);
- }
- #endif