]> git.proxmox.com Git - mirror_edk2.git/blame - SourceLevelDebugPkg/Library/DebugCommunicationLibSerialPort/DebugCommunicationLibSerialPort.c
MdeModulePkg: add error handling when DXE IPL PPI is not found.
[mirror_edk2.git] / SourceLevelDebugPkg / Library / DebugCommunicationLibSerialPort / DebugCommunicationLibSerialPort.c
CommitLineData
18b144ea 1/** @file\r
2 Debug Port Library implementation based on serial port.\r
3\r
f95e6f6b 4 Copyright (c) 2010 - 2014, Intel Corporation. All rights reserved.<BR>\r
18b144ea 5 This program and the accompanying materials\r
6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php.\r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include <Base.h>\r
16\r
17#include <Library/DebugCommunicationLib.h>\r
18#include <Library/SerialPortLib.h>\r
19#include <Library/TimerLib.h>\r
e2104834 20#include <Library/DebugLib.h>\r
b422b62c 21#include <Library/BaseLib.h>\r
22#include <Library/BaseMemoryLib.h>\r
23\r
24#pragma pack(1)\r
25//\r
26// The internal data structure of DEBUG_PORT_HANDLE, which stores some\r
27// important datum which are used across various phases.\r
28//\r
29typedef struct _SERIAL_DEBUG_PORT_HANDLE{\r
30 //\r
31 // Timter settings\r
32 //\r
33 UINT64 TimerFrequency;\r
34 UINT64 TimerCycle;\r
35 BOOLEAN TimerCountDown;\r
36} SERIAL_DEBUG_PORT_HANDLE;\r
37#pragma pack()\r
38\r
39//\r
40// The global variable which can be used after memory is ready.\r
41//\r
42SERIAL_DEBUG_PORT_HANDLE mSerialDebugPortHandle;\r
43\r
44/**\r
45 Check if the timer is timeout.\r
46 \r
47 @param[in] SerialDebugPortHandle Pointer to Serial Debug port handle\r
48 @param[in] Timer The start timer from the begin.\r
49 @param[in] TimeoutTicker Ticker number need time out.\r
50\r
51 @return TRUE Timer time out occurs.\r
52 @retval FALSE Timer does not time out.\r
53\r
54**/\r
55BOOLEAN\r
56IsTimerTimeout (\r
57 IN SERIAL_DEBUG_PORT_HANDLE *SerialDebugPortHandle,\r
58 IN UINT64 Timer,\r
59 IN UINT64 TimeoutTicker\r
60 )\r
61{\r
62 UINT64 CurrentTimer;\r
63 UINT64 Delta;\r
64\r
65 CurrentTimer = GetPerformanceCounter ();\r
66\r
67 if (SerialDebugPortHandle->TimerCountDown) {\r
68 //\r
69 // The timer counter counts down. Check for roll over condition.\r
70 //\r
71 if (CurrentTimer < Timer) {\r
72 Delta = Timer - CurrentTimer;\r
73 } else {\r
74 //\r
75 // Handle one roll-over. \r
76 //\r
77 Delta = SerialDebugPortHandle->TimerCycle - (CurrentTimer - Timer);\r
78 }\r
79 } else {\r
80 //\r
81 // The timer counter counts up. Check for roll over condition.\r
82 //\r
83 if (CurrentTimer > Timer) {\r
84 Delta = CurrentTimer - Timer;\r
85 } else {\r
86 //\r
87 // Handle one roll-over. \r
88 //\r
89 Delta = SerialDebugPortHandle->TimerCycle - (Timer - CurrentTimer);\r
90 }\r
91 }\r
92 \r
93 return (BOOLEAN) (Delta >= TimeoutTicker);\r
94}\r
18b144ea 95\r
96/**\r
97 Initialize the debug port.\r
98\r
99 This function will initialize debug port to get it ready for data transmition. If\r
100 certain Debug Communication Library instance has to save some private data in the\r
101 stack, this function must work on the mode that doesn't return to the caller, then\r
102 the caller needs to wrap up all rest of logic after DebugPortInitialize() into one\r
103 function and pass it into DebugPortInitialize(). DebugPortInitialize() is\r
104 responsible to invoke the passing-in funciton at the end of DebugPortInitialize().\r
105\r
106 If the paramter Function is not NULL, Debug Communication Libary instance will\r
107 invoke it by passing in the Context to be the first parameter. Debug Communication\r
108 Library instance could create one debug port handle to be the second parameter\r
109 passing into the Function. Debug Communication Library instance also could pass\r
110 NULL to be the second parameter if it doesn't create the debug port handle.\r
111\r
112 If the parameter Function is NULL, and Context is not NULL. At this time, Context\r
113 is the debug port handle created by the previous Debug Communication Library\r
114 instance.\r
115 a) If the instance can understand and continue use the private data of the previous\r
116 instance, it could return the same handle as passed in (as Context parameter).\r
117 b) If the instance does not understand, or does not want to continue use the\r
118 private data of the previous instance, it could ignore the input Context parameter\r
f95e6f6b 119 and create the new handle to be returned.\r
18b144ea 120\r
121 If Function() is NULL and Context is NULL, Debug Communication Library could create a\r
122 new handle and return it. NULL is also a valid handle to be returned.\r
123\r
124 @param[in] Context Context needed by callback function; it was optional.\r
125 @param[in] Function Continue function called by Debug Communication library;\r
126 it was optional.\r
127\r
128 @return The debug port handle created by Debug Communication Library if Function\r
129 is not NULL.\r
130\r
131**/\r
132DEBUG_PORT_HANDLE\r
133EFIAPI\r
134DebugPortInitialize (\r
135 IN VOID *Context,\r
136 IN DEBUG_PORT_CONTINUE Function\r
137 )\r
138{\r
b422b62c 139 RETURN_STATUS Status;\r
140 SERIAL_DEBUG_PORT_HANDLE Handle;\r
141 SERIAL_DEBUG_PORT_HANDLE *SerialDebugPortHandle;\r
142 UINT64 TimerStartValue;\r
143 UINT64 TimerEndValue;\r
144\r
145 //\r
146 // Validate the PCD PcdDebugPortHandleBufferSize value \r
147 //\r
148 ASSERT (PcdGet16 (PcdDebugPortHandleBufferSize) == sizeof (SERIAL_DEBUG_PORT_HANDLE));\r
149\r
150 if (Context != NULL && Function == NULL) {\r
151 SerialDebugPortHandle = (SERIAL_DEBUG_PORT_HANDLE *)Context;\r
152 } else {\r
153 ZeroMem (&Handle, sizeof (SERIAL_DEBUG_PORT_HANDLE));\r
154 SerialDebugPortHandle = &Handle;\r
155 }\r
156 SerialDebugPortHandle->TimerFrequency = GetPerformanceCounterProperties (\r
157 &TimerStartValue,\r
158 &TimerEndValue\r
159 );\r
160 DEBUG ((EFI_D_INFO, "Serial Debug Port: TimerFrequency = 0x%lx\n", SerialDebugPortHandle->TimerFrequency)); \r
161 DEBUG ((EFI_D_INFO, "Serial Debug Port: TimerStartValue = 0x%lx\n", TimerStartValue)); \r
162 DEBUG ((EFI_D_INFO, "Serial Debug Port: TimerEndValue = 0x%lx\n", TimerEndValue)); \r
163\r
164 if (TimerEndValue < TimerStartValue) {\r
165 SerialDebugPortHandle->TimerCountDown = TRUE;\r
166 SerialDebugPortHandle->TimerCycle = TimerStartValue - TimerEndValue;\r
167 } else {\r
168 SerialDebugPortHandle->TimerCountDown = FALSE;\r
169 SerialDebugPortHandle->TimerCycle = TimerEndValue - TimerStartValue;\r
170 } \r
171\r
172 if (Function == NULL && Context != NULL) {\r
173 return (DEBUG_PORT_HANDLE *) Context;\r
174 }\r
e2104834 175\r
176 Status = SerialPortInitialize ();\r
177 if (RETURN_ERROR(Status)) {\r
178 DEBUG ((EFI_D_ERROR, "Debug Serial Port: Initialization failed!\n")); \r
179 }\r
18b144ea 180\r
181 if (Function != NULL) {\r
b422b62c 182 Function (Context, SerialDebugPortHandle);\r
183 } else {\r
184 CopyMem(&mSerialDebugPortHandle, SerialDebugPortHandle, sizeof (SERIAL_DEBUG_PORT_HANDLE));\r
18b144ea 185 }\r
186\r
b422b62c 187 return (DEBUG_PORT_HANDLE)(UINTN)&mSerialDebugPortHandle;\r
18b144ea 188}\r
189\r
190/**\r
191 Read data from debug device and save the datas in buffer.\r
192\r
193 Reads NumberOfBytes data bytes from a debug device into the buffer\r
194 specified by Buffer. The number of bytes actually read is returned.\r
195 If the return value is less than NumberOfBytes, then the rest operation failed.\r
196 If NumberOfBytes is zero, then return 0.\r
197\r
198 @param Handle Debug port handle.\r
199 @param Buffer Pointer to the data buffer to store the data read from the debug device.\r
200 @param NumberOfBytes Number of bytes which will be read.\r
201 @param Timeout Timeout value for reading from debug device. It unit is Microsecond.\r
202\r
203 @retval 0 Read data failed, no data is to be read.\r
204 @retval >0 Actual number of bytes read from debug device.\r
205\r
206**/\r
207UINTN\r
208EFIAPI\r
209DebugPortReadBuffer (\r
210 IN DEBUG_PORT_HANDLE Handle,\r
211 IN UINT8 *Buffer,\r
212 IN UINTN NumberOfBytes,\r
213 IN UINTN Timeout\r
214 )\r
215{\r
b422b62c 216 SERIAL_DEBUG_PORT_HANDLE *SerialDebugPortHandle;\r
217 UINTN Index;\r
218 UINT64 Begin;\r
219 UINT64 TimeoutTicker;\r
220 UINT64 TimerRound;\r
221 \r
222 //\r
223 // If Handle is NULL, it means memory is ready for use.\r
224 // Use global variable to store handle value.\r
225 //\r
226 if (Handle == NULL) {\r
227 SerialDebugPortHandle = &mSerialDebugPortHandle;\r
228 } else {\r
229 SerialDebugPortHandle = (SERIAL_DEBUG_PORT_HANDLE *)Handle;\r
230 }\r
231\r
232 Begin = 0;\r
233 TimeoutTicker = 0; \r
234 TimerRound = 0;\r
235 if (Timeout != 0) {\r
236 Begin = GetPerformanceCounter ();\r
237 TimeoutTicker = DivU64x32 (\r
238 MultU64x64 (\r
239 SerialDebugPortHandle->TimerFrequency,\r
240 Timeout\r
241 ),\r
242 1000000u\r
243 );\r
244 TimerRound = DivU64x64Remainder (\r
245 TimeoutTicker,\r
246 DivU64x32 (SerialDebugPortHandle->TimerCycle, 2),\r
247 &TimeoutTicker\r
248 );\r
249 }\r
250 Index = 0;\r
251 while (Index < NumberOfBytes) {\r
252 if (SerialPortPoll () || Timeout == 0) {\r
253 SerialPortRead (Buffer + Index, 1);\r
254 Index ++; \r
255 continue;\r
256 }\r
257 if (TimerRound == 0) {\r
258 if (IsTimerTimeout (SerialDebugPortHandle, Begin, TimeoutTicker)) {\r
259 //\r
260 // If time out occurs.\r
261 //\r
18b144ea 262 return 0;\r
263 }\r
b422b62c 264 } else {\r
265 if (IsTimerTimeout (SerialDebugPortHandle, Begin, DivU64x32 (SerialDebugPortHandle->TimerCycle, 2))) {\r
266 TimerRound --;\r
267 }\r
18b144ea 268 }\r
269 }\r
270\r
b422b62c 271 return Index;\r
18b144ea 272}\r
273\r
274/**\r
275 Write data from buffer to debug device.\r
276\r
277 Writes NumberOfBytes data bytes from Buffer to the debug device.\r
278 The number of bytes actually written to the debug device is returned.\r
279 If the return value is less than NumberOfBytes, then the write operation failed.\r
280 If NumberOfBytes is zero, then return 0.\r
281\r
282 @param Handle Debug port handle.\r
283 @param Buffer Pointer to the data buffer to be written.\r
284 @param NumberOfBytes Number of bytes to written to the debug device.\r
285\r
286 @retval 0 NumberOfBytes is 0.\r
287 @retval >0 The number of bytes written to the debug device.\r
288 If this value is less than NumberOfBytes, then the read operation failed.\r
289\r
290**/\r
291UINTN\r
292EFIAPI\r
293DebugPortWriteBuffer (\r
294 IN DEBUG_PORT_HANDLE Handle,\r
295 IN UINT8 *Buffer,\r
296 IN UINTN NumberOfBytes\r
297 )\r
298{\r
299 return SerialPortWrite (Buffer, NumberOfBytes);\r
300}\r
301\r
302/**\r
303 Polls a debug device to see if there is any data waiting to be read.\r
304\r
305 Polls a debug device to see if there is any data waiting to be read.\r
306 If there is data waiting to be read from the debug device, then TRUE is returned.\r
307 If there is no data waiting to be read from the debug device, then FALSE is returned.\r
308\r
309 @param Handle Debug port handle.\r
310\r
311 @retval TRUE Data is waiting to be read from the debug device.\r
312 @retval FALSE There is no data waiting to be read from the serial device.\r
313\r
314**/\r
315BOOLEAN\r
316EFIAPI\r
317DebugPortPollBuffer (\r
318 IN DEBUG_PORT_HANDLE Handle\r
319 )\r
320{\r
321 return SerialPortPoll ();\r
322}\r
323\r