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