File: fat.h

package info (click to toggle)
makebootfat 1.4-8
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • size: 2,804 kB
  • sloc: sh: 3,118; ansic: 2,746; asm: 148; makefile: 92
file content (185 lines) | stat: -rw-r--r-- 5,997 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
 * This file is part of the Advance project.
 *
 * Copyright (C) 2004, 2005 Andrea Mazzoleni
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details. 
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#ifndef __FAT_H
#define __FAT_H

#include "disk.h"

#define FAT_CLUSTER_SIZE (SECTOR_SIZE*128)

#define FAT_TABLE_FREE 0
#define FAT_TABLE_EOC 0xFFFFFFFF
#define FAT_TABLE_BAD 0xFFFFFFF7

/**
 * FAT information.
 */
struct fat_info {
	unsigned fat_bit; /**< Number of bit in the fat. */
	unsigned fat_num; /**< Number of fat table. */
	unsigned fat_pos; /**< Fat table position (in sectors). */
	unsigned fat_size; /**< Fat size (in sectors). */
	unsigned data_pos; /**< Data position (in sectors). */
	unsigned data_size; /**< Data size (in sectors). */
	unsigned cluster_num; /**< Number of clusters. */
	unsigned cluster_size; /**< Number of sectors per cluster. */
	unsigned root_pos; /**< Root dir position (in sectors). */
	unsigned root_size; /**< Root dir size (in sectors). */
};

/**
 * FAT internal context.
 */
struct fat_context {
	struct disk_handle* h; ///< Handle of the device.
	unsigned h_pos; ///< Offset in the device (in sectors).
	unsigned h_size; ///< Size in the device (in sectors).

	unsigned char* table; /**< Fat table. */

	unsigned char tmp[FAT_CLUSTER_SIZE]; /**< Temporary data. */

	struct fat_info info; /**< Fat information. */

	struct disk_geometry geometry; /**< Disk geometry. */

	unsigned first_free; /**< First free cluster. */
};

struct fat_boot_sector {
	unsigned char BS_JumpBoot[3];
	char BS_OemName[8];
	unsigned char BPB_BytesPerSec[2];
	unsigned char BPB_SecPerClus;
	unsigned char BPB_RsvdSecCnt[2];
	unsigned char BPB_NumFATs;
	unsigned char BPB_RootEntCnt[2];
	unsigned char BPB_TotSec16[2];
	unsigned char BPB_Media;
	unsigned char BPB_FATSz16[2];
	unsigned char BPB_SecPerTrk[2];
	unsigned char BPB_NumHeads[2];
	unsigned char BPB_HiddSec[4];
	unsigned char BPB_TotSec32[4];
	union {
		struct {
			unsigned char BS_DrvNum;
			unsigned char BS_Reserved1;
			unsigned char BS_BootSig;
			unsigned char BS_VolID[4];
			char BS_VolLab[11];
			char BS_FilSysType[8];
			unsigned char BS_Code[448];
		} fat16;
		struct {
			unsigned char BPB_FATSz32[4];
			unsigned char BPB_ExtFlags[2];
			unsigned char BPB_FSVer[2];
			unsigned char BPB_RootClus[4];
			unsigned char BPB_FSInfo[2];
			unsigned char BPB_BkBootSec[2];
			unsigned char BPB_Reserved[12];
			unsigned char BS_DrvNum;
			unsigned char BS_Reserved1;
			unsigned char BS_BootSig;
			unsigned char BS_VolID[4];
			char BS_VolLab[11];
			char BS_FilSysType[8];
			unsigned char BS_Code[420];
		} fat32;
	} bit;
	unsigned char BS_Sign[2];
};

struct fat_fsinfo {
	unsigned char FSI_LeadSig[4];
	unsigned char FSI_Reserved1[480];
	unsigned char FSI_StrucSig[4];
	unsigned char FSI_Free_Count[4];
	unsigned char FSI_Nxt_Free[4];
	unsigned char FSI_Reserved2[12];
	unsigned char FSI_TrailSig[4];
};

#define FAT_ATTRIB_READONLY 0x01 
#define FAT_ATTRIB_HIDDEN 0x02 
#define FAT_ATTRIB_SYSTEM 0x04 
#define FAT_ATTRIB_VOLUME 0x08 
#define FAT_ATTRIB_DIRECTORY 0x10
#define FAT_ATTRIB_ARCHIVE 0x20
#define FAT_ATTRIB_LONGNAME (FAT_ATTRIB_READONLY | FAT_ATTRIB_HIDDEN | FAT_ATTRIB_SYSTEM | FAT_ATTRIB_VOLUME)

#define FAT_DIRENTRY_SIZE 32

/**
 * FAT directory entry.
 */
struct fat_direntry {
	char DE_Name[11];
	unsigned char DE_Attributes;
	unsigned char DE_Reserved;
	unsigned char DE_CrtTimeTenth;
	unsigned char DE_CrtTime[2];
	unsigned char DE_CrtDate[2];
	unsigned char DE_LstAccDate[2];
	unsigned char DE_ClusterH[2];
	unsigned char DE_WrtTime[2];
	unsigned char DE_WrtDate[2];
	unsigned char DE_ClusterL[2];
	unsigned char DE_FileSize[4];
};

/**
 * FAT directory entry for long names.
 */
struct fat_longentry {
	unsigned char DE_Ordinal;
	unsigned char DE_Name0[2];
	unsigned char DE_Name1[2];
	unsigned char DE_Name2[2];
	unsigned char DE_Name3[2];
	unsigned char DE_Name4[2];
	unsigned char DE_Attributes;
	unsigned char DE_Reserved;
	unsigned char DE_CheckSum;
	unsigned char DE_Name5[2];
	unsigned char DE_Name6[2];
	unsigned char DE_Name7[2];
	unsigned char DE_Name8[2];
	unsigned char DE_Name9[2];
	unsigned char DE_Name10[2];
	unsigned char DE_ClusterL[2];
	unsigned char DE_Name11[2];
	unsigned char DE_Name12[2];
};

struct fat_context* fat_open(struct disk_handle* h, unsigned pos, unsigned size, const struct disk_geometry* geometry);
int fat_close(struct fat_context* fat);
int fat_format(struct fat_context* fat, unsigned size, unsigned bit, unsigned sector_per_cluster, const char* oem, const char* label, unsigned serial, const struct disk_geometry* geometry);
int fat_entry_add(struct fat_context* fat, unsigned dir_cluster, const char* file_name, unsigned file_cluster, unsigned file_size, unsigned file_attrib, time_t file_time);
int fat_cluster_dir(struct fat_context* fat, unsigned root_cluster, unsigned* cluster, time_t time);
int fat_cluster_file(struct fat_context* fat, const char* file, unsigned* cluster, unsigned* size);
int fat_cluster_chain(struct fat_context* fat, unsigned cluster, unsigned* cluster_map, unsigned cluster_max);
int fat_sector_chain(struct fat_context* fat, unsigned cluster, unsigned* sector_map, unsigned sector_max);
int fat_boot_setup(unsigned char* boot, const unsigned char* code, unsigned bit);

#endif