⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dosformt.c

📁 M-System DOC(Disk on a Chip) Flash芯片的诊断工具, 可以从Flash芯片中获取特定的数据信息, 用于判断芯片当前的状态.
💻 C
📖 第 1 页 / 共 2 页
字号:
/***********************************************************************************/
/*                        M-Systems Confidential                                   */
/*           Copyright (C) M-Systems Flash Disk Pioneers Ltd. 1995-2003            */
/*                         All Rights Reserved                                     */
/***********************************************************************************/
/*                            NOTICE OF M-SYSTEMS OEM                              */
/*                           SOFTWARE LICENSE AGREEMENT                            */
/*                                                                                 */
/*      THE USE OF THIS SOFTWARE IS GOVERNED BY A SEPARATE LICENSE                 */
/*      AGREEMENT BETWEEN THE OEM AND M-SYSTEMS. REFER TO THAT AGREEMENT           */
/*      FOR THE SPECIFIC TERMS AND CONDITIONS OF USE,                              */
/*      OR CONTACT M-SYSTEMS FOR LICENSE ASSISTANCE:                               */
/*      E-MAIL = info@m-sys.com                                                    */
/***********************************************************************************/

/*
 * $Log:   V:/PVCSDB/DiskOnChip/archives/general storage/TrueFFS/src/fs/dosformt.c-arc  $
 * 
 *    Rev 1.9   Jan 14 2004 22:29:42   oris
 * Bug fix - format routine used wrong format on fat12/fat16 partition boundary.
 * 
 *    Rev 1.8   Nov 20 2003 18:48:38   omerk
 * Added TFFS_DLL_API prefix to flBuildGeometry routine.
 * Added the "extern C" cover to flBuildGeometry export declaration.
 * 
 *    Rev 1.7   Nov 19 2003 17:30:26   omerk
 * Wrong calculation of sectorsPerCluster field inside the BPB record, in getDriveGeometry() routine
 * 
 *    Rev 1.6   Oct 23 2003 13:56:34   oris
 * Bug fix - Use TL recommended cluster information only if it bigger then user MIN_CLUSTER_SIZE defintion.
 * Bug fix - Support single sector size clusters.
 * Bug fix - remove double sector allignment.
 * 
 *    Rev 1.5   Oct 20 2003 09:01:12   oris
 * Added alignment of usable clusters to unit boundary.
 * Bug fix - enlarging of the default cluster size was not done properly.
 * 
 *    Rev 1.4   Sep 30 2003 17:56:46   oris
 * Revised format algorithm to force FAT16 if possible and to use the recommended cluster size given by MIN_CLUSTER_SIZE and the TL
 * 
 *    Rev 1.3   Sep 01 2003 19:09:16   oris
 * - Removed tffsRAMXXX calls.
 * - Bug fix - missing initialization of bpb structure.
 * 
 *    Rev 1.2   May 11 2003 18:50:22   OriS
 * Removed warnings
 * 
 *    Rev 1.1   May 06 2003 11:52:58   OriS
 * Changed TFFSCPY to tffsRAMcpy
 * Changed TFFSCMP to tffsRAMcmp
 * Changed TFFSSET to tffsRAMset
 * 
 *    Rev 1.0   Apr 09 2003 12:16:02   OriS
 * Initial revision.
 * 
 */

#include "fltl.h"
#ifdef FORMAT_VOLUME

#ifdef __cplusplus
extern "C"
{
#endif /* __cplusplus */
extern TFFS_DLL_API void NAMING_CONVENTION flBuildGeometry(FLDword capacity, FLDword FAR2 *cylinders,
         FLDword FAR2 *heads,FLDword FAR2 *sectors, FLBoolean oldFormat);
#ifdef __cplusplus
}
#endif /* __cplusplus */

FLDword noOfClusters = 0;
#define FAT12bit ( noOfClusters < 4085LU )
 /*----------------------------------------------------------------------*/
/*      	      g e t D r i v e G e o m e t r y			            */
/*									                                    */
/* Calculates the geometry parameters for BIOS/DOS media		        */
/*                                                                      */
/* Parameters:                                                          */
/*	vol		  : Pointer identifying drive			                    */
/*  oldFormat : Format media with coluster size of 1 sector             */
/*                                                                      */
/* Returns:                                                             */
/*	bpb		: volume BIOS parameter block			                    */
/*	cylinders	: Number of "cylinders" in volume		                */
/*	noOfFATs	: Number of FAT copies				                    */

/*----------------------------------------------------------------------*/

