TianoCore EDK2 master
Loading...
Searching...
No Matches
FdtSerialPortAddressLib.c
Go to the documentation of this file.
1
13#include <Library/BaseLib.h>
15#include <libfdt.h>
16
38RETURN_STATUS
40 IN CONST VOID *DeviceTree,
41 IN INT32 Node,
42 OUT UINT64 *BaseAddress
43 )
44{
45 CONST CHAR8 *NodeStatus;
46 CONST VOID *RegProp;
47 INT32 PropSize;
48
49 NodeStatus = fdt_getprop (DeviceTree, Node, "status", NULL);
50 if ((NodeStatus != NULL) && (AsciiStrCmp (NodeStatus, "okay") != 0)) {
52 }
53
54 RegProp = fdt_getprop (DeviceTree, Node, "reg", &PropSize);
55 if (RegProp == NULL) {
56 return RETURN_NOT_FOUND;
57 }
58
59 if (PropSize != 16) {
61 }
62
63 *BaseAddress = fdt64_to_cpu (ReadUnaligned64 (RegProp));
64 return RETURN_SUCCESS;
65}
66
92RETURN_STATUS
93EFIAPI
95 IN CONST VOID *DeviceTree,
96 IN CONST CHAR8 *Compatible,
98 )
99{
100 INT32 Node;
101
102 if (fdt_check_header (DeviceTree) != 0) {
104 }
105
106 Ports->NumberOfPorts = 0;
107 Node = fdt_next_node (DeviceTree, 0, NULL);
108 while ((Node > 0) &&
109 (Ports->NumberOfPorts < ARRAY_SIZE (Ports->BaseAddress)))
110 {
111 CONST CHAR8 *CompatProp;
112 INT32 PropSize;
113
114 CompatProp = fdt_getprop (DeviceTree, Node, "compatible", &PropSize);
115 if (CompatProp != NULL) {
116 CONST CHAR8 *CompatItem;
117
118 CompatItem = CompatProp;
119 while ((CompatItem < CompatProp + PropSize) &&
120 (AsciiStrCmp (CompatItem, Compatible) != 0))
121 {
122 CompatItem += AsciiStrLen (CompatItem) + 1;
123 }
124
125 if (CompatItem < CompatProp + PropSize) {
126 RETURN_STATUS Status;
127 UINT64 BaseAddress;
128
129 Status = GetBaseAddress (DeviceTree, Node, &BaseAddress);
130 if (!RETURN_ERROR (Status)) {
131 Ports->BaseAddress[Ports->NumberOfPorts++] = BaseAddress;
132 }
133 }
134 }
135
136 Node = fdt_next_node (DeviceTree, Node, NULL);
137 }
138
139 return Ports->NumberOfPorts > 0 ? RETURN_SUCCESS : RETURN_NOT_FOUND;
140}
141
166RETURN_STATUS
167EFIAPI
169 IN CONST VOID *DeviceTree,
170 OUT UINT64 *BaseAddress
171 )
172{
173 INT32 ChosenNode;
174 CONST CHAR8 *StdoutPathProp;
175 INT32 PropSize;
176 CONST CHAR8 *StdoutPathEnd;
177 UINTN StdoutPathLength;
178 INT32 ConsoleNode;
179 RETURN_STATUS Status;
180
181 if (fdt_check_header (DeviceTree) != 0) {
183 }
184
185 ChosenNode = fdt_path_offset (DeviceTree, "/chosen");
186 if (ChosenNode < 0) {
187 return RETURN_NOT_FOUND;
188 }
189
190 StdoutPathProp = fdt_getprop (
191 DeviceTree,
192 ChosenNode,
193 "stdout-path",
194 &PropSize
195 );
196 if (StdoutPathProp == NULL) {
197 return RETURN_NOT_FOUND;
198 }
199
200 //
201 // If StdoutPathProp contains a colon (":"), then the colon terminates the
202 // path we're interested in.
203 //
204 StdoutPathEnd = AsciiStrStr (StdoutPathProp, ":");
205 if (StdoutPathEnd == NULL) {
206 StdoutPathLength = PropSize - 1;
207 } else {
208 StdoutPathLength = StdoutPathEnd - StdoutPathProp;
209 }
210
211 if (StdoutPathLength == 0) {
213 }
214
215 if (StdoutPathProp[0] == '/') {
216 //
217 // StdoutPathProp starts with an absolute node path.
218 //
219 ConsoleNode = fdt_path_offset_namelen (
220 DeviceTree,
221 StdoutPathProp,
222 (INT32)StdoutPathLength
223 );
224 } else {
225 //
226 // StdoutPathProp starts with an alias.
227 //
228 CONST CHAR8 *ResolvedStdoutPath;
229
230 ResolvedStdoutPath = fdt_get_alias_namelen (
231 DeviceTree,
232 StdoutPathProp,
233 (INT32)StdoutPathLength
234 );
235 if (ResolvedStdoutPath == NULL) {
236 return RETURN_NOT_FOUND;
237 }
238
239 ConsoleNode = fdt_path_offset (DeviceTree, ResolvedStdoutPath);
240 }
241
242 if (ConsoleNode < 0) {
243 return RETURN_NOT_FOUND;
244 }
245
246 Status = GetBaseAddress (DeviceTree, ConsoleNode, BaseAddress);
247 switch (Status) {
248 case RETURN_NOT_FOUND:
251 case RETURN_SUCCESS:
252 return RETURN_SUCCESS;
253 default:
254 return RETURN_NOT_FOUND;
255 }
256}
UINT64 UINTN
UINT64 EFIAPI ReadUnaligned64(IN CONST UINT64 *Buffer)
Definition: Unaligned.c:204
UINTN EFIAPI AsciiStrLen(IN CONST CHAR8 *String)
Definition: String.c:641
INTN EFIAPI AsciiStrCmp(IN CONST CHAR8 *FirstString, IN CONST CHAR8 *SecondString)
Definition: String.c:716
CHAR8 *EFIAPI AsciiStrStr(IN CONST CHAR8 *String, IN CONST CHAR8 *SearchString)
Definition: String.c:931
RETURN_STATUS EFIAPI FdtSerialGetConsolePort(IN CONST VOID *DeviceTree, OUT UINT64 *BaseAddress)
STATIC RETURN_STATUS GetBaseAddress(IN CONST VOID *DeviceTree, IN INT32 Node, OUT UINT64 *BaseAddress)
RETURN_STATUS EFIAPI FdtSerialGetPorts(IN CONST VOID *DeviceTree, IN CONST CHAR8 *Compatible, OUT FDT_SERIAL_PORTS *Ports)
#define NULL
Definition: Base.h:319
#define CONST
Definition: Base.h:259
#define STATIC
Definition: Base.h:264
#define RETURN_PROTOCOL_ERROR
Definition: Base.h:1192
#define RETURN_ERROR(StatusCode)
Definition: Base.h:1061
#define RETURN_NOT_FOUND
Definition: Base.h:1142
#define RETURN_DEVICE_ERROR
Definition: Base.h:1104
#define RETURN_SUCCESS
Definition: Base.h:1066
#define ARRAY_SIZE(Array)
Definition: Base.h:1393
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
#define RETURN_INVALID_PARAMETER
Definition: Base.h:1076
#define RETURN_BAD_BUFFER_SIZE
Definition: Base.h:1086