2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2004 Pawel Jakub Dawidek <pjd@FreeBSD.org>
5 * Copyright (c) 2006 Tobias Reifenberger
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
38 #include <geom/geom.h>
39 #include <geom/geom_dbg.h>
40 #include <geom/label/g_label.h>
41 #include <geom/label/g_label_msdosfs.h>
43 #define G_LABEL_MSDOSFS_DIR "msdosfs"
44 #define LABEL_NO_NAME "NO NAME "
47 g_label_msdosfs_taste(struct g_consumer *cp, char *label, size_t size)
49 struct g_provider *pp;
50 FAT_BSBPB *pfat_bsbpb;
51 FAT32_BSBPB *pfat32_bsbpb;
53 uint8_t *sector0, *sector;
55 g_topology_assert_not();
61 /* Check if the sector size of the medium is a valid FAT sector size. */
62 switch(pp->sectorsize) {
69 G_LABEL_DEBUG(1, "MSDOSFS: %s: sector size %d not compatible.",
70 pp->name, pp->sectorsize);
74 /* Load 1st sector with boot sector and boot parameter block. */
75 sector0 = (uint8_t *)g_read_data(cp, 0, pp->sectorsize, NULL);
79 /* Check for the FAT boot sector signature. */
80 if (sector0[510] != 0x55 || sector0[511] != 0xaa) {
81 G_LABEL_DEBUG(1, "MSDOSFS: %s: no FAT signature found.",
88 * Test if this is really a FAT volume and determine the FAT type.
91 pfat_bsbpb = (FAT_BSBPB *)sector0;
92 pfat32_bsbpb = (FAT32_BSBPB *)sector0;
94 if (UINT16BYTES(pfat_bsbpb->BPB_FATSz16) != 0) {
96 * If the BPB_FATSz16 field is not zero and the string "FAT" is
97 * at the right place, this should be a FAT12 or FAT16 volume.
99 if (strncmp(pfat_bsbpb->BS_FilSysType, "FAT", 3) != 0) {
101 "MSDOSFS: %s: FAT12/16 volume not valid.",
105 G_LABEL_DEBUG(1, "MSDOSFS: %s: FAT12/FAT16 volume detected.",
108 /* A volume with no name should have "NO NAME " as label. */
109 if (strncmp(pfat_bsbpb->BS_VolLab, LABEL_NO_NAME,
110 sizeof(pfat_bsbpb->BS_VolLab)) == 0) {
112 "MSDOSFS: %s: FAT12/16 volume has no name.",
116 strlcpy(label, pfat_bsbpb->BS_VolLab,
117 MIN(size, sizeof(pfat_bsbpb->BS_VolLab) + 1));
118 } else if (UINT32BYTES(pfat32_bsbpb->BPB_FATSz32) != 0) {
119 uint32_t fat_FirstDataSector, fat_BytesPerSector, offset;
122 * If the BPB_FATSz32 field is not zero and the string "FAT" is
123 * at the right place, this should be a FAT32 volume.
125 if (strncmp(pfat32_bsbpb->BS_FilSysType, "FAT", 3) != 0) {
126 G_LABEL_DEBUG(1, "MSDOSFS: %s: FAT32 volume not valid.",
130 G_LABEL_DEBUG(1, "MSDOSFS: %s: FAT32 volume detected.",
134 * If the volume label is not "NO NAME " we're done.
136 if (strncmp(pfat32_bsbpb->BS_VolLab, LABEL_NO_NAME,
137 sizeof(pfat32_bsbpb->BS_VolLab)) != 0) {
138 strlcpy(label, pfat32_bsbpb->BS_VolLab,
139 MIN(size, sizeof(pfat32_bsbpb->BS_VolLab) + 1));
144 * If the volume label "NO NAME " is in the boot sector, the
145 * label of FAT32 volumes may be stored as a special entry in
146 * the root directory.
148 fat_FirstDataSector =
149 UINT16BYTES(pfat32_bsbpb->BPB_RsvdSecCnt) +
150 (pfat32_bsbpb->BPB_NumFATs *
151 UINT32BYTES(pfat32_bsbpb->BPB_FATSz32));
152 fat_BytesPerSector = UINT16BYTES(pfat32_bsbpb->BPB_BytsPerSec);
155 "MSDOSFS: FAT_FirstDataSector=0x%x, FAT_BytesPerSector=%d",
156 fat_FirstDataSector, fat_BytesPerSector);
158 for (offset = fat_BytesPerSector * fat_FirstDataSector;;
159 offset += fat_BytesPerSector) {
160 sector = (uint8_t *)g_read_data(cp, offset,
161 fat_BytesPerSector, NULL);
165 pfat_entry = (FAT_DES *)sector;
167 /* No more entries available. */
168 if (pfat_entry->DIR_Name[0] == 0) {
169 G_LABEL_DEBUG(1, "MSDOSFS: %s: "
170 "FAT32 volume has no name.",
175 /* Skip empty or long name entries. */
176 if (pfat_entry->DIR_Name[0] == 0xe5 ||
177 (pfat_entry->DIR_Attr &
178 FAT_DES_ATTR_LONG_NAME) ==
179 FAT_DES_ATTR_LONG_NAME) {
184 * The name of the entry is the volume label if
185 * ATTR_VOLUME_ID is set.
187 if (pfat_entry->DIR_Attr &
188 FAT_DES_ATTR_VOLUME_ID) {
189 strlcpy(label, pfat_entry->DIR_Name,
191 sizeof(pfat_entry->DIR_Name) + 1));
194 } while((uint8_t *)(++pfat_entry) <
195 (uint8_t *)(sector + fat_BytesPerSector));
199 G_LABEL_DEBUG(1, "MSDOSFS: %s: no FAT volume detected.",
205 g_label_rtrim(label, size);
214 struct g_label_desc g_label_msdosfs = {
215 .ld_taste = g_label_msdosfs_taste,
216 .ld_dir = G_LABEL_MSDOSFS_DIR,
220 G_LABEL_INIT(msdosfs, g_label_msdosfs, "Create device nodes for MSDOSFS volumes");