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