2 * COPYRIGHT: GPLv2+ - See COPYING in the top level directory
3 * PROJECT: ReactOS Virtual DOS Machine
5 * PURPOSE: DOS Device Support
6 * PROGRAMMERS: Aleksandar Andrejevic <theflash AT sdf DOT lonestar DOT org>
9 /* INCLUDES *******************************************************************/
22 /* PRIVATE VARIABLES **********************************************************/
24 static const BYTE StrategyRoutine
[] = {
32 static const BYTE InterruptRoutine
[] = {
40 C_ASSERT((sizeof(StrategyRoutine
) + sizeof(InterruptRoutine
)) == DEVICE_CODE_SIZE
);
42 static LIST_ENTRY DeviceList
= { &DeviceList
, &DeviceList
};
43 static PDOS_REQUEST_HEADER DeviceRequest
;
45 /* PRIVATE FUNCTIONS **********************************************************/
47 static VOID
DosCallDriver(DWORD Driver
, PDOS_REQUEST_HEADER Request
)
49 PDOS_DRIVER DriverBlock
= (PDOS_DRIVER
)FAR_POINTER(Driver
);
53 /* Set ES:BX to the location of the request */
54 setES(DOS_DATA_SEGMENT
);
55 setBX(DOS_DATA_OFFSET(Sda
.Request
));
57 /* Copy the request structure to ES:BX */
58 RtlMoveMemory(&Sda
->Request
, Request
, Request
->RequestLength
);
60 /* Call the strategy routine, and then the interrupt routine */
61 RunCallback16(&DosContext
, MAKELONG(DriverBlock
->StrategyRoutine
, HIWORD(Driver
)));
62 RunCallback16(&DosContext
, MAKELONG(DriverBlock
->InterruptRoutine
, HIWORD(Driver
)));
64 /* Get the request structure from ES:BX */
65 RtlMoveMemory(Request
, &Sda
->Request
, Request
->RequestLength
);
72 static inline WORD NTAPI
DosDriverReadInternal(PDOS_DEVICE_NODE DeviceNode
,
77 DOS_RW_REQUEST Request
;
79 Request
.Header
.RequestLength
= IoControl
? sizeof(DOS_IOCTL_RW_REQUEST
)
80 : sizeof(DOS_RW_REQUEST
);
81 Request
.Header
.CommandCode
= IoControl
? DOS_DEVCMD_IOCTL_READ
: DOS_DEVCMD_READ
;
82 Request
.BufferPointer
= Buffer
;
83 Request
.Length
= *Length
;
85 DosCallDriver(DeviceNode
->Driver
, &Request
.Header
);
87 *Length
= Request
.Length
;
88 return Request
.Header
.Status
;
91 static inline WORD NTAPI
DosDriverWriteInternal(PDOS_DEVICE_NODE DeviceNode
,
96 DOS_RW_REQUEST Request
;
98 Request
.Header
.RequestLength
= IoControl
? sizeof(DOS_IOCTL_RW_REQUEST
)
99 : sizeof(DOS_RW_REQUEST
);
100 Request
.Header
.CommandCode
= IoControl
? DOS_DEVCMD_IOCTL_WRITE
: DOS_DEVCMD_WRITE
;
101 Request
.BufferPointer
= Buffer
;
102 Request
.Length
= *Length
;
104 DosCallDriver(DeviceNode
->Driver
, &Request
.Header
);
106 *Length
= Request
.Length
;
107 return Request
.Header
.Status
;
110 static inline WORD NTAPI
DosDriverGenericRequest(PDOS_DEVICE_NODE DeviceNode
,
113 DOS_REQUEST_HEADER Request
;
115 Request
.RequestLength
= sizeof(DOS_REQUEST_HEADER
);
116 Request
.CommandCode
= CommandCode
;
118 DosCallDriver(DeviceNode
->Driver
, &Request
);
120 return Request
.Status
;
123 static WORD NTAPI
DosDriverDispatchIoctlRead(PDOS_DEVICE_NODE DeviceNode
,
127 return DosDriverReadInternal(DeviceNode
, Buffer
, Length
, TRUE
);
130 static WORD NTAPI
DosDriverDispatchRead(PDOS_DEVICE_NODE DeviceNode
,
134 return DosDriverReadInternal(DeviceNode
, Buffer
, Length
, FALSE
);
137 static WORD NTAPI
DosDriverDispatchPeek(PDOS_DEVICE_NODE DeviceNode
,
140 DOS_PEEK_REQUEST Request
;
142 Request
.Header
.RequestLength
= sizeof(DOS_PEEK_REQUEST
);
143 Request
.Header
.CommandCode
= DOS_DEVCMD_PEEK
;
145 DosCallDriver(DeviceNode
->Driver
, &Request
.Header
);
147 *Character
= Request
.Character
;
148 return Request
.Header
.Status
;
151 static WORD NTAPI
DosDriverDispatchInputStatus(PDOS_DEVICE_NODE DeviceNode
)
153 return DosDriverGenericRequest(DeviceNode
, DOS_DEVCMD_INSTAT
);
156 static WORD NTAPI
DosDriverDispatchFlushInput(PDOS_DEVICE_NODE DeviceNode
)
158 return DosDriverGenericRequest(DeviceNode
, DOS_DEVCMD_FLUSH_INPUT
);
161 static WORD NTAPI
DosDriverDispatchIoctlWrite(PDOS_DEVICE_NODE DeviceNode
,
165 return DosDriverWriteInternal(DeviceNode
, Buffer
, Length
, TRUE
);
168 static WORD NTAPI
DosDriverDispatchWrite(PDOS_DEVICE_NODE DeviceNode
,
172 return DosDriverWriteInternal(DeviceNode
, Buffer
, Length
, FALSE
);
175 static WORD NTAPI
DosDriverDispatchOutputStatus(PDOS_DEVICE_NODE DeviceNode
)
177 return DosDriverGenericRequest(DeviceNode
, DOS_DEVCMD_OUTSTAT
);
180 static WORD NTAPI
DosDriverDispatchFlushOutput(PDOS_DEVICE_NODE DeviceNode
)
182 return DosDriverGenericRequest(DeviceNode
, DOS_DEVCMD_FLUSH_OUTPUT
);
185 static WORD NTAPI
DosDriverDispatchOpen(PDOS_DEVICE_NODE DeviceNode
)
187 return DosDriverGenericRequest(DeviceNode
, DOS_DEVCMD_OPEN
);
190 static WORD NTAPI
DosDriverDispatchClose(PDOS_DEVICE_NODE DeviceNode
)
192 return DosDriverGenericRequest(DeviceNode
, DOS_DEVCMD_CLOSE
);
195 static WORD NTAPI
DosDriverDispatchOutputUntilBusy(PDOS_DEVICE_NODE DeviceNode
,
199 DOS_OUTPUT_BUSY_REQUEST Request
;
201 Request
.Header
.RequestLength
= sizeof(DOS_OUTPUT_BUSY_REQUEST
);
202 Request
.Header
.CommandCode
= DOS_DEVCMD_OUTPUT_BUSY
;
203 Request
.BufferPointer
= Buffer
;
204 Request
.Length
= *Length
;
206 DosCallDriver(DeviceNode
->Driver
, &Request
.Header
);
208 *Length
= Request
.Length
;
209 return Request
.Header
.Status
;
212 static VOID
DosAddDriver(DWORD Driver
)
214 PDOS_DRIVER LastDriver
= &SysVars
->NullDevice
;
216 /* Find the last driver in the list */
217 while (LOWORD(LastDriver
->Link
) != 0xFFFF)
219 LastDriver
= (PDOS_DRIVER
)FAR_POINTER(LastDriver
->Link
);
222 /* Add the new driver to the list */
223 LastDriver
->Link
= Driver
;
224 LastDriver
= (PDOS_DRIVER
)FAR_POINTER(Driver
);
226 if (LastDriver
->DeviceAttributes
& DOS_DEVATTR_CLOCK
)
228 /* Update the active CLOCK driver */
229 SysVars
->ActiveClock
= Driver
;
232 if (LastDriver
->DeviceAttributes
233 & (DOS_DEVATTR_STDIN
| DOS_DEVATTR_STDOUT
| DOS_DEVATTR_CON
))
235 /* Update the active CON driver */
236 SysVars
->ActiveCon
= Driver
;
240 static VOID
DosRemoveDriver(DWORD Driver
)
242 DWORD CurrentDriver
= MAKELONG(DOS_DATA_OFFSET(SysVars
.NullDevice
), DOS_DATA_SEGMENT
);
244 while (LOWORD(CurrentDriver
) != 0xFFFF)
246 PDOS_DRIVER DriverHeader
= (PDOS_DRIVER
)FAR_POINTER(CurrentDriver
);
248 if (DriverHeader
->Link
== Driver
)
250 /* Remove it from the list */
251 DriverHeader
->Link
= ((PDOS_DRIVER
)FAR_POINTER(DriverHeader
->Link
))->Link
;
255 CurrentDriver
= DriverHeader
->Link
;
259 static PDOS_DEVICE_NODE
DosCreateDeviceNode(DWORD Driver
)
262 PDOS_DRIVER DriverHeader
= (PDOS_DRIVER
)FAR_POINTER(Driver
);
263 PDOS_DEVICE_NODE Node
= RtlAllocateHeap(RtlGetProcessHeap(),
266 if (Node
== NULL
) return NULL
;
268 Node
->Driver
= Driver
;
269 Node
->DeviceAttributes
= DriverHeader
->DeviceAttributes
;
271 /* Initialize the name string */
272 Node
->Name
.Buffer
= Node
->NameBuffer
;
273 Node
->Name
.MaximumLength
= MAX_DEVICE_NAME
;
275 for (i
= 0; i
< MAX_DEVICE_NAME
; i
++)
277 if (DriverHeader
->DeviceName
[i
] == ' ') break;
278 Node
->Name
.Buffer
[i
] = DriverHeader
->DeviceName
[i
];
281 Node
->Name
.Length
= i
;
283 InsertTailList(&DeviceList
, &Node
->Entry
);
287 /* PUBLIC FUNCTIONS ***********************************************************/
289 PDOS_DEVICE_NODE
DosGetDriverNode(DWORD Driver
)
292 PDOS_DEVICE_NODE Node
;
294 for (i
= DeviceList
.Flink
; i
!= &DeviceList
; i
= i
->Flink
)
296 Node
= CONTAINING_RECORD(i
, DOS_DEVICE_NODE
, Entry
);
297 if (Node
->Driver
== Driver
) break;
300 if (i
== &DeviceList
)
302 DPRINT1("The driver at %04X:%04X has no associated device node. "
303 "Installing automagically.\n",
307 /* Create the device node */
308 Node
= DosCreateDeviceNode(Driver
);
309 Node
->IoctlReadRoutine
= DosDriverDispatchIoctlRead
;
310 Node
->ReadRoutine
= DosDriverDispatchRead
;
311 Node
->PeekRoutine
= DosDriverDispatchPeek
;
312 Node
->InputStatusRoutine
= DosDriverDispatchInputStatus
;
313 Node
->FlushInputRoutine
= DosDriverDispatchFlushInput
;
314 Node
->IoctlWriteRoutine
= DosDriverDispatchIoctlWrite
;
315 Node
->WriteRoutine
= DosDriverDispatchWrite
;
316 Node
->OutputStatusRoutine
= DosDriverDispatchOutputStatus
;
317 Node
->FlushOutputRoutine
= DosDriverDispatchFlushOutput
;
318 Node
->OpenRoutine
= DosDriverDispatchOpen
;
319 Node
->CloseRoutine
= DosDriverDispatchClose
;
320 Node
->OutputUntilBusyRoutine
= DosDriverDispatchOutputUntilBusy
;
326 PDOS_DEVICE_NODE
DosGetDevice(LPCSTR DeviceName
)
328 DWORD CurrentDriver
= MAKELONG(DOS_DATA_OFFSET(SysVars
.NullDevice
), DOS_DATA_SEGMENT
);
329 ANSI_STRING DeviceNameString
;
331 RtlInitAnsiString(&DeviceNameString
, DeviceName
);
333 while (LOWORD(CurrentDriver
) != 0xFFFF)
335 PDOS_DEVICE_NODE Node
= DosGetDriverNode(CurrentDriver
);
336 PDOS_DRIVER DriverHeader
= (PDOS_DRIVER
)FAR_POINTER(CurrentDriver
);
338 if (RtlEqualString(&Node
->Name
, &DeviceNameString
, TRUE
)) return Node
;
339 CurrentDriver
= DriverHeader
->Link
;
345 PDOS_DEVICE_NODE
DosCreateDeviceEx(WORD Attributes
, PCHAR DeviceName
, WORD PrivateDataSize
)
349 PDOS_DRIVER DriverHeader
;
350 PDOS_DEVICE_NODE Node
;
352 /* Make sure this is a character device */
353 if (!(Attributes
& DOS_DEVATTR_CHARACTER
))
355 DPRINT1("ERROR: Block devices are not supported.\n");
359 /* Create a driver header for this device */
360 Segment
= DosAllocateMemory(sizeof(DOS_DRIVER
) + DEVICE_CODE_SIZE
+ PrivateDataSize
, NULL
);
361 if (Segment
== 0) return NULL
;
363 /* Fill the header with data */
364 DriverHeader
= SEG_OFF_TO_PTR(Segment
, 0);
365 DriverHeader
->Link
= 0xFFFFFFFF;
366 DriverHeader
->DeviceAttributes
= Attributes
;
367 DriverHeader
->StrategyRoutine
= sizeof(DOS_DRIVER
);
368 DriverHeader
->InterruptRoutine
= sizeof(DOS_DRIVER
) + sizeof(StrategyRoutine
);
370 RtlFillMemory(DriverHeader
->DeviceName
, MAX_DEVICE_NAME
, ' ');
371 for (i
= 0; i
< MAX_DEVICE_NAME
; i
++)
373 if (DeviceName
[i
] == '\0' || DeviceName
[i
] == ' ') break;
374 DriverHeader
->DeviceName
[i
] = DeviceName
[i
];
377 /* Write the routines */
378 RtlMoveMemory(SEG_OFF_TO_PTR(Segment
, DriverHeader
->StrategyRoutine
),
380 sizeof(StrategyRoutine
));
381 RtlMoveMemory(SEG_OFF_TO_PTR(Segment
, DriverHeader
->InterruptRoutine
),
383 sizeof(InterruptRoutine
));
385 /* Create the node */
386 Node
= DosCreateDeviceNode(MAKELONG(0, Segment
));
389 DosFreeMemory(Segment
);
393 DosAddDriver(Node
->Driver
);
397 PDOS_DEVICE_NODE
DosCreateDevice(WORD Attributes
, PCHAR DeviceName
)
399 /* Call the extended API */
400 return DosCreateDeviceEx(Attributes
, DeviceName
, 0);
403 VOID
DosDeleteDevice(PDOS_DEVICE_NODE DeviceNode
)
405 DosRemoveDriver(DeviceNode
->Driver
);
407 ASSERT(LOWORD(DeviceNode
->Driver
) == 0);
408 DosFreeMemory(HIWORD(DeviceNode
->Driver
));
410 RemoveEntryList(&DeviceNode
->Entry
);
411 RtlFreeHeap(RtlGetProcessHeap(), 0, DeviceNode
);
414 VOID
DeviceStrategyBop(VOID
)
417 DeviceRequest
= (PDOS_REQUEST_HEADER
)SEG_OFF_TO_PTR(getES(), getBX());
420 VOID
DeviceInterruptBop(VOID
)
423 PDOS_DEVICE_NODE Node
;
424 DWORD DriverAddress
= (getCS() << 4) + getIP() - sizeof(DOS_DRIVER
) - 9;
426 /* Get the device node for this driver */
427 for (i
= DeviceList
.Flink
; i
!= &DeviceList
; i
= i
->Flink
)
429 Node
= CONTAINING_RECORD(i
, DOS_DEVICE_NODE
, Entry
);
430 if (TO_LINEAR(HIWORD(Node
->Driver
), LOWORD(Node
->Driver
)) == DriverAddress
) break;
433 if (i
== &DeviceList
)
435 DPRINT1("Device interrupt BOP from an unknown location.\n");
439 switch (DeviceRequest
->CommandCode
)
441 case DOS_DEVCMD_IOCTL_READ
:
443 PDOS_IOCTL_RW_REQUEST Request
= (PDOS_IOCTL_RW_REQUEST
)DeviceRequest
;
445 DeviceRequest
->Status
= Node
->IoctlReadRoutine(
447 Request
->BufferPointer
,
454 case DOS_DEVCMD_READ
:
456 PDOS_RW_REQUEST Request
= (PDOS_RW_REQUEST
)DeviceRequest
;
458 DeviceRequest
->Status
= Node
->ReadRoutine(
460 Request
->BufferPointer
,
467 case DOS_DEVCMD_PEEK
:
469 PDOS_PEEK_REQUEST Request
= (PDOS_PEEK_REQUEST
)DeviceRequest
;
470 DeviceRequest
->Status
= Node
->PeekRoutine(Node
, &Request
->Character
);
474 case DOS_DEVCMD_INSTAT
:
476 DeviceRequest
->Status
= Node
->InputStatusRoutine(Node
);
480 case DOS_DEVCMD_FLUSH_INPUT
:
482 DeviceRequest
->Status
= Node
->FlushInputRoutine(Node
);
486 case DOS_DEVCMD_IOCTL_WRITE
:
488 PDOS_IOCTL_RW_REQUEST Request
= (PDOS_IOCTL_RW_REQUEST
)DeviceRequest
;
490 DeviceRequest
->Status
= Node
->IoctlWriteRoutine(
492 Request
->BufferPointer
,
499 case DOS_DEVCMD_WRITE
:
501 PDOS_RW_REQUEST Request
= (PDOS_RW_REQUEST
)DeviceRequest
;
503 DeviceRequest
->Status
= Node
->WriteRoutine(Node
,
504 Request
->BufferPointer
,
511 case DOS_DEVCMD_OUTSTAT
:
513 DeviceRequest
->Status
= Node
->OutputStatusRoutine(Node
);
517 case DOS_DEVCMD_FLUSH_OUTPUT
:
519 DeviceRequest
->Status
= Node
->FlushOutputRoutine(Node
);
523 case DOS_DEVCMD_OPEN
:
525 DeviceRequest
->Status
= Node
->OpenRoutine(Node
);
529 case DOS_DEVCMD_CLOSE
:
531 DeviceRequest
->Status
= Node
->CloseRoutine(Node
);
535 case DOS_DEVCMD_OUTPUT_BUSY
:
537 PDOS_OUTPUT_BUSY_REQUEST Request
= (PDOS_OUTPUT_BUSY_REQUEST
)DeviceRequest
;
539 DeviceRequest
->Status
= Node
->OutputUntilBusyRoutine(
541 Request
->BufferPointer
,
550 DPRINT1("Unknown device command code: %u\n", DeviceRequest
->CommandCode
);
555 DWORD
DosLoadDriver(LPCSTR DriverFile
)
557 DWORD Result
= ERROR_SUCCESS
;
558 HANDLE FileHandle
= INVALID_HANDLE_VALUE
, FileMapping
= NULL
;
559 LPBYTE Address
= NULL
;
561 PDOS_DRIVER DriverHeader
;
564 DWORD DriversLoaded
= 0;
565 DOS_INIT_REQUEST Request
;
566 PDOS_DEVICE_NODE DeviceNode
;
568 /* Open a handle to the driver file */
569 FileHandle
= CreateFileA(DriverFile
,
574 FILE_ATTRIBUTE_NORMAL
,
576 if (FileHandle
== INVALID_HANDLE_VALUE
)
578 Result
= GetLastError();
582 /* Get the file size */
583 FileSize
= GetFileSize(FileHandle
, NULL
);
585 /* Allocate DOS memory for the driver */
586 Segment
= DosAllocateMemory(FileSize
>> 4, NULL
);
589 Result
= Sda
->LastErrorCode
;
593 /* Create a mapping object for the file */
594 FileMapping
= CreateFileMapping(FileHandle
,
600 if (FileMapping
== NULL
)
602 Result
= GetLastError();
606 /* Map the file into memory */
607 Address
= (LPBYTE
)MapViewOfFile(FileMapping
, FILE_MAP_READ
, 0, 0, 0);
610 Result
= GetLastError();
614 /* Copy the entire file to the DOS memory */
615 Driver
= MAKELONG(0, Segment
);
616 DriverHeader
= (PDOS_DRIVER
)FAR_POINTER(Driver
);
617 RtlCopyMemory(DriverHeader
, Address
, FileSize
);
619 /* Loop through all the drivers in this file */
622 if (!(DriverHeader
->DeviceAttributes
& DOS_DEVATTR_CHARACTER
))
624 DPRINT1("Error loading driver at %04X:%04X: "
625 "Block device drivers are not supported.\n",
631 /* Send the driver an init request */
632 RtlZeroMemory(&Request
, sizeof(Request
));
633 Request
.Header
.RequestLength
= sizeof(DOS_INIT_REQUEST
);
634 Request
.Header
.CommandCode
= DOS_DEVCMD_INIT
;
635 // TODO: Set Request.DeviceString to the appropriate line in CONFIG.NT!
636 DosCallDriver(Driver
, &Request
.Header
);
638 if (Request
.Header
.Status
& DOS_DEVSTAT_ERROR
)
640 DPRINT1("Error loading driver at %04X:%04X: "
641 "Initialization routine returned error %u.\n",
644 Request
.Header
.Status
& 0x7F);
648 /* Create the device node */
649 DeviceNode
= DosCreateDeviceNode(Driver
);
650 DeviceNode
->IoctlReadRoutine
= DosDriverDispatchIoctlRead
;
651 DeviceNode
->ReadRoutine
= DosDriverDispatchRead
;
652 DeviceNode
->PeekRoutine
= DosDriverDispatchPeek
;
653 DeviceNode
->InputStatusRoutine
= DosDriverDispatchInputStatus
;
654 DeviceNode
->FlushInputRoutine
= DosDriverDispatchFlushInput
;
655 DeviceNode
->IoctlWriteRoutine
= DosDriverDispatchIoctlWrite
;
656 DeviceNode
->WriteRoutine
= DosDriverDispatchWrite
;
657 DeviceNode
->OutputStatusRoutine
= DosDriverDispatchOutputStatus
;
658 DeviceNode
->FlushOutputRoutine
= DosDriverDispatchFlushOutput
;
659 DeviceNode
->OpenRoutine
= DosDriverDispatchOpen
;
660 DeviceNode
->CloseRoutine
= DosDriverDispatchClose
;
661 DeviceNode
->OutputUntilBusyRoutine
= DosDriverDispatchOutputUntilBusy
;
663 DosAddDriver(Driver
);
667 if (LOWORD(DriverHeader
->Link
) == 0xFFFF) break;
668 Driver
= DriverHeader
->Link
;
669 DriverHeader
= (PDOS_DRIVER
)FAR_POINTER(Driver
);
672 DPRINT1("%u drivers loaded from %s.\n", DriversLoaded
, DriverFile
);
675 if (Result
!= ERROR_SUCCESS
)
677 /* It was not successful, cleanup the DOS memory */
678 if (Segment
) DosFreeMemory(Segment
);
682 if (Address
!= NULL
) UnmapViewOfFile(Address
);
684 /* Close the file mapping object */
685 if (FileMapping
!= NULL
) CloseHandle(FileMapping
);
687 /* Close the file handle */
688 if (FileHandle
!= INVALID_HANDLE_VALUE
) CloseHandle(FileHandle
);