libfsid - A new library to determine filesystems.
[dragonfly.git] / lib / libfsid / msdosfs.c
1 /*
2  * Copyright (c) 2010 The DragonFly Project.  All rights reserved.
3  *
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>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
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.
21  *
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.
34  */
35 #include "libfsid.h"
36 #include <vfs/msdosfs/bootsect.h>
37
38 #define MSDOS_BOOT_BLOCK_SIZE 512
39
40 static char buffer[MSDOS_BOOT_BLOCK_SIZE * 4];
41 static char *get_volname(char *buf);
42
43 int
44 msdosfs_probe(const char *dev)
45 {
46         if (fsid_dev_read(dev, 0L, sizeof(buffer), buffer) != 0)
47                 return 0;
48
49         if (get_volname(buffer) != NULL)
50                 return 1;
51         else
52                 return 0;
53 }
54 char *
55 msdosfs_volname(const char *dev)
56 {
57         char *volname;
58
59         if (fsid_dev_read(dev, 0L, sizeof(buffer), buffer) != 0)
60                 return NULL;
61
62         volname = get_volname(buffer);
63         if (volname == NULL)
64                 return NULL;
65
66         if (volname[0] == '\0')
67                 return NULL;
68
69         volname[10] = '\0';
70         return volname;
71 }
72
73 /*
74  * This function used to get the offset address of
75  * the volume name, of the FAT partition.
76  * It also checks, that is a real FAT partition.
77 */
78 static char *
79 get_volname(char *buff)
80 {
81         struct bootsector710 *bpb7;
82         struct bootsector50 *bpb4;
83         struct extboot *extb;
84
85         bpb7 = (struct bootsector710 *)buff;
86         bpb4 = (struct bootsector50 *)buff;
87
88         /*
89          * First, assume BPB v7
90          */
91         extb = (struct extboot *)bpb7->bsExt;
92         if ((extb->exBootSignature == 0x28 || extb->exBootSignature == 0x29) &&
93             strncmp(extb->exFileSysType, "FAT", 3) == 0)
94                 return extb->exVolumeLabel;
95
96         /*
97          * If this is not a BPB v7, try it as a bpb v4.
98          */
99         extb = (struct extboot *)bpb4->bsExt;
100         if ((extb->exBootSignature == 0x28 || extb->exBootSignature == 0x29) &&
101             strncmp(extb->exFileSysType, "FAT", 3) == 0)
102                 return extb->exVolumeLabel;
103
104         /*
105          * If previous checks failed, it may not a FAT filesystem, or
106          * it may an older one without a volume name.
107          */
108         return NULL;
109 }