FdtDxe.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /** @file
  2. RISC-V Flattened Device Tree DXE module
  3. The Linux booting protocol on RISC-V requires the id of the booting hart to
  4. be passed as a0. Therefore the EFISTUB needs to get this information. Because
  5. it runs in S-Mode, it cannot get this information from mhartid. Instead we
  6. insert the id into the device tree, that the EFIFSTUB can read from the config table.
  7. Copyright (c) 2021, Hewlett Packard Enterprise Development LP. All rights reserved.<BR>
  8. SPDX-License-Identifier: BSD-2-Clause-Patent
  9. **/
  10. #include <Library/UefiBootServicesTableLib.h>
  11. #include <Library/DebugLib.h>
  12. #include <Library/HobLib.h>
  13. #include <Library/BaseMemoryLib.h>
  14. #include <Library/PcdLib.h>
  15. #include <libfdt.h>
  16. /**
  17. Fix up the device tree with booting hartid for the kernel
  18. @param DtbBlob The device tree. Is extended to fit the hart id.
  19. @retval EFI_SUCCESS The device tree was success fixed up with the hart id.
  20. @retval EFI_OUT_OF_RESOURCES There is not enough memory available to complete the operation.
  21. **/
  22. EFI_STATUS
  23. EFIAPI
  24. FixDtb (
  25. IN OUT VOID *DtbBlob,
  26. IN UINTN BootingHartId
  27. )
  28. {
  29. fdt32_t Size;
  30. UINT32 ChosenOffset, Err;
  31. DEBUG ((DEBUG_INFO, "Fixing up device tree with boot hart id: %d\n",
  32. BootingHartId));
  33. Size = fdt_totalsize(DtbBlob);
  34. Err = fdt_open_into(DtbBlob, DtbBlob, Size + 32);
  35. if (Err < 0) {
  36. DEBUG ((DEBUG_ERROR,
  37. "Device Tree can't be expanded to accommodate new node\n", __FUNCTION__));
  38. return EFI_OUT_OF_RESOURCES;
  39. }
  40. ChosenOffset = fdt_path_offset(DtbBlob, "/chosen");
  41. fdt_setprop_u32(DtbBlob, ChosenOffset, "boot-hartid", BootingHartId);
  42. return EFI_SUCCESS;
  43. }
  44. /**
  45. Install the FDT passed in HOB into EFI system configuration table.
  46. @retval EFI_SUCCESS Successfully installed fixed up FDT in config table.
  47. @retval EFI_NOT_FOUND Did not find FDT HOB.
  48. @retval EFI_OUT_OF_RESOURCES There is not enough memory available to complete the operation.
  49. **/
  50. EFI_STATUS
  51. EFIAPI
  52. InstallFdtFromHob (VOID)
  53. {
  54. EFI_STATUS Status;
  55. EFI_HOB_GUID_TYPE *GuidHob;
  56. VOID *DataInHob;
  57. UINTN DataSize;
  58. GuidHob = GetFirstGuidHob (&gFdtHobGuid);
  59. if (GuidHob == NULL) {
  60. DEBUG ((DEBUG_ERROR, "Failed to find RISC-V DTB Hob\n",
  61. __FUNCTION__));
  62. return EFI_NOT_FOUND;
  63. }
  64. DataInHob = (VOID *) *((UINTN *) GET_GUID_HOB_DATA (GuidHob));
  65. DataSize = GET_GUID_HOB_DATA_SIZE (GuidHob);
  66. Status = FixDtb (DataInHob, PcdGet32(PcdBootHartId));
  67. if (EFI_ERROR (Status)) {
  68. return Status;
  69. }
  70. Status = gBS->InstallConfigurationTable (&gFdtTableGuid, DataInHob);
  71. if (EFI_ERROR (Status)) {
  72. DEBUG ((DEBUG_ERROR, "%a: failed to install FDT configuration table\n",
  73. __FUNCTION__));
  74. }
  75. return Status;
  76. }
  77. /**
  78. Install the FDT from the HOB into the EFI system configuration table.
  79. @param ImageHandle Image handle of this driver.
  80. @param SystemTable Pointer to the System Table.
  81. @retval EFI_SUCCESS FDT successfully installed into config table.
  82. @retval EFI_NOT_FOUND Did not find FDT HOB.
  83. @retval EFI_OUT_OF_RESOURCES There is not enough memory available to complete the operation.
  84. **/
  85. EFI_STATUS
  86. EFIAPI
  87. InstallFdt (
  88. IN EFI_HANDLE ImageHandle,
  89. IN EFI_SYSTEM_TABLE *SystemTable
  90. )
  91. {
  92. EFI_STATUS Status;
  93. Status = InstallFdtFromHob ();
  94. return Status;
  95. }