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