arm64 libc: hide .cerror, .curbrk, .minbrk for WITHOUT_SYMVER
[freebsd-src.git] / usr.sbin / fstyp / msdosfs.c
blob3d86802f6a2e0f27780955c139337e185acf34ca
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.
7 * This software was developed by Edward Tomasz Napierala under sponsorship
8 * from the FreeBSD Foundation.
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.
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.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
39 #include "fstyp.h"
40 #include "msdosfs.h"
42 #define LABEL_NO_NAME "NO NAME "
44 int
45 fstyp_msdosfs(FILE *fp, char *label, size_t size)
47 FAT_BSBPB *pfat_bsbpb;
48 FAT32_BSBPB *pfat32_bsbpb;
49 FAT_DES *pfat_entry;
50 uint8_t *sector0, *sector;
52 sector0 = NULL;
53 sector = NULL;
55 /* Load 1st sector with boot sector and boot parameter block. */
56 sector0 = (uint8_t *)read_buf(fp, 0, 512);
57 if (sector0 == NULL)
58 return (1);
60 /* Check for the FAT boot sector signature. */
61 if (sector0[510] != 0x55 || sector0[511] != 0xaa) {
62 goto error;
66 * Test if this is really a FAT volume and determine the FAT type.
69 pfat_bsbpb = (FAT_BSBPB *)sector0;
70 pfat32_bsbpb = (FAT32_BSBPB *)sector0;
72 if (UINT16BYTES(pfat_bsbpb->BPB_FATSz16) != 0) {
74 * If the BPB_FATSz16 field is not zero and the string "FAT" is
75 * at the right place, this should be a FAT12 or FAT16 volume.
77 if (strncmp(pfat_bsbpb->BS_FilSysType, "FAT", 3) != 0) {
78 goto error;
81 /* A volume with no name should have "NO NAME " as label. */
82 if (strncmp(pfat_bsbpb->BS_VolLab, LABEL_NO_NAME,
83 sizeof(pfat_bsbpb->BS_VolLab)) == 0) {
84 goto endofchecks;
86 strlcpy(label, pfat_bsbpb->BS_VolLab,
87 MIN(size, sizeof(pfat_bsbpb->BS_VolLab) + 1));
88 } else if (UINT32BYTES(pfat32_bsbpb->BPB_FATSz32) != 0) {
89 uint32_t fat_FirstDataSector, fat_BytesPerSector, offset;
92 * If the BPB_FATSz32 field is not zero and the string "FAT" is
93 * at the right place, this should be a FAT32 volume.
95 if (strncmp(pfat32_bsbpb->BS_FilSysType, "FAT", 3) != 0) {
96 goto error;
100 * If the volume label is not "NO NAME " we're done.
102 if (strncmp(pfat32_bsbpb->BS_VolLab, LABEL_NO_NAME,
103 sizeof(pfat32_bsbpb->BS_VolLab)) != 0) {
104 strlcpy(label, pfat32_bsbpb->BS_VolLab,
105 MIN(size, sizeof(pfat32_bsbpb->BS_VolLab) + 1));
106 goto endofchecks;
110 * If the volume label "NO NAME " is in the boot sector, the
111 * label of FAT32 volumes may be stored as a special entry in
112 * the root directory.
114 fat_FirstDataSector =
115 UINT16BYTES(pfat32_bsbpb->BPB_RsvdSecCnt) +
116 (pfat32_bsbpb->BPB_NumFATs *
117 UINT32BYTES(pfat32_bsbpb->BPB_FATSz32));
118 fat_BytesPerSector = UINT16BYTES(pfat32_bsbpb->BPB_BytsPerSec);
120 // fat_FirstDataSector, fat_BytesPerSector);
122 for (offset = fat_BytesPerSector * fat_FirstDataSector;;
123 offset += fat_BytesPerSector) {
124 sector = (uint8_t *)read_buf(fp, offset, fat_BytesPerSector);
125 if (sector == NULL)
126 goto error;
128 pfat_entry = (FAT_DES *)sector;
129 do {
130 /* No more entries available. */
131 if (pfat_entry->DIR_Name[0] == 0) {
132 goto endofchecks;
135 /* Skip empty or long name entries. */
136 if (pfat_entry->DIR_Name[0] == 0xe5 ||
137 (pfat_entry->DIR_Attr &
138 FAT_DES_ATTR_LONG_NAME) ==
139 FAT_DES_ATTR_LONG_NAME) {
140 continue;
144 * The name of the entry is the volume label if
145 * ATTR_VOLUME_ID is set.
147 if (pfat_entry->DIR_Attr &
148 FAT_DES_ATTR_VOLUME_ID) {
149 strlcpy(label, pfat_entry->DIR_Name,
150 MIN(size,
151 sizeof(pfat_entry->DIR_Name) + 1));
152 goto endofchecks;
154 } while((uint8_t *)(++pfat_entry) <
155 (uint8_t *)(sector + fat_BytesPerSector));
156 free(sector);
158 } else {
159 goto error;
162 endofchecks:
163 rtrim(label, size);
165 free(sector0);
166 free(sector);
168 return (0);
170 error:
171 free(sector0);
172 free(sector);
174 return (1);