Taku Izumi | 0f96a99 | 2015-09-30 23:01:56 +0900 | [diff] [blame] | 1 | /* |
| 2 | * fake_mem.c |
| 3 | * |
| 4 | * Copyright (C) 2015 FUJITSU LIMITED |
| 5 | * Author: Taku Izumi <izumi.taku@jp.fujitsu.com> |
| 6 | * |
| 7 | * This code introduces new boot option named "efi_fake_mem" |
| 8 | * By specifying this parameter, you can add arbitrary attribute to |
| 9 | * specific memory range by updating original (firmware provided) EFI |
| 10 | * 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 | */ |
| 27 | |
| 28 | #include <linux/kernel.h> |
| 29 | #include <linux/efi.h> |
| 30 | #include <linux/init.h> |
| 31 | #include <linux/memblock.h> |
| 32 | #include <linux/types.h> |
| 33 | #include <linux/sort.h> |
| 34 | #include <asm/efi.h> |
| 35 | |
| 36 | #define EFI_MAX_FAKEMEM CONFIG_EFI_MAX_FAKE_MEM |
| 37 | |
Matt Fleming | 60863c0 | 2016-02-29 20:30:39 +0000 | [diff] [blame] | 38 | static struct efi_mem_range fake_mems[EFI_MAX_FAKEMEM]; |
Taku Izumi | 0f96a99 | 2015-09-30 23:01:56 +0900 | [diff] [blame] | 39 | static int nr_fake_mem; |
| 40 | |
| 41 | static int __init cmp_fake_mem(const void *x1, const void *x2) |
| 42 | { |
Matt Fleming | 60863c0 | 2016-02-29 20:30:39 +0000 | [diff] [blame] | 43 | const struct efi_mem_range *m1 = x1; |
| 44 | const struct efi_mem_range *m2 = x2; |
Taku Izumi | 0f96a99 | 2015-09-30 23:01:56 +0900 | [diff] [blame] | 45 | |
| 46 | if (m1->range.start < m2->range.start) |
| 47 | return -1; |
| 48 | if (m1->range.start > m2->range.start) |
| 49 | return 1; |
| 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | void __init efi_fake_memmap(void) |
| 54 | { |
Matt Fleming | 884f4f6 | 2016-04-25 21:06:39 +0100 | [diff] [blame] | 55 | int new_nr_map = efi.memmap.nr_map; |
Taku Izumi | 0f96a99 | 2015-09-30 23:01:56 +0900 | [diff] [blame] | 56 | efi_memory_desc_t *md; |
Taku Izumi | 78b9bc9 | 2015-10-23 11:48:17 +0200 | [diff] [blame] | 57 | phys_addr_t new_memmap_phy; |
Taku Izumi | 0f96a99 | 2015-09-30 23:01:56 +0900 | [diff] [blame] | 58 | void *new_memmap; |
Taku Izumi | 0f96a99 | 2015-09-30 23:01:56 +0900 | [diff] [blame] | 59 | int i; |
| 60 | |
Matt Fleming | 4971531 | 2016-06-21 23:11:38 +0100 | [diff] [blame] | 61 | if (!nr_fake_mem) |
Taku Izumi | 0f96a99 | 2015-09-30 23:01:56 +0900 | [diff] [blame] | 62 | return; |
| 63 | |
| 64 | /* count up the number of EFI memory descriptor */ |
Matt Fleming | c8c1a4c | 2016-02-29 16:58:18 +0000 | [diff] [blame] | 65 | for (i = 0; i < nr_fake_mem; i++) { |
| 66 | for_each_efi_memory_desc(md) { |
| 67 | struct range *r = &fake_mems[i].range; |
Taku Izumi | 0f96a99 | 2015-09-30 23:01:56 +0900 | [diff] [blame] | 68 | |
Matt Fleming | 60863c0 | 2016-02-29 20:30:39 +0000 | [diff] [blame] | 69 | new_nr_map += efi_memmap_split_count(md, r); |
Taku Izumi | 0f96a99 | 2015-09-30 23:01:56 +0900 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | |
| 73 | /* allocate memory for new EFI memmap */ |
Matt Fleming | 884f4f6 | 2016-04-25 21:06:39 +0100 | [diff] [blame] | 74 | new_memmap_phy = memblock_alloc(efi.memmap.desc_size * new_nr_map, |
Taku Izumi | 0f96a99 | 2015-09-30 23:01:56 +0900 | [diff] [blame] | 75 | PAGE_SIZE); |
| 76 | if (!new_memmap_phy) |
| 77 | return; |
| 78 | |
| 79 | /* create new EFI memmap */ |
| 80 | new_memmap = early_memremap(new_memmap_phy, |
Matt Fleming | 884f4f6 | 2016-04-25 21:06:39 +0100 | [diff] [blame] | 81 | efi.memmap.desc_size * new_nr_map); |
Taku Izumi | 0f96a99 | 2015-09-30 23:01:56 +0900 | [diff] [blame] | 82 | if (!new_memmap) { |
Matt Fleming | 884f4f6 | 2016-04-25 21:06:39 +0100 | [diff] [blame] | 83 | memblock_free(new_memmap_phy, efi.memmap.desc_size * new_nr_map); |
Taku Izumi | 0f96a99 | 2015-09-30 23:01:56 +0900 | [diff] [blame] | 84 | return; |
| 85 | } |
| 86 | |
Matt Fleming | c8c1a4c | 2016-02-29 16:58:18 +0000 | [diff] [blame] | 87 | for (i = 0; i < nr_fake_mem; i++) |
Matt Fleming | 60863c0 | 2016-02-29 20:30:39 +0000 | [diff] [blame] | 88 | efi_memmap_insert(&efi.memmap, new_memmap, &fake_mems[i]); |
Taku Izumi | 0f96a99 | 2015-09-30 23:01:56 +0900 | [diff] [blame] | 89 | |
| 90 | /* swap into new EFI memmap */ |
Matt Fleming | 9479c7c | 2016-02-26 21:22:05 +0000 | [diff] [blame] | 91 | early_memunmap(new_memmap, efi.memmap.desc_size * new_nr_map); |
Matt Fleming | 9479c7c | 2016-02-26 21:22:05 +0000 | [diff] [blame] | 92 | |
Matt Fleming | c45f4da | 2016-06-22 16:54:00 +0100 | [diff] [blame] | 93 | efi_memmap_install(new_memmap_phy, new_nr_map); |
Taku Izumi | 0f96a99 | 2015-09-30 23:01:56 +0900 | [diff] [blame] | 94 | |
| 95 | /* print new EFI memmap */ |
| 96 | efi_print_memmap(); |
| 97 | } |
| 98 | |
| 99 | static int __init setup_fake_mem(char *p) |
| 100 | { |
| 101 | u64 start = 0, mem_size = 0, attribute = 0; |
| 102 | int i; |
| 103 | |
| 104 | if (!p) |
| 105 | return -EINVAL; |
| 106 | |
| 107 | while (*p != '\0') { |
| 108 | mem_size = memparse(p, &p); |
| 109 | if (*p == '@') |
| 110 | start = memparse(p+1, &p); |
| 111 | else |
| 112 | break; |
| 113 | |
| 114 | if (*p == ':') |
| 115 | attribute = simple_strtoull(p+1, &p, 0); |
| 116 | else |
| 117 | break; |
| 118 | |
| 119 | if (nr_fake_mem >= EFI_MAX_FAKEMEM) |
| 120 | break; |
| 121 | |
| 122 | fake_mems[nr_fake_mem].range.start = start; |
| 123 | fake_mems[nr_fake_mem].range.end = start + mem_size - 1; |
| 124 | fake_mems[nr_fake_mem].attribute = attribute; |
| 125 | nr_fake_mem++; |
| 126 | |
| 127 | if (*p == ',') |
| 128 | p++; |
| 129 | } |
| 130 | |
Matt Fleming | 60863c0 | 2016-02-29 20:30:39 +0000 | [diff] [blame] | 131 | sort(fake_mems, nr_fake_mem, sizeof(struct efi_mem_range), |
Taku Izumi | 0f96a99 | 2015-09-30 23:01:56 +0900 | [diff] [blame] | 132 | cmp_fake_mem, NULL); |
| 133 | |
| 134 | for (i = 0; i < nr_fake_mem; i++) |
| 135 | pr_info("efi_fake_mem: add attr=0x%016llx to [mem 0x%016llx-0x%016llx]", |
| 136 | fake_mems[i].attribute, fake_mems[i].range.start, |
| 137 | fake_mems[i].range.end); |
| 138 | |
| 139 | return *p == '\0' ? 0 : -EINVAL; |
| 140 | } |
| 141 | |
| 142 | early_param("efi_fake_mem", setup_fake_mem); |