Fx_ulsr.c
上传用户:super_houu
上传日期:2008-09-21
资源大小:4099k
文件大小:18k
- /**************************************************************************/
- /* */
- /* 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 */
- /** */
- /** Utility (UTI) */
- /** */
- /**************************************************************************/
- /**************************************************************************/
- #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_uti.h"
- /**************************************************************************/
- /* */
- /* FUNCTION RELEASE */
- /* */
- /* _fx_utility_log_sector_read PORTABLE C */
- /* 3.0 */
- /* AUTHOR */
- /* */
- /* William E. Lamie, Express Logic, Inc. */
- /* */
- /* DESCRIPTION */
- /* */
- /* This function handles logical sector read requests for all FileX */
- /* components. If the logical sector is currently in the buffer */
- /* supplied by the caller, the function simply returns a successful */
- /* status to the caller. Otherwise, physical I/O is requested through */
- /* the corresponding I/O driver. */
- /* */
- /* Note: Conversion of the logical sector is done inside the driver. */
- /* This results in a performance boost for FLASH or RAM media */
- /* devices. */
- /* */
- /* INPUT */
- /* */
- /* media_ptr Media control block pointer */
- /* logical_sector Logical sector number */
- /* buffer_ptr Pointer of receiving buffer */
- /* sectors Number of sectors to read */
- /* */
- /* OUTPUT */
- /* */
- /* return status */
- /* */
- /* CALLS */
- /* */
- /* I/O Driver */
- /* */
- /* CALLED BY */
- /* */
- /* FileX System Functions */
- /* */
- /* 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 added */
- /* logical sector cache, */
- /* resulting in version 2.0. */
- /* 03-01-2002 Mohammad N. Minhaz Modified comment(s) and */
- /* modified total sector logic,*/
- /* resulting in version 3.0. */
- /* */
- /**************************************************************************/
- UINT _fx_utility_log_sector_read(FX_MEDIA *media_ptr, ULONG logical_sector,
- VOID *buffer_ptr, ULONG sectors)
- {
- FX_CACHED_SECTOR *cache_entry;
- FX_CACHED_SECTOR *previous_cache_entry;
- ULONG cache_size;
- /* Determine if the request is for the internal media buffer area. */
- if ((((UCHAR_PTR) buffer_ptr) >= media_ptr -> fx_media_memory_buffer_orig) &&//jerryc was fx_media_memory_buffer
- (((UCHAR_PTR) buffer_ptr) <= media_ptr -> fx_media_sector_cache_end))
- {
- /* Internal cache buffer is requested. */
- /* Search for an entry in the cache that matches this request. */
- cache_size = media_ptr -> fx_media_sector_cache_size;
- cache_entry = media_ptr -> fx_media_sector_cache_list_ptr;
- previous_cache_entry = FX_NULL;
- /* Look at the cache entries until a match is found or the end of
- the cache is reached. */
- while(cache_size--)
- {
-
- #ifdef FILEX_READ_4_SECTORS
-
- if(sectors > 1)
- break;
- #endif
-
- /* Determine if the requested sector has been found. */
- if (cache_entry -> fx_cached_sector == logical_sector)
- {
- #ifndef FX_TEST_CACHE
- /* Yes, we found a match. Simply setup the pointer to this
- buffer and return. */
- media_ptr -> fx_media_memory_buffer = cache_entry -> fx_cached_sector_memory_buffer;
- #else
- if(cache_entry -> fx_cached_sector_memory_buffer != ((DWORD)media_ptr -> fx_media_memory_buffer))
- {
- _fx_switch_cache(media_ptr, cache_entry);
- }
- media_ptr->fx_media_driver_buffer = (UCHAR_PTR)media_ptr -> fx_media_memory_buffer;
- #endif
- /* Determine if we need to update the last used list. */
- if (previous_cache_entry)
- {
- /* Yes, the current entry is not at the front of the list
- so we need to change the order. */
-
- /* Link the previous entry to this entry's next pointer. */
- previous_cache_entry -> fx_cached_sector_next_used =
- cache_entry -> fx_cached_sector_next_used;
- /* Place this entry at the head of the list. */
- cache_entry -> fx_cached_sector_next_used =
- media_ptr -> fx_media_sector_cache_list_ptr;
- media_ptr -> fx_media_sector_cache_list_ptr = cache_entry;
- }
- /* Success, return to caller immediately! */
- return(FX_SUCCESS);
- }
- /* Otherwise, we have not found the cached entry yet. */
- /* If there are more entries, move to the next one. */
- if (cache_entry -> fx_cached_sector_next_used)
- {
- previous_cache_entry = cache_entry;
- cache_entry = cache_entry -> fx_cached_sector_next_used;
- }
- }
- /* At this point, we need to read in a sector from the media. */
- /* First, check and see if the last used entry has been
- modified. */
- #ifdef FILEX_READ_4_SECTORS
-
- cache_entry = media_ptr -> fx_media_sector_cache_list_ptr;
- cache_size = (UINT) media_ptr -> fx_media_sector_cache_size;
-
- while(cache_size--){
-
- #endif
- if (cache_entry -> fx_cached_sector_buffer_dirty)
- {
- /* Yes, we need to flush this buffer out to the physical media
- before we read in the new buffer. */
- /* Build Write request to the driver. */
- media_ptr -> fx_media_driver_request = FX_DRIVER_WRITE;
- media_ptr -> fx_media_driver_status = FX_IO_ERROR;
- #ifndef FX_TEST_CACHE
- media_ptr -> fx_media_driver_buffer = cache_entry -> fx_cached_sector_memory_buffer;
- #else
- if(cache_entry -> fx_cached_sector_memory_buffer != ((DWORD)media_ptr -> fx_media_memory_buffer))
- {
- _fx_switch_cache(media_ptr, cache_entry);
- }
- media_ptr->fx_media_driver_buffer = (UCHAR_PTR)media_ptr -> fx_media_memory_buffer;
- #endif
- media_ptr -> fx_media_driver_logical_sector = cache_entry -> fx_cached_sector;
- media_ptr -> fx_media_driver_sectors = sectors;
- /* Invoke the driver to write the sector. */
- (media_ptr -> fx_media_driver_entry) (media_ptr);
- /* Check for successful completion. */
- if (media_ptr -> fx_media_driver_status)
- {
- /* Error writing a cached sector out. Return the
- error status. */
- return(media_ptr -> fx_media_driver_status);
- }
- /* Clear the buffer dirty flag since it has been flushed
- out. */
- cache_entry -> fx_cached_sector_buffer_dirty = FX_FALSE;
- }
- #ifdef FILEX_READ_4_SECTORS
- cache_entry = cache_entry -> fx_cached_sector_next_used;
-
- }
- #endif
-
- #ifdef FILEX_READ_4_SECTORS
- cache_size = (UINT) media_ptr -> fx_media_sector_cache_size;
- cache_entry = media_ptr -> fx_media_sector_cache;
- while (cache_size--)
- {
- /* Initialize each of the cache entries. */
- cache_entry -> fx_cached_sector_buffer_dirty = FX_FALSE;
- cache_entry -> fx_cached_sector_next_used = cache_entry + 1;
- /* Move to the next cache sector entry. */
- cache_entry++;
- }
- cache_entry--;
-
- cache_entry -> fx_cached_sector_next_used = FX_NULL;
-
- media_ptr -> fx_media_sector_cache_list_ptr = media_ptr -> fx_media_sector_cache;
- previous_cache_entry = NULL;
-
- cache_entry = media_ptr -> fx_media_sector_cache_list_ptr;
- #endif
-
-
- /* At this point, we can go out and setup this cached sector
- entry. */
- /* Compare against logical sector to make sure it is valid. */
- //if (logical_sector >= (ULONG) media_ptr -> fx_media_total_sectors)
- // return(FX_SECTOR_INVALID);
- /* Build Read request to the driver. */
- media_ptr -> fx_media_driver_request = FX_DRIVER_READ;
- media_ptr -> fx_media_driver_status = FX_IO_ERROR;
- #ifndef FX_TEST_CACHE
- media_ptr -> fx_media_driver_buffer = cache_entry -> fx_cached_sector_memory_buffer;
- #else
- if(cache_entry -> fx_cached_sector_memory_buffer != ((DWORD)media_ptr -> fx_media_memory_buffer))
- {
- _fx_switch_cache(media_ptr, cache_entry);
- }
- media_ptr->fx_media_driver_buffer = (UCHAR_PTR)media_ptr -> fx_media_memory_buffer;
- #endif
- media_ptr -> fx_media_driver_logical_sector = logical_sector;
- #ifdef FILEX_READ_4_SECTORS
- media_ptr -> fx_media_driver_sectors = 4;
- #else
- media_ptr -> fx_media_driver_sectors = sectors;
- #endif
-
- /* Invoke the driver to read the sector. */
- (media_ptr -> fx_media_driver_entry) (media_ptr);
-
- /* Determine if the read was successful. */
- if (media_ptr -> fx_media_driver_status == FX_SUCCESS)
- {
- /* Remember the sector number. */
- #ifdef FILEX_READ_4_SECTORS
- {
- previous_cache_entry = media_ptr -> fx_media_sector_cache_list_ptr;
-
- cache_size = 4;
-
- while (cache_size--){
- previous_cache_entry->fx_cached_sector = logical_sector;
- logical_sector++;
- previous_cache_entry++;
- }
- previous_cache_entry = NULL;
- }
-
- if(sectors == 1){
-
- #if 1
- memcpy(media_ptr -> fx_media_memory_buffer_orig + 4*512u, media_ptr -> fx_media_driver_buffer, 512);
- cache_entry++;
- cache_entry++;
- cache_entry++;
- cache_entry++;
- //jerryc_april_14
- cache_entry -> fx_cached_sector = media_ptr -> fx_media_sector_cache_list_ptr->fx_cached_sector;
- //jerryc_april_14
- #endif
- }
- #else
- cache_entry -> fx_cached_sector = logical_sector;
- #endif
- /* Place this entry that the head of the cached sector
- list. */
- /* Determine if we need to update the last used list. */
- if (previous_cache_entry)
- {
- /* Yes, the current entry is not at the front of the list
- so we need to change the order. */
-
- /* Link the previous entry to this entry's next pointer. */
- previous_cache_entry -> fx_cached_sector_next_used =
- cache_entry -> fx_cached_sector_next_used;
- /* Place this entry at the head of the list. */
- cache_entry -> fx_cached_sector_next_used =
- media_ptr -> fx_media_sector_cache_list_ptr;
- media_ptr -> fx_media_sector_cache_list_ptr = cache_entry;
- }
- }
- else
- {
- /* Invalidate the cache entry on read errors. */
- cache_entry -> fx_cached_sector = 0;
- }
- /* Simply setup the pointer to this buffer and return. */
- media_ptr -> fx_media_memory_buffer = cache_entry -> fx_cached_sector_memory_buffer;
- /* Return the driver status. */
- return(media_ptr -> fx_media_driver_status);
- }
- else
- {
- /* Direct I/O to application buffer area. */
- /* Compare against logical sector to make sure it is valid. */
- //if (logical_sector >= (ULONG) media_ptr -> fx_media_total_sectors)
- // return(FX_SECTOR_INVALID);
- /* Build Read request to the driver. */
- media_ptr -> fx_media_driver_request = FX_DRIVER_READ;
- media_ptr -> fx_media_driver_status = FX_IO_ERROR;
- media_ptr -> fx_media_driver_buffer = buffer_ptr;
- media_ptr -> fx_media_driver_logical_sector = logical_sector;
- media_ptr -> fx_media_driver_sectors = sectors;
- /* Invoke the driver to read the sector. */
- (media_ptr -> fx_media_driver_entry) (media_ptr);
- /* Return the driver status. */
- return(media_ptr -> fx_media_driver_status);
- }
- }
- #endif