[NTOS:INBV]: Fix parameter name and type.
[reactos.git] / reactos / ntoskrnl / ex / hdlsterm.c
1 /*
2 * PROJECT: ReactOS Kernel
3 * LICENSE: BSD - See COPYING.ARM in the top level directory
4 * FILE: ntoskrnl/ex/hdlsterm.c
5 * PURPOSE: Headless Terminal Support
6 * PROGRAMMERS: ReactOS Portable Systems Group
7 */
8
9 /* INCLUDES *******************************************************************/
10
11 #include <ntoskrnl.h>
12 #include <debug.h>
13
14 /* GLOBALS ********************************************************************/
15
16 PHEADLESS_GLOBALS HeadlessGlobals;
17
18 /* FUNCTIONS ******************************************************************/
19
20 FORCEINLINE
21 KIRQL
22 HdlspAcquireGlobalLock(VOID)
23 {
24 KIRQL OldIrql;
25
26 /* Don't acquire the lock if we are bugchecking */
27 if (!HeadlessGlobals->InBugCheck)
28 {
29 KeAcquireSpinLock(&HeadlessGlobals->SpinLock, &OldIrql);
30 }
31 else
32 {
33 OldIrql = 0xFF;
34 }
35
36 return OldIrql;
37 }
38
39 FORCEINLINE
40 VOID
41 HdlspReleaselobalLock(IN KIRQL OldIrql)
42 {
43 /* Only release the lock if we aren't bugchecking */
44 if (OldIrql != 0xFF)
45 {
46 KeReleaseSpinLock(&HeadlessGlobals->SpinLock, OldIrql);
47 }
48 else
49 {
50 ASSERT(HeadlessGlobals->InBugCheck == TRUE);
51 }
52 }
53
54 VOID
55 NTAPI
56 HdlspSendStringAtBaud(IN PUCHAR String)
57 {
58 /* Send every byte */
59 while (*String++ != ANSI_NULL)
60 {
61 InbvPortPutByte(HeadlessGlobals->TerminalPort, *String);
62 }
63 }
64
65 NTSTATUS
66 NTAPI
67 HdlspEnableTerminal(IN BOOLEAN Enable)
68 {
69 /* Enable if requested, as long as this isn't a PCI serial port crashing */
70 if ((Enable) &&
71 !(HeadlessGlobals->TerminalEnabled) &&
72 !((HeadlessGlobals->IsMMIODevice) && (HeadlessGlobals->InBugCheck)))
73 {
74 /* Initialize the COM port with cportlib */
75 HeadlessGlobals->TerminalEnabled = InbvPortInitialize(HeadlessGlobals->
76 TerminalBaudRate,
77 HeadlessGlobals->
78 TerminalPortNumber,
79 HeadlessGlobals->
80 TerminalPortAddress,
81 &HeadlessGlobals->
82 TerminalPort,
83 HeadlessGlobals->
84 IsMMIODevice);
85 if (!HeadlessGlobals->TerminalEnabled)
86 {
87 DPRINT1("Failed to initialize port through cportlib\n");
88 return STATUS_UNSUCCESSFUL;
89 }
90
91 /* Cleanup the screen and reset the cursor */
92 HdlspSendStringAtBaud((PUCHAR)"\x1B[2J");
93 HdlspSendStringAtBaud((PUCHAR)"\x1B[H");
94
95 /* Enable FIFO */
96 InbvPortEnableFifo(HeadlessGlobals->TerminalPort, TRUE);
97 }
98 else if (!Enable)
99 {
100 /* Specific case when headless is being disabled */
101 InbvPortTerminate(HeadlessGlobals->TerminalPort);
102 HeadlessGlobals->TerminalPort = 0;
103 HeadlessGlobals->TerminalEnabled = FALSE;
104 }
105
106 /* All done */
107 return STATUS_SUCCESS;
108 }
109
110 VOID
111 NTAPI
112 INIT_FUNCTION
113 HeadlessInit(IN PLOADER_PARAMETER_BLOCK LoaderBlock)
114 {
115 PHEADLESS_LOADER_BLOCK HeadlessBlock;
116
117 /* Only initialize further if the loader found EMS enabled */
118 HeadlessBlock = LoaderBlock->Extension->HeadlessLoaderBlock;
119 if (!HeadlessBlock) return;
120
121 /* Ignore invalid EMS settings */
122 if ((HeadlessBlock->PortNumber > 4) && (HeadlessBlock->UsedBiosSettings)) return;
123
124 /* Allocate the global headless data */
125 HeadlessGlobals = ExAllocatePoolWithTag(NonPagedPool,
126 sizeof(*HeadlessGlobals),
127 'sldH');
128 if (!HeadlessGlobals) return;
129
130 /* Zero and copy loader data */
131 RtlZeroMemory(HeadlessGlobals, sizeof(*HeadlessGlobals));
132 HeadlessGlobals->TerminalPortNumber = HeadlessBlock->PortNumber;
133 HeadlessGlobals->TerminalPortAddress = HeadlessBlock->PortAddress;
134 HeadlessGlobals->TerminalBaudRate = HeadlessBlock->BaudRate;
135 HeadlessGlobals->TerminalParity = HeadlessBlock->Parity;
136 HeadlessGlobals->TerminalStopBits = HeadlessBlock->StopBits;
137 HeadlessGlobals->UsedBiosSettings = HeadlessBlock->UsedBiosSettings;
138 HeadlessGlobals->IsMMIODevice = HeadlessBlock->IsMMIODevice;
139 HeadlessGlobals->TerminalType = HeadlessBlock->TerminalType;
140 HeadlessGlobals->SystemGUID = HeadlessBlock->SystemGUID;
141 DPRINT1("EMS on Port %lu (0x%p) at %lu bps\n",
142 HeadlessGlobals->TerminalPortNumber,
143 HeadlessGlobals->TerminalPortAddress,
144 HeadlessGlobals->TerminalBaudRate);
145
146 /* These two are opposites of each other */
147 if (HeadlessGlobals->IsMMIODevice) HeadlessGlobals->IsNonLegacyDevice = TRUE;
148
149 /* Check for a PCI device, warn that this isn't supported */
150 if (HeadlessBlock->PciDeviceId != PCI_INVALID_VENDORID)
151 {
152 DPRINT1("PCI Serial Ports not supported\n");
153 }
154
155 /* Log entries are not yet supported */
156 DPRINT1("FIXME: No Headless logging support\n");
157
158 /* Allocate temporary buffer */
159 HeadlessGlobals->TmpBuffer = ExAllocatePoolWithTag(NonPagedPool, 80, 'sldH');
160 if (!HeadlessGlobals->TmpBuffer) return;
161
162 /* Windows seems to apply some special hacks for 9600 bps */
163 if (HeadlessGlobals->TerminalBaudRate == 9600)
164 {
165 DPRINT1("Please use other baud rate than 9600bps for now\n");
166 }
167
168 /* Enable the terminal */
169 HdlspEnableTerminal(TRUE);
170 }
171
172 VOID
173 NTAPI
174 HdlspPutData(IN PUCHAR Data,
175 IN ULONG DataSize)
176 {
177 ULONG i;
178 for (i = 0; i < DataSize; i++)
179 {
180 InbvPortPutByte(HeadlessGlobals->TerminalPort, Data[i]++);
181 }
182 }
183
184 VOID
185 NTAPI
186 HdlspPutString(IN PUCHAR String)
187 {
188 PUCHAR Dest = HeadlessGlobals->TmpBuffer;
189 UCHAR Char = 0;
190
191 /* Scan each character */
192 while (*String != ANSI_NULL)
193 {
194 /* Check for rotate, send existing buffer and restart from where we are */
195 if (Dest >= &HeadlessGlobals->TmpBuffer[79])
196 {
197 HeadlessGlobals->TmpBuffer[79] = ANSI_NULL;
198 HdlspSendStringAtBaud(HeadlessGlobals->TmpBuffer);
199 Dest = HeadlessGlobals->TmpBuffer;
200 }
201 else
202 {
203 /* Get the current character and check for special graphical chars */
204 Char = *String;
205 if (Char & 0x80)
206 {
207 switch (Char)
208 {
209 case 0xB0: case 0xB3: case 0xBA:
210 Char = '|';
211 break;
212 case 0xB1: case 0xDC: case 0xDD: case 0xDE: case 0xDF:
213 Char = '%';
214 break;
215 case 0xB2: case 0xDB:
216 Char = '#';
217 break;
218 case 0xA9: case 0xAA: case 0xBB: case 0xBC: case 0xBF:
219 case 0xC0: case 0xC8: case 0xC9: case 0xD9: case 0xDA:
220 Char = '+';
221 break;
222 case 0xC4:
223 Char = '-';
224 break;
225 case 0xCD:
226 Char = '=';
227 break;
228 }
229 }
230
231 /* Anything else must be Unicode */
232 if (Char & 0x80)
233 {
234 /* Can't do Unicode yet */
235 UNIMPLEMENTED;
236 }
237 else
238 {
239 /* Add the modified char to the temporary buffer */
240 *Dest++ = Char;
241 }
242
243 /* Check the next char */
244 String++;
245 }
246 }
247
248 /* Finish and send */
249 *Dest = ANSI_NULL;
250 HdlspSendStringAtBaud(HeadlessGlobals->TmpBuffer);
251 }
252
253 NTSTATUS
254 NTAPI
255 HdlspDispatch(IN HEADLESS_CMD Command,
256 IN PVOID InputBuffer,
257 IN SIZE_T InputBufferSize,
258 OUT PVOID OutputBuffer,
259 OUT PSIZE_T OutputBufferSize)
260 {
261 KIRQL OldIrql;
262 PHEADLESS_RSP_QUERY_INFO HeadlessInfo;
263 PHEADLESS_CMD_PUT_STRING PutString;
264 PHEADLESS_CMD_ENABLE_TERMINAL EnableTerminal;
265 PHEADLESS_RSP_GET_BYTE GetByte;
266 NTSTATUS Status = STATUS_NOT_IMPLEMENTED;
267 ASSERT(HeadlessGlobals != NULL);
268 // ASSERT(HeadlessGlobals->PageLockHandle != NULL);
269
270 /* Ignore non-reentrant commands */
271 if ((Command != HeadlessCmdAddLogEntry) &&
272 (Command != HeadlessCmdStartBugCheck) &&
273 (Command != HeadlessCmdSendBlueScreenData) &&
274 (Command != HeadlessCmdDoBugCheckProcessing))
275 {
276 OldIrql = HdlspAcquireGlobalLock();
277
278 if (HeadlessGlobals->ProcessingCmd)
279 {
280 HdlspReleaselobalLock(OldIrql);
281 return STATUS_UNSUCCESSFUL;
282 }
283
284 /* Don't allow these commands next time */
285 HeadlessGlobals->ProcessingCmd = TRUE;
286 HdlspReleaselobalLock(OldIrql);
287 }
288
289 /* Handle each command */
290 switch (Command)
291 {
292 case HeadlessCmdEnableTerminal:
293 {
294 /* Make sure the caller passed valid data */
295 if (!(InputBuffer) ||
296 (InputBufferSize != sizeof(*EnableTerminal)))
297 {
298 DPRINT1("Invalid buffer\n");
299 Status = STATUS_INVALID_PARAMETER;
300 break;
301 }
302
303 /* Go and enable it */
304 EnableTerminal = InputBuffer;
305 Status = HdlspEnableTerminal(EnableTerminal->Enable);
306 break;
307 }
308
309 case HeadlessCmdCheckForReboot:
310 break;
311
312 case HeadlessCmdPutString:
313 {
314 /* Validate the existence of an input buffer */
315 if (!InputBuffer)
316 {
317 Status = STATUS_INVALID_PARAMETER;
318 break;
319 }
320
321 /* Terminal should be on */
322 if (HeadlessGlobals->TerminalEnabled)
323 {
324 /* Print each byte in the string making sure VT100 chars are used */
325 PutString = InputBuffer;
326 HdlspPutString(PutString->String);
327 }
328
329 /* Return success either way */
330 Status = STATUS_SUCCESS;
331 break;
332 }
333
334 case HeadlessCmdClearDisplay:
335 {
336 /* Send the VT100 clear screen command if the terminal is enabled */
337 if (HeadlessGlobals->TerminalEnabled)
338 {
339 HdlspSendStringAtBaud((PUCHAR)"\033[2J");
340 }
341
342 /* Return success either way */
343 Status = STATUS_SUCCESS;
344 break;
345 }
346
347 case HeadlessCmdClearToEndOfDisplay:
348 break;
349 case HeadlessCmdClearToEndOfLine:
350 break;
351 case HeadlessCmdDisplayAttributesOff:
352 break;
353 case HeadlessCmdDisplayInverseVideo:
354 break;
355 case HeadlessCmdSetColor:
356 break;
357 case HeadlessCmdPositionCursor:
358 break;
359 case HeadlessCmdTerminalPoll:
360 break;
361
362 case HeadlessCmdGetByte:
363 {
364 /* Make sure the caller passed valid data */
365 if (!(OutputBuffer) ||
366 !(OutputBufferSize) ||
367 (*OutputBufferSize < sizeof(*GetByte)))
368 {
369 DPRINT1("Invalid buffer\n");
370 Status = STATUS_INVALID_PARAMETER;
371 break;
372 }
373
374 /* Make sure the terminal is enabled */
375 GetByte = OutputBuffer;
376 if (HeadlessGlobals->TerminalEnabled)
377 {
378 /* Poll if something is on the wire */
379 if (InbvPortPollOnly(HeadlessGlobals->TerminalPort))
380 {
381 /* If so, read it */
382 InbvPortGetByte(HeadlessGlobals->TerminalPort,
383 &GetByte->Value);
384 }
385 else
386 {
387 /* Nothing is there, return 0 */
388 GetByte->Value = 0;
389 }
390 }
391 else
392 {
393 /* Otherwise return nothing */
394 GetByte->Value = 0;
395 }
396
397 /* Return success either way */
398 Status = STATUS_SUCCESS;
399 break;
400 }
401
402 case HeadlessCmdGetLine:
403 break;
404 case HeadlessCmdStartBugCheck:
405 break;
406 case HeadlessCmdDoBugCheckProcessing:
407 break;
408
409 case HeadlessCmdQueryInformation:
410 {
411 /* Make sure the caller passed valid data */
412 if (!(OutputBuffer) ||
413 !(OutputBufferSize) ||
414 (*OutputBufferSize < sizeof(*HeadlessInfo)))
415 {
416 DPRINT1("Invalid buffer\n");
417 Status = STATUS_INVALID_PARAMETER;
418 break;
419 }
420
421 /* If we got here, headless is enabled -- we know this much */
422 HeadlessInfo = OutputBuffer;
423 HeadlessInfo->PortType = HeadlessSerialPort;
424 HeadlessInfo->Serial.TerminalAttached = TRUE;
425 HeadlessInfo->Serial.UsedBiosSettings = HeadlessGlobals->UsedBiosSettings != 0;
426 HeadlessInfo->Serial.TerminalBaudRate = HeadlessGlobals->TerminalBaudRate;
427 HeadlessInfo->Serial.TerminalType = HeadlessGlobals->TerminalType;
428
429 /* Now check on what port/baud it's enabled on */
430 if ((HeadlessGlobals->TerminalPortNumber >= 1) ||
431 (HeadlessGlobals->UsedBiosSettings))
432 {
433 /* Get the EMS information */
434 HeadlessInfo->Serial.TerminalPort = HeadlessGlobals->
435 TerminalPortNumber;
436 HeadlessInfo->Serial.TerminalPortBaseAddress = HeadlessGlobals->
437 TerminalPortAddress;
438 }
439 else
440 {
441 /* We don't know for sure */
442 HeadlessInfo->Serial.TerminalPort = SerialPortUndefined;
443 HeadlessInfo->Serial.TerminalPortBaseAddress = 0;
444 }
445
446 /* All done */
447 Status = STATUS_SUCCESS;
448 break;
449 }
450
451 case HeadlessCmdAddLogEntry:
452 break;
453 case HeadlessCmdDisplayLog:
454 break;
455
456 case HeadlessCmdSetBlueScreenData:
457 {
458 /* Validate the existence of an input buffer */
459 if (!InputBuffer)
460 {
461 Status = STATUS_INVALID_PARAMETER;
462 break;
463 }
464
465 /* Lie so that we can get Hdl bringup a little bit further */
466 UNIMPLEMENTED;
467 Status = STATUS_SUCCESS;
468 break;
469 }
470
471 case HeadlessCmdSendBlueScreenData:
472 break;
473 case HeadlessCmdQueryGUID:
474 break;
475
476 case HeadlessCmdPutData:
477 {
478 /* Validate the existence of an input buffer */
479 if (!(InputBuffer) || !(InputBufferSize))
480 {
481 Status = STATUS_INVALID_PARAMETER;
482 break;
483 }
484
485 /* Terminal should be on */
486 if (HeadlessGlobals->TerminalEnabled)
487 {
488 /* Print each byte in the string making sure VT100 chars are used */
489 PutString = InputBuffer;
490 HdlspPutData(PutString->String, InputBufferSize);
491 }
492
493 /* Return success either way */
494 Status = STATUS_SUCCESS;
495 break;
496 }
497
498 default:
499 break;
500 }
501
502 /* Unset processing state */
503 if ((Command != HeadlessCmdAddLogEntry) &&
504 (Command != HeadlessCmdStartBugCheck) &&
505 (Command != HeadlessCmdSendBlueScreenData) &&
506 (Command != HeadlessCmdDoBugCheckProcessing))
507 {
508 ASSERT(HeadlessGlobals->ProcessingCmd == TRUE);
509 HeadlessGlobals->ProcessingCmd = FALSE;
510 }
511
512 /* All done */
513 return Status;
514 }
515
516 /*
517 * @implemented
518 */
519 NTSTATUS
520 NTAPI
521 HeadlessDispatch(IN HEADLESS_CMD Command,
522 IN PVOID InputBuffer,
523 IN SIZE_T InputBufferSize,
524 OUT PVOID OutputBuffer,
525 OUT PSIZE_T OutputBufferSize)
526 {
527 /* Check for stubs that will expect something even with headless off */
528 if (!HeadlessGlobals)
529 {
530 /* Don't allow the SAC to connect */
531 if (Command == HeadlessCmdEnableTerminal) return STATUS_UNSUCCESSFUL;
532
533 /* Send bogus reply */
534 if ((Command == HeadlessCmdQueryInformation) ||
535 (Command == HeadlessCmdGetByte) ||
536 (Command == HeadlessCmdGetLine) ||
537 (Command == HeadlessCmdCheckForReboot) ||
538 (Command == HeadlessCmdTerminalPoll))
539 {
540 if (!(OutputBuffer) || !(OutputBufferSize))
541 {
542 return STATUS_INVALID_PARAMETER;
543 }
544
545 RtlZeroMemory(OutputBuffer, *OutputBufferSize);
546 }
547
548 return STATUS_SUCCESS;
549 }
550
551 /* Do the real work */
552 return HdlspDispatch(Command,
553 InputBuffer,
554 InputBufferSize,
555 OutputBuffer,
556 OutputBufferSize);
557 }
558
559 /* EOF */