]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/SerialDxe/SerialIo.c
MdeModulePkg: Fix IPv4 double free
[mirror_edk2.git] / MdeModulePkg / Universal / SerialDxe / SerialIo.c
CommitLineData
bf6b810f
SZ
1/** @file\r
2 Serial driver that layers on top of a Serial Port Library instance.\r
3\r
4 Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>\r
5 Copyright (c) 2013-2014, ARM Ltd. All rights reserved.<BR>\r
6 Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>\r
7\r
8 This program and the accompanying materials\r
9 are licensed and made available under the terms and conditions of the BSD License\r
10 which accompanies this distribution. The full text of the license may be found at\r
11 http://opensource.org/licenses/bsd-license.php\r
12\r
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
15\r
16**/\r
17\r
18#include <Library/UefiBootServicesTableLib.h>\r
19#include <Library/SerialPortLib.h>\r
20#include <Library/DebugLib.h>\r
21#include <Library/PcdLib.h>\r
22\r
23#include <Protocol/SerialIo.h>\r
24#include <Protocol/DevicePath.h>\r
25\r
26typedef struct {\r
27 VENDOR_DEVICE_PATH Guid;\r
28 UART_DEVICE_PATH Uart;\r
29 EFI_DEVICE_PATH_PROTOCOL End;\r
30} SERIAL_DEVICE_PATH;\r
31\r
32/**\r
33 Reset the serial device.\r
34\r
35 @param This Protocol instance pointer.\r
36\r
37 @retval EFI_SUCCESS The device was reset.\r
38 @retval EFI_DEVICE_ERROR The serial device could not be reset.\r
39\r
40**/\r
41EFI_STATUS\r
42EFIAPI\r
43SerialReset (\r
44 IN EFI_SERIAL_IO_PROTOCOL *This\r
45 );\r
46\r
47/**\r
48 Sets the baud rate, receive FIFO depth, transmit/receive time out, parity,\r
49 data bits, and stop bits on a serial device.\r
50\r
51 @param This Protocol instance pointer.\r
52 @param BaudRate The requested baud rate. A BaudRate value of 0 will use the the\r
53 device's default interface speed.\r
54 @param ReceiveFifoDepth The requested depth of the FIFO on the receive side of the\r
55 serial interface. A ReceiveFifoDepth value of 0 will use\r
56 the device's default FIFO depth.\r
57 @param Timeout The requested time out for a single character in microseconds.\r
58 This timeout applies to both the transmit and receive side of the\r
59 interface. A Timeout value of 0 will use the device's default time\r
60 out value.\r
61 @param Parity The type of parity to use on this serial device. A Parity value of\r
62 DefaultParity will use the device's default parity value.\r
63 @param DataBits The number of data bits to use on the serial device. A DataBits\r
64 value of 0 will use the device's default data bit setting.\r
65 @param StopBits The number of stop bits to use on this serial device. A StopBits\r
66 value of DefaultStopBits will use the device's default number of\r
67 stop bits.\r
68\r
69 @retval EFI_SUCCESS The device was reset.\r
70 @retval EFI_DEVICE_ERROR The serial device could not be reset.\r
71\r
72**/\r
73EFI_STATUS\r
74EFIAPI\r
75SerialSetAttributes (\r
76 IN EFI_SERIAL_IO_PROTOCOL *This,\r
77 IN UINT64 BaudRate,\r
78 IN UINT32 ReceiveFifoDepth,\r
79 IN UINT32 Timeout,\r
80 IN EFI_PARITY_TYPE Parity,\r
81 IN UINT8 DataBits,\r
82 IN EFI_STOP_BITS_TYPE StopBits\r
83 );\r
84\r
85/**\r
86 Set the control bits on a serial device\r
87\r
88 @param This Protocol instance pointer.\r
89 @param Control Set the bits of Control that are settable.\r
90\r
91 @retval EFI_SUCCESS The new control bits were set on the serial device.\r
92 @retval EFI_UNSUPPORTED The serial device does not support this operation.\r
93 @retval EFI_DEVICE_ERROR The serial device is not functioning correctly.\r
94\r
95**/\r
96EFI_STATUS\r
97EFIAPI\r
98SerialSetControl (\r
99 IN EFI_SERIAL_IO_PROTOCOL *This,\r
100 IN UINT32 Control\r
101 );\r
102\r
103/**\r
104 Retrieves the status of the control bits on a serial device\r
105\r
106 @param This Protocol instance pointer.\r
107 @param Control A pointer to return the current Control signals from the serial device.\r
108\r
109 @retval EFI_SUCCESS The control bits were read from the serial device.\r
110 @retval EFI_DEVICE_ERROR The serial device is not functioning correctly.\r
111\r
112**/\r
113EFI_STATUS\r
114EFIAPI\r
115SerialGetControl (\r
116 IN EFI_SERIAL_IO_PROTOCOL *This,\r
117 OUT UINT32 *Control\r
118 );\r
119\r
120/**\r
121 Writes data to a serial device.\r
122\r
123 @param This Protocol instance pointer.\r
124 @param BufferSize On input, the size of the Buffer. On output, the amount of\r
125 data actually written.\r
126 @param Buffer The buffer of data to write\r
127\r
128 @retval EFI_SUCCESS The data was written.\r
129 @retval EFI_DEVICE_ERROR The device reported an error.\r
130 @retval EFI_TIMEOUT The data write was stopped due to a timeout.\r
131\r
132**/\r
133EFI_STATUS\r
134EFIAPI\r
135SerialWrite (\r
136 IN EFI_SERIAL_IO_PROTOCOL *This,\r
137 IN OUT UINTN *BufferSize,\r
138 IN VOID *Buffer\r
139 );\r
140\r
141/**\r
142 Reads data from a serial device.\r
143\r
144 @param This Protocol instance pointer.\r
145 @param BufferSize On input, the size of the Buffer. On output, the amount of\r
146 data returned in Buffer.\r
147 @param Buffer The buffer to return the data into.\r
148\r
149 @retval EFI_SUCCESS The data was read.\r
150 @retval EFI_DEVICE_ERROR The device reported an error.\r
151 @retval EFI_TIMEOUT The data write was stopped due to a timeout.\r
152\r
153**/\r
154EFI_STATUS\r
155EFIAPI\r
156SerialRead (\r
157 IN EFI_SERIAL_IO_PROTOCOL *This,\r
158 IN OUT UINTN *BufferSize,\r
159 OUT VOID *Buffer\r
160 );\r
161\r
162EFI_HANDLE mSerialHandle = NULL;\r
163\r
164SERIAL_DEVICE_PATH mSerialDevicePath = {\r
165 {\r
166 { HARDWARE_DEVICE_PATH, HW_VENDOR_DP, { sizeof (VENDOR_DEVICE_PATH), 0} },\r
167 EFI_CALLER_ID_GUID // Use the driver's GUID\r
168 },\r
169 {\r
170 { MESSAGING_DEVICE_PATH, MSG_UART_DP, { sizeof (UART_DEVICE_PATH), 0} },\r
171 0, // Reserved\r
172 0, // BaudRate\r
173 0, // DataBits\r
174 0, // Parity\r
175 0 // StopBits\r
176 },\r
177 { END_DEVICE_PATH_TYPE, END_ENTIRE_DEVICE_PATH_SUBTYPE, { sizeof (EFI_DEVICE_PATH_PROTOCOL), 0 } }\r
178};\r
179\r
180//\r
181// Template used to initialize the Serial IO protocols.\r
182//\r
183EFI_SERIAL_IO_MODE mSerialIoMode = {\r
c0beed6d
LE
184 //\r
185 // value field set in SerialDxeInitialize()?\r
186 //--------- ------------------- -----------------------------\r
187 0, // ControlMask\r
ea012619 188 1000 * 1000, // Timeout\r
c0beed6d
LE
189 0, // BaudRate yes\r
190 1, // ReceiveFifoDepth\r
191 0, // DataBits yes\r
192 0, // Parity yes\r
193 0 // StopBits yes\r
bf6b810f
SZ
194};\r
195\r
196EFI_SERIAL_IO_PROTOCOL mSerialIoTemplate = {\r
197 SERIAL_IO_INTERFACE_REVISION,\r
198 SerialReset,\r
199 SerialSetAttributes,\r
200 SerialSetControl,\r
201 SerialGetControl,\r
202 SerialWrite,\r
203 SerialRead,\r
204 &mSerialIoMode\r
205};\r
206\r
207/**\r
208 Reset the serial device.\r
209\r
210 @param This Protocol instance pointer.\r
211\r
212 @retval EFI_SUCCESS The device was reset.\r
213 @retval EFI_DEVICE_ERROR The serial device could not be reset.\r
214\r
215**/\r
216EFI_STATUS\r
217EFIAPI\r
218SerialReset (\r
219 IN EFI_SERIAL_IO_PROTOCOL *This\r
220 )\r
221{\r
222 EFI_STATUS Status;\r
223 EFI_TPL Tpl;\r
224\r
225 Status = SerialPortInitialize ();\r
226 if (EFI_ERROR (Status)) {\r
227 return Status;\r
228 }\r
229\r
230 //\r
231 // Set the Serial I/O mode and update the device path\r
232 //\r
233\r
234 Tpl = gBS->RaiseTPL (TPL_NOTIFY);\r
235\r
236 //\r
237 // Set the Serial I/O mode\r
238 //\r
239 This->Mode->ReceiveFifoDepth = 1;\r
ea012619 240 This->Mode->Timeout = 1000 * 1000;\r
bf6b810f
SZ
241 This->Mode->BaudRate = PcdGet64 (PcdUartDefaultBaudRate);\r
242 This->Mode->DataBits = (UINT32) PcdGet8 (PcdUartDefaultDataBits);\r
243 This->Mode->Parity = (UINT32) PcdGet8 (PcdUartDefaultParity);\r
244 This->Mode->StopBits = (UINT32) PcdGet8 (PcdUartDefaultStopBits);\r
245\r
246 //\r
247 // Check if the device path has actually changed\r
248 //\r
249 if (mSerialDevicePath.Uart.BaudRate == This->Mode->BaudRate &&\r
250 mSerialDevicePath.Uart.DataBits == (UINT8) This->Mode->DataBits &&\r
251 mSerialDevicePath.Uart.Parity == (UINT8) This->Mode->Parity &&\r
252 mSerialDevicePath.Uart.StopBits == (UINT8) This->Mode->StopBits\r
253 ) {\r
254 gBS->RestoreTPL (Tpl);\r
255 return EFI_SUCCESS;\r
256 }\r
257\r
258 //\r
259 // Update the device path\r
260 //\r
261 mSerialDevicePath.Uart.BaudRate = This->Mode->BaudRate;\r
262 mSerialDevicePath.Uart.DataBits = (UINT8) This->Mode->DataBits;\r
263 mSerialDevicePath.Uart.Parity = (UINT8) This->Mode->Parity;\r
264 mSerialDevicePath.Uart.StopBits = (UINT8) This->Mode->StopBits;\r
265\r
266 Status = gBS->ReinstallProtocolInterface (\r
267 mSerialHandle,\r
268 &gEfiDevicePathProtocolGuid,\r
269 &mSerialDevicePath,\r
270 &mSerialDevicePath\r
271 );\r
272\r
273 gBS->RestoreTPL (Tpl);\r
274\r
275 return Status;\r
276}\r
277\r
278/**\r
279 Sets the baud rate, receive FIFO depth, transmit/receive time out, parity,\r
280 data bits, and stop bits on a serial device.\r
281\r
282 @param This Protocol instance pointer.\r
283 @param BaudRate The requested baud rate. A BaudRate value of 0 will use the the\r
284 device's default interface speed.\r
285 @param ReceiveFifoDepth The requested depth of the FIFO on the receive side of the\r
286 serial interface. A ReceiveFifoDepth value of 0 will use\r
287 the device's default FIFO depth.\r
288 @param Timeout The requested time out for a single character in microseconds.\r
289 This timeout applies to both the transmit and receive side of the\r
290 interface. A Timeout value of 0 will use the device's default time\r
291 out value.\r
292 @param Parity The type of parity to use on this serial device. A Parity value of\r
293 DefaultParity will use the device's default parity value.\r
294 @param DataBits The number of data bits to use on the serial device. A DataBits\r
295 value of 0 will use the device's default data bit setting.\r
296 @param StopBits The number of stop bits to use on this serial device. A StopBits\r
297 value of DefaultStopBits will use the device's default number of\r
298 stop bits.\r
299\r
300 @retval EFI_SUCCESS The device was reset.\r
301 @retval EFI_DEVICE_ERROR The serial device could not be reset.\r
302\r
303**/\r
304EFI_STATUS\r
305EFIAPI\r
306SerialSetAttributes (\r
307 IN EFI_SERIAL_IO_PROTOCOL *This,\r
308 IN UINT64 BaudRate,\r
309 IN UINT32 ReceiveFifoDepth,\r
310 IN UINT32 Timeout,\r
311 IN EFI_PARITY_TYPE Parity,\r
312 IN UINT8 DataBits,\r
313 IN EFI_STOP_BITS_TYPE StopBits\r
314 )\r
315{\r
316 EFI_STATUS Status;\r
317 EFI_TPL Tpl;\r
318\r
319 Status = SerialPortSetAttributes (&BaudRate, &ReceiveFifoDepth, &Timeout, &Parity, &DataBits, &StopBits);\r
320 if (EFI_ERROR (Status)) {\r
321 return Status;\r
322 }\r
323\r
324 //\r
325 // Set the Serial I/O mode and update the device path\r
326 //\r
327\r
328 Tpl = gBS->RaiseTPL (TPL_NOTIFY);\r
329\r
330 //\r
331 // Set the Serial I/O mode\r
332 //\r
333 This->Mode->ReceiveFifoDepth = ReceiveFifoDepth;\r
334 This->Mode->Timeout = Timeout;\r
335 This->Mode->BaudRate = BaudRate;\r
336 This->Mode->DataBits = (UINT32) DataBits;\r
337 This->Mode->Parity = (UINT32) Parity;\r
338 This->Mode->StopBits = (UINT32) StopBits;\r
339\r
340 //\r
341 // Check if the device path has actually changed\r
342 //\r
343 if (mSerialDevicePath.Uart.BaudRate == BaudRate &&\r
344 mSerialDevicePath.Uart.DataBits == DataBits &&\r
345 mSerialDevicePath.Uart.Parity == (UINT8) Parity &&\r
346 mSerialDevicePath.Uart.StopBits == (UINT8) StopBits\r
347 ) {\r
348 gBS->RestoreTPL (Tpl);\r
349 return EFI_SUCCESS;\r
350 }\r
351\r
352 //\r
353 // Update the device path\r
354 //\r
355 mSerialDevicePath.Uart.BaudRate = BaudRate;\r
356 mSerialDevicePath.Uart.DataBits = DataBits;\r
357 mSerialDevicePath.Uart.Parity = (UINT8) Parity;\r
358 mSerialDevicePath.Uart.StopBits = (UINT8) StopBits;\r
359\r
360 Status = gBS->ReinstallProtocolInterface (\r
361 mSerialHandle,\r
362 &gEfiDevicePathProtocolGuid,\r
363 &mSerialDevicePath,\r
364 &mSerialDevicePath\r
365 );\r
366\r
367 gBS->RestoreTPL (Tpl);\r
368\r
369 return Status;\r
370}\r
371\r
372/**\r
373 Set the control bits on a serial device\r
374\r
375 @param This Protocol instance pointer.\r
376 @param Control Set the bits of Control that are settable.\r
377\r
378 @retval EFI_SUCCESS The new control bits were set on the serial device.\r
379 @retval EFI_UNSUPPORTED The serial device does not support this operation.\r
380 @retval EFI_DEVICE_ERROR The serial device is not functioning correctly.\r
381\r
382**/\r
383EFI_STATUS\r
384EFIAPI\r
385SerialSetControl (\r
386 IN EFI_SERIAL_IO_PROTOCOL *This,\r
387 IN UINT32 Control\r
388 )\r
389{\r
390 return SerialPortSetControl (Control);\r
391}\r
392\r
393/**\r
394 Retrieves the status of the control bits on a serial device\r
395\r
396 @param This Protocol instance pointer.\r
397 @param Control A pointer to return the current Control signals from the serial device.\r
398\r
399 @retval EFI_SUCCESS The control bits were read from the serial device.\r
400 @retval EFI_DEVICE_ERROR The serial device is not functioning correctly.\r
401\r
402**/\r
403EFI_STATUS\r
404EFIAPI\r
405SerialGetControl (\r
406 IN EFI_SERIAL_IO_PROTOCOL *This,\r
407 OUT UINT32 *Control\r
408 )\r
409{\r
410 return SerialPortGetControl (Control);\r
411}\r
412\r
413/**\r
414 Writes data to a serial device.\r
415\r
416 @param This Protocol instance pointer.\r
417 @param BufferSize On input, the size of the Buffer. On output, the amount of\r
418 data actually written.\r
419 @param Buffer The buffer of data to write\r
420\r
421 @retval EFI_SUCCESS The data was written.\r
422 @retval EFI_DEVICE_ERROR The device reported an error.\r
423 @retval EFI_TIMEOUT The data write was stopped due to a timeout.\r
424\r
425**/\r
426EFI_STATUS\r
427EFIAPI\r
428SerialWrite (\r
429 IN EFI_SERIAL_IO_PROTOCOL *This,\r
430 IN OUT UINTN *BufferSize,\r
431 IN VOID *Buffer\r
432 )\r
433{\r
434 UINTN Count;\r
435\r
436 Count = SerialPortWrite (Buffer, *BufferSize);\r
437\r
438 if (Count != *BufferSize) {\r
439 *BufferSize = Count;\r
440 return EFI_TIMEOUT;\r
441 }\r
442\r
443 return EFI_SUCCESS;\r
444}\r
445\r
446/**\r
447 Reads data from a serial device.\r
448\r
449 @param This Protocol instance pointer.\r
450 @param BufferSize On input, the size of the Buffer. On output, the amount of\r
451 data returned in Buffer.\r
452 @param Buffer The buffer to return the data into.\r
453\r
454 @retval EFI_SUCCESS The data was read.\r
455 @retval EFI_DEVICE_ERROR The device reported an error.\r
456 @retval EFI_TIMEOUT The data write was stopped due to a timeout.\r
457\r
458**/\r
459EFI_STATUS\r
460EFIAPI\r
461SerialRead (\r
462 IN EFI_SERIAL_IO_PROTOCOL *This,\r
463 IN OUT UINTN *BufferSize,\r
464 OUT VOID *Buffer\r
465 )\r
466{\r
467 UINTN Count;\r
468\r
469 Count = 0;\r
470\r
471 if (SerialPortPoll ()) {\r
472 Count = SerialPortRead (Buffer, *BufferSize);\r
473 }\r
474\r
475 if (Count != *BufferSize) {\r
476 *BufferSize = Count;\r
477 return EFI_TIMEOUT;\r
478 }\r
479\r
480 return EFI_SUCCESS;\r
481}\r
482\r
483/**\r
484 Initialization for the Serial Io Protocol.\r
485\r
486 @param[in] ImageHandle The firmware allocated handle for the EFI image.\r
487 @param[in] SystemTable A pointer to the EFI System Table.\r
488\r
489 @retval EFI_SUCCESS The entry point is executed successfully.\r
490 @retval other Some error occurs when executing this entry point.\r
491\r
492**/\r
493EFI_STATUS\r
494EFIAPI\r
495SerialDxeInitialize (\r
496 IN EFI_HANDLE ImageHandle,\r
497 IN EFI_SYSTEM_TABLE *SystemTable\r
498 )\r
499{\r
500 EFI_STATUS Status;\r
501\r
502 Status = SerialPortInitialize ();\r
503 if (EFI_ERROR (Status)) {\r
504 return Status;\r
505 }\r
506\r
507 mSerialIoMode.BaudRate = PcdGet64 (PcdUartDefaultBaudRate);\r
508 mSerialIoMode.DataBits = (UINT32) PcdGet8 (PcdUartDefaultDataBits);\r
509 mSerialIoMode.Parity = (UINT32) PcdGet8 (PcdUartDefaultParity);\r
510 mSerialIoMode.StopBits = (UINT32) PcdGet8 (PcdUartDefaultStopBits);\r
511 mSerialDevicePath.Uart.BaudRate = PcdGet64 (PcdUartDefaultBaudRate);\r
512 mSerialDevicePath.Uart.DataBits = PcdGet8 (PcdUartDefaultDataBits);\r
513 mSerialDevicePath.Uart.Parity = PcdGet8 (PcdUartDefaultParity);\r
514 mSerialDevicePath.Uart.StopBits = PcdGet8 (PcdUartDefaultStopBits);\r
515\r
516 //\r
517 // Make a new handle with Serial IO protocol and its device path on it.\r
518 //\r
519 Status = gBS->InstallMultipleProtocolInterfaces (\r
520 &mSerialHandle,\r
521 &gEfiSerialIoProtocolGuid, &mSerialIoTemplate,\r
522 &gEfiDevicePathProtocolGuid, &mSerialDevicePath,\r
523 NULL\r
524 );\r
525 ASSERT_EFI_ERROR (Status);\r
526\r
527 return Status;\r
528}\r
529\r