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#define ATTRIBUTE_PACKED __attribute__((packed))
30
31struct bootsector1216 {
32	uint8 	BS_jmpBoot[3];
33	uint8 	BS_OEMName[8];
34	uint16 	BPB_BytsPerSec;
35	uint8 	BPB_SecPerClus;
36	uint16	BPB_RsvdSecCnt;
37	uint8 	BPB_NumFATs;
38	uint16 	BPB_RootEntCnt;
39	uint16 	BPB_TotSec16;
40	uint8 	BPB_Media;
41	uint16 	BPB_FATSz16;
42	uint16 	BPB_SecPerTrk;
43	uint16 	BPB_NumHeads;
44	uint32 	BPB_HiddSec;
45	uint32 	BPB_TotSec32;
46	uint8 	BS_DrvNum;
47	uint8 	BS_Reserved1;
48	uint8 	BS_BootSig;
49	uint8 	BS_VolID[4];
50	uint8 	BS_VolLab[11];
51	uint8	BS_FilSysType[8];
52	uint8   bootcode[448];
53	uint16	signature;
54} ATTRIBUTE_PACKED;
55
56struct bootsector32 {
57	uint8 	BS_jmpBoot[3];
58	uint8 	BS_OEMName[8];
59	uint16 	BPB_BytsPerSec;
60	uint8 	BPB_SecPerClus;
61	uint16	BPB_RsvdSecCnt;
62	uint8 	BPB_NumFATs;
63	uint16 	BPB_RootEntCnt;
64	uint16 	BPB_TotSec16;
65	uint8 	BPB_Media;
66	uint16 	BPB_FATSz16;
67	uint16 	BPB_SecPerTrk;
68	uint16 	BPB_NumHeads;
69	uint32 	BPB_HiddSec;
70	uint32 	BPB_TotSec32;
71	uint32 	BPB_FATSz32;
72	uint16 	BPB_ExtFlags;
73	uint16 	BPB_FSVer;
74	uint32 	BPB_RootClus;
75	uint16 	BPB_FSInfo;
76	uint16 	BPB_BkBootSec;
77	uint8 	BPB_Reserved[12];
78	uint8 	BS_DrvNum;
79	uint8 	BS_Reserved1;
80	uint8 	BS_BootSig;
81	uint8 	BS_VolID[4];
82	uint8 	BS_VolLab[11];
83	uint8 	BS_FilSysType[8];
84	uint8   bootcode[420];
85	uint16	signature;
86} ATTRIBUTE_PACKED;
87
88struct fsinfosector32 {
89	uint32	FSI_LeadSig;
90	uint8	FSI_Reserved1[480];
91	uint32	FSI_StrucSig;
92	uint32	FSI_Free_Count;
93	uint32	FSI_Nxt_Free;
94	uint8	FSI_Reserved2[12];
95	uint32	FSI_TrailSig;
96} ATTRIBUTE_PACKED;
97
98
99// a FAT directory entry
100struct dirent {
101	uint8 Name[11];
102	uint8 Attr;
103	uint8 NTRes;
104	uint8 CrtTimeTenth;
105	uint16 CrtTime;
106	uint16 CrtDate;
107	uint16 LstAccDate;
108	uint16 FstClusHI;
109	uint16 WrtTime;
110	uint16 WrtDate;
111	uint16 FstClusLO;
112	uint32 FileSize;
113} ATTRIBUTE_PACKED;
114
115
116//maximum size of a 2,88 MB floppy
117#define FLOPPY_MAX_SIZE (2 * 80 * 36 * 512)
118
119//maximum size of a cluster
120#define CLUSTER_MAX_SIZE 32768
121
122//not sure if that's correct
123#define FAT12_CLUSTER_MAX_SIZE	1024
124
125//limits
126#define FAT12_MAX_CLUSTER_COUNT 4084LL
127#define FAT16_MAX_CLUSTER_COUNT 65524LL
128#define FAT32_MAX_CLUSTER_COUNT 0x0fffffffLL
129
130
131#define BOOTJMP_START_OFFSET 0x00
132uint8 bootjmp[] = {
133	0xeb, 0x7e, 0x90
134};
135
136#define BOOTCODE_START_OFFSET 0x80
137uint8 bootcode[] = {
138	0x31, 0xc0, 0x8e, 0xd0, 0xbc, 0x00, 0x7c, 0x8e,
139	0xd8, 0xb4, 0x0e, 0x31, 0xdb, 0xbe, 0x00, 0x7d,
140	0xfc, 0xac, 0x08, 0xc0, 0x74, 0x04, 0xcd, 0x10,
141	0xeb, 0xf7, 0xb4, 0x00, 0xcd, 0x16, 0xcd, 0x19,
142	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
143	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
144	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
145	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
146	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
147	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
148	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
149	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
150	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
151	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
152	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
153	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
154	0x53, 0x6f, 0x72, 0x72, 0x79, 0x2c, 0x20, 0x74,
155	0x68, 0x69, 0x73, 0x20, 0x64, 0x69, 0x73, 0x6b,
156	0x20, 0x69, 0x73, 0x20, 0x6e, 0x6f, 0x74, 0x20,
157	0x62, 0x6f, 0x6f, 0x74, 0x61, 0x62, 0x6c, 0x65,
158	0x2e, 0x0d, 0x0a, 0x50, 0x6c, 0x65, 0x61, 0x73,
159	0x65, 0x20, 0x70, 0x72, 0x65, 0x73, 0x73, 0x20,
160	0x61, 0x6e, 0x79, 0x20, 0x6b, 0x65, 0x79, 0x20,
161	0x74, 0x6f, 0x20, 0x72, 0x65, 0x62, 0x6f, 0x6f,
162	0x74, 0x2e, 0x0d, 0x0a, 0x00
163};
164
165#define BOOT_SECTOR_NUM 0
166#define FSINFO_SECTOR_NUM 1
167#define BACKUP_SECTOR_NUM 6
168#define FAT32_ROOT_CLUSTER 2
169
170