/******************************************************************************/
/*                                                                            */
/*  Copyright (C), 1995-2007, SanDisk IL Ltd. All rights reserved.            */
/*                                                                            */
/*  Redistribution and use in source and binary forms, with or without        */
/*  modification, are permitted provided that the following conditions are    */
/*  met:                                                                      */
/*  1. Redistributions of source code must retain the above copyright notice, */
/*     this list of conditions and the following disclaimer.                  */
/*  2. Redistributions in binary form must reproduce the above copyright      */
/*     notice, this list of conditions and the following disclaimer in the    */
/*     documentation and/or other materials provided with the distribution.   */
/*  3. Neither the name of SanDisk IL nor the names of its contributors may be*/
/*     used to endorse or promote products derived from this software without */
/*     specific prior written permission.                                     */
/*                                                                            */
/*  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       */
/*  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED */
/*  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR             */
/*  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT      */
/*  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,     */
/*  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED  */
/*  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR    */
/*  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    */
/*  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING      */
/*  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS        */
/*  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              */
/*                                                                            */
/*  NOTWITHSTANDING ANYTHING STATED TO THE CONTRARY, SANDISK'S TOTAL LIABILITY*/
/*  ARISING OUT OF OR RELATED TO THE SOFTWARE OR ANY SUPPORT SERVICES         */
/*  PROVIDED WITH RESPECT THERETO SHALL NOT EXCEED $100.                      */
/*                                                                            */
/*  The laws of the State of California, United States of America, exclusive  */
/*  of conflict-of-laws provisions, shall govern this license in all respects.*/
/*  The the federal or state courts of competent jurisdiction in the State    */ 
/*  of California shall have exclusive jurisdiction with respect to all       */
/*  actions commenced in relation to the software. Licensee agrees not        */ 
/*  to export or re-export, directly or indirectly any technical data         */ 
/*  acquired from SanDisk or any products utilizing such data in violation    */
/*  of applicable export laws and regulations.                                */
/*                                                                            */
/******************************************************************************/
/*
 * $Log:   V:/PVCSDB/DiskOnChip/archives/Testing/TrueFFS 6.3/Drop 2.5/3/common/dosformt.h-arc  $
 * 
 *    Rev 1.12   Oct 22 2007 11:34:50   einat.avidan
 * update copyrights header
 * 
 *    Rev 1.11   Feb 28 2007 09:53:46   einat.avidan
 * No change
 * 
 *    Rev 1.10.1.0   Nov 12 2006 09:47:10   Yaniv.Iarovici
 * Added FAT 32 support.
 * 
 *    Rev 1.10   Sep 11 2006 13:45:18   yaniv.iarovici
 * Legal header added
 * 
 *    Rev 1.9   Aug 09 2006 16:52:48   Polina.Marimont
 * initial for DOC Driver 1.0
 */

#ifndef DOSFORMT_H
#define DOSFORMT_H

#include "flbase.h"
#include "fltl.h"


#define DRIVE_NUMBER            0x80
#define EXTENDED_BOOT_SIGNATURE 0x29
#define BACKUP_OF_BOOT_SECTOR   6
#define FSI_LEADING_SIGNATURE   0x41615252
#define FSI_STRUCTURE_SIGNATURE 0x61417272

#ifndef FL_ROOT_DIR_ENTRIES
#define FL_ROOT_DIR_ENTRIES 512
#endif /* FL_ROOT_DIR_ENTRIES */


/* The BIOS parameter block (a part of the boot sector)		    */
/* Note that this is NOT the customary definition of the BPB    */
/* (customary is to start it on 'bytesPerSector'). To avoid the */
/* nuisance of a structure that starts at an odd offset, we add */
/* the first 11 bytes of the boot sector here.			        */

#ifndef FL_NO_PACKED_STRUCTS_SUPPORTED

#ifdef FL_WINDOWS_PACKING
#pragma pack(push, 1)
#endif /* FL_WINDOWS_PACKING */


