syslinux-3.08-2 sources from FC4
[bootcd.git] / syslinux / libfat / open.c
1 #ident "$Id: open.c,v 1.2 2004/12/15 20:29:17 hpa Exp $"
2 /* ----------------------------------------------------------------------- *
3  *   
4  *   Copyright 2004 H. Peter Anvin - All Rights Reserved
5  *
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation, Inc., 53 Temple Place Ste 330,
9  *   Boston MA 02111-1307, USA; either version 2 of the License, or
10  *   (at your option) any later version; incorporated herein by reference.
11  *
12  * ----------------------------------------------------------------------- */
13
14 /*
15  * open.c
16  *
17  * Open a FAT filesystem and compute some initial values; return NULL
18  * on failure.
19  */
20
21 #include <stdlib.h>
22 #include "libfatint.h"
23 #include "ulint.h"
24
25 struct libfat_filesystem *
26 libfat_open(int (*readfunc)(intptr_t, void *, size_t, libfat_sector_t),
27             intptr_t readptr)
28 {
29   struct libfat_filesystem *fs = NULL;
30   struct fat_bootsect *bs;
31   int i;
32   uint32_t sectors, fatsize, minfatsize, rootdirsize;
33   uint32_t nclusters;
34
35   fs = malloc(sizeof(struct libfat_filesystem));
36   if ( !fs )
37     goto barf;
38   
39   fs->sectors = NULL;
40   fs->read = readfunc;
41   fs->readptr = readptr;
42
43   bs = libfat_get_sector(fs, 0);
44   if ( !bs ) 
45     goto barf;
46
47   if ( read16(&bs->bsBytesPerSec) != LIBFAT_SECTOR_SIZE )
48     goto barf;
49
50   for ( i = 0 ; i <= 8 ; i++ ) {
51     if ( (uint8_t)(1 << i) == read8(&bs->bsSecPerClust) )
52       break;
53   }
54   if ( i > 8 )
55     goto barf;
56   fs->clustsize  = 1 << i;      /* Treat 0 as 2^8 = 64K */
57   fs->clustshift = i;
58   
59   sectors = read16(&bs->bsSectors);
60   if ( !sectors )
61     sectors = read32(&bs->bsHugeSectors);
62
63   fs->end = sectors;
64   
65   fs->fat     = read16(&bs->bsResSectors);
66   fatsize     = read16(&bs->bsFATsecs);
67   if ( !fatsize )
68     fatsize   = read32(&bs->u.fat32.bpb_fatsz32);
69   
70   fs->rootdir = fs->fat + fatsize * read8(&bs->bsFATs);
71   
72   rootdirsize = ((read16(&bs->bsRootDirEnts) << 5) + LIBFAT_SECTOR_MASK)
73                                             >> LIBFAT_SECTOR_SHIFT;
74   fs->data    = fs->rootdir + rootdirsize;
75
76   /* Sanity checking */
77   if ( fs->data >= fs->end )
78     goto barf;
79
80   /* Figure out how many clusters */
81   nclusters = (fs->end - fs->data) >> fs->clustshift;
82   fs->endcluster = nclusters + 2;
83
84   if ( nclusters <= 0xff4 ) {
85     fs->fat_type = FAT12;
86     minfatsize = fs->endcluster + (fs->endcluster >> 1);
87   } else if ( nclusters <= 0xfff4 ) {
88     fs->fat_type = FAT16;
89     minfatsize = fs->endcluster << 1;
90   }  else if ( nclusters <= 0xffffff4 ) {
91     fs->fat_type = FAT28;
92     minfatsize = fs->endcluster << 2;
93   } else
94     goto barf;                  /* Impossibly many clusters */
95   
96   minfatsize = (minfatsize + LIBFAT_SECTOR_SIZE-1) >> LIBFAT_SECTOR_SHIFT;
97
98   if ( minfatsize > fatsize )
99     goto barf;                  /* The FATs don't fit */
100
101   if ( fs->fat_type == FAT28 )
102     fs->rootcluster = read32(&bs->u.fat32.bpb_rootclus);
103   else
104     fs->rootcluster = 0;
105
106   return fs;                    /* All good */
107
108  barf:
109   if ( fs )
110     free(fs);
111   return NULL;
112 }
113
114 void libfat_close(struct libfat_filesystem *fs)
115 {
116   libfat_flush(fs);
117   free(fs);
118 }