/
fdisk2.c
583 lines (485 loc) · 16.4 KB
/
fdisk2.c
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
//FDISK - Disk partitionner for Nextor
//This is the extra functions file.
//These functions are called from the main program (fidsk.c)
//by using its CallFunctionInExtraBank function.
// Compilation command line:
//
// sdcc --code-loc 0x4150 --data-loc 0xA000 -mz80 --disable-warning 196 --disable-warning 84 --no-std-crt0 fdisk_crt0.rel msxchar.lib asm.lib fdisk2.c
// hex2bin -e dat fdisk2.ihx
//
// Once compiled, embed the first 8000 bytes of fdisk.dat at position 98560 of the appropriate Nextor ROM file:
// dd if=fdisk2.dat of=nextor.rom bs=1 count=8000 seek=98560
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "../../tools/C/types.h"
#include "../../tools/C/system.h"
#include "../../tools/C/dos.h"
#include "../../tools/C/asmcall.h"
#include "drivercall.h"
#include "../../tools/C/partit.h"
#include "fdisk.h"
byte sectorBuffer[512];
byte sectorBufferBackup[512];
byte ASMRUT[4];
byte OUT_FLAGS;
Z80_registers regs;
byte driverSlot;
byte deviceIndex;
byte selectedLunIndex;
int partitionsCount;
partitionInfo* partitions;
ulong nextDeviceSector;
ulong mainExtendedPartitionSectorCount;
ulong mainExtendedPartitionFirstSector;
uint sectorsPerTrack;
#define Clear(address, len) memset(address, 0, len)
#define ReadSectorFromDevice(driverSlot, deviceIndex, lunIndex, firstDeviceSector) DeviceSectorRW(driverSlot, deviceIndex, lunIndex, firstDeviceSector, 0)
#define WriteSectorToDevice(driverSlot, deviceIndex, lunIndex, firstDeviceSector) DeviceSectorRW(driverSlot, deviceIndex, lunIndex, firstDeviceSector, 1)
int remote_CreateFatFileSystem(byte* callerParameters);
byte CreateFatFileSystem(byte driverSlot, byte deviceIndex, byte lunIndex, ulong firstDeviceSector, ulong fileSystemSizeInK);
void CreateFatBootSector(dosFilesystemParameters* parameters);
ulong GetNewSerialNumber();
void ClearSectorBuffer();
void SectorBootCode();
int remote_CalculateFatFileSystemParameters(byte* callerParameters);
void CalculateFatFileSystemParameters(ulong fileSystemSizeInK, dosFilesystemParameters* parameters);
int CalculateFatFileSystemParametersFat12(ulong fileSystemSizeInK, dosFilesystemParameters* parameters);
int CalculateFatFileSystemParametersFat16(ulong fileSystemSizeInK, dosFilesystemParameters* parameters);
byte DeviceSectorRW(byte driverSlot, byte deviceIndex, byte lunIndex, ulong firstDeviceSector, byte write);
int remote_PreparePartitioningProcess(byte* callerParameters);
int remote_CreatePartition(byte* callerParameters);
int CreatePartition(int index);
int remote_ToggleStatusBit(byte* callerParameters);
int ToggleStatusBit(byte partitionTableEntryIndex, ulong partitonTablesector);
void Locate(byte x, byte y);
//BC = function number (defined in fdisk.h), HL = address of parameters block
int main(int bc, int hl)
{
ASMRUT[0] = 0xC3; //Code for JP
switch(bc) {
case f_CalculateFatFileSystemParameters:
return remote_CalculateFatFileSystemParameters((byte*)hl);
break;
case f_CreateFatFileSystem:
return remote_CreateFatFileSystem((byte*)hl);
break;
case f_PreparePartitioningProcess:
return remote_PreparePartitioningProcess((byte*)hl);
break;
case f_CreatePartition:
return remote_CreatePartition((byte*)hl);
break;
case f_ToggleStatusBit:
return remote_ToggleStatusBit((byte*)hl);
break;
default:
return 0;
}
}
int remote_CreateFatFileSystem(byte* callerParameters)
{
return (int)CreateFatFileSystem(
callerParameters[0],
callerParameters[1],
callerParameters[2],
*((ulong*)&callerParameters[3]),
*((ulong*)&callerParameters[7]));
}
byte CreateFatFileSystem(byte driverSlot, byte deviceIndex, byte lunIndex, ulong firstDeviceSector, ulong fileSystemSizeInK)
{
dosFilesystemParameters parameters;
byte error;
ulong sectorNumber;
uint zeroSectorsToWrite;
uint i;
CalculateFatFileSystemParameters(fileSystemSizeInK, ¶meters);
//* Boot sector
CreateFatBootSector(¶meters);
if((error = WriteSectorToDevice(driverSlot, deviceIndex, lunIndex, firstDeviceSector)) != 0) {
return error;
}
//* FAT (except 1st sector) and root directory sectors
ClearSectorBuffer();
zeroSectorsToWrite = (parameters.sectorsPerFat * FAT_COPIES) + (parameters.sectorsPerRootDirectory) - 1;
sectorNumber = firstDeviceSector + 2;
for(i = 0; i < zeroSectorsToWrite; i++) {
if((error = WriteSectorToDevice(driverSlot, deviceIndex, lunIndex, sectorNumber)) != 0) {
return error;
}
sectorNumber++;
}
//* First sector of each FAT
sectorBuffer[0] = 0xF0;
sectorBuffer[1] = 0xFF;
sectorBuffer[2] = 0xFF;
if(parameters.isFat16) {
sectorBuffer[3] = 0xFF;
}
if((error = WriteSectorToDevice(driverSlot, deviceIndex, lunIndex, firstDeviceSector + 1)) != 0) {
return error;
}
if((error = WriteSectorToDevice(driverSlot, deviceIndex, lunIndex, firstDeviceSector + 1 + parameters.sectorsPerFat)) != 0) {
return error;
}
//* Done
return 0;
}
void CreateFatBootSector(dosFilesystemParameters* parameters)
{
fatBootSector* sector = (fatBootSector*)sectorBuffer;
ClearSectorBuffer();
sector->jumpInstruction[0] = 0xEB;
sector->jumpInstruction[1] = 0xFE;
sector->jumpInstruction[2] = 0x90;
strcpy(sector->oemNameString, "NEXTOR20");
sector->sectorSize = 512;
sector->sectorsPerCluster = parameters->sectorsPerCluster;
sector->reservedSectors = 1;
sector->numberOfFats = FAT_COPIES;
sector->rootDirectoryEntries = parameters->sectorsPerRootDirectory * DIR_ENTRIES_PER_SECTOR;
if((parameters->totalSectors & 0xFFFF0000) == 0) {
sector->smallSectorCount = parameters->totalSectors;
}
sector->mediaId = 0xF0;
sector->sectorsPerFat = parameters->sectorsPerFat;
strcpy(sector->params.standard.volumeLabelString, "NEXTOR 2.0 "); //it is same for DOS 2.20 format
sector->params.standard.serialNumber = GetNewSerialNumber(); //it is same for DOS 2.20 format
sector->mbrSignature = 0xAA55;
if(parameters->isFat16) {
sector->params.standard.bigSectorCount = parameters->totalSectors;
sector->params.standard.extendedBlockSignature = 0x29;
strcpy(sector->params.standard.fatTypeString, "FAT16 ");
} else {
sector->params.DOS220.z80JumpInstruction[0] = 0x18;
sector->params.DOS220.z80JumpInstruction[1] = 0x1E;
strcpy(sector->params.DOS220.volIdString, "VOL_ID");
strcpy(sector->params.DOS220.fatTypeString, "FAT12 ");
memcpy(&(sector->params.DOS220.z80BootCode), SectorBootCode, (uint)0xC090 - (uint)0xC03E);
}
}
ulong GetNewSerialNumber() __naked
{
__asm
ld a,r
xor b
ld e,a
or #128
ld b,a
gnsn_1:
nop
djnz gnsn_1
ld a,r
xor e
ld d,a
or #64
ld b,a
gnsn_2:
nop
nop
djnz gnsn_2
ld a,r
xor d
ld l,a
or #32
ld b,a
gnsn_3:
nop
nop
nop
djnz gnsn_3
ld a,r
xor l
ld h,a
ret
__endasm;
}
void ClearSectorBuffer() __naked
{
__asm
ld hl,#_sectorBuffer
ld de,#_sectorBuffer
inc de
ld bc,#512-1
ld (hl),#0
ldir
ret
__endasm;
}
void SectorBootCode() __naked
{
__asm
ret nc
ld (#0xc07b),de
ld de,#0xc078
ld (hl),e
inc hl
ld (hl),d
ld de,#0xc080
ld c,#0x0f
call #0xf37d
inc a
jp z,#0x4022
ld de,#0x100
ld c,#0x1a
call #0xf37d
ld hl,#1
ld (#0xc08e),hl
ld hl,#0x3f00
ld de,#0xc080
ld c,#0x27
push de
call #0xf37d
pop de
ld c,#0x10
call #0xf37d
jp 0x0100
ld l,b
ret nz
call #0
jp 0x4022
nop
.ascii "MSXDOS SYS"
nop
nop
nop
nop
__endasm;
}
//used only for debugging
int remote_CalculateFatFileSystemParameters(byte* callerParameters)
{
ulong fileSystemSizeInK = *((ulong*)&callerParameters[0]);
dosFilesystemParameters* parameters = *((dosFilesystemParameters**)&callerParameters[4]);
CalculateFatFileSystemParameters(fileSystemSizeInK, parameters);
return 0xB5;
}
void CalculateFatFileSystemParameters(ulong fileSystemSizeInK, dosFilesystemParameters* parameters)
{
if(fileSystemSizeInK > MAX_FAT12_PARTITION_SIZE_IN_K) {
CalculateFatFileSystemParametersFat16(fileSystemSizeInK, parameters);
} else {
CalculateFatFileSystemParametersFat12(fileSystemSizeInK, parameters);
}
}
int CalculateFatFileSystemParametersFat12(ulong fileSystemSizeInK, dosFilesystemParameters* parameters)
{
//Note: Partitions <=16M are defined to have at most 3 sectors per FAT,
//so that they can boot DOS 1. This limits the cluster count to 1021.
uint sectorsPerCluster;
uint sectorsPerFat;
uint clusterCount;
ulong dataSectorsCount;
uint difference;
uint sectorsPerClusterPower;
uint maxClusterCount = MAX_FAT12_CLUSTER_COUNT;
uint maxSectorsPerFat = 12;
if(fileSystemSizeInK <= (2 * (ulong)1024)) {
sectorsPerClusterPower = 0;
sectorsPerCluster = 1;
} else if(fileSystemSizeInK <= (4 * (ulong)1024)) {
sectorsPerClusterPower = 1;
sectorsPerCluster = 2;
} else if(fileSystemSizeInK <= (8 * (ulong)1024)) {
sectorsPerClusterPower = 2;
sectorsPerCluster = 4;
} else if(fileSystemSizeInK <= (16 * (ulong)1024)) {
sectorsPerClusterPower = 3;
sectorsPerCluster = 8;
} else {
sectorsPerClusterPower = 4;
sectorsPerCluster = 16;
}
if(fileSystemSizeInK <= (16 * (ulong)1024)) {
maxClusterCount = 1021;
maxSectorsPerFat = 3;
sectorsPerCluster *= 4;
sectorsPerClusterPower += 2;
}
dataSectorsCount = (fileSystemSizeInK * 2) - (FAT12_ROOT_DIR_ENTRIES / DIR_ENTRIES_PER_SECTOR) - 1;
clusterCount = dataSectorsCount >> sectorsPerClusterPower;
sectorsPerFat = ((uint)clusterCount + 2) * 3;
if((sectorsPerFat & 0x3FF) == 0) {
sectorsPerFat >>= 10;
} else {
sectorsPerFat >>= 10;
sectorsPerFat++;
}
clusterCount = (dataSectorsCount - FAT_COPIES * sectorsPerFat) >> sectorsPerClusterPower;
dataSectorsCount = (uint)clusterCount * (uint)sectorsPerCluster;
if(clusterCount > maxClusterCount) {
difference = clusterCount - maxClusterCount;
clusterCount = maxClusterCount;
sectorsPerFat = maxSectorsPerFat;
dataSectorsCount -= difference * sectorsPerCluster;
}
parameters->totalSectors = dataSectorsCount + 1 + (sectorsPerFat * FAT_COPIES) + (FAT12_ROOT_DIR_ENTRIES / DIR_ENTRIES_PER_SECTOR);
parameters->dataSectors = dataSectorsCount;
parameters->clusterCount = clusterCount;
parameters->sectorsPerFat = sectorsPerFat;
parameters->sectorsPerCluster = sectorsPerCluster;
parameters->sectorsPerRootDirectory = (FAT12_ROOT_DIR_ENTRIES / DIR_ENTRIES_PER_SECTOR);
parameters->isFat16 = false;
return 0;
}
int CalculateFatFileSystemParametersFat16(ulong fileSystemSizeInK, dosFilesystemParameters* parameters)
{
byte sectorsPerCluster;
uint sectorsPerFat;
ulong clusterCount;
ulong dataSectorsCount;
uint sectorsPerClusterPower;
ulong fileSystemSizeInM = fileSystemSizeInK >> 10;
ulong difference;
if(fileSystemSizeInM <= (ulong)128) {
sectorsPerClusterPower = 2;
sectorsPerCluster = 4;
} else if(fileSystemSizeInM <= (ulong)256) {
sectorsPerClusterPower = 3;
sectorsPerCluster = 8;
} else if(fileSystemSizeInM <= (ulong)512) {
sectorsPerClusterPower = 4;
sectorsPerCluster = 16;
} else if(fileSystemSizeInM <= (ulong)1024) {
sectorsPerClusterPower = 5;
sectorsPerCluster = 32;
} else if(fileSystemSizeInM <= (ulong)2048) {
sectorsPerClusterPower = 6;
sectorsPerCluster = 64;
} else {
sectorsPerClusterPower = 7;
sectorsPerCluster = 128;
}
dataSectorsCount = (fileSystemSizeInK * 2) - (FAT16_ROOT_DIR_ENTRIES / DIR_ENTRIES_PER_SECTOR) - 1;
clusterCount = dataSectorsCount >> sectorsPerClusterPower;
sectorsPerFat = (clusterCount + 2) >> 8;
if(((clusterCount + 2) & 0xFF) != 0) {
sectorsPerFat++;
}
clusterCount = (dataSectorsCount - FAT_COPIES * sectorsPerFat);
clusterCount >>= sectorsPerClusterPower;
dataSectorsCount = clusterCount << sectorsPerClusterPower;
if(clusterCount > MAX_FAT16_CLUSTER_COUNT) {
difference = clusterCount - MAX_FAT16_CLUSTER_COUNT;
clusterCount = MAX_FAT16_CLUSTER_COUNT;
sectorsPerFat = 256;
dataSectorsCount -= difference << sectorsPerClusterPower;
}
parameters->totalSectors = dataSectorsCount + 1 + (sectorsPerFat * FAT_COPIES) + (FAT16_ROOT_DIR_ENTRIES / DIR_ENTRIES_PER_SECTOR);
parameters->dataSectors = dataSectorsCount;
parameters->clusterCount = clusterCount;
parameters->sectorsPerFat = sectorsPerFat;
parameters->sectorsPerCluster = sectorsPerCluster;
parameters->sectorsPerRootDirectory = (FAT16_ROOT_DIR_ENTRIES / DIR_ENTRIES_PER_SECTOR);
parameters->isFat16 = true;
return 0;
}
byte DeviceSectorRW(byte driverSlot, byte deviceIndex, byte lunIndex, ulong firstDeviceSector, byte write)
{
regs.Flags.C = write;
regs.Bytes.A = deviceIndex;
regs.Bytes.B = 1;
regs.Bytes.C = lunIndex;
regs.Words.HL = (int)sectorBuffer;
regs.Words.DE = (int)&firstDeviceSector;
DriverCall(driverSlot, DEV_RW);
return regs.Bytes.A;
}
int remote_PreparePartitioningProcess(byte* callerParameters)
{
int i;
int sectorsRemaining;
partitionInfo* partition = &partitions[1];
driverSlot = callerParameters[0];
deviceIndex = callerParameters[1];
selectedLunIndex = callerParameters[2];
partitionsCount = *((uint*)&callerParameters[3]);
partitions = *((partitionInfo**)&callerParameters[5]);
sectorsPerTrack = *((uint*)&callerParameters[7]);
nextDeviceSector = 0;
mainExtendedPartitionSectorCount = 0;
mainExtendedPartitionFirstSector = 0;
for(i = 1; i < partitionsCount; i++) {
mainExtendedPartitionSectorCount += ((&partitions[i])->sizeInK * 2) + 1; //+1 for the MBR
}
return 0;
}
int remote_CreatePartition(byte* callerParameters)
{
return (int)CreatePartition(
*((int*)&callerParameters[0]));
}
int CreatePartition(int index)
{
byte error;
masterBootRecord* mbr = (masterBootRecord*)sectorBuffer;
partitionInfo* partition = &partitions[index];
ulong mbrSector;
uint paddingSectors;
ulong firstFileSystemSector;
ulong extendedPartitionFirstAbsoluteSector;
partitionTableEntry* tableEntry;
ulong x;
mbrSector = nextDeviceSector;
tableEntry = &(mbr->primaryPartitions[0]);
ClearSectorBuffer();
tableEntry->firstAbsoluteSector = 1;
tableEntry->status = partition->status;
tableEntry->partitionType = partition->partitionType;
tableEntry->sectorCount = partition->sizeInK * 2;
firstFileSystemSector = mbrSector + tableEntry->firstAbsoluteSector;
nextDeviceSector += tableEntry->firstAbsoluteSector + tableEntry->sectorCount;
if(index != (partitionsCount - 1)) {
tableEntry++;
tableEntry->partitionType = PARTYPE_EXTENDED_LBA;
tableEntry->firstAbsoluteSector = nextDeviceSector;
if(index == 0) {
mainExtendedPartitionFirstSector = nextDeviceSector;
tableEntry->sectorCount = mainExtendedPartitionSectorCount;
} else {
tableEntry->firstAbsoluteSector -= mainExtendedPartitionFirstSector;
tableEntry->sectorCount = (((partitionInfo*)(partition + 1))->sizeInK * 2);
}
}
if(index == 0) {
mbr->jumpInstruction[0] = 0xEB;
mbr->jumpInstruction[1] = 0xFE;
mbr->jumpInstruction[2] = 0x90;
strcpy(mbr->oemNameString, "NEXTOR20");
}
mbr->mbrSignature = 0xAA55;
x = tableEntry->firstAbsoluteSector; //Without this, firstAbsoluteSector is written to disk as 0. WTF???
tableEntry->firstAbsoluteSector = x;
memcpy(sectorBufferBackup, sectorBuffer, 512);
if((error = WriteSectorToDevice(driverSlot, deviceIndex, selectedLunIndex, mbrSector)) != 0) {
return error;
}
return CreateFatFileSystem(driverSlot, deviceIndex, selectedLunIndex, firstFileSystemSector, partition->sizeInK);
}
int remote_ToggleStatusBit(byte* callerParameters)
{
return (int)ToggleStatusBit(
callerParameters[0],
*((ulong*)&callerParameters[1]));
}
int ToggleStatusBit(byte partitionTableEntryIndex, ulong partitonTablesector)
{
int error;
masterBootRecord* mbr = (masterBootRecord*)sectorBuffer;
partitionTableEntry* entry;
error = ReadSectorFromDevice(driverSlot, deviceIndex, selectedLunIndex, partitonTablesector);
if(error != 0)
return error;
entry =&mbr->primaryPartitions[partitionTableEntryIndex];
entry->status ^= 0x80;
return WriteSectorToDevice(driverSlot, deviceIndex, selectedLunIndex, partitonTablesector);
}
void Locate(byte x, byte y)
{
regs.Bytes.H = x + 1;
regs.Bytes.L = y + 1;
AsmCall(POSIT, ®s, REGS_MAIN, REGS_NONE);
}
#include "../../tools/C/asmcall.c"
#include "drivercall.c"