#else /* FL_NO_PACKED_STRUCTS_SUPPORTED */

#endif /* FL_NO_PACKED_STRUCTS_SUPPORTED */

typedef struct 
{
    FLDword   size;
    FLByte    clusterSize;
}DiskParamsTable;

#ifndef FL_NO_PACKED_STRUCTS_SUPPORTED

typedef FL_PACKED_STRUCTURES_BEFORE struct FL_PACKED_STRUCTURES_AFTER {
  BPB	bpb;
  FLByte	physicalDriveNo;
  FLSByte	reserved0;
  FLSByte	extendedBootSignature;
  FLSByte	volumeId[4];
  FLSByte	volumeLabel[11];
  FLSByte	systemId[8];
  FLSByte	bootstrap[448];
  LEushort	signature;
} DOSBootSector;
typedef FL_PACKED_STRUCTURES_BEFORE struct FL_PACKED_STRUCTURES_AFTER {
  BPB	    bpb;
  LEulong	FAT32Size;
  LEushort  extFlags;
  LEushort  FSVer;
  LEulong   RootClus;
  LEushort  FSInfo;
  LEushort  bckUpBootSect;
  FLByte    reserved[12];
  FLByte    physicalDriveNo;
  FLSByte	reserved0;
  FLSByte	extendedBootSignature;
  FLSByte	volumeId[4];
  FLSByte	volumeLabel[11];
  FLSByte	systemId[8];
  FLSByte	bootstrap[420];
  LEushort	signature;
} FAT32BootSector;

typedef FL_PACKED_STRUCTURES_BEFORE struct FL_PACKED_STRUCTURES_AFTER {
  LEulong FSI_LeadSig;
  FLByte  FSI_Reserved1[480];
  LEulong FSI_StructSig;
  LEulong FSI_Free_Count;
  LEulong FSI_Nxt_Free;
  FLByte  FSI_Reserved2[14];
  LEushort	signature;
}FAT32FSInfo;
#else /* FL_NO_PACKED_STRUCTS_SUPPORTED */

#define FL_DOSBOOTSECTOR_BPB_OFFSET			0	/* size 36 */
#define FL_DOSBOOTSECTOR_PHYSICAL_DRIVE_NO_OFFSET	36	/* size 1 */
#define FL_DOSBOOTSECTOR_RESERVED0_OFFSET		37	/* size 1 */
#define FL_DOSBOOTSECTOR_EXTENDED_BOOT_SIGNATURE_OFFSET	38	/* size 1 */
#define FL_DOSBOOTSECTOR_VOLUME_ID_OFFSET		39	/* size 4, array */
#define FL_DOSBOOTSECTOR_VOLUME_LABEL_OFFSET		43	/* size 11, array */
#define FL_DOSBOOTSECTOR_SYSTEM_ID_OFFSET		54	/* size 8, array */
#define FL_DOSBOOTSECTOR_BOOTSTRAP_OFFSET		62	/* size 448, array */
#define FL_DOSBOOTSECTOR_SIGNATURE_OFFSET		510	/* size 2 */
#define FL_DOSBOOTSECTOR_SIZE				512

