]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - MdeModulePkg/Universal/DebugPortDxe/DebugPort.c
Append change log for the retirement of several macros in UefiBaseType.h
[mirror_edk2.git] / MdeModulePkg / Universal / DebugPortDxe / DebugPort.c
... / ...
CommitLineData
1/** @file\r
2 Top level C file for debugport driver. Contains initialization function.\r
3 This driver layers on top of SerialIo.\r
4 ALL CODE IN THE SERIALIO STACK MUST BE RE-ENTRANT AND CALLABLE FROM\r
5 INTERRUPT CONTEXT\r
6\r
7Copyright (c) 2006 - 2008, Intel Corporation. <BR>\r
8All rights reserved. This program and the accompanying materials\r
9are licensed and made available under the terms and conditions of the BSD License\r
10which accompanies this distribution. The full text of the license may be found at\r
11http://opensource.org/licenses/bsd-license.php\r
12\r
13THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
14WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
15\r
16**/\r
17\r
18#include "DebugPort.h"\r
19\r
20//\r
21// Globals\r
22//\r
23EFI_DRIVER_BINDING_PROTOCOL gDebugPortDriverBinding = {\r
24 DebugPortSupported,\r
25 DebugPortStart,\r
26 DebugPortStop,\r
27 DEBUGPORT_DRIVER_VERSION,\r
28 NULL,\r
29 NULL\r
30};\r
31\r
32DEBUGPORT_DEVICE mDebugPortDevice = {\r
33 DEBUGPORT_DEVICE_SIGNATURE,\r
34 (EFI_HANDLE) 0,\r
35 (EFI_HANDLE) 0,\r
36 (VOID *) NULL,\r
37 (EFI_DEVICE_PATH_PROTOCOL *) NULL,\r
38 {\r
39 DebugPortReset,\r
40 DebugPortRead, \r
41 DebugPortWrite, \r
42 DebugPortPoll \r
43 },\r
44 (EFI_HANDLE) 0,\r
45 (EFI_SERIAL_IO_PROTOCOL *) NULL,\r
46 DEBUGPORT_UART_DEFAULT_BAUDRATE,\r
47 DEBUGPORT_UART_DEFAULT_FIFO_DEPTH,\r
48 DEBUGPORT_UART_DEFAULT_TIMEOUT,\r
49 (EFI_PARITY_TYPE) DEBUGPORT_UART_DEFAULT_PARITY,\r
50 DEBUGPORT_UART_DEFAULT_DATA_BITS,\r
51 (EFI_STOP_BITS_TYPE) DEBUGPORT_UART_DEFAULT_STOP_BITS\r
52};\r
53\r
54/**\r
55 Local worker function to obtain device path information from DebugPort variable.\r
56\r
57 Records requested settings in DebugPort device structure.\r
58\r
59**/\r
60VOID\r
61GetDebugPortVariable (\r
62 VOID\r
63 )\r
64{\r
65 UINTN DataSize;\r
66 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
67 EFI_STATUS Status;\r
68\r
69 DataSize = 0;\r
70\r
71 Status = gRT->GetVariable (\r
72 (CHAR16 *) EFI_DEBUGPORT_VARIABLE_NAME,\r
73 &gEfiDebugPortVariableGuid,\r
74 NULL,\r
75 &DataSize,\r
76 mDebugPortDevice.DebugPortVariable\r
77 );\r
78\r
79 if (Status == EFI_BUFFER_TOO_SMALL) {\r
80 if (mDebugPortDevice.DebugPortVariable != NULL) {\r
81 FreePool (mDebugPortDevice.DebugPortVariable);\r
82 }\r
83\r
84 mDebugPortDevice.DebugPortVariable = AllocatePool (DataSize);\r
85 if (mDebugPortDevice.DebugPortVariable != NULL) {\r
86 gRT->GetVariable (\r
87 (CHAR16 *) EFI_DEBUGPORT_VARIABLE_NAME,\r
88 &gEfiDebugPortVariableGuid,\r
89 NULL,\r
90 &DataSize,\r
91 mDebugPortDevice.DebugPortVariable\r
92 );\r
93 DevicePath = (EFI_DEVICE_PATH_PROTOCOL *) mDebugPortDevice.DebugPortVariable;\r
94 while (!IsDevicePathEnd (DevicePath) && !IS_UART_DEVICEPATH (DevicePath)) {\r
95 DevicePath = NextDevicePathNode (DevicePath);\r
96 }\r
97\r
98 if (IsDevicePathEnd (DevicePath)) {\r
99 FreePool (mDebugPortDevice.DebugPortVariable);\r
100 mDebugPortDevice.DebugPortVariable = NULL;\r
101 } else {\r
102 CopyMem (\r
103 &mDebugPortDevice.BaudRate,\r
104 &((UART_DEVICE_PATH *) DevicePath)->BaudRate,\r
105 sizeof (((UART_DEVICE_PATH *) DevicePath)->BaudRate)\r
106 );\r
107 mDebugPortDevice.ReceiveFifoDepth = DEBUGPORT_UART_DEFAULT_FIFO_DEPTH;\r
108 mDebugPortDevice.Timeout = DEBUGPORT_UART_DEFAULT_TIMEOUT;\r
109 CopyMem (\r
110 &mDebugPortDevice.Parity,\r
111 &((UART_DEVICE_PATH *) DevicePath)->Parity,\r
112 sizeof (((UART_DEVICE_PATH *) DevicePath)->Parity)\r
113 );\r
114 CopyMem (\r
115 &mDebugPortDevice.DataBits,\r
116 &((UART_DEVICE_PATH *) DevicePath)->DataBits,\r
117 sizeof (((UART_DEVICE_PATH *) DevicePath)->DataBits)\r
118 );\r
119 CopyMem (\r
120 &mDebugPortDevice.StopBits,\r
121 &((UART_DEVICE_PATH *) DevicePath)->StopBits,\r
122 sizeof (((UART_DEVICE_PATH *) DevicePath)->StopBits)\r
123 );\r
124 }\r
125 }\r
126 }\r
127}\r
128\r
129/**\r
130 Debug Port Driver entry point. \r
131\r
132 Reads DebugPort variable to determine what device and settings to use as the\r
133 debug port. Binds exclusively to SerialIo. Reverts to defaults if no variable\r
134 is found.\r
135\r
136 @param[in] ImageHandle The firmware allocated handle for the EFI image. \r
137 @param[in] SystemTable A pointer to the EFI System Table.\r
138 \r
139 @retval EFI_SUCCESS The entry point is executed successfully.\r
140 @retval EFI_OUT_OF_RESOURCES Fails to allocate memory for device.\r
141 @retval other Some error occurs when executing this entry point.\r
142\r
143**/\r
144EFI_STATUS\r
145EFIAPI\r
146InitializeDebugPortDriver (\r
147 IN EFI_HANDLE ImageHandle,\r
148 IN EFI_SYSTEM_TABLE *SystemTable\r
149 )\r
150{\r
151 EFI_STATUS Status;\r
152\r
153 //\r
154 // Install driver model protocol(s).\r
155 //\r
156 Status = EfiLibInstallDriverBindingComponentName2 (\r
157 ImageHandle,\r
158 SystemTable,\r
159 &gDebugPortDriverBinding,\r
160 ImageHandle,\r
161 &gDebugPortComponentName,\r
162 &gDebugPortComponentName2\r
163 );\r
164 ASSERT_EFI_ERROR (Status);\r
165\r
166 return EFI_SUCCESS;\r
167}\r
168\r
169/**\r
170 Checks to see if there's not already a DebugPort interface somewhere. \r
171\r
172 If there's a DEBUGPORT variable, the device path must match exactly. If there's\r
173 no DEBUGPORT variable, then device path is not checked and does not matter.\r
174 Checks to see that there's a serial io interface on the controller handle\r
175 that can be bound BY_DRIVER | EXCLUSIVE.\r
176 If all these tests succeed, then we return EFI_SUCCESS, else, EFI_UNSUPPORTED\r
177 or other error returned by OpenProtocol.\r
178\r
179 @param This Protocol instance pointer.\r
180 @param ControllerHandle Handle of device to test.\r
181 @param RemainingDevicePath Optional parameter use to pick a specific child\r
182 device to start.\r
183\r
184 @retval EFI_SUCCESS This driver supports this device.\r
185 @retval EFI_UNSUPPORTED Debug Port device is not supported.\r
186 @retval EFI_OUT_OF_RESOURCES Fails to allocate memory for device.\r
187 @retval others Some error occurs.\r
188\r
189**/\r
190EFI_STATUS\r
191EFIAPI\r
192DebugPortSupported (\r
193 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
194 IN EFI_HANDLE ControllerHandle,\r
195 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath\r
196 )\r
197{\r
198 EFI_STATUS Status;\r
199 EFI_DEVICE_PATH_PROTOCOL *Dp1;\r
200 EFI_DEVICE_PATH_PROTOCOL *Dp2;\r
201 EFI_SERIAL_IO_PROTOCOL *SerialIo;\r
202 EFI_DEBUGPORT_PROTOCOL *DebugPortInterface;\r
203 EFI_HANDLE TempHandle;\r
204\r
205 //\r
206 // Check to see that there's not a debugport protocol already published\r
207 // Question: Why do we prevent debugport protocol published on more one device?\r
208 //\r
209 if (gBS->LocateProtocol (&gEfiDebugPortProtocolGuid, NULL, (VOID **) &DebugPortInterface) != EFI_NOT_FOUND) {\r
210 return EFI_UNSUPPORTED;\r
211 }\r
212 //\r
213 // Read DebugPort variable to determine debug port selection and parameters\r
214 //\r
215 GetDebugPortVariable ();\r
216\r
217 if (mDebugPortDevice.DebugPortVariable != NULL) {\r
218 //\r
219 // There's a DEBUGPORT variable, so do LocateDevicePath and check to see if\r
220 // the closest matching handle matches the controller handle, and if it does,\r
221 // check to see that the remaining device path has the DebugPort GUIDed messaging\r
222 // device path only. Otherwise, it's a mismatch and EFI_UNSUPPORTED is returned.\r
223 //\r
224 Dp1 = DuplicateDevicePath ((EFI_DEVICE_PATH_PROTOCOL *) mDebugPortDevice.DebugPortVariable);\r
225 if (Dp1 == NULL) {\r
226 return EFI_OUT_OF_RESOURCES;\r
227 }\r
228\r
229 Dp2 = Dp1;\r
230\r
231 Status = gBS->LocateDevicePath (\r
232 &gEfiSerialIoProtocolGuid,\r
233 &Dp2,\r
234 &TempHandle\r
235 );\r
236\r
237 if (Status == EFI_SUCCESS && TempHandle != ControllerHandle) {\r
238 Status = EFI_UNSUPPORTED;\r
239 }\r
240\r
241 if (Status == EFI_SUCCESS && \r
242 (Dp2->Type != MESSAGING_DEVICE_PATH ||\r
243 Dp2->SubType != MSG_VENDOR_DP || \r
244 *((UINT16 *) Dp2->Length) != sizeof (DEBUGPORT_DEVICE_PATH))) {\r
245\r
246 Status = EFI_UNSUPPORTED;\r
247 }\r
248\r
249 if (Status == EFI_SUCCESS && CompareMem (&gEfiDebugPortDevicePathGuid, Dp2 + 1, sizeof (EFI_GUID))) {\r
250 Status = EFI_UNSUPPORTED;\r
251 }\r
252\r
253 FreePool (Dp1);\r
254 if (EFI_ERROR (Status)) {\r
255 return Status;\r
256 }\r
257 }\r
258\r
259 Status = gBS->OpenProtocol (\r
260 ControllerHandle,\r
261 &gEfiSerialIoProtocolGuid,\r
262 (VOID **) &SerialIo,\r
263 This->DriverBindingHandle,\r
264 ControllerHandle,\r
265 EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE\r
266 );\r
267 if (EFI_ERROR (Status)) {\r
268 return Status;\r
269 }\r
270\r
271 gBS->CloseProtocol (\r
272 ControllerHandle,\r
273 &gEfiSerialIoProtocolGuid,\r
274 This->DriverBindingHandle,\r
275 ControllerHandle\r
276 );\r
277\r
278 return EFI_SUCCESS;\r
279}\r
280\r
281/**\r
282 Binds exclusively to serial io on the controller handle, Produces DebugPort\r
283 protocol and DevicePath on new handle.\r
284\r
285 @param This Protocol instance pointer.\r
286 @param ControllerHandle Handle of device to bind driver to.\r
287 @param RemainingDevicePath Optional parameter use to pick a specific child\r
288 device to start.\r
289\r
290 @retval EFI_SUCCESS This driver is added to ControllerHandle.\r
291 @retval EFI_OUT_OF_RESOURCES Fails to allocate memory for device.\r
292 @retval others Some error occurs. \r
293\r
294**/\r
295EFI_STATUS\r
296EFIAPI\r
297DebugPortStart (\r
298 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
299 IN EFI_HANDLE ControllerHandle,\r
300 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath\r
301 )\r
302{\r
303 EFI_STATUS Status;\r
304 DEBUGPORT_DEVICE_PATH DebugPortDP;\r
305 EFI_DEVICE_PATH_PROTOCOL EndDP;\r
306 EFI_DEVICE_PATH_PROTOCOL *Dp1;\r
307\r
308 Status = gBS->OpenProtocol (\r
309 ControllerHandle,\r
310 &gEfiSerialIoProtocolGuid,\r
311 (VOID **) &mDebugPortDevice.SerialIoBinding,\r
312 This->DriverBindingHandle,\r
313 ControllerHandle,\r
314 EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE\r
315 );\r
316 if (EFI_ERROR (Status)) {\r
317 return Status;\r
318 }\r
319\r
320 mDebugPortDevice.SerialIoDeviceHandle = ControllerHandle;\r
321\r
322 //\r
323 // Initialize the Serial Io interface...\r
324 //\r
325 Status = mDebugPortDevice.SerialIoBinding->SetAttributes (\r
326 mDebugPortDevice.SerialIoBinding,\r
327 mDebugPortDevice.BaudRate,\r
328 mDebugPortDevice.ReceiveFifoDepth,\r
329 mDebugPortDevice.Timeout,\r
330 mDebugPortDevice.Parity,\r
331 mDebugPortDevice.DataBits,\r
332 mDebugPortDevice.StopBits\r
333 );\r
334 if (EFI_ERROR (Status)) {\r
335 mDebugPortDevice.BaudRate = 0;\r
336 mDebugPortDevice.Parity = DefaultParity;\r
337 mDebugPortDevice.DataBits = 0;\r
338 mDebugPortDevice.StopBits = DefaultStopBits;\r
339 mDebugPortDevice.ReceiveFifoDepth = 0;\r
340 Status = mDebugPortDevice.SerialIoBinding->SetAttributes (\r
341 mDebugPortDevice.SerialIoBinding,\r
342 mDebugPortDevice.BaudRate,\r
343 mDebugPortDevice.ReceiveFifoDepth,\r
344 mDebugPortDevice.Timeout,\r
345 mDebugPortDevice.Parity,\r
346 mDebugPortDevice.DataBits,\r
347 mDebugPortDevice.StopBits\r
348 );\r
349 if (EFI_ERROR (Status)) {\r
350 gBS->CloseProtocol (\r
351 ControllerHandle,\r
352 &gEfiSerialIoProtocolGuid,\r
353 This->DriverBindingHandle,\r
354 ControllerHandle\r
355 );\r
356 return Status;\r
357 }\r
358 }\r
359\r
360 mDebugPortDevice.SerialIoBinding->Reset (mDebugPortDevice.SerialIoBinding);\r
361\r
362 //\r
363 // Create device path instance for DebugPort\r
364 //\r
365 DebugPortDP.Header.Type = MESSAGING_DEVICE_PATH;\r
366 DebugPortDP.Header.SubType = MSG_VENDOR_DP;\r
367 SetDevicePathNodeLength (&(DebugPortDP.Header), sizeof (DebugPortDP));\r
368 CopyMem (&DebugPortDP.Guid, &gEfiDebugPortDevicePathGuid, sizeof (EFI_GUID));\r
369\r
370 Dp1 = DevicePathFromHandle (ControllerHandle);\r
371 if (Dp1 == NULL) {\r
372 Dp1 = &EndDP;\r
373 SetDevicePathEndNode (Dp1);\r
374 }\r
375\r
376 mDebugPortDevice.DebugPortDevicePath = AppendDevicePathNode (Dp1, (EFI_DEVICE_PATH_PROTOCOL *) &DebugPortDP);\r
377 if (mDebugPortDevice.DebugPortDevicePath == NULL) {\r
378 return EFI_OUT_OF_RESOURCES;\r
379 }\r
380 //\r
381 // Publish DebugPort and Device Path protocols\r
382 //\r
383 Status = gBS->InstallMultipleProtocolInterfaces (\r
384 &mDebugPortDevice.DebugPortDeviceHandle,\r
385 &gEfiDevicePathProtocolGuid,\r
386 mDebugPortDevice.DebugPortDevicePath,\r
387 &gEfiDebugPortProtocolGuid,\r
388 &mDebugPortDevice.DebugPortInterface,\r
389 NULL\r
390 );\r
391\r
392 if (EFI_ERROR (Status)) {\r
393 gBS->CloseProtocol (\r
394 ControllerHandle,\r
395 &gEfiSerialIoProtocolGuid,\r
396 This->DriverBindingHandle,\r
397 ControllerHandle\r
398 );\r
399 return Status;\r
400 }\r
401 //\r
402 // Connect debugport child to serial io\r
403 //\r
404 Status = gBS->OpenProtocol (\r
405 ControllerHandle,\r
406 &gEfiSerialIoProtocolGuid,\r
407 (VOID **) &mDebugPortDevice.SerialIoBinding,\r
408 This->DriverBindingHandle,\r
409 mDebugPortDevice.DebugPortDeviceHandle,\r
410 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER\r
411 );\r
412\r
413 if (EFI_ERROR (Status)) {\r
414 DEBUG_CODE_BEGIN ();\r
415 UINTN BufferSize;\r
416\r
417 BufferSize = 48;\r
418 DebugPortWrite (\r
419 &mDebugPortDevice.DebugPortInterface,\r
420 0,\r
421 &BufferSize,\r
422 "DebugPort driver failed to open child controller\n\n"\r
423 );\r
424 DEBUG_CODE_END ();\r
425\r
426 gBS->CloseProtocol (\r
427 ControllerHandle,\r
428 &gEfiSerialIoProtocolGuid,\r
429 This->DriverBindingHandle,\r
430 ControllerHandle\r
431 );\r
432 return Status;\r
433 }\r
434\r
435 DEBUG_CODE_BEGIN ();\r
436 UINTN BufferSize;\r
437\r
438 BufferSize = 38;\r
439 DebugPortWrite (\r
440 &mDebugPortDevice.DebugPortInterface,\r
441 0,\r
442 &BufferSize,\r
443 "Hello World from the DebugPort driver\n\n"\r
444 );\r
445\r
446 DEBUG_CODE_END ();\r
447\r
448 return EFI_SUCCESS;\r
449}\r
450\r
451/**\r
452 Stop this driver on ControllerHandle by removing Serial IO protocol on\r
453 the ControllerHandle.\r
454\r
455 @param This Protocol instance pointer.\r
456 @param ControllerHandle Handle of device to stop driver on\r
457 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of\r
458 children is zero stop the entire bus driver.\r
459 @param ChildHandleBuffer List of Child Handles to Stop.\r
460\r
461 @retval EFI_SUCCESS This driver is removed ControllerHandle.\r
462 @retval other This driver was not removed from this device.\r
463\r
464**/\r
465EFI_STATUS\r
466EFIAPI\r
467DebugPortStop (\r
468 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
469 IN EFI_HANDLE ControllerHandle,\r
470 IN UINTN NumberOfChildren,\r
471 IN EFI_HANDLE *ChildHandleBuffer\r
472 )\r
473{\r
474 EFI_STATUS Status;\r
475\r
476 if (NumberOfChildren == 0) {\r
477 //\r
478 // Close the bus driver\r
479 //\r
480 gBS->CloseProtocol (\r
481 ControllerHandle,\r
482 &gEfiSerialIoProtocolGuid,\r
483 This->DriverBindingHandle,\r
484 ControllerHandle\r
485 );\r
486\r
487 mDebugPortDevice.SerialIoBinding = NULL;\r
488\r
489 gBS->CloseProtocol (\r
490 ControllerHandle,\r
491 &gEfiDevicePathProtocolGuid,\r
492 This->DriverBindingHandle,\r
493 ControllerHandle\r
494 );\r
495\r
496 FreePool (mDebugPortDevice.DebugPortDevicePath);\r
497\r
498 return EFI_SUCCESS;\r
499 } else {\r
500 //\r
501 // Disconnect SerialIo child handle\r
502 //\r
503 Status = gBS->CloseProtocol (\r
504 mDebugPortDevice.SerialIoDeviceHandle,\r
505 &gEfiSerialIoProtocolGuid,\r
506 This->DriverBindingHandle,\r
507 mDebugPortDevice.DebugPortDeviceHandle\r
508 );\r
509\r
510 if (EFI_ERROR (Status)) {\r
511 return Status;\r
512 }\r
513 //\r
514 // Unpublish our protocols (DevicePath, DebugPort)\r
515 //\r
516 Status = gBS->UninstallMultipleProtocolInterfaces (\r
517 mDebugPortDevice.DebugPortDeviceHandle,\r
518 &gEfiDevicePathProtocolGuid,\r
519 mDebugPortDevice.DebugPortDevicePath,\r
520 &gEfiDebugPortProtocolGuid,\r
521 &mDebugPortDevice.DebugPortInterface,\r
522 NULL\r
523 );\r
524\r
525 if (EFI_ERROR (Status)) {\r
526 gBS->OpenProtocol (\r
527 ControllerHandle,\r
528 &gEfiSerialIoProtocolGuid,\r
529 (VOID **) &mDebugPortDevice.SerialIoBinding,\r
530 This->DriverBindingHandle,\r
531 mDebugPortDevice.DebugPortDeviceHandle,\r
532 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER\r
533 );\r
534 } else {\r
535 mDebugPortDevice.DebugPortDeviceHandle = NULL;\r
536 }\r
537 }\r
538\r
539 return Status;\r
540}\r
541\r
542/**\r
543 DebugPort protocol member function. Calls SerialIo:GetControl to flush buffer.\r
544 We cannot call SerialIo:SetAttributes because it uses pool services, which use\r
545 locks, which affect TPL, so it's not interrupt context safe or re-entrant.\r
546 SerialIo:Reset() calls SetAttributes, so it can't be used either.\r
547\r
548 The port itself should be fine since it was set up during initialization.\r
549\r
550 @param This Protocol instance pointer. \r
551\r
552 @return EFI_SUCCESS Always.\r
553\r
554**/ \r
555EFI_STATUS\r
556EFIAPI\r
557DebugPortReset (\r
558 IN EFI_DEBUGPORT_PROTOCOL *This\r
559 )\r
560{\r
561 UINTN BufferSize;\r
562 UINTN BitBucket;\r
563\r
564 while (This->Poll (This) == EFI_SUCCESS) {\r
565 BufferSize = 1;\r
566 This->Read (This, 0, &BufferSize, &BitBucket);\r
567 }\r
568\r
569 return EFI_SUCCESS;\r
570}\r
571\r
572/**\r
573 DebugPort protocol member function. Calls SerialIo:Read() after setting\r
574 if it's different than the last SerialIo access.\r
575\r
576 @param This Pointer to DebugPort protocol.\r
577 @param Timeout Timeout value.\r
578 @param BufferSize On input, the size of Buffer.\r
579 On output, the amount of data actually written.\r
580 @param Buffer Pointer to buffer to read.\r
581\r
582 @retval EFI_SUCCESS \r
583 @retval others \r
584\r
585**/\r
586EFI_STATUS\r
587EFIAPI\r
588DebugPortRead (\r
589 IN EFI_DEBUGPORT_PROTOCOL *This,\r
590 IN UINT32 Timeout,\r
591 IN OUT UINTN *BufferSize,\r
592 IN VOID *Buffer\r
593 )\r
594{\r
595 DEBUGPORT_DEVICE *DebugPortDevice;\r
596 UINTN LocalBufferSize;\r
597 EFI_STATUS Status;\r
598 UINT8 *BufferPtr;\r
599\r
600 DebugPortDevice = DEBUGPORT_DEVICE_FROM_THIS (This);\r
601 BufferPtr = Buffer;\r
602 LocalBufferSize = *BufferSize;\r
603\r
604 do {\r
605 Status = DebugPortDevice->SerialIoBinding->Read (\r
606 DebugPortDevice->SerialIoBinding,\r
607 &LocalBufferSize,\r
608 BufferPtr\r
609 );\r
610 if (Status == EFI_TIMEOUT) {\r
611 if (Timeout > DEBUGPORT_UART_DEFAULT_TIMEOUT) {\r
612 Timeout -= DEBUGPORT_UART_DEFAULT_TIMEOUT;\r
613 } else {\r
614 Timeout = 0;\r
615 }\r
616 } else if (EFI_ERROR (Status)) {\r
617 break;\r
618 }\r
619\r
620 BufferPtr += LocalBufferSize;\r
621 LocalBufferSize = *BufferSize - (BufferPtr - (UINT8 *) Buffer);\r
622 } while (LocalBufferSize != 0 && Timeout > 0);\r
623\r
624 *BufferSize = (UINTN) (BufferPtr - (UINT8 *) Buffer);\r
625\r
626 return Status;\r
627}\r
628\r
629/**\r
630 DebugPort protocol member function. Calls SerialIo:Write() Writes 8 bytes at\r
631 a time and does a GetControl between 8 byte writes to help insure reads are\r
632 interspersed This is poor-man's flow control.\r
633\r
634 @param This Pointer to DebugPort protocol.\r
635 @param Timeout Timeout value.\r
636 @param BufferSize On input, the size of Buffer.\r
637 On output, the amount of data actually written.\r
638 @param Buffer Pointer to buffer to read.\r
639\r
640 @retval EFI_SUCCESS The data was written.\r
641 @retval others Fails when writting datas to debug port device.\r
642\r
643**/\r
644EFI_STATUS\r
645EFIAPI\r
646DebugPortWrite (\r
647 IN EFI_DEBUGPORT_PROTOCOL *This,\r
648 IN UINT32 Timeout,\r
649 IN OUT UINTN *BufferSize,\r
650 OUT VOID *Buffer\r
651 )\r
652{\r
653 DEBUGPORT_DEVICE *DebugPortDevice;\r
654 UINTN Position;\r
655 UINTN WriteSize;\r
656 EFI_STATUS Status;\r
657 UINT32 SerialControl;\r
658\r
659 Status = EFI_SUCCESS;\r
660 DebugPortDevice = DEBUGPORT_DEVICE_FROM_THIS (This);\r
661\r
662 WriteSize = 8;\r
663 for (Position = 0; Position < *BufferSize && !EFI_ERROR (Status); Position += WriteSize) {\r
664 DebugPortDevice->SerialIoBinding->GetControl (\r
665 DebugPortDevice->SerialIoBinding,\r
666 &SerialControl\r
667 );\r
668 if (*BufferSize - Position < 8) {\r
669 WriteSize = *BufferSize - Position;\r
670 }\r
671\r
672 Status = DebugPortDevice->SerialIoBinding->Write (\r
673 DebugPortDevice->SerialIoBinding,\r
674 &WriteSize,\r
675 &((UINT8 *) Buffer)[Position]\r
676 );\r
677 }\r
678\r
679 *BufferSize = Position;\r
680 return Status;\r
681}\r
682\r
683/**\r
684 DebugPort protocol member function. Calls SerialIo:Write() after setting\r
685 if it's different than the last SerialIo access.\r
686\r
687 @param This Pointer to DebugPort protocol.\r
688\r
689 @retval EFI_SUCCESS At least 1 character is ready to be read from\r
690 the DebugPort interface.\r
691 @retval EFI_NOT_READY There are no characters ready to read from the\r
692 DebugPort interface\r
693 @retval EFI_DEVICE_ERROR A hardware failure occured... (from SerialIo)\r
694\r
695**/ \r
696EFI_STATUS\r
697EFIAPI\r
698DebugPortPoll (\r
699 IN EFI_DEBUGPORT_PROTOCOL *This\r
700 )\r
701{\r
702 EFI_STATUS Status;\r
703 UINT32 SerialControl;\r
704 DEBUGPORT_DEVICE *DebugPortDevice;\r
705\r
706 DebugPortDevice = DEBUGPORT_DEVICE_FROM_THIS (This);\r
707\r
708 Status = DebugPortDevice->SerialIoBinding->GetControl (\r
709 DebugPortDevice->SerialIoBinding,\r
710 &SerialControl\r
711 );\r
712\r
713 if (!EFI_ERROR (Status)) {\r
714 if ((SerialControl & EFI_SERIAL_INPUT_BUFFER_EMPTY) != 0) {\r
715 Status = EFI_NOT_READY;\r
716 } else {\r
717 Status = EFI_SUCCESS;\r
718 }\r
719 }\r
720\r
721 return Status;\r
722}\r
723\r
724/**\r
725 Unload function that is registered in the LoadImage protocol. It un-installs\r
726 protocols produced and deallocates pool used by the driver. Called by the core\r
727 when unloading the driver.\r
728\r
729 @param ImageHandle\r
730\r
731 @retval EFI_SUCCESS Unload Debug Port driver successfully.\r
732 @retval EFI_ABORTED Serial IO is still binding.\r
733\r
734**/\r
735EFI_STATUS\r
736EFIAPI\r
737ImageUnloadHandler (\r
738 EFI_HANDLE ImageHandle\r
739 )\r
740{\r
741 if (mDebugPortDevice.SerialIoBinding != NULL) {\r
742 return EFI_ABORTED;\r
743 }\r
744\r
745 //\r
746 // Clean up allocations\r
747 //\r
748 if (mDebugPortDevice.DebugPortVariable != NULL) {\r
749 FreePool (mDebugPortDevice.DebugPortVariable);\r
750 }\r
751\r
752 return EFI_SUCCESS;\r
753}\r