FdtClientDxe.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /** @file
  2. * FDT client driver
  3. *
  4. * Copyright (c) 2016, Linaro Ltd. All rights reserved.<BR>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause-Patent
  7. *
  8. **/
  9. #include <Library/BaseLib.h>
  10. #include <Library/DebugLib.h>
  11. #include <Library/UefiDriverEntryPoint.h>
  12. #include <Library/UefiBootServicesTableLib.h>
  13. #include <Library/HobLib.h>
  14. #include <libfdt.h>
  15. #include <Guid/Fdt.h>
  16. #include <Guid/FdtHob.h>
  17. #include <Guid/PlatformHasDeviceTree.h>
  18. #include <Protocol/FdtClient.h>
  19. STATIC VOID *mDeviceTreeBase;
  20. STATIC
  21. EFI_STATUS
  22. EFIAPI
  23. GetNodeProperty (
  24. IN FDT_CLIENT_PROTOCOL *This,
  25. IN INT32 Node,
  26. IN CONST CHAR8 *PropertyName,
  27. OUT CONST VOID **Prop,
  28. OUT UINT32 *PropSize OPTIONAL
  29. )
  30. {
  31. INT32 Len;
  32. ASSERT (mDeviceTreeBase != NULL);
  33. ASSERT (Prop != NULL);
  34. *Prop = fdt_getprop (mDeviceTreeBase, Node, PropertyName, &Len);
  35. if (*Prop == NULL) {
  36. return EFI_NOT_FOUND;
  37. }
  38. if (PropSize != NULL) {
  39. *PropSize = Len;
  40. }
  41. return EFI_SUCCESS;
  42. }
  43. STATIC
  44. EFI_STATUS
  45. EFIAPI
  46. SetNodeProperty (
  47. IN FDT_CLIENT_PROTOCOL *This,
  48. IN INT32 Node,
  49. IN CONST CHAR8 *PropertyName,
  50. IN CONST VOID *Prop,
  51. IN UINT32 PropSize
  52. )
  53. {
  54. INT32 Ret;
  55. ASSERT (mDeviceTreeBase != NULL);
  56. Ret = fdt_setprop (mDeviceTreeBase, Node, PropertyName, Prop, PropSize);
  57. if (Ret != 0) {
  58. return EFI_DEVICE_ERROR;
  59. }
  60. return EFI_SUCCESS;
  61. }
  62. STATIC
  63. BOOLEAN
  64. IsNodeEnabled (
  65. INT32 Node
  66. )
  67. {
  68. CONST CHAR8 *NodeStatus;
  69. INT32 Len;
  70. //
  71. // A missing status property implies 'ok' so ignore any errors that
  72. // may occur here. If the status property is present, check whether
  73. // it is set to 'ok' or 'okay', anything else is treated as 'disabled'.
  74. //
  75. NodeStatus = fdt_getprop (mDeviceTreeBase, Node, "status", &Len);
  76. if (NodeStatus == NULL) {
  77. return TRUE;
  78. }
  79. if (Len >= 5 && AsciiStrCmp (NodeStatus, "okay") == 0) {
  80. return TRUE;
  81. }
  82. if (Len >= 3 && AsciiStrCmp (NodeStatus, "ok") == 0) {
  83. return TRUE;
  84. }
  85. return FALSE;
  86. }
  87. STATIC
  88. EFI_STATUS
  89. EFIAPI
  90. FindNextCompatibleNode (
  91. IN FDT_CLIENT_PROTOCOL *This,
  92. IN CONST CHAR8 *CompatibleString,
  93. IN INT32 PrevNode,
  94. OUT INT32 *Node
  95. )
  96. {
  97. INT32 Prev, Next;
  98. CONST CHAR8 *Type, *Compatible;
  99. INT32 Len;
  100. ASSERT (mDeviceTreeBase != NULL);
  101. ASSERT (Node != NULL);
  102. for (Prev = PrevNode;; Prev = Next) {
  103. Next = fdt_next_node (mDeviceTreeBase, Prev, NULL);
  104. if (Next < 0) {
  105. break;
  106. }
  107. if (!IsNodeEnabled (Next)) {
  108. continue;
  109. }
  110. Type = fdt_getprop (mDeviceTreeBase, Next, "compatible", &Len);
  111. if (Type == NULL) {
  112. continue;
  113. }
  114. //
  115. // A 'compatible' node may contain a sequence of NUL terminated
  116. // compatible strings so check each one
  117. //
  118. for (Compatible = Type; Compatible < Type + Len && *Compatible;
  119. Compatible += 1 + AsciiStrLen (Compatible)) {
  120. if (AsciiStrCmp (CompatibleString, Compatible) == 0) {
  121. *Node = Next;
  122. return EFI_SUCCESS;
  123. }
  124. }
  125. }
  126. return EFI_NOT_FOUND;
  127. }
  128. STATIC
  129. EFI_STATUS
  130. EFIAPI
  131. FindCompatibleNode (
  132. IN FDT_CLIENT_PROTOCOL *This,
  133. IN CONST CHAR8 *CompatibleString,
  134. OUT INT32 *Node
  135. )
  136. {
  137. return FindNextCompatibleNode (This, CompatibleString, 0, Node);
  138. }
  139. STATIC
  140. EFI_STATUS
  141. EFIAPI
  142. FindCompatibleNodeProperty (
  143. IN FDT_CLIENT_PROTOCOL *This,
  144. IN CONST CHAR8 *CompatibleString,
  145. IN CONST CHAR8 *PropertyName,
  146. OUT CONST VOID **Prop,
  147. OUT UINT32 *PropSize OPTIONAL
  148. )
  149. {
  150. EFI_STATUS Status;
  151. INT32 Node;
  152. Status = FindCompatibleNode (This, CompatibleString, &Node);
  153. if (EFI_ERROR (Status)) {
  154. return Status;
  155. }
  156. return GetNodeProperty (This, Node, PropertyName, Prop, PropSize);
  157. }
  158. STATIC
  159. EFI_STATUS
  160. EFIAPI
  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,
  167. OUT UINT32 *RegSize
  168. )
  169. {
  170. EFI_STATUS Status;
  171. ASSERT (RegSize != NULL);
  172. //
  173. // Get the 'reg' property of this node. For now, we will assume
  174. // 8 byte quantities for base and size, respectively.
  175. // TODO use #cells root properties instead
  176. //
  177. Status = FindCompatibleNodeProperty (This, CompatibleString, "reg", Reg,
  178. RegSize);
  179. if (EFI_ERROR (Status)) {
  180. return Status;
  181. }
  182. if ((*RegSize % 16) != 0) {
  183. DEBUG ((EFI_D_ERROR,
  184. "%a: '%a' compatible node has invalid 'reg' property (size == 0x%x)\n",
  185. __FUNCTION__, CompatibleString, *RegSize));
  186. return EFI_NOT_FOUND;
  187. }
  188. *AddressCells = 2;
  189. *SizeCells = 2;
  190. return EFI_SUCCESS;
  191. }
  192. STATIC
  193. EFI_STATUS
  194. EFIAPI
  195. FindNextMemoryNodeReg (
  196. IN FDT_CLIENT_PROTOCOL *This,
  197. IN INT32 PrevNode,
  198. OUT INT32 *Node,
  199. OUT CONST VOID **Reg,
  200. OUT UINTN *AddressCells,
  201. OUT UINTN *SizeCells,
  202. OUT UINT32 *RegSize
  203. )
  204. {
  205. INT32 Prev, Next;
  206. CONST CHAR8 *DeviceType;
  207. INT32 Len;
  208. EFI_STATUS Status;
  209. ASSERT (mDeviceTreeBase != NULL);
  210. ASSERT (Node != NULL);
  211. for (Prev = PrevNode;; Prev = Next) {
  212. Next = fdt_next_node (mDeviceTreeBase, Prev, NULL);
  213. if (Next < 0) {
  214. break;
  215. }
  216. if (!IsNodeEnabled (Next)) {
  217. DEBUG ((DEBUG_WARN, "%a: ignoring disabled memory node\n", __FUNCTION__));
  218. continue;
  219. }
  220. DeviceType = fdt_getprop (mDeviceTreeBase, Next, "device_type", &Len);
  221. if (DeviceType != NULL && AsciiStrCmp (DeviceType, "memory") == 0) {
  222. //
  223. // Get the 'reg' property of this memory node. For now, we will assume
  224. // 8 byte quantities for base and size, respectively.
  225. // TODO use #cells root properties instead
  226. //
  227. Status = GetNodeProperty (This, Next, "reg", Reg, RegSize);
  228. if (EFI_ERROR (Status)) {
  229. DEBUG ((EFI_D_WARN,
  230. "%a: ignoring memory node with no 'reg' property\n",
  231. __FUNCTION__));
  232. continue;
  233. }
  234. if ((*RegSize % 16) != 0) {
  235. DEBUG ((EFI_D_WARN,
  236. "%a: ignoring memory node with invalid 'reg' property (size == 0x%x)\n",
  237. __FUNCTION__, *RegSize));
  238. continue;
  239. }
  240. *Node = Next;
  241. *AddressCells = 2;
  242. *SizeCells = 2;
  243. return EFI_SUCCESS;
  244. }
  245. }
  246. return EFI_NOT_FOUND;
  247. }
  248. STATIC
  249. EFI_STATUS
  250. EFIAPI
  251. FindMemoryNodeReg (
  252. IN FDT_CLIENT_PROTOCOL *This,
  253. OUT INT32 *Node,
  254. OUT CONST VOID **Reg,
  255. OUT UINTN *AddressCells,
  256. OUT UINTN *SizeCells,
  257. OUT UINT32 *RegSize
  258. )
  259. {
  260. return FindNextMemoryNodeReg (This, 0, Node, Reg, AddressCells, SizeCells,
  261. RegSize);
  262. }
  263. STATIC
  264. EFI_STATUS
  265. EFIAPI
  266. GetOrInsertChosenNode (
  267. IN FDT_CLIENT_PROTOCOL *This,
  268. OUT INT32 *Node
  269. )
  270. {
  271. INT32 NewNode;
  272. ASSERT (mDeviceTreeBase != NULL);
  273. ASSERT (Node != NULL);
  274. NewNode = fdt_path_offset (mDeviceTreeBase, "/chosen");
  275. if (NewNode < 0) {
  276. NewNode = fdt_add_subnode (mDeviceTreeBase, 0, "/chosen");
  277. }
  278. if (NewNode < 0) {
  279. return EFI_OUT_OF_RESOURCES;
  280. }
  281. *Node = NewNode;
  282. return EFI_SUCCESS;
  283. }
  284. STATIC FDT_CLIENT_PROTOCOL mFdtClientProtocol = {
  285. GetNodeProperty,
  286. SetNodeProperty,
  287. FindCompatibleNode,
  288. FindNextCompatibleNode,
  289. FindCompatibleNodeProperty,
  290. FindCompatibleNodeReg,
  291. FindMemoryNodeReg,
  292. FindNextMemoryNodeReg,
  293. GetOrInsertChosenNode,
  294. };
  295. STATIC
  296. VOID
  297. EFIAPI
  298. OnPlatformHasDeviceTree (
  299. IN EFI_EVENT Event,
  300. IN VOID *Context
  301. )
  302. {
  303. EFI_STATUS Status;
  304. VOID *Interface;
  305. VOID *DeviceTreeBase;
  306. Status = gBS->LocateProtocol (
  307. &gEdkiiPlatformHasDeviceTreeGuid,
  308. NULL, // Registration
  309. &Interface
  310. );
  311. if (EFI_ERROR (Status)) {
  312. return;
  313. }
  314. DeviceTreeBase = Context;
  315. DEBUG ((
  316. DEBUG_INFO,
  317. "%a: exposing DTB @ 0x%p to OS\n",
  318. __FUNCTION__,
  319. DeviceTreeBase
  320. ));
  321. Status = gBS->InstallConfigurationTable (&gFdtTableGuid, DeviceTreeBase);
  322. ASSERT_EFI_ERROR (Status);
  323. gBS->CloseEvent (Event);
  324. }
  325. EFI_STATUS
  326. EFIAPI
  327. InitializeFdtClientDxe (
  328. IN EFI_HANDLE ImageHandle,
  329. IN EFI_SYSTEM_TABLE *SystemTable
  330. )
  331. {
  332. VOID *Hob;
  333. VOID *DeviceTreeBase;
  334. EFI_STATUS Status;
  335. EFI_EVENT PlatformHasDeviceTreeEvent;
  336. VOID *Registration;
  337. Hob = GetFirstGuidHob (&gFdtHobGuid);
  338. if (Hob == NULL || GET_GUID_HOB_DATA_SIZE (Hob) != sizeof (UINT64)) {
  339. return EFI_NOT_FOUND;
  340. }
  341. DeviceTreeBase = (VOID *)(UINTN)*(UINT64 *)GET_GUID_HOB_DATA (Hob);
  342. if (fdt_check_header (DeviceTreeBase) != 0) {
  343. DEBUG ((EFI_D_ERROR, "%a: No DTB found @ 0x%p\n", __FUNCTION__,
  344. DeviceTreeBase));
  345. return EFI_NOT_FOUND;
  346. }
  347. mDeviceTreeBase = DeviceTreeBase;
  348. DEBUG ((EFI_D_INFO, "%a: DTB @ 0x%p\n", __FUNCTION__, mDeviceTreeBase));
  349. //
  350. // Register a protocol notify for the EDKII Platform Has Device Tree
  351. // Protocol.
  352. //
  353. Status = gBS->CreateEvent (
  354. EVT_NOTIFY_SIGNAL,
  355. TPL_CALLBACK,
  356. OnPlatformHasDeviceTree,
  357. DeviceTreeBase, // Context
  358. &PlatformHasDeviceTreeEvent
  359. );
  360. if (EFI_ERROR (Status)) {
  361. DEBUG ((DEBUG_ERROR, "%a: CreateEvent(): %r\n", __FUNCTION__, Status));
  362. return Status;
  363. }
  364. Status = gBS->RegisterProtocolNotify (
  365. &gEdkiiPlatformHasDeviceTreeGuid,
  366. PlatformHasDeviceTreeEvent,
  367. &Registration
  368. );
  369. if (EFI_ERROR (Status)) {
  370. DEBUG ((
  371. DEBUG_ERROR,
  372. "%a: RegisterProtocolNotify(): %r\n",
  373. __FUNCTION__,
  374. Status
  375. ));
  376. goto CloseEvent;
  377. }
  378. //
  379. // Kick the event; the protocol could be available already.
  380. //
  381. Status = gBS->SignalEvent (PlatformHasDeviceTreeEvent);
  382. if (EFI_ERROR (Status)) {
  383. DEBUG ((DEBUG_ERROR, "%a: SignalEvent(): %r\n", __FUNCTION__, Status));
  384. goto CloseEvent;
  385. }
  386. Status = gBS->InstallProtocolInterface (
  387. &ImageHandle,
  388. &gFdtClientProtocolGuid,
  389. EFI_NATIVE_INTERFACE,
  390. &mFdtClientProtocol
  391. );
  392. if (EFI_ERROR (Status)) {
  393. DEBUG ((
  394. DEBUG_ERROR,
  395. "%a: InstallProtocolInterface(): %r\n",
  396. __FUNCTION__,
  397. Status
  398. ));
  399. goto CloseEvent;
  400. }
  401. return Status;
  402. CloseEvent:
  403. gBS->CloseEvent (PlatformHasDeviceTreeEvent);
  404. return Status;
  405. }