#define FL_FAT32BOOTSECTOR_BPB_OFFSET			0	/* size 36 */
#define FL_FAT32BOOTSECTOR_FAT32SIZE_OFFSET	    36	/* size 4 */
#define FL_FAT32BOOTSECTOR_EXTFLAGS_OFFSET		40	/* size 2 */
#define FL_FAT32BOOTSECTOR_FSVER_OFFSET	        42	/* size 2 */ 
#define FL_FAT32BOOTSECTOR_ROOTCLUSTER_OFFSET	44	/* size 4 */
#define FL_FAT32BOOTSECTOR_FSINFO_OFFSET		48	/* size 2 */
#define FL_FAT32BOOTSECTOR_BCKUPBOOTSECT_OFFSET	50	/* size 2*/
#define FL_FAT32BOOTSECTOR_RESERVED_OFFSET	    52	/* size 12, array */
#define FL_FAT32BOOTSECTOR_PHYDRIVENO_OFFSET	64	/* size 1 */
#define FL_FAT32BOOTSECTOR_RESERVED0_OFFSET	    65	/* size 1*/
#define FL_FAT32BOOTSECTOR_EXTSIGNATURE_OFFSET	66	/* size 1 */
#define FL_FAT32BOOTSECTOR_VOLUMEID_OFFSET	    67	/* size 4, array */
#define FL_FAT32BOOTSECTOR_VOLUMELABEL_OFFSET	71	/* size 11, array */
#define FL_FAT32BOOTSECTOR_SYSTEMID_OFFSET	    82	/* size 8 */
#define FL_FAT32BOOTSECTOR_BOOTSTRAP_OFFSET	    90	/* size 420, array */
#define FL_FAT32BOOTSECTOR_SIGNATURE_OFFSET		510	/* size 2 */
#define FL_FAT32BOOTSECTOR_SIZE				    512



#define FL_FAT32_FSINFO_LEADSIGNATURE_OFFSET	0   /* size 4*/
#define FL_FAT32_FSINFO_RESERVED1_OFFSET		4   /* size 480, array*/
#define FL_FAT32_FSINFO_STRUCTSIGNATURE_OFFSET	484 /* size 4*/
#define FL_FAT32_FSINFO_FREECOUNT_OFFSET		488 /* size 4*/
#define FL_FAT32_FSINFO_NEXTFREE_OFFSET			492 /* size 4*/
#define FL_FAT32_FSINFO_RESERVED2_OFFSET		496 /* size 4*/
#define FL_FAT32_FSINFO_SIGNATURE_OFFSET		510 /* size 14, array*/
#define FL_FAT32_FSINFO_SIZE				    512

typedef FLByte DOSBootSector[FL_DOSBOOTSECTOR_SIZE];
typedef FLByte FAT32BootSector[FL_FAT32BOOTSECTOR_SIZE];
typedef FLByte FAT32FSInfo[FL_FAT32_FSINFO_SIZE];
#endif /* FL_NO_PACKED_STRUCTS_SUPPORTED */



#ifndef FL_NO_PACKED_STRUCTS_SUPPORTED

typedef FL_PACKED_STRUCTURES_BEFORE struct FL_PACKED_STRUCTURES_AFTER {
	FLByte	   orderNum;			/* 6 significant bits of LFN order number */
	Unaligned  name1[5];			/* part 1 of scattered name */
	FLByte	   attributes;			/* file attributes, must be RO, System, Hidden, Volume */
	FLByte	   reserved;			/* must be 0 */
	FLByte	   checksum;			/* checksum created from short form name */
	LEushort   name2[6];			/* part 2 of scattered name */
	LEushort   startingCluster;		/* must be 0 */
	LEushort   name3[2];			/* part 3 of scattered name */
} LfnDirectoryEntry;
#else /* FL_NO_PACKED_STRUCTS_SUPPORTED */

#define FL_LFN_DIRECTORY_ENTRY_ORDER_NUM_OFFSET			0	/* size 1 */
#define FL_LFN_DIRECTORY_ENTRY_NAME1_OFFSET			1	/* size 10, unaligned array of 2 */
#define FL_LFN_DIRECTORY_ENTRY_ATTRIBUTES_OFFSET		11	/* size 1 */
#define FL_LFN_DIRECTORY_ENTRY_RESERVED_OFFSET			12	/* size 1 */
#define FL_LFN_DIRECTORY_ENTRY_CHECKSUM_OFFSET			13	/* size 1 */
#define FL_LFN_DIRECTORY_ENTRY_NAME2_OFFSET			14	/* size 12, array of 2 */
#define FL_LFN_DIRECTORY_ENTRY_STARTING_CLUSTER_OFFSET		26	/* size 2 */
#define FL_LFN_DIRECTORY_ENTRY_NAME3_OFFSET			28	/* size 4, array of 2 */
#define FL_LFN_DIRECTORY_ENTRY_SIZE				32

