Fx_ugmg.c
上传用户:super_houu
上传日期:2008-09-21
资源大小:4099k
文件大小:16k
- /**************************************************************************/
- /* */
- /* 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"
- // Remove any previous remapping for memory copy when compiling this
- #undef _fx_utility_get_media_geometry
- #undef _fx_utility_create_boot_sector
- #undef _fx_utility_create_mbr_sector
- /**************************************************************************/
- /* */
- /* FUNCTION RELEASE */
- /* */
- /* _fx_utility_get_geometry PORTABLE C */
- /* 3.0 */
- /* AUTHOR */
- /* */
- /* Ofir Alon, Zoran MicroElectronics Ltd. */
- /* */
- /* DESCRIPTION */
- /* */
- /* This function attempts to supply all relevant information regarding */
- /* the media's geometry. It first invokes a driver service that */
- /* to obtain as much information as possible from lower levels. The */
- /* missing information is complemented by this function, by mean of */
- /* calculations and assumptions. */
- /* */
- /* INPUT */
- /* */
- /* media_ptr Media control block pointer */
- /* */
- /* OUTPUT */
- /* */
- /* Return status */
- /* */
- /* CALLS */
- /* */
- /* None */
- /* */
- /* CALLED BY */
- /* */
- /* _fx_media_full_format */
- /* */
- /* RELEASE HISTORY */
- /* */
- /* DATE NAME DESCRIPTION */
- /* */
- /* 07-16-2002 Ofir Alon Initial Version 3.0 */
- /* */
- /**************************************************************************/
- UINT _fx_utility_get_geometry( FX_MEDIA *media_ptr,
- BOOL bPartition,
- ULONG ulStart,
- ULONG ulSize )
- {
- UINT uiStatus;
-
- ULONG ulCapacity;
- USHORT uwHeads;
- USHORT uwSectorsPerTrack;
- USHORT uwCylinders;
- ULONG ulTemp;
- ULONG ulHiddenSectors;
- ULONG ulSizeInSectors;
- BYTE byFatType;
- BYTE bySectorsPerCluster;
- USHORT uwSectorsPerFat;
- USHORT uwRootEntries;
- USHORT uwDirectorySectors;
- FX_GEOMETRY_PTR psGeometry = &media_ptr->fx_geometry;
- // Fill by illegal values:
- psGeometry->uwCylinders = FX_UNDEF_CYLINDERS;
- psGeometry->uwStartHead = FX_UNDEF_START_END;
- psGeometry->bySectorsPerCluster = FX_UNDEF_CLUSTER_SIZE;
- psGeometry->uwRootEntries = FX_UNDEF_ROOT_ENTRIES;
- psGeometry->ulSizeInSectors = FX_UNDEF_SIZE_IN_SECTORS;
- psGeometry->uwSectorsPerFAT = FX_UNDEF_SECTORS_PER_FAT;
- psGeometry->ulHiddenSectors = FX_UNDEF_HIDDEN_SECTORS;
- psGeometry->byFatType = FX_UNDEF_FAT_TYPE;
- psGeometry->ulPartitionAlignment = FX_UNDEF_PARTITION_ALIGNMENT;
- psGeometry->ulMbrSector = FX_UNDEF_MBR_SECTOR;
- // Acquire geometry through a driver call, either for the device
- // as a whole, or for a certain partition.
- if ( bPartition == TRUE )
- {
- // We set the partition dimensions. They must not be run over by the
- // driver call.
- psGeometry->ulHiddenSectors = ulStart;
- psGeometry->ulSizeInSectors = ulSize;
- media_ptr->fx_media_driver_request = FX_DRIVER_GET_MEDIA_GEOMETRY;
- } else
- {
- media_ptr->fx_media_driver_request = FX_DRIVER_GET_DEVICE_GEOMETRY;
- }
-
- media_ptr->fx_media_driver_status = FX_IO_ERROR;
- (media_ptr->fx_media_driver_entry)( media_ptr );
-
- uiStatus = media_ptr->fx_media_driver_status;
- if ( uiStatus != FX_SUCCESS )
- {
- return uiStatus;
- }
- // Do we have partition alignment restriction?
- if( psGeometry->ulPartitionAlignment == FX_UNDEF_PARTITION_ALIGNMENT )
- {
- // No. We set it to no restriction.
- psGeometry->ulPartitionAlignment = 1;
- }
- // Do we have MBR sector number?
- if( psGeometry->ulMbrSector == FX_UNDEF_MBR_SECTOR )
- {
- // No. We set it to first sector in device.
- psGeometry->ulMbrSector = 0;
- }
- // At this point, we can only continue if we got the size of the media.
- if ( psGeometry->ulSizeInSectors == FX_UNDEF_SIZE_IN_SECTORS )
- {
- // No. We cannot calculate anything else.
- return FX_SUCCESS;
- }
- // Copy result capacity.
- ulCapacity = psGeometry->ulSizeInSectors;
- // Now we check which information the driver has supplied, and calculate
- // the information that was not supplied.
- // Was the number of cylinders given by the driver?
- if ( psGeometry->uwCylinders == FX_UNDEF_CYLINDERS )
- {
- // No. We will calculate it manually.
-
- // Set number of cylinders to max value.
- uwCylinders = 1024;
- // Set number of heads to max value.
- uwHeads = 16;
-
- // Compute divisor for heads.
- ulTemp = uwCylinders * uwHeads;
- // Compute value for sectors per track.
- uwSectorsPerTrack = (USHORT)( ulCapacity / ulTemp );
- // If no remainder, done!
- if ( ulCapacity % ulTemp )
- {
- // Else, increment number of sectors.
- uwSectorsPerTrack++;
-
- // Compute divisor for heads.
- ulTemp = uwCylinders * uwSectorsPerTrack;
-
- // Compute value for heads.
- uwHeads = (USHORT)( ulCapacity / ulTemp );
- // If no remainder, done!
- if ( ulCapacity % ulTemp )
- {
- // Else, increment number of heads.
- uwHeads++;
- // Compute divisor for cylinders.
- ulTemp = uwHeads * uwSectorsPerTrack;
-
- // Update number of cylinders.
- uwCylinders = (USHORT)( ulCapacity / ulTemp );
- }
- }
- // Keep calculated values in geometry structure.
- psGeometry->uwCylinders = uwCylinders;
- psGeometry->uwHeads = uwHeads;
- psGeometry->uwSectorsPerTrack = uwSectorsPerTrack;
- }
- else
- {
- // Yes. We also get the number of heads and number of sectors per track.
- uwCylinders = psGeometry->uwCylinders;
- uwHeads = psGeometry->uwHeads;
- uwSectorsPerTrack = psGeometry->uwSectorsPerTrack;
- }
- // Do we have hidden sectors set by the driver?
- if( psGeometry->ulHiddenSectors == FX_UNDEF_HIDDEN_SECTORS )
- {
- // No. Set the number of hidden sectors to be 1 track. This
- // is the default used here.
- // :TODO: Ofir
- // Need to be checked.
- ulHiddenSectors = uwSectorsPerTrack;
- psGeometry->ulHiddenSectors = ulHiddenSectors;
- }
- else
- {
- // Yes. Use the value given.
- ulHiddenSectors = psGeometry->ulHiddenSectors;
- }
- // Do we have media size in sectors set by the driver?
- if ( psGeometry->ulSizeInSectors == FX_UNDEF_SIZE_IN_SECTORS )
- {
- // No. Calculate it manually.
- // :TODO: Ofir
- // Need to be checked.
- ulSizeInSectors = ((ULONG)(uwCylinders - 2) * uwHeads * uwSectorsPerTrack) +
- ((uwHeads - 1) * uwSectorsPerTrack);
- psGeometry->ulSizeInSectors = ulSizeInSectors;
- }
- else
- {
- // Yes. Use the value given.
- ulSizeInSectors = psGeometry->ulSizeInSectors;
- }
-
- // Do we have the type of FAT set by the driver?
- if( psGeometry->byFatType != FX_UNDEF_FAT_TYPE )
- {
- // Yes. Use the given value.
- byFatType = psGeometry->byFatType;
- }
- else if ( ulSizeInSectors > 65530UL)
- {
- byFatType = FX_BIGDOS;
- }
- else if( ulSizeInSectors < 32680 )
- {
- byFatType = FX_FAT12;
- }
- else
- {
- byFatType = FX_FAT16;
- }
- psGeometry->byFatType = byFatType;
- // Do we have the sectors per cluster set by the driver?
- if ( psGeometry->bySectorsPerCluster != FX_UNDEF_CLUSTER_SIZE )
- {
- // Yes. Use the given value.
- bySectorsPerCluster = psGeometry->bySectorsPerCluster;
- }
- else if ( byFatType == FX_FAT12 )
- {
- bySectorsPerCluster = 8;
- }
- else if ( byFatType == FX_FAT16 )
- {
- bySectorsPerCluster = 4;
- }
- else
- {
- // We have BIGDOS.
- USHORT uwTemp = (USHORT)(ulSizeInSectors / 262144L);
- switch ( uwTemp )
- {
- case 0 :
- {
- bySectorsPerCluster = 4;
- break;
- }
- case 1:
- {
- bySectorsPerCluster = 8;
- break;
- }
- case 2:
- case 3:
- {
- bySectorsPerCluster = 16;
- break;
- }
- case 4:
- case 5:
- case 6:
- case 7:
- {
- bySectorsPerCluster = 32;
- break;
- }
- default :
- {
- bySectorsPerCluster = 64;
- break;
- }
- }
- }
- psGeometry->bySectorsPerCluster = bySectorsPerCluster;
- // Do we have the sectors per FAT set by the driver?
- if( psGeometry->uwSectorsPerFAT != FX_UNDEF_SECTORS_PER_FAT )
- {
- // Yes. Use the given value;
- uwSectorsPerFat = psGeometry->uwSectorsPerFAT;
- }
- else if ( byFatType == FX_FAT12 )
- {
- // No. We have FAT12.
- uwSectorsPerFat = (USHORT)(((((ulSizeInSectors + bySectorsPerCluster - 1) / bySectorsPerCluster) * 12 + bySectorsPerCluster - 1) / bySectorsPerCluster) + FX_BOOT_SECTOR_SIZE - 1) / FX_BOOT_SECTOR_SIZE;
- }
- else {
- // No. We have FAT16 or BIGDOS.
- uwSectorsPerFat = (USHORT)(((((ulSizeInSectors + bySectorsPerCluster - 1) / bySectorsPerCluster) * 2) + FX_BOOT_SECTOR_SIZE - 1) / FX_BOOT_SECTOR_SIZE);
- }
- psGeometry->uwSectorsPerFAT = uwSectorsPerFat;
- // :TODO: Ofir
- // FAT32 not supported.
- // Do we have the number of root entries set by the driver?
- if( psGeometry->uwRootEntries == FX_UNDEF_ROOT_ENTRIES )
- {
- // No. We calculate it manually.
- uwDirectorySectors = (USHORT)(ulCapacity / 200);
-
- if ( uwDirectorySectors < 1 )
- {
- uwDirectorySectors = 1;
- }
-
- if ( uwDirectorySectors > 32 )
- {
- uwDirectorySectors = 32;
- }
-
- uwRootEntries = uwDirectorySectors * (FX_BOOT_SECTOR_SIZE /
- FX_DIR_ENTRY_SIZE);
- psGeometry->uwRootEntries = uwRootEntries;
- }
- else
- {
- // Yes. Use the given value.
- uwRootEntries = psGeometry->uwRootEntries;
- }
- // Do we have the start/end CHS set by the driver?
- if( psGeometry->uwStartHead == FX_UNDEF_START_END )
- {
- // No. We calculate them here.
- if ( uwHeads > 1 )
- {
- psGeometry->uwStartHead = 1;
- psGeometry->uwStartCylinder = 0;
- psGeometry->uwStartSector = 1;
- }
- else
- {
- psGeometry->uwStartHead = 0;
- psGeometry->uwStartCylinder = 1;
- psGeometry->uwStartSector = 1;
- }
- psGeometry->uwEndHead = uwHeads - 1;
- psGeometry->uwEndCylinder = uwCylinders - 2;
- psGeometry->uwEndSector = uwSectorsPerTrack;
-
- psGeometry->ulPartitionStartSector = uwSectorsPerTrack;
- }
- // Done.
- return FX_SUCCESS;
- }
- #endif