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