FdtParser.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Copyright (c) 2015, Linaro Ltd. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause-Patent
  5. */
  6. #include <Uefi.h>
  7. #include <Include/libfdt.h>
  8. BOOLEAN
  9. FindMemnode (
  10. IN VOID *DeviceTreeBlob,
  11. OUT UINT64 *SystemMemoryBase,
  12. OUT UINT64 *SystemMemorySize
  13. )
  14. {
  15. INT32 MemoryNode;
  16. INT32 AddressCells;
  17. INT32 SizeCells;
  18. INT32 Length;
  19. CONST INT32 *Prop;
  20. if (fdt_check_header (DeviceTreeBlob) != 0) {
  21. return FALSE;
  22. }
  23. //
  24. // Look for a node called "memory" at the lowest level of the tree
  25. //
  26. MemoryNode = fdt_path_offset (DeviceTreeBlob, "/memory");
  27. if (MemoryNode <= 0) {
  28. return FALSE;
  29. }
  30. //
  31. // Retrieve the #address-cells and #size-cells properties
  32. // from the root node, or use the default if not provided.
  33. //
  34. AddressCells = 1;
  35. SizeCells = 1;
  36. Prop = fdt_getprop (DeviceTreeBlob, 0, "#address-cells", &Length);
  37. if (Length == 4) {
  38. AddressCells = fdt32_to_cpu (*Prop);
  39. }
  40. Prop = fdt_getprop (DeviceTreeBlob, 0, "#size-cells", &Length);
  41. if (Length == 4) {
  42. SizeCells = fdt32_to_cpu (*Prop);
  43. }
  44. //
  45. // Now find the 'reg' property of the /memory node, and read the first
  46. // range listed.
  47. //
  48. Prop = fdt_getprop (DeviceTreeBlob, MemoryNode, "reg", &Length);
  49. if (Length < (AddressCells + SizeCells) * sizeof (INT32)) {
  50. return FALSE;
  51. }
  52. *SystemMemoryBase = fdt32_to_cpu (Prop[0]);
  53. if (AddressCells > 1) {
  54. *SystemMemoryBase = (*SystemMemoryBase << 32) | fdt32_to_cpu (Prop[1]);
  55. }
  56. Prop += AddressCells;
  57. *SystemMemorySize = fdt32_to_cpu (Prop[0]);
  58. if (SizeCells > 1) {
  59. *SystemMemorySize = (*SystemMemorySize << 32) | fdt32_to_cpu (Prop[1]);
  60. }
  61. return TRUE;
  62. }
  63. VOID
  64. CopyFdt (
  65. IN VOID *FdtDest,
  66. IN VOID *FdtSource
  67. )
  68. {
  69. fdt_pack(FdtSource);
  70. CopyMem (FdtDest, FdtSource, fdt_totalsize (FdtSource));
  71. }