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