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 "fatblock.h"
23
24typedef uint32_t cluster_t;
25typedef uint64_t sector_t;
26typedef cluster_t fat_entry_t;
27
28struct fat_boot_sector {
29	uint8_t jump[3];
30	char name[8];
31	uint16_t sector_size;
32	uint8_t sectors_per_cluster;
33	uint16_t reserved_sectors;
34	uint8_t fats;
35	uint16_t rootdir_size;
36	uint16_t sectors16;
37	uint8_t media_desc;
38	uint16_t fat_sectors16;
39	uint16_t sectors_per_track;
40	uint16_t heads;
41	uint32_t hidden_sectors;
42	uint32_t sectors32;
43	uint32_t fat_sectors32;
44	uint16_t fat_flags;
45	uint16_t version;
46	cluster_t rootdir_start;
47	uint16_t fs_info_sector;
48	uint16_t backup_boot_sector;
49	uint8_t reserved1[12];
50	uint8_t phys_drive;
51	uint8_t reserved2;
52	uint8_t ext_boot_sig;
53	uint32_t serial;
54	char vol_label[11];
55	char type[8];
56	char boot_code[420];
57	uint8_t boot_sig[2];
58} __attribute__((__packed__));
59
60#define FAT_MEDIA_DESC_FIXED 0xF8
61
62#define FAT_PHYS_DRIVE_REMOVABLE 0x00
63#define FAT_PHYS_DRIVE_FIXED     0x80
64
65#define FAT_EXT_BOOT_SIG 0x29
66
67extern const char FAT_BOOT_SIG[2];
68
69extern const char FAT_INFO_SIG1[4];
70extern const char FAT_INFO_SIG2[4];
71#define FAT_INFO_SIG3 FAT_BOOT_SIG
72
73struct fat_info_sector {
74	char info_sig1[4];
75	char reserved1[480];
76	char info_sig2[4];
77	cluster_t free_clusters;
78	cluster_t last_cluster;
79	char reserved2[14];
80	char info_sig3[2];
81} __attribute__((__packed__));
82
83struct fat_bootinfo {
84	struct fat_boot_sector boot;
85	struct fat_info_sector info;
86} __attribute__((__packed__));
87
88struct fat_dirent {
89	char name[11];
90	uint8_t attr;
91	uint8_t reserved;
92	uint8_t ctime_ms;
93	uint16_t ctime;
94	uint16_t cdate;
95	uint16_t adate;
96	uint16_t first_cluster_hi;
97	uint16_t mtime;
98	uint16_t mdate;
99	uint16_t first_cluster_lo;
100	uint32_t size;
101} __attribute__((__packed__));
102
103#define FAT_ATTR_READONLY 0x01
104#define FAT_ATTR_HIDDEN   0x02
105#define FAT_ATTR_SYSTEM   0x04
106#define FAT_ATTR_VOLLABEL 0x08
107#define FAT_ATTR_SUBDIR   0x10
108#define FAT_ATTR_ARCHIVE  0x20
109#define FAT_ATTR_DEVICE   0x40
110
111#define FAT_ENTRY_FREE  0x00000000
112#define FAT_ENTRY_BAD   0x0FFFFFF7
113#define FAT_ENTRY_EOC   0x0FFFFFF8
114
115#define FAT_SECTOR_SIZE 512
116#define FAT_CLUSTER_ZERO 2
117#define FAT_ENTRIES_PER_SECTOR ((SECTOR_SIZE) / (sizeof(fat_entry_t)))
118
119void fat_dirent_set_first_cluster(struct fat_dirent *de, cluster_t cluster);
120void fat_dirent_set(struct fat_dirent *de,
121                    char *name, uint8_t attr,
122                    cluster_t first_cluster, uint32_t size);
123
124#endif
125