View of xos/tools/fat12.h


XOS | Parent Directory | View | Download

/* Copyright (C) 2008  Emmanuel Varoquaux
 
   This file is part of XOS.
 
   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 3 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
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   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, see <http://www.gnu.org/licenses/>. */
 
/* Variables globales et fonctions relatives a un peripherique de stockage
 * formate en FAT12. */
 
#ifndef FAT12_H
#define FAT12_H
 
#include "vfat.h"
 
#define EOC         0x0fff
#define BAD_CLUSTER 0x0ff7
 
/* Parametres du volume */
 
extern bootSector_t bootSector;
extern unsigned short rootDirSectors;
extern unsigned short firstDataSector;
extern unsigned short dataSec;
extern unsigned short countOfClusters;
 
void init_volume(unsigned short resvdSecCnt);
unsigned short firstSectorOfCluster(unsigned short n);
unsigned short FATOffset(unsigned short n);
unsigned short thisFATSecNum(unsigned short n, unsigned char i);
unsigned short thisFATEntOffset(unsigned short n);
unsigned short accessFATEntry(unsigned short n, const char *secBuff);
void setFATEntry(unsigned short n, unsigned short clusEntryVal, char *secBuff);
 
/* Fonctions sur les numeros de cluster */
 
#ifdef IN_FAT12_C
#define extern
#define inline
#endif
 
extern inline unsigned short firstSectorOfCluster(unsigned short n)
{
  return (n - 2) * bootSector.BPB_SecPerClus + firstDataSector;
}
 
extern inline unsigned short FATOffset(unsigned short n)
{
  return n + n / 2;
}
 
extern inline unsigned short thisFATSecNum(unsigned short n, unsigned char i)
{
  return bootSector.BPB_ResvdSecCnt + FATOffset(n) / bootSector.BPB_BytsPerSec + i * bootSector.BPB_FATSz16;
}
 
extern inline unsigned short thisFATEntOffset(unsigned short n)
{
  return FATOffset(n) % bootSector.BPB_BytsPerSec;
}
 
extern inline unsigned short accessFATEntry(unsigned short n, const char secBuff[])
{
  unsigned short clusEntryVal;
 
  clusEntryVal = *(const unsigned short *)&secBuff[thisFATEntOffset(n)];
  if (n & 1)
    clusEntryVal >>= 4;
  else
    clusEntryVal &= 0x0fff;
  return clusEntryVal;
}
 
extern inline void setFATEntry(unsigned short n, unsigned short clusEntryVal, char secBuff[])
{
  if (n & 1) {
    clusEntryVal <<= 4;
    *(unsigned short *)&secBuff[thisFATEntOffset(n)] &= 0x000f;
  }
  else {
    clusEntryVal &= 0x0fff;
    *(unsigned short *)&secBuff[thisFATEntOffset(n)] &= 0xf000;
  }
  *(unsigned short *)&secBuff[thisFATEntOffset(n)] |= clusEntryVal;
}
 
#endif