1/*
2
3mkdos shell tool
4
5Initialize FAT16 or FAT32 partitions, FAT12 floppy disks not supported
6
7Copyright (c) 2002 Marcus Overhagen <marcus@overhagen.de>, OpenBeOS project
8
9Permission is hereby granted, free of charge, to any person obtaining a copy of
10this software and associated documentation files (the "Software"), to deal in
11the Software without restriction, including without limitation the rights to
12use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
13of the Software, and to permit persons to whom the Software is furnished to do
14so, subject to the following conditions:
15
16The above copyright notice and this permission notice shall be included in all
17copies or substantial portions of the Software.
18
19THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25THE SOFTWARE.
26
27*/
28
29#include <fs_interface.h>
30
31
32struct initialize_parameters {
33	uint32	fatBits;
34	uint32	flags;
35	bool	verbose;
36};
37
38#ifdef __cplusplus
39extern "C" {
40#endif
41
42status_t
43dosfs_initialize(int fd, partition_id partitionID, const char* name,
44	const char* parameterString, off_t /*partitionSize*/, disk_job_id job);
45status_t
46dosfs_uninitialize(int fd, partition_id partitionID, off_t partitionSize,
47	uint32 blockSize, disk_job_id job);
48
49#ifdef __cplusplus
50}
51#endif
52
53
54#ifdef MKDOS
55
56#define ATTRIBUTE_PACKED __attribute__((packed))
57
58struct bootsector1216 {
59	uint8 	BS_jmpBoot[3];
60	uint8 	BS_OEMName[8];
61	uint16 	BPB_BytsPerSec;
62	uint8 	BPB_SecPerClus;
63	uint16	BPB_RsvdSecCnt;
64	uint8 	BPB_NumFATs;
65	uint16 	BPB_RootEntCnt;
66	uint16 	BPB_TotSec16;
67	uint8 	BPB_Media;
68	uint16 	BPB_FATSz16;
69	uint16 	BPB_SecPerTrk;
70	uint16 	BPB_NumHeads;
71	uint32 	BPB_HiddSec;
72	uint32 	BPB_TotSec32;
73	uint8 	BS_DrvNum;
74	uint8 	BS_Reserved1;
75	uint8 	BS_BootSig;
76	uint8 	BS_VolID[4];
77	uint8 	BS_VolLab[11];
78	uint8	BS_FilSysType[8];
79	uint8   bootcode[448];
80	uint16	signature;
81} ATTRIBUTE_PACKED;
82
83struct bootsector32 {
84	uint8 	BS_jmpBoot[3];
85	uint8 	BS_OEMName[8];
86	uint16 	BPB_BytsPerSec;
87	uint8 	BPB_SecPerClus;
88	uint16	BPB_RsvdSecCnt;
89	uint8 	BPB_NumFATs;
90	uint16 	BPB_RootEntCnt;
91	uint16 	BPB_TotSec16;
92	uint8 	BPB_Media;
93	uint16 	BPB_FATSz16;
94	uint16 	BPB_SecPerTrk;
95	uint16 	BPB_NumHeads;
96	uint32 	BPB_HiddSec;
97	uint32 	BPB_TotSec32;
98	uint32 	BPB_FATSz32;
99	uint16 	BPB_ExtFlags;
100	uint16 	BPB_FSVer;
101	uint32 	BPB_RootClus;
102	uint16 	BPB_FSInfo;
103	uint16 	BPB_BkBootSec;
104	uint8 	BPB_Reserved[12];
105	uint8 	BS_DrvNum;
106	uint8 	BS_Reserved1;
107	uint8 	BS_BootSig;
108	uint8 	BS_VolID[4];
109	uint8 	BS_VolLab[11];
110	uint8 	BS_FilSysType[8];
111	uint8   bootcode[420];
112	uint16	signature;
113} ATTRIBUTE_PACKED;
114
115struct fsinfosector32 {
116	uint32	FSI_LeadSig;
117	uint8	FSI_Reserved1[480];
118	uint32	FSI_StrucSig;
119	uint32	FSI_Free_Count;
120	uint32	FSI_Nxt_Free;
121	uint8	FSI_Reserved2[12];
122	uint32	FSI_TrailSig;
123} ATTRIBUTE_PACKED;
124
125
126// a FAT directory entry
127struct dirent {
128	uint8 Name[11];
129	uint8 Attr;
130	uint8 NTRes;
131	uint8 CrtTimeTenth;
132	uint16 CrtTime;
133	uint16 CrtDate;
134	uint16 LstAccDate;
135	uint16 FstClusHI;
136	uint16 WrtTime;
137	uint16 WrtDate;
138	uint16 FstClusLO;
139	uint32 FileSize;
140} ATTRIBUTE_PACKED;
141
142
143//maximum size of a 2,88 MB floppy
144#define FLOPPY_MAX_SIZE (2 * 80 * 36 * 512)
145
146//maximum size of a cluster
147#define CLUSTER_MAX_SIZE 32768
148
149//not sure if that's correct
150#define FAT12_CLUSTER_MAX_SIZE	1024
151
152//limits
153#define FAT12_MAX_CLUSTER_COUNT 4084LL
154#define FAT16_MAX_CLUSTER_COUNT 65524LL
155#define FAT32_MAX_CLUSTER_COUNT 0x0fffffffLL
156
157
158#define BOOTJMP_START_OFFSET 0x00
159uint8 bootjmp[] = {
160	0xeb, 0x7e, 0x90
161};
162
163#define BOOTCODE_START_OFFSET 0x80
164uint8 bootcode[] = {
165	0x31, 0xc0, 0x8e, 0xd0, 0xbc, 0x00, 0x7c, 0x8e,
166	0xd8, 0xb4, 0x0e, 0x31, 0xdb, 0xbe, 0x00, 0x7d,
167	0xfc, 0xac, 0x08, 0xc0, 0x74, 0x04, 0xcd, 0x10,
168	0xeb, 0xf7, 0xb4, 0x00, 0xcd, 0x16, 0xcd, 0x19,
169	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
170	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
171	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
172	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
173	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
174	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
175	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
176	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
177	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
178	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
179	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
180	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
181	0x53, 0x6f, 0x72, 0x72, 0x79, 0x2c, 0x20, 0x74,
182	0x68, 0x69, 0x73, 0x20, 0x64, 0x69, 0x73, 0x6b,
183	0x20, 0x69, 0x73, 0x20, 0x6e, 0x6f, 0x74, 0x20,
184	0x62, 0x6f, 0x6f, 0x74, 0x61, 0x62, 0x6c, 0x65,
185	0x2e, 0x0d, 0x0a, 0x50, 0x6c, 0x65, 0x61, 0x73,
186	0x65, 0x20, 0x70, 0x72, 0x65, 0x73, 0x73, 0x20,
187	0x61, 0x6e, 0x79, 0x20, 0x6b, 0x65, 0x79, 0x20,
188	0x74, 0x6f, 0x20, 0x72, 0x65, 0x62, 0x6f, 0x6f,
189	0x74, 0x2e, 0x0d, 0x0a, 0x00
190};
191
192#define BOOT_SECTOR_NUM 0
193#define FSINFO_SECTOR_NUM 1
194#define BACKUP_SECTOR_NUM 6
195#define FAT32_ROOT_CLUSTER 2
196
197#endif
198