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; |
Heinrich Schuchardt | c57840e | 2021-02-02 07:27:42 +0100 | [diff] [blame^] | 113 | size_t total_size; |
Heinrich Schuchardt | 2c1e224 | 2020-12-13 10:30:24 +0100 | [diff] [blame] | 114 | bootm_headers_t img = { 0 }; |
| 115 | |
| 116 | EFI_ENTRY("%p, %p, %p, %d", this, dtb, buffer_size, flags); |
| 117 | |
| 118 | if (this != &efi_dt_fixup_prot || !dtb || !buffer_size || |
| 119 | !flags || (flags & ~EFI_DT_ALL)) { |
| 120 | ret = EFI_INVALID_PARAMETER; |
| 121 | goto out; |
| 122 | } |
| 123 | if (fdt_check_header(dtb)) { |
| 124 | ret = EFI_INVALID_PARAMETER; |
| 125 | goto out; |
| 126 | } |
| 127 | if (flags & EFI_DT_APPLY_FIXUPS) { |
Heinrich Schuchardt | c57840e | 2021-02-02 07:27:42 +0100 | [diff] [blame^] | 128 | /* Check size */ |
Heinrich Schuchardt | 2c1e224 | 2020-12-13 10:30:24 +0100 | [diff] [blame] | 129 | required_size = fdt_off_dt_strings(dtb) + |
| 130 | fdt_size_dt_strings(dtb) + |
| 131 | 0x3000; |
Heinrich Schuchardt | c57840e | 2021-02-02 07:27:42 +0100 | [diff] [blame^] | 132 | total_size = fdt_totalsize(dtb); |
| 133 | if (required_size < total_size) |
| 134 | required_size = total_size; |
| 135 | if (required_size > *buffer_size) { |
| 136 | *buffer_size = required_size; |
| 137 | ret = EFI_BUFFER_TOO_SMALL; |
| 138 | goto out; |
| 139 | } |
Heinrich Schuchardt | 2c1e224 | 2020-12-13 10:30:24 +0100 | [diff] [blame] | 140 | |
Heinrich Schuchardt | c57840e | 2021-02-02 07:27:42 +0100 | [diff] [blame^] | 141 | fdt_set_totalsize(dtb, *buffer_size); |
Heinrich Schuchardt | 2c1e224 | 2020-12-13 10:30:24 +0100 | [diff] [blame] | 142 | if (image_setup_libfdt(&img, dtb, 0, NULL)) { |
| 143 | log_err("failed to process device tree\n"); |
| 144 | ret = EFI_INVALID_PARAMETER; |
| 145 | goto out; |
| 146 | } |
| 147 | } |
| 148 | if (flags & EFI_DT_RESERVE_MEMORY) |
| 149 | efi_carve_out_dt_rsv(dtb); |
| 150 | |
Heinrich Schuchardt | c57840e | 2021-02-02 07:27:42 +0100 | [diff] [blame^] | 151 | if (flags & EFI_DT_INSTALL_TABLE) { |
Heinrich Schuchardt | 2c1e224 | 2020-12-13 10:30:24 +0100 | [diff] [blame] | 152 | ret = efi_install_configuration_table(&efi_guid_fdt, dtb); |
| 153 | if (ret != EFI_SUCCESS) { |
Heinrich Schuchardt | c57840e | 2021-02-02 07:27:42 +0100 | [diff] [blame^] | 154 | log_err("failed to install device tree\n"); |
Heinrich Schuchardt | 2c1e224 | 2020-12-13 10:30:24 +0100 | [diff] [blame] | 155 | goto out; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | ret = EFI_SUCCESS; |
| 160 | out: |
| 161 | return EFI_EXIT(ret); |
| 162 | } |
Heinrich Schuchardt | 20d48db | 2021-01-16 08:50:10 +0100 | [diff] [blame] | 163 | |
| 164 | struct efi_dt_fixup_protocol efi_dt_fixup_prot = { |
| 165 | .revision = EFI_DT_FIXUP_PROTOCOL_REVISION, |
| 166 | .fixup = efi_dt_fixup |
| 167 | }; |