xref: /reactos/sdk/lib/fslib/vfatlib/check/fsck.fat.h (revision 469289ed)
1c2c66affSColin Finck /* fsck.fat.h  -  Common data structures and global variables
2c2c66affSColin Finck 
3c2c66affSColin Finck    Copyright (C) 1993 Werner Almesberger <werner.almesberger@lrc.di.epfl.ch>
4c2c66affSColin Finck    Copyright (C) 1998 Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>
5c2c66affSColin Finck    Copyright (C) 2008-2014 Daniel Baumann <mail@daniel-baumann.ch>
6c2c66affSColin Finck    Copyright (C) 2015 Andreas Bombe <aeb@debian.org>
7c2c66affSColin Finck 
8c2c66affSColin Finck    This program is free software: you can redistribute it and/or modify
9c2c66affSColin Finck    it under the terms of the GNU General Public License as published by
10c2c66affSColin Finck    the Free Software Foundation, either version 3 of the License, or
11c2c66affSColin Finck    (at your option) any later version.
12c2c66affSColin Finck 
13c2c66affSColin Finck    This program is distributed in the hope that it will be useful,
14c2c66affSColin Finck    but WITHOUT ANY WARRANTY; without even the implied warranty of
15c2c66affSColin Finck    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16c2c66affSColin Finck    GNU General Public License for more details.
17c2c66affSColin Finck 
18c2c66affSColin Finck    You should have received a copy of the GNU General Public License
19c2c66affSColin Finck    along with this program. If not, see <http://www.gnu.org/licenses/>.
20c2c66affSColin Finck 
21c2c66affSColin Finck    The complete text of the GNU General Public License
22c2c66affSColin Finck    can be found in /usr/share/common-licenses/GPL-3 file.
23c2c66affSColin Finck */
24c2c66affSColin Finck 
25c2c66affSColin Finck /* FAT32, VFAT, Atari format support, and various fixes additions May 1998
26c2c66affSColin Finck  * by Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de> */
27c2c66affSColin Finck 
28c2c66affSColin Finck #ifndef _DOSFSCK_H
29c2c66affSColin Finck #define _DOSFSCK_H
30c2c66affSColin Finck 
31*469289edSPierre Schweitzer #ifndef __REACTOS__
32*469289edSPierre Schweitzer #include <sys/types.h>
33*469289edSPierre Schweitzer #include <fcntl.h>
34*469289edSPierre Schweitzer #include <stddef.h>
35*469289edSPierre Schweitzer #endif
36c2c66affSColin Finck #include <stdint.h>
37c2c66affSColin Finck 
38*469289edSPierre Schweitzer #ifdef __REACTOS__
39c2c66affSColin Finck #ifdef _WIN32
40c2c66affSColin Finck 
41c2c66affSColin Finck typedef unsigned int __u32;
42c2c66affSColin Finck typedef unsigned __int64 __u64;
43c2c66affSColin Finck 
44c2c66affSColin Finck #define le16toh(v) (v)
45c2c66affSColin Finck #define le32toh(v) (v)
46c2c66affSColin Finck #define htole16(v) (v)
47c2c66affSColin Finck #define htole32(v) (v)
48c2c66affSColin Finck 
49c2c66affSColin Finck #endif
50c2c66affSColin Finck 
51c2c66affSColin Finck #ifdef _M_IX86
52c2c66affSColin Finck #include "byteorder.h"
53c2c66affSColin Finck #endif
54c2c66affSColin Finck 
55c2c66affSColin Finck #if __BYTE_ORDER == __BIG_ENDIAN
56c2c66affSColin Finck #include "byteswap.h"
57c2c66affSColin Finck #else
58c2c66affSColin Finck #define le16toh(v) (v)
59c2c66affSColin Finck #define le32toh(v) (v)
60c2c66affSColin Finck #define htole16(v) (v)
61c2c66affSColin Finck #define htole32(v) (v)
62c2c66affSColin Finck #endif /* __BIG_ENDIAN */
63c2c66affSColin Finck 
64*469289edSPierre Schweitzer #endif
65c2c66affSColin Finck 
66*469289edSPierre Schweitzer #ifndef __REACTOS__
67*469289edSPierre Schweitzer #include "endian_compat.h"
68*469289edSPierre Schweitzer #else
69c2c66affSColin Finck #ifndef offsetof
70c2c66affSColin Finck #define offsetof(t,e)	((int)&(((t *)0)->e))
71c2c66affSColin Finck #endif
72c2c66affSColin Finck 
73c2c66affSColin Finck #include "rosglue.h"
74*469289edSPierre Schweitzer #endif
75c2c66affSColin Finck 
76c2c66affSColin Finck #include "msdos_fs.h"
77c2c66affSColin Finck 
78c2c66affSColin Finck #define VFAT_LN_ATTR (ATTR_RO | ATTR_HIDDEN | ATTR_SYS | ATTR_VOLUME)
79c2c66affSColin Finck 
80c2c66affSColin Finck #define FAT_STATE_DIRTY 0x01
81c2c66affSColin Finck 
82*469289edSPierre Schweitzer #ifdef __REACTOS__
83c2c66affSColin Finck #include <pshpack1.h>
84*469289edSPierre Schweitzer #endif
85c2c66affSColin Finck 
86c2c66affSColin Finck /* ++roman: Use own definition of boot sector structure -- the kernel headers'
87c2c66affSColin Finck  * name for it is msdos_boot_sector in 2.0 and fat_boot_sector in 2.1 ... */
88c2c66affSColin Finck struct boot_sector {
89c2c66affSColin Finck     uint8_t ignored[3];		/* Boot strap short or near jump */
90c2c66affSColin Finck     uint8_t system_id[8];	/* Name - can be used to special case
91c2c66affSColin Finck 				   partition manager volumes */
92c2c66affSColin Finck     uint8_t sector_size[2];	/* bytes per logical sector */
93c2c66affSColin Finck     uint8_t cluster_size;	/* sectors/cluster */
94c2c66affSColin Finck     uint16_t reserved;		/* reserved sectors */
95c2c66affSColin Finck     uint8_t fats;		/* number of FATs */
96c2c66affSColin Finck     uint8_t dir_entries[2];	/* root directory entries */
97c2c66affSColin Finck     uint8_t sectors[2];		/* number of sectors */
98c2c66affSColin Finck     uint8_t media;		/* media code (unused) */
99c2c66affSColin Finck     uint16_t fat_length;	/* sectors/FAT */
100c2c66affSColin Finck     uint16_t secs_track;	/* sectors per track */
101c2c66affSColin Finck     uint16_t heads;		/* number of heads */
102c2c66affSColin Finck     uint32_t hidden;		/* hidden sectors (unused) */
103c2c66affSColin Finck     uint32_t total_sect;	/* number of sectors (if sectors == 0) */
104c2c66affSColin Finck 
105c2c66affSColin Finck     /* The following fields are only used by FAT32 */
106c2c66affSColin Finck     uint32_t fat32_length;	/* sectors/FAT */
107c2c66affSColin Finck     uint16_t flags;		/* bit 8: fat mirroring, low 4: active fat */
108c2c66affSColin Finck     uint8_t version[2];		/* major, minor filesystem version */
109c2c66affSColin Finck     uint32_t root_cluster;	/* first cluster in root directory */
110c2c66affSColin Finck     uint16_t info_sector;	/* filesystem info sector */
111c2c66affSColin Finck     uint16_t backup_boot;	/* backup boot sector */
112c2c66affSColin Finck     uint8_t reserved2[12];	/* Unused */
113c2c66affSColin Finck 
114c2c66affSColin Finck     uint8_t drive_number;	/* Logical Drive Number */
115c2c66affSColin Finck     uint8_t reserved3;		/* Unused */
116c2c66affSColin Finck 
117c2c66affSColin Finck     uint8_t extended_sig;	/* Extended Signature (0x29) */
118c2c66affSColin Finck     uint32_t serial;		/* Serial number */
119c2c66affSColin Finck     uint8_t label[11];		/* FS label */
120c2c66affSColin Finck     uint8_t fs_type[8];		/* FS Type */
121c2c66affSColin Finck 
122c2c66affSColin Finck     /* fill up to 512 bytes */
123c2c66affSColin Finck     uint8_t junk[422];
124c2c66affSColin Finck } __attribute__ ((packed));
125c2c66affSColin Finck 
126c2c66affSColin Finck struct boot_sector_16 {
127c2c66affSColin Finck     uint8_t ignored[3];		/* Boot strap short or near jump */
128c2c66affSColin Finck     uint8_t system_id[8];	/* Name - can be used to special case
129c2c66affSColin Finck 				   partition manager volumes */
130c2c66affSColin Finck     uint8_t sector_size[2];	/* bytes per logical sector */
131c2c66affSColin Finck     uint8_t cluster_size;	/* sectors/cluster */
132c2c66affSColin Finck     uint16_t reserved;		/* reserved sectors */
133c2c66affSColin Finck     uint8_t fats;		/* number of FATs */
134c2c66affSColin Finck     uint8_t dir_entries[2];	/* root directory entries */
135c2c66affSColin Finck     uint8_t sectors[2];		/* number of sectors */
136c2c66affSColin Finck     uint8_t media;		/* media code (unused) */
137c2c66affSColin Finck     uint16_t fat_length;	/* sectors/FAT */
138c2c66affSColin Finck     uint16_t secs_track;	/* sectors per track */
139c2c66affSColin Finck     uint16_t heads;		/* number of heads */
140c2c66affSColin Finck     uint32_t hidden;		/* hidden sectors (unused) */
141c2c66affSColin Finck     uint32_t total_sect;	/* number of sectors (if sectors == 0) */
142c2c66affSColin Finck 
143c2c66affSColin Finck     uint8_t drive_number;	/* Logical Drive Number */
144c2c66affSColin Finck     uint8_t reserved2;		/* Unused */
145c2c66affSColin Finck 
146c2c66affSColin Finck     uint8_t extended_sig;	/* Extended Signature (0x29) */
147c2c66affSColin Finck     uint32_t serial;		/* Serial number */
148c2c66affSColin Finck     uint8_t label[11];		/* FS label */
149c2c66affSColin Finck     uint8_t fs_type[8];		/* FS Type */
150c2c66affSColin Finck 
151c2c66affSColin Finck     /* fill up to 512 bytes */
152c2c66affSColin Finck     uint8_t junk[450];
153c2c66affSColin Finck } __attribute__ ((packed));
154c2c66affSColin Finck 
155c2c66affSColin Finck struct info_sector {
156c2c66affSColin Finck     uint32_t magic;		/* Magic for info sector ('RRaA') */
157*469289edSPierre Schweitzer     uint8_t reserved1[480];
158c2c66affSColin Finck     uint32_t signature;		/* 0x61417272 ('rrAa') */
159c2c66affSColin Finck     uint32_t free_clusters;	/* Free cluster count.  -1 if unknown */
160c2c66affSColin Finck     uint32_t next_cluster;	/* Most recently allocated cluster. */
161*469289edSPierre Schweitzer     uint8_t reserved2[12];
162*469289edSPierre Schweitzer     uint32_t boot_sign;
163c2c66affSColin Finck };
164c2c66affSColin Finck 
165c2c66affSColin Finck typedef struct {
166c2c66affSColin Finck     uint8_t name[MSDOS_NAME];	/* name including extension */
167c2c66affSColin Finck     uint8_t attr;		/* attribute bits */
168c2c66affSColin Finck     uint8_t lcase;		/* Case for base and extension */
169c2c66affSColin Finck     uint8_t ctime_ms;		/* Creation time, milliseconds */
170c2c66affSColin Finck     uint16_t ctime;		/* Creation time */
171c2c66affSColin Finck     uint16_t cdate;		/* Creation date */
172c2c66affSColin Finck     uint16_t adate;		/* Last access date */
173c2c66affSColin Finck     uint16_t starthi;		/* High 16 bits of cluster in FAT32 */
174c2c66affSColin Finck     uint16_t time, date, start;	/* time, date and first cluster */
175c2c66affSColin Finck     uint32_t size;		/* file size (in bytes) */
176c2c66affSColin Finck } __attribute__ ((packed)) DIR_ENT;
177c2c66affSColin Finck 
178*469289edSPierre Schweitzer #ifdef __REACTOS__
179c2c66affSColin Finck #include <poppack.h>
180*469289edSPierre Schweitzer #endif
181c2c66affSColin Finck 
182c2c66affSColin Finck typedef struct _dos_file {
183c2c66affSColin Finck     DIR_ENT dir_ent;
184c2c66affSColin Finck     char *lfn;
185c2c66affSColin Finck     off_t offset;
186c2c66affSColin Finck     off_t lfn_offset;
187c2c66affSColin Finck     struct _dos_file *parent;	/* parent directory */
188c2c66affSColin Finck     struct _dos_file *next;	/* next entry */
189c2c66affSColin Finck     struct _dos_file *first;	/* first entry (directory only) */
190c2c66affSColin Finck } DOS_FILE;
191c2c66affSColin Finck 
192c2c66affSColin Finck typedef struct {
193c2c66affSColin Finck     uint32_t value;
194c2c66affSColin Finck     uint32_t reserved;
195c2c66affSColin Finck } FAT_ENTRY;
196c2c66affSColin Finck 
197c2c66affSColin Finck typedef struct {
198c2c66affSColin Finck     int nfats;
199c2c66affSColin Finck     off_t fat_start;
200*469289edSPierre Schweitzer     off_t fat_size;		/* unit is bytes */
201c2c66affSColin Finck     unsigned int fat_bits;	/* size of a FAT entry */
202c2c66affSColin Finck     unsigned int eff_fat_bits;	/* # of used bits in a FAT entry */
203c2c66affSColin Finck     uint32_t root_cluster;	/* 0 for old-style root dir */
204c2c66affSColin Finck     off_t root_start;
205c2c66affSColin Finck     unsigned int root_entries;
206c2c66affSColin Finck     off_t data_start;
207c2c66affSColin Finck     unsigned int cluster_size;
208c2c66affSColin Finck     uint32_t data_clusters;	/* not including two reserved cluster numbers */
209c2c66affSColin Finck     off_t fsinfo_start;		/* 0 if not present */
210c2c66affSColin Finck     long free_clusters;
211c2c66affSColin Finck     off_t backupboot_start;	/* 0 if not present */
212c2c66affSColin Finck     unsigned char *fat;
213c2c66affSColin Finck     DOS_FILE **cluster_owner;
214c2c66affSColin Finck     char *label;
215c2c66affSColin Finck } DOS_FS;
216c2c66affSColin Finck 
217*469289edSPierre Schweitzer #ifndef __REACTOS__
218*469289edSPierre Schweitzer extern int interactive, rw, list, verbose, test, write_immed;
219*469289edSPierre Schweitzer extern int atari_format;
220*469289edSPierre Schweitzer extern unsigned n_files;
221*469289edSPierre Schweitzer extern void *mem_queue;
222*469289edSPierre Schweitzer #endif
223c2c66affSColin Finck 
224c2c66affSColin Finck /* value to use as end-of-file marker */
225c2c66affSColin Finck #define FAT_EOF(fs)	((atari_format ? 0xfff : 0xff8) | FAT_EXTD(fs))
226c2c66affSColin Finck #define FAT_IS_EOF(fs,v) ((uint32_t)(v) >= (0xff8|FAT_EXTD(fs)))
227c2c66affSColin Finck /* value to mark bad clusters */
228c2c66affSColin Finck #define FAT_BAD(fs)	(0xff7 | FAT_EXTD(fs))
229c2c66affSColin Finck /* range of values used for bad clusters */
230c2c66affSColin Finck #define FAT_MIN_BAD(fs)	((atari_format ? 0xff0 : 0xff7) | FAT_EXTD(fs))
231c2c66affSColin Finck #define FAT_MAX_BAD(fs)	((atari_format ? 0xff7 : 0xff7) | FAT_EXTD(fs))
232c2c66affSColin Finck #define FAT_IS_BAD(fs,v) ((v) >= FAT_MIN_BAD(fs) && (v) <= FAT_MAX_BAD(fs))
233c2c66affSColin Finck 
234c2c66affSColin Finck /* return -16 as a number with fs->fat_bits bits */
235c2c66affSColin Finck #define FAT_EXTD(fs)	(((1 << fs->eff_fat_bits)-1) & ~0xf)
236c2c66affSColin Finck 
237c2c66affSColin Finck /* marker for files with no 8.3 name */
238c2c66affSColin Finck #define FAT_NO_83NAME 32
239c2c66affSColin Finck 
240c2c66affSColin Finck #endif
241