| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * fake_mem.c |
|---|
| 3 | 4 | * |
|---|
| .. | .. |
|---|
| 8 | 9 | * By specifying this parameter, you can add arbitrary attribute to |
|---|
| 9 | 10 | * specific memory range by updating original (firmware provided) EFI |
|---|
| 10 | 11 | * memmap. |
|---|
| 11 | | - * |
|---|
| 12 | | - * This program is free software; you can redistribute it and/or modify it |
|---|
| 13 | | - * under the terms and conditions of the GNU General Public License, |
|---|
| 14 | | - * version 2, as published by the Free Software Foundation. |
|---|
| 15 | | - * |
|---|
| 16 | | - * This program is distributed in the hope it will be useful, but WITHOUT |
|---|
| 17 | | - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|---|
| 18 | | - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
|---|
| 19 | | - * more details. |
|---|
| 20 | | - * |
|---|
| 21 | | - * You should have received a copy of the GNU General Public License along with |
|---|
| 22 | | - * this program; if not, see <http://www.gnu.org/licenses/>. |
|---|
| 23 | | - * |
|---|
| 24 | | - * The full GNU General Public License is included in this distribution in |
|---|
| 25 | | - * the file called "COPYING". |
|---|
| 26 | 12 | */ |
|---|
| 27 | 13 | |
|---|
| 28 | 14 | #include <linux/kernel.h> |
|---|
| .. | .. |
|---|
| 31 | 17 | #include <linux/memblock.h> |
|---|
| 32 | 18 | #include <linux/types.h> |
|---|
| 33 | 19 | #include <linux/sort.h> |
|---|
| 34 | | -#include <asm/efi.h> |
|---|
| 20 | +#include "fake_mem.h" |
|---|
| 35 | 21 | |
|---|
| 36 | | -#define EFI_MAX_FAKEMEM CONFIG_EFI_MAX_FAKE_MEM |
|---|
| 37 | | - |
|---|
| 38 | | -static struct efi_mem_range fake_mems[EFI_MAX_FAKEMEM]; |
|---|
| 39 | | -static int nr_fake_mem; |
|---|
| 22 | +struct efi_mem_range efi_fake_mems[EFI_MAX_FAKEMEM]; |
|---|
| 23 | +int nr_fake_mem; |
|---|
| 40 | 24 | |
|---|
| 41 | 25 | static int __init cmp_fake_mem(const void *x1, const void *x2) |
|---|
| 42 | 26 | { |
|---|
| .. | .. |
|---|
| 50 | 34 | return 0; |
|---|
| 51 | 35 | } |
|---|
| 52 | 36 | |
|---|
| 53 | | -void __init efi_fake_memmap(void) |
|---|
| 37 | +static void __init efi_fake_range(struct efi_mem_range *efi_range) |
|---|
| 54 | 38 | { |
|---|
| 39 | + struct efi_memory_map_data data = { 0 }; |
|---|
| 55 | 40 | int new_nr_map = efi.memmap.nr_map; |
|---|
| 56 | 41 | efi_memory_desc_t *md; |
|---|
| 57 | | - phys_addr_t new_memmap_phy; |
|---|
| 58 | 42 | void *new_memmap; |
|---|
| 59 | | - int i; |
|---|
| 60 | | - |
|---|
| 61 | | - if (!nr_fake_mem) |
|---|
| 62 | | - return; |
|---|
| 63 | 43 | |
|---|
| 64 | 44 | /* count up the number of EFI memory descriptor */ |
|---|
| 65 | | - for (i = 0; i < nr_fake_mem; i++) { |
|---|
| 66 | | - for_each_efi_memory_desc(md) { |
|---|
| 67 | | - struct range *r = &fake_mems[i].range; |
|---|
| 68 | | - |
|---|
| 69 | | - new_nr_map += efi_memmap_split_count(md, r); |
|---|
| 70 | | - } |
|---|
| 71 | | - } |
|---|
| 45 | + for_each_efi_memory_desc(md) |
|---|
| 46 | + new_nr_map += efi_memmap_split_count(md, &efi_range->range); |
|---|
| 72 | 47 | |
|---|
| 73 | 48 | /* allocate memory for new EFI memmap */ |
|---|
| 74 | | - new_memmap_phy = efi_memmap_alloc(new_nr_map); |
|---|
| 75 | | - if (!new_memmap_phy) |
|---|
| 49 | + if (efi_memmap_alloc(new_nr_map, &data) != 0) |
|---|
| 76 | 50 | return; |
|---|
| 77 | 51 | |
|---|
| 78 | 52 | /* create new EFI memmap */ |
|---|
| 79 | | - new_memmap = early_memremap(new_memmap_phy, |
|---|
| 80 | | - efi.memmap.desc_size * new_nr_map); |
|---|
| 53 | + new_memmap = early_memremap(data.phys_map, data.size); |
|---|
| 81 | 54 | if (!new_memmap) { |
|---|
| 82 | | - memblock_free(new_memmap_phy, efi.memmap.desc_size * new_nr_map); |
|---|
| 55 | + __efi_memmap_free(data.phys_map, data.size, data.flags); |
|---|
| 83 | 56 | return; |
|---|
| 84 | 57 | } |
|---|
| 85 | 58 | |
|---|
| 86 | | - for (i = 0; i < nr_fake_mem; i++) |
|---|
| 87 | | - efi_memmap_insert(&efi.memmap, new_memmap, &fake_mems[i]); |
|---|
| 59 | + efi_memmap_insert(&efi.memmap, new_memmap, efi_range); |
|---|
| 88 | 60 | |
|---|
| 89 | 61 | /* swap into new EFI memmap */ |
|---|
| 90 | | - early_memunmap(new_memmap, efi.memmap.desc_size * new_nr_map); |
|---|
| 62 | + early_memunmap(new_memmap, data.size); |
|---|
| 91 | 63 | |
|---|
| 92 | | - efi_memmap_install(new_memmap_phy, new_nr_map); |
|---|
| 64 | + efi_memmap_install(&data); |
|---|
| 65 | +} |
|---|
| 66 | + |
|---|
| 67 | +void __init efi_fake_memmap(void) |
|---|
| 68 | +{ |
|---|
| 69 | + int i; |
|---|
| 70 | + |
|---|
| 71 | + if (!efi_enabled(EFI_MEMMAP) || !nr_fake_mem) |
|---|
| 72 | + return; |
|---|
| 73 | + |
|---|
| 74 | + for (i = 0; i < nr_fake_mem; i++) |
|---|
| 75 | + efi_fake_range(&efi_fake_mems[i]); |
|---|
| 93 | 76 | |
|---|
| 94 | 77 | /* print new EFI memmap */ |
|---|
| 95 | 78 | efi_print_memmap(); |
|---|
| .. | .. |
|---|
| 118 | 101 | if (nr_fake_mem >= EFI_MAX_FAKEMEM) |
|---|
| 119 | 102 | break; |
|---|
| 120 | 103 | |
|---|
| 121 | | - fake_mems[nr_fake_mem].range.start = start; |
|---|
| 122 | | - fake_mems[nr_fake_mem].range.end = start + mem_size - 1; |
|---|
| 123 | | - fake_mems[nr_fake_mem].attribute = attribute; |
|---|
| 104 | + efi_fake_mems[nr_fake_mem].range.start = start; |
|---|
| 105 | + efi_fake_mems[nr_fake_mem].range.end = start + mem_size - 1; |
|---|
| 106 | + efi_fake_mems[nr_fake_mem].attribute = attribute; |
|---|
| 124 | 107 | nr_fake_mem++; |
|---|
| 125 | 108 | |
|---|
| 126 | 109 | if (*p == ',') |
|---|
| 127 | 110 | p++; |
|---|
| 128 | 111 | } |
|---|
| 129 | 112 | |
|---|
| 130 | | - sort(fake_mems, nr_fake_mem, sizeof(struct efi_mem_range), |
|---|
| 113 | + sort(efi_fake_mems, nr_fake_mem, sizeof(struct efi_mem_range), |
|---|
| 131 | 114 | cmp_fake_mem, NULL); |
|---|
| 132 | 115 | |
|---|
| 133 | 116 | for (i = 0; i < nr_fake_mem; i++) |
|---|
| 134 | 117 | pr_info("efi_fake_mem: add attr=0x%016llx to [mem 0x%016llx-0x%016llx]", |
|---|
| 135 | | - fake_mems[i].attribute, fake_mems[i].range.start, |
|---|
| 136 | | - fake_mems[i].range.end); |
|---|
| 118 | + efi_fake_mems[i].attribute, efi_fake_mems[i].range.start, |
|---|
| 119 | + efi_fake_mems[i].range.end); |
|---|
| 137 | 120 | |
|---|
| 138 | 121 | return *p == '\0' ? 0 : -EINVAL; |
|---|
| 139 | 122 | } |
|---|