2 * COPYRIGHT: GPLv2+ - See COPYING in the top level directory
3 * PROJECT: ReactOS Virtual DOS Machine
4 * FILE: subsystems/mvdm/ntvdm/dos/dos32krnl/device.c
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
);
60 /* Set ES:BX to the location of the request */
61 setES(DOS_DATA_SEGMENT
);
62 setBX(DOS_DATA_OFFSET(Sda
.Request
));
64 /* Copy the request structure to ES:BX */
65 RtlMoveMemory(&Sda
->Request
, Request
, Request
->RequestLength
);
67 /* Call the strategy routine, and then the interrupt routine */
68 RunCallback16(&DosContext
, MAKELONG(DriverBlock
->StrategyRoutine
, HIWORD(Driver
)));
69 RunCallback16(&DosContext
, MAKELONG(DriverBlock
->InterruptRoutine
, HIWORD(Driver
)));
71 /* Get the request structure from ES:BX */
72 RtlMoveMemory(Request
, &Sda
->Request
, Request
->RequestLength
);
74 /* Restore the registers */
86 static inline WORD NTAPI
DosDriverReadInternal(PDOS_DEVICE_NODE DeviceNode
,
91 DOS_RW_REQUEST Request
;
93 Request
.Header
.RequestLength
= IoControl
? sizeof(DOS_IOCTL_RW_REQUEST
)
94 : sizeof(DOS_RW_REQUEST
);
95 Request
.Header
.CommandCode
= IoControl
? DOS_DEVCMD_IOCTL_READ
: DOS_DEVCMD_READ
;
96 Request
.BufferPointer
= Buffer
;
97 Request
.Length
= *Length
;
99 DosCallDriver(DeviceNode
->Driver
, &Request
.Header
);
101 *Length
= Request
.Length
;
102 return Request
.Header
.Status
;
105 static inline WORD NTAPI
DosDriverWriteInternal(PDOS_DEVICE_NODE DeviceNode
,
110 DOS_RW_REQUEST Request
;
112 Request
.Header
.RequestLength
= IoControl
? sizeof(DOS_IOCTL_RW_REQUEST
)
113 : sizeof(DOS_RW_REQUEST
);
114 Request
.Header
.CommandCode
= IoControl
? DOS_DEVCMD_IOCTL_WRITE
: DOS_DEVCMD_WRITE
;
115 Request
.BufferPointer
= Buffer
;
116 Request
.Length
= *Length
;
118 DosCallDriver(DeviceNode
->Driver
, &Request
.Header
);
120 *Length
= Request
.Length
;
121 return Request
.Header
.Status
;
124 static inline WORD NTAPI
DosDriverGenericRequest(PDOS_DEVICE_NODE DeviceNode
,
127 DOS_REQUEST_HEADER Request
;
129 Request
.RequestLength
= sizeof(DOS_REQUEST_HEADER
);
130 Request
.CommandCode
= CommandCode
;
132 DosCallDriver(DeviceNode
->Driver
, &Request
);
134 return Request
.Status
;
137 static WORD NTAPI
DosDriverDispatchIoctlRead(PDOS_DEVICE_NODE DeviceNode
,
141 return DosDriverReadInternal(DeviceNode
, Buffer
, Length
, TRUE
);
144 static WORD NTAPI
DosDriverDispatchRead(PDOS_DEVICE_NODE DeviceNode
,
148 return DosDriverReadInternal(DeviceNode
, Buffer
, Length
, FALSE
);
151 static WORD NTAPI
DosDriverDispatchPeek(PDOS_DEVICE_NODE DeviceNode
,
154 DOS_PEEK_REQUEST Request
;
156 Request
.Header
.RequestLength
= sizeof(DOS_PEEK_REQUEST
);
157 Request
.Header
.CommandCode
= DOS_DEVCMD_PEEK
;
159 DosCallDriver(DeviceNode
->Driver
, &Request
.Header
);
161 *Character
= Request
.Character
;
162 return Request
.Header
.Status
;
165 static WORD NTAPI
DosDriverDispatchInputStatus(PDOS_DEVICE_NODE DeviceNode
)
167 return DosDriverGenericRequest(DeviceNode
, DOS_DEVCMD_INSTAT
);
170 static WORD NTAPI
DosDriverDispatchFlushInput(PDOS_DEVICE_NODE DeviceNode
)
172 return DosDriverGenericRequest(DeviceNode
, DOS_DEVCMD_FLUSH_INPUT
);
175 static WORD NTAPI
DosDriverDispatchIoctlWrite(PDOS_DEVICE_NODE DeviceNode
,
179 return DosDriverWriteInternal(DeviceNode
, Buffer
, Length
, TRUE
);
182 static WORD NTAPI
DosDriverDispatchWrite(PDOS_DEVICE_NODE DeviceNode
,
186 return DosDriverWriteInternal(DeviceNode
, Buffer
, Length
, FALSE
);
189 static WORD NTAPI
DosDriverDispatchOutputStatus(PDOS_DEVICE_NODE DeviceNode
)
191 return DosDriverGenericRequest(DeviceNode
, DOS_DEVCMD_OUTSTAT
);
194 static WORD NTAPI
DosDriverDispatchFlushOutput(PDOS_DEVICE_NODE DeviceNode
)
196 return DosDriverGenericRequest(DeviceNode
, DOS_DEVCMD_FLUSH_OUTPUT
);
199 static WORD NTAPI
DosDriverDispatchOpen(PDOS_DEVICE_NODE DeviceNode
)
201 return DosDriverGenericRequest(DeviceNode
, DOS_DEVCMD_OPEN
);
204 static WORD NTAPI
DosDriverDispatchClose(PDOS_DEVICE_NODE DeviceNode
)
206 return DosDriverGenericRequest(DeviceNode
, DOS_DEVCMD_CLOSE
);
209 static WORD NTAPI
DosDriverDispatchOutputUntilBusy(PDOS_DEVICE_NODE DeviceNode
,
213 DOS_OUTPUT_BUSY_REQUEST Request
;
215 Request
.Header
.RequestLength
= sizeof(DOS_OUTPUT_BUSY_REQUEST
);
216 Request
.Header
.CommandCode
= DOS_DEVCMD_OUTPUT_BUSY
;
217 Request
.BufferPointer
= Buffer
;
218 Request
.Length
= *Length
;
220 DosCallDriver(DeviceNode
->Driver
, &Request
.Header
);
222 *Length
= Request
.Length
;
223 return Request
.Header
.Status
;
226 static VOID
DosAddDriver(DWORD Driver
)
228 PDOS_DRIVER LastDriver
= &SysVars
->NullDevice
;
230 /* Find the last driver in the list */
231 while (LOWORD(LastDriver
->Link
) != 0xFFFF)
233 LastDriver
= (PDOS_DRIVER
)FAR_POINTER(LastDriver
->Link
);
236 /* Add the new driver to the list */
237 LastDriver
->Link
= Driver
;
238 LastDriver
= (PDOS_DRIVER
)FAR_POINTER(Driver
);
240 if (LastDriver
->DeviceAttributes
& DOS_DEVATTR_CLOCK
)
242 /* Update the active CLOCK driver */
243 SysVars
->ActiveClock
= Driver
;
246 if (LastDriver
->DeviceAttributes
247 & (DOS_DEVATTR_STDIN
| DOS_DEVATTR_STDOUT
| DOS_DEVATTR_CON
))
249 /* Update the active CON driver */
250 SysVars
->ActiveCon
= Driver
;
254 static VOID
DosRemoveDriver(DWORD Driver
)
256 DWORD CurrentDriver
= MAKELONG(DOS_DATA_OFFSET(SysVars
.NullDevice
), DOS_DATA_SEGMENT
);
258 while (LOWORD(CurrentDriver
) != 0xFFFF)
260 PDOS_DRIVER DriverHeader
= (PDOS_DRIVER
)FAR_POINTER(CurrentDriver
);
262 if (DriverHeader
->Link
== Driver
)
264 /* Remove it from the list */
265 DriverHeader
->Link
= ((PDOS_DRIVER
)FAR_POINTER(DriverHeader
->Link
))->Link
;
269 CurrentDriver
= DriverHeader
->Link
;
273 static PDOS_DEVICE_NODE
DosCreateDeviceNode(DWORD Driver
)
276 PDOS_DRIVER DriverHeader
= (PDOS_DRIVER
)FAR_POINTER(Driver
);
277 PDOS_DEVICE_NODE Node
= RtlAllocateHeap(RtlGetProcessHeap(),
280 if (Node
== NULL
) return NULL
;
282 Node
->Driver
= Driver
;
283 Node
->DeviceAttributes
= DriverHeader
->DeviceAttributes
;
285 /* Initialize the name string */
286 Node
->Name
.Buffer
= Node
->NameBuffer
;
287 Node
->Name
.MaximumLength
= MAX_DEVICE_NAME
;
289 for (i
= 0; i
< MAX_DEVICE_NAME
; i
++)
291 if (DriverHeader
->DeviceName
[i
] == ' ') break;
292 Node
->Name
.Buffer
[i
] = DriverHeader
->DeviceName
[i
];
295 Node
->Name
.Length
= i
;
297 InsertTailList(&DeviceList
, &Node
->Entry
);
301 /* PUBLIC FUNCTIONS ***********************************************************/
303 PDOS_DEVICE_NODE
DosGetDriverNode(DWORD Driver
)
306 PDOS_DEVICE_NODE Node
;
308 for (i
= DeviceList
.Flink
; i
!= &DeviceList
; i
= i
->Flink
)
310 Node
= CONTAINING_RECORD(i
, DOS_DEVICE_NODE
, Entry
);
311 if (Node
->Driver
== Driver
) break;
314 if (i
== &DeviceList
)
316 DPRINT1("The driver at %04X:%04X has no associated device node. "
317 "Installing automagically.\n",
321 /* Create the device node */
322 Node
= DosCreateDeviceNode(Driver
);
323 Node
->IoctlReadRoutine
= DosDriverDispatchIoctlRead
;
324 Node
->ReadRoutine
= DosDriverDispatchRead
;
325 Node
->PeekRoutine
= DosDriverDispatchPeek
;
326 Node
->InputStatusRoutine
= DosDriverDispatchInputStatus
;
327 Node
->FlushInputRoutine
= DosDriverDispatchFlushInput
;
328 Node
->IoctlWriteRoutine
= DosDriverDispatchIoctlWrite
;
329 Node
->WriteRoutine
= DosDriverDispatchWrite
;
330 Node
->OutputStatusRoutine
= DosDriverDispatchOutputStatus
;
331 Node
->FlushOutputRoutine
= DosDriverDispatchFlushOutput
;
332 Node
->OpenRoutine
= DosDriverDispatchOpen
;
333 Node
->CloseRoutine
= DosDriverDispatchClose
;
334 Node
->OutputUntilBusyRoutine
= DosDriverDispatchOutputUntilBusy
;
340 PDOS_DEVICE_NODE
DosGetDevice(LPCSTR DeviceName
)
342 DWORD CurrentDriver
= MAKELONG(DOS_DATA_OFFSET(SysVars
.NullDevice
), DOS_DATA_SEGMENT
);
343 ANSI_STRING DeviceNameString
;
345 RtlInitAnsiString(&DeviceNameString
, DeviceName
);
347 while (LOWORD(CurrentDriver
) != 0xFFFF)
349 PDOS_DEVICE_NODE Node
= DosGetDriverNode(CurrentDriver
);
350 PDOS_DRIVER DriverHeader
= (PDOS_DRIVER
)FAR_POINTER(CurrentDriver
);
352 if (RtlEqualString(&Node
->Name
, &DeviceNameString
, TRUE
)) return Node
;
353 CurrentDriver
= DriverHeader
->Link
;
359 PDOS_DEVICE_NODE
DosCreateDeviceEx(WORD Attributes
, PCHAR DeviceName
, WORD PrivateDataSize
)
363 PDOS_DRIVER DriverHeader
;
364 PDOS_DEVICE_NODE Node
;
366 /* Make sure this is a character device */
367 if (!(Attributes
& DOS_DEVATTR_CHARACTER
))
369 DPRINT1("ERROR: Block devices are not supported.\n");
373 /* Create a driver header for this device */
374 Segment
= DosAllocateMemory(sizeof(DOS_DRIVER
) + DEVICE_CODE_SIZE
+ PrivateDataSize
, NULL
);
375 if (Segment
== 0) return NULL
;
377 /* Fill the header with data */
378 DriverHeader
= SEG_OFF_TO_PTR(Segment
, 0);
379 DriverHeader
->Link
= 0xFFFFFFFF;
380 DriverHeader
->DeviceAttributes
= Attributes
;
381 DriverHeader
->StrategyRoutine
= sizeof(DOS_DRIVER
);
382 DriverHeader
->InterruptRoutine
= sizeof(DOS_DRIVER
) + sizeof(StrategyRoutine
);
384 RtlFillMemory(DriverHeader
->DeviceName
, MAX_DEVICE_NAME
, ' ');
385 for (i
= 0; i
< MAX_DEVICE_NAME
; i
++)
387 if (DeviceName
[i
] == '\0' || DeviceName
[i
] == ' ') break;
388 DriverHeader
->DeviceName
[i
] = DeviceName
[i
];
391 /* Write the routines */
392 RtlMoveMemory(SEG_OFF_TO_PTR(Segment
, DriverHeader
->StrategyRoutine
),
394 sizeof(StrategyRoutine
));
395 RtlMoveMemory(SEG_OFF_TO_PTR(Segment
, DriverHeader
->InterruptRoutine
),
397 sizeof(InterruptRoutine
));
399 /* Create the node */
400 Node
= DosCreateDeviceNode(MAKELONG(0, Segment
));
403 DosFreeMemory(Segment
);
407 DosAddDriver(Node
->Driver
);
411 PDOS_DEVICE_NODE
DosCreateDevice(WORD Attributes
, PCHAR DeviceName
)
413 /* Call the extended API */
414 return DosCreateDeviceEx(Attributes
, DeviceName
, 0);
417 VOID
DosDeleteDevice(PDOS_DEVICE_NODE DeviceNode
)
419 DosRemoveDriver(DeviceNode
->Driver
);
421 ASSERT(LOWORD(DeviceNode
->Driver
) == 0);
422 DosFreeMemory(HIWORD(DeviceNode
->Driver
));
424 RemoveEntryList(&DeviceNode
->Entry
);
425 RtlFreeHeap(RtlGetProcessHeap(), 0, DeviceNode
);
428 VOID
DeviceStrategyBop(VOID
)
431 DeviceRequest
= (PDOS_REQUEST_HEADER
)SEG_OFF_TO_PTR(getES(), getBX());
434 VOID
DeviceInterruptBop(VOID
)
437 PDOS_DEVICE_NODE Node
;
438 DWORD DriverAddress
= (getCS() << 4) + getIP() - sizeof(DOS_DRIVER
) - 9;
440 /* Get the device node for this driver */
441 for (i
= DeviceList
.Flink
; i
!= &DeviceList
; i
= i
->Flink
)
443 Node
= CONTAINING_RECORD(i
, DOS_DEVICE_NODE
, Entry
);
444 if (TO_LINEAR(HIWORD(Node
->Driver
), LOWORD(Node
->Driver
)) == DriverAddress
) break;
447 if (i
== &DeviceList
)
449 DPRINT1("Device interrupt BOP from an unknown location.\n");
453 switch (DeviceRequest
->CommandCode
)
455 case DOS_DEVCMD_IOCTL_READ
:
457 PDOS_IOCTL_RW_REQUEST Request
= (PDOS_IOCTL_RW_REQUEST
)DeviceRequest
;
459 DeviceRequest
->Status
= Node
->IoctlReadRoutine(
461 Request
->BufferPointer
,
468 case DOS_DEVCMD_READ
:
470 PDOS_RW_REQUEST Request
= (PDOS_RW_REQUEST
)DeviceRequest
;
472 DeviceRequest
->Status
= Node
->ReadRoutine(
474 Request
->BufferPointer
,
481 case DOS_DEVCMD_PEEK
:
483 PDOS_PEEK_REQUEST Request
= (PDOS_PEEK_REQUEST
)DeviceRequest
;
484 DeviceRequest
->Status
= Node
->PeekRoutine(Node
, &Request
->Character
);
488 case DOS_DEVCMD_INSTAT
:
490 DeviceRequest
->Status
= Node
->InputStatusRoutine(Node
);
494 case DOS_DEVCMD_FLUSH_INPUT
:
496 DeviceRequest
->Status
= Node
->FlushInputRoutine(Node
);
500 case DOS_DEVCMD_IOCTL_WRITE
:
502 PDOS_IOCTL_RW_REQUEST Request
= (PDOS_IOCTL_RW_REQUEST
)DeviceRequest
;
504 DeviceRequest
->Status
= Node
->IoctlWriteRoutine(
506 Request
->BufferPointer
,
513 case DOS_DEVCMD_WRITE
:
515 PDOS_RW_REQUEST Request
= (PDOS_RW_REQUEST
)DeviceRequest
;
517 DeviceRequest
->Status
= Node
->WriteRoutine(Node
,
518 Request
->BufferPointer
,
525 case DOS_DEVCMD_OUTSTAT
:
527 DeviceRequest
->Status
= Node
->OutputStatusRoutine(Node
);
531 case DOS_DEVCMD_FLUSH_OUTPUT
:
533 DeviceRequest
->Status
= Node
->FlushOutputRoutine(Node
);
537 case DOS_DEVCMD_OPEN
:
539 DeviceRequest
->Status
= Node
->OpenRoutine(Node
);
543 case DOS_DEVCMD_CLOSE
:
545 DeviceRequest
->Status
= Node
->CloseRoutine(Node
);
549 case DOS_DEVCMD_OUTPUT_BUSY
:
551 PDOS_OUTPUT_BUSY_REQUEST Request
= (PDOS_OUTPUT_BUSY_REQUEST
)DeviceRequest
;
553 DeviceRequest
->Status
= Node
->OutputUntilBusyRoutine(
555 Request
->BufferPointer
,
564 DPRINT1("Unknown device command code: %u\n", DeviceRequest
->CommandCode
);
569 DWORD
DosLoadDriver(LPCSTR DriverFile
)
571 DWORD Result
= ERROR_SUCCESS
;
572 HANDLE FileHandle
= INVALID_HANDLE_VALUE
, FileMapping
= NULL
;
573 LPBYTE Address
= NULL
;
575 PDOS_DRIVER DriverHeader
;
578 DWORD DriversLoaded
= 0;
579 DOS_INIT_REQUEST Request
;
580 PDOS_DEVICE_NODE DeviceNode
;
582 /* Open a handle to the driver file */
583 FileHandle
= CreateFileA(DriverFile
,
588 FILE_ATTRIBUTE_NORMAL
,
590 if (FileHandle
== INVALID_HANDLE_VALUE
)
592 Result
= GetLastError();
596 /* Get the file size */
597 FileSize
= GetFileSize(FileHandle
, NULL
);
599 /* Allocate DOS memory for the driver */
600 Segment
= DosAllocateMemory(FileSize
>> 4, NULL
);
603 Result
= Sda
->LastErrorCode
;
607 /* Create a mapping object for the file */
608 FileMapping
= CreateFileMapping(FileHandle
,
614 if (FileMapping
== NULL
)
616 Result
= GetLastError();
620 /* Map the file into memory */
621 Address
= (LPBYTE
)MapViewOfFile(FileMapping
, FILE_MAP_READ
, 0, 0, 0);
624 Result
= GetLastError();
628 /* Copy the entire file to the DOS memory */
629 Driver
= MAKELONG(0, Segment
);
630 DriverHeader
= (PDOS_DRIVER
)FAR_POINTER(Driver
);
631 RtlCopyMemory(DriverHeader
, Address
, FileSize
);
633 /* Loop through all the drivers in this file */
636 if (!(DriverHeader
->DeviceAttributes
& DOS_DEVATTR_CHARACTER
))
638 DPRINT1("Error loading driver at %04X:%04X: "
639 "Block device drivers are not supported.\n",
645 /* Send the driver an init request */
646 RtlZeroMemory(&Request
, sizeof(Request
));
647 Request
.Header
.RequestLength
= sizeof(DOS_INIT_REQUEST
);
648 Request
.Header
.CommandCode
= DOS_DEVCMD_INIT
;
649 // TODO: Set Request.DeviceString to the appropriate line in CONFIG.NT!
650 DosCallDriver(Driver
, &Request
.Header
);
652 if (Request
.Header
.Status
& DOS_DEVSTAT_ERROR
)
654 DPRINT1("Error loading driver at %04X:%04X: "
655 "Initialization routine returned error %u.\n",
658 Request
.Header
.Status
& 0x7F);
662 /* Create the device node */
663 DeviceNode
= DosCreateDeviceNode(Driver
);
664 DeviceNode
->IoctlReadRoutine
= DosDriverDispatchIoctlRead
;
665 DeviceNode
->ReadRoutine
= DosDriverDispatchRead
;
666 DeviceNode
->PeekRoutine
= DosDriverDispatchPeek
;
667 DeviceNode
->InputStatusRoutine
= DosDriverDispatchInputStatus
;
668 DeviceNode
->FlushInputRoutine
= DosDriverDispatchFlushInput
;
669 DeviceNode
->IoctlWriteRoutine
= DosDriverDispatchIoctlWrite
;
670 DeviceNode
->WriteRoutine
= DosDriverDispatchWrite
;
671 DeviceNode
->OutputStatusRoutine
= DosDriverDispatchOutputStatus
;
672 DeviceNode
->FlushOutputRoutine
= DosDriverDispatchFlushOutput
;
673 DeviceNode
->OpenRoutine
= DosDriverDispatchOpen
;
674 DeviceNode
->CloseRoutine
= DosDriverDispatchClose
;
675 DeviceNode
->OutputUntilBusyRoutine
= DosDriverDispatchOutputUntilBusy
;
677 DosAddDriver(Driver
);
681 if (LOWORD(DriverHeader
->Link
) == 0xFFFF) break;
682 Driver
= DriverHeader
->Link
;
683 DriverHeader
= (PDOS_DRIVER
)FAR_POINTER(Driver
);
686 DPRINT1("%u drivers loaded from %s.\n", DriversLoaded
, DriverFile
);
689 if (Result
!= ERROR_SUCCESS
)
691 /* It was not successful, cleanup the DOS memory */
692 if (Segment
) DosFreeMemory(Segment
);
696 if (Address
!= NULL
) UnmapViewOfFile(Address
);
698 /* Close the file mapping object */
699 if (FileMapping
!= NULL
) CloseHandle(FileMapping
);
701 /* Close the file handle */
702 if (FileHandle
!= INVALID_HANDLE_VALUE
) CloseHandle(FileHandle
);