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