typedef FLByte LfnDirectoryEntry[FL_LFN_DIRECTORY_ENTRY_SIZE];
#endif /* FL_NO_PACKED_STRUCTS_SUPPORTED */

/* Directory entry attribute bits */



#ifndef FL_NO_PACKED_STRUCTS_SUPPORTED

typedef FL_PACKED_STRUCTURES_BEFORE struct FL_PACKED_STRUCTURES_AFTER {
  /* First partition entry starts here. We do not map it as a	*/
  /* separate structure because it is not longword aligned	*/
  FLByte	activeFlag;	/* 80h = bootable */
  FLByte        startingHead;
  LEushort	startingCylinderSector;
  FLSByte       type;
  FLByte	endingHead;
  LEushort	endingCylinderSector;
  Unaligned4	startingSectorOfPartition;
  Unaligned4	sectorsInPartition;
  /* Partition entries 2,3 and 4 are structured as the 1st partition */
} Partition;
#else /* FL_NO_PACKED_STRUCTS_SUPPORTED */

#define FL_PARTITION_ACTIVE_FLAG_OFFSET				0	/* size 1 */
#define FL_PARTITION_STARTING_HEAD_OFFSET			1	/* size 1 */
#define FL_PARTITION_STARTING_CYLINDER_SECTOR_OFFSET		2	/* size 2 */
#define FL_PARTITION_TYPE_OFFSET				4	/* size 1 */
#define FL_PARTITION_ENDING_HEAD_OFFSET				5	/* size 1 */
#define FL_PARTITION_ENDING_CYLINDER_SECTOR_OFFSET		6	/* size 2 */
#define FL_PARTITION_STARTING_SECTOR_OF_PARTITION_OFFSET	8	/* size 4 */
#define FL_PARTITION_SECTORS_IN_PARTITION_OFFSET		12	/* size 4 */
#define FL_PARTITION_SIZE					16

typedef FLByte Partition[FL_PARTITION_SIZE];
#endif /* FL_NO_PACKED_STRUCTS_SUPPORTED */


#ifndef FL_NO_PACKED_STRUCTS_SUPPORTED

typedef FL_PACKED_STRUCTURES_BEFORE struct FL_PACKED_STRUCTURES_AFTER {
  FLSByte       reserved1[0x1A6];
  Unaligned4    passwordInfo[3];  /* M-Systems proprietary */
  FLSByte       reserved2[0xC];   /* NT4 or NT5 signature place */

  /* First partition entry starts here. We do not map it as a	*/
  /* separate structure because it is not longword aligned	*/
  Partition     ptEntry[4];
  LEushort      signature;	/* = PARTITION_SIGNATURE */
} PartitionTable;
#else /* FL_NO_PACKED_STRUCTS_SUPPORTED */

#define FL_PARTITION_TABLE_RESERVED1_OFFSET			0	/* size 0x1A6 */
#define FL_PARTITION_TABLE_PASSWORD_INFO_OFFSET			422	/* size 12, unaligned array of 4 */
#define FL_PARTITION_TABLE_RESERVED2_OFFSET			434	/* size 12, array */
#define FL_PARTITION_TABLE_PT_ENTRY_OFFSET			446	/* size 4x16, array of partition entries */
#define FL_PARTITION_TABLE_SIGNATURE_OFFSET			510	/* size 2 */
#define FL_PARTITION_TABLE_SIZE					512

typedef FLByte PartitionTable[FL_PARTITION_TABLE_SIZE];
#endif /* FL_NO_PACKED_STRUCTS_SUPPORTED */

