summaryrefslogtreecommitdiffstats
path: root/contrib/syslinux-4.02/libfat/open.c
blob: 7281e03fae4080d1376c0da48aa9119a2d2b7c93 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/* ----------------------------------------------------------------------- *
 *
 *   Copyright 2004-2008 H. Peter Anvin - All Rights Reserved
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, Inc., 53 Temple Place Ste 330,
 *   Boston MA 02111-1307, USA; either version 2 of the License, or
 *   (at your option) any later version; incorporated herein by reference.
 *
 * ----------------------------------------------------------------------- */

/*
 * open.c
 *
 * Open a FAT filesystem and compute some initial values; return NULL
 * on failure.
 */

#include <stdlib.h>
#include "libfatint.h"
#include "ulint.h"

struct libfat_filesystem *
libfat_open(int (*readfunc) (intptr_t, void *, size_t, libfat_sector_t),
	    intptr_t readptr)
{
    struct libfat_filesystem *fs = NULL;
    struct fat_bootsect *bs;
    int i;
    uint32_t sectors, fatsize, minfatsize, rootdirsize;
    uint32_t nclusters;

    fs = malloc(sizeof(struct libfat_filesystem));
    if (!fs)
	goto barf;

    fs->sectors = NULL;
    fs->read = readfunc;
    fs->readptr = readptr;

    bs = libfat_get_sector(fs, 0);
    if (!bs)
	goto barf;

    if (read16(&bs->bsBytesPerSec) != LIBFAT_SECTOR_SIZE)
	goto barf;

    for (i = 0; i <= 8; i++) {
	if ((uint8_t) (1 << i) == read8(&bs->bsSecPerClust))
	    break;
    }
    if (i > 8)
	goto barf;
    fs->clustsize = 1 << i;	/* Treat 0 as 2^8 = 64K */
    fs->clustshift = i;

    sectors = read16(&bs->bsSectors);
    if (!sectors)
	sectors = read32(&bs->bsHugeSectors);

    fs->end = sectors;

    fs->fat = read16(&bs->bsResSectors);
    fatsize = read16(&bs->bsFATsecs);
    if (!fatsize)
	fatsize = read32(&bs->u.fat32.bpb_fatsz32);

    fs->rootdir = fs->fat + fatsize * read8(&bs->bsFATs);

    rootdirsize = ((read16(&bs->bsRootDirEnts) << 5) + LIBFAT_SECTOR_MASK)
	>> LIBFAT_SECTOR_SHIFT;
    fs->data = fs->rootdir + rootdirsize;

    /* Sanity checking */
    if (fs->data >= fs->end)
	goto barf;

    /* Figure out how many clusters */
    nclusters = (fs->end - fs->data) >> fs->clustshift;
    fs->endcluster = nclusters + 2;

    if (nclusters <= 0xff4) {
	fs->fat_type = FAT12;
	minfatsize = fs->endcluster + (fs->endcluster >> 1);
    } else if (nclusters <= 0xfff4) {
	fs->fat_type = FAT16;
	minfatsize = fs->endcluster << 1;
    } else if (nclusters <= 0xffffff4) {
	fs->fat_type = FAT28;
	minfatsize = fs->endcluster << 2;
    } else
	goto barf;		/* Impossibly many clusters */

    minfatsize = (minfatsize + LIBFAT_SECTOR_SIZE - 1) >> LIBFAT_SECTOR_SHIFT;

    if (minfatsize > fatsize)
	goto barf;		/* The FATs don't fit */

    if (fs->fat_type == FAT28)
	fs->rootcluster = read32(&bs->u.fat32.bpb_rootclus);
    else
	fs->rootcluster = 0;

    return fs;			/* All good */

barf:
    if (fs)
	free(fs);
    return NULL;
}

void libfat_close(struct libfat_filesystem *fs)
{
    libfat_flush(fs);
    free(fs);
}