sbin/hammer: Add a trivial wrapper over blockmap_lookup()
[dragonfly.git] / usr.sbin / fstyp / msdosfs.c
blob9e111e03976e60d76c4027e23fcab3f36f00ef79
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.
8 * This software was developed by Edward Tomasz Napierala under sponsorship
9 * from the FreeBSD Foundation.
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.
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.
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
37 #include "fstyp.h"
38 #include "msdosfs.h"
40 #define LABEL_NO_NAME "NO NAME "
42 int
43 fstyp_msdosfs(FILE *fp, char *label, size_t size)
45 FAT_BSBPB *pfat_bsbpb;
46 FAT32_BSBPB *pfat32_bsbpb;
47 FAT_DES *pfat_entry;
48 uint8_t *sector0, *sector;
50 sector0 = NULL;
51 sector = NULL;
53 /* Load 1st sector with boot sector and boot parameter block. */
54 sector0 = (uint8_t *)read_buf(fp, 0, 512);
55 if (sector0 == NULL)
56 return (1);
58 /* Check for the FAT boot sector signature. */
59 if (sector0[510] != 0x55 || sector0[511] != 0xaa) {
60 goto error;
64 * Test if this is really a FAT volume and determine the FAT type.
67 pfat_bsbpb = (FAT_BSBPB *)sector0;
68 pfat32_bsbpb = (FAT32_BSBPB *)sector0;
70 if (UINT16BYTES(pfat_bsbpb->BPB_FATSz16) != 0) {
72 * If the BPB_FATSz16 field is not zero and the string "FAT" is
73 * at the right place, this should be a FAT12 or FAT16 volume.
75 if (strncmp(pfat_bsbpb->BS_FilSysType, "FAT", 3) != 0) {
76 goto error;
79 /* A volume with no name should have "NO NAME " as label. */
80 if (strncmp(pfat_bsbpb->BS_VolLab, LABEL_NO_NAME,
81 sizeof(pfat_bsbpb->BS_VolLab)) == 0) {
82 goto endofchecks;
84 strlcpy(label, pfat_bsbpb->BS_VolLab,
85 MIN(size, sizeof(pfat_bsbpb->BS_VolLab) + 1));
86 } else if (UINT32BYTES(pfat32_bsbpb->BPB_FATSz32) != 0) {
87 uint32_t fat_FirstDataSector, fat_BytesPerSector, offset;
90 * If the BPB_FATSz32 field is not zero and the string "FAT" is
91 * at the right place, this should be a FAT32 volume.
93 if (strncmp(pfat32_bsbpb->BS_FilSysType, "FAT", 3) != 0) {
94 goto error;
98 * If the volume label is not "NO NAME " we're done.
100 if (strncmp(pfat32_bsbpb->BS_VolLab, LABEL_NO_NAME,
101 sizeof(pfat32_bsbpb->BS_VolLab)) != 0) {
102 strlcpy(label, pfat32_bsbpb->BS_VolLab,
103 MIN(size, sizeof(pfat32_bsbpb->BS_VolLab) + 1));
104 goto endofchecks;
108 * If the volume label "NO NAME " is in the boot sector, the
109 * label of FAT32 volumes may be stored as a special entry in
110 * the root directory.
112 fat_FirstDataSector =
113 UINT16BYTES(pfat32_bsbpb->BPB_RsvdSecCnt) +
114 (pfat32_bsbpb->BPB_NumFATs *
115 UINT32BYTES(pfat32_bsbpb->BPB_FATSz32));
116 fat_BytesPerSector = UINT16BYTES(pfat32_bsbpb->BPB_BytsPerSec);
118 // fat_FirstDataSector, fat_BytesPerSector);
120 for (offset = fat_BytesPerSector * fat_FirstDataSector;;
121 offset += fat_BytesPerSector) {
122 sector = (uint8_t *)read_buf(fp, offset, fat_BytesPerSector);
123 if (sector == NULL)
124 goto error;
126 pfat_entry = (FAT_DES *)sector;
127 do {
128 /* No more entries available. */
129 if (pfat_entry->DIR_Name[0] == 0) {
130 goto endofchecks;
133 /* Skip empty or long name entries. */
134 if (pfat_entry->DIR_Name[0] == 0xe5 ||
135 (pfat_entry->DIR_Attr &
136 FAT_DES_ATTR_LONG_NAME) ==
137 FAT_DES_ATTR_LONG_NAME) {
138 continue;
142 * The name of the entry is the volume label if
143 * ATTR_VOLUME_ID is set.
145 if (pfat_entry->DIR_Attr &
146 FAT_DES_ATTR_VOLUME_ID) {
147 strlcpy(label, pfat_entry->DIR_Name,
148 MIN(size,
149 sizeof(pfat_entry->DIR_Name) + 1));
150 goto endofchecks;
152 } while((uint8_t *)(++pfat_entry) <
153 (uint8_t *)(sector + fat_BytesPerSector));
154 free(sector);
156 } else {
157 goto error;
160 endofchecks:
161 rtrim(label, size);
163 free(sector0);
164 free(sector);
166 return (0);
168 error:
169 free(sector0);
170 free(sector);
172 return (1);