FdtClientDxe.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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. {
  121. if (AsciiStrCmp (CompatibleString, Compatible) == 0) {
  122. *Node = Next;
  123. return EFI_SUCCESS;
  124. }
  125. }
  126. }
  127. return EFI_NOT_FOUND;
  128. }
  129. STATIC
  130. EFI_STATUS
  131. EFIAPI
  132. FindCompatibleNode (
  133. IN FDT_CLIENT_PROTOCOL *This,
  134. IN CONST CHAR8 *CompatibleString,
  135. OUT INT32 *Node
  136. )
  137. {
  138. return FindNextCompatibleNode (This, CompatibleString, 0, Node);
  139. }
  140. STATIC
  141. EFI_STATUS
  142. EFIAPI
  143. FindCompatibleNodeProperty (
  144. IN FDT_CLIENT_PROTOCOL *This,
  145. IN CONST CHAR8 *CompatibleString,
  146. IN CONST CHAR8 *PropertyName,
  147. OUT CONST VOID **Prop,
  148. OUT UINT32 *PropSize OPTIONAL
  149. )
  150. {
  151. EFI_STATUS Status;
  152. INT32 Node;
  153. Status = FindCompatibleNode (This, CompatibleString, &Node);
  154. if (EFI_ERROR (Status)) {
  155. return Status;
  156. }
  157. return GetNodeProperty (This, Node, PropertyName, Prop, PropSize);
  158. }
  159. STATIC
  160. EFI_STATUS
  161. EFIAPI
  162. FindCompatibleNodeReg (
  163. IN FDT_CLIENT_PROTOCOL *This,
  164. IN CONST CHAR8 *CompatibleString,
  165. OUT CONST VOID **Reg,
  166. OUT UINTN *AddressCells,
  167. OUT UINTN *SizeCells,
  168. OUT UINT32 *RegSize
  169. )
  170. {
  171. EFI_STATUS Status;
  172. ASSERT (RegSize != NULL);
  173. //
  174. // Get the 'reg' property of this node. For now, we will assume
  175. // 8 byte quantities for base and size, respectively.
  176. // TODO use #cells root properties instead
  177. //
  178. Status = FindCompatibleNodeProperty (
  179. This,
  180. CompatibleString,
  181. "reg",
  182. Reg,
  183. RegSize
  184. );
  185. if (EFI_ERROR (Status)) {
  186. return Status;
  187. }
  188. if ((*RegSize % 16) != 0) {
  189. DEBUG ((
  190. DEBUG_ERROR,
  191. "%a: '%a' compatible node has invalid 'reg' property (size == 0x%x)\n",
  192. __FUNCTION__,
  193. CompatibleString,
  194. *RegSize
  195. ));
  196. return EFI_NOT_FOUND;
  197. }
  198. *AddressCells = 2;
  199. *SizeCells = 2;
  200. return EFI_SUCCESS;
  201. }
  202. STATIC
  203. EFI_STATUS
  204. EFIAPI
  205. FindNextMemoryNodeReg (
  206. IN FDT_CLIENT_PROTOCOL *This,
  207. IN INT32 PrevNode,
  208. OUT INT32 *Node,
  209. OUT CONST VOID **Reg,
  210. OUT UINTN *AddressCells,
  211. OUT UINTN *SizeCells,
  212. OUT UINT32 *RegSize
  213. )
  214. {
  215. INT32 Prev, Next;
  216. CONST CHAR8 *DeviceType;
  217. INT32 Len;
  218. EFI_STATUS Status;
  219. ASSERT (mDeviceTreeBase != NULL);
  220. ASSERT (Node != NULL);
  221. for (Prev = PrevNode; ; Prev = Next) {
  222. Next = fdt_next_node (mDeviceTreeBase, Prev, NULL);
  223. if (Next < 0) {
  224. break;
  225. }
  226. if (!IsNodeEnabled (Next)) {
  227. DEBUG ((DEBUG_WARN, "%a: ignoring disabled memory node\n", __FUNCTION__));
  228. continue;
  229. }
  230. DeviceType = fdt_getprop (mDeviceTreeBase, Next, "device_type", &Len);
  231. if ((DeviceType != NULL) && (AsciiStrCmp (DeviceType, "memory") == 0)) {
  232. //
  233. // Get the 'reg' property of this memory node. For now, we will assume
  234. // 8 byte quantities for base and size, respectively.
  235. // TODO use #cells root properties instead
  236. //
  237. Status = GetNodeProperty (This, Next, "reg", Reg, RegSize);
  238. if (EFI_ERROR (Status)) {
  239. DEBUG ((
  240. DEBUG_WARN,
  241. "%a: ignoring memory node with no 'reg' property\n",
  242. __FUNCTION__
  243. ));
  244. continue;
  245. }
  246. if ((*RegSize % 16) != 0) {
  247. DEBUG ((
  248. DEBUG_WARN,
  249. "%a: ignoring memory node with invalid 'reg' property (size == 0x%x)\n",
  250. __FUNCTION__,
  251. *RegSize
  252. ));
  253. continue;
  254. }
  255. *Node = Next;
  256. *AddressCells = 2;
  257. *SizeCells = 2;
  258. return EFI_SUCCESS;
  259. }
  260. }
  261. return EFI_NOT_FOUND;
  262. }
  263. STATIC
  264. EFI_STATUS
  265. EFIAPI
  266. FindMemoryNodeReg (
  267. IN FDT_CLIENT_PROTOCOL *This,
  268. OUT INT32 *Node,
  269. OUT CONST VOID **Reg,
  270. OUT UINTN *AddressCells,
  271. OUT UINTN *SizeCells,
  272. OUT UINT32 *RegSize
  273. )
  274. {
  275. return FindNextMemoryNodeReg (
  276. This,
  277. 0,
  278. Node,
  279. Reg,
  280. AddressCells,
  281. SizeCells,
  282. RegSize
  283. );
  284. }
  285. STATIC
  286. EFI_STATUS
  287. EFIAPI
  288. GetOrInsertChosenNode (
  289. IN FDT_CLIENT_PROTOCOL *This,
  290. OUT INT32 *Node
  291. )
  292. {
  293. INT32 NewNode;
  294. ASSERT (mDeviceTreeBase != NULL);
  295. ASSERT (Node != NULL);
  296. NewNode = fdt_path_offset (mDeviceTreeBase, "/chosen");
  297. if (NewNode < 0) {
  298. NewNode = fdt_add_subnode (mDeviceTreeBase, 0, "/chosen");
  299. }
  300. if (NewNode < 0) {
  301. return EFI_OUT_OF_RESOURCES;
  302. }
  303. *Node = NewNode;
  304. return EFI_SUCCESS;
  305. }
  306. STATIC FDT_CLIENT_PROTOCOL mFdtClientProtocol = {
  307. GetNodeProperty,
  308. SetNodeProperty,
  309. FindCompatibleNode,
  310. FindNextCompatibleNode,
  311. FindCompatibleNodeProperty,
  312. FindCompatibleNodeReg,
  313. FindMemoryNodeReg,
  314. FindNextMemoryNodeReg,
  315. GetOrInsertChosenNode,
  316. };
  317. STATIC
  318. VOID
  319. EFIAPI
  320. OnPlatformHasDeviceTree (
  321. IN EFI_EVENT Event,
  322. IN VOID *Context
  323. )
  324. {
  325. EFI_STATUS Status;
  326. VOID *Interface;
  327. VOID *DeviceTreeBase;
  328. Status = gBS->LocateProtocol (
  329. &gEdkiiPlatformHasDeviceTreeGuid,
  330. NULL, // Registration
  331. &Interface
  332. );
  333. if (EFI_ERROR (Status)) {
  334. return;
  335. }
  336. DeviceTreeBase = Context;
  337. DEBUG ((
  338. DEBUG_INFO,
  339. "%a: exposing DTB @ 0x%p to OS\n",
  340. __FUNCTION__,
  341. DeviceTreeBase
  342. ));
  343. Status = gBS->InstallConfigurationTable (&gFdtTableGuid, DeviceTreeBase);
  344. ASSERT_EFI_ERROR (Status);
  345. gBS->CloseEvent (Event);
  346. }
  347. EFI_STATUS
  348. EFIAPI
  349. InitializeFdtClientDxe (
  350. IN EFI_HANDLE ImageHandle,
  351. IN EFI_SYSTEM_TABLE *SystemTable
  352. )
  353. {
  354. VOID *Hob;
  355. VOID *DeviceTreeBase;
  356. EFI_STATUS Status;
  357. EFI_EVENT PlatformHasDeviceTreeEvent;
  358. VOID *Registration;
  359. Hob = GetFirstGuidHob (&gFdtHobGuid);
  360. if ((Hob == NULL) || (GET_GUID_HOB_DATA_SIZE (Hob) != sizeof (UINT64))) {
  361. return EFI_NOT_FOUND;
  362. }
  363. DeviceTreeBase = (VOID *)(UINTN)*(UINT64 *)GET_GUID_HOB_DATA (Hob);
  364. if (fdt_check_header (DeviceTreeBase) != 0) {
  365. DEBUG ((
  366. DEBUG_ERROR,
  367. "%a: No DTB found @ 0x%p\n",
  368. __FUNCTION__,
  369. DeviceTreeBase
  370. ));
  371. return EFI_NOT_FOUND;
  372. }
  373. mDeviceTreeBase = DeviceTreeBase;
  374. DEBUG ((DEBUG_INFO, "%a: DTB @ 0x%p\n", __FUNCTION__, mDeviceTreeBase));
  375. //
  376. // Register a protocol notify for the EDKII Platform Has Device Tree
  377. // Protocol.
  378. //
  379. Status = gBS->CreateEvent (
  380. EVT_NOTIFY_SIGNAL,
  381. TPL_CALLBACK,
  382. OnPlatformHasDeviceTree,
  383. DeviceTreeBase, // Context
  384. &PlatformHasDeviceTreeEvent
  385. );
  386. if (EFI_ERROR (Status)) {
  387. DEBUG ((DEBUG_ERROR, "%a: CreateEvent(): %r\n", __FUNCTION__, Status));
  388. return Status;
  389. }
  390. Status = gBS->RegisterProtocolNotify (
  391. &gEdkiiPlatformHasDeviceTreeGuid,
  392. PlatformHasDeviceTreeEvent,
  393. &Registration
  394. );
  395. if (EFI_ERROR (Status)) {
  396. DEBUG ((
  397. DEBUG_ERROR,
  398. "%a: RegisterProtocolNotify(): %r\n",
  399. __FUNCTION__,
  400. Status
  401. ));
  402. goto CloseEvent;
  403. }
  404. //
  405. // Kick the event; the protocol could be available already.
  406. //
  407. Status = gBS->SignalEvent (PlatformHasDeviceTreeEvent);
  408. if (EFI_ERROR (Status)) {
  409. DEBUG ((DEBUG_ERROR, "%a: SignalEvent(): %r\n", __FUNCTION__, Status));
  410. goto CloseEvent;
  411. }
  412. Status = gBS->InstallProtocolInterface (
  413. &ImageHandle,
  414. &gFdtClientProtocolGuid,
  415. EFI_NATIVE_INTERFACE,
  416. &mFdtClientProtocol
  417. );
  418. if (EFI_ERROR (Status)) {
  419. DEBUG ((
  420. DEBUG_ERROR,
  421. "%a: InstallProtocolInterface(): %r\n",
  422. __FUNCTION__,
  423. Status
  424. ));
  425. goto CloseEvent;
  426. }
  427. return Status;
  428. CloseEvent:
  429. gBS->CloseEvent (PlatformHasDeviceTreeEvent);
  430. return Status;
  431. }