Logo Search packages:      
Sourcecode: fatsort version File versions


      FATSort, utility for sorting FAT directory structures
      Copyright (C) 2004 Boris Leidner <fatsort(at)formenos.de>

      This program is free software; you can redistribute it and/or
      modify it under the terms of the GNU General Public License
      as published by the Free Software Foundation; either version 2
      of the License, or (at your option) any later version.

      This program is distributed in the hope that it will be useful,
      but WITHOUT ANY WARRANTY; without even the implied warranty of
      GNU General Public License for more details.

      You should have received a copy of the GNU General Public License
      along with this program; if not, write to the Free Software
      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

      This file contains/describes functions that are used to read, write, check,
      and use FAT filesystems.

#ifndef __FAT_fs_h__
#define __FAT_fs_h__

// FAT types
#define FATTYPE_FAT12 12
#define FATTYPE_FAT16 16
#define FATTYPE_FAT32 32

// file attributes
#define ATTR_READ_ONLY 0x01
#define ATTR_HIDDEN 0x02
#define ATTR_SYSTEM 0x04
#define ATTR_VOLUME_ID 0x08
#define ATTR_DIRECTORY 0x10
#define ATTR_ARCHIVE 0x20

// constants for the LDIR structure
#define DE_FREE (char) 0xe5
#define DE_FOLLOWING_FREE 0x00
#define LAST_LONG_ENTRY 0x40

#define DIR_ENTRY_SIZE 32

// maximum path len on FAT file systems (above specification)
#define MAX_PATH_LEN 512

// maximum file len
// (specification: file < 4GB which is 
// maximum clusters in chain * cluster size)

#include <stdio.h>
#include <sys/types.h>

#include "platform.h"

// Directory entry structures
// Structure for long directory names
struct sLongDirEntry {
      u_char LDIR_Ord;        // Order of entry in sequence
      char LDIR_Name1[10];          // Chars 1-5 of long name
      u_char LDIR_Attr;       // Attributes (ATTR_LONG_NAME must be set)
      u_char LDIR_Type;       // Type
      u_char LDIR_Checksum;         // Short name checksum
      char LDIR_Name2[12];          // Chars 6-11 of long name
      u_int16_t LDIR_FstClusLO;     // Zero
      char LDIR_Name3[4];           // Chars 12-13 of long name
} __attribute__((packed));

// Structure for old short directory names
struct sShortDirEntry {
      char DIR_Name[11];            // Short name
      u_char DIR_Atrr;        // File attributes
      u_char DIR_NTRes;       // Reserved for NT
      u_char DIR_CrtTimeTenth;      // Time of creation in ms
      u_int16_t DIR_CrtTime;        // Time of creation
      u_int16_t DIR_CrtDate;        // Date of creation
      u_int16_t DIR_LstAccDate;     // Last access date
      u_int16_t DIR_FstClusHI;      // Hiword of first cluster
      u_int16_t DIR_WrtTime;        // Time of last write
      u_int16_t DIR_WrtDate;        // Date of last write
      u_int16_t DIR_FstClusLO;      // Loword of first cluster
      u_int32_t DIR_FileSize;       // file size in bytes
} __attribute__((packed));

union sDirEntry {
      struct sShortDirEntry ShortDirEntry;
      struct sLongDirEntry LongDirEntry;
} __attribute__((packed));

// Bootsector structures
// FAT12 and FAT16
struct sFAT12_16 {
      u_char BS_DrvNum;       // Physical drive number
      u_char BS_Reserved;           // Current head
      u_char BS_BootSig;            // Signature
      u_int32_t BS_VolID;           // Volume ID
      char BS_VolLab[11];           // Volume Label
      char BS_FilSysType[8];        // FAT file system type (e.g. FAT, FAT12, FAT16, FAT32)
      u_char unused[448];           // unused space in bootsector
} __attribute__((packed));

// FAT32
struct sFAT32 {
      u_int32_t BS_FATSz32;         // Sectors per FAT
      u_int16_t BS_ExtFlags;        // Flags
      u_int16_t BS_FSVer;           // Version
      u_int32_t BS_RootClus;        // Root Directory Cluster
      u_int16_t BS_FSInfo;          // Sector of FSInfo structure
      u_int16_t BS_BkBootSec;       // Sector number of the boot sector copy in reserved sectors
      char BS_Reserved[12];         // for future expansion
      char BS_DrvNum;               // see fat12/16
      char BS_Reserved1;            // see fat12/16
      char BS_BootSig;        // ...
      u_int32_t BS_VolID;
      char BS_VolLab[11];
      char BS_FilSysType[8];
      u_char unused[420];           // unused space in bootsector
} __attribute__((packed));

union sFATxx {
      struct sFAT12_16 FAT12_16;
      struct sFAT32 FAT32;
} __attribute__((packed));

// First sector = boot sector
struct sBootSector {
      u_char BS_JmpBoot[3];         // Jump instruction (to skip over header on boot)
      char BS_OEMName[8];           // OEM Name (padded with spaces)
      u_int16_t BS_BytesPerSec;     // Bytes per sector
      u_char BS_SecPerClus;         // Sectors per cluster
      u_int16_t BS_RsvdSecCnt;      // Reserved sector count (including boot sector)
      u_char BS_NumFATs;            // Number of file allocation tables
      u_int16_t BS_RootEntCnt;      // Number of root directory entries
      u_int16_t BS_TotSec16;        // Total sectors (bits 0-15)
      u_char BS_Media;        // Media descriptor
      u_int16_t BS_FATSz16;         // Sectors per file allocation table
      u_int16_t BS_SecPerTrk; // Sectors per track
      u_int16_t BS_NumHeads;        // Number of heads
      u_int32_t BS_HiddSec;         // Hidden sectors
      u_int32_t BS_TotSec32;        // Total sectors (bits 16-47)
      union sFATxx FATxx;
      u_int16_t BS_EndOfBS;         // marks end of bootsector
} __attribute__((packed));

// functions

// lazy check if this is really a FAT bootsector
int32_t check_bootsector(struct sBootSector *bs);

// reads bootsector
int32_t read_bootsector(FILE *fd, struct sBootSector *bs);

// calculates count of clusters
int32_t getCountOfClusters(struct sBootSector *bs);

// retrieves FAT type from bootsector
int32_t getFATType(struct sBootSector *bs);

// retrieves FAT entry for a cluster number
int32_t getFATEntry(FILE *fd, struct sBootSector *bs, u_int32_t cluster, u_int32_t *data);

// write a FAT entry
int32_t putFATEntry(FILE *fd, struct sBootSector *bs, u_int32_t cluster, u_int32_t data);

// returns the offset of a specific cluster in the data region of the file system
off_t getClusterOffset(struct sBootSector *bs, u_int32_t cluster);

// parses one directory entry
int32_t parseEntry(FILE *fd, union sDirEntry *de);

#endif // __FAT_fs_h__

Generated by  Doxygen 1.6.0   Back to index