#ifndef FL_NO_PACKED_STRUCTS_SUPPORTED

#ifdef FL_WINDOWS_PACKING
#pragma pack(pop)
#endif /* FL_WINDOWS_PACKING */

#endif /* FL_NO_PACKED_STRUCTS_SUPPORTED */

#define PARTITION_ENTRIES	4


/** Values of irFlags for flOpenFile: */

#define ACCESS_MODE_MASK   3 /* Mask for access mode bits */

/* Individual flags */

#define FL_ACCESS_CREATE      2 /* Create new file      */

/* commands to flGetSetFileInfo (in addition to above) */
#define FL_GET_DATETIME        -1 /* Get date/time                        */
#define FL_GET_ATTRIBUTES      -2 /* Get attributes                       */
#define FL_GET_CREATE_DATETIME -8 /* Get creation date/time               */


#ifndef LFN_NAME_MAX_CHARACTERS
#define LFN_NAME_MAX_CHARACTERS			255
#endif /* LFN_NAME_MAX_CHARACTERS */

#ifndef PATH_SEGMENTS_MAX_NUM
#define PATH_SEGMENTS_MAX_NUM			10
#endif /* PATH_SEGMENTS_MAX_NUM */

#ifndef PATH_CHARACTERS_MAX_NUM
#define PATH_CHARACTERS_MAX_NUM			LFN_NAME_MAX_CHARACTERS
#endif  /* PATH_CHARACTERS_MAX_NUM */

#ifndef FL_MAX_TREE_LEVELS
#define FL_MAX_TREE_LEVELS	  ( PATH_CHARACTERS_MAX_NUM / 2 + 1 )
#endif  /* FL_MAX_TREE_LEVELS */

#ifndef FL_ROOT_DIR_ENTRIES
#define FL_ROOT_DIR_ENTRIES(socket,partition) 512
#endif /* FL_ROOT_DIR_ENTRIES */

#define UNICODE_CHAR_PER_LFN_ENTRY		13

#define SHORT_NAME_MAX_LENGTH	        8
#define EXTENSION_MAX_LENGTH	        3
#define NAME_8_3_MAX_LENGTH	            12		/* "tttttttt.xxx" */

/* Directory entry attribute bits for internal use */
#define ATTR_SUREFS_GEN_ATTENTION_FLAG	0x40
#define ATTR_SUREFS_REN_ATTENTION_FLAG	0x80
#define ATTR_SUREFS_ATTENTION_FLAG_MASK	(ATTR_SUREFS_GEN_ATTENTION_FLAG |	\
										ATTR_SUREFS_REN_ATTENTION_FLAG)

#define ATTR_MASK	((unsigned char)(FL_ATTR_READ_ONLY | FL_ATTR_HIDDEN | FL_ATTR_SYSTEM | \
                                     FL_ATTR_VOL_LABEL | FL_ATTR_DIRECTORY | FL_ATTR_ARCHIVE))
#define LFN_ATTRIBUTES	((unsigned char)(FL_ATTR_READ_ONLY | FL_ATTR_HIDDEN | \
                                         FL_ATTR_SYSTEM | FL_ATTR_VOL_LABEL))

#define DIRECTORY_ENTRY_SIZE	         32

#define DIRECTORY_ENTRIES_PER_SECTOR	 (FL_SECTOR_SIZE / DIRECTORY_ENTRY_SIZE)
#define MAX_ROOT_DIR_SECTORS		((0x10000/DIRECTORY_ENTRIES_PER_SECTOR) - 1)

#ifdef FS_EXP_FAT32
/* setting this to 0xffffffff may improve performance *
 * setting this to 0x0fffffff improves compatibility  */
#define FAT32_ENTRY_BITS_COMP            0x0fffffff
#endif /*FS_EXP_FAT32*/

