Home | History | Annotate | Download | only in GenBootSector
      1 /** @file
      2 
      3   Get Drv Num offset from Fat file system.
      4 
      5 Copyright (c) 2006 - 2008, Intel Corporation. All rights reserved.<BR>
      6 This program and the accompanying materials
      7 are licensed and made available under the terms and conditions of the BSD License
      8 which accompanies this distribution.  The full text of the license may be found at
      9 http://opensource.org/licenses/bsd-license.php
     10 
     11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
     12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
     13 
     14 **/
     15 
     16 #include <stdio.h>
     17 #include "FatFormat.h"
     18 
     19 INTN
     20 GetDrvNumOffset (
     21   IN VOID *BootSector
     22   )
     23 {
     24   FAT_BPB_STRUCT  *FatBpb;
     25   UINTN           RootDirSectors;
     26   UINTN           FATSz;
     27   UINTN           TotSec;
     28   UINTN           DataSec;
     29   UINTN           CountOfClusters;
     30 
     31   FatBpb  = (FAT_BPB_STRUCT *) BootSector;
     32 
     33   //
     34   // Check FAT type algorithm from FAT spec
     35   //
     36   RootDirSectors = ((FatBpb->Fat12_16.BPB_RootEntCnt * sizeof(FAT_DIRECTORY_ENTRY)) +
     37                     (FatBpb->Fat12_16.BPB_BytsPerSec - 1)) / FatBpb->Fat12_16.BPB_BytsPerSec;
     38 
     39   if (FatBpb->Fat12_16.BPB_FATSz16 != 0) {
     40     FATSz = FatBpb->Fat12_16.BPB_FATSz16;
     41   } else {
     42     FATSz = FatBpb->Fat32.BPB_FATSz32;
     43   }
     44   if (FATSz == 0) {
     45     fprintf (stderr, "error E3003: FAT - BPB_FATSz16, BPB_FATSz32 - 0, expected: Non-Zero number\n");
     46     return -1;
     47   }
     48 
     49   if (FatBpb->Fat12_16.BPB_TotSec16 != 0) {
     50     TotSec = FatBpb->Fat12_16.BPB_TotSec16;
     51   } else {
     52     TotSec = FatBpb->Fat12_16.BPB_TotSec32;
     53   }
     54   if (TotSec == 0) {
     55     fprintf (stderr, "error E3003: FAT - BPB_TotSec16, BPB_TotSec32 - 0, expected: Non-Zero number\n");
     56     return -1;
     57   }
     58 
     59   DataSec = TotSec - (
     60                       FatBpb->Fat12_16.BPB_RsvdSecCnt +
     61                       FatBpb->Fat12_16.BPB_NumFATs * FATSz +
     62                       RootDirSectors
     63                      );
     64 
     65   CountOfClusters = DataSec / FatBpb->Fat12_16.BPB_SecPerClus;
     66 
     67   if (CountOfClusters < FAT_MAX_FAT16_CLUSTER) {
     68     return (INTN) ((UINTN) &FatBpb->Fat12_16.BS_DrvNum - (UINTN) FatBpb);
     69   } else {
     70     return (INTN) ((UINTN) &FatBpb->Fat32.BS_DrvNum - (UINTN) FatBpb);
     71   }
     72 }
     73 
     74