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