1 #ident "$Id: open.c,v 1.2 2004/12/15 20:29:17 hpa Exp $"
2 /* ----------------------------------------------------------------------- *
4 * Copyright 2004 H. Peter Anvin - All Rights Reserved
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.
12 * ----------------------------------------------------------------------- */
17 * Open a FAT filesystem and compute some initial values; return NULL
22 #include "libfatint.h"
25 struct libfat_filesystem *
26 libfat_open(int (*readfunc)(intptr_t, void *, size_t, libfat_sector_t),
29 struct libfat_filesystem *fs = NULL;
30 struct fat_bootsect *bs;
32 uint32_t sectors, fatsize, minfatsize, rootdirsize;
35 fs = malloc(sizeof(struct libfat_filesystem));
41 fs->readptr = readptr;
43 bs = libfat_get_sector(fs, 0);
47 if ( read16(&bs->bsBytesPerSec) != LIBFAT_SECTOR_SIZE )
50 for ( i = 0 ; i <= 8 ; i++ ) {
51 if ( (uint8_t)(1 << i) == read8(&bs->bsSecPerClust) )
56 fs->clustsize = 1 << i; /* Treat 0 as 2^8 = 64K */
59 sectors = read16(&bs->bsSectors);
61 sectors = read32(&bs->bsHugeSectors);
65 fs->fat = read16(&bs->bsResSectors);
66 fatsize = read16(&bs->bsFATsecs);
68 fatsize = read32(&bs->u.fat32.bpb_fatsz32);
70 fs->rootdir = fs->fat + fatsize * read8(&bs->bsFATs);
72 rootdirsize = ((read16(&bs->bsRootDirEnts) << 5) + LIBFAT_SECTOR_MASK)
73 >> LIBFAT_SECTOR_SHIFT;
74 fs->data = fs->rootdir + rootdirsize;
77 if ( fs->data >= fs->end )
80 /* Figure out how many clusters */
81 nclusters = (fs->end - fs->data) >> fs->clustshift;
82 fs->endcluster = nclusters + 2;
84 if ( nclusters <= 0xff4 ) {
86 minfatsize = fs->endcluster + (fs->endcluster >> 1);
87 } else if ( nclusters <= 0xfff4 ) {
89 minfatsize = fs->endcluster << 1;
90 } else if ( nclusters <= 0xffffff4 ) {
92 minfatsize = fs->endcluster << 2;
94 goto barf; /* Impossibly many clusters */
96 minfatsize = (minfatsize + LIBFAT_SECTOR_SIZE-1) >> LIBFAT_SECTOR_SHIFT;
98 if ( minfatsize > fatsize )
99 goto barf; /* The FATs don't fit */
101 if ( fs->fat_type == FAT28 )
102 fs->rootcluster = read32(&bs->u.fat32.bpb_rootclus);
106 return fs; /* All good */
114 void libfat_close(struct libfat_filesystem *fs)