]> git.proxmox.com Git - mirror_edk2.git/blob - EmbeddedPkg/Library/GdbSerialDebugPortLib/GdbSerialDebugPortLib.c
9f0f437c1dbd7aae934d904b60e371e839be95b1
[mirror_edk2.git] / EmbeddedPkg / Library / GdbSerialDebugPortLib / GdbSerialDebugPortLib.c
1 /** @file
2 Basic serial IO abstraction for GDB
3
4 Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
5
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7
8 **/
9
10 #include <Uefi.h>
11 #include <Library/GdbSerialLib.h>
12 #include <Library/PcdLib.h>
13 #include <Library/IoLib.h>
14 #include <Library/DebugLib.h>
15 #include <Library/UefiBootServicesTableLib.h>
16
17 #include <Protocol/DebugPort.h>
18
19 EFI_DEBUGPORT_PROTOCOL *gDebugPort = NULL;
20 UINT32 gTimeOut = 0;
21
22 /**
23 The constructor function initializes the UART.
24
25 @param ImageHandle The firmware allocated handle for the EFI image.
26 @param SystemTable A pointer to the EFI System Table.
27
28 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
29
30 **/
31 RETURN_STATUS
32 EFIAPI
33 GdbSerialLibDebugPortConstructor (
34 IN EFI_HANDLE ImageHandle,
35 IN EFI_SYSTEM_TABLE *SystemTable
36 )
37 {
38 EFI_STATUS Status;
39
40 Status = gBS->LocateProtocol (&gEfiDebugPortProtocolGuid, NULL, (VOID **)&gDebugPort);
41 if (!EFI_ERROR (Status)) {
42 gTimeOut = PcdGet32 (PcdGdbMaxPacketRetryCount);
43 gDebugPort->Reset (gDebugPort);
44 }
45
46 return Status;
47 }
48
49 /**
50 Sets the baud rate, receive FIFO depth, transmit/receive time out, parity,
51 data buts, and stop bits on a serial device. This call is optional as the serial
52 port will be set up with defaults base on PCD values.
53
54 @param BaudRate The requested baud rate. A BaudRate value of 0 will use the
55 device's default interface speed.
56 @param Parity The type of parity to use on this serial device. A Parity value of
57 DefaultParity will use the device's default parity value.
58 @param DataBits The number of data bits to use on the serial device. A DataBits
59 value of 0 will use the device's default data bit setting.
60 @param StopBits The number of stop bits to use on this serial device. A StopBits
61 value of DefaultStopBits will use the device's default number of
62 stop bits.
63
64 @retval EFI_SUCCESS The device was configured.
65 @retval EFI_DEVICE_ERROR The serial device could not be configured.
66
67 **/
68 RETURN_STATUS
69 EFIAPI
70 GdbSerialInit (
71 IN UINT64 BaudRate,
72 IN UINT8 Parity,
73 IN UINT8 DataBits,
74 IN UINT8 StopBits
75 )
76 {
77 EFI_STATUS Status;
78
79 Status = gDebugPort->Reset (gDebugPort);
80 return Status;
81 }
82
83 /**
84 Check to see if a character is available from GDB. Do not read the character as that is
85 done via GdbGetChar().
86
87 @return TRUE - Character available
88 @return FALSE - Character not available
89
90 **/
91 BOOLEAN
92 EFIAPI
93 GdbIsCharAvailable (
94 VOID
95 )
96 {
97 EFI_STATUS Status;
98
99 Status = gDebugPort->Poll (gDebugPort);
100
101 return (Status == EFI_SUCCESS ? TRUE : FALSE);
102 }
103
104 /**
105 Get a character from GDB. This function must be able to run in interrupt context.
106
107 @return A character from GDB
108
109 **/
110 CHAR8
111 EFIAPI
112 GdbGetChar (
113 VOID
114 )
115 {
116 EFI_STATUS Status;
117 CHAR8 Char;
118 UINTN BufferSize;
119
120 do {
121 BufferSize = sizeof (Char);
122 Status = gDebugPort->Read (gDebugPort, gTimeOut, &BufferSize, &Char);
123 } while (EFI_ERROR (Status) || BufferSize != sizeof (Char));
124
125 return Char;
126 }
127
128 /**
129 Send a character to GDB. This function must be able to run in interrupt context.
130
131
132 @param Char Send a character to GDB
133
134 **/
135 VOID
136 EFIAPI
137 GdbPutChar (
138 IN CHAR8 Char
139 )
140 {
141 EFI_STATUS Status;
142 UINTN BufferSize;
143
144 do {
145 BufferSize = sizeof (Char);
146 Status = gDebugPort->Write (gDebugPort, gTimeOut, &BufferSize, &Char);
147 } while (EFI_ERROR (Status) || BufferSize != sizeof (Char));
148
149 return;
150 }
151
152 /**
153 Send an ASCII string to GDB. This function must be able to run in interrupt context.
154
155
156 @param String Send a string to GDB
157
158 **/
159 VOID
160 GdbPutString (
161 IN CHAR8 *String
162 )
163 {
164 // We could performance enhance this function by calling gDebugPort->Write ()
165 while (*String != '\0') {
166 GdbPutChar (*String);
167 String++;
168 }
169 }