1/* ----------------------------------------------------------------------- *
2 *
3 *   Copyright 2004-2008 H. Peter Anvin - All Rights Reserved
4 *
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.
10 *
11 * ----------------------------------------------------------------------- */
12
13/*
14 * open.c
15 *
16 * Open a FAT filesystem and compute some initial values; return NULL
17 * on failure.
18 */
19
20#include <stdlib.h>
21#include "libfatint.h"
22#include "ulint.h"
23
24struct libfat_filesystem *
25libfat_open(int (*readfunc) (intptr_t, void *, size_t, libfat_sector_t),
26	    intptr_t readptr)
27{
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;
33
34    fs = malloc(sizeof(struct libfat_filesystem));
35    if (!fs)
36	goto barf;
37
38    fs->sectors = NULL;
39    fs->read = readfunc;
40    fs->readptr = readptr;
41
42    bs = libfat_get_sector(fs, 0);
43    if (!bs)
44	goto barf;
45
46    if (read16(&bs->bsBytesPerSec) != LIBFAT_SECTOR_SIZE)
47	goto barf;
48
49    for (i = 0; i <= 8; i++) {
50	if ((uint8_t) (1 << i) == read8(&bs->bsSecPerClust))
51	    break;
52    }
53    if (i > 8)
54	goto barf;
55    fs->clustsize = 1 << i;	/* Treat 0 as 2^8 = 64K */
56    fs->clustshift = i;
57
58    sectors = read16(&bs->bsSectors);
59    if (!sectors)
60	sectors = read32(&bs->bsHugeSectors);
61
62    fs->end = sectors;
63
64    fs->fat = read16(&bs->bsResSectors);
65    fatsize = read16(&bs->bsFATsecs);
66    if (!fatsize)
67	fatsize = read32(&bs->u.fat32.bpb_fatsz32);
68
69    fs->rootdir = fs->fat + fatsize * read8(&bs->bsFATs);
70
71    rootdirsize = ((read16(&bs->bsRootDirEnts) << 5) + LIBFAT_SECTOR_MASK)
72	>> LIBFAT_SECTOR_SHIFT;
73    fs->data = fs->rootdir + rootdirsize;
74
75    /* Sanity checking */
76    if (fs->data >= fs->end)
77	goto barf;
78
79    /* Figure out how many clusters */
80    nclusters = (fs->end - fs->data) >> fs->clustshift;
81    fs->endcluster = nclusters + 2;
82
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 */
94
95    minfatsize = (minfatsize + LIBFAT_SECTOR_SIZE - 1) >> LIBFAT_SECTOR_SHIFT;
96
97    if (minfatsize > fatsize)
98	goto barf;		/* The FATs don't fit */
99
100    if (fs->fat_type == FAT28)
101	fs->rootcluster = read32(&bs->u.fat32.bpb_rootclus);
102    else
103	fs->rootcluster = 0;
104
105    return fs;			/* All good */
106
107barf:
108    if (fs)
109	free(fs);
110    return NULL;
111}
112
113void libfat_close(struct libfat_filesystem *fs)
114{
115    libfat_flush(fs);
116    free(fs);
117}
118