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