static FLStatus getDriveGeometry(TL vol, BPB FAR2 *bpb, FLDword FAR2 *cylinders, unsigned noOfFATs, FLBoolean oldFormat)
{
  FLDword heads, sectors;
  FLSDword sizeInSectors;
  int directorySectors, sectorsPerFAT;
  FLDword clusterSize, tmpClusterSize;
#ifndef FL_NO_SURE_FS_SUPPORT
  FLByte bIndex, bLastIndex = 0, bCount = 0;
  FLDword maxClusterSize;
#endif /* FL_NO_SURE_FS_SUPPORT */
  FLDword sectorAlignment = 0;

  SectorNo capacity = vol.sectorsInVolume(vol.rec); /* Volume size in sectors */

  clusterSize = ((oldFormat == TRUE) ? 1: MIN_CLUSTER_SIZE);

  flBuildGeometry( (FLDword)capacity, (FLDword FAR2 *)cylinders,
                 (FLDword FAR2 *)&heads, (FLDword FAR2 *)&sectors,oldFormat);

  if (vol.recommendedClusterInfo)
	vol.recommendedClusterInfo(vol.rec,&tmpClusterSize,&sectorAlignment);

  clusterSize = TFFSMAX(clusterSize,tmpClusterSize);

  toLE2(bpb->noOfHeads,(FLWord) heads);
  toUNAL2(bpb->bytesPerSector,FL_SECTOR_SIZE);
  bpb->noOfFATS = (FLByte )noOfFATs;
  bpb->mediaDescriptor = 0xf8;	/* hard disk */

  sizeInSectors = (FLSDword) (*cylinders) * heads * sectors - sectors;

  toLE4(bpb->totalSectorsInVolume,sizeInSectors);
  toUNAL2(bpb->totalSectorsInVolumeDOS3,
         (FLWord)(sizeInSectors > 65535l ? 0 : sizeInSectors));

#ifndef FL_NO_SURE_FS_SUPPORT
	/* Calculate cluster size in sectors to avoid FAT12 */

  if (vol.recommendedClusterInfo)
		maxClusterSize = ( LE4(bpb->totalSectorsInVolume) - ( sectorAlignment>>FL_SECTOR_SIZE_BITS ) ) / 0xff6;
	else
		maxClusterSize = LE4(bpb->totalSectorsInVolume) / 0xff6;


	/* validity check */
	if( maxClusterSize == 0 )
		maxClusterSize = 1;
	else
	{
		for( bIndex = 0 ; bIndex < 8 ; bIndex++ )
		{
			if( (maxClusterSize&(0x1<<bIndex) ) != 0x0 )
			{
				bLastIndex = bIndex;
				bCount++;
			}
		}
		if( bCount != 1 )
			maxClusterSize = 0x1<<(bLastIndex);
	}

	clusterSize = (maxClusterSize >= clusterSize)? clusterSize: maxClusterSize;

#endif /* FL_NO_SURE_FS_SUPPORT */
  
  if (vol.recommendedClusterInfo)
		noOfClusters = ( sizeInSectors - ( sectorAlignment>>FL_SECTOR_SIZE_BITS ) ) / clusterSize;
	else
		noOfClusters = sizeInSectors / clusterSize;

  for (bpb->sectorsPerCluster = (FLByte )clusterSize;
       noOfClusters > (FLDword)(!oldFormat && bpb->sectorsPerCluster < 8 ? 32766l : 65534l);
       bpb->sectorsPerCluster <<= 1, noOfClusters >>= 1);

  if (FAT12bit)
    sectorsPerFAT =
      (FLWord) ((((noOfClusters + 2L) * 3 + 1) / 2 - 1) / FL_SECTOR_SIZE + 1);
  else
    sectorsPerFAT =
      (FLWord) (((noOfClusters + 2L) * 2 - 1) / FL_SECTOR_SIZE + 1);
  toLE2(bpb->sectorsPerFAT,(FLWord)sectorsPerFAT);

  directorySectors = (int)(capacity / 200);
  if (directorySectors < 1) directorySectors = 1;
  if (directorySectors > 15) directorySectors = 15;
  toUNAL2(bpb->rootDirectoryEntries,
         (FLWord)(directorySectors * (FL_SECTOR_SIZE / sizeof(DirectoryEntry))));

  toLE2(bpb->sectorsPerTrack,(FLWord) sectors);
  toLE4(bpb->noOfHiddenSectors,sectors);
  toLE2(bpb->reservedSectors,1);

	if (vol.recommendedClusterInfo)
	{
		/* check alignment */
		FLDword firstDataSector, sectorsInBlock, eraseBlockSize, reserved;
		firstDataSector = (FLDword)( LE2(bpb->sectorsPerTrack) + LE2(bpb->reservedSectors) + (FLWord)bpb->noOfFATS * LE2(bpb->sectorsPerFAT) + ((UNAL2(bpb->rootDirectoryEntries) * DIRECTORY_ENTRY_SIZE - 1) >> FL_SECTOR_SIZE_BITS) + 1 );
		vol.recommendedClusterInfo(vol.rec,&clusterSize,&eraseBlockSize);

		sectorsInBlock = eraseBlockSize >> FL_SECTOR_SIZE_BITS;
		if( ( firstDataSector %  sectorsInBlock ) != 0 )
		{
			/* aligned first DataSector to unit size */
			reserved = LE2(bpb->reservedSectors);
			reserved += sectorsInBlock - ( firstDataSector % sectorsInBlock );
            /* Should never happen, but please check if reserved sectors > 16 bits */
            if(reserved == 0) reserved = 1;
			toLE2(bpb->reservedSectors, (FLWord)reserved);
		}
	}

  return flOK;
}
 

/*----------------------------------------------------------------------*/
/*      	 c r e a t e M a s t e r B o o t R e c o r d		*/
/*									*/
/* Creates the Master Boot Record (Sector 0)				*/
/*									*/
/* Parameters:                                                          */
/*	vol		: Pointer identifying drive			*/
/*	bpb		: volume BIOS parameter block			*/
/*                                                                      */
/* Returns:                                                             */
/*	FLStatus	: 0 on success, failed otherwise		*/
/*	cylinders	: Number of "cylinders" in volume		*/
/*----------------------------------------------------------------------*/

static FLStatus createMasterBootRecord(TL vol,
				     BPB *bpb,
				     FLDword cylinders)
{
  static FLByte  bootCode[] = {
    0xFA, 0x33, 0xC0, 0x8E, 0xD0, 0xBC, 0x00, 0x7C,
    0x8B, 0xF4, 0x50, 0x07, 0x50, 0x1F, 0xFB, 0xFC,

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -