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