#define SET_GLOBAL_ATTENTION_FLAG        0
#ifdef FS_EXP_FAT32
#define RESET_GLOBAL_ATTENTION_FLAG	     0x0fffffff
#else /*FS_EXP_FAT32*/
#define RESET_GLOBAL_ATTENTION_FLAG	     0xffff
#endif /*FS_EXP_FAT32*/
#define GLOBAL_ATT_FLAG_FAT_ENTRY        1

#define	NEVER_USED_DIR_ENTRY             0

#ifndef FL_NO_PACKED_STRUCTS_SUPPORTED
#define	DELETED_DIR_ENTRY                ((FLSByte) 0xe5)
#else /*FL_NO_PACKED_STRUCTS_SUPPORTED*/
#define	DELETED_DIR_ENTRY                ((FLByte) 0xe5)
#endif /*FL_NO_PACKED_STRUCTS_SUPPORTED*/

/* FAT definitions */

#define	FAT_FREE                         0
#define	FAT_BAD_CLUSTER	                 0xfff7
#ifdef FS_EXP_FAT32
#define	FAT_LAST_CLUSTER                 0x0fffffff  /* FAT16 values are converted */
#else /*FS_EXP_FAT32*/
#define	FAT_LAST_CLUSTER                 0xffff  /* actually any of 0xfff8-0xffff */
#endif /*FS_EXP_FAT32*/

/* Partition table definitions */

#define PARTITION_SIGNATURE	             0xaa55

#define CYLINDER_SECTOR(cylinder,sector) ((sector) + ((cylinder & 0xff) << 8) + ((cylinder & 0x300) >> 2))


#ifndef FL_MAX_PARTITION_DEPTH
#define FL_MAX_PARTITION_DEPTH           0x08
#endif /* FL_MAX_PARTITION_DEPTH */

#define LFN_SHORT_NAME			0		/* for 8.3 short file name	*/
#define LFN_LONG_NAME			1		/* for long file name		*/
#define LFN_INVALID_SET			2		/* found invalid file name characters				*/
#define SFN_LOWERCASE_NAME8		0x08		/* these 2 bit-flags are to support */
#define SFN_LOWERCASE_EXT3		0x10		/* NT VFAT lowercase bits */

/*-------------------------------------------------------------------------------------------
*				F L P a t h S e g m e n t
* The structure is used when analyzing path string.
* Parameters:	name - address of first character of the path segment (unicode null terminated
*						string. For example, for first segment of /first/second/third.ext it 
*						will point on "first".
*				length - number of characters of the path segment
*				buffSize - in case of retrieving file name this field contains size of supplied buffer
*										in characters.
*				get - if TRUE, this structure is used to retrieve found file name 
*						(getFirstFileName()). If FALSE, this structure contains file name to 
*						be opened/created/found	(findFileName()).
*------------------------------------------------------------------------------------------*/
typedef struct {
	FLWchar FAR1	*name;
	short           length;
	short           buffSize;
	FLBoolean       get;
	FLBoolean       reserved;
} FLPathSegment;

/*--------------------------------------------------------------------------
*				F L L f n S e a r c h
* The FLLfnSearch structer is used when searching directory by specified	
* file name.																
* Parameters:	
*		neverUsed - indicates that found at least one directory entry in sector
*					that was never used.
*		verifiedChars - Number of characters that successfully compared	with	
*						file name or found in free entries in previous sector	
*		checksum - LFN checksum found in previous sector							
*		lastNum - LFN order number of last LFN entry found in previous sector		
*		idx - directory entry index of found entry								
*		status - LFN_SHORT_NAME, LFN_LONG_NAME, LFN_UPPERCASE_FOR_SHORT,
*					LFN_INVALID_SET
*---------------------------------------------------------------------------*/
typedef struct {
	unsigned	verifiedChars;		
	unsigned	lastNum;
	unsigned	idx;
	int		status;
	unsigned char	checksum;
	FLBoolean	neverUsed;
} FLLfnSearch;



#endif /* DOSFORMT_H */

