Oops, fix up my previous commit properly.
[dragonfly.git] / lib / libfsid / msdosfs.c
blob65d81077b657f08b85db453d75dfc790f1a4dc84
1 /*
2 * Copyright (c) 2010 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Alex Hornung <ahornung@gmail.com
6 * and Ákos Kovács <akoskovacs@gmx.com>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 * 3. Neither the name of The DragonFly Project nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific, prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
36 #include "libfsid.h"
38 #include <vfs/msdosfs/bootsect.h>
40 #define MSDOS_BOOT_BLOCK_SIZE 512
42 static char *get_volname(char *buf);
43 static char buffer[MSDOS_BOOT_BLOCK_SIZE * 4];
45 fsid_t
46 msdosfs_probe(const char *dev)
48 if (fsid_dev_read(dev, 0L, sizeof(buffer), buffer) != 0)
49 return FSID_UNKNOWN;
51 if (get_volname(buffer) != NULL)
52 return FSID_MSDOSFS;
54 return FSID_MSDOSFS;
56 char *
57 msdosfs_volname(const char *dev)
59 char *volname;
61 if (fsid_dev_read(dev, 0L, sizeof(buffer), buffer) != 0)
62 return NULL;
64 volname = get_volname(buffer);
66 if (volname == NULL || volname[0] == '\0')
67 return NULL;
69 volname[sizeof(volname) - 1] = '\0';
71 return volname;
75 * This function used to get the offset address of
76 * the volume name, of the FAT partition.
77 * It also checks, that is a real FAT partition.
79 static char *
80 get_volname(char *buff)
82 struct bootsector710 *bpb7;
83 struct bootsector50 *bpb4;
84 struct extboot *extb;
86 bpb7 = (struct bootsector710 *)buff;
87 bpb4 = (struct bootsector50 *)buff;
90 * First, assume BPB v7
92 extb = (struct extboot *)bpb7->bsExt;
93 if ((extb->exBootSignature == 0x28 || extb->exBootSignature == 0x29) &&
94 strncmp(extb->exFileSysType, "FAT", 3) == 0)
95 return extb->exVolumeLabel;
98 * If this is not a BPB v7, try it as a bpb v4.
100 extb = (struct extboot *)bpb4->bsExt;
101 if ((extb->exBootSignature == EXBOOTSIG ||
102 extb->exBootSignature == EXBOOTSIG2) &&
103 strncmp(extb->exFileSysType, "FAT", 3) == 0)
104 return extb->exVolumeLabel;
107 * If previous checks failed, it may not a FAT filesystem, or
108 * it may an older one without a volume name.
110 return NULL;