1 /** @file
2   Debug Port Library implementation based on serial port.
3 
4   Copyright (c) 2010 - 2015, Intel Corporation. All rights reserved.<BR>
5   This program and the accompanying materials
6   are licensed and made available under the terms and conditions of the BSD License
7   which accompanies this distribution.  The full text of the license may be found at
8   http://opensource.org/licenses/bsd-license.php.
9 
10   THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11   WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12 
13 **/
14 
15 #include <Base.h>
16 
17 #include <Library/DebugCommunicationLib.h>
18 #include <Library/SerialPortLib.h>
19 #include <Library/DebugLib.h>
20 
21 /**
22   Initialize the debug port.
23 
24   This function will initialize debug port to get it ready for data transmition. If
25   certain Debug Communication Library instance has to save some private data in the
26   stack, this function must work on the mode that doesn't return to the caller, then
27   the caller needs to wrap up all rest of logic after DebugPortInitialize() into one
28   function and pass it into DebugPortInitialize(). DebugPortInitialize() is
29   responsible to invoke the passing-in funciton at the end of DebugPortInitialize().
30 
31   If the paramter Function is not NULL, Debug Communication Libary instance will
32   invoke it by passing in the Context to be the first parameter. Debug Communication
33   Library instance could create one debug port handle to be the second parameter
34   passing into the Function. Debug Communication Library instance also could pass
35   NULL to be the second parameter if it doesn't create the debug port handle.
36 
37   If the parameter Function is NULL, and Context is not NULL. At this time, Context
38   is the debug port handle created by the previous Debug Communication Library
39   instance.
40   a) If the instance can understand and continue use the private data of the previous
41      instance, it could return the same handle as passed in (as Context parameter).
42   b) If the instance does not understand, or does not want to continue use the
43      private data of the previous instance, it could ignore the input Context parameter
44      and create the new handle to be returned.
45 
46   If Function() is NULL and Context is NULL, Debug Communication Library could create a
47   new handle and return it. NULL is also a valid handle to be returned.
48 
49   @param[in] Context      Context needed by callback function; it was optional.
50   @param[in] Function     Continue function called by Debug Communication library;
51                           it was optional.
52 
53   @return  The debug port handle created by Debug Communication Library if Function
54            is not NULL.
55 
56 **/
57 DEBUG_PORT_HANDLE
58 EFIAPI
DebugPortInitialize(IN VOID * Context,IN DEBUG_PORT_CONTINUE Function)59 DebugPortInitialize (
60   IN VOID                 *Context,
61   IN DEBUG_PORT_CONTINUE  Function
62   )
63 {
64   RETURN_STATUS      Status;
65 
66   Status = SerialPortInitialize ();
67   if (RETURN_ERROR(Status)) {
68     DEBUG ((EFI_D_ERROR, "Debug Serial Port: Initialization failed!\n"));
69   }
70 
71   if (Function != NULL) {
72     Function (Context, NULL);
73   }
74 
75   return NULL;
76 }
77 
78 /**
79   Read data from debug device and save the datas in buffer.
80 
81   Reads NumberOfBytes data bytes from a debug device into the buffer
82   specified by Buffer. The number of bytes actually read is returned.
83   If the return value is less than NumberOfBytes, then the rest operation failed.
84   If NumberOfBytes is zero, then return 0.
85 
86   @param  Handle           Debug port handle.
87   @param  Buffer           Pointer to the data buffer to store the data read from the debug device.
88   @param  NumberOfBytes    Number of bytes which will be read.
89   @param  Timeout          Timeout value for reading from debug device. It unit is Microsecond.
90 
91   @retval 0                Read data failed, no data is to be read.
92   @retval >0               Actual number of bytes read from debug device.
93 
94 **/
95 UINTN
96 EFIAPI
DebugPortReadBuffer(IN DEBUG_PORT_HANDLE Handle,IN UINT8 * Buffer,IN UINTN NumberOfBytes,IN UINTN Timeout)97 DebugPortReadBuffer (
98   IN DEBUG_PORT_HANDLE     Handle,
99   IN UINT8                 *Buffer,
100   IN UINTN                 NumberOfBytes,
101   IN UINTN                 Timeout
102   )
103 {
104   if (NumberOfBytes != 1 || Buffer == NULL || Timeout != 0) {
105     return 0;
106   }
107 
108   return SerialPortRead (Buffer, 1);
109 }
110 
111 /**
112   Write data from buffer to debug device.
113 
114   Writes NumberOfBytes data bytes from Buffer to the debug device.
115   The number of bytes actually written to the debug device is returned.
116   If the return value is less than NumberOfBytes, then the write operation failed.
117   If NumberOfBytes is zero, then return 0.
118 
119   @param  Handle           Debug port handle.
120   @param  Buffer           Pointer to the data buffer to be written.
121   @param  NumberOfBytes    Number of bytes to written to the debug device.
122 
123   @retval 0                NumberOfBytes is 0.
124   @retval >0               The number of bytes written to the debug device.
125                            If this value is less than NumberOfBytes, then the read operation failed.
126 
127 **/
128 UINTN
129 EFIAPI
DebugPortWriteBuffer(IN DEBUG_PORT_HANDLE Handle,IN UINT8 * Buffer,IN UINTN NumberOfBytes)130 DebugPortWriteBuffer (
131   IN DEBUG_PORT_HANDLE     Handle,
132   IN UINT8                 *Buffer,
133   IN UINTN                 NumberOfBytes
134   )
135 {
136   return SerialPortWrite (Buffer, NumberOfBytes);
137 }
138 
139 /**
140   Polls a debug device to see if there is any data waiting to be read.
141 
142   Polls a debug device to see if there is any data waiting to be read.
143   If there is data waiting to be read from the debug device, then TRUE is returned.
144   If there is no data waiting to be read from the debug device, then FALSE is returned.
145 
146   @param  Handle           Debug port handle.
147 
148   @retval TRUE             Data is waiting to be read from the debug device.
149   @retval FALSE            There is no data waiting to be read from the serial device.
150 
151 **/
152 BOOLEAN
153 EFIAPI
DebugPortPollBuffer(IN DEBUG_PORT_HANDLE Handle)154 DebugPortPollBuffer (
155   IN DEBUG_PORT_HANDLE     Handle
156   )
157 {
158   return SerialPortPoll ();
159 }
160 
161