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
27#include <sys/types.h>
28
29/*
30 * Conversion macros for little endian encoded unsigned integers
31 * in byte streams to the local unsigned integer format.
32 */
33#define UINT16BYTES(p) ((uint32_t)((p)[0] + (256*(p)[1])))
34#define UINT32BYTES(p) ((uint32_t)((p)[0] + (256*(p)[1]) +		\
35	    (65536*(p)[2]) + (16777216*(p)[3])))
36
37/*
38 * All following structures are according to:
39 *
40 * Microsoft Extensible Firmware Initiative FAT32 File System Specification
41 * FAT: General Overview of On-Disk Format
42 * Version 1.03, December 6, 2000
43 * Microsoft Corporation
44 */
45
46/*
47 * FAT boot sector and boot parameter block for
48 * FAT12 and FAT16 volumes
49 */
50typedef struct fat_bsbpb {
51	/* common fields */
52	uint8_t BS_jmpBoot[3];
53	uint8_t BS_OEMName[8];
54	uint8_t BPB_BytsPerSec[2];
55	uint8_t BPB_SecPerClus;
56	uint8_t BPB_RsvdSecCnt[2];
57	uint8_t BPB_NumFATs;
58	uint8_t BPB_RootEntCnt[2];
59	uint8_t BPB_TotSec16[2];
60	uint8_t BPB_Media;
61	uint8_t BPB_FATSz16[2];
62	uint8_t BPB_SecPerTrack[2];
63	uint8_t BPB_NumHeads[2];
64	uint8_t BPB_HiddSec[4];
65	uint8_t BPB_TotSec32[4];
66	/* FAT12/FAT16 only fields */
67	uint8_t BS_DrvNum;
68	uint8_t BS_Reserved1;
69	uint8_t BS_BootSig;
70	uint8_t BS_VolID[4];
71	uint8_t BS_VolLab[11];
72	uint8_t BS_FilSysType[8];
73} FAT_BSBPB; /* 62 bytes */
74
75/*
76 * FAT boot sector and boot parameter block for
77 * FAT32 volumes
78 */
79typedef struct fat32_bsbpb {
80	/* common fields */
81	uint8_t BS_jmpBoot[3];
82	uint8_t BS_OEMName[8];
83	uint8_t BPB_BytsPerSec[2];
84	uint8_t BPB_SecPerClus;
85	uint8_t BPB_RsvdSecCnt[2];
86	uint8_t BPB_NumFATs;
87	uint8_t BPB_RootEntCnt[2];
88	uint8_t BPB_TotSec16[2];
89	uint8_t BPB_Media;
90	uint8_t BPB_FATSz16[2];
91	uint8_t BPB_SecPerTrack[2];
92	uint8_t BPB_NumHeads[2];
93	uint8_t BPB_HiddSec[4];
94	uint8_t BPB_TotSec32[4];
95	/* FAT32 only fields */
96	uint8_t BPB_FATSz32[4];
97	uint8_t BPB_ExtFlags[2];
98	uint8_t BPB_FSVer[2];
99	uint8_t BPB_RootClus[4];
100	uint8_t BPB_FSInfo[2];
101	uint8_t BPB_BkBootSec[2];
102	uint8_t BPB_Reserved[12];
103	uint8_t BS_DrvNum;
104	uint8_t BS_Reserved1;
105	uint8_t BS_BootSig;
106	uint8_t BS_VolID[4];
107	uint8_t BS_VolLab[11];
108	uint8_t BS_FilSysType[8];
109} FAT32_BSBPB; /* 90 bytes */
110
111/*
112 * FAT directory entry structure
113 */
114#define	FAT_DES_ATTR_READ_ONLY	0x01
115#define	FAT_DES_ATTR_HIDDEN	0x02
116#define	FAT_DES_ATTR_SYSTEM	0x04
117#define	FAT_DES_ATTR_VOLUME_ID	0x08
118#define	FAT_DES_ATTR_DIRECTORY	0x10
119#define	FAT_DES_ATTR_ARCHIVE	0x20
120#define	FAT_DES_ATTR_LONG_NAME	(FAT_DES_ATTR_READ_ONLY |		\
121				 FAT_DES_ATTR_HIDDEN |			\
122				 FAT_DES_ATTR_SYSTEM |			\
123				 FAT_DES_ATTR_VOLUME_ID)
124
125typedef struct fat_des {
126	uint8_t DIR_Name[11];
127	uint8_t DIR_Attr;
128	uint8_t DIR_NTRes;
129	uint8_t DIR_CrtTimeTenth;
130	uint8_t DIR_CrtTime[2];
131	uint8_t DIR_CrtDate[2];
132	uint8_t DIR_LstAccDate[2];
133	uint8_t DIR_FstClusHI[2];
134	uint8_t DIR_WrtTime[2];
135	uint8_t DIR_WrtDate[2];
136	uint8_t DIR_FstClusLO[2];
137	uint8_t DIR_FileSize[4];
138} FAT_DES;
139