fat.h revision f79b2dff1024db4f6326f3422236bed169dd902f
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef FAT_H
18#define FAT_H
19
20#include <stdint.h>
21
22#include "types.h"
23
24typedef uint64_t sector_t;
25typedef cluster_t fat_entry_t;
26
27struct fat_boot_sector {
28	uint8_t jump[3];
29	char name[8];
30	uint16_t sector_size;
31	uint8_t sectors_per_cluster;
32	uint16_t reserved_sectors;
33	uint8_t fats;
34	uint16_t rootdir_size;
35	uint16_t sectors16;
36	uint8_t media_desc;
37	uint16_t fat_sectors16;
38	uint16_t sectors_per_track;
39	uint16_t heads;
40	uint32_t hidden_sectors;
41	uint32_t sectors32;
42	uint32_t fat_sectors32;
43	uint16_t fat_flags;
44	uint16_t version;
45	cluster_t rootdir_start;
46	uint16_t fs_info_sector;
47	uint16_t backup_boot_sector;
48	uint8_t reserved1[12];
49	uint8_t phys_drive;
50	uint8_t reserved2;
51	uint8_t ext_boot_sig;
52	uint32_t serial;
53	char vol_label[11];
54	char type[8];
55	char boot_code[420];
56	uint8_t boot_sig[2];
57} __attribute__((__packed__));
58
59#define FAT_MEDIA_DESC_FIXED 0xF8
60
61#define FAT_PHYS_DRIVE_REMOVABLE 0x00
62#define FAT_PHYS_DRIVE_FIXED     0x80
63
64#define FAT_EXT_BOOT_SIG 0x29
65
66extern const char FAT_BOOT_SIG[2];
67
68extern const char FAT_INFO_SIG1[4];
69extern const char FAT_INFO_SIG2[4];
70#define FAT_INFO_SIG3 FAT_BOOT_SIG
71
72struct fat_info_sector {
73	char info_sig1[4];
74	char reserved1[480];
75	char info_sig2[4];
76	cluster_t free_clusters;
77	cluster_t last_cluster;
78	char reserved2[14];
79	char info_sig3[2];
80} __attribute__((__packed__));
81
82struct fat_bootinfo {
83	struct fat_boot_sector boot;
84	struct fat_info_sector info;
85} __attribute__((__packed__));
86
87struct fat_dirent {
88	char name[11];
89	uint8_t attr;
90	uint8_t reserved;
91	uint8_t ctime_ms;
92	uint16_t ctime;
93	uint16_t cdate;
94	uint16_t adate;
95	uint16_t first_cluster_hi;
96	uint16_t mtime;
97	uint16_t mdate;
98	uint16_t first_cluster_lo;
99	uint32_t size;
100} __attribute__((__packed__));
101
102#define FAT_ATTR_READONLY 0x01
103#define FAT_ATTR_HIDDEN   0x02
104#define FAT_ATTR_SYSTEM   0x04
105#define FAT_ATTR_VOLLABEL 0x08
106#define FAT_ATTR_SUBDIR   0x10
107#define FAT_ATTR_ARCHIVE  0x20
108#define FAT_ATTR_DEVICE   0x40
109
110#define FAT_ENTRY_FREE  0x00000000
111#define FAT_ENTRY_BAD   0x0FFFFFF7
112#define FAT_ENTRY_EOC   0x0FFFFFF8
113
114#define FAT_SECTOR_SIZE 512
115#define FAT_CLUSTER_ZERO 2
116#define FAT_ENTRIES_PER_SECTOR ((SECTOR_SIZE) / (sizeof(fat_entry_t)))
117
118void fat_dirent_set_first_cluster(struct fat_dirent *de, cluster_t cluster);
119void fat_dirent_set(struct fat_dirent *de,
120                    char *name, uint8_t attr,
121                    cluster_t first_cluster, uint32_t size);
122
123#endif
124