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