-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathTESTREAD.C
119 lines (104 loc) · 2.69 KB
/
TESTREAD.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
/* Read first 4 sectors of track 0 to check 8ISR applied sector size
1KB buffer filler byte 0xCC for unused/unread data
Memory model: TINY */
#include <stdio.h>
#include <stdlib.h>
#include <dos.h>
#include <ctype.h>
#include <string.h>
unsigned char nDriveNumber = 0;
const unsigned char nSectorInfo[] = "\nTrack 0, sector %u likely contains %s";
void ReadSector(unsigned char nSector)
{
union REGS regs;
struct SREGS sregs;
FILE* pFile;
char sFileName[12] = {0};
unsigned char* pBuffer = malloc(1024);
if (!pBuffer)
{
printf("\nMemory allocation error\n");
exit(-1);
}
memset(pBuffer, 0xCC, 1024);
regs.x.ax = 0;
regs.h.dl = nDriveNumber;
int86(0x13, ®s, ®s);
regs.h.ah = 2;
regs.h.al = 1;
regs.h.ch = 0;
regs.h.cl = nSector;
regs.h.dh = 0;
regs.h.dl = nDriveNumber;
regs.x.bx = FP_OFF(pBuffer);
sregs.es = FP_SEG(pBuffer);
int86x(0x13, ®s, ®s, &sregs);
if (regs.x.cflag > 0)
{
printf("\nINT 13h AH=02 Read sector %d failed for drive %u (%c:)",
nSector, nDriveNumber, nDriveNumber+0x41);
free(pBuffer);
return;
}
/* Filler byte 0xCC for unread data */
sprintf(sFileName, "SECTOR%u.BIN", nSector);
pFile = fopen(sFileName, "wb");
if (!pFile)
{
printf("\nCannot create file %s", sFileName);
free(pBuffer);
return;
}
if ((pBuffer[0] == 0xCC) && (pBuffer[1023] == 0xCC))
{
printf(nSectorInfo, nSector, "no valid data");
}
else if (pBuffer[128] == 0xCC)
{
printf(nSectorInfo, nSector, "a 128-byte sector");
printf(" (%s)", sFileName);
fwrite(pBuffer, 128, 1, pFile);
}
else if (pBuffer[256] == 0xCC)
{
printf(nSectorInfo, nSector, "a 256-byte sector");
printf(" (%s)", sFileName);
fwrite(pBuffer, 256, 1, pFile);
}
else if (pBuffer[512] == 0xCC)
{
printf(nSectorInfo, nSector, "a 512-byte sector");
printf(" (%s)", sFileName);
fwrite(pBuffer, 512, 1, pFile);
}
else
{
printf(nSectorInfo, nSector, "a 1024-byte sector");
printf(" (%s)", sFileName);
fwrite(pBuffer, 1024, 1, pFile);
}
fclose(pFile);
free(pBuffer);
}
int main(int argc, char* argv[])
{
const unsigned char* pArgument;
if (argc != 2)
{
printf("\nSpecify floppy drive (A: to D:)\n");
return 0;
}
pArgument = strupr(argv[1]);
if ((strlen(pArgument) > 2) || (pArgument[0] < 0x41) || (pArgument[0] > 0x44))
{
printf("\nInvalid drive specified\n");
return -1;
}
nDriveNumber = pArgument[0] - 0x41;
ReadSector(1);
ReadSector(2);
ReadSector(3);
ReadSector(4);
printf("\n");
return 0;
}