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