-
Notifications
You must be signed in to change notification settings - Fork 0
/
sector.c
353 lines (297 loc) · 9.06 KB
/
sector.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
/*-------------------------------------------------------------------*/
/* SECTOR.C read/write of Sectors on Floppys and HD's */
/*-------------------------------------------------------------------*/
#define INCL_BASE
#define INCL_DOSDEVIOCTL
#include <os2.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define INCL_32
#define INCL_REXXSAA
#include <rexxsaa.h>
#define BUILDRXSTRING(t, s) {strcpy((t)->strptr,(s)); (t)->strlength=strlen((s)); }
RexxFunctionHandler QDrive;
RexxFunctionHandler QPDisk;
RexxFunctionHandler ReadSect;
RexxFunctionHandler WritSect;
RexxFunctionHandler ReadPSect;
typedef _Packed struct
{
USHORT bytesSector;
UCHAR sectorsCluster;
USHORT reservedSectors;
UCHAR numFats;
USHORT rootDirEntries;
USHORT totallSectors;
UCHAR mediaDescriptor;
USHORT sectorsFat;
USHORT sectorsTrack;
USHORT numHeads;
ULONG hiddenSectors;
ULONG largeTotalSectors;
CHAR reserved[6];
USHORT numCylinders;
UCHAR type;
USHORT attributes;
} diskLDevice;
typedef _Packed struct
{
USHORT reserved1;
USHORT numCylinders;
USHORT numHeads;
USHORT sectorsTrack;
USHORT reserved2;
USHORT reserved3;
USHORT reserved4;
USHORT reserved5;
} diskPDevice;
typedef struct
{
UCHAR command;
UCHAR drive;
} parmList;
void error(char *msg, int error);
/*-------------------------------------------------------------------*/
/* QDRIVE - Query number of sectors */
/*-------------------------------------------------------------------*/
ULONG QDrive(PUCHAR Name, ULONG argc, RXSTRING argv[], PSZ Queuename, PRXSTRING Retstr)
{
char ret[100];
char *szDrive;
HFILE fhFile;
diskLDevice device;
ULONG deviceLength = sizeof(device);
parmList parm;
ULONG parmLength = sizeof(parm);
ULONG ulAction;
APIRET rc;
szDrive = argv[0].strptr;
rc = DosOpen(szDrive, &fhFile, &ulAction, 0L, FILE_NORMAL,
OPEN_ACTION_OPEN_IF_EXISTS,
OPEN_FLAGS_DASD |
OPEN_FLAGS_WRITE_THROUGH |
OPEN_FLAGS_NOINHERIT |
OPEN_SHARE_DENYREADWRITE |
OPEN_ACCESS_READONLY, 0L);
if (rc) {
sprintf(ret, "Error: %d (DosOpen)", rc);
BUILDRXSTRING(Retstr, ret);
return(0);
}
parm.command = 1;
rc = DosDevIOCtl(fhFile, 0x08L, 0x63L,
&parm, parmLength, &parmLength,
&device, deviceLength, &deviceLength);
if (rc) {
sprintf(ret, "Error: %d (DosDevIOCtl,8,63)", rc);
BUILDRXSTRING(Retstr, ret);
return(0);
}
rc = DosClose(fhFile);
if (rc) {
sprintf(ret, "Error: %d (DosClose)", rc);
BUILDRXSTRING(Retstr, ret);
return(0);
}
rc = max(device.largeTotalSectors, device.totallSectors);
sprintf(ret, "%d %d %d %d %d", rc, device.numCylinders, device.numHeads,
device.sectorsTrack, device.bytesSector);
BUILDRXSTRING(Retstr, ret);
return(0);
}
ULONG ReadSect(PUCHAR Name, ULONG argc, RXSTRING argv[], PSZ Queuename, PRXSTRING Retstr)
{
int sec_no;
char *szDrive;
HFILE fhFile;
ULONG BytesRead, ulAction, NewPtr;
char buffer[515], ret[100];
APIRET rc;
if (argc != 2)
return 40;
szDrive = argv[0].strptr;
sec_no = atoi(argv[1].strptr);
rc = DosOpen(szDrive, &fhFile, &ulAction, 0L, FILE_NORMAL,
OPEN_ACTION_OPEN_IF_EXISTS,
OPEN_FLAGS_DASD |
OPEN_FLAGS_WRITE_THROUGH |
OPEN_FLAGS_NOINHERIT |
OPEN_SHARE_DENYREADWRITE |
OPEN_ACCESS_READONLY , 0L);
if (rc){
sprintf(ret, "Error: %d (DosOpen)", rc);
Retstr->strptr=ret;
Retstr->strlength=strlen(ret);
return 0;
}
rc = DosSetFilePtr(fhFile, sec_no*512,0, &NewPtr);
if (rc){
DosClose(fhFile);
sprintf(ret, "Error: %d (DosSetFilePtr)", rc);
Retstr->strptr=ret;
Retstr->strlength=strlen(ret);
return 0;
}
rc = DosRead(fhFile, buffer, 512, &BytesRead);
if (rc){
DosClose(fhFile);
sprintf(ret, "Error: %d (DosRead)", rc);
Retstr->strptr=ret;
Retstr->strlength=strlen(ret);
return 0;
}
rc = DosClose(fhFile);
Retstr->strptr=buffer;
Retstr->strlength=512;
return 0;
}
ULONG WritSect(PUCHAR Name, ULONG argc, RXSTRING argv[], PSZ Queuename, PRXSTRING Retstr)
{
char *szDrive, *ptr;
UCHAR ret[100];
HFILE fhFile;
ULONG sec_no, BytesRead, ulAction, NewPtr;
APIRET rc;
if (argc != 3)
return 40;
szDrive = argv[0].strptr;
sec_no = atoi(argv[1].strptr);
ptr = argv[2].strptr;
rc = DosOpen(szDrive, &fhFile, &ulAction, 0L, FILE_NORMAL,
OPEN_ACTION_OPEN_IF_EXISTS,
OPEN_FLAGS_DASD |
OPEN_FLAGS_WRITE_THROUGH |
OPEN_FLAGS_NOINHERIT |
OPEN_SHARE_DENYREADWRITE |
OPEN_ACCESS_READWRITE, 0L);
if (rc){
sprintf(ret, "Error: %d (DosOpen)", rc);
Retstr->strptr=ret;
Retstr->strlength=strlen(ret);
return 0;
}
rc = DosDevIOCtl(fhFile, 0x08L, 0x00L, 0, 0, 0, 0, 0, 0);
if (rc){
DosClose(fhFile);
sprintf(ret, "Error: %d (DosDevIOCtl 8,0)", rc);
Retstr->strptr=ret;
Retstr->strlength=strlen(ret);
return 0;
}
rc = DosSetFilePtr(fhFile, sec_no*512,0, &NewPtr);
if (rc){
DosDevIOCtl(fhFile, 0x08L, 0x01L, 0, 0, 0, 0, 0, 0);
DosClose(fhFile);
sprintf(ret, "Error: %d (DosSetFilePtr)", rc);
Retstr->strptr=ret;
Retstr->strlength=strlen(ret);
return 0;
}
rc = DosWrite(fhFile, ptr, 512, &BytesRead);
if (rc){
DosDevIOCtl(fhFile, 0x08L, 0x01L, 0, 0, 0, 0, 0, 0);
DosClose(fhFile);
sprintf(ret, "Error: %d (DosWrite)", rc);
Retstr->strptr=ret;
Retstr->strlength=strlen(ret);
return 0;
}
rc = DosDevIOCtl(fhFile, 0x08L, 0x01L, 0, 0, 0, 0, 0, 0);
rc = DosClose(fhFile);
Retstr->strlength=1;
return 0;
}
ULONG QPDisk(PUCHAR Name, ULONG argc, RXSTRING argv[], PSZ Queuename, PRXSTRING Retstr)
{
char ret[100];
char *szDisk;
HFILE fhFile;
diskPDevice device;
ULONG deviceLength = sizeof(device);
parmList parm;
ULONG parmLength = sizeof(parm);
ULONG ulAction;
APIRET rc;
szDisk = argv[0].strptr;
rc = DosPhysicalDisk(2, &fhFile, 2, szDisk ,3);
if (rc){
sprintf(ret, "Error: %d (DosPhysicalDisk,2)", rc);
Retstr->strptr=ret;
Retstr->strlength=strlen(ret);
return 0;
}
parm.command = 1;
rc = DosDevIOCtl(fhFile, 0x09L, 0x63L,
&parm, parmLength, &parmLength,
&device, deviceLength, &deviceLength);
if (rc){
sprintf(ret, "Error: %d (DosDecIOCtl,9,63)", rc);
Retstr->strptr=ret;
Retstr->strlength=strlen(ret);
return 0;
}
rc = DosPhysicalDisk(3, NULL, 0, (PVOID) &fhFile , 2);
if (rc){
sprintf(ret, "Error: %d (DosPhysicalDisk,3)", rc);
Retstr->strptr=ret;
Retstr->strlength=strlen(ret);
return 0;
}
sprintf(ret, "%d %d %d", device.numCylinders, device.numHeads,
device.sectorsTrack);
BUILDRXSTRING(Retstr, ret);
return(0);
}
ULONG ReadPSect(PUCHAR Name, ULONG argc, RXSTRING argv[], PSZ Queuename, PRXSTRING Retstr)
{
HFILE fhFile;
UCHAR TrackBuf[sizeof(TRACKLAYOUT)+256*2*sizeof(USHORT)];
TRACKLAYOUT *Track = (PVOID) TrackBuf;
ULONG TrackLength=sizeof(TrackBuf);
USHORT usHead, usCyl, usSect, i;
UCHAR data[2000], ret[100], *szDisk;
ULONG dataLength=sizeof(data);
APIRET rc;
if (argc != 4)
return 40 ;
szDisk = argv[0].strptr;
usCyl = atoi(argv[1].strptr);
usHead = atoi(argv[2].strptr);
usSect = atoi(argv[3].strptr);
rc = DosPhysicalDisk(2, &fhFile, 2, szDisk ,3);
if (rc){
sprintf(ret, "Error: %d (DosPhysicalDisk,2)", rc);
Retstr->strptr=ret;
Retstr->strlength=strlen(ret);
return 0;
}
Track->bCommand = 1;
Track->usHead = usHead;
Track->usCylinder = usCyl;
Track->usFirstSector = usSect;
Track->cSectors = 1;
for(i=0;i<256;i++){
Track->TrackTable[i].usSectorNumber=i+1;
Track->TrackTable[i].usSectorSize=512;
}
rc = DosDevIOCtl(fhFile, 0x09L, 0x64L,
Track, TrackLength, &TrackLength,
&data, dataLength, &dataLength);
if (rc){
sprintf(ret, "Error: %d (DosDevIOCtl,9,64)", rc);
Retstr->strptr=ret;
Retstr->strlength=strlen(ret);
return 0;
}
rc = DosPhysicalDisk(3, NULL, 0, (PVOID) &fhFile , 2);
if (rc){
sprintf(ret, "Error: %d (DosPhysicalDisk,3)", rc);
Retstr->strptr=ret;
Retstr->strlength=strlen(ret);
return 0;
}
Retstr->strptr=data;
Retstr->strlength=512;
return 0;
}