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