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

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. /**   Media (MED)                                                         */
  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_med.h"
  44. #include "PlaycoreFileSysFileXfx_uti.h"
  45. /**************************************************************************/ 
  46. /*                                                                        */ 
  47. /*  FUNCTION                                               RELEASE        */ 
  48. /*                                                                        */ 
  49. /*    _fx_media_open                                      PORTABLE C      */ 
  50. /*                                                           3.0          */ 
  51. /*  AUTHOR                                                                */ 
  52. /*                                                                        */ 
  53. /*    William E. Lamie, Express Logic, Inc.                               */ 
  54. /*                                                                        */ 
  55. /*  DESCRIPTION                                                           */ 
  56. /*                                                                        */ 
  57. /*    This function opens the specified media with the supplied device    */ 
  58. /*    driver.  The specified media must conform to the MS-DOS compatible  */ 
  59. /*    file format, which is verified during the media open process.  In   */ 
  60. /*    addition, the supplied FileX device driver must also conform to     */ 
  61. /*    the FileX device driver specification.                              */ 
  62. /*                                                                        */ 
  63. /*    The MS-DOS boot sector (512 bytes) that is verified by this         */ 
  64. /*    function must look like the following:                              */ 
  65. /*                                                                        */ 
  66. /*          Byte Offset         Meaning             Size                  */ 
  67. /*                                                                        */ 
  68. /*            0x000         Jump Instructions        3                    */ 
  69. /*            0x003         OEM Name                 8                    */ 
  70. /*            0x00B        *Bytes per Sector         2                    */ 
  71. /*            0x00D        *Sectors per Cluster      1                    */ 
  72. /*            0x00E        *Reserved Sectors         2                    */ 
  73. /*            0x010        *Number of FATs           1                    */ 
  74. /*            0x011        *Max Root Dir Entries     2                    */ 
  75. /*            0x013        *Number of Sectors        2                    */ 
  76. /*            0x015         Media Type               1                    */ 
  77. /*            0x016        *Sectors per FAT          2                    */ 
  78. /*            0x018        *Sectors per Track        2                    */ 
  79. /*            0x01A        *Number of Heads          2                    */ 
  80. /*            0x01C        *Hidden Sectors           4                    */ 
  81. /*            0x020        *Huge Sectors             4                    */ 
  82. /*            0x024         Drive Number             1                    */ 
  83. /*            0x025         Reserved                 1                    */ 
  84. /*            0x026         Boot Signature           1                    */ 
  85. /*            0x027         Volume ID                4                    */ 
  86. /*            0x02B         Volume Label             11                   */ 
  87. /*            0x036         File System Type         8                    */ 
  88. /*             ...              ...                 ...                   */ 
  89. /*            0x1FE       **Signature (0x55aa)       2                    */ 
  90. /*                                                                        */ 
  91. /*            * Denotes which elements of the boot record                 */ 
  92. /*              FileX uses.                                               */ 
  93. /*                                                                        */ 
  94. /*            **Denotes the element is checked by the I/O                 */ 
  95. /*              driver.  This eliminates the need for a minimum           */ 
  96. /*              512-byte buffer for FileX.                                */ 
  97. /*                                                                        */ 
  98. /*  Note: All values above are in little endian format, i.e. the LSB is   */ 
  99. /*        in the lowest address.                                          */ 
  100. /*                                                                        */ 
  101. /*  INPUT                                                                 */ 
  102. /*                                                                        */ 
  103. /*    media_ptr                             Media control block pointer   */ 
  104. /*    media_name                            Pointer to media name string  */ 
  105. /*    media_driver                          Media driver entry function   */ 
  106. /*    driver_info_ptr                       Optional information pointer  */ 
  107. /*                                            supplied to media driver    */ 
  108. /*    memory_ptr                            Pointer to memory used by the */ 
  109. /*                                            FileX for this media.       */ 
  110. /*    memory_size                           Size of media memory - must   */ 
  111. /*                                            at least 512 bytes and      */ 
  112. /*                                            one sector size.            */ 
  113. /*                                                                        */ 
  114. /*  OUTPUT                                                                */ 
  115. /*                                                                        */ 
  116. /*    return status                                                       */ 
  117. /*                                                                        */ 
  118. /*  CALLS                                                                 */ 
  119. /*                                                                        */ 
  120. /*    I/O Driver                                                          */ 
  121. /*    _fx_media_boot_info_extract           Extract media information     */ 
  122. /*    _fx_utility_FAT_entry_read            Pickup FAT entry contents     */ 
  123. /*    tx_semaphore_create                   Create protection semaphore   */ 
  124. /*                                                                        */ 
  125. /*  CALLED BY                                                             */ 
  126. /*                                                                        */ 
  127. /*    Application Code                                                    */ 
  128. /*                                                                        */ 
  129. /*  RELEASE HISTORY                                                       */ 
  130. /*                                                                        */ 
  131. /*    DATE              NAME                      DESCRIPTION             */ 
  132. /*                                                                        */ 
  133. /*  01-01-1999     William E. Lamie         Initial Version 1.0           */ 
  134. /*  04-19-1999     William E. Lamie         Corrected logic for 12/16-bit */ 
  135. /*                                            FAT determination,          */ 
  136. /*                                            resulting in version 1.0a   */ 
  137. /*  03-01-2000     William E. Lamie         Modified comment(s), moved    */ 
  138. /*                                            data sector start setup to  */ 
  139. /*                                            ensure it is valid prior to */ 
  140. /*                                            calculating total clusters, */ 
  141. /*                                            and added initialization    */ 
  142. /*                                            current path string,        */ 
  143. /*                                            resulting in version 1.0b.  */ 
  144. /*  01-28-2001     William E. Lamie         Modified comment(s),          */ 
  145. /*                                            added support for logical   */ 
  146. /*                                            sector cache, optimized FAT */ 
  147. /*                                            entry search, and local     */ 
  148. /*                                            path setup, resulting in    */ 
  149. /*                                            version 2.0.                */
  150. /*  03-01-2002     Mohammad N. Minhaz       Modified comment(s), and      */ 
  151. /*                                            added logic to read FAT32   */ 
  152. /*                                            boot record, resulting in   */ 
  153. /*                                            version 3.0.                */ 
  154. /*                                                                        */ 
  155. /**************************************************************************/ 
  156. UINT  _fx_media_open(FX_MEDIA *media_ptr, CHAR *media_name,
  157.                  VOID (*media_driver)(FX_MEDIA *), VOID *driver_info_ptr, 
  158.                  VOID *memory_ptr, ULONG memory_size, UINT partition)
  159. {
  160. FX_MEDIA_PTR        tail_ptr;
  161. ULONG                cluster_number;
  162. ULONG                FAT_entry;
  163. ULONG                i;
  164. FX_CACHED_SECTOR    *cache_entry_ptr;
  165. UINT                status;
  166.     /* Save the basic information in the media control block.  */
  167.     media_ptr -> fx_media_name               = media_name;
  168.     media_ptr -> fx_media_driver_entry       = media_driver;
  169.     media_ptr -> fx_media_memory_buffer      = (UCHAR_PTR)memory_ptr;
  170.     media_ptr -> fx_media_sector_cache_end   = (UCHAR_PTR)memory_ptr;
  171.     media_ptr -> fx_media_memory_buffer_orig = (UCHAR_PTR)memory_ptr;
  172.     media_ptr -> fx_media_memory_size        = memory_size;
  173.     /* Initialize the supplied media I/O driver.  First, build the 
  174.        initialize driver request.  */
  175.     media_ptr -> fx_media_driver_request =              FX_DRIVER_INIT;
  176.     media_ptr -> fx_media_driver_status =               FX_IO_ERROR;
  177.     media_ptr -> fx_media_driver_buffer         = memory_ptr;
  178.     media_ptr -> fx_media_driver_info =                 driver_info_ptr;
  179.     media_ptr -> fx_media_driver_write_protect =        FX_FALSE;
  180.     media_ptr -> fx_media_driver_free_sector_update =   FX_FALSE;
  181.     /* Call the specified I/O driver with the initialize request.  */
  182.     (media_ptr -> fx_media_driver_entry) (media_ptr);
  183.     /* Determine if the I/O driver initialized successfully.  */
  184.     if (media_ptr -> fx_media_driver_status != FX_SUCCESS)
  185.     {
  186.         /* Return the driver error status.  */
  187.         return(FX_IO_ERROR);
  188.     }
  189.     // Get geometry of device.
  190.     status = _fx_utility_get_geometry( media_ptr, FALSE, 0, 0 );
  191.     
  192.     if ( status != FX_SUCCESS )
  193.     {
  194.         return status;
  195.     }
  196.     /* Read the boot sector from the device.  Build the read boot sector
  197.        command. fx_media_driver_logical_sector holds the partition to be
  198.        opened. */
  199.     media_ptr -> fx_media_driver_request        = FX_DRIVER_BOOT_READ;
  200.     media_ptr -> fx_media_driver_status         = FX_IO_ERROR;
  201.     media_ptr -> fx_media_driver_buffer         = memory_ptr;
  202.     media_ptr -> fx_media_driver_logical_sector = partition;
  203.     media_ptr -> fx_media_driver_sectors        = 1;
  204.     /* Invoke the driver to read the boot sector.  */
  205.     (media_ptr -> fx_media_driver_entry) (media_ptr);
  206.     /* Determine if the boot sector was read correctly. */
  207.     if (media_ptr -> fx_media_driver_status != FX_SUCCESS)
  208.     {
  209.         /* Create the media protection structure if FX_SINGLE_THREAD is not
  210.         defined.  */
  211. #ifndef FX_SINGLE_THREAD
  212.         os_semaphore_create(&(media_ptr -> fx_media_protect), "FileX Media Semaphore", 1);
  213. #endif
  214.         /* Load the media ID field in the media control block.  */
  215.         media_ptr -> fx_media_id =  (ULONG) FX_MEDIA_DIRECT_ID;
  216.         /* There are no hidden sectors in the media. */
  217.         media_ptr -> fx_media_hidden_sectors = 0;
  218.         /* Default value. */
  219.         media_ptr ->fx_media_bytes_per_sector = 512;
  220.         /* Return a successful status.  */
  221.         return(FX_DIRECT_SUCCESS);
  222.     }
  223.     /* Extract and validate the media parameters from the boot sector.  */
  224.     if (_fx_media_boot_info_extract(media_ptr,
  225.                                     media_ptr -> fx_media_driver_buffer) !=
  226.                                     FX_SUCCESS)
  227.     {
  228.         /* Return the invalid media error status.  */
  229.         return(FX_MEDIA_INVALID);
  230.     }
  231.     /* Determine how many logical sector's can be cached with user's supplied 
  232.        buffer area - there must be at least enough for one sector!  */
  233.     media_ptr -> fx_media_sector_cache_size =  memory_size / media_ptr -> fx_media_bytes_per_sector;
  234.     /* Is there at least one?  */
  235.     if (media_ptr -> fx_media_sector_cache_size == 0)
  236.     {
  237.         /* Error in the buffer size supplied by user.  */
  238.         return(FX_BUFFER_ERROR);
  239.     }
  240.     /* Adjust the internal cache to fit the fixed number of sector cache control blocks
  241.        built into the media control block.  */
  242.     if (media_ptr -> fx_media_sector_cache_size > FX_MAX_SECTOR_CACHE)
  243.     {
  244.         /* Adjust the number of cache sectors downward.  If this is insufficient,
  245.            the FX_MAX_SECTOR_CACHE constant in FX_API.H must be changed and the FileX
  246.            library must be rebuilt.  */
  247.         media_ptr -> fx_media_sector_cache_size =  FX_MAX_SECTOR_CACHE;
  248.     }
  249.     /* Otherwise, everything is okay.  Initialize the data structures for managing the 
  250.        logical sector cache.  */
  251.     i =  (UINT) media_ptr -> fx_media_sector_cache_size;
  252.     cache_entry_ptr =  media_ptr -> fx_media_sector_cache;
  253.     while (i--)
  254.     {
  255.         /* Initialize each of the cache entries.  */
  256.         cache_entry_ptr -> fx_cached_sector_memory_buffer =  (UCHAR_PTR) memory_ptr;
  257.         cache_entry_ptr -> fx_cached_sector =                0;
  258.         cache_entry_ptr -> fx_cached_sector_buffer_dirty =   FX_FALSE;
  259.         cache_entry_ptr -> fx_cached_sector_next_used =      cache_entry_ptr + 1;
  260.         /* Move to the next cache sector entry.  */
  261.         cache_entry_ptr++;
  262.         /* Update the memory pointer to the next buffer slot.  */
  263.         memory_ptr =  (VOID *) (((UCHAR_PTR) memory_ptr) + media_ptr -> fx_media_bytes_per_sector);
  264.     }
  265.     /* Backup to the last cache entry to set it's next pointer to NULL.  */
  266.     cache_entry_ptr--;
  267.     cache_entry_ptr -> fx_cached_sector_next_used =  FX_NULL;
  268.     /* Remember the last memory address used by the caching logic.  */
  269.     media_ptr -> fx_media_sector_cache_end =  (UCHAR_PTR) memory_ptr;
  270.     /* Setup the head pointer of the list.  */
  271.     media_ptr -> fx_media_sector_cache_list_ptr =  media_ptr -> fx_media_sector_cache;
  272.     if (media_ptr -> fx_media_32_bit_FAT == FX_TRUE)
  273.     {
  274.         /* fx_media_root_sector_start has been extracted from FX_ROOT_CLUS_32 in fx_mbie.c */
  275.         media_ptr -> fx_media_root_sector_start = media_ptr -> fx_media_data_sector_start +
  276.             (media_ptr -> fx_media_root_clus_32 - FX_FAT_ENTRY_START) * 
  277.             media_ptr -> fx_media_sectors_per_cluster;
  278.         /* Calculate maximum possible value for fx_media_root_directory_entries */
  279.         i = 0;
  280.         for(cluster_number = media_ptr->fx_media_root_clus_32;;)
  281.         {
  282.             status =  _fx_utility_FAT_entry_read(media_ptr, cluster_number, &FAT_entry);
  283.             i++;
  284.             /* Determine if the read was successful.  */
  285.             if (status != FX_SUCCESS)
  286.                 return(FX_FAT_READ_ERROR);
  287.             if ((cluster_number == FAT_entry) || (i > media_ptr -> fx_media_total_sectors))
  288.                 return(FX_FAT_READ_ERROR);
  289.             if (FAT_entry >= FX_RESERVED_1_32)
  290.                 break;
  291.             cluster_number = FAT_entry;
  292.         }
  293.         media_ptr -> fx_media_root_directory_entries = (i * media_ptr->fx_media_sectors_per_cluster * 
  294.             media_ptr->fx_media_bytes_per_sector) / FX_DIR_ENTRY_SIZE; 
  295.     }
  296.     /* Calculate the number of available clusters.  */
  297.     media_ptr -> fx_media_available_clusters =  0;
  298.     /* Set the cluster search start to an invalid value.  */
  299.     media_ptr -> fx_media_cluster_search_start =  0;
  300.     /* Loop to read each cluster entry in the first FAT.  */
  301.     for (cluster_number = FX_FAT_ENTRY_START; 
  302.                 cluster_number < (media_ptr -> fx_media_total_clusters) + FX_FAT_ENTRY_START; 
  303.                                                                                 cluster_number++)
  304.     {
  305.         /* Read a FAT entry.  */
  306.         status =  _fx_utility_FAT_entry_read(media_ptr, cluster_number, &FAT_entry);
  307.         /* Determine if the read was successful.  */
  308.         if (status != FX_SUCCESS)
  309.             return(FX_FAT_READ_ERROR);
  310.         /* Now determine if the FAT entry is available.  */
  311.         if (FAT_entry == FX_FREE_CLUSTER)
  312.         {
  313.             /* Increment the number of available clusters.  */
  314.             media_ptr -> fx_media_available_clusters++;
  315.             /* Check to see if the cluster search value is invalid.  */
  316.             if (media_ptr -> fx_media_cluster_search_start == 0)
  317.             {
  318.             
  319.                 /* Remember the first free cluster to start further searches from.  */
  320.                 media_ptr -> fx_media_cluster_search_start =  cluster_number;
  321.             }
  322.         }
  323.     }
  324.     /* If there were no free clusters, just set the search pointer to the 
  325.        first cluster number.  */
  326.     if (media_ptr -> fx_media_cluster_search_start == 0)
  327.         media_ptr -> fx_media_cluster_search_start =  FX_FAT_ENTRY_START;
  328.     /* Setup the current working directory fields to default to the root
  329.        directory.  */
  330.     media_ptr -> fx_media_default_path.fx_path_directory.fx_dir_entry_name[0] =     0;
  331.     media_ptr -> fx_media_default_path.fx_path_string[0] =                   (CHAR) 0;
  332.     media_ptr -> fx_media_default_path.fx_path_string[FX_MAXIMUM_PATH-1] =   (CHAR) 0;
  333.     media_ptr -> fx_media_default_path.fx_path_string[FX_MAXIMUM_PATH-2] =   (CHAR) 0;
  334.     media_ptr -> fx_media_default_path.fx_path_current_entry =                      0;
  335.     /* Initialize the opened file linked list and associated counter.  */
  336.     media_ptr -> fx_media_opened_file_list =      FX_NULL;
  337.     media_ptr -> fx_media_opened_file_count =     0;
  338.     /* Create the media protection structure if FX_SINGLE_THREAD is not
  339.        defined.  */
  340. #ifndef FX_SINGLE_THREAD
  341.     os_semaphore_create(&(media_ptr -> fx_media_protect), "FileX Media Semaphore", 1);
  342. #endif
  343.     /* At this point, the media has been opened successfully.  Place the 
  344.        media on the linked list of currently opened media.  */
  345.     /* Load the media ID field in the media control block.  */
  346.     media_ptr -> fx_media_id =  (ULONG) FX_MEDIA_ID;
  347.     /* Place the thread on the list of opened media.  First,
  348.        check for an empty list.  */
  349.     if (_fx_system_media_opened_ptr)
  350.     {
  351.         /* Pickup tail pointer.  */
  352.         tail_ptr =  _fx_system_media_opened_ptr -> fx_media_opened_previous;
  353.         /* Place the new media in the list.  */
  354.         _fx_system_media_opened_ptr -> fx_media_opened_previous =  media_ptr;
  355.         tail_ptr -> fx_media_opened_next =  media_ptr;
  356.         /* Setup this media's opened links.  */
  357.         media_ptr -> fx_media_opened_previous =  tail_ptr;
  358.         media_ptr -> fx_media_opened_next =      _fx_system_media_opened_ptr;
  359.     }
  360.     else
  361.     {
  362.         /* The opened media list is empty.  Add the media to empty list.  */
  363.         _fx_system_media_opened_ptr =           media_ptr;
  364.         media_ptr -> fx_media_opened_next =     media_ptr;
  365.         media_ptr -> fx_media_opened_previous = media_ptr;
  366.     }
  367.     /* Increment the opened media counter.  */
  368.     _fx_system_media_opened_count++;
  369.     /* Return a successful status.  */
  370.     return(FX_SUCCESS);
  371. }
  372. /**************************************************************************/ 
  373. /*                                                                        */ 
  374. /*  FUNCTION                                               RELEASE        */ 
  375. /*                                                                        */ 
  376. /*    _fx_media_invalidate                                PORTABLE C      */ 
  377. /*                                                           3.0          */ 
  378. /*  AUTHOR                                                                */ 
  379. /*                                                                        */ 
  380. /*    Ofir Alon, Zoran MicroElectronics Ltd.                              */ 
  381. /*                                                                        */ 
  382. /*  DESCRIPTION                                                           */ 
  383. /*                                                                        */ 
  384. /*    This function marks the specified media as closed, without touching */
  385. /*    anything else or making flush operations. If any memory needs to be */
  386. /*    freed, it will do so. This is used when we need to reopen a media   */
  387. /*    but it is possible that the media has changed (like a card taken    */
  388. /*    out and then another card inserted instead) so it will not be       */
  389. /*    to correct to flush the media.                                      */
  390. /*                                                                        */ 
  391. /*  INPUT                                                                 */ 
  392. /*                                                                        */ 
  393. /*    media_ptr                             Media control block pointer   */
  394. /*                                                                        */ 
  395. /*  OUTPUT                                                                */ 
  396. /*                                                                        */ 
  397. /*    Return status                                                       */ 
  398. /*                                                                        */ 
  399. /*  CALLS                                                                 */ 
  400. /*                                                                        */ 
  401. /*    None.                                                               */
  402. /*                                                                        */ 
  403. /*  CALLED BY                                                             */ 
  404. /*                                                                        */ 
  405. /*    Application Code                                                    */ 
  406. /*                                                                        */ 
  407. /*  RELEASE HISTORY                                                       */ 
  408. /*                                                                        */ 
  409. /*    DATE              NAME                      DESCRIPTION             */ 
  410. /*                                                                        */ 
  411. /*  10-04-2002     Ofir Alon                Initial Version 3.0           */ 
  412. /*                                                                        */ 
  413. /**************************************************************************/ 
  414. UINT _fx_media_invalidate( FX_MEDIA *media_ptr )
  415. {
  416.     // Mark media as closed.
  417.     media_ptr->fx_media_id = FX_MEDIA_CLOSED_ID;
  418. #ifndef FX_SINGLE_THREAD
  419.     tx_semaphore_delete(&(media_ptr -> fx_media_protect));
  420. #endif
  421.     return FX_SUCCESS;
  422. }
  423. #endif