4 * Copyright (c) 2016, Linaro Ltd. All rights reserved.<BR>
6 * This program and the accompanying materials are
7 * licensed and made available under the terms and conditions of the BSD License
8 * which accompanies this distribution. The full text of the license may be found at
9 * http://opensource.org/licenses/bsd-license.php
11 * THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include <Library/BaseLib.h>
17 #include <Library/DebugLib.h>
18 #include <Library/UefiDriverEntryPoint.h>
19 #include <Library/UefiBootServicesTableLib.h>
20 #include <Library/HobLib.h>
24 #include <Guid/FdtHob.h>
25 #include <Guid/PlatformHasDeviceTree.h>
27 #include <Protocol/FdtClient.h>
29 STATIC VOID
*mDeviceTreeBase
;
35 IN FDT_CLIENT_PROTOCOL
*This
,
37 IN CONST CHAR8
*PropertyName
,
38 OUT CONST VOID
**Prop
,
39 OUT UINT32
*PropSize OPTIONAL
44 ASSERT (mDeviceTreeBase
!= NULL
);
45 ASSERT (Prop
!= NULL
);
47 *Prop
= fdt_getprop (mDeviceTreeBase
, Node
, PropertyName
, &Len
);
52 if (PropSize
!= NULL
) {
62 IN FDT_CLIENT_PROTOCOL
*This
,
64 IN CONST CHAR8
*PropertyName
,
71 ASSERT (mDeviceTreeBase
!= NULL
);
73 Ret
= fdt_setprop (mDeviceTreeBase
, Node
, PropertyName
, Prop
, PropSize
);
75 return EFI_DEVICE_ERROR
;
84 FindNextCompatibleNode (
85 IN FDT_CLIENT_PROTOCOL
*This
,
86 IN CONST CHAR8
*CompatibleString
,
92 CONST CHAR8
*Type
, *Compatible
;
95 ASSERT (mDeviceTreeBase
!= NULL
);
96 ASSERT (Node
!= NULL
);
98 for (Prev
= PrevNode
;; Prev
= Next
) {
99 Next
= fdt_next_node (mDeviceTreeBase
, Prev
, NULL
);
104 Type
= fdt_getprop (mDeviceTreeBase
, Next
, "compatible", &Len
);
110 // A 'compatible' node may contain a sequence of NUL terminated
111 // compatible strings so check each one
113 for (Compatible
= Type
; Compatible
< Type
+ Len
&& *Compatible
;
114 Compatible
+= 1 + AsciiStrLen (Compatible
)) {
115 if (AsciiStrCmp (CompatibleString
, Compatible
) == 0) {
121 return EFI_NOT_FOUND
;
128 IN FDT_CLIENT_PROTOCOL
*This
,
129 IN CONST CHAR8
*CompatibleString
,
133 return FindNextCompatibleNode (This
, CompatibleString
, 0, Node
);
139 FindCompatibleNodeProperty (
140 IN FDT_CLIENT_PROTOCOL
*This
,
141 IN CONST CHAR8
*CompatibleString
,
142 IN CONST CHAR8
*PropertyName
,
143 OUT CONST VOID
**Prop
,
144 OUT UINT32
*PropSize OPTIONAL
150 Status
= FindCompatibleNode (This
, CompatibleString
, &Node
);
151 if (EFI_ERROR (Status
)) {
155 return GetNodeProperty (This
, Node
, PropertyName
, Prop
, PropSize
);
161 FindCompatibleNodeReg (
162 IN FDT_CLIENT_PROTOCOL
*This
,
163 IN CONST CHAR8
*CompatibleString
,
164 OUT CONST VOID
**Reg
,
165 OUT UINTN
*AddressCells
,
166 OUT UINTN
*SizeCells
,
172 ASSERT (RegSize
!= NULL
);
175 // Get the 'reg' property of this node. For now, we will assume
176 // 8 byte quantities for base and size, respectively.
177 // TODO use #cells root properties instead
179 Status
= FindCompatibleNodeProperty (This
, CompatibleString
, "reg", Reg
,
181 if (EFI_ERROR (Status
)) {
185 if ((*RegSize
% 16) != 0) {
187 "%a: '%a' compatible node has invalid 'reg' property (size == 0x%x)\n",
188 __FUNCTION__
, CompatibleString
, *RegSize
));
189 return EFI_NOT_FOUND
;
201 FindNextMemoryNodeReg (
202 IN FDT_CLIENT_PROTOCOL
*This
,
205 OUT CONST VOID
**Reg
,
206 OUT UINTN
*AddressCells
,
207 OUT UINTN
*SizeCells
,
212 CONST CHAR8
*DeviceType
;
213 CONST CHAR8
*NodeStatus
;
217 ASSERT (mDeviceTreeBase
!= NULL
);
218 ASSERT (Node
!= NULL
);
220 for (Prev
= PrevNode
;; Prev
= Next
) {
221 Next
= fdt_next_node (mDeviceTreeBase
, Prev
, NULL
);
226 NodeStatus
= fdt_getprop (mDeviceTreeBase
, Next
, "status", &Len
);
227 if (NodeStatus
!= NULL
&& AsciiStrCmp (NodeStatus
, "okay") != 0) {
228 DEBUG ((DEBUG_WARN
, "%a: ignoring memory node with status \"%a\"\n",
229 __FUNCTION__
, NodeStatus
));
233 DeviceType
= fdt_getprop (mDeviceTreeBase
, Next
, "device_type", &Len
);
234 if (DeviceType
!= NULL
&& AsciiStrCmp (DeviceType
, "memory") == 0) {
236 // Get the 'reg' property of this memory node. For now, we will assume
237 // 8 byte quantities for base and size, respectively.
238 // TODO use #cells root properties instead
240 Status
= GetNodeProperty (This
, Next
, "reg", Reg
, RegSize
);
241 if (EFI_ERROR (Status
)) {
243 "%a: ignoring memory node with no 'reg' property\n",
247 if ((*RegSize
% 16) != 0) {
249 "%a: ignoring memory node with invalid 'reg' property (size == 0x%x)\n",
250 __FUNCTION__
, *RegSize
));
260 return EFI_NOT_FOUND
;
267 IN FDT_CLIENT_PROTOCOL
*This
,
269 OUT CONST VOID
**Reg
,
270 OUT UINTN
*AddressCells
,
271 OUT UINTN
*SizeCells
,
275 return FindNextMemoryNodeReg (This
, 0, Node
, Reg
, AddressCells
, SizeCells
,
282 GetOrInsertChosenNode (
283 IN FDT_CLIENT_PROTOCOL
*This
,
289 ASSERT (mDeviceTreeBase
!= NULL
);
290 ASSERT (Node
!= NULL
);
292 NewNode
= fdt_path_offset (mDeviceTreeBase
, "/chosen");
294 NewNode
= fdt_add_subnode (mDeviceTreeBase
, 0, "/chosen");
298 return EFI_OUT_OF_RESOURCES
;
306 STATIC FDT_CLIENT_PROTOCOL mFdtClientProtocol
= {
310 FindNextCompatibleNode
,
311 FindCompatibleNodeProperty
,
312 FindCompatibleNodeReg
,
314 FindNextMemoryNodeReg
,
315 GetOrInsertChosenNode
,
321 OnPlatformHasDeviceTree (
328 VOID
*DeviceTreeBase
;
330 Status
= gBS
->LocateProtocol (
331 &gEdkiiPlatformHasDeviceTreeGuid
,
332 NULL
, // Registration
335 if (EFI_ERROR (Status
)) {
339 DeviceTreeBase
= Context
;
342 "%a: exposing DTB @ 0x%p to OS\n",
346 Status
= gBS
->InstallConfigurationTable (&gFdtTableGuid
, DeviceTreeBase
);
347 ASSERT_EFI_ERROR (Status
);
349 gBS
->CloseEvent (Event
);
354 InitializeFdtClientDxe (
355 IN EFI_HANDLE ImageHandle
,
356 IN EFI_SYSTEM_TABLE
*SystemTable
360 VOID
*DeviceTreeBase
;
362 EFI_EVENT PlatformHasDeviceTreeEvent
;
365 Hob
= GetFirstGuidHob (&gFdtHobGuid
);
366 if (Hob
== NULL
|| GET_GUID_HOB_DATA_SIZE (Hob
) != sizeof (UINT64
)) {
367 return EFI_NOT_FOUND
;
369 DeviceTreeBase
= (VOID
*)(UINTN
)*(UINT64
*)GET_GUID_HOB_DATA (Hob
);
371 if (fdt_check_header (DeviceTreeBase
) != 0) {
372 DEBUG ((EFI_D_ERROR
, "%a: No DTB found @ 0x%p\n", __FUNCTION__
,
374 return EFI_NOT_FOUND
;
377 mDeviceTreeBase
= DeviceTreeBase
;
379 DEBUG ((EFI_D_INFO
, "%a: DTB @ 0x%p\n", __FUNCTION__
, mDeviceTreeBase
));
382 // Register a protocol notify for the EDKII Platform Has Device Tree
385 Status
= gBS
->CreateEvent (
388 OnPlatformHasDeviceTree
,
389 DeviceTreeBase
, // Context
390 &PlatformHasDeviceTreeEvent
392 if (EFI_ERROR (Status
)) {
393 DEBUG ((DEBUG_ERROR
, "%a: CreateEvent(): %r\n", __FUNCTION__
, Status
));
397 Status
= gBS
->RegisterProtocolNotify (
398 &gEdkiiPlatformHasDeviceTreeGuid
,
399 PlatformHasDeviceTreeEvent
,
402 if (EFI_ERROR (Status
)) {
405 "%a: RegisterProtocolNotify(): %r\n",
413 // Kick the event; the protocol could be available already.
415 Status
= gBS
->SignalEvent (PlatformHasDeviceTreeEvent
);
416 if (EFI_ERROR (Status
)) {
417 DEBUG ((DEBUG_ERROR
, "%a: SignalEvent(): %r\n", __FUNCTION__
, Status
));
421 Status
= gBS
->InstallProtocolInterface (
423 &gFdtClientProtocolGuid
,
424 EFI_NATIVE_INTERFACE
,
427 if (EFI_ERROR (Status
)) {
430 "%a: InstallProtocolInterface(): %r\n",
440 gBS
->CloseEvent (PlatformHasDeviceTreeEvent
);