-
-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathvsft.c
More file actions
412 lines (334 loc) · 9.18 KB
/
Copy pathvsft.c
File metadata and controls
412 lines (334 loc) · 9.18 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/* Volume size fix tool v1.0
By Konamiman 5/2014
Compilation command line:
sdcc --code-loc 0x180 --data-loc 0 -mz80 --disable-warning 196
--no-std-crt0 crt0_msxdos_advanced.rel
vsft.c
hex2bin -e com vsft.ihx
*/
/* Includes */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include "strcmpi.h"
#include "asmcall.h"
#include "types.h"
#include "dos.h"
/* Typedefs */
typedef struct {
byte jumpInstruction[3];
char oemNameString[8];
uint sectorSize;
byte sectorsPerCluster;
uint reservedSectors;
byte numberOfFats;
uint rootDirectoryEntries;
uint smallSectorCount;
byte mediaId;
uint sectorsPerFat;
uint sectorsPerTrack;
uint numberOfHeads;
union {
struct {
ulong hiddenSectors;
ulong bigSectorCount;
byte physicalDriveNum;
byte reserved;
byte extendedBlockSignature;
ulong serialNumber;
char volumeLabelString[11];
char fatTypeString[8];
} standard;
struct {
uint hiddenSectors;
byte z80JumpInstruction[2];
char volIdString[6];
byte dirtyDiskFlag;
ulong serialNumber;
char volumeLabelString[11];
char fatTypeString[8];
byte z80BootCode;
} DOS220;
} params;
} fatBootSector;
/* Defines */
#define MAX_FAT12_CLUSTER_COUNT 4084
#define MAX_12BIT_CLUSTER_COUNT 4095
#define MAX_FAT16_CLUSTER_COUNT 65524
/* Strings */
const char* strTitle=
"Volume Size Fix Tool v1.0\r\n"
"By Konamiman, 5/2014\r\n"
"\r\n";
const char* strUsage=
"Usage: vsft <drive>: [fix] \r\n"
"\r\n"
"This tool checks the cluster count calculated by DOS for a given volume\r\n"
"and offers the possibility of fixing it if it is over the standard limits\r\n"
"(4084 clusters for FAT12, 65524 clusters for FAT16)\r\n"
"by appropriately reducing the volume size in the boot sector.\r\n"
"\r\n"
"Run the tool as 'vsft <drive>:' first, and if it says that a fix is needed,\r\n"
"run again adding the \"fix\" parameter to actually perform the fix.\r\n"
"\r\n"
"Fixing FAT16 volumes is supported when running the tool in Nextor.\r\n"
"DO NOT try to fix a FAT16 volume when running MSX-DOS with a FAT16 patch!\r\n";
const char* strInvParam = "Invalid parameter";
const char* strCRLF = "\r\n";
/* Global variables */
byte ASMRUT[4];
byte OUT_FLAGS;
Z80_registers regs;
bool isNextor;
fatBootSector* Buffer = (fatBootSector*)0x8000;
bool isFat16;
int driveNumber; //0=A:, etc
bool doFix;
uint totalClusters;
ulong totalSectors;
byte sectorsPerCluster;
int sectorsToDecreaseForFix;
/* Some handy code defines */
#define PrintNewLine() printf(strCRLF)
#define StringIs(a, b) (strcmpi(a,b)==0)
/* Function prototypes */
void Terminate(const char* errorMessage);
void TerminateWithDosError(byte errorCode);
void CheckDosVersion();
void ExtractParameters(char** argv, int argc);
void GetDriveInfo();
void PrintDriveInfo();
void PrintSizeInK(unsigned long size);
void CalculateSectorsToDecreaseForFix();
void DoFix();
void PrintFixInfo();
void ReadBootSector();
void FixVolumeSize();
void WritebootSector();
void print(char* s);
/* MAIN */
int main(char** argv, int argc)
{
ASMRUT[0] = 0xC3;
print(strTitle);
if(argc == 0) {
print(strUsage);
Terminate(null);
}
CheckDosVersion();
ExtractParameters(argv, argc);
GetDriveInfo();
PrintDriveInfo();
CalculateSectorsToDecreaseForFix();
if(sectorsToDecreaseForFix == 0) {
print("\r\nVolume size is correct. No need to fix it.\r\n");
Terminate(null);
} else if(doFix) {
DoFix();
} else {
PrintFixInfo();
}
Terminate(null);
return 0;
}
/* Functions */
void Terminate(const char* errorMessage)
{
if(errorMessage != NULL) {
printf("\r\x1BK*** %s\r\n", errorMessage);
}
regs.Bytes.B = (errorMessage == NULL ? 0 : 1);
DosCall(_TERM, ®s, REGS_MAIN, REGS_NONE);
}
void TerminateWithDosError(byte errorCode)
{
regs.Bytes.B = errorCode;
DosCall(_TERM, ®s, REGS_MAIN, REGS_NONE);
}
void CheckDosVersion()
{
regs.Bytes.B = 0x5A;
regs.Words.HL = 0x1234;
regs.UWords.DE = 0xABCD;
regs.Words.IX = 0;
DosCall(_DOSVER, ®s, REGS_ALL, REGS_ALL);
if(regs.Bytes.B < 2) {
Terminate("This program is for MSX-DOS 2 only.");
}
isNextor = (regs.Bytes.IXh == 1);
}
void ExtractParameters(char** argv, int argc)
{
if(argc > 2) {
Terminate(strInvParam);
}
if(strlen(argv[0]) > 2 || argv[0][1] != ':') {
Terminate(strInvParam);
}
driveNumber = (argv[0][0] | 32) - 'a';
if(driveNumber < 0 || driveNumber > ('h'-'a')) {
Terminate(strInvParam);
}
if(argc == 2) {
if(StringIs(argv[1], "fix")) {
doFix = true;
} else {
Terminate(strInvParam);
}
} else {
doFix = false;
}
}
void GetDriveInfo()
{
regs.Words.DE = (int)Buffer;
regs.Bytes.L = driveNumber + 1;
DosCall(_DPARM, ®s, REGS_MAIN, REGS_MAIN);
if(regs.Bytes.A != 0) {
TerminateWithDosError(regs.Bytes.A);
}
if(isNextor) {
totalSectors = *((ulong*)((byte*)Buffer+24));
isFat16 = (*((byte*)Buffer+28) == 1) ? true : false;
} else {
totalSectors = *((uint*)((byte*)Buffer+9));
isFat16 = false;
}
if(totalSectors == 0) {
Terminate("Reported volume size is zero (are you trying to access a FAT16 volume from MSX-DOS?)");
}
regs.Bytes.E = driveNumber + 1;
DosCall(_ALLOC, ®s, REGS_MAIN, REGS_MAIN);
totalClusters = regs.UWords.DE;
sectorsPerCluster = regs.Bytes.A;
}
void PrintDriveInfo()
{
printf("Drive: %c:\r\n", driveNumber+'A');
printf("Size: "); PrintSizeInK(totalSectors/2); PrintNewLine();
printf("Cluster count: %u\r\n", totalClusters);
if(sectorsPerCluster == 1) {
print("Cluster size: 512 bytes\r\n");
} else {
printf("Cluster size: %i KBytes\r\n", sectorsPerCluster / 2);
}
if(isNextor) {
printf("Filesystem: %s\r\n", isFat16 ? "FAT16" : "FAT12");
} else if(totalClusters > MAX_12BIT_CLUSTER_COUNT) {
Terminate("Cluster count does not fit in 12 bits - this is not supposed to be supported by MSX-DOS!");
} else {
print("Filesystem: FAT12 assumed (MSX-DOS does not support anything else)\r\n");
}
}
void PrintSizeInK(unsigned long size)
{
int remaining;
if(size < 1024) {
printf("%i KBytes", size);
return;
}
remaining = size & 1023;
if(remaining > 1000) {
remaining = 999;
}
size >>= 10;
if(size < 1024) {
printf("%i.%i MBytes", (int)size, remaining/100);
return;
}
remaining = size & 1023;
if(remaining > 1000) {
remaining = 999;
}
size >>= 10;
printf("%i.%i GBytes", (int)size, remaining/100);
}
void CalculateSectorsToDecreaseForFix()
{
if(isFat16 && (totalClusters > MAX_FAT16_CLUSTER_COUNT)) {
sectorsToDecreaseForFix = (totalClusters - MAX_FAT16_CLUSTER_COUNT) * sectorsPerCluster;
} else if(!isFat16 && (totalClusters > MAX_FAT12_CLUSTER_COUNT)) {
sectorsToDecreaseForFix = (totalClusters - MAX_FAT12_CLUSTER_COUNT) * sectorsPerCluster;
} else {
sectorsToDecreaseForFix = 0;
}
}
void DoFix()
{
ReadBootSector();
FixVolumeSize();
WritebootSector();
print("Fix applied!\r\n");
}
void PrintFixInfo()
{
printf("\r\nCluster count exceeds the maximum allowed (%u).\r\n",
(isFat16 ? MAX_FAT16_CLUSTER_COUNT : MAX_FAT12_CLUSTER_COUNT));
printf("This can be fixed by reducing the volume size by %i KBytes.\r\n",
sectorsToDecreaseForFix/2);
print("Run the tool again adding the 'fix' parameter to apply the fix.\r\n");
}
void ReadBootSector()
{
print("\r\nReading boot sector...\r\n");
regs.Words.DE = (int)Buffer;
DosCall(_SETDTA, ®s, REGS_MAIN, REGS_MAIN);
if(isNextor) {
regs.Bytes.A = driveNumber;
regs.Bytes.B = 1;
regs.Words.HL = 0;
regs.Words.DE = 0;
DosCall(_RDDRV, ®s, REGS_MAIN, REGS_MAIN);
} else {
regs.Bytes.L = driveNumber;
regs.Bytes.H = 1;
regs.Words.DE = 0;
DosCall(_RDABS, ®s, REGS_MAIN, REGS_MAIN);
}
if(regs.Bytes.A != 0) {
TerminateWithDosError(regs.Bytes.A);
}
}
void FixVolumeSize()
{
ulong totalSectors;
totalSectors = Buffer->smallSectorCount;
if(totalSectors == 0) {
totalSectors = Buffer->params.standard.bigSectorCount;
}
totalSectors -= sectorsToDecreaseForFix;
if(totalSectors <= 0xFFFF) {
Buffer->smallSectorCount = (uint)totalSectors;
} else {
Buffer->smallSectorCount = 0;
Buffer->params.standard.bigSectorCount = totalSectors;
}
}
void WritebootSector()
{
print("Writing updated boot sector...\r\n");
regs.Words.DE = (int)Buffer;
DosCall(_SETDTA, ®s, REGS_MAIN, REGS_MAIN);
if(isNextor) {
regs.Bytes.A = driveNumber;
regs.Bytes.B = 1;
regs.Words.HL = 0;
regs.Words.DE = 0;
DosCall(_WRDRV, ®s, REGS_MAIN, REGS_MAIN);
} else {
regs.Bytes.L = driveNumber;
regs.Bytes.H = 1;
regs.Words.DE = 0;
DosCall(_WRABS, ®s, REGS_MAIN, REGS_MAIN);
}
if(regs.Bytes.A != 0) {
TerminateWithDosError(regs.Bytes.A);
}
}
#define COM_FILE
#include "print_msxdos.c"
#include "printf.c"
#include "asmcall.c"
#include "strcmpi.c"