Merge branch 'vendor/GDB'
[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
36 #include "libfsid.h"
37
38 #include <vfs/msdosfs/bootsect.h>
39
40 #define MSDOS_BOOT_BLOCK_SIZE 512
41
42 static char *get_volname(char *buf);
43 static char buffer[MSDOS_BOOT_BLOCK_SIZE * 4];
44
45 fsid_t
46 msdosfs_probe(const char *dev)
47 {
48         if (fsid_dev_read(dev, 0L, sizeof(buffer), buffer) != 0)
49                 return FSID_UNKNOWN;
50
51         if (get_volname(buffer) != NULL)
52                 return FSID_MSDOSFS;
53
54         return FSID_MSDOSFS;
55 }
56 char *
57 msdosfs_volname(const char *dev)
58 {
59         char *volname;
60
61         if (fsid_dev_read(dev, 0L, sizeof(buffer), buffer) != 0)
62                 return NULL;
63
64         volname = get_volname(buffer);
65
66         if (volname == NULL || volname[0] == '\0')
67                 return NULL;
68
69         volname[sizeof(volname) - 1] = '\0';
70
71         return volname;
72 }
73
74 /*
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.
78 */
79 static char *
80 get_volname(char *buff)
81 {
82         struct bootsector710 *bpb7;
83         struct bootsector50 *bpb4;
84         struct extboot *extb;
85
86         bpb7 = (struct bootsector710 *)buff;
87         bpb4 = (struct bootsector50 *)buff;
88
89         /*
90          * First, assume BPB v7
91          */
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;
96
97         /*
98          * If this is not a BPB v7, try it as a bpb v4.
99          */
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;
105
106         /*
107          * If previous checks failed, it may not a FAT filesystem, or
108          * it may an older one without a volume name.
109          */
110         return NULL;
111 }