g_label_msdosfs.c revision 256281
144743Smarkm/*- 244743Smarkm * Copyright (c) 2004 Pawel Jakub Dawidek <pjd@FreeBSD.org> 344743Smarkm * Copyright (c) 2006 Tobias Reifenberger 444743Smarkm * All rights reserved. 544743Smarkm * 656977Sshin * Redistribution and use in source and binary forms, with or without 756977Sshin * modification, are permitted provided that the following conditions 844743Smarkm * are met: 944743Smarkm * 1. Redistributions of source code must retain the above copyright 1044743Smarkm * notice, this list of conditions and the following disclaimer. 1144743Smarkm * 2. Redistributions in binary form must reproduce the above copyright 1244743Smarkm * notice, this list of conditions and the following disclaimer in the 1344743Smarkm * documentation and/or other materials provided with the distribution. 1444743Smarkm * 1544743Smarkm * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 1656977Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1756977Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1856977Sshin * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 1944743Smarkm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2044743Smarkm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2144743Smarkm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2244743Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2344743Smarkm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2444743Smarkm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2544743Smarkm * SUCH DAMAGE. 2644743Smarkm */ 2744743Smarkm 2844743Smarkm#include <sys/cdefs.h> 2944743Smarkm__FBSDID("$FreeBSD: stable/10/sys/geom/label/g_label_msdosfs.c 199875 2009-11-28 11:57:43Z trasz $"); 3044743Smarkm 3144743Smarkm#include <sys/param.h> 3244743Smarkm#include <sys/systm.h> 3344743Smarkm#include <sys/kernel.h> 3444743Smarkm#include <sys/malloc.h> 3544743Smarkm 3644743Smarkm#include <geom/geom.h> 37210386Srpaulo#include <geom/label/g_label.h> 3844743Smarkm#include <geom/label/g_label_msdosfs.h> 3944743Smarkm 4044743Smarkm#define G_LABEL_MSDOSFS_DIR "msdosfs" 4144743Smarkm#define LABEL_NO_NAME "NO NAME " 4244743Smarkm 4344743Smarkmstatic void 4444743Smarkmg_label_msdosfs_taste(struct g_consumer *cp, char *label, size_t size) 4544743Smarkm{ 4644743Smarkm struct g_provider *pp; 4744743Smarkm FAT_BSBPB *pfat_bsbpb; 4844743Smarkm FAT32_BSBPB *pfat32_bsbpb; 4944743Smarkm FAT_DES *pfat_entry; 5056977Sshin uint8_t *sector0, *sector; 5156977Sshin uint32_t i; 5256977Sshin 5344743Smarkm g_topology_assert_not(); 5456977Sshin pp = cp->provider; 5556977Sshin sector0 = NULL; 5656977Sshin sector = NULL; 5756977Sshin bzero(label, size); 5856977Sshin 5956977Sshin /* Check if the sector size of the medium is a valid FAT sector size. */ 6056977Sshin switch(pp->sectorsize) { 6156977Sshin case 512: 6256977Sshin case 1024: 6356977Sshin case 2048: 6456977Sshin case 4096: 6556977Sshin break; 6656977Sshin default: 6744743Smarkm G_LABEL_DEBUG(1, "MSDOSFS: %s: sector size %d not compatible.", 6844743Smarkm pp->name, pp->sectorsize); 6944743Smarkm return; 7044743Smarkm } 7144743Smarkm 7244743Smarkm /* Load 1st sector with boot sector and boot parameter block. */ 7344743Smarkm sector0 = (uint8_t *)g_read_data(cp, 0, pp->sectorsize, NULL); 7444743Smarkm if (sector0 == NULL) 7544743Smarkm return; 7644743Smarkm 7744743Smarkm /* Check for the FAT boot sector signature. */ 7844743Smarkm if (sector0[510] != 0x55 || sector0[511] != 0xaa) { 7944743Smarkm G_LABEL_DEBUG(1, "MSDOSFS: %s: no FAT signature found.", 8044743Smarkm pp->name); 8144743Smarkm goto error; 8244743Smarkm } 8344743Smarkm 8444743Smarkm 8544743Smarkm /* 8644743Smarkm * Test if this is really a FAT volume and determine the FAT type. 8744743Smarkm */ 8844743Smarkm 8944743Smarkm pfat_bsbpb = (FAT_BSBPB *)sector0; 9044743Smarkm pfat32_bsbpb = (FAT32_BSBPB *)sector0; 9144743Smarkm 9244743Smarkm if (UINT16BYTES(pfat_bsbpb->BPB_FATSz16) != 0) { 9344743Smarkm /* 9444743Smarkm * If the BPB_FATSz16 field is not zero and the string "FAT" is 9544743Smarkm * at the right place, this should be a FAT12 or FAT16 volume. 9644743Smarkm */ 9744743Smarkm if (strncmp(pfat_bsbpb->BS_FilSysType, "FAT", 3) != 0) { 9844743Smarkm G_LABEL_DEBUG(1, 9944743Smarkm "MSDOSFS: %s: FAT12/16 volume not valid.", 10044743Smarkm pp->name); 10144743Smarkm goto error; 10244743Smarkm } 10344743Smarkm G_LABEL_DEBUG(1, "MSDOSFS: %s: FAT12/FAT16 volume detected.", 10444743Smarkm pp->name); 10544743Smarkm 10644743Smarkm /* A volume with no name should have "NO NAME " as label. */ 10744743Smarkm if (strncmp(pfat_bsbpb->BS_VolLab, LABEL_NO_NAME, 10844743Smarkm sizeof(pfat_bsbpb->BS_VolLab)) == 0) { 10944743Smarkm G_LABEL_DEBUG(1, 11044743Smarkm "MSDOSFS: %s: FAT12/16 volume has no name.", 11144743Smarkm pp->name); 11244743Smarkm goto error; 11344743Smarkm } 11444743Smarkm strlcpy(label, pfat_bsbpb->BS_VolLab, 11544743Smarkm MIN(size, sizeof(pfat_bsbpb->BS_VolLab) + 1)); 11644743Smarkm } else if (UINT32BYTES(pfat32_bsbpb->BPB_FATSz32) != 0) { 11744743Smarkm uint32_t fat_FirstDataSector, fat_BytesPerSector, offset; 11844743Smarkm 11944743Smarkm /* 12044743Smarkm * If the BPB_FATSz32 field is not zero and the string "FAT" is 12144743Smarkm * at the right place, this should be a FAT32 volume. 12244743Smarkm */ 12344743Smarkm if (strncmp(pfat32_bsbpb->BS_FilSysType, "FAT", 3) != 0) { 12444743Smarkm G_LABEL_DEBUG(1, "MSDOSFS: %s: FAT32 volume not valid.", 12544743Smarkm pp->name); 12644743Smarkm goto error; 12744743Smarkm } 12844743Smarkm G_LABEL_DEBUG(1, "MSDOSFS: %s: FAT32 volume detected.", 12944743Smarkm pp->name); 13044743Smarkm 13144743Smarkm /* 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 202endofchecks: 203 for (i = size - 1; i > 0; i--) { 204 if (label[i] == '\0') 205 continue; 206 else if (label[i] == ' ') 207 label[i] = '\0'; 208 else 209 break; 210 } 211 212error: 213 if (sector0 != NULL) 214 g_free(sector0); 215 if (sector != NULL) 216 g_free(sector); 217} 218 219struct g_label_desc g_label_msdosfs = { 220 .ld_taste = g_label_msdosfs_taste, 221 .ld_dir = G_LABEL_MSDOSFS_DIR, 222 .ld_enabled = 1 223}; 224 225G_LABEL_INIT(msdosfs, g_label_msdosfs, "Create device nodes for MSDOSFS volumes"); 226