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

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. /**   File (FIL)                                                          */
  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_fil.h"
  44. #include "PlaycoreFileSysFileXfx_uti.h"
  45. #include "DriveCardReaderCardReaderDriver.h"
  46. #ifdef FILEX_READ_4_SECTORS
  47. #include "mediacardsincluderegister.h"
  48. extern CARD_DETECT_TYPE card_type;
  49. extern int I49_BitstreamWrite8Bit(UINT8 *data,UINT16 length);
  50. #include "kerneluitronrtos.h"
  51. #endif
  52. /**************************************************************************/ 
  53. /*                                                                        */ 
  54. /*  FUNCTION                                               RELEASE        */ 
  55. /*                                                                        */ 
  56. /*    _fx_file_read                                       PORTABLE C      */ 
  57. /*                                                           3.0          */ 
  58. /*  AUTHOR                                                                */ 
  59. /*                                                                        */ 
  60. /*    William E. Lamie, Express Logic, Inc.                               */ 
  61. /*                                                                        */ 
  62. /*  DESCRIPTION                                                           */ 
  63. /*                                                                        */ 
  64. /*    This function reads the specified number of bytes (or as many as    */ 
  65. /*    possible into the buffer supplied by the caller.  The actual number */ 
  66. /*    of bytes and the status of the read operation is returned to the    */ 
  67. /*    caller.  In addition, various internal file pointers in the file    */ 
  68. /*    control block are also updated.                                     */ 
  69. /*                                                                        */ 
  70. /*  INPUT                                                                 */ 
  71. /*                                                                        */ 
  72. /*    file_ptr                              File control block pointer    */ 
  73. /*    buffer_ptr                            Buffer pointer                */ 
  74. /*    request_size                          Number of bytes requested     */ 
  75. /*    actual_size                           Pointer to variable for the   */ 
  76. /*                                            number of bytes read        */ 
  77. /*                                                                        */ 
  78. /*  OUTPUT                                                                */ 
  79. /*                                                                        */ 
  80. /*    return status                                                       */ 
  81. /*                                                                        */ 
  82. /*  CALLS                                                                 */ 
  83. /*                                                                        */ 
  84. /*    _fx_utility_FAT_entry_read            Read a FAT entry              */ 
  85. /*    _fx_utility_log_sector_read           Read a logical sector         */ 
  86. /*    _fx_utility_memory_copy               Fast memory copy routine      */ 
  87. /*                                                                        */ 
  88. /*  CALLED BY                                                             */ 
  89. /*                                                                        */ 
  90. /*    Application Code                                                    */ 
  91. /*                                                                        */ 
  92. /*  RELEASE HISTORY                                                       */ 
  93. /*                                                                        */ 
  94. /*    DATE              NAME                      DESCRIPTION             */ 
  95. /*                                                                        */ 
  96. /*  01-01-1999     William E. Lamie         Initial Version 1.0           */ 
  97. /*  03-01-2000     William E. Lamie         Modified comment(s),          */ 
  98. /*                                            resulting in version 1.0b.  */ 
  99. /*  01-28-2001     William E. Lamie         Modified comment(s) and       */ 
  100. /*                                            corrected exact end-of-file */ 
  101. /*                                            problem that caused data    */ 
  102. /*                                            not to be copied into the   */ 
  103. /*                                            user's buffer, resulting in */ 
  104. /*                                            version 2.0.                */ 
  105. /*  03-01-2002     Mohammad N. Minhaz       Changed to 32 bit variables,  */ 
  106. /*                                            consecutive sectors read    */
  107. /*                                            together, resulting in      */ 
  108. /*                                            version 3.0.                */
  109. /*                                                                        */ 
  110. /**************************************************************************/ 
  111. UINT  _fx_file_read(FX_FILE *file_ptr, VOID *buffer_ptr, ULONG request_size, ULONG *actual_size)
  112. {
  113. UINT        status;
  114. ULONG       bytes_remaining, i;
  115. ULONG       copy_bytes;
  116. UCHAR_PTR   destination_ptr;
  117. ULONG       cluster, next_cluster;
  118. ULONG       sectors;
  119. FX_MEDIA    *media_ptr;
  120. ULONG       fat_reserved; 
  121.     /* First, determine if the file is still open.  */
  122.     if (file_ptr -> fx_file_id != FX_FILE_ID)
  123.     {
  124.         /* Return the file not open error status.  */
  125.         return(FX_NOT_OPEN);
  126.     }
  127.     /* Setup pointer to associated media control block.  */
  128.     media_ptr =  file_ptr -> fx_file_media_ptr;
  129.     /* Determine the type of FAT and setup variables accordingly.  */
  130.     if (media_ptr -> fx_media_32_bit_FAT) fat_reserved = FX_RESERVED_1_32;
  131.     else fat_reserved = FX_RESERVED_1;
  132.     /* Protect against other threads accessing the media.  */
  133.     FX_PROTECT
  134.     /* Next, determine if there is any more bytes to read in the file.  */
  135.     if (file_ptr -> fx_file_current_file_offset >=
  136.                                     file_ptr -> fx_file_current_file_size)
  137.     {
  138.         /* Release media protection.  */
  139.         FX_UNPROTECT
  140.         /* The file is at the end, return the proper status and set the
  141.            actual size to 0.  */
  142.         *actual_size =  0;
  143.         return(FX_END_OF_FILE);
  144.     }
  145.     /* At this point there is something to read.  */
  146.     /* Setup local buffer pointer.  */
  147.     destination_ptr =  (UCHAR_PTR) buffer_ptr;
  148.     /* Determine if there are less bytes left in the file than that specified
  149.        by the request.  If so, ajust the requested size.  */
  150.     if (request_size > 
  151.                 (file_ptr -> fx_file_current_file_size - file_ptr -> fx_file_current_file_offset))
  152.     {
  153.         /* Adjust the bytes remaining to what's available.  */
  154.         request_size =  file_ptr -> fx_file_current_file_size - file_ptr -> fx_file_current_file_offset;
  155.     }
  156.     /* Setup the remaining number of bytes to read.  */
  157.     bytes_remaining =  request_size;
  158.     /* Loop to read all of the bytes.  */
  159.     while (bytes_remaining)
  160.     {
  161.         /* Determine if a beginning or ending partial read is required.  */
  162.         if ((file_ptr -> fx_file_current_logical_offset) || 
  163.                     (bytes_remaining < media_ptr -> fx_media_bytes_per_sector))
  164.         {
  165.             /* A partial sector read is required.  */
  166.             /* Read the current logical sector.  */
  167.             status =  _fx_utility_log_sector_read(media_ptr,
  168.                             file_ptr -> fx_file_current_logical_sector,
  169.                             media_ptr -> fx_media_memory_buffer, (ULONG) 1);
  170.             /* Check for good completion status.  */
  171.             if (status !=  FX_SUCCESS)
  172.             {
  173.                 /* Release media protection.  */
  174.                 FX_UNPROTECT
  175.                 /* Return the error status.  */
  176.                 return(status);
  177.             }
  178.             /* Copy the appropriate number of bytes into the destination buffer.  */
  179.             copy_bytes =  media_ptr -> fx_media_bytes_per_sector - 
  180.                                             file_ptr -> fx_file_current_logical_offset;
  181.             
  182.             /* Check to see if only a portion of the read sector needs to be
  183.                copied.  */
  184.             if (copy_bytes > bytes_remaining)
  185.             {
  186.             
  187.                 /* Adjust the number of bytes to copy.  */
  188.                 copy_bytes =  bytes_remaining;
  189.             }
  190.             /* Actually perform the memory copy.  */
  191.             if (!IsDestDecoder(destination_ptr)) 
  192.             memcpy( destination_ptr,
  193.              ((UCHAR_PTR) media_ptr -> fx_media_memory_buffer) +
  194.                  file_ptr -> fx_file_current_logical_offset,
  195.                                                     copy_bytes);
  196.             else
  197.               printf("todo: fx_fr error!!!n"); 
  198.             /* Increment the logical sector byte offset.  */
  199.             file_ptr -> fx_file_current_logical_offset =  
  200.                      file_ptr -> fx_file_current_logical_offset + copy_bytes;
  201.         
  202.             /* Adjust the remaining bytes to read.  */
  203.             bytes_remaining =  bytes_remaining - copy_bytes;
  204.             /* Adjust the pointer to the destination buffer.  */
  205.             if (!IsDestDecoder(destination_ptr)) //jerry cai
  206.             destination_ptr =  destination_ptr + copy_bytes;
  207.         }
  208.         else
  209.         {
  210.             /* Attempt to read multiple sectors directly into the destination
  211.                buffer.  */
  212.             /* Calculate the number of whole sectors to read directly into
  213.                the destination buffer.  */
  214.             sectors =  (UINT) (bytes_remaining/media_ptr -> fx_media_bytes_per_sector);
  215.             next_cluster = cluster = file_ptr -> fx_file_current_physical_cluster;
  216.             for(i = (media_ptr -> fx_media_sectors_per_cluster -
  217.                             file_ptr -> fx_file_current_relative_sector); i<sectors; i += media_ptr->fx_media_sectors_per_cluster)
  218.             {
  219.                 status =  _fx_utility_FAT_entry_read(media_ptr, cluster, &next_cluster);
  220.                 /* Determine if an error is present.  */
  221.                 if ((status != FX_SUCCESS) || (next_cluster < FX_FAT_ENTRY_START) ||
  222.                     (next_cluster > fat_reserved))
  223.                 {
  224.                     /* Release media protection.  */
  225.                     FX_UNPROTECT
  226.                     /* Send error message back to caller.  */
  227.                     if (status != FX_SUCCESS)
  228.                         return(status);
  229. //                    else
  230. //                        return(FX_FILE_CORRUPT);
  231.                 }
  232.                 if (next_cluster != cluster + 1)
  233.                     break;
  234.                 else
  235.                     cluster = next_cluster;
  236.             }
  237.             if (i < sectors)
  238.                 sectors = i;
  239. #ifdef FILEX_READ_4_SECTORS
  240. if(CARD_DETECT_SSFDC == card_type || CARD_DETECT_XD == card_type){
  241.                 
  242.             /* Perform the data read directly into the user's buffer of
  243.                the appropriate number of sectors.  */
  244.         
  245.              UINT remaining_sectors;
  246.                 remaining_sectors = sectors;
  247.                 while(remaining_sectors > 0){ 
  248.                  UINT unit = 4;
  249.                     
  250.                  if(remaining_sectors < unit)
  251.                      unit = remaining_sectors;
  252. if(unit > 4)
  253.                      printf("n");                                      
  254.                     status =  _fx_utility_log_sector_read(media_ptr, file_ptr -> fx_file_current_logical_sector + (sectors - remaining_sectors),
  255.                                                                           IsDestDecoder(destination_ptr)? media_ptr -> fx_media_memory_buffer_orig: 
  256.                                                                           destination_ptr, 
  257.                                                                           (ULONG) unit);
  258.                     if(IsDestDecoder(destination_ptr)){
  259.                         I49_BitstreamWrite8Bit((UINT8 *)media_ptr -> fx_media_memory_buffer_orig,unit*512u);
  260.                     }
  261.                     remaining_sectors -= unit;
  262.                  }
  263. }
  264.         else
  265.                     status =  _fx_utility_log_sector_read(media_ptr, file_ptr -> fx_file_current_logical_sector,
  266.                      destination_ptr, 
  267.                                                                           (ULONG) sectors);
  268.         
  269. #else
  270.             status =  _fx_utility_log_sector_read(media_ptr, file_ptr -> fx_file_current_logical_sector,
  271.                                                                   destination_ptr, (ULONG) sectors);
  272. #endif
  273.             /* Check for good completion status.  */
  274.             if (status !=  FX_SUCCESS)
  275.             {
  276.                 /* Release media protection.  */
  277.                 FX_UNPROTECT
  278.                 /* Return the error status.  */
  279.                 return(status);
  280.             }
  281.             /* Now adjust the various file pointers.  */
  282.             /* Increment the current logical sector.  Subtract one from
  283.                the sector count because we are going to use the logical
  284.                offset to do additional sector/cluster arithmetic below.  */
  285.             file_ptr -> fx_file_current_logical_sector =
  286.                         file_ptr -> fx_file_current_logical_sector +
  287.                         (sectors - 1);
  288.             /* Move the relative sector and cluster as well.  */
  289.             file_ptr -> fx_file_current_relative_cluster = file_ptr -> fx_file_current_relative_cluster
  290.                     + (file_ptr -> fx_file_current_relative_sector + (sectors - 1))
  291.                     / media_ptr -> fx_media_sectors_per_cluster;
  292.             file_ptr -> fx_file_current_relative_sector =
  293.                         (file_ptr -> fx_file_current_relative_sector +
  294.                         (sectors - 1)) % media_ptr -> fx_media_sectors_per_cluster;
  295.             /* Increment the logical sector byte offset.  */
  296.             file_ptr -> fx_file_current_logical_offset =
  297.                         media_ptr -> fx_media_bytes_per_sector;
  298.             file_ptr -> fx_file_current_physical_cluster = cluster;
  299.             /* Adjust the remaining bytes.  */
  300.             bytes_remaining =  bytes_remaining -
  301.                                (((ULONG) media_ptr -> fx_media_bytes_per_sector) * sectors);
  302.             if (!IsDestDecoder(destination_ptr)) 
  303.             {
  304.                /* Adjust the pointer to the destination buffer.  */
  305.                destination_ptr =  destination_ptr +
  306.                                    (((ULONG) media_ptr -> fx_media_bytes_per_sector) * sectors);
  307.             }
  308.             if ((next_cluster < FX_FAT_ENTRY_START) || (next_cluster > fat_reserved))
  309.                return(FX_END_OF_FILE);
  310.         }
  311.         /* At this point, we have either read a partial sector or have successfully
  312.            read one or more whole sectors.  Determine if we are at the end of
  313.            the current logical sector.  */
  314.         if (file_ptr -> fx_file_current_logical_offset >=
  315.                                     media_ptr -> fx_media_bytes_per_sector)
  316.         {
  317.             /* Determine if we are at the exact physical end of the file at the end of reading.  */
  318.             if ((bytes_remaining == 0) && ((file_ptr -> fx_file_current_file_offset + request_size) >=
  319.                                                 file_ptr -> fx_file_current_available_size))
  320.             {
  321.                 /* Skip the following file parameter adjustments.  The next write will
  322.                    detect the logical offset out of the range of the sector and reset
  323.                    all of the pertinent information.  */
  324.                 break;
  325.             }
  326.             /* We need to move to the next logical sector, but first
  327.                determine if the next logical sector is within the same
  328.                cluster.  */
  329.             /* Increment the current relative sector in the cluster.  */
  330.             file_ptr -> fx_file_current_relative_sector++;
  331.             /* Determine if this is in a new cluster.  */
  332.             if (file_ptr -> fx_file_current_relative_sector >=
  333.                     media_ptr -> fx_media_sectors_per_cluster)
  334.             {
  335.                 /* Read the FAT entry of the current cluster to find
  336.                    the next cluster.  */
  337.                 status =  _fx_utility_FAT_entry_read(media_ptr,
  338.                         file_ptr -> fx_file_current_physical_cluster, &next_cluster);
  339.                 /* Determine if an error is present.  */
  340.                 if ((status != FX_SUCCESS) || (next_cluster < FX_FAT_ENTRY_START) ||
  341.                     (next_cluster > fat_reserved))
  342.                 {
  343.                     /* Release media protection.  */
  344.                     FX_UNPROTECT
  345.                     /* Send error message back to caller.  */
  346.                     if (status != FX_SUCCESS)
  347.                         return(status);
  348.                     else
  349.                         return(FX_FILE_CORRUPT);
  350.                 }
  351.                 /* Otherwise, we have a new cluster.  Save it in the file
  352.                    control block and calculate a new logical sector value.  */
  353.                 file_ptr -> fx_file_current_physical_cluster =  next_cluster;
  354.                 file_ptr -> fx_file_current_relative_cluster++;
  355.                 file_ptr -> fx_file_current_logical_sector = ((ULONG) media_ptr -> fx_media_data_sector_start) +
  356.                                                               ((((ULONG) next_cluster) - FX_FAT_ENTRY_START) *
  357.                                                                 ((ULONG) media_ptr -> fx_media_sectors_per_cluster));
  358.                 file_ptr -> fx_file_current_relative_sector =  0;
  359.             }
  360.             else
  361.             {
  362.                 /* Still within the same cluster so just increment the
  363.                    logical sector.  */
  364.                 file_ptr -> fx_file_current_logical_sector++;
  365.             }
  366.             /* In either case, we are now positioned at a new sector so
  367.                clear the logical sector offset.  */
  368.             file_ptr -> fx_file_current_logical_offset =  0;
  369.         }
  370.     }
  371.     dbg_printf(("fx_file_read, size:%x, offset:%lxn", request_size, file_ptr -> fx_file_current_file_offset));
  372.     /* Adjust the current file offset accordingly.  */
  373.     file_ptr -> fx_file_current_file_offset =
  374.                     file_ptr -> fx_file_current_file_offset + request_size;
  375.     /* Store the number of bytes actually read.  */
  376.     *actual_size =  request_size;
  377.     /* Release media protection.  */
  378.     FX_UNPROTECT
  379.     /* Return a successful status to the caller.  */
  380.     return(FX_SUCCESS);
  381. }
  382. #endif