efi_dt_fixup.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * EFI_DT_FIXUP_PROTOCOL
  4. *
  5. * Copyright (c) 2020 Heinrich Schuchardt
  6. */
  7. #include <common.h>
  8. #include <efi_dt_fixup.h>
  9. #include <efi_loader.h>
  10. #include <fdtdec.h>
  11. #include <mapmem.h>
  12. const efi_guid_t efi_guid_dt_fixup_protocol = EFI_DT_FIXUP_PROTOCOL_GUID;
  13. /**
  14. * efi_reserve_memory() - add reserved memory to memory map
  15. *
  16. * @addr: start address of the reserved memory range
  17. * @size: size of the reserved memory range
  18. * @nomap: indicates that the memory range shall not be accessed by the
  19. * UEFI payload
  20. */
  21. static void efi_reserve_memory(u64 addr, u64 size, bool nomap)
  22. {
  23. int type;
  24. efi_uintn_t ret;
  25. /* Convert from sandbox address space. */
  26. addr = (uintptr_t)map_sysmem(addr, 0);
  27. if (nomap)
  28. type = EFI_RESERVED_MEMORY_TYPE;
  29. else
  30. type = EFI_BOOT_SERVICES_DATA;
  31. ret = efi_add_memory_map(addr, size, type);
  32. if (ret != EFI_SUCCESS)
  33. log_err("Reserved memory mapping failed addr %llx size %llx\n",
  34. addr, size);
  35. }
  36. /**
  37. * efi_carve_out_dt_rsv() - Carve out DT reserved memory ranges
  38. *
  39. * The mem_rsv entries of the FDT are added to the memory map. Any failures are
  40. * ignored because this is not critical and we would rather continue to try to
  41. * boot.
  42. *
  43. * @fdt: Pointer to device tree
  44. */
  45. void efi_carve_out_dt_rsv(void *fdt)
  46. {
  47. int nr_rsv, i;
  48. u64 addr, size;
  49. int nodeoffset, subnode;
  50. nr_rsv = fdt_num_mem_rsv(fdt);
  51. /* Look for an existing entry and add it to the efi mem map. */
  52. for (i = 0; i < nr_rsv; i++) {
  53. if (fdt_get_mem_rsv(fdt, i, &addr, &size) != 0)
  54. continue;
  55. efi_reserve_memory(addr, size, true);
  56. }
  57. /* process reserved-memory */
  58. nodeoffset = fdt_subnode_offset(fdt, 0, "reserved-memory");
  59. if (nodeoffset >= 0) {
  60. subnode = fdt_first_subnode(fdt, nodeoffset);
  61. while (subnode >= 0) {
  62. fdt_addr_t fdt_addr;
  63. fdt_size_t fdt_size;
  64. /* check if this subnode has a reg property */
  65. fdt_addr = fdtdec_get_addr_size_auto_parent(
  66. fdt, nodeoffset, subnode,
  67. "reg", 0, &fdt_size, false);
  68. /*
  69. * The /reserved-memory node may have children with
  70. * a size instead of a reg property.
  71. */
  72. if (fdt_addr != FDT_ADDR_T_NONE &&
  73. fdtdec_get_is_enabled(fdt, subnode)) {
  74. bool nomap;
  75. nomap = !!fdt_getprop(fdt, subnode, "no-map",
  76. NULL);
  77. efi_reserve_memory(fdt_addr, fdt_size, nomap);
  78. }
  79. subnode = fdt_next_subnode(fdt, subnode);
  80. }
  81. }
  82. }
  83. /**
  84. * efi_dt_fixup() - fix up device tree
  85. *
  86. * This function implements the Fixup() service of the
  87. * EFI Device Tree Fixup Protocol.
  88. *
  89. * @this: instance of the protocol
  90. * @dtb: device tree provided by caller
  91. * @buffer_size: size of buffer for the device tree including free space
  92. * @flags: bit field designating action to be performed
  93. * Return: status code
  94. */
  95. static efi_status_t __maybe_unused EFIAPI
  96. efi_dt_fixup(struct efi_dt_fixup_protocol *this, void *dtb,
  97. efi_uintn_t *buffer_size, u32 flags)
  98. {
  99. efi_status_t ret;
  100. size_t required_size;
  101. size_t total_size;
  102. bootm_headers_t img = { 0 };
  103. EFI_ENTRY("%p, %p, %p, %d", this, dtb, buffer_size, flags);
  104. if (this != &efi_dt_fixup_prot || !dtb || !buffer_size ||
  105. !flags || (flags & ~EFI_DT_ALL)) {
  106. ret = EFI_INVALID_PARAMETER;
  107. goto out;
  108. }
  109. if (fdt_check_header(dtb)) {
  110. ret = EFI_INVALID_PARAMETER;
  111. goto out;
  112. }
  113. if (flags & EFI_DT_APPLY_FIXUPS) {
  114. /* Check size */
  115. required_size = fdt_off_dt_strings(dtb) +
  116. fdt_size_dt_strings(dtb) +
  117. 0x3000;
  118. total_size = fdt_totalsize(dtb);
  119. if (required_size < total_size)
  120. required_size = total_size;
  121. if (required_size > *buffer_size) {
  122. *buffer_size = required_size;
  123. ret = EFI_BUFFER_TOO_SMALL;
  124. goto out;
  125. }
  126. fdt_set_totalsize(dtb, *buffer_size);
  127. if (image_setup_libfdt(&img, dtb, 0, NULL)) {
  128. log_err("failed to process device tree\n");
  129. ret = EFI_INVALID_PARAMETER;
  130. goto out;
  131. }
  132. }
  133. if (flags & EFI_DT_RESERVE_MEMORY)
  134. efi_carve_out_dt_rsv(dtb);
  135. if (flags & EFI_DT_INSTALL_TABLE) {
  136. ret = efi_install_configuration_table(&efi_guid_fdt, dtb);
  137. if (ret != EFI_SUCCESS) {
  138. log_err("failed to install device tree\n");
  139. goto out;
  140. }
  141. }
  142. ret = EFI_SUCCESS;
  143. out:
  144. return EFI_EXIT(ret);
  145. }
  146. struct efi_dt_fixup_protocol efi_dt_fixup_prot = {
  147. .revision = EFI_DT_FIXUP_PROTOCOL_REVISION,
  148. .fixup = efi_dt_fixup
  149. };