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>
14 /* PRIVATE VARIABLES **********************************************************/
16 static const BYTE StrategyRoutine
[] = {
24 static const BYTE InterruptRoutine
[] = {
32 C_ASSERT((sizeof(StrategyRoutine
) + sizeof(InterruptRoutine
)) == DEVICE_CODE_SIZE
);
34 static LIST_ENTRY DeviceList
= { &DeviceList
, &DeviceList
};
35 static PDOS_REQUEST_HEADER DeviceRequest
;
37 /* PRIVATE FUNCTIONS **********************************************************/
39 static VOID
DosCallDriver(DWORD Driver
, PDOS_REQUEST_HEADER Request
)
41 PDOS_DRIVER DriverBlock
= (PDOS_DRIVER
)FAR_POINTER(Driver
);
52 /* Set ES:BX to the location of the request */
53 setES(DOS_DATA_SEGMENT
);
54 setBX(DOS_DATA_OFFSET(Sda
.Request
));
56 /* Copy the request structure to ES:BX */
57 RtlMoveMemory(&Sda
->Request
, Request
, Request
->RequestLength
);
59 /* Call the strategy routine, and then the interrupt routine */
60 RunCallback16(&DosContext
, MAKELONG(DriverBlock
->StrategyRoutine
, HIWORD(Driver
)));
61 RunCallback16(&DosContext
, MAKELONG(DriverBlock
->InterruptRoutine
, HIWORD(Driver
)));
63 /* Get the request structure from ES:BX */
64 RtlMoveMemory(Request
, &Sda
->Request
, Request
->RequestLength
);
66 /* Restore the registers */
78 static inline WORD NTAPI
DosDriverReadInternal(PDOS_DEVICE_NODE DeviceNode
,
83 DOS_RW_REQUEST Request
;
85 Request
.Header
.RequestLength
= IoControl
? sizeof(DOS_IOCTL_RW_REQUEST
)
86 : sizeof(DOS_RW_REQUEST
);
87 Request
.Header
.CommandCode
= IoControl
? DOS_DEVCMD_IOCTL_READ
: DOS_DEVCMD_READ
;
88 Request
.BufferPointer
= Buffer
;
89 Request
.Length
= *Length
;
91 DosCallDriver(DeviceNode
->Driver
, &Request
.Header
);
93 *Length
= Request
.Length
;
94 return Request
.Header
.Status
;
97 static inline WORD NTAPI
DosDriverWriteInternal(PDOS_DEVICE_NODE DeviceNode
,
102 DOS_RW_REQUEST Request
;
104 Request
.Header
.RequestLength
= IoControl
? sizeof(DOS_IOCTL_RW_REQUEST
)
105 : sizeof(DOS_RW_REQUEST
);
106 Request
.Header
.CommandCode
= IoControl
? DOS_DEVCMD_IOCTL_WRITE
: DOS_DEVCMD_WRITE
;
107 Request
.BufferPointer
= Buffer
;
108 Request
.Length
= *Length
;
110 DosCallDriver(DeviceNode
->Driver
, &Request
.Header
);
112 *Length
= Request
.Length
;
113 return Request
.Header
.Status
;
116 static inline WORD NTAPI
DosDriverGenericRequest(PDOS_DEVICE_NODE DeviceNode
,
119 DOS_REQUEST_HEADER Request
;
121 Request
.RequestLength
= sizeof(DOS_REQUEST_HEADER
);
122 Request
.CommandCode
= CommandCode
;
124 DosCallDriver(DeviceNode
->Driver
, &Request
);
126 return Request
.Status
;
129 static WORD NTAPI
DosDriverDispatchIoctlRead(PDOS_DEVICE_NODE DeviceNode
,
133 return DosDriverReadInternal(DeviceNode
, Buffer
, Length
, TRUE
);
136 static WORD NTAPI
DosDriverDispatchRead(PDOS_DEVICE_NODE DeviceNode
,
140 return DosDriverReadInternal(DeviceNode
, Buffer
, Length
, FALSE
);
143 static WORD NTAPI
DosDriverDispatchPeek(PDOS_DEVICE_NODE DeviceNode
,
146 DOS_PEEK_REQUEST Request
;
148 Request
.Header
.RequestLength
= sizeof(DOS_PEEK_REQUEST
);
149 Request
.Header
.CommandCode
= DOS_DEVCMD_PEEK
;
151 DosCallDriver(DeviceNode
->Driver
, &Request
.Header
);
153 *Character
= Request
.Character
;
154 return Request
.Header
.Status
;
157 static WORD NTAPI
DosDriverDispatchInputStatus(PDOS_DEVICE_NODE DeviceNode
)
159 return DosDriverGenericRequest(DeviceNode
, DOS_DEVCMD_INSTAT
);
162 static WORD NTAPI
DosDriverDispatchFlushInput(PDOS_DEVICE_NODE DeviceNode
)
164 return DosDriverGenericRequest(DeviceNode
, DOS_DEVCMD_FLUSH_INPUT
);
167 static WORD NTAPI
DosDriverDispatchIoctlWrite(PDOS_DEVICE_NODE DeviceNode
,
171 return DosDriverWriteInternal(DeviceNode
, Buffer
, Length
, TRUE
);
174 static WORD NTAPI
DosDriverDispatchWrite(PDOS_DEVICE_NODE DeviceNode
,
178 return DosDriverWriteInternal(DeviceNode
, Buffer
, Length
, FALSE
);
181 static WORD NTAPI
DosDriverDispatchOutputStatus(PDOS_DEVICE_NODE DeviceNode
)
183 return DosDriverGenericRequest(DeviceNode
, DOS_DEVCMD_OUTSTAT
);
186 static WORD NTAPI
DosDriverDispatchFlushOutput(PDOS_DEVICE_NODE DeviceNode
)
188 return DosDriverGenericRequest(DeviceNode
, DOS_DEVCMD_FLUSH_OUTPUT
);
191 static WORD NTAPI
DosDriverDispatchOpen(PDOS_DEVICE_NODE DeviceNode
)
193 return DosDriverGenericRequest(DeviceNode
, DOS_DEVCMD_OPEN
);
196 static WORD NTAPI
DosDriverDispatchClose(PDOS_DEVICE_NODE DeviceNode
)
198 return DosDriverGenericRequest(DeviceNode
, DOS_DEVCMD_CLOSE
);
201 static WORD NTAPI
DosDriverDispatchOutputUntilBusy(PDOS_DEVICE_NODE DeviceNode
,
205 DOS_OUTPUT_BUSY_REQUEST Request
;
207 Request
.Header
.RequestLength
= sizeof(DOS_OUTPUT_BUSY_REQUEST
);
208 Request
.Header
.CommandCode
= DOS_DEVCMD_OUTPUT_BUSY
;
209 Request
.BufferPointer
= Buffer
;
210 Request
.Length
= *Length
;
212 DosCallDriver(DeviceNode
->Driver
, &Request
.Header
);
214 *Length
= Request
.Length
;
215 return Request
.Header
.Status
;
218 static VOID
DosAddDriver(DWORD Driver
)
220 PDOS_DRIVER LastDriver
= &SysVars
->NullDevice
;
222 /* Find the last driver in the list */
223 while (LOWORD(LastDriver
->Link
) != 0xFFFF)
225 LastDriver
= (PDOS_DRIVER
)FAR_POINTER(LastDriver
->Link
);
228 /* Add the new driver to the list */
229 LastDriver
->Link
= Driver
;
230 LastDriver
= (PDOS_DRIVER
)FAR_POINTER(Driver
);
232 if (LastDriver
->DeviceAttributes
& DOS_DEVATTR_CLOCK
)
234 /* Update the active CLOCK driver */
235 SysVars
->ActiveClock
= Driver
;
238 if (LastDriver
->DeviceAttributes
239 & (DOS_DEVATTR_STDIN
| DOS_DEVATTR_STDOUT
| DOS_DEVATTR_CON
))
241 /* Update the active CON driver */
242 SysVars
->ActiveCon
= Driver
;
246 static VOID
DosRemoveDriver(DWORD Driver
)
248 DWORD CurrentDriver
= MAKELONG(DOS_DATA_OFFSET(SysVars
.NullDevice
), DOS_DATA_SEGMENT
);
250 while (LOWORD(CurrentDriver
) != 0xFFFF)
252 PDOS_DRIVER DriverHeader
= (PDOS_DRIVER
)FAR_POINTER(CurrentDriver
);
254 if (DriverHeader
->Link
== Driver
)
256 /* Remove it from the list */
257 DriverHeader
->Link
= ((PDOS_DRIVER
)FAR_POINTER(DriverHeader
->Link
))->Link
;
261 CurrentDriver
= DriverHeader
->Link
;
265 static PDOS_DEVICE_NODE
DosCreateDeviceNode(DWORD Driver
)
268 PDOS_DRIVER DriverHeader
= (PDOS_DRIVER
)FAR_POINTER(Driver
);
269 PDOS_DEVICE_NODE Node
= RtlAllocateHeap(RtlGetProcessHeap(),
272 if (Node
== NULL
) return NULL
;
274 Node
->Driver
= Driver
;
275 Node
->DeviceAttributes
= DriverHeader
->DeviceAttributes
;
277 /* Initialize the name string */
278 Node
->Name
.Buffer
= Node
->NameBuffer
;
279 Node
->Name
.MaximumLength
= MAX_DEVICE_NAME
;
281 for (i
= 0; i
< MAX_DEVICE_NAME
; i
++)
283 if (DriverHeader
->DeviceName
[i
] == ' ') break;
284 Node
->Name
.Buffer
[i
] = DriverHeader
->DeviceName
[i
];
287 Node
->Name
.Length
= i
;
289 InsertTailList(&DeviceList
, &Node
->Entry
);
293 /* PUBLIC FUNCTIONS ***********************************************************/
295 PDOS_DEVICE_NODE
DosGetDriverNode(DWORD Driver
)
298 PDOS_DEVICE_NODE Node
;
300 for (i
= DeviceList
.Flink
; i
!= &DeviceList
; i
= i
->Flink
)
302 Node
= CONTAINING_RECORD(i
, DOS_DEVICE_NODE
, Entry
);
303 if (Node
->Driver
== Driver
) break;
306 if (i
== &DeviceList
)
308 DPRINT1("The driver at %04X:%04X has no associated device node. "
309 "Installing automagically.\n",
313 /* Create the device node */
314 Node
= DosCreateDeviceNode(Driver
);
315 Node
->IoctlReadRoutine
= DosDriverDispatchIoctlRead
;
316 Node
->ReadRoutine
= DosDriverDispatchRead
;
317 Node
->PeekRoutine
= DosDriverDispatchPeek
;
318 Node
->InputStatusRoutine
= DosDriverDispatchInputStatus
;
319 Node
->FlushInputRoutine
= DosDriverDispatchFlushInput
;
320 Node
->IoctlWriteRoutine
= DosDriverDispatchIoctlWrite
;
321 Node
->WriteRoutine
= DosDriverDispatchWrite
;
322 Node
->OutputStatusRoutine
= DosDriverDispatchOutputStatus
;
323 Node
->FlushOutputRoutine
= DosDriverDispatchFlushOutput
;
324 Node
->OpenRoutine
= DosDriverDispatchOpen
;
325 Node
->CloseRoutine
= DosDriverDispatchClose
;
326 Node
->OutputUntilBusyRoutine
= DosDriverDispatchOutputUntilBusy
;
332 PDOS_DEVICE_NODE
DosGetDevice(LPCSTR DeviceName
)
334 DWORD CurrentDriver
= MAKELONG(DOS_DATA_OFFSET(SysVars
.NullDevice
), DOS_DATA_SEGMENT
);
335 ANSI_STRING DeviceNameString
;
337 RtlInitAnsiString(&DeviceNameString
, DeviceName
);
339 while (LOWORD(CurrentDriver
) != 0xFFFF)
341 PDOS_DEVICE_NODE Node
= DosGetDriverNode(CurrentDriver
);
342 PDOS_DRIVER DriverHeader
= (PDOS_DRIVER
)FAR_POINTER(CurrentDriver
);
344 if (RtlEqualString(&Node
->Name
, &DeviceNameString
, TRUE
)) return Node
;
345 CurrentDriver
= DriverHeader
->Link
;
351 PDOS_DEVICE_NODE
DosCreateDeviceEx(WORD Attributes
, PCHAR DeviceName
, WORD PrivateDataSize
)
355 PDOS_DRIVER DriverHeader
;
356 PDOS_DEVICE_NODE Node
;
358 /* Make sure this is a character device */
359 if (!(Attributes
& DOS_DEVATTR_CHARACTER
))
361 DPRINT1("ERROR: Block devices are not supported.\n");
365 /* Create a driver header for this device */
366 Segment
= DosAllocateMemory(sizeof(DOS_DRIVER
) + DEVICE_CODE_SIZE
+ PrivateDataSize
, NULL
);
367 if (Segment
== 0) return NULL
;
369 /* Fill the header with data */
370 DriverHeader
= SEG_OFF_TO_PTR(Segment
, 0);
371 DriverHeader
->Link
= 0xFFFFFFFF;
372 DriverHeader
->DeviceAttributes
= Attributes
;
373 DriverHeader
->StrategyRoutine
= sizeof(DOS_DRIVER
);
374 DriverHeader
->InterruptRoutine
= sizeof(DOS_DRIVER
) + sizeof(StrategyRoutine
);
376 RtlFillMemory(DriverHeader
->DeviceName
, MAX_DEVICE_NAME
, ' ');
377 for (i
= 0; i
< MAX_DEVICE_NAME
; i
++)
379 if (DeviceName
[i
] == '\0' || DeviceName
[i
] == ' ') break;
380 DriverHeader
->DeviceName
[i
] = DeviceName
[i
];
383 /* Write the routines */
384 RtlMoveMemory(SEG_OFF_TO_PTR(Segment
, DriverHeader
->StrategyRoutine
),
386 sizeof(StrategyRoutine
));
387 RtlMoveMemory(SEG_OFF_TO_PTR(Segment
, DriverHeader
->InterruptRoutine
),
389 sizeof(InterruptRoutine
));
391 /* Create the node */
392 Node
= DosCreateDeviceNode(MAKELONG(0, Segment
));
395 DosFreeMemory(Segment
);
399 DosAddDriver(Node
->Driver
);
403 PDOS_DEVICE_NODE
DosCreateDevice(WORD Attributes
, PCHAR DeviceName
)
405 /* Call the extended API */
406 return DosCreateDeviceEx(Attributes
, DeviceName
, 0);
409 VOID
DosDeleteDevice(PDOS_DEVICE_NODE DeviceNode
)
411 DosRemoveDriver(DeviceNode
->Driver
);
413 ASSERT(LOWORD(DeviceNode
->Driver
) == 0);
414 DosFreeMemory(HIWORD(DeviceNode
->Driver
));
416 RemoveEntryList(&DeviceNode
->Entry
);
417 RtlFreeHeap(RtlGetProcessHeap(), 0, DeviceNode
);
420 VOID
DeviceStrategyBop(VOID
)
423 DeviceRequest
= (PDOS_REQUEST_HEADER
)SEG_OFF_TO_PTR(getES(), getBX());
426 VOID
DeviceInterruptBop(VOID
)
429 PDOS_DEVICE_NODE Node
;
430 DWORD DriverAddress
= (getCS() << 4) + getIP() - sizeof(DOS_DRIVER
) - 9;
432 /* Get the device node for this driver */
433 for (i
= DeviceList
.Flink
; i
!= &DeviceList
; i
= i
->Flink
)
435 Node
= CONTAINING_RECORD(i
, DOS_DEVICE_NODE
, Entry
);
436 if (TO_LINEAR(HIWORD(Node
->Driver
), LOWORD(Node
->Driver
)) == DriverAddress
) break;
439 if (i
== &DeviceList
)
441 DPRINT1("Device interrupt BOP from an unknown location.\n");
445 switch (DeviceRequest
->CommandCode
)
447 case DOS_DEVCMD_IOCTL_READ
:
449 PDOS_IOCTL_RW_REQUEST Request
= (PDOS_IOCTL_RW_REQUEST
)DeviceRequest
;
451 DeviceRequest
->Status
= Node
->IoctlReadRoutine(
453 Request
->BufferPointer
,
460 case DOS_DEVCMD_READ
:
462 PDOS_RW_REQUEST Request
= (PDOS_RW_REQUEST
)DeviceRequest
;
464 DeviceRequest
->Status
= Node
->ReadRoutine(
466 Request
->BufferPointer
,
473 case DOS_DEVCMD_PEEK
:
475 PDOS_PEEK_REQUEST Request
= (PDOS_PEEK_REQUEST
)DeviceRequest
;
476 DeviceRequest
->Status
= Node
->PeekRoutine(Node
, &Request
->Character
);
480 case DOS_DEVCMD_INSTAT
:
482 DeviceRequest
->Status
= Node
->InputStatusRoutine(Node
);
486 case DOS_DEVCMD_FLUSH_INPUT
:
488 DeviceRequest
->Status
= Node
->FlushInputRoutine(Node
);
492 case DOS_DEVCMD_IOCTL_WRITE
:
494 PDOS_IOCTL_RW_REQUEST Request
= (PDOS_IOCTL_RW_REQUEST
)DeviceRequest
;
496 DeviceRequest
->Status
= Node
->IoctlWriteRoutine(
498 Request
->BufferPointer
,
505 case DOS_DEVCMD_WRITE
:
507 PDOS_RW_REQUEST Request
= (PDOS_RW_REQUEST
)DeviceRequest
;
509 DeviceRequest
->Status
= Node
->WriteRoutine(Node
,
510 Request
->BufferPointer
,
517 case DOS_DEVCMD_OUTSTAT
:
519 DeviceRequest
->Status
= Node
->OutputStatusRoutine(Node
);
523 case DOS_DEVCMD_FLUSH_OUTPUT
:
525 DeviceRequest
->Status
= Node
->FlushOutputRoutine(Node
);
529 case DOS_DEVCMD_OPEN
:
531 DeviceRequest
->Status
= Node
->OpenRoutine(Node
);
535 case DOS_DEVCMD_CLOSE
:
537 DeviceRequest
->Status
= Node
->CloseRoutine(Node
);
541 case DOS_DEVCMD_OUTPUT_BUSY
:
543 PDOS_OUTPUT_BUSY_REQUEST Request
= (PDOS_OUTPUT_BUSY_REQUEST
)DeviceRequest
;
545 DeviceRequest
->Status
= Node
->OutputUntilBusyRoutine(
547 Request
->BufferPointer
,
556 DPRINT1("Unknown device command code: %u\n", DeviceRequest
->CommandCode
);
561 DWORD
DosLoadDriver(LPCSTR DriverFile
)
563 DWORD Result
= ERROR_SUCCESS
;
564 HANDLE FileHandle
= INVALID_HANDLE_VALUE
, FileMapping
= NULL
;
565 LPBYTE Address
= NULL
;
567 PDOS_DRIVER DriverHeader
;
570 DWORD DriversLoaded
= 0;
571 DOS_INIT_REQUEST Request
;
572 PDOS_DEVICE_NODE DeviceNode
;
574 /* Open a handle to the driver file */
575 FileHandle
= CreateFileA(DriverFile
,
580 FILE_ATTRIBUTE_NORMAL
,
582 if (FileHandle
== INVALID_HANDLE_VALUE
)
584 Result
= GetLastError();
588 /* Get the file size */
589 FileSize
= GetFileSize(FileHandle
, NULL
);
591 /* Allocate DOS memory for the driver */
592 Segment
= DosAllocateMemory(FileSize
>> 4, NULL
);
595 Result
= Sda
->LastErrorCode
;
599 /* Create a mapping object for the file */
600 FileMapping
= CreateFileMapping(FileHandle
,
606 if (FileMapping
== NULL
)
608 Result
= GetLastError();
612 /* Map the file into memory */
613 Address
= (LPBYTE
)MapViewOfFile(FileMapping
, FILE_MAP_READ
, 0, 0, 0);
616 Result
= GetLastError();
620 /* Copy the entire file to the DOS memory */
621 Driver
= MAKELONG(0, Segment
);
622 DriverHeader
= (PDOS_DRIVER
)FAR_POINTER(Driver
);
623 RtlCopyMemory(DriverHeader
, Address
, FileSize
);
625 /* Loop through all the drivers in this file */
628 if (!(DriverHeader
->DeviceAttributes
& DOS_DEVATTR_CHARACTER
))
630 DPRINT1("Error loading driver at %04X:%04X: "
631 "Block device drivers are not supported.\n",
637 /* Send the driver an init request */
638 RtlZeroMemory(&Request
, sizeof(Request
));
639 Request
.Header
.RequestLength
= sizeof(DOS_INIT_REQUEST
);
640 Request
.Header
.CommandCode
= DOS_DEVCMD_INIT
;
641 // TODO: Set Request.DeviceString to the appropriate line in CONFIG.NT!
642 DosCallDriver(Driver
, &Request
.Header
);
644 if (Request
.Header
.Status
& DOS_DEVSTAT_ERROR
)
646 DPRINT1("Error loading driver at %04X:%04X: "
647 "Initialization routine returned error %u.\n",
650 Request
.Header
.Status
& 0x7F);
654 /* Create the device node */
655 DeviceNode
= DosCreateDeviceNode(Driver
);
656 DeviceNode
->IoctlReadRoutine
= DosDriverDispatchIoctlRead
;
657 DeviceNode
->ReadRoutine
= DosDriverDispatchRead
;
658 DeviceNode
->PeekRoutine
= DosDriverDispatchPeek
;
659 DeviceNode
->InputStatusRoutine
= DosDriverDispatchInputStatus
;
660 DeviceNode
->FlushInputRoutine
= DosDriverDispatchFlushInput
;
661 DeviceNode
->IoctlWriteRoutine
= DosDriverDispatchIoctlWrite
;
662 DeviceNode
->WriteRoutine
= DosDriverDispatchWrite
;
663 DeviceNode
->OutputStatusRoutine
= DosDriverDispatchOutputStatus
;
664 DeviceNode
->FlushOutputRoutine
= DosDriverDispatchFlushOutput
;
665 DeviceNode
->OpenRoutine
= DosDriverDispatchOpen
;
666 DeviceNode
->CloseRoutine
= DosDriverDispatchClose
;
667 DeviceNode
->OutputUntilBusyRoutine
= DosDriverDispatchOutputUntilBusy
;
669 DosAddDriver(Driver
);
673 if (LOWORD(DriverHeader
->Link
) == 0xFFFF) break;
674 Driver
= DriverHeader
->Link
;
675 DriverHeader
= (PDOS_DRIVER
)FAR_POINTER(Driver
);
678 DPRINT1("%u drivers loaded from %s.\n", DriversLoaded
, DriverFile
);
681 if (Result
!= ERROR_SUCCESS
)
683 /* It was not successful, cleanup the DOS memory */
684 if (Segment
) DosFreeMemory(Segment
);
688 if (Address
!= NULL
) UnmapViewOfFile(Address
);
690 /* Close the file mapping object */
691 if (FileMapping
!= NULL
) CloseHandle(FileMapping
);
693 /* Close the file handle */
694 if (FileHandle
!= INVALID_HANDLE_VALUE
) CloseHandle(FileHandle
);