]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - EmbeddedPkg/SerialDxe/SerialIo.c
EmbeddedPkg: Introduced 'SerialPortExtLib.h'
[mirror_edk2.git] / EmbeddedPkg / SerialDxe / SerialIo.c
... / ...
CommitLineData
1/** @file\r
2 Serial IO Abstraction for GDB stub. This allows an EFI consoles that shows up on the system \r
3 running GDB. One console for error information and another console for user input/output.\r
4 \r
5 Basic packet format is $packet-data#checksum. So every command has 4 bytes of overhead: $,\r
6 #, 0, 0. The 0 and 0 are the ascii characters for the checksum. \r
7 \r
8\r
9 Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>\r
10 \r
11 This program and the accompanying materials\r
12 are licensed and made available under the terms and conditions of the BSD License\r
13 which accompanies this distribution. The full text of the license may be found at\r
14 http://opensource.org/licenses/bsd-license.php\r
15\r
16 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
17 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
18\r
19**/\r
20\r
21#include <PiDxe.h>\r
22#include <Library/UefiLib.h>\r
23#include <Library/UefiBootServicesTableLib.h>\r
24#include <Library/DebugLib.h>\r
25#include <Library/SerialPortLib.h>\r
26#include <Library/SerialPortExtLib.h>\r
27#include <Library/PcdLib.h>\r
28\r
29#include <Protocol/SerialIo.h>\r
30\r
31typedef struct {\r
32 VENDOR_DEVICE_PATH Guid;\r
33 UART_DEVICE_PATH Uart;\r
34 EFI_DEVICE_PATH_PROTOCOL End;\r
35} SIMPLE_TEXT_OUT_DEVICE_PATH;\r
36\r
37SIMPLE_TEXT_OUT_DEVICE_PATH mDevicePath = {\r
38 {\r
39 { HARDWARE_DEVICE_PATH, HW_VENDOR_DP, sizeof (VENDOR_DEVICE_PATH), 0},\r
40 EFI_CALLER_ID_GUID // Use the drivers GUID\r
41 },\r
42 {\r
43 { MESSAGING_DEVICE_PATH, MSG_UART_DP, sizeof (UART_DEVICE_PATH), 0},\r
44 0, // Reserved\r
45 FixedPcdGet64 (PcdUartDefaultBaudRate), // BaudRate\r
46 FixedPcdGet8 (PcdUartDefaultDataBits), // DataBits\r
47 FixedPcdGet8 (PcdUartDefaultParity), // Parity (N)\r
48 FixedPcdGet8 (PcdUartDefaultStopBits) // StopBits\r
49 },\r
50 { END_DEVICE_PATH_TYPE, END_ENTIRE_DEVICE_PATH_SUBTYPE, sizeof (EFI_DEVICE_PATH_PROTOCOL), 0}\r
51};\r
52\r
53EFI_HANDLE gHandle = NULL;\r
54\r
55/**\r
56 Reset the serial device.\r
57\r
58 @param This Protocol instance pointer.\r
59 \r
60 @retval EFI_SUCCESS The device was reset.\r
61 @retval EFI_DEVICE_ERROR The serial device could not be reset.\r
62\r
63**/\r
64EFI_STATUS\r
65EFIAPI\r
66SerialReset (\r
67 IN EFI_SERIAL_IO_PROTOCOL *This\r
68 )\r
69{\r
70 EFI_STATUS Status;\r
71 EFI_TPL Tpl;\r
72\r
73 Status = SerialPortInitialize ();\r
74 if (EFI_ERROR(Status)) {\r
75 return Status;\r
76 }\r
77\r
78 //\r
79 // Set the Serial I/O mode and update the device path\r
80 //\r
81\r
82 Tpl = gBS->RaiseTPL (TPL_NOTIFY);\r
83\r
84 //\r
85 // Set the Serial I/O mode\r
86 //\r
87 This->Mode->ReceiveFifoDepth = 0;\r
88 This->Mode->Timeout = 1000000;\r
89 This->Mode->BaudRate = PcdGet64 (PcdUartDefaultBaudRate);\r
90 This->Mode->DataBits = (UINT32)PcdGet8 (PcdUartDefaultDataBits);\r
91 This->Mode->Parity = (UINT32)PcdGet8 (PcdUartDefaultParity);\r
92 This->Mode->StopBits = (UINT32)PcdGet8 (PcdUartDefaultStopBits);\r
93\r
94 //\r
95 // Check if the device path has actually changed\r
96 //\r
97 if (mDevicePath.Uart.BaudRate == This->Mode->BaudRate &&\r
98 mDevicePath.Uart.DataBits == (UINT8)This->Mode->DataBits &&\r
99 mDevicePath.Uart.Parity == (UINT8)This->Mode->Parity &&\r
100 mDevicePath.Uart.StopBits == (UINT8)This->Mode->StopBits\r
101 ) {\r
102 gBS->RestoreTPL (Tpl);\r
103 return EFI_SUCCESS;\r
104 }\r
105\r
106 //\r
107 // Update the device path\r
108 //\r
109 mDevicePath.Uart.BaudRate = This->Mode->BaudRate;\r
110 mDevicePath.Uart.DataBits = (UINT8)This->Mode->DataBits;\r
111 mDevicePath.Uart.Parity = (UINT8)This->Mode->Parity;\r
112 mDevicePath.Uart.StopBits = (UINT8)This->Mode->StopBits;\r
113\r
114 Status = gBS->ReinstallProtocolInterface (\r
115 gHandle,\r
116 &gEfiDevicePathProtocolGuid,\r
117 &mDevicePath,\r
118 &mDevicePath\r
119 );\r
120\r
121 gBS->RestoreTPL (Tpl);\r
122\r
123 return Status;\r
124}\r
125\r
126\r
127/**\r
128 Sets the baud rate, receive FIFO depth, transmit/receive time out, parity,\r
129 data buts, and stop bits on a serial device.\r
130\r
131 @param This Protocol instance pointer.\r
132 @param BaudRate The requested baud rate. A BaudRate value of 0 will use the the\r
133 device's default interface speed.\r
134 @param ReceiveFifoDepth The requested depth of the FIFO on the receive side of the\r
135 serial interface. A ReceiveFifoDepth value of 0 will use\r
136 the device's default FIFO depth.\r
137 @param Timeout The requested time out for a single character in microseconds.\r
138 This timeout applies to both the transmit and receive side of the\r
139 interface. A Timeout value of 0 will use the device's default time\r
140 out value.\r
141 @param Parity The type of parity to use on this serial device. A Parity value of\r
142 DefaultParity will use the device's default parity value.\r
143 @param DataBits The number of data bits to use on the serial device. A DataBits\r
144 value of 0 will use the device's default data bit setting.\r
145 @param StopBits The number of stop bits to use on this serial device. A StopBits\r
146 value of DefaultStopBits will use the device's default number of\r
147 stop bits.\r
148\r
149 @retval EFI_SUCCESS The device was reset.\r
150 @retval EFI_DEVICE_ERROR The serial device could not be reset.\r
151\r
152**/\r
153EFI_STATUS\r
154EFIAPI\r
155SerialSetAttributes (\r
156 IN EFI_SERIAL_IO_PROTOCOL *This,\r
157 IN UINT64 BaudRate,\r
158 IN UINT32 ReceiveFifoDepth,\r
159 IN UINT32 Timeout,\r
160 IN EFI_PARITY_TYPE Parity,\r
161 IN UINT8 DataBits,\r
162 IN EFI_STOP_BITS_TYPE StopBits\r
163 )\r
164{\r
165 EFI_STATUS Status;\r
166 EFI_TPL Tpl;\r
167\r
168 Status = SerialPortSetAttributes (BaudRate, ReceiveFifoDepth, Timeout, Parity, DataBits, StopBits);\r
169 if (EFI_ERROR(Status)) {\r
170 return Status;\r
171 }\r
172\r
173 //\r
174 // Set the Serial I/O mode and update the device path\r
175 //\r
176\r
177 Tpl = gBS->RaiseTPL (TPL_NOTIFY);\r
178\r
179 //\r
180 // Set the Serial I/O mode\r
181 //\r
182 This->Mode->BaudRate = BaudRate;\r
183 This->Mode->ReceiveFifoDepth = ReceiveFifoDepth;\r
184 This->Mode->Timeout = Timeout;\r
185 This->Mode->Parity = (UINT32)Parity;\r
186 This->Mode->DataBits = (UINT32)DataBits;\r
187 This->Mode->StopBits = (UINT32)StopBits;\r
188\r
189 //\r
190 // Check if the device path has actually changed\r
191 //\r
192 if (mDevicePath.Uart.BaudRate == BaudRate &&\r
193 mDevicePath.Uart.Parity == (UINT8)Parity &&\r
194 mDevicePath.Uart.DataBits == DataBits &&\r
195 mDevicePath.Uart.StopBits == (UINT8)StopBits\r
196 ) {\r
197 gBS->RestoreTPL (Tpl);\r
198 return EFI_SUCCESS;\r
199 }\r
200\r
201 //\r
202 // Update the device path\r
203 //\r
204 mDevicePath.Uart.BaudRate = BaudRate;\r
205 mDevicePath.Uart.DataBits = DataBits;\r
206 mDevicePath.Uart.Parity = (UINT8) Parity;\r
207 mDevicePath.Uart.StopBits = (UINT8) StopBits;\r
208\r
209 Status = gBS->ReinstallProtocolInterface (\r
210 gHandle,\r
211 &gEfiDevicePathProtocolGuid,\r
212 &mDevicePath,\r
213 &mDevicePath\r
214 );\r
215\r
216 gBS->RestoreTPL (Tpl);\r
217\r
218 return Status;\r
219}\r
220\r
221\r
222/**\r
223 Set the control bits on a serial device\r
224\r
225 @param This Protocol instance pointer.\r
226 @param Control Set the bits of Control that are settable.\r
227\r
228 @retval EFI_SUCCESS The new control bits were set on the serial device.\r
229 @retval EFI_UNSUPPORTED The serial device does not support this operation.\r
230 @retval EFI_DEVICE_ERROR The serial device is not functioning correctly.\r
231\r
232**/\r
233EFI_STATUS\r
234EFIAPI\r
235SerialSetControl (\r
236 IN EFI_SERIAL_IO_PROTOCOL *This,\r
237 IN UINT32 Control\r
238 )\r
239{\r
240 return SerialPortSetControl(Control);\r
241}\r
242\r
243\r
244/**\r
245 Retrieves the status of the control bits on a serial device\r
246\r
247 @param This Protocol instance pointer.\r
248 @param Control A pointer to return the current Control signals from the serial device.\r
249 \r
250 @retval EFI_SUCCESS The control bits were read from the serial device.\r
251 @retval EFI_DEVICE_ERROR The serial device is not functioning correctly.\r
252\r
253**/\r
254EFI_STATUS\r
255EFIAPI\r
256SerialGetControl (\r
257 IN EFI_SERIAL_IO_PROTOCOL *This,\r
258 OUT UINT32 *Control\r
259 )\r
260{\r
261 return SerialPortGetControl(Control);\r
262}\r
263\r
264\r
265/**\r
266 Writes data to a serial device.\r
267\r
268 @param This Protocol instance pointer.\r
269 @param BufferSize On input, the size of the Buffer. On output, the amount of\r
270 data actually written.\r
271 @param Buffer The buffer of data to write\r
272\r
273 @retval EFI_SUCCESS The data was written.\r
274 @retval EFI_DEVICE_ERROR The device reported an error.\r
275 @retval EFI_TIMEOUT The data write was stopped due to a timeout.\r
276\r
277**/\r
278EFI_STATUS\r
279EFIAPI\r
280SerialWrite (\r
281 IN EFI_SERIAL_IO_PROTOCOL *This,\r
282 IN OUT UINTN *BufferSize,\r
283 IN VOID *Buffer\r
284 )\r
285{\r
286 UINTN Count;\r
287 \r
288 Count = SerialPortWrite (Buffer, *BufferSize);\r
289\r
290 if (Count != *BufferSize) {\r
291 *BufferSize = Count;\r
292 return EFI_TIMEOUT;\r
293 }\r
294\r
295 return EFI_SUCCESS;\r
296}\r
297\r
298/**\r
299 Reads data from a serial device.\r
300\r
301 @param This Protocol instance pointer.\r
302 @param BufferSize On input, the size of the Buffer. On output, the amount of\r
303 data returned in Buffer.\r
304 @param Buffer The buffer to return the data into.\r
305\r
306 @retval EFI_SUCCESS The data was read.\r
307 @retval EFI_DEVICE_ERROR The device reported an error.\r
308 @retval EFI_TIMEOUT The data write was stopped due to a timeout.\r
309\r
310**/\r
311\r
312EFI_STATUS\r
313EFIAPI\r
314SerialRead (\r
315 IN EFI_SERIAL_IO_PROTOCOL *This,\r
316 IN OUT UINTN *BufferSize,\r
317 OUT VOID *Buffer\r
318 )\r
319{\r
320 UINTN Count = 0;\r
321\r
322 if (SerialPortPoll()) {\r
323 Count = SerialPortRead (Buffer, *BufferSize);\r
324 }\r
325\r
326 if (Count != *BufferSize) {\r
327 *BufferSize = Count;\r
328 return EFI_TIMEOUT;\r
329 }\r
330\r
331 return EFI_SUCCESS;\r
332}\r
333\r
334// \r
335// Template used to initialize the GDB Serial IO protocols\r
336//\r
337EFI_SERIAL_IO_MODE gSerialIoMode = {\r
338 0, // ControlMask\r
339 0, // Timeout\r
340 FixedPcdGet64 (PcdUartDefaultBaudRate), // BaudRate\r
341 1, // ReceiveFifoDepth\r
342 FixedPcdGet8 (PcdUartDefaultDataBits), // DataBits\r
343 FixedPcdGet8 (PcdUartDefaultParity), // Parity\r
344 FixedPcdGet8 (PcdUartDefaultStopBits) // StopBits\r
345};\r
346\r
347\r
348EFI_SERIAL_IO_PROTOCOL gSerialIoTemplate = {\r
349 SERIAL_IO_INTERFACE_REVISION,\r
350 SerialReset,\r
351 SerialSetAttributes,\r
352 SerialSetControl,\r
353 SerialGetControl,\r
354 SerialWrite,\r
355 SerialRead,\r
356 &gSerialIoMode\r
357};\r
358\r
359/**\r
360 Initialize the state information for the Serial Io Protocol\r
361\r
362 @param ImageHandle of the loaded driver\r
363 @param SystemTable Pointer to the System Table\r
364\r
365 @retval EFI_SUCCESS Protocol registered\r
366 @retval EFI_OUT_OF_RESOURCES Cannot allocate protocol data structure\r
367 @retval EFI_DEVICE_ERROR Hardware problems\r
368\r
369**/\r
370EFI_STATUS\r
371EFIAPI\r
372SerialDxeInitialize (\r
373 IN EFI_HANDLE ImageHandle,\r
374 IN EFI_SYSTEM_TABLE *SystemTable\r
375 )\r
376{\r
377 EFI_STATUS Status;\r
378\r
379 // Make a new handle with Serial IO protocol and its device path on it.\r
380 Status = gBS->InstallMultipleProtocolInterfaces (\r
381 &gHandle, \r
382 &gEfiSerialIoProtocolGuid, &gSerialIoTemplate,\r
383 &gEfiDevicePathProtocolGuid, &mDevicePath, \r
384 NULL\r
385 );\r
386 ASSERT_EFI_ERROR (Status);\r
387 \r
388 return Status;\r
389}\r
390\r