xref: /netbsd-src/usr.sbin/fstyp/msdosfs.h (revision b985414b8f8688f3bfa6718c583260b26b0bf1d1)
1*b985414bSchristos /*	$NetBSD: msdosfs.h,v 1.1 2018/01/09 03:31:15 christos Exp $	*/
2*b985414bSchristos 
3*b985414bSchristos /*-
4*b985414bSchristos  * Copyright (c) 2017 The NetBSD Foundation, Inc.
5*b985414bSchristos  * Copyright (c) 2016 The DragonFly Project
6*b985414bSchristos  * Copyright (c) 2006 Tobias Reifenberger
7*b985414bSchristos  * All rights reserved.
8*b985414bSchristos  *
9*b985414bSchristos  * This code is derived from software contributed to The NetBSD Foundation
10*b985414bSchristos  * by Tomohiro Kusumi <kusumi.tomohiro@gmail.com>.
11*b985414bSchristos  *
12*b985414bSchristos  * Redistribution and use in source and binary forms, with or without
13*b985414bSchristos  * modification, are permitted provided that the following conditions
14*b985414bSchristos  * are met:
15*b985414bSchristos  * 1. Redistributions of source code must retain the above copyright
16*b985414bSchristos  *    notice, this list of conditions and the following disclaimer.
17*b985414bSchristos  * 2. Redistributions in binary form must reproduce the above copyright
18*b985414bSchristos  *    notice, this list of conditions and the following disclaimer in the
19*b985414bSchristos  *    documentation and/or other materials provided with the distribution.
20*b985414bSchristos  *
21*b985414bSchristos  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
22*b985414bSchristos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23*b985414bSchristos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24*b985414bSchristos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
25*b985414bSchristos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26*b985414bSchristos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27*b985414bSchristos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28*b985414bSchristos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29*b985414bSchristos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30*b985414bSchristos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31*b985414bSchristos  * SUCH DAMAGE.
32*b985414bSchristos  *
33*b985414bSchristos  * $FreeBSD$
34*b985414bSchristos  */
35*b985414bSchristos 
36*b985414bSchristos #include <sys/types.h>
37*b985414bSchristos 
38*b985414bSchristos /*
39*b985414bSchristos  * Conversion macros for little endian encoded unsigned integers
40*b985414bSchristos  * in byte streams to the local unsigned integer format.
41*b985414bSchristos  */
42*b985414bSchristos #define UINT16BYTES(p) ((uint32_t)((p)[0] + (256*(p)[1])))
43*b985414bSchristos #define UINT32BYTES(p) ((uint32_t)((p)[0] + (256*(p)[1]) +		\
44*b985414bSchristos 	    (65536*(p)[2]) + (16777216*(p)[3])))
45*b985414bSchristos 
46*b985414bSchristos /*
47*b985414bSchristos  * All following structures are according to:
48*b985414bSchristos  *
49*b985414bSchristos  * Microsoft Extensible Firmware Initiative FAT32 File System Specification
50*b985414bSchristos  * FAT: General Overview of On-Disk Format
51*b985414bSchristos  * Version 1.03, December 6, 2000
52*b985414bSchristos  * Microsoft Corporation
53*b985414bSchristos  */
54*b985414bSchristos 
55*b985414bSchristos /*
56*b985414bSchristos  * FAT boot sector and boot parameter block for
57*b985414bSchristos  * FAT12 and FAT16 volumes
58*b985414bSchristos  */
59*b985414bSchristos typedef struct fat_bsbpb {
60*b985414bSchristos 	/* common fields */
61*b985414bSchristos 	uint8_t BS_jmpBoot[3];
62*b985414bSchristos 	uint8_t BS_OEMName[8];
63*b985414bSchristos 	uint8_t BPB_BytsPerSec[2];
64*b985414bSchristos 	uint8_t BPB_SecPerClus;
65*b985414bSchristos 	uint8_t BPB_RsvdSecCnt[2];
66*b985414bSchristos 	uint8_t BPB_NumFATs;
67*b985414bSchristos 	uint8_t BPB_RootEntCnt[2];
68*b985414bSchristos 	uint8_t BPB_TotSec16[2];
69*b985414bSchristos 	uint8_t BPB_Media;
70*b985414bSchristos 	uint8_t BPB_FATSz16[2];
71*b985414bSchristos 	uint8_t BPB_SecPerTrack[2];
72*b985414bSchristos 	uint8_t BPB_NumHeads[2];
73*b985414bSchristos 	uint8_t BPB_HiddSec[4];
74*b985414bSchristos 	uint8_t BPB_TotSec32[4];
75*b985414bSchristos 	/* FAT12/FAT16 only fields */
76*b985414bSchristos 	uint8_t BS_DrvNum;
77*b985414bSchristos 	uint8_t BS_Reserved1;
78*b985414bSchristos 	uint8_t BS_BootSig;
79*b985414bSchristos 	uint8_t BS_VolID[4];
80*b985414bSchristos 	uint8_t BS_VolLab[11];
81*b985414bSchristos 	uint8_t BS_FilSysType[8];
82*b985414bSchristos } FAT_BSBPB; /* 62 bytes */
83*b985414bSchristos 
84*b985414bSchristos /*
85*b985414bSchristos  * FAT boot sector and boot parameter block for
86*b985414bSchristos  * FAT32 volumes
87*b985414bSchristos  */
88*b985414bSchristos typedef struct fat32_bsbpb {
89*b985414bSchristos 	/* common fields */
90*b985414bSchristos 	uint8_t BS_jmpBoot[3];
91*b985414bSchristos 	uint8_t BS_OEMName[8];
92*b985414bSchristos 	uint8_t BPB_BytsPerSec[2];
93*b985414bSchristos 	uint8_t BPB_SecPerClus;
94*b985414bSchristos 	uint8_t BPB_RsvdSecCnt[2];
95*b985414bSchristos 	uint8_t BPB_NumFATs;
96*b985414bSchristos 	uint8_t BPB_RootEntCnt[2];
97*b985414bSchristos 	uint8_t BPB_TotSec16[2];
98*b985414bSchristos 	uint8_t BPB_Media;
99*b985414bSchristos 	uint8_t BPB_FATSz16[2];
100*b985414bSchristos 	uint8_t BPB_SecPerTrack[2];
101*b985414bSchristos 	uint8_t BPB_NumHeads[2];
102*b985414bSchristos 	uint8_t BPB_HiddSec[4];
103*b985414bSchristos 	uint8_t BPB_TotSec32[4];
104*b985414bSchristos 	/* FAT32 only fields */
105*b985414bSchristos 	uint8_t BPB_FATSz32[4];
106*b985414bSchristos 	uint8_t BPB_ExtFlags[2];
107*b985414bSchristos 	uint8_t BPB_FSVer[2];
108*b985414bSchristos 	uint8_t BPB_RootClus[4];
109*b985414bSchristos 	uint8_t BPB_FSInfo[2];
110*b985414bSchristos 	uint8_t BPB_BkBootSec[2];
111*b985414bSchristos 	uint8_t BPB_Reserved[12];
112*b985414bSchristos 	uint8_t BS_DrvNum;
113*b985414bSchristos 	uint8_t BS_Reserved1;
114*b985414bSchristos 	uint8_t BS_BootSig;
115*b985414bSchristos 	uint8_t BS_VolID[4];
116*b985414bSchristos 	uint8_t BS_VolLab[11];
117*b985414bSchristos 	uint8_t BS_FilSysType[8];
118*b985414bSchristos } FAT32_BSBPB; /* 90 bytes */
119*b985414bSchristos 
120*b985414bSchristos /*
121*b985414bSchristos  * FAT directory entry structure
122*b985414bSchristos  */
123*b985414bSchristos #define	FAT_DES_ATTR_READ_ONLY	0x01
124*b985414bSchristos #define	FAT_DES_ATTR_HIDDEN	0x02
125*b985414bSchristos #define	FAT_DES_ATTR_SYSTEM	0x04
126*b985414bSchristos #define	FAT_DES_ATTR_VOLUME_ID	0x08
127*b985414bSchristos #define	FAT_DES_ATTR_DIRECTORY	0x10
128*b985414bSchristos #define	FAT_DES_ATTR_ARCHIVE	0x20
129*b985414bSchristos #define	FAT_DES_ATTR_LONG_NAME	(FAT_DES_ATTR_READ_ONLY |		\
130*b985414bSchristos 				 FAT_DES_ATTR_HIDDEN |			\
131*b985414bSchristos 				 FAT_DES_ATTR_SYSTEM |			\
132*b985414bSchristos 				 FAT_DES_ATTR_VOLUME_ID)
133*b985414bSchristos 
134*b985414bSchristos typedef struct fat_des {
135*b985414bSchristos 	uint8_t DIR_Name[11];
136*b985414bSchristos 	uint8_t DIR_Attr;
137*b985414bSchristos 	uint8_t DIR_NTRes;
138*b985414bSchristos 	uint8_t DIR_CrtTimeTenth;
139*b985414bSchristos 	uint8_t DIR_CrtTime[2];
140*b985414bSchristos 	uint8_t DIR_CrtDate[2];
141*b985414bSchristos 	uint8_t DIR_LstAccDate[2];
142*b985414bSchristos 	uint8_t DIR_FstClusHI[2];
143*b985414bSchristos 	uint8_t DIR_WrtTime[2];
144*b985414bSchristos 	uint8_t DIR_WrtDate[2];
145*b985414bSchristos 	uint8_t DIR_FstClusLO[2];
146*b985414bSchristos 	uint8_t DIR_FileSize[4];
147*b985414bSchristos } FAT_DES;
148