1 /*- 2 * Copyright (c) 2016 The DragonFly Project 3 * Copyright (c) 2004 Pawel Jakub Dawidek <pjd@FreeBSD.org> 4 * Copyright (c) 2006 Tobias Reifenberger 5 * Copyright (c) 2014 The FreeBSD Foundation 6 * All rights reserved. 7 * 8 * This software was developed by Edward Tomasz Napierala under sponsorship 9 * from the FreeBSD Foundation. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #include <sys/param.h> 34 #include <stdio.h> 35 #include <stdlib.h> 36 #include <string.h> 37 38 #include "fstyp.h" 39 #include "msdosfs.h" 40 41 #define LABEL_NO_NAME "NO NAME " 42 43 int 44 fstyp_msdosfs(FILE *fp, char *label, size_t size) 45 { 46 FAT_BSBPB *pfat_bsbpb; 47 FAT32_BSBPB *pfat32_bsbpb; 48 FAT_DES *pfat_entry; 49 uint8_t *sector0, *sector; 50 51 sector0 = NULL; 52 sector = NULL; 53 54 /* Load 1st sector with boot sector and boot parameter block. */ 55 sector0 = (uint8_t *)read_buf(fp, 0, 512); 56 if (sector0 == NULL) 57 return (1); 58 59 /* Check for the FAT boot sector signature. */ 60 if (sector0[510] != 0x55 || sector0[511] != 0xaa) { 61 goto error; 62 } 63 64 /* 65 * Test if this is really a FAT volume and determine the FAT type. 66 */ 67 68 pfat_bsbpb = (FAT_BSBPB *)sector0; 69 pfat32_bsbpb = (FAT32_BSBPB *)sector0; 70 71 if (UINT16BYTES(pfat_bsbpb->BPB_FATSz16) != 0) { 72 /* 73 * If the BPB_FATSz16 field is not zero and the string "FAT" is 74 * at the right place, this should be a FAT12 or FAT16 volume. 75 */ 76 if (strncmp(pfat_bsbpb->BS_FilSysType, "FAT", 3) != 0) { 77 goto error; 78 } 79 80 /* A volume with no name should have "NO NAME " as label. */ 81 if (strncmp(pfat_bsbpb->BS_VolLab, LABEL_NO_NAME, 82 sizeof(pfat_bsbpb->BS_VolLab)) == 0) { 83 goto endofchecks; 84 } 85 strlcpy(label, pfat_bsbpb->BS_VolLab, 86 MIN(size, sizeof(pfat_bsbpb->BS_VolLab) + 1)); 87 } else if (UINT32BYTES(pfat32_bsbpb->BPB_FATSz32) != 0) { 88 uint32_t fat_FirstDataSector, fat_BytesPerSector, offset; 89 90 /* 91 * If the BPB_FATSz32 field is not zero and the string "FAT" is 92 * at the right place, this should be a FAT32 volume. 93 */ 94 if (strncmp(pfat32_bsbpb->BS_FilSysType, "FAT", 3) != 0) { 95 goto error; 96 } 97 98 /* 99 * If the volume label is not "NO NAME " we're done. 100 */ 101 if (strncmp(pfat32_bsbpb->BS_VolLab, LABEL_NO_NAME, 102 sizeof(pfat32_bsbpb->BS_VolLab)) != 0) { 103 strlcpy(label, pfat32_bsbpb->BS_VolLab, 104 MIN(size, sizeof(pfat32_bsbpb->BS_VolLab) + 1)); 105 goto endofchecks; 106 } 107 108 /* 109 * If the volume label "NO NAME " is in the boot sector, the 110 * label of FAT32 volumes may be stored as a special entry in 111 * the root directory. 112 */ 113 fat_FirstDataSector = 114 UINT16BYTES(pfat32_bsbpb->BPB_RsvdSecCnt) + 115 (pfat32_bsbpb->BPB_NumFATs * 116 UINT32BYTES(pfat32_bsbpb->BPB_FATSz32)); 117 fat_BytesPerSector = UINT16BYTES(pfat32_bsbpb->BPB_BytsPerSec); 118 119 // fat_FirstDataSector, fat_BytesPerSector); 120 121 for (offset = fat_BytesPerSector * fat_FirstDataSector;; 122 offset += fat_BytesPerSector) { 123 sector = (uint8_t *)read_buf(fp, offset, fat_BytesPerSector); 124 if (sector == NULL) 125 goto error; 126 127 pfat_entry = (FAT_DES *)sector; 128 do { 129 /* No more entries available. */ 130 if (pfat_entry->DIR_Name[0] == 0) { 131 goto endofchecks; 132 } 133 134 /* Skip empty or long name entries. */ 135 if (pfat_entry->DIR_Name[0] == 0xe5 || 136 (pfat_entry->DIR_Attr & 137 FAT_DES_ATTR_LONG_NAME) == 138 FAT_DES_ATTR_LONG_NAME) { 139 continue; 140 } 141 142 /* 143 * The name of the entry is the volume label if 144 * ATTR_VOLUME_ID is set. 145 */ 146 if (pfat_entry->DIR_Attr & 147 FAT_DES_ATTR_VOLUME_ID) { 148 strlcpy(label, pfat_entry->DIR_Name, 149 MIN(size, 150 sizeof(pfat_entry->DIR_Name) + 1)); 151 goto endofchecks; 152 } 153 } while((uint8_t *)(++pfat_entry) < 154 (uint8_t *)(sector + fat_BytesPerSector)); 155 free(sector); 156 } 157 } else { 158 goto error; 159 } 160 161 endofchecks: 162 rtrim(label, size); 163 164 free(sector0); 165 free(sector); 166 167 return (0); 168 169 error: 170 free(sector0); 171 free(sector); 172 173 return (1); 174 } 175