]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/geom/label/g_label_msdosfs.c
amd64: use register macros for gdb_cpu_getreg()
[FreeBSD/FreeBSD.git] / sys / geom / label / g_label_msdosfs.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 __FBSDID("$FreeBSD$");
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37
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>
42
43 #define LABEL_NO_NAME           "NO NAME    "
44
45 static void
46 g_label_msdosfs_taste(struct g_consumer *cp, char *label, size_t size)
47 {
48         struct g_provider *pp;
49         FAT_BSBPB *pfat_bsbpb;
50         FAT32_BSBPB *pfat32_bsbpb;
51         FAT_DES *pfat_entry;
52         uint8_t *sector0, *sector;
53
54         g_topology_assert_not();
55         pp = cp->provider;
56         sector0 = NULL;
57         sector = NULL;
58         bzero(label, size);
59
60         /* Check if the sector size of the medium is a valid FAT sector size. */
61         switch(pp->sectorsize) {
62         case 512:
63         case 1024:
64         case 2048:
65         case 4096:
66                 break;
67         default:
68                 G_LABEL_DEBUG(1, "MSDOSFS: %s: sector size %d not compatible.",
69                     pp->name, pp->sectorsize);
70                 return;
71         }
72
73         /* Load 1st sector with boot sector and boot parameter block. */
74         sector0 = (uint8_t *)g_read_data(cp, 0, pp->sectorsize, NULL);
75         if (sector0 == NULL)
76                 return;
77
78         /* Check for the FAT boot sector signature. */
79         if (sector0[510] != 0x55 || sector0[511] != 0xaa) {
80                 G_LABEL_DEBUG(1, "MSDOSFS: %s: no FAT signature found.",
81                     pp->name);
82                 goto error;
83         }
84
85         /*
86          * Test if this is really a FAT volume and determine the FAT type.
87          */
88
89         pfat_bsbpb = (FAT_BSBPB *)sector0;
90         pfat32_bsbpb = (FAT32_BSBPB *)sector0;
91
92         if (UINT16BYTES(pfat_bsbpb->BPB_FATSz16) != 0) {
93                 /*
94                  * If the BPB_FATSz16 field is not zero and the string "FAT" is
95                  * at the right place, this should be a FAT12 or FAT16 volume.
96                  */
97                 if (strncmp(pfat_bsbpb->BS_FilSysType, "FAT", 3) != 0) {
98                         G_LABEL_DEBUG(1,
99                             "MSDOSFS: %s: FAT12/16 volume not valid.",
100                             pp->name);
101                         goto error;
102                 }
103                 G_LABEL_DEBUG(1, "MSDOSFS: %s: FAT12/FAT16 volume detected.",
104                     pp->name);
105
106                 /* A volume with no name should have "NO NAME    " as label. */
107                 if (strncmp(pfat_bsbpb->BS_VolLab, LABEL_NO_NAME,
108                     sizeof(pfat_bsbpb->BS_VolLab)) == 0) {
109                         G_LABEL_DEBUG(1,
110                             "MSDOSFS: %s: FAT12/16 volume has no name.",
111                             pp->name);
112                         goto error;
113                 }
114                 strlcpy(label, pfat_bsbpb->BS_VolLab,
115                     MIN(size, sizeof(pfat_bsbpb->BS_VolLab) + 1));
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                         strlcpy(label, pfat32_bsbpb->BS_VolLab,
137                             MIN(size, sizeof(pfat32_bsbpb->BS_VolLab) + 1));
138                         goto endofchecks;
139                 }
140
141                 /*
142                  * If the volume label "NO NAME    " is in the boot sector, the
143                  * label of FAT32 volumes may be stored as a special entry in
144                  * the root directory.
145                  */
146                 fat_FirstDataSector =
147                     UINT16BYTES(pfat32_bsbpb->BPB_RsvdSecCnt) +
148                     (pfat32_bsbpb->BPB_NumFATs *
149                      UINT32BYTES(pfat32_bsbpb->BPB_FATSz32));
150                 fat_BytesPerSector = UINT16BYTES(pfat32_bsbpb->BPB_BytsPerSec);
151
152                 G_LABEL_DEBUG(2,
153                     "MSDOSFS: FAT_FirstDataSector=0x%x, FAT_BytesPerSector=%d",
154                     fat_FirstDataSector, fat_BytesPerSector);
155
156                 for (offset = fat_BytesPerSector * fat_FirstDataSector;;
157                     offset += fat_BytesPerSector) {
158                         sector = (uint8_t *)g_read_data(cp, offset,
159                             fat_BytesPerSector, NULL);
160                         if (sector == NULL)
161                                 goto error;
162
163                         pfat_entry = (FAT_DES *)sector;
164                         do {
165                                 /* No more entries available. */
166                                 if (pfat_entry->DIR_Name[0] == 0) {
167                                         G_LABEL_DEBUG(1, "MSDOSFS: %s: "
168                                             "FAT32 volume has no name.",
169                                             pp->name);
170                                         goto error;
171                                 }
172
173                                 /* Skip empty or long name entries. */
174                                 if (pfat_entry->DIR_Name[0] == 0xe5 ||
175                                     (pfat_entry->DIR_Attr &
176                                      FAT_DES_ATTR_LONG_NAME) ==
177                                     FAT_DES_ATTR_LONG_NAME) {
178                                         continue;
179                                 }
180
181                                 /*
182                                  * The name of the entry is the volume label if
183                                  * ATTR_VOLUME_ID is set.
184                                  */
185                                 if (pfat_entry->DIR_Attr &
186                                     FAT_DES_ATTR_VOLUME_ID) {
187                                         strlcpy(label, pfat_entry->DIR_Name,
188                                             MIN(size,
189                                             sizeof(pfat_entry->DIR_Name) + 1));
190                                         goto endofchecks;
191                                 }
192                         } while((uint8_t *)(++pfat_entry) <
193                             (uint8_t *)(sector + fat_BytesPerSector));
194                         g_free(sector);
195                 }
196         } else {
197                 G_LABEL_DEBUG(1, "MSDOSFS: %s: no FAT volume detected.",
198                     pp->name);
199                 goto error;
200         }
201
202 endofchecks:
203         g_label_rtrim(label, size);
204
205 error:
206         if (sector0 != NULL)
207                 g_free(sector0);
208         if (sector != NULL)
209                 g_free(sector);
210 }
211
212 struct g_label_desc g_label_msdosfs = {
213         .ld_taste = g_label_msdosfs_taste,
214         .ld_dirprefix = "msdosfs/",
215         .ld_enabled = 1
216 };
217
218 G_LABEL_INIT(msdosfs, g_label_msdosfs, "Create device nodes for MSDOSFS volumes");