xref: /freebsd-src/usr.sbin/fstyp/msdosfs.c (revision 1d386b48a555f61cb7325543adbbb5c3f3407a66)
1be3a49eeSEdward Tomasz Napierala /*-
2be3a49eeSEdward Tomasz Napierala  * Copyright (c) 2004 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3be3a49eeSEdward Tomasz Napierala  * Copyright (c) 2006 Tobias Reifenberger
4be3a49eeSEdward Tomasz Napierala  * Copyright (c) 2014 The FreeBSD Foundation
5be3a49eeSEdward Tomasz Napierala  * All rights reserved.
6be3a49eeSEdward Tomasz Napierala  *
7be3a49eeSEdward Tomasz Napierala  * This software was developed by Edward Tomasz Napierala under sponsorship
8be3a49eeSEdward Tomasz Napierala  * from the FreeBSD Foundation.
9be3a49eeSEdward Tomasz Napierala  *
10be3a49eeSEdward Tomasz Napierala  * Redistribution and use in source and binary forms, with or without
11be3a49eeSEdward Tomasz Napierala  * modification, are permitted provided that the following conditions
12be3a49eeSEdward Tomasz Napierala  * are met:
13be3a49eeSEdward Tomasz Napierala  * 1. Redistributions of source code must retain the above copyright
14be3a49eeSEdward Tomasz Napierala  *    notice, this list of conditions and the following disclaimer.
15be3a49eeSEdward Tomasz Napierala  * 2. Redistributions in binary form must reproduce the above copyright
16be3a49eeSEdward Tomasz Napierala  *    notice, this list of conditions and the following disclaimer in the
17be3a49eeSEdward Tomasz Napierala  *    documentation and/or other materials provided with the distribution.
18be3a49eeSEdward Tomasz Napierala  *
19be3a49eeSEdward Tomasz Napierala  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
20be3a49eeSEdward Tomasz Napierala  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21be3a49eeSEdward Tomasz Napierala  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22be3a49eeSEdward Tomasz Napierala  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
23be3a49eeSEdward Tomasz Napierala  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24be3a49eeSEdward Tomasz Napierala  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25be3a49eeSEdward Tomasz Napierala  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26be3a49eeSEdward Tomasz Napierala  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27be3a49eeSEdward Tomasz Napierala  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28be3a49eeSEdward Tomasz Napierala  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29be3a49eeSEdward Tomasz Napierala  * SUCH DAMAGE.
30be3a49eeSEdward Tomasz Napierala  */
31be3a49eeSEdward Tomasz Napierala 
32be3a49eeSEdward Tomasz Napierala #include <sys/cdefs.h>
33be3a49eeSEdward Tomasz Napierala #include <stdio.h>
34be3a49eeSEdward Tomasz Napierala #include <stdlib.h>
35be3a49eeSEdward Tomasz Napierala #include <string.h>
36be3a49eeSEdward Tomasz Napierala 
37be3a49eeSEdward Tomasz Napierala #include "fstyp.h"
38be3a49eeSEdward Tomasz Napierala #include "msdosfs.h"
39be3a49eeSEdward Tomasz Napierala 
40be3a49eeSEdward Tomasz Napierala #define LABEL_NO_NAME		"NO NAME    "
41be3a49eeSEdward Tomasz Napierala 
42868c1b84SEd Maste /*
43868c1b84SEd Maste  * XXX the signature 0x55 0xAA as the last two bytes of 512 is not required
44868c1b84SEd Maste  * by specifications, but was historically required by fstyp.  This check
45868c1b84SEd Maste  * should be removed, with a more comprehensive BPB validation instead.
46868c1b84SEd Maste  */
47868c1b84SEd Maste static bool
check_signature(uint8_t sector0[512])48868c1b84SEd Maste check_signature(uint8_t sector0[512])
49868c1b84SEd Maste {
50868c1b84SEd Maste 	/* Check for the FAT boot sector signature. */
51868c1b84SEd Maste 	if (sector0[510] == 0x55 && sector0[511] == 0xaa)
52868c1b84SEd Maste 		return (true);
53*27c2f016SEd Maste 	/* Special case for Raspberry Pi Pico bootloader. */
54868c1b84SEd Maste 	if (sector0[510] == 0 && sector0[511] == 0 &&
55868c1b84SEd Maste 	    sector0[0] == 0xeb && sector0[1] == 0x3c && sector0[2] == 0x90)
56868c1b84SEd Maste 		return (true);
57868c1b84SEd Maste 	return (false);
58868c1b84SEd Maste }
59868c1b84SEd Maste 
60be3a49eeSEdward Tomasz Napierala int
fstyp_msdosfs(FILE * fp,char * label,size_t size)61be3a49eeSEdward Tomasz Napierala fstyp_msdosfs(FILE *fp, char *label, size_t size)
62be3a49eeSEdward Tomasz Napierala {
63be3a49eeSEdward Tomasz Napierala 	FAT_BSBPB *pfat_bsbpb;
64be3a49eeSEdward Tomasz Napierala 	FAT32_BSBPB *pfat32_bsbpb;
65be3a49eeSEdward Tomasz Napierala 	FAT_DES *pfat_entry;
66be3a49eeSEdward Tomasz Napierala 	uint8_t *sector0, *sector;
6734fb1c13SJessica Clarke 	size_t copysize;
68be3a49eeSEdward Tomasz Napierala 
69be3a49eeSEdward Tomasz Napierala 	sector0 = NULL;
70be3a49eeSEdward Tomasz Napierala 	sector = NULL;
71be3a49eeSEdward Tomasz Napierala 
72be3a49eeSEdward Tomasz Napierala 	/* Load 1st sector with boot sector and boot parameter block. */
73be3a49eeSEdward Tomasz Napierala 	sector0 = (uint8_t *)read_buf(fp, 0, 512);
74be3a49eeSEdward Tomasz Napierala 	if (sector0 == NULL)
75be3a49eeSEdward Tomasz Napierala 		return (1);
76be3a49eeSEdward Tomasz Napierala 
77868c1b84SEd Maste 	if (!check_signature(sector0)) {
78be3a49eeSEdward Tomasz Napierala 		goto error;
79be3a49eeSEdward Tomasz Napierala 	}
80be3a49eeSEdward Tomasz Napierala 
81be3a49eeSEdward Tomasz Napierala 	/*
82be3a49eeSEdward Tomasz Napierala 	 * Test if this is really a FAT volume and determine the FAT type.
83be3a49eeSEdward Tomasz Napierala 	 */
84be3a49eeSEdward Tomasz Napierala 
85be3a49eeSEdward Tomasz Napierala 	pfat_bsbpb = (FAT_BSBPB *)sector0;
86be3a49eeSEdward Tomasz Napierala 	pfat32_bsbpb = (FAT32_BSBPB *)sector0;
87be3a49eeSEdward Tomasz Napierala 
88be3a49eeSEdward Tomasz Napierala 	if (UINT16BYTES(pfat_bsbpb->BPB_FATSz16) != 0) {
89be3a49eeSEdward Tomasz Napierala 		/*
90be3a49eeSEdward Tomasz Napierala 		 * If the BPB_FATSz16 field is not zero and the string "FAT" is
91be3a49eeSEdward Tomasz Napierala 		 * at the right place, this should be a FAT12 or FAT16 volume.
92be3a49eeSEdward Tomasz Napierala 		 */
93be3a49eeSEdward Tomasz Napierala 		if (strncmp(pfat_bsbpb->BS_FilSysType, "FAT", 3) != 0) {
94be3a49eeSEdward Tomasz Napierala 			goto error;
95be3a49eeSEdward Tomasz Napierala 		}
96be3a49eeSEdward Tomasz Napierala 
97be3a49eeSEdward Tomasz Napierala 		/* A volume with no name should have "NO NAME    " as label. */
98be3a49eeSEdward Tomasz Napierala 		if (strncmp(pfat_bsbpb->BS_VolLab, LABEL_NO_NAME,
99be3a49eeSEdward Tomasz Napierala 		    sizeof(pfat_bsbpb->BS_VolLab)) == 0) {
100be3a49eeSEdward Tomasz Napierala 			goto endofchecks;
101be3a49eeSEdward Tomasz Napierala 		}
10234fb1c13SJessica Clarke 		copysize = MIN(size - 1, sizeof(pfat_bsbpb->BS_VolLab));
10334fb1c13SJessica Clarke 		memcpy(label, pfat_bsbpb->BS_VolLab, copysize);
10434fb1c13SJessica Clarke 		label[copysize] = '\0';
105be3a49eeSEdward Tomasz Napierala 	} else if (UINT32BYTES(pfat32_bsbpb->BPB_FATSz32) != 0) {
106be3a49eeSEdward Tomasz Napierala 		uint32_t fat_FirstDataSector, fat_BytesPerSector, offset;
107be3a49eeSEdward Tomasz Napierala 
108be3a49eeSEdward Tomasz Napierala 		/*
109be3a49eeSEdward Tomasz Napierala 		 * If the BPB_FATSz32 field is not zero and the string "FAT" is
110be3a49eeSEdward Tomasz Napierala 		 * at the right place, this should be a FAT32 volume.
111be3a49eeSEdward Tomasz Napierala 		 */
112be3a49eeSEdward Tomasz Napierala 		if (strncmp(pfat32_bsbpb->BS_FilSysType, "FAT", 3) != 0) {
113be3a49eeSEdward Tomasz Napierala 			goto error;
114be3a49eeSEdward Tomasz Napierala 		}
115be3a49eeSEdward Tomasz Napierala 
116be3a49eeSEdward Tomasz Napierala 		/*
117be3a49eeSEdward Tomasz Napierala 		 * If the volume label is not "NO NAME    " we're done.
118be3a49eeSEdward Tomasz Napierala 		 */
119be3a49eeSEdward Tomasz Napierala 		if (strncmp(pfat32_bsbpb->BS_VolLab, LABEL_NO_NAME,
120be3a49eeSEdward Tomasz Napierala 		    sizeof(pfat32_bsbpb->BS_VolLab)) != 0) {
12134fb1c13SJessica Clarke 			copysize = MIN(size - 1,
12263d24336SJessica Clarke 			    sizeof(pfat32_bsbpb->BS_VolLab));
12334fb1c13SJessica Clarke 			memcpy(label, pfat32_bsbpb->BS_VolLab, copysize);
12434fb1c13SJessica Clarke 			label[copysize] = '\0';
125be3a49eeSEdward Tomasz Napierala 			goto endofchecks;
126be3a49eeSEdward Tomasz Napierala 		}
127be3a49eeSEdward Tomasz Napierala 
128be3a49eeSEdward Tomasz Napierala 		/*
129be3a49eeSEdward Tomasz Napierala 		 * If the volume label "NO NAME    " is in the boot sector, the
130be3a49eeSEdward Tomasz Napierala 		 * label of FAT32 volumes may be stored as a special entry in
131be3a49eeSEdward Tomasz Napierala 		 * the root directory.
132be3a49eeSEdward Tomasz Napierala 		 */
133be3a49eeSEdward Tomasz Napierala 		fat_FirstDataSector =
134be3a49eeSEdward Tomasz Napierala 		    UINT16BYTES(pfat32_bsbpb->BPB_RsvdSecCnt) +
135be3a49eeSEdward Tomasz Napierala 		    (pfat32_bsbpb->BPB_NumFATs *
136be3a49eeSEdward Tomasz Napierala 		     UINT32BYTES(pfat32_bsbpb->BPB_FATSz32));
137be3a49eeSEdward Tomasz Napierala 		fat_BytesPerSector = UINT16BYTES(pfat32_bsbpb->BPB_BytsPerSec);
138be3a49eeSEdward Tomasz Napierala 
139be3a49eeSEdward Tomasz Napierala 		//    fat_FirstDataSector, fat_BytesPerSector);
140be3a49eeSEdward Tomasz Napierala 
141be3a49eeSEdward Tomasz Napierala 		for (offset = fat_BytesPerSector * fat_FirstDataSector;;
142be3a49eeSEdward Tomasz Napierala 		    offset += fat_BytesPerSector) {
143be3a49eeSEdward Tomasz Napierala 			sector = (uint8_t *)read_buf(fp, offset, fat_BytesPerSector);
144be3a49eeSEdward Tomasz Napierala 			if (sector == NULL)
145be3a49eeSEdward Tomasz Napierala 				goto error;
146be3a49eeSEdward Tomasz Napierala 
147be3a49eeSEdward Tomasz Napierala 			pfat_entry = (FAT_DES *)sector;
148be3a49eeSEdward Tomasz Napierala 			do {
149be3a49eeSEdward Tomasz Napierala 				/* No more entries available. */
150be3a49eeSEdward Tomasz Napierala 				if (pfat_entry->DIR_Name[0] == 0) {
151be3a49eeSEdward Tomasz Napierala 					goto endofchecks;
152be3a49eeSEdward Tomasz Napierala 				}
153be3a49eeSEdward Tomasz Napierala 
154be3a49eeSEdward Tomasz Napierala 				/* Skip empty or long name entries. */
155be3a49eeSEdward Tomasz Napierala 				if (pfat_entry->DIR_Name[0] == 0xe5 ||
156be3a49eeSEdward Tomasz Napierala 				    (pfat_entry->DIR_Attr &
157be3a49eeSEdward Tomasz Napierala 				     FAT_DES_ATTR_LONG_NAME) ==
158be3a49eeSEdward Tomasz Napierala 				    FAT_DES_ATTR_LONG_NAME) {
159be3a49eeSEdward Tomasz Napierala 					continue;
160be3a49eeSEdward Tomasz Napierala 				}
161be3a49eeSEdward Tomasz Napierala 
162be3a49eeSEdward Tomasz Napierala 				/*
163be3a49eeSEdward Tomasz Napierala 				 * The name of the entry is the volume label if
164be3a49eeSEdward Tomasz Napierala 				 * ATTR_VOLUME_ID is set.
165be3a49eeSEdward Tomasz Napierala 				 */
166be3a49eeSEdward Tomasz Napierala 				if (pfat_entry->DIR_Attr &
167be3a49eeSEdward Tomasz Napierala 				    FAT_DES_ATTR_VOLUME_ID) {
16834fb1c13SJessica Clarke 					copysize = MIN(size - 1,
16934fb1c13SJessica Clarke 					    sizeof(pfat_entry->DIR_Name));
17034fb1c13SJessica Clarke 					memcpy(label, pfat_entry->DIR_Name,
17134fb1c13SJessica Clarke 					    copysize);
17234fb1c13SJessica Clarke 					label[copysize] = '\0';
173be3a49eeSEdward Tomasz Napierala 					goto endofchecks;
174be3a49eeSEdward Tomasz Napierala 				}
175be3a49eeSEdward Tomasz Napierala 			} while((uint8_t *)(++pfat_entry) <
176be3a49eeSEdward Tomasz Napierala 			    (uint8_t *)(sector + fat_BytesPerSector));
177be3a49eeSEdward Tomasz Napierala 			free(sector);
178be3a49eeSEdward Tomasz Napierala 		}
179be3a49eeSEdward Tomasz Napierala 	} else {
180be3a49eeSEdward Tomasz Napierala 		goto error;
181be3a49eeSEdward Tomasz Napierala 	}
182be3a49eeSEdward Tomasz Napierala 
183be3a49eeSEdward Tomasz Napierala endofchecks:
184628b7128SEdward Tomasz Napierala 	rtrim(label, size);
185be3a49eeSEdward Tomasz Napierala 
186be3a49eeSEdward Tomasz Napierala 	free(sector0);
187be3a49eeSEdward Tomasz Napierala 	free(sector);
188be3a49eeSEdward Tomasz Napierala 
189be3a49eeSEdward Tomasz Napierala 	return (0);
190be3a49eeSEdward Tomasz Napierala 
191be3a49eeSEdward Tomasz Napierala error:
192be3a49eeSEdward Tomasz Napierala 	free(sector0);
193be3a49eeSEdward Tomasz Napierala 	free(sector);
194be3a49eeSEdward Tomasz Napierala 
195be3a49eeSEdward Tomasz Napierala 	return (1);
196be3a49eeSEdward Tomasz Napierala }
197