]> git.proxmox.com Git - mirror_edk2.git/blob - ArmVirtPkg/FdtClientDxe/FdtClientDxe.c
ArmVirtPkg/FdtClientDxe: fix check for size of "reg" properties
[mirror_edk2.git] / ArmVirtPkg / FdtClientDxe / FdtClientDxe.c
1 /** @file
2 * FDT client driver
3 *
4 * Copyright (c) 2016, Linaro Ltd. All rights reserved.<BR>
5 *
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
10 *
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.
13 *
14 **/
15
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>
21 #include <libfdt.h>
22
23 #include <Guid/Fdt.h>
24 #include <Guid/FdtHob.h>
25
26 #include <Protocol/FdtClient.h>
27
28 STATIC VOID *mDeviceTreeBase;
29
30 STATIC
31 EFI_STATUS
32 GetNodeProperty (
33 IN FDT_CLIENT_PROTOCOL *This,
34 IN INT32 Node,
35 IN CONST CHAR8 *PropertyName,
36 OUT CONST VOID **Prop,
37 OUT UINT32 *PropSize OPTIONAL
38 )
39 {
40 INT32 Len;
41
42 ASSERT (mDeviceTreeBase != NULL);
43 ASSERT (Prop != NULL);
44
45 *Prop = fdt_getprop (mDeviceTreeBase, Node, PropertyName, &Len);
46 if (*Prop == NULL) {
47 return EFI_NOT_FOUND;
48 }
49
50 if (PropSize != NULL) {
51 *PropSize = Len;
52 }
53 return EFI_SUCCESS;
54 }
55
56 STATIC
57 EFI_STATUS
58 SetNodeProperty (
59 IN FDT_CLIENT_PROTOCOL *This,
60 IN INT32 Node,
61 IN CONST CHAR8 *PropertyName,
62 IN CONST VOID *Prop,
63 IN UINT32 PropSize
64 )
65 {
66 INT32 Ret;
67
68 ASSERT (mDeviceTreeBase != NULL);
69
70 Ret = fdt_setprop (mDeviceTreeBase, Node, PropertyName, Prop, PropSize);
71 if (Ret != 0) {
72 return EFI_DEVICE_ERROR;
73 }
74
75 return EFI_SUCCESS;
76 }
77
78 STATIC
79 EFI_STATUS
80 EFIAPI
81 FindNextCompatibleNode (
82 IN FDT_CLIENT_PROTOCOL *This,
83 IN CONST CHAR8 *CompatibleString,
84 IN INT32 PrevNode,
85 OUT INT32 *Node
86 )
87 {
88 INT32 Prev, Next;
89 CONST CHAR8 *Type, *Compatible;
90 INT32 Len;
91
92 ASSERT (mDeviceTreeBase != NULL);
93 ASSERT (Node != NULL);
94
95 for (Prev = PrevNode;; Prev = Next) {
96 Next = fdt_next_node (mDeviceTreeBase, Prev, NULL);
97 if (Next < 0) {
98 break;
99 }
100
101 Type = fdt_getprop (mDeviceTreeBase, Next, "compatible", &Len);
102 if (Type == NULL) {
103 continue;
104 }
105
106 //
107 // A 'compatible' node may contain a sequence of NUL terminated
108 // compatible strings so check each one
109 //
110 for (Compatible = Type; Compatible < Type + Len && *Compatible;
111 Compatible += 1 + AsciiStrLen (Compatible)) {
112 if (AsciiStrCmp (CompatibleString, Compatible) == 0) {
113 *Node = Next;
114 return EFI_SUCCESS;
115 }
116 }
117 }
118 return EFI_NOT_FOUND;
119 }
120
121 STATIC
122 EFI_STATUS
123 EFIAPI
124 FindCompatibleNode (
125 IN FDT_CLIENT_PROTOCOL *This,
126 IN CONST CHAR8 *CompatibleString,
127 OUT INT32 *Node
128 )
129 {
130 return FindNextCompatibleNode (This, CompatibleString, 0, Node);
131 }
132
133 STATIC
134 EFI_STATUS
135 EFIAPI
136 FindCompatibleNodeProperty (
137 IN FDT_CLIENT_PROTOCOL *This,
138 IN CONST CHAR8 *CompatibleString,
139 IN CONST CHAR8 *PropertyName,
140 OUT CONST VOID **Prop,
141 OUT UINT32 *PropSize OPTIONAL
142 )
143 {
144 EFI_STATUS Status;
145 INT32 Node;
146
147 Status = FindCompatibleNode (This, CompatibleString, &Node);
148 if (EFI_ERROR (Status)) {
149 return Status;
150 }
151
152 return GetNodeProperty (This, Node, PropertyName, Prop, PropSize);
153 }
154
155 STATIC
156 EFI_STATUS
157 EFIAPI
158 FindCompatibleNodeReg (
159 IN FDT_CLIENT_PROTOCOL *This,
160 IN CONST CHAR8 *CompatibleString,
161 OUT CONST VOID **Reg,
162 OUT UINT32 *RegElemSize,
163 OUT UINT32 *RegSize
164 )
165 {
166 EFI_STATUS Status;
167
168 ASSERT (RegSize != NULL);
169
170 //
171 // Get the 'reg' property of this node. For now, we will assume
172 // 8 byte quantities for base and size, respectively.
173 // TODO use #cells root properties instead
174 //
175 Status = FindCompatibleNodeProperty (This, CompatibleString, "reg", Reg,
176 RegSize);
177 if (EFI_ERROR (Status)) {
178 return Status;
179 }
180
181 if ((*RegSize % 16) != 0) {
182 DEBUG ((EFI_D_ERROR,
183 "%a: '%a' compatible node has invalid 'reg' property (size == 0x%x)\n",
184 __FUNCTION__, CompatibleString, *RegSize));
185 return EFI_NOT_FOUND;
186 }
187
188 *RegElemSize = 8;
189
190 return EFI_SUCCESS;
191 }
192
193 STATIC
194 EFI_STATUS
195 GetOrInsertChosenNode (
196 IN FDT_CLIENT_PROTOCOL *This,
197 OUT INT32 *Node
198 )
199 {
200 INT32 NewNode;
201
202 ASSERT (mDeviceTreeBase != NULL);
203 ASSERT (Node != NULL);
204
205 NewNode = fdt_path_offset (mDeviceTreeBase, "/chosen");
206 if (NewNode < 0) {
207 NewNode = fdt_add_subnode (mDeviceTreeBase, 0, "/chosen");
208 }
209
210 if (NewNode < 0) {
211 return EFI_OUT_OF_RESOURCES;
212 }
213
214 *Node = NewNode;
215
216 return EFI_SUCCESS;
217 }
218
219 STATIC FDT_CLIENT_PROTOCOL mFdtClientProtocol = {
220 GetNodeProperty,
221 SetNodeProperty,
222 FindCompatibleNode,
223 FindNextCompatibleNode,
224 FindCompatibleNodeProperty,
225 FindCompatibleNodeReg,
226 GetOrInsertChosenNode,
227 };
228
229 EFI_STATUS
230 EFIAPI
231 InitializeFdtClientDxe (
232 IN EFI_HANDLE ImageHandle,
233 IN EFI_SYSTEM_TABLE *SystemTable
234 )
235 {
236 VOID *Hob;
237 VOID *DeviceTreeBase;
238 EFI_STATUS Status;
239
240 Hob = GetFirstGuidHob (&gFdtHobGuid);
241 if (Hob == NULL || GET_GUID_HOB_DATA_SIZE (Hob) != sizeof (UINT64)) {
242 return EFI_NOT_FOUND;
243 }
244 DeviceTreeBase = (VOID *)(UINTN)*(UINT64 *)GET_GUID_HOB_DATA (Hob);
245
246 if (fdt_check_header (DeviceTreeBase) != 0) {
247 DEBUG ((EFI_D_ERROR, "%a: No DTB found @ 0x%p\n", __FUNCTION__,
248 DeviceTreeBase));
249 return EFI_NOT_FOUND;
250 }
251
252 mDeviceTreeBase = DeviceTreeBase;
253
254 DEBUG ((EFI_D_INFO, "%a: DTB @ 0x%p\n", __FUNCTION__, mDeviceTreeBase));
255
256 if (!FeaturePcdGet (PcdPureAcpiBoot)) {
257 //
258 // Only install the FDT as a configuration table if we want to leave it up
259 // to the OS to decide whether it prefers ACPI over DT.
260 //
261 Status = gBS->InstallConfigurationTable (&gFdtTableGuid, DeviceTreeBase);
262 ASSERT_EFI_ERROR (Status);
263 }
264
265 return gBS->InstallProtocolInterface (&ImageHandle, &gFdtClientProtocolGuid,
266 EFI_NATIVE_INTERFACE, &mFdtClientProtocol);
267 }