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