Fx_ulsr.c
上传用户:super_houu
上传日期:2008-09-21
资源大小:4099k
文件大小:18k
源码类别:

DVD

开发平台:

Others

  1. /**************************************************************************/ 
  2. /*                                                                        */ 
  3. /*            Copyright (c) 1996-2002 by Express Logic Inc.               */ 
  4. /*                                                                        */ 
  5. /*  This software is copyrighted by and is the sole property of Express   */ 
  6. /*  Logic, Inc.  All rights, title, ownership, or other interests         */ 
  7. /*  in the software remain the property of Express Logic, Inc.  This      */ 
  8. /*  software may only be used in accordance with the corresponding        */ 
  9. /*  license agreement.  Any unauthorized use, duplication, transmission,  */ 
  10. /*  distribution, or disclosure of this software is expressly forbidden.  */ 
  11. /*                                                                        */
  12. /*  This Copyright notice may not be removed or modified without prior    */ 
  13. /*  written consent of Express Logic, Inc.                                */ 
  14. /*                                                                        */ 
  15. /*  Express Logic, Inc. reserves the right to modify this software        */ 
  16. /*  without notice.                                                       */ 
  17. /*                                                                        */ 
  18. /*  Express Logic, Inc.                     info@expresslogic.com         */
  19. /*  11423 West Bernardo Court               http://www.expresslogic.com   */
  20. /*  San Diego, CA  92127                                                  */
  21. /*                                                                        */
  22. /**************************************************************************/
  23. /**************************************************************************/
  24. /**************************************************************************/
  25. /**                                                                       */ 
  26. /** FileX Component                                                       */ 
  27. /**                                                                       */
  28. /**   Utility (UTI)                                                       */
  29. /**                                                                       */
  30. /**************************************************************************/
  31. /**************************************************************************/
  32. #include "Config.h" // Global Configuration - do not remove!
  33. #ifdef ENABLE_FILEX
  34. #ifdef _DEBUG
  35. #undef IFTRACE
  36. #define IFTRACE if (gTraceFileSys)
  37. #include "DebugDbgMain.h"
  38. #endif //_DEBUG
  39. #define FX_SOURCE_CODE
  40. /* Include necessary system files.  */
  41. #include "PlaycoreFileSysFileXfx_api.h"
  42. #include "PlaycoreFileSysFileXfx_sys.h"
  43. #include "PlaycoreFileSysFileXfx_uti.h"
  44. /**************************************************************************/ 
  45. /*                                                                        */ 
  46. /*  FUNCTION                                               RELEASE        */ 
  47. /*                                                                        */ 
  48. /*    _fx_utility_log_sector_read                         PORTABLE C      */ 
  49. /*                                                           3.0          */ 
  50. /*  AUTHOR                                                                */ 
  51. /*                                                                        */ 
  52. /*    William E. Lamie, Express Logic, Inc.                               */ 
  53. /*                                                                        */ 
  54. /*  DESCRIPTION                                                           */ 
  55. /*                                                                        */ 
  56. /*    This function handles logical sector read requests for all FileX    */ 
  57. /*    components.  If the logical sector is currently in the buffer       */ 
  58. /*    supplied by the caller, the function simply returns a successful    */ 
  59. /*    status to the caller.  Otherwise, physical I/O is requested through */ 
  60. /*    the corresponding I/O driver.                                       */ 
  61. /*                                                                        */ 
  62. /*    Note: Conversion of the logical sector is done inside the driver.   */ 
  63. /*          This results in a performance boost for FLASH or RAM media    */ 
  64. /*          devices.                                                      */ 
  65. /*                                                                        */ 
  66. /*  INPUT                                                                 */ 
  67. /*                                                                        */ 
  68. /*    media_ptr                             Media control block pointer   */ 
  69. /*    logical_sector                        Logical sector number         */ 
  70. /*    buffer_ptr                            Pointer of receiving buffer   */ 
  71. /*    sectors                               Number of sectors to read     */ 
  72. /*                                                                        */ 
  73. /*  OUTPUT                                                                */ 
  74. /*                                                                        */ 
  75. /*    return status                                                       */ 
  76. /*                                                                        */ 
  77. /*  CALLS                                                                 */ 
  78. /*                                                                        */ 
  79. /*    I/O Driver                                                          */ 
  80. /*                                                                        */ 
  81. /*  CALLED BY                                                             */ 
  82. /*                                                                        */ 
  83. /*    FileX System Functions                                              */ 
  84. /*                                                                        */ 
  85. /*  RELEASE HISTORY                                                       */ 
  86. /*                                                                        */ 
  87. /*    DATE              NAME                      DESCRIPTION             */ 
  88. /*                                                                        */ 
  89. /*  01-01-1999     William E. Lamie         Initial Version 1.0           */ 
  90. /*  03-01-2000     William E. Lamie         Modified comment(s),          */ 
  91. /*                                            resulting in version 1.0b.  */ 
  92. /*  01-28-2001     William E. Lamie         Modified comment(s) and added */ 
  93. /*                                            logical sector cache,       */ 
  94. /*                                            resulting in version 2.0.   */ 
  95. /*  03-01-2002     Mohammad N. Minhaz       Modified comment(s) and       */ 
  96. /*                                            modified total sector logic,*/ 
  97. /*                                            resulting in version 3.0.   */ 
  98. /*                                                                        */ 
  99. /**************************************************************************/ 
  100. UINT  _fx_utility_log_sector_read(FX_MEDIA *media_ptr, ULONG logical_sector, 
  101.                                                 VOID *buffer_ptr, ULONG sectors)
  102. {
  103. FX_CACHED_SECTOR    *cache_entry;
  104. FX_CACHED_SECTOR    *previous_cache_entry;
  105. ULONG                cache_size;
  106.     /* Determine if the request is for the internal media buffer area.  */
  107.     if ((((UCHAR_PTR) buffer_ptr) >= media_ptr -> fx_media_memory_buffer_orig) &&//jerryc was fx_media_memory_buffer
  108.          (((UCHAR_PTR) buffer_ptr) <= media_ptr -> fx_media_sector_cache_end))
  109.     {
  110.         /* Internal cache buffer is requested.  */
  111.         /* Search for an entry in the cache that matches this request.  */
  112.         cache_size =            media_ptr -> fx_media_sector_cache_size;
  113.         cache_entry =           media_ptr -> fx_media_sector_cache_list_ptr;
  114.         previous_cache_entry =  FX_NULL;
  115.         /* Look at the cache entries until a match is found or the end of
  116.            the cache is reached.  */
  117.         while(cache_size--)
  118.         {
  119.         
  120. #ifdef FILEX_READ_4_SECTORS
  121.     
  122.     if(sectors > 1)
  123.          break;
  124. #endif
  125.         
  126.             /* Determine if the requested sector has been found.  */
  127.             if (cache_entry -> fx_cached_sector == logical_sector)
  128.             {
  129. #ifndef FX_TEST_CACHE 
  130.                 /* Yes, we found a match.  Simply setup the pointer to this
  131.                    buffer and return.  */
  132.                 media_ptr -> fx_media_memory_buffer =  cache_entry -> fx_cached_sector_memory_buffer;
  133. #else
  134.  if(cache_entry -> fx_cached_sector_memory_buffer != ((DWORD)media_ptr -> fx_media_memory_buffer))
  135.  {
  136.    _fx_switch_cache(media_ptr, cache_entry);
  137.  }
  138.   media_ptr->fx_media_driver_buffer = (UCHAR_PTR)media_ptr -> fx_media_memory_buffer;
  139. #endif
  140.                 /* Determine if we need to update the last used list.  */
  141.                 if (previous_cache_entry)
  142.                 {
  143.                     /* Yes, the current entry is not at the front of the list
  144.                        so we need to change the order.  */
  145.                 
  146.                     /* Link the previous entry to this entry's next pointer.  */
  147.                     previous_cache_entry -> fx_cached_sector_next_used =
  148.                                     cache_entry -> fx_cached_sector_next_used;
  149.                     /* Place this entry at the head of the list.  */
  150.                     cache_entry -> fx_cached_sector_next_used =  
  151.                                     media_ptr -> fx_media_sector_cache_list_ptr;
  152.                     media_ptr -> fx_media_sector_cache_list_ptr =  cache_entry;
  153.                 }
  154.                 /* Success, return to caller immediately!  */
  155.                 return(FX_SUCCESS);
  156.             } 
  157.             /* Otherwise, we have not found the cached entry yet.  */
  158.             /* If there are more entries, move to the next one.  */
  159.             if (cache_entry -> fx_cached_sector_next_used)
  160.             {
  161.                 previous_cache_entry =  cache_entry;
  162.                 cache_entry =           cache_entry -> fx_cached_sector_next_used;
  163.             }
  164.         }        
  165.         /* At this point, we need to read in a sector from the media.  */
  166.         /* First, check and see if the last used entry has been 
  167.            modified.  */
  168. #ifdef FILEX_READ_4_SECTORS
  169.     
  170.     cache_entry =           media_ptr -> fx_media_sector_cache_list_ptr;
  171. cache_size =  (UINT) media_ptr -> fx_media_sector_cache_size;
  172.             
  173. while(cache_size--){
  174.             
  175. #endif
  176.         if (cache_entry -> fx_cached_sector_buffer_dirty)
  177.         {
  178.             /* Yes, we need to flush this buffer out to the physical media
  179.                before we read in the new buffer.  */
  180.             /* Build Write request to the driver.  */
  181.             media_ptr -> fx_media_driver_request =          FX_DRIVER_WRITE;
  182.             media_ptr -> fx_media_driver_status =           FX_IO_ERROR;
  183. #ifndef FX_TEST_CACHE
  184.             media_ptr -> fx_media_driver_buffer =           cache_entry -> fx_cached_sector_memory_buffer;
  185. #else
  186.      if(cache_entry -> fx_cached_sector_memory_buffer != ((DWORD)media_ptr -> fx_media_memory_buffer))
  187.      {
  188. _fx_switch_cache(media_ptr, cache_entry);
  189.      }
  190. media_ptr->fx_media_driver_buffer = (UCHAR_PTR)media_ptr -> fx_media_memory_buffer;
  191. #endif
  192.             media_ptr -> fx_media_driver_logical_sector =   cache_entry -> fx_cached_sector;
  193.             media_ptr -> fx_media_driver_sectors =          sectors;
  194.             /* Invoke the driver to write the sector.  */
  195.             (media_ptr -> fx_media_driver_entry) (media_ptr);
  196.             /* Check for successful completion.  */
  197.             if (media_ptr -> fx_media_driver_status)
  198.             {
  199.                 /* Error writing a cached sector out.  Return the 
  200.                    error status.  */
  201.                 return(media_ptr -> fx_media_driver_status);
  202.             }
  203.             /* Clear the buffer dirty flag since it has been flushed 
  204.                out.  */
  205.             cache_entry -> fx_cached_sector_buffer_dirty =  FX_FALSE;
  206.         }
  207. #ifdef FILEX_READ_4_SECTORS
  208. cache_entry =           cache_entry -> fx_cached_sector_next_used;
  209.     
  210. }            
  211. #endif
  212.         
  213. #ifdef FILEX_READ_4_SECTORS
  214.             cache_size =  (UINT) media_ptr -> fx_media_sector_cache_size;
  215.             cache_entry =  media_ptr -> fx_media_sector_cache;
  216.             while (cache_size--)
  217.             {
  218.                 /* Initialize each of the cache entries.  */
  219.                 cache_entry -> fx_cached_sector_buffer_dirty =   FX_FALSE;
  220.                 cache_entry -> fx_cached_sector_next_used =      cache_entry + 1;
  221.                 /* Move to the next cache sector entry.  */
  222.                 cache_entry++;
  223.             }
  224. cache_entry--;
  225.             
  226.             cache_entry -> fx_cached_sector_next_used =      FX_NULL;
  227.             
  228.     media_ptr -> fx_media_sector_cache_list_ptr =  media_ptr -> fx_media_sector_cache;
  229. previous_cache_entry = NULL;            
  230.                       
  231.             cache_entry = media_ptr -> fx_media_sector_cache_list_ptr;
  232. #endif
  233.     
  234.         
  235.         /* At this point, we can go out and setup this cached sector
  236.            entry.  */
  237.         /* Compare against logical sector to make sure it is valid.  */
  238.         //if (logical_sector >= (ULONG) media_ptr -> fx_media_total_sectors)
  239.         //        return(FX_SECTOR_INVALID);
  240.         /* Build Read request to the driver.  */
  241.         media_ptr -> fx_media_driver_request =          FX_DRIVER_READ;
  242.         media_ptr -> fx_media_driver_status =           FX_IO_ERROR;
  243. #ifndef FX_TEST_CACHE
  244.         media_ptr -> fx_media_driver_buffer =           cache_entry -> fx_cached_sector_memory_buffer;
  245. #else
  246.  if(cache_entry -> fx_cached_sector_memory_buffer != ((DWORD)media_ptr -> fx_media_memory_buffer))
  247.  {
  248. _fx_switch_cache(media_ptr, cache_entry);
  249.  }
  250.  media_ptr->fx_media_driver_buffer = (UCHAR_PTR)media_ptr -> fx_media_memory_buffer;
  251. #endif
  252.         media_ptr -> fx_media_driver_logical_sector =   logical_sector;
  253. #ifdef FILEX_READ_4_SECTORS
  254.         media_ptr -> fx_media_driver_sectors =          4;
  255. #else
  256.         media_ptr -> fx_media_driver_sectors =          sectors;
  257. #endif        
  258.             
  259.         /* Invoke the driver to read the sector.  */
  260.         (media_ptr -> fx_media_driver_entry) (media_ptr);
  261.         
  262.         /* Determine if the read was successful.  */
  263.         if (media_ptr -> fx_media_driver_status == FX_SUCCESS)
  264.         {
  265.             /* Remember the sector number.  */
  266. #ifdef FILEX_READ_4_SECTORS
  267. {
  268.                 previous_cache_entry = media_ptr -> fx_media_sector_cache_list_ptr;
  269.                 
  270. cache_size = 4;
  271.                 
  272.                 while (cache_size--){
  273.                     previous_cache_entry->fx_cached_sector = logical_sector;
  274.                     logical_sector++;
  275.                     previous_cache_entry++;
  276.                 }
  277.                 previous_cache_entry = NULL;            
  278.             }
  279.             
  280.             if(sectors == 1){
  281.             
  282. #if 1                
  283. memcpy(media_ptr -> fx_media_memory_buffer_orig + 4*512u, media_ptr -> fx_media_driver_buffer, 512); 
  284.                 cache_entry++;
  285.                 cache_entry++;
  286.                 cache_entry++;
  287.                 cache_entry++;
  288.                 //jerryc_april_14
  289.             cache_entry -> fx_cached_sector =  media_ptr -> fx_media_sector_cache_list_ptr->fx_cached_sector;
  290.                 //jerryc_april_14
  291. #endif
  292.             }
  293. #else
  294.             cache_entry -> fx_cached_sector =  logical_sector;
  295. #endif
  296.             /* Place this entry that the head of the cached sector
  297.                list.  */
  298.             /* Determine if we need to update the last used list.  */
  299.             if (previous_cache_entry)
  300.             {
  301.                 /* Yes, the current entry is not at the front of the list
  302.                    so we need to change the order.  */
  303.                 
  304.                 /* Link the previous entry to this entry's next pointer.  */
  305.                 previous_cache_entry -> fx_cached_sector_next_used =
  306.                                       cache_entry -> fx_cached_sector_next_used;
  307.                 /* Place this entry at the head of the list.  */
  308.                 cache_entry -> fx_cached_sector_next_used =  
  309.                                     media_ptr -> fx_media_sector_cache_list_ptr;
  310.                 media_ptr -> fx_media_sector_cache_list_ptr =  cache_entry;
  311.             }
  312.         }
  313.         else
  314.         {
  315.             /* Invalidate the cache entry on read errors.  */
  316.             cache_entry -> fx_cached_sector =  0;
  317.         }
  318.         /* Simply setup the pointer to this buffer and return.  */
  319.         media_ptr -> fx_media_memory_buffer =  cache_entry -> fx_cached_sector_memory_buffer;
  320.         /* Return the driver status.  */
  321.         return(media_ptr -> fx_media_driver_status);
  322.     }
  323.     else
  324.     {
  325.         /* Direct I/O to application buffer area.  */
  326.         /* Compare against logical sector to make sure it is valid.  */
  327.         //if (logical_sector >= (ULONG) media_ptr -> fx_media_total_sectors)
  328.         //    return(FX_SECTOR_INVALID);
  329.         /* Build Read request to the driver.  */
  330.         media_ptr -> fx_media_driver_request =          FX_DRIVER_READ;
  331.         media_ptr -> fx_media_driver_status =           FX_IO_ERROR;
  332.         media_ptr -> fx_media_driver_buffer =           buffer_ptr;
  333.         media_ptr -> fx_media_driver_logical_sector =   logical_sector;
  334.         media_ptr -> fx_media_driver_sectors =          sectors;
  335.         /* Invoke the driver to read the sector.  */
  336.         (media_ptr -> fx_media_driver_entry) (media_ptr);
  337.         /* Return the driver status.  */
  338.         return(media_ptr -> fx_media_driver_status);
  339.     }
  340. }
  341. #endif