xref: /freebsd/sys/geom/label/g_label_msdosfs.c (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2004 Pawel Jakub Dawidek <pjd@FreeBSD.org>
5  * Copyright (c) 2006 Tobias Reifenberger
6  * All rights reserved.
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  * 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.
16  *
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
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/malloc.h>
35 
36 #include <geom/geom.h>
37 #include <geom/geom_dbg.h>
38 #include <geom/label/g_label.h>
39 #include <geom/label/g_label_msdosfs.h>
40 
41 #define LABEL_NO_NAME		"NO NAME    "
42 
43 static void
44 g_label_msdosfs_taste(struct g_consumer *cp, char *label, size_t size)
45 {
46 	struct g_provider *pp;
47 	FAT_BSBPB *pfat_bsbpb;
48 	FAT32_BSBPB *pfat32_bsbpb;
49 	FAT_DES *pfat_entry;
50 	uint8_t *sector0, *sector;
51 	size_t copysize;
52 
53 	g_topology_assert_not();
54 	pp = cp->provider;
55 	sector0 = NULL;
56 	sector = NULL;
57 	bzero(label, size);
58 
59 	/* Check if the sector size of the medium is a valid FAT sector size. */
60 	switch(pp->sectorsize) {
61 	case 512:
62 	case 1024:
63 	case 2048:
64 	case 4096:
65 		break;
66 	default:
67 		G_LABEL_DEBUG(1, "MSDOSFS: %s: sector size %d not compatible.",
68 		    pp->name, pp->sectorsize);
69 		return;
70 	}
71 
72 	/* Load 1st sector with boot sector and boot parameter block. */
73 	sector0 = (uint8_t *)g_read_data(cp, 0, pp->sectorsize, NULL);
74 	if (sector0 == NULL)
75 		return;
76 
77 	/* Check for the FAT boot sector signature. */
78 	if (sector0[510] != 0x55 || sector0[511] != 0xaa) {
79 		G_LABEL_DEBUG(1, "MSDOSFS: %s: no FAT signature found.",
80 		    pp->name);
81 		goto error;
82 	}
83 
84 	/*
85 	 * Test if this is really a FAT volume and determine the FAT type.
86 	 */
87 
88 	pfat_bsbpb = (FAT_BSBPB *)sector0;
89 	pfat32_bsbpb = (FAT32_BSBPB *)sector0;
90 
91 	if (UINT16BYTES(pfat_bsbpb->BPB_FATSz16) != 0) {
92 		/*
93 		 * If the BPB_FATSz16 field is not zero and the string "FAT" is
94 		 * at the right place, this should be a FAT12 or FAT16 volume.
95 		 */
96 		if (strncmp(pfat_bsbpb->BS_FilSysType, "FAT", 3) != 0) {
97 			G_LABEL_DEBUG(1,
98 			    "MSDOSFS: %s: FAT12/16 volume not valid.",
99 			    pp->name);
100 			goto error;
101 		}
102 		G_LABEL_DEBUG(1, "MSDOSFS: %s: FAT12/FAT16 volume detected.",
103 		    pp->name);
104 
105 		/* A volume with no name should have "NO NAME    " as label. */
106 		if (strncmp(pfat_bsbpb->BS_VolLab, LABEL_NO_NAME,
107 		    sizeof(pfat_bsbpb->BS_VolLab)) == 0) {
108 			G_LABEL_DEBUG(1,
109 			    "MSDOSFS: %s: FAT12/16 volume has no name.",
110 			    pp->name);
111 			goto error;
112 		}
113 		copysize = MIN(size - 1, sizeof(pfat_bsbpb->BS_VolLab));
114 		memcpy(label, pfat_bsbpb->BS_VolLab, copysize);
115 		label[copysize] = '\0';
116 	} else if (UINT32BYTES(pfat32_bsbpb->BPB_FATSz32) != 0) {
117 		uint32_t fat_FirstDataSector, fat_BytesPerSector, offset;
118 
119 		/*
120 		 * If the BPB_FATSz32 field is not zero and the string "FAT" is
121 		 * at the right place, this should be a FAT32 volume.
122 		 */
123 		if (strncmp(pfat32_bsbpb->BS_FilSysType, "FAT", 3) != 0) {
124 			G_LABEL_DEBUG(1, "MSDOSFS: %s: FAT32 volume not valid.",
125 			    pp->name);
126 			goto error;
127 		}
128 		G_LABEL_DEBUG(1, "MSDOSFS: %s: FAT32 volume detected.",
129 		    pp->name);
130 
131 		/*
132 		 * If the volume label is not "NO NAME    " we're done.
133 		 */
134 		if (strncmp(pfat32_bsbpb->BS_VolLab, LABEL_NO_NAME,
135 		    sizeof(pfat32_bsbpb->BS_VolLab)) != 0) {
136 			copysize = MIN(size - 1,
137 			    sizeof(pfat32_bsbpb->BS_VolLab));
138 			memcpy(label, pfat32_bsbpb->BS_VolLab, copysize);
139 			label[copysize] = '\0';
140 			goto endofchecks;
141 		}
142 
143 		/*
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.
147 		 */
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);
153 
154 		G_LABEL_DEBUG(2,
155 		    "MSDOSFS: FAT_FirstDataSector=0x%x, FAT_BytesPerSector=%d",
156 		    fat_FirstDataSector, fat_BytesPerSector);
157 		if (fat_BytesPerSector == 0 ||
158 		    fat_BytesPerSector % pp->sectorsize != 0) {
159 			G_LABEL_DEBUG(1, "MSDOSFS: %s: corrupted BPB",
160 			    pp->name);
161 			goto error;
162 		}
163 
164 		for (offset = fat_BytesPerSector * fat_FirstDataSector;;
165 		    offset += fat_BytesPerSector) {
166 			sector = (uint8_t *)g_read_data(cp, offset,
167 			    fat_BytesPerSector, NULL);
168 			if (sector == NULL)
169 				goto error;
170 
171 			pfat_entry = (FAT_DES *)sector;
172 			do {
173 				/* No more entries available. */
174 				if (pfat_entry->DIR_Name[0] == 0) {
175 					G_LABEL_DEBUG(1, "MSDOSFS: %s: "
176 					    "FAT32 volume has no name.",
177 					    pp->name);
178 					goto error;
179 				}
180 
181 				/* Skip empty or long name entries. */
182 				if (pfat_entry->DIR_Name[0] == 0xe5 ||
183 				    (pfat_entry->DIR_Attr &
184 				     FAT_DES_ATTR_LONG_NAME) ==
185 				    FAT_DES_ATTR_LONG_NAME) {
186 					continue;
187 				}
188 
189 				/*
190 				 * The name of the entry is the volume label if
191 				 * ATTR_VOLUME_ID is set.
192 				 */
193 				if (pfat_entry->DIR_Attr &
194 				    FAT_DES_ATTR_VOLUME_ID) {
195 					copysize = MIN(size - 1,
196 					    sizeof(pfat_entry->DIR_Name));
197 					memcpy(label, pfat_entry->DIR_Name,
198 					    copysize);
199 					label[copysize] = '\0';
200 					goto endofchecks;
201 				}
202 			} while((uint8_t *)(++pfat_entry) <
203 			    (uint8_t *)(sector + fat_BytesPerSector));
204 			g_free(sector);
205 		}
206 	} else {
207 		G_LABEL_DEBUG(1, "MSDOSFS: %s: no FAT volume detected.",
208 		    pp->name);
209 		goto error;
210 	}
211 
212 endofchecks:
213 	g_label_rtrim(label, size);
214 
215 error:
216 	g_free(sector0);
217 	g_free(sector);
218 }
219 
220 struct g_label_desc g_label_msdosfs = {
221 	.ld_taste = g_label_msdosfs_taste,
222 	.ld_dirprefix = "msdosfs/",
223 	.ld_enabled = 1
224 };
225 
226 G_LABEL_INIT(msdosfs, g_label_msdosfs, "Create device nodes for MSDOSFS volumes");
227