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

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. // Remove any previous remapping for memory copy when compiling this 
  45. #undef _fx_utility_get_media_geometry
  46. #undef _fx_utility_create_boot_sector
  47. #undef _fx_utility_create_mbr_sector
  48. /**************************************************************************/ 
  49. /*                                                                        */ 
  50. /*  FUNCTION                                               RELEASE        */ 
  51. /*                                                                        */ 
  52. /*    _fx_utility_get_geometry                            PORTABLE C      */ 
  53. /*                                                           3.0          */ 
  54. /*  AUTHOR                                                                */ 
  55. /*                                                                        */ 
  56. /*    Ofir Alon, Zoran MicroElectronics Ltd.                              */
  57. /*                                                                        */ 
  58. /*  DESCRIPTION                                                           */ 
  59. /*                                                                        */ 
  60. /*    This function attempts to supply all relevant information regarding */
  61. /*    the media's geometry. It first invokes a driver service that        */
  62. /*    to obtain as much information as possible from lower levels. The    */
  63. /*    missing information is complemented by this function, by mean of    */
  64. /*    calculations and assumptions.                                       */ 
  65. /*                                                                        */ 
  66. /*  INPUT                                                                 */ 
  67. /*                                                                        */ 
  68. /*    media_ptr                             Media control block pointer   */ 
  69. /*                                                                        */ 
  70. /*  OUTPUT                                                                */ 
  71. /*                                                                        */ 
  72. /*    Return status                                                       */ 
  73. /*                                                                        */ 
  74. /*  CALLS                                                                 */ 
  75. /*                                                                        */ 
  76. /*    None                                                                */
  77. /*                                                                        */ 
  78. /*  CALLED BY                                                             */ 
  79. /*                                                                        */ 
  80. /*    _fx_media_full_format                                               */ 
  81. /*                                                                        */ 
  82. /*  RELEASE HISTORY                                                       */ 
  83. /*                                                                        */ 
  84. /*    DATE              NAME                      DESCRIPTION             */ 
  85. /*                                                                        */ 
  86. /*  07-16-2002     Ofir Alon                Initial Version 3.0           */ 
  87. /*                                                                        */ 
  88. /**************************************************************************/ 
  89. UINT _fx_utility_get_geometry( FX_MEDIA *media_ptr,
  90.                                BOOL      bPartition,
  91.                                ULONG     ulStart,
  92.                                ULONG     ulSize )
  93. {
  94.     UINT   uiStatus;
  95.     
  96.     ULONG  ulCapacity;
  97.     USHORT uwHeads;
  98.     USHORT uwSectorsPerTrack;
  99.     USHORT uwCylinders;
  100.     ULONG  ulTemp;
  101.     ULONG  ulHiddenSectors;
  102.     ULONG  ulSizeInSectors;
  103.     BYTE   byFatType;
  104.     BYTE   bySectorsPerCluster;
  105.     USHORT uwSectorsPerFat;
  106.     USHORT uwRootEntries;
  107.     USHORT uwDirectorySectors;
  108.     FX_GEOMETRY_PTR psGeometry = &media_ptr->fx_geometry;
  109.     // Fill by illegal values:
  110.     psGeometry->uwCylinders          = FX_UNDEF_CYLINDERS;
  111.     psGeometry->uwStartHead          = FX_UNDEF_START_END;
  112.     psGeometry->bySectorsPerCluster  = FX_UNDEF_CLUSTER_SIZE;
  113.     psGeometry->uwRootEntries        = FX_UNDEF_ROOT_ENTRIES;
  114.     psGeometry->ulSizeInSectors      = FX_UNDEF_SIZE_IN_SECTORS;
  115.     psGeometry->uwSectorsPerFAT      = FX_UNDEF_SECTORS_PER_FAT;
  116.     psGeometry->ulHiddenSectors      = FX_UNDEF_HIDDEN_SECTORS;
  117.     psGeometry->byFatType            = FX_UNDEF_FAT_TYPE;
  118.     psGeometry->ulPartitionAlignment = FX_UNDEF_PARTITION_ALIGNMENT;
  119.     psGeometry->ulMbrSector          = FX_UNDEF_MBR_SECTOR;
  120.     // Acquire geometry through a driver call, either for the device
  121.     // as a whole, or for a certain partition.
  122.     if ( bPartition == TRUE )
  123.     {
  124.         // We set the partition dimensions. They must not be run over by the
  125.         // driver call.
  126.         psGeometry->ulHiddenSectors = ulStart;
  127.         psGeometry->ulSizeInSectors = ulSize;
  128.         media_ptr->fx_media_driver_request = FX_DRIVER_GET_MEDIA_GEOMETRY;
  129.     } else
  130.     {
  131.         media_ptr->fx_media_driver_request = FX_DRIVER_GET_DEVICE_GEOMETRY;    
  132.     }
  133.     
  134.     media_ptr->fx_media_driver_status  =   FX_IO_ERROR;
  135.     (media_ptr->fx_media_driver_entry)( media_ptr );
  136.     
  137.     uiStatus = media_ptr->fx_media_driver_status;
  138.     if ( uiStatus != FX_SUCCESS )
  139.     {
  140.         return uiStatus;
  141.     }
  142.     // Do we have partition alignment restriction?
  143.     if( psGeometry->ulPartitionAlignment == FX_UNDEF_PARTITION_ALIGNMENT )
  144.     {
  145.         // No. We set it to no restriction.
  146.         psGeometry->ulPartitionAlignment = 1;
  147.     }
  148.     // Do we have MBR sector number?
  149.     if( psGeometry->ulMbrSector == FX_UNDEF_MBR_SECTOR )
  150.     {
  151.         // No. We set it to first sector in device.
  152.         psGeometry->ulMbrSector = 0;
  153.     }
  154.     // At this point, we can only continue if we got the size of the media.
  155.     if ( psGeometry->ulSizeInSectors == FX_UNDEF_SIZE_IN_SECTORS )
  156.     {
  157.         // No. We cannot calculate anything else.
  158.         return FX_SUCCESS;
  159.     }
  160.     // Copy result capacity.
  161.     ulCapacity = psGeometry->ulSizeInSectors;
  162.     // Now we check which information the driver has supplied, and calculate
  163.     // the information that was not supplied.
  164.     // Was the number of cylinders given by the driver?
  165.     if ( psGeometry->uwCylinders == FX_UNDEF_CYLINDERS )
  166.     {
  167.         // No. We will calculate it manually.
  168.         
  169.         // Set number of cylinders to max value.
  170.         uwCylinders = 1024;            
  171.         // Set number of heads to max value.
  172.         uwHeads = 16;                      
  173.         
  174.         // Compute divisor for heads.
  175.         ulTemp = uwCylinders * uwHeads;
  176.         // Compute value for sectors per track.
  177.         uwSectorsPerTrack = (USHORT)( ulCapacity / ulTemp );
  178.         // If no remainder, done!
  179.         if ( ulCapacity % ulTemp )
  180.         {            
  181.             // Else, increment number of sectors.
  182.             uwSectorsPerTrack++;                    
  183.             
  184.             // Compute divisor for heads.
  185.             ulTemp = uwCylinders * uwSectorsPerTrack;  
  186.             
  187.             // Compute value for heads.
  188.             uwHeads = (USHORT)( ulCapacity / ulTemp );
  189.             // If no remainder, done!
  190.             if ( ulCapacity % ulTemp )
  191.             {
  192.                 // Else, increment number of heads.
  193.                 uwHeads++;
  194.                 // Compute divisor for cylinders.
  195.                 ulTemp = uwHeads * uwSectorsPerTrack;
  196.                 
  197.                 // Update number of cylinders.
  198.                 uwCylinders = (USHORT)( ulCapacity / ulTemp );
  199.             }
  200.         }
  201.         // Keep calculated values in geometry structure.
  202.         psGeometry->uwCylinders       = uwCylinders;
  203.         psGeometry->uwHeads           = uwHeads;
  204.         psGeometry->uwSectorsPerTrack = uwSectorsPerTrack;
  205.     }
  206.     else
  207.     {
  208.        // Yes. We also get the number of heads and number of sectors per track.
  209.        uwCylinders       = psGeometry->uwCylinders;
  210.        uwHeads           = psGeometry->uwHeads;
  211.        uwSectorsPerTrack = psGeometry->uwSectorsPerTrack;
  212.     }
  213.     // Do we have hidden sectors set by the driver?
  214.     if( psGeometry->ulHiddenSectors == FX_UNDEF_HIDDEN_SECTORS )
  215.     {
  216.         // No. Set the number of hidden sectors to be 1 track. This
  217.         // is the default used here.
  218.         // :TODO: Ofir
  219.         // Need to be checked.
  220.         ulHiddenSectors = uwSectorsPerTrack;
  221.         psGeometry->ulHiddenSectors = ulHiddenSectors;
  222.     }
  223.     else
  224.     {
  225.         // Yes. Use the value given.
  226.         ulHiddenSectors = psGeometry->ulHiddenSectors;
  227.     }
  228.     // Do we have media size in sectors set by the driver?
  229.     if ( psGeometry->ulSizeInSectors == FX_UNDEF_SIZE_IN_SECTORS )
  230.     {
  231.         // No. Calculate it manually.
  232.         // :TODO: Ofir
  233.         // Need to be checked.
  234.        ulSizeInSectors = ((ULONG)(uwCylinders - 2) * uwHeads * uwSectorsPerTrack) +
  235.                               ((uwHeads - 1) * uwSectorsPerTrack);
  236.        psGeometry->ulSizeInSectors = ulSizeInSectors;
  237.     }
  238.     else
  239.     {
  240.        // Yes. Use the value given.
  241.        ulSizeInSectors = psGeometry->ulSizeInSectors;
  242.     }
  243.         
  244.     // Do we have the type of FAT set by the driver?
  245.     if( psGeometry->byFatType != FX_UNDEF_FAT_TYPE )
  246.     {
  247.         // Yes. Use the given value.
  248.        byFatType = psGeometry->byFatType;
  249.     }
  250.     else if ( ulSizeInSectors > 65530UL)
  251.     {
  252.        byFatType = FX_BIGDOS;
  253.     }
  254.     else if( ulSizeInSectors < 32680 )
  255.     {
  256.        byFatType = FX_FAT12;
  257.     }
  258.     else
  259.     {
  260.        byFatType = FX_FAT16;
  261.     }
  262.     psGeometry->byFatType = byFatType;
  263.     // Do we have the sectors per cluster set by the driver?
  264.     if ( psGeometry->bySectorsPerCluster != FX_UNDEF_CLUSTER_SIZE )
  265.     {
  266.         // Yes. Use the given value.
  267.         bySectorsPerCluster = psGeometry->bySectorsPerCluster;
  268.     }
  269.     else if ( byFatType == FX_FAT12 )
  270.     {
  271.         bySectorsPerCluster = 8;
  272.     }
  273.     else if ( byFatType == FX_FAT16 )
  274.     {
  275.         bySectorsPerCluster = 4;
  276.     }
  277.     else
  278.     { 
  279.         // We have BIGDOS.
  280.         USHORT uwTemp = (USHORT)(ulSizeInSectors / 262144L);
  281.         switch ( uwTemp )
  282.         {
  283.             case 0 :
  284.             {
  285.                 bySectorsPerCluster = 4;
  286.                 break;
  287.             }
  288.             case 1:
  289.             {
  290.                 bySectorsPerCluster = 8;
  291.                 break;
  292.             }
  293.             case 2:
  294.             case 3:
  295.             {
  296.                 bySectorsPerCluster = 16;
  297.                 break;
  298.             }
  299.             case 4:
  300.             case 5:
  301.             case 6:
  302.             case 7:
  303.             {
  304.                 bySectorsPerCluster = 32;
  305.                 break;
  306.             }
  307.             default :
  308.             {
  309.                 bySectorsPerCluster = 64;
  310.                 break;
  311.             }
  312.         }
  313.     }
  314.     psGeometry->bySectorsPerCluster = bySectorsPerCluster;
  315.     // Do we have the sectors per FAT set by the driver?
  316.     if( psGeometry->uwSectorsPerFAT != FX_UNDEF_SECTORS_PER_FAT )
  317.     {
  318.         // Yes. Use the given value;
  319.         uwSectorsPerFat = psGeometry->uwSectorsPerFAT;
  320.     }
  321.     else if ( byFatType == FX_FAT12 )
  322.     {
  323.         // No. We have FAT12.
  324.         uwSectorsPerFat = (USHORT)(((((ulSizeInSectors + bySectorsPerCluster - 1) / bySectorsPerCluster) * 12 + bySectorsPerCluster - 1) / bySectorsPerCluster) + FX_BOOT_SECTOR_SIZE - 1) / FX_BOOT_SECTOR_SIZE;
  325.     }
  326.     else { 
  327.         // No. We have FAT16 or BIGDOS.
  328.         uwSectorsPerFat = (USHORT)(((((ulSizeInSectors + bySectorsPerCluster - 1) / bySectorsPerCluster) * 2) + FX_BOOT_SECTOR_SIZE - 1) / FX_BOOT_SECTOR_SIZE);
  329.     }
  330.     psGeometry->uwSectorsPerFAT = uwSectorsPerFat;
  331.     // :TODO: Ofir
  332.     // FAT32 not supported.
  333.     // Do we have the number of root entries set by the driver?
  334.     if( psGeometry->uwRootEntries == FX_UNDEF_ROOT_ENTRIES )
  335.     {
  336.         // No. We calculate it manually.
  337.         uwDirectorySectors = (USHORT)(ulCapacity / 200);
  338.         
  339.         if ( uwDirectorySectors < 1 )
  340.         {
  341.             uwDirectorySectors = 1;
  342.         }
  343.        
  344.         if ( uwDirectorySectors > 32 )
  345.         {
  346.             uwDirectorySectors = 32;
  347.         }
  348.        
  349.         uwRootEntries = uwDirectorySectors * (FX_BOOT_SECTOR_SIZE /
  350.                                                 FX_DIR_ENTRY_SIZE);
  351.         psGeometry->uwRootEntries = uwRootEntries;
  352.     }
  353.     else
  354.     {
  355.         // Yes. Use the given value.
  356.         uwRootEntries = psGeometry->uwRootEntries;
  357.     }
  358.     // Do we have the start/end CHS set by the driver?
  359.     if( psGeometry->uwStartHead == FX_UNDEF_START_END )
  360.     {
  361.         // No. We calculate them here.
  362.         if ( uwHeads > 1 )
  363.         {
  364.             psGeometry->uwStartHead        = 1;
  365.             psGeometry->uwStartCylinder    = 0;
  366.             psGeometry->uwStartSector      = 1;
  367.         }
  368.         else
  369.         {
  370.             psGeometry->uwStartHead        = 0;
  371.             psGeometry->uwStartCylinder    = 1;
  372.             psGeometry->uwStartSector      = 1;
  373.         }
  374.         psGeometry->uwEndHead              = uwHeads - 1;
  375.         psGeometry->uwEndCylinder          = uwCylinders - 2;
  376.         psGeometry->uwEndSector            = uwSectorsPerTrack;
  377.      
  378.         psGeometry->ulPartitionStartSector = uwSectorsPerTrack;
  379.     }
  380.     // Done.
  381.     return FX_SUCCESS;
  382. }
  383. #endif