]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/DebugPortDxe/DebugPort.c
1. Use Mde library for Debug Port Module
[mirror_edk2.git] / MdeModulePkg / Universal / DebugPortDxe / DebugPort.c
1 /** @file
2 Top level C file for debugport driver. Contains initialization function.
3 This driver layers on top of SerialIo.
4 ALL CODE IN THE SERIALIO STACK MUST BE RE-ENTRANT AND CALLABLE FROM
5 INTERRUPT CONTEXT
6
7 Copyright (c) 2006 - 2008, Intel Corporation. <BR>
8 All rights reserved. This program and the accompanying materials
9 are licensed and made available under the terms and conditions of the BSD License
10 which accompanies this distribution. The full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php
12
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15
16 **/
17
18 #include "DebugPort.h"
19
20 //
21 // Globals
22 //
23 EFI_DRIVER_BINDING_PROTOCOL gDebugPortDriverBinding = {
24 DebugPortSupported,
25 DebugPortStart,
26 DebugPortStop,
27 DEBUGPORT_DRIVER_VERSION,
28 NULL,
29 NULL
30 };
31
32 DEBUGPORT_DEVICE mDebugPortDevice = {
33 DEBUGPORT_DEVICE_SIGNATURE,
34 (EFI_HANDLE) 0,
35 (EFI_HANDLE) 0,
36 (VOID *) NULL,
37 (EFI_DEVICE_PATH_PROTOCOL *) NULL,
38 {
39 DebugPortReset,
40 DebugPortRead,
41 DebugPortWrite,
42 DebugPortPoll
43 },
44 (EFI_HANDLE) 0,
45 (EFI_SERIAL_IO_PROTOCOL *) NULL,
46 DEBUGPORT_UART_DEFAULT_BAUDRATE,
47 DEBUGPORT_UART_DEFAULT_FIFO_DEPTH,
48 DEBUGPORT_UART_DEFAULT_TIMEOUT,
49 (EFI_PARITY_TYPE) DEBUGPORT_UART_DEFAULT_PARITY,
50 DEBUGPORT_UART_DEFAULT_DATA_BITS,
51 (EFI_STOP_BITS_TYPE) DEBUGPORT_UART_DEFAULT_STOP_BITS
52 };
53
54 /**
55 Local worker function to obtain device path information from DebugPort variable.
56
57 Records requested settings in DebugPort device structure.
58
59 **/
60 VOID
61 GetDebugPortVariable (
62 VOID
63 )
64 {
65 UINTN DataSize;
66 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
67 EFI_STATUS Status;
68
69 DataSize = 0;
70
71 Status = gRT->GetVariable (
72 (CHAR16 *) EFI_DEBUGPORT_VARIABLE_NAME,
73 &gEfiDebugPortVariableGuid,
74 NULL,
75 &DataSize,
76 mDebugPortDevice.DebugPortVariable
77 );
78
79 if (Status == EFI_BUFFER_TOO_SMALL) {
80 if (mDebugPortDevice.DebugPortVariable != NULL) {
81 FreePool (mDebugPortDevice.DebugPortVariable);
82 }
83
84 mDebugPortDevice.DebugPortVariable = AllocatePool (DataSize);
85 if (mDebugPortDevice.DebugPortVariable != NULL) {
86 gRT->GetVariable (
87 (CHAR16 *) EFI_DEBUGPORT_VARIABLE_NAME,
88 &gEfiDebugPortVariableGuid,
89 NULL,
90 &DataSize,
91 mDebugPortDevice.DebugPortVariable
92 );
93 DevicePath = (EFI_DEVICE_PATH_PROTOCOL *) mDebugPortDevice.DebugPortVariable;
94 while (!IsDevicePathEnd (DevicePath) && !IS_UART_DEVICEPATH (DevicePath)) {
95 DevicePath = NextDevicePathNode (DevicePath);
96 }
97
98 if (IsDevicePathEnd (DevicePath)) {
99 FreePool (mDebugPortDevice.DebugPortVariable);
100 mDebugPortDevice.DebugPortVariable = NULL;
101 } else {
102 CopyMem (
103 &mDebugPortDevice.BaudRate,
104 &((UART_DEVICE_PATH *) DevicePath)->BaudRate,
105 sizeof (((UART_DEVICE_PATH *) DevicePath)->BaudRate)
106 );
107 mDebugPortDevice.ReceiveFifoDepth = DEBUGPORT_UART_DEFAULT_FIFO_DEPTH;
108 mDebugPortDevice.Timeout = DEBUGPORT_UART_DEFAULT_TIMEOUT;
109 CopyMem (
110 &mDebugPortDevice.Parity,
111 &((UART_DEVICE_PATH *) DevicePath)->Parity,
112 sizeof (((UART_DEVICE_PATH *) DevicePath)->Parity)
113 );
114 CopyMem (
115 &mDebugPortDevice.DataBits,
116 &((UART_DEVICE_PATH *) DevicePath)->DataBits,
117 sizeof (((UART_DEVICE_PATH *) DevicePath)->DataBits)
118 );
119 CopyMem (
120 &mDebugPortDevice.StopBits,
121 &((UART_DEVICE_PATH *) DevicePath)->StopBits,
122 sizeof (((UART_DEVICE_PATH *) DevicePath)->StopBits)
123 );
124 }
125 }
126 }
127 }
128
129 /**
130 Debug Port Driver entry point.
131
132 Reads DebugPort variable to determine what device and settings to use as the
133 debug port. Binds exclusively to SerialIo. Reverts to defaults if no variable
134 is found.
135
136 @param[in] ImageHandle The firmware allocated handle for the EFI image.
137 @param[in] SystemTable A pointer to the EFI System Table.
138
139 @retval EFI_SUCCESS The entry point is executed successfully.
140 @retval EFI_OUT_OF_RESOURCES Fails to allocate memory for device.
141 @retval other Some error occurs when executing this entry point.
142
143 **/
144 EFI_STATUS
145 EFIAPI
146 InitializeDebugPortDriver (
147 IN EFI_HANDLE ImageHandle,
148 IN EFI_SYSTEM_TABLE *SystemTable
149 )
150 {
151 EFI_STATUS Status;
152
153 //
154 // Install driver model protocol(s).
155 //
156 Status = EfiLibInstallDriverBindingComponentName2 (
157 ImageHandle,
158 SystemTable,
159 &gDebugPortDriverBinding,
160 ImageHandle,
161 &gDebugPortComponentName,
162 &gDebugPortComponentName2
163 );
164 ASSERT_EFI_ERROR (Status);
165
166 return EFI_SUCCESS;
167 }
168
169 /**
170 Checks to see if there's not already a DebugPort interface somewhere.
171
172 If there's a DEBUGPORT variable, the device path must match exactly. If there's
173 no DEBUGPORT variable, then device path is not checked and does not matter.
174 Checks to see that there's a serial io interface on the controller handle
175 that can be bound BY_DRIVER | EXCLUSIVE.
176 If all these tests succeed, then we return EFI_SUCCESS, else, EFI_UNSUPPORTED
177 or other error returned by OpenProtocol.
178
179 @param This Protocol instance pointer.
180 @param ControllerHandle Handle of device to test.
181 @param RemainingDevicePath Optional parameter use to pick a specific child
182 device to start.
183
184 @retval EFI_SUCCESS This driver supports this device.
185 @retval EFI_UNSUPPORTED Debug Port device is not supported.
186 @retval EFI_OUT_OF_RESOURCES Fails to allocate memory for device.
187 @retval others Some error occurs.
188
189 **/
190 EFI_STATUS
191 EFIAPI
192 DebugPortSupported (
193 IN EFI_DRIVER_BINDING_PROTOCOL *This,
194 IN EFI_HANDLE ControllerHandle,
195 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
196 )
197 {
198 EFI_STATUS Status;
199 EFI_DEVICE_PATH_PROTOCOL *Dp1;
200 EFI_DEVICE_PATH_PROTOCOL *Dp2;
201 EFI_SERIAL_IO_PROTOCOL *SerialIo;
202 EFI_DEBUGPORT_PROTOCOL *DebugPortInterface;
203 EFI_HANDLE TempHandle;
204
205 //
206 // Check to see that there's not a debugport protocol already published,
207 // since only one standard UART serial port could be supported by this driver.
208 //
209 if (gBS->LocateProtocol (&gEfiDebugPortProtocolGuid, NULL, (VOID **) &DebugPortInterface) != EFI_NOT_FOUND) {
210 return EFI_UNSUPPORTED;
211 }
212 //
213 // Read DebugPort variable to determine debug port selection and parameters
214 //
215 GetDebugPortVariable ();
216
217 if (mDebugPortDevice.DebugPortVariable != NULL) {
218 //
219 // There's a DEBUGPORT variable, so do LocateDevicePath and check to see if
220 // the closest matching handle matches the controller handle, and if it does,
221 // check to see that the remaining device path has the DebugPort GUIDed messaging
222 // device path only. Otherwise, it's a mismatch and EFI_UNSUPPORTED is returned.
223 //
224 Dp1 = DuplicateDevicePath ((EFI_DEVICE_PATH_PROTOCOL *) mDebugPortDevice.DebugPortVariable);
225 if (Dp1 == NULL) {
226 return EFI_OUT_OF_RESOURCES;
227 }
228
229 Dp2 = Dp1;
230
231 Status = gBS->LocateDevicePath (
232 &gEfiSerialIoProtocolGuid,
233 &Dp2,
234 &TempHandle
235 );
236
237 if (Status == EFI_SUCCESS && TempHandle != ControllerHandle) {
238 Status = EFI_UNSUPPORTED;
239 }
240
241 if (Status == EFI_SUCCESS &&
242 (Dp2->Type != MESSAGING_DEVICE_PATH ||
243 Dp2->SubType != MSG_VENDOR_DP ||
244 *((UINT16 *) Dp2->Length) != sizeof (DEBUGPORT_DEVICE_PATH))) {
245
246 Status = EFI_UNSUPPORTED;
247 }
248
249 if (Status == EFI_SUCCESS && !CompareGuid (&gEfiDebugPortDevicePathGuid, (GUID *) (Dp2 + 1))) {
250 Status = EFI_UNSUPPORTED;
251 }
252
253 FreePool (Dp1);
254 if (EFI_ERROR (Status)) {
255 return Status;
256 }
257 }
258
259 Status = gBS->OpenProtocol (
260 ControllerHandle,
261 &gEfiSerialIoProtocolGuid,
262 (VOID **) &SerialIo,
263 This->DriverBindingHandle,
264 ControllerHandle,
265 EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE
266 );
267 if (EFI_ERROR (Status)) {
268 return Status;
269 }
270
271 gBS->CloseProtocol (
272 ControllerHandle,
273 &gEfiSerialIoProtocolGuid,
274 This->DriverBindingHandle,
275 ControllerHandle
276 );
277
278 return EFI_SUCCESS;
279 }
280
281 /**
282 Binds exclusively to serial io on the controller handle, Produces DebugPort
283 protocol and DevicePath on new handle.
284
285 @param This Protocol instance pointer.
286 @param ControllerHandle Handle of device to bind driver to.
287 @param RemainingDevicePath Optional parameter use to pick a specific child
288 device to start.
289
290 @retval EFI_SUCCESS This driver is added to ControllerHandle.
291 @retval EFI_OUT_OF_RESOURCES Fails to allocate memory for device.
292 @retval others Some error occurs.
293
294 **/
295 EFI_STATUS
296 EFIAPI
297 DebugPortStart (
298 IN EFI_DRIVER_BINDING_PROTOCOL *This,
299 IN EFI_HANDLE ControllerHandle,
300 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
301 )
302 {
303 EFI_STATUS Status;
304 DEBUGPORT_DEVICE_PATH DebugPortDP;
305 EFI_DEVICE_PATH_PROTOCOL EndDP;
306 EFI_DEVICE_PATH_PROTOCOL *Dp1;
307
308 Status = gBS->OpenProtocol (
309 ControllerHandle,
310 &gEfiSerialIoProtocolGuid,
311 (VOID **) &mDebugPortDevice.SerialIoBinding,
312 This->DriverBindingHandle,
313 ControllerHandle,
314 EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE
315 );
316 if (EFI_ERROR (Status)) {
317 return Status;
318 }
319
320 mDebugPortDevice.SerialIoDeviceHandle = ControllerHandle;
321
322 //
323 // Initialize the Serial Io interface...
324 //
325 Status = mDebugPortDevice.SerialIoBinding->SetAttributes (
326 mDebugPortDevice.SerialIoBinding,
327 mDebugPortDevice.BaudRate,
328 mDebugPortDevice.ReceiveFifoDepth,
329 mDebugPortDevice.Timeout,
330 mDebugPortDevice.Parity,
331 mDebugPortDevice.DataBits,
332 mDebugPortDevice.StopBits
333 );
334 if (EFI_ERROR (Status)) {
335 mDebugPortDevice.BaudRate = 0;
336 mDebugPortDevice.Parity = DefaultParity;
337 mDebugPortDevice.DataBits = 0;
338 mDebugPortDevice.StopBits = DefaultStopBits;
339 mDebugPortDevice.ReceiveFifoDepth = 0;
340 Status = mDebugPortDevice.SerialIoBinding->SetAttributes (
341 mDebugPortDevice.SerialIoBinding,
342 mDebugPortDevice.BaudRate,
343 mDebugPortDevice.ReceiveFifoDepth,
344 mDebugPortDevice.Timeout,
345 mDebugPortDevice.Parity,
346 mDebugPortDevice.DataBits,
347 mDebugPortDevice.StopBits
348 );
349 if (EFI_ERROR (Status)) {
350 gBS->CloseProtocol (
351 ControllerHandle,
352 &gEfiSerialIoProtocolGuid,
353 This->DriverBindingHandle,
354 ControllerHandle
355 );
356 return Status;
357 }
358 }
359
360 mDebugPortDevice.SerialIoBinding->Reset (mDebugPortDevice.SerialIoBinding);
361
362 //
363 // Create device path instance for DebugPort
364 //
365 DebugPortDP.Header.Type = MESSAGING_DEVICE_PATH;
366 DebugPortDP.Header.SubType = MSG_VENDOR_DP;
367 SetDevicePathNodeLength (&(DebugPortDP.Header), sizeof (DebugPortDP));
368 CopyGuid (&DebugPortDP.Guid, &gEfiDebugPortDevicePathGuid);
369
370 Dp1 = DevicePathFromHandle (ControllerHandle);
371 if (Dp1 == NULL) {
372 Dp1 = &EndDP;
373 SetDevicePathEndNode (Dp1);
374 }
375
376 mDebugPortDevice.DebugPortDevicePath = AppendDevicePathNode (Dp1, (EFI_DEVICE_PATH_PROTOCOL *) &DebugPortDP);
377 if (mDebugPortDevice.DebugPortDevicePath == NULL) {
378 return EFI_OUT_OF_RESOURCES;
379 }
380 //
381 // Publish DebugPort and Device Path protocols
382 //
383 Status = gBS->InstallMultipleProtocolInterfaces (
384 &mDebugPortDevice.DebugPortDeviceHandle,
385 &gEfiDevicePathProtocolGuid,
386 mDebugPortDevice.DebugPortDevicePath,
387 &gEfiDebugPortProtocolGuid,
388 &mDebugPortDevice.DebugPortInterface,
389 NULL
390 );
391
392 if (EFI_ERROR (Status)) {
393 gBS->CloseProtocol (
394 ControllerHandle,
395 &gEfiSerialIoProtocolGuid,
396 This->DriverBindingHandle,
397 ControllerHandle
398 );
399 return Status;
400 }
401 //
402 // Connect debugport child to serial io
403 //
404 Status = gBS->OpenProtocol (
405 ControllerHandle,
406 &gEfiSerialIoProtocolGuid,
407 (VOID **) &mDebugPortDevice.SerialIoBinding,
408 This->DriverBindingHandle,
409 mDebugPortDevice.DebugPortDeviceHandle,
410 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
411 );
412
413 if (EFI_ERROR (Status)) {
414 DEBUG_CODE_BEGIN ();
415 UINTN BufferSize;
416
417 BufferSize = 48;
418 DebugPortWrite (
419 &mDebugPortDevice.DebugPortInterface,
420 0,
421 &BufferSize,
422 "DebugPort driver failed to open child controller\n\n"
423 );
424 DEBUG_CODE_END ();
425
426 gBS->CloseProtocol (
427 ControllerHandle,
428 &gEfiSerialIoProtocolGuid,
429 This->DriverBindingHandle,
430 ControllerHandle
431 );
432 return Status;
433 }
434
435 DEBUG_CODE_BEGIN ();
436 UINTN BufferSize;
437
438 BufferSize = 38;
439 DebugPortWrite (
440 &mDebugPortDevice.DebugPortInterface,
441 0,
442 &BufferSize,
443 "Hello World from the DebugPort driver\n\n"
444 );
445
446 DEBUG_CODE_END ();
447
448 return EFI_SUCCESS;
449 }
450
451 /**
452 Stop this driver on ControllerHandle by removing Serial IO protocol on
453 the ControllerHandle.
454
455 @param This Protocol instance pointer.
456 @param ControllerHandle Handle of device to stop driver on
457 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
458 children is zero stop the entire bus driver.
459 @param ChildHandleBuffer List of Child Handles to Stop.
460
461 @retval EFI_SUCCESS This driver is removed ControllerHandle.
462 @retval other This driver was not removed from this device.
463
464 **/
465 EFI_STATUS
466 EFIAPI
467 DebugPortStop (
468 IN EFI_DRIVER_BINDING_PROTOCOL *This,
469 IN EFI_HANDLE ControllerHandle,
470 IN UINTN NumberOfChildren,
471 IN EFI_HANDLE *ChildHandleBuffer
472 )
473 {
474 EFI_STATUS Status;
475
476 if (NumberOfChildren == 0) {
477 //
478 // Close the bus driver
479 //
480 gBS->CloseProtocol (
481 ControllerHandle,
482 &gEfiSerialIoProtocolGuid,
483 This->DriverBindingHandle,
484 ControllerHandle
485 );
486
487 mDebugPortDevice.SerialIoBinding = NULL;
488
489 gBS->CloseProtocol (
490 ControllerHandle,
491 &gEfiDevicePathProtocolGuid,
492 This->DriverBindingHandle,
493 ControllerHandle
494 );
495
496 FreePool (mDebugPortDevice.DebugPortDevicePath);
497
498 return EFI_SUCCESS;
499 } else {
500 //
501 // Disconnect SerialIo child handle
502 //
503 Status = gBS->CloseProtocol (
504 mDebugPortDevice.SerialIoDeviceHandle,
505 &gEfiSerialIoProtocolGuid,
506 This->DriverBindingHandle,
507 mDebugPortDevice.DebugPortDeviceHandle
508 );
509
510 if (EFI_ERROR (Status)) {
511 return Status;
512 }
513 //
514 // Unpublish our protocols (DevicePath, DebugPort)
515 //
516 Status = gBS->UninstallMultipleProtocolInterfaces (
517 mDebugPortDevice.DebugPortDeviceHandle,
518 &gEfiDevicePathProtocolGuid,
519 mDebugPortDevice.DebugPortDevicePath,
520 &gEfiDebugPortProtocolGuid,
521 &mDebugPortDevice.DebugPortInterface,
522 NULL
523 );
524
525 if (EFI_ERROR (Status)) {
526 gBS->OpenProtocol (
527 ControllerHandle,
528 &gEfiSerialIoProtocolGuid,
529 (VOID **) &mDebugPortDevice.SerialIoBinding,
530 This->DriverBindingHandle,
531 mDebugPortDevice.DebugPortDeviceHandle,
532 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
533 );
534 } else {
535 mDebugPortDevice.DebugPortDeviceHandle = NULL;
536 }
537 }
538
539 return Status;
540 }
541
542 /**
543 DebugPort protocol member function. Calls SerialIo:GetControl to flush buffer.
544 We cannot call SerialIo:SetAttributes because it uses pool services, which use
545 locks, which affect TPL, so it's not interrupt context safe or re-entrant.
546 SerialIo:Reset() calls SetAttributes, so it can't be used either.
547
548 The port itself should be fine since it was set up during initialization.
549
550 @param This Protocol instance pointer.
551
552 @return EFI_SUCCESS Always.
553
554 **/
555 EFI_STATUS
556 EFIAPI
557 DebugPortReset (
558 IN EFI_DEBUGPORT_PROTOCOL *This
559 )
560 {
561 UINTN BufferSize;
562 UINTN BitBucket;
563
564 while (This->Poll (This) == EFI_SUCCESS) {
565 BufferSize = 1;
566 This->Read (This, 0, &BufferSize, &BitBucket);
567 }
568
569 return EFI_SUCCESS;
570 }
571
572 /**
573 DebugPort protocol member function. Calls SerialIo:Read() after setting
574 if it's different than the last SerialIo access.
575
576 @param This Pointer to DebugPort protocol.
577 @param Timeout Timeout value.
578 @param BufferSize On input, the size of Buffer.
579 On output, the amount of data actually written.
580 @param Buffer Pointer to buffer to read.
581
582 @retval EFI_SUCCESS
583 @retval others
584
585 **/
586 EFI_STATUS
587 EFIAPI
588 DebugPortRead (
589 IN EFI_DEBUGPORT_PROTOCOL *This,
590 IN UINT32 Timeout,
591 IN OUT UINTN *BufferSize,
592 IN VOID *Buffer
593 )
594 {
595 DEBUGPORT_DEVICE *DebugPortDevice;
596 UINTN LocalBufferSize;
597 EFI_STATUS Status;
598 UINT8 *BufferPtr;
599
600 DebugPortDevice = DEBUGPORT_DEVICE_FROM_THIS (This);
601 BufferPtr = Buffer;
602 LocalBufferSize = *BufferSize;
603
604 do {
605 Status = DebugPortDevice->SerialIoBinding->Read (
606 DebugPortDevice->SerialIoBinding,
607 &LocalBufferSize,
608 BufferPtr
609 );
610 if (Status == EFI_TIMEOUT) {
611 if (Timeout > DEBUGPORT_UART_DEFAULT_TIMEOUT) {
612 Timeout -= DEBUGPORT_UART_DEFAULT_TIMEOUT;
613 } else {
614 Timeout = 0;
615 }
616 } else if (EFI_ERROR (Status)) {
617 break;
618 }
619
620 BufferPtr += LocalBufferSize;
621 LocalBufferSize = *BufferSize - (BufferPtr - (UINT8 *) Buffer);
622 } while (LocalBufferSize != 0 && Timeout > 0);
623
624 *BufferSize = (UINTN) (BufferPtr - (UINT8 *) Buffer);
625
626 return Status;
627 }
628
629 /**
630 DebugPort protocol member function. Calls SerialIo:Write() Writes 8 bytes at
631 a time and does a GetControl between 8 byte writes to help insure reads are
632 interspersed This is poor-man's flow control.
633
634 @param This Pointer to DebugPort protocol.
635 @param Timeout Timeout value.
636 @param BufferSize On input, the size of Buffer.
637 On output, the amount of data actually written.
638 @param Buffer Pointer to buffer to read.
639
640 @retval EFI_SUCCESS The data was written.
641 @retval others Fails when writting datas to debug port device.
642
643 **/
644 EFI_STATUS
645 EFIAPI
646 DebugPortWrite (
647 IN EFI_DEBUGPORT_PROTOCOL *This,
648 IN UINT32 Timeout,
649 IN OUT UINTN *BufferSize,
650 OUT VOID *Buffer
651 )
652 {
653 DEBUGPORT_DEVICE *DebugPortDevice;
654 UINTN Position;
655 UINTN WriteSize;
656 EFI_STATUS Status;
657 UINT32 SerialControl;
658
659 Status = EFI_SUCCESS;
660 DebugPortDevice = DEBUGPORT_DEVICE_FROM_THIS (This);
661
662 WriteSize = 8;
663 for (Position = 0; Position < *BufferSize && !EFI_ERROR (Status); Position += WriteSize) {
664 DebugPortDevice->SerialIoBinding->GetControl (
665 DebugPortDevice->SerialIoBinding,
666 &SerialControl
667 );
668 if (*BufferSize - Position < 8) {
669 WriteSize = *BufferSize - Position;
670 }
671
672 Status = DebugPortDevice->SerialIoBinding->Write (
673 DebugPortDevice->SerialIoBinding,
674 &WriteSize,
675 &((UINT8 *) Buffer)[Position]
676 );
677 }
678
679 *BufferSize = Position;
680 return Status;
681 }
682
683 /**
684 DebugPort protocol member function. Calls SerialIo:Write() after setting
685 if it's different than the last SerialIo access.
686
687 @param This Pointer to DebugPort protocol.
688
689 @retval EFI_SUCCESS At least 1 character is ready to be read from
690 the DebugPort interface.
691 @retval EFI_NOT_READY There are no characters ready to read from the
692 DebugPort interface
693 @retval EFI_DEVICE_ERROR A hardware failure occured... (from SerialIo)
694
695 **/
696 EFI_STATUS
697 EFIAPI
698 DebugPortPoll (
699 IN EFI_DEBUGPORT_PROTOCOL *This
700 )
701 {
702 EFI_STATUS Status;
703 UINT32 SerialControl;
704 DEBUGPORT_DEVICE *DebugPortDevice;
705
706 DebugPortDevice = DEBUGPORT_DEVICE_FROM_THIS (This);
707
708 Status = DebugPortDevice->SerialIoBinding->GetControl (
709 DebugPortDevice->SerialIoBinding,
710 &SerialControl
711 );
712
713 if (!EFI_ERROR (Status)) {
714 if ((SerialControl & EFI_SERIAL_INPUT_BUFFER_EMPTY) != 0) {
715 Status = EFI_NOT_READY;
716 } else {
717 Status = EFI_SUCCESS;
718 }
719 }
720
721 return Status;
722 }
723
724 /**
725 Unload function that is registered in the LoadImage protocol. It un-installs
726 protocols produced and deallocates pool used by the driver. Called by the core
727 when unloading the driver.
728
729 @param ImageHandle
730
731 @retval EFI_SUCCESS Unload Debug Port driver successfully.
732 @retval EFI_ABORTED Serial IO is still binding.
733
734 **/
735 EFI_STATUS
736 EFIAPI
737 ImageUnloadHandler (
738 EFI_HANDLE ImageHandle
739 )
740 {
741 if (mDebugPortDevice.SerialIoBinding != NULL) {
742 return EFI_ABORTED;
743 }
744
745 //
746 // Clean up allocations
747 //
748 if (mDebugPortDevice.DebugPortVariable != NULL) {
749 FreePool (mDebugPortDevice.DebugPortVariable);
750 }
751
752 return EFI_SUCCESS;
753 }