msdos_fs.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2 #ifndef _UAPI_LINUX_MSDOS_FS_H
3 #define _UAPI_LINUX_MSDOS_FS_H
4 
5 /*
6  * The MS-DOS filesystem constants/structures
7  */
8 
9 #ifndef SECTOR_SIZE
10 #define SECTOR_SIZE 512 /* sector size (bytes) */
11 #endif
12 #define SECTOR_BITS 9 /* log2(SECTOR_SIZE) */
13 #define MSDOS_DPB (MSDOS_DPS) /* dir entries per block */
14 #define MSDOS_DPB_BITS 4 /* log2(MSDOS_DPB) */
15 #define MSDOS_DPS (SECTOR_SIZE / sizeof(struct msdos_dir_entry))
16 #define MSDOS_DPS_BITS 4 /* log2(MSDOS_DPS) */
17 #define MSDOS_LONGNAME 256 /* maximum name length */
18 
19 #define MSDOS_ROOT_INO 1 /* The root inode number */
20 #define MSDOS_FSINFO_INO 2 /* Used for managing the FSINFO block */
21 
22 #define MSDOS_DIR_BITS 5 /* log2(sizeof(struct msdos_dir_entry)) */
23 
24 /* directory limit */
25 #define FAT_MAX_DIR_ENTRIES (65536)
26 #define FAT_MAX_DIR_SIZE (FAT_MAX_DIR_ENTRIES << MSDOS_DIR_BITS)
27 
28 #define ATTR_NONE 0x0000 /* no attribute bits */
29 #define ATTR_RO 0x0001 /* read-only */
30 #define ATTR_HIDDEN 0x0002 /* hidden */
31 #define ATTR_SYS 0x0004 /* system */
32 #define ATTR_VOLUME 0x0008 /* volume label */
33 #define ATTR_DIR 0x0010 /* directory */
34 #define ATTR_ARCH 0x0020 /* archived */
35 
36 /* attribute bits that are copied "as is" */
37 #define ATTR_UNUSED (ATTR_VOLUME | ATTR_ARCH | ATTR_SYS | ATTR_HIDDEN)
38 /* bits that are used by the Windows 95/Windows NT extended FAT */
39 #define ATTR_EXT (ATTR_RO | ATTR_HIDDEN | ATTR_SYS | ATTR_VOLUME)
40 
41 #define CASE_LOWER_BASE 8 /* base is lower case */
42 #define CASE_LOWER_EXT 16 /* extension is lower case */
43 
44 #define DELETED_FLAG 0xe5 /* marks file as deleted when in name[0] */
45 #define IS_FREE(n) (!*(n) || *(n) == DELETED_FLAG)
46 
47 #define FAT_LFN_LEN 255 /* maximum long name length */
48 #define MSDOS_NAME 11 /* maximum name length */
49 #define MSDOS_SLOTS 21 /* max # of slots for short and long names */
50 #define MSDOS_DOT ". " /* ".", padded to MSDOS_NAME chars */
51 #define MSDOS_DOTDOT ".. " /* "..", padded to MSDOS_NAME chars */
52 
53 /* start of data cluster's entry (number of reserved clusters) */
54 #define FAT_START_ENT 2
55 
56 /* maximum number of clusters */
57 // #define MAX_FAT12 0xFF4
58 // #define MAX_FAT16 0xFFF4
59 // #define MAX_FAT32 0x0FFFFFF6
60 
61 /* bad cluster mark */
62 #define BAD_FAT12 0xFF7
63 #define BAD_FAT16 0xFFF7
64 #define BAD_FAT32 0x0FFFFFF7
65 
66 /* standard EOF */
67 #define EOF_FAT12 0xFFF
68 #define EOF_FAT16 0xFFFF
69 #define EOF_FAT32 0x0FFFFFFF
70 
71 #define FAT_ENT_FREE (0)
72 #define FAT_ENT_BAD (BAD_FAT32)
73 #define FAT_ENT_EOF (EOF_FAT32)
74 
75 #define FAT_FSINFO_SIG1 0x41615252
76 #define FAT_FSINFO_SIG2 0x61417272
77 #define IS_FSINFO(x) ((x)->signature1 == FAT_FSINFO_SIG1 && (x)->signature2 == FAT_FSINFO_SIG2)
78 
79 #define FAT_STATE_DIRTY 0x01
80 
81 struct __attribute__((packed)) fat_boot_sector_t {
82  uint8_t jmp_boot[3]; /* Boot strap short or near jump */
83  char system_id[8]; /* Name - can be used to special case partition manager volumes */
84  uint16_t sector_size; /* bytes per logical sector */
85  uint8_t sec_per_clus; /* sectors/cluster */
86  uint16_t reserved; /* reserved sectors */
87  uint8_t num_fats; /* number of FATs */
88  uint16_t dir_entries; /* root directory entries */
89  uint16_t sectors; /* number of sectors */
90  uint8_t media; /* media code */
91  uint16_t fat_length; /* sectors/FAT */
92  uint16_t secs_track; /* sectors per track */
93  uint16_t heads; /* number of heads */
94  uint32_t hidden; /* hidden sectors (unused) */
95  uint32_t total_sect; /* number of sectors (if sectors == 0) */
96 
97  union {
98  struct __attribute__((packed)) {
99  /* Extended BPB Fields for FAT16 */
100  uint8_t drive_number; /* Physical drive number */
101  uint8_t state; /* undocumented, but used for mount state. */
102  uint8_t signature; /* extended boot signature */
103  uint32_t vol_id; /* volume ID */
104  char vol_label[MSDOS_NAME]; /* volume label */
105  uint64_t fs_type; /* file system type */
106  /* other fields are not added here */
107  } fat16;
108 
109  struct __attribute__((packed)) {
110  /* only used by FAT32 */
111  uint32_t fat_length; /* sectors/FAT */
112  uint16_t flags; /* bit 8: fat mirroring, low 4: active fat */
113  uint16_t version; /* major, minor filesystem version */
114  uint32_t root_cluster; /* first cluster in root directory */
115  uint16_t info_sector; /* filesystem info sector */
116  uint16_t backup_boot; /* backup boot sector */
117  uint16_t reserved2[6]; /* Unused */
118  /* Extended BPB Fields for FAT32 */
119  uint8_t drive_number; /* Physical drive number */
120  uint8_t state; /* undocumented, but used for mount state. */
121  uint8_t signature; /* extended boot signature */
122  uint32_t vol_id; /* volume ID */
123  char vol_label[MSDOS_NAME]; /* volume label */
124  uint64_t fs_type; /* file system type */
125  /* other fields are not added here */
126  } fat32;
127 
128  struct __attribute__((packed)) {
129  uint8_t extra_info[510 - 36];
130  uint16_t signature;
131  };
132  };
133 };
134 
135 static_assert(offsetof(fat_boot_sector_t, signature) == 510, "Bad fat_boot_sector_t");
136 
138  uint32_t signature1; /* 0x41615252L */
139  uint32_t reserved1[120]; /* Nothing as far as I can tell */
140  uint32_t signature2; /* 0x61417272L */
141  uint32_t free_clusters; /* Free cluster count. -1 if unknown */
142  uint32_t next_cluster; /* Most recently allocated cluster */
143  uint16_t reserved2[7];
144  uint16_t signature;
145 };
146 
147 static_assert(offsetof(fat_boot_fsinfo_t, signature) == 510, "Bad fat_boot_fsinfo_t");
148 
150  uint8_t name[MSDOS_NAME]; /* name and extension */
151  uint8_t attr; /* attribute bits */
152  uint8_t lcase; /* Case for base and extension */
153  uint8_t ctime_cs; /* Creation time, centiseconds (0-199) */
154  uint16_t ctime; /* Creation time */
155  uint16_t cdate; /* Creation date */
156  uint16_t adate; /* Last access date */
157  uint16_t starthi; /* High 16 bits of cluster in FAT32 */
158  uint16_t time, date, start; /* time, date and first cluster */
159  uint32_t size; /* file size (in bytes) */
160 };
161 
162 static_assert(sizeof(msdos_dir_entry_t) == 32, "Bad msdos_dir_entry_t");
163 
164 /* Up to 13 characters of the name */
166  uint8_t id; /* sequence number for slot */
167  uint8_t name0_4[10]; /* first 5 characters in name */
168  uint8_t attr; /* attribute byte */
169  uint8_t reserved; /* always 0 */
170  uint8_t alias_checksum; /* checksum for 8.3 alias */
171  uint8_t name5_10[12]; /* 6 more characters in name */
172  uint16_t start; /* starting cluster number, 0 in long slots */
173  uint8_t name11_12[4]; /* last 2 characters in name */
174 };
175 
176 #endif /* _UAPI_LINUX_MSDOS_FS_H */
#define MSDOS_NAME
Definition: msdos_fs.h:48
Definition: msdos_fs.h:137
uint16_t reserved2[7]
Definition: msdos_fs.h:143
uint32_t free_clusters
Definition: msdos_fs.h:141
uint32_t reserved1[120]
Definition: msdos_fs.h:139
uint32_t signature2
Definition: msdos_fs.h:140
uint32_t next_cluster
Definition: msdos_fs.h:142
uint32_t signature1
Definition: msdos_fs.h:138
uint16_t signature
Definition: msdos_fs.h:144
Definition: msdos_fs.h:81
uint64_t fs_type
Definition: msdos_fs.h:105
uint8_t drive_number
Definition: msdos_fs.h:100
uint16_t info_sector
Definition: msdos_fs.h:115
uint16_t reserved
Definition: msdos_fs.h:86
uint8_t state
Definition: msdos_fs.h:101
uint8_t media
Definition: msdos_fs.h:90
uint16_t signature
Definition: msdos_fs.h:130
uint16_t heads
Definition: msdos_fs.h:93
uint32_t hidden
Definition: msdos_fs.h:94
uint16_t dir_entries
Definition: msdos_fs.h:88
uint16_t version
Definition: msdos_fs.h:113
uint32_t fat_length
Definition: msdos_fs.h:111
uint16_t flags
Definition: msdos_fs.h:112
uint16_t backup_boot
Definition: msdos_fs.h:116
uint32_t root_cluster
Definition: msdos_fs.h:114
uint16_t fat_length
Definition: msdos_fs.h:91
uint8_t num_fats
Definition: msdos_fs.h:87
uint16_t sector_size
Definition: msdos_fs.h:84
uint16_t secs_track
Definition: msdos_fs.h:92
uint32_t vol_id
Definition: msdos_fs.h:103
uint32_t total_sect
Definition: msdos_fs.h:95
uint8_t sec_per_clus
Definition: msdos_fs.h:85
uint8_t signature
Definition: msdos_fs.h:102
uint16_t sectors
Definition: msdos_fs.h:89
Definition: msdos_fs.h:149
uint16_t starthi
Definition: msdos_fs.h:157
uint8_t ctime_cs
Definition: msdos_fs.h:153
uint32_t size
Definition: msdos_fs.h:159
uint8_t lcase
Definition: msdos_fs.h:152
uint8_t attr
Definition: msdos_fs.h:151
uint16_t adate
Definition: msdos_fs.h:156
uint8_t name[MSDOS_NAME]
Definition: msdos_fs.h:150
uint16_t start
Definition: msdos_fs.h:158
uint16_t date
Definition: msdos_fs.h:158
uint16_t ctime
Definition: msdos_fs.h:154
uint16_t time
Definition: msdos_fs.h:158
uint16_t cdate
Definition: msdos_fs.h:155
Definition: msdos_fs.h:165
uint8_t name0_4[10]
Definition: msdos_fs.h:167
uint8_t reserved
Definition: msdos_fs.h:169
uint16_t start
Definition: msdos_fs.h:172
uint8_t attr
Definition: msdos_fs.h:168
uint8_t name5_10[12]
Definition: msdos_fs.h:171
uint8_t alias_checksum
Definition: msdos_fs.h:170
uint8_t id
Definition: msdos_fs.h:166
uint8_t name11_12[4]
Definition: msdos_fs.h:173