Ticket #2: diracc.cpp

File diracc.cpp, 5.0 KB (added by dwgras, 17 years ago)
Line 
1#define INCL_BASE
2#define INCL_DOSDEVIOCTL
3#include <os2.h>
4
5#include <ctype.h>
6#include <stdio.h>
7#include <stdlib.h>
8#include <string.h>
9
10
11USHORT GetSectorSize(UCHAR ucDrive)
12{
13    FSALLOCATE fs={0};
14    APIRET rc;
15    int uDrive;
16
17    uDrive = toupper(ucDrive) - 'A' + 1;
18    rc = DosQueryFSInfo(uDrive,FSIL_ALLOC,&fs,sizeof(fs));
19    if (rc == NO_ERROR)
20    {
21        return fs.cbSector;
22    }
23    else
24    {
25        return 0;
26    }
27}
28
29ULONG LockDrive(HFILE hDrive)
30{
31    UCHAR cmd=0;
32    UCHAR data=0;
33    ULONG ulp = sizeof(cmd);
34    ULONG uld = sizeof(data);
35
36    return DosDevIOCtl(
37                        hDrive,
38                        IOCTL_DISK,
39                        DSK_LOCKDRIVE,
40                        &cmd,
41                        ulp,
42                        &ulp,
43                        &data,
44                        uld,
45                        &uld);
46}
47
48ULONG UnlockDrive(HFILE hDrive)
49{
50    UCHAR cmd=0;
51    UCHAR data=0;
52    ULONG ulp = sizeof(cmd);
53    ULONG uld = sizeof(data);
54
55    return DosDevIOCtl(
56                        hDrive,
57                        IOCTL_DISK,
58                        DSK_UNLOCKDRIVE,
59                        &cmd,
60                        ulp,
61                        &ulp,
62                        &data,
63                        uld,
64                        &uld);
65}
66
67PCHAR AllocDataBuffer(UCHAR ucDrive,ULONG ulEntities,PULONG pulFactor,ULONG ulFlag)
68{
69    APIRET rc= NO_ERROR;
70    PVOID pBuf=NULL;
71
72    if (ulFlag)
73    {
74        *pulFactor = GetSectorSize(ucDrive);
75    }
76    else
77    {
78        *pulFactor = 1;
79    }
80
81    printf("Allocating %d bytes of memory\n",*pulFactor * ulEntities);
82    rc = DosAllocMem(&pBuf,*pulFactor * ulEntities,PAG_COMMIT|PAG_READ|PAG_WRITE);
83    if (rc == NO_ERROR)
84    {
85        memset(pBuf,0xFF,*pulFactor * ulEntities);
86        return (PCHAR)pBuf;
87    }
88    else
89    {
90        return NULL;
91    }
92}
93
94VOID DeallocDataBuffer(PCHAR pBuf)
95{
96    if (pBuf)
97    {
98        DosFreeMem(pBuf);
99    }
100}
101
102APIRET MyDosOpen(PSZ pszDrive,PHFILE phDrive,BOOL fFlag)
103{
104    APIRET  rc;
105    ULONG   ulAction;
106
107    rc = DosOpen(
108                    pszDrive,
109                    phDrive,
110                    &ulAction,
111                    0UL,
112                    FILE_NORMAL,
113                    OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_FAIL_IF_NEW,
114                    OPEN_ACCESS_READONLY | OPEN_SHARE_DENYNONE | OPEN_FLAGS_DASD,
115                    NULL);
116
117    if ((rc == NO_ERROR) && fFlag)
118    {
119#define FSCTL_SECTORIO 0x00009014
120#define MAGIC_SECTORIO 0xDEADFACE
121
122        ULONG pwd   = MAGIC_SECTORIO;
123        PBYTE ppwd  = (PBYTE)&pwd;
124        ULONG rdl   = 0UL;
125        ULONG lpwd  = sizeof(ppwd);
126
127        rc = DosFSCtl(
128                        NULL,
129                        0UL,
130                        &rdl,
131                        ppwd,
132                        sizeof(ppwd),
133                        &lpwd,
134                        FSCTL_SECTORIO,
135                        NULL,
136                        *phDrive,
137                        FSCTL_HANDLE);
138    }
139    return rc;
140}
141
142APIRET MyDosClose(HFILE hDrive)
143{
144    return DosClose(hDrive);
145}
146
147int main(int argc,char *argv[])
148{
149    HFILE hDrive = NULLHANDLE;
150    ULONG ulAction = 0UL;
151    APIRET rc = NO_ERROR;
152    int aDrive = 0;
153    int aFlag  = 0;
154    int aEntities = 0;
155    ULONG ulEntities;
156    BOOL ulFlag;
157    UCHAR ucDrive;
158
159    if (argc != 4)
160    {
161        printf("Usage: %s [Drive:] [NumEntities] [SectorIOFlag]\n",argv[0]);
162        return 1;
163    }
164
165    aDrive = sscanf(argv[1],"%c:",&ucDrive);
166    aEntities = sscanf(argv[2],"%d",&ulEntities);
167    aFlag  = sscanf(argv[3],"%d",&ulFlag);
168
169    if (aDrive && aFlag && aEntities)
170    {
171        rc = MyDosOpen(
172                    argv[1],
173                    &hDrive,
174                    ulFlag
175                    );
176
177        if (rc == NO_ERROR)
178        {
179            ULONG ulRead = 0UL;
180            PCHAR pBuf=NULL;
181            ULONG ulFactor = 0UL;
182
183            pBuf = AllocDataBuffer(ucDrive,ulEntities,&ulFactor,ulFlag);
184
185            if (pBuf)
186            {
187                rc = LockDrive(hDrive);
188
189                rc = DosRead(hDrive,pBuf,ulEntities,&ulRead);
190
191                if (rc == NO_ERROR && ulRead)
192                {
193                    printf("Successfully read %d Bytes\n",ulRead*ulFactor);
194                    if ((ulRead * ulFactor) >= sizeof(ULONG))
195                    {
196                        printf("Start DWORD:%#.8x, End DWORD:%#.8x\n",*(PULONG)pBuf,*(PULONG)(pBuf+ulRead*ulFactor-sizeof(ULONG)));
197                    }
198                }
199                else
200                {
201                    printf("DosRead returned with rc=%d\n",rc);
202                }
203
204                rc = UnlockDrive(hDrive);
205
206            }
207
208            DeallocDataBuffer(pBuf);
209
210            rc = MyDosClose(hDrive);
211        }
212        return 0;
213    }
214    return 1;
215}
216