hc
2024-05-13 9d77db3c730780c8ef5ccd4b66403ff5675cfe4e
kernel/drivers/firmware/efi/fake_mem.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0
12 /*
23 * fake_mem.c
34 *
....@@ -8,21 +9,6 @@
89 * By specifying this parameter, you can add arbitrary attribute to
910 * specific memory range by updating original (firmware provided) EFI
1011 * 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".
2612 */
2713
2814 #include <linux/kernel.h>
....@@ -31,12 +17,10 @@
3117 #include <linux/memblock.h>
3218 #include <linux/types.h>
3319 #include <linux/sort.h>
34
-#include <asm/efi.h>
20
+#include "fake_mem.h"
3521
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;
4024
4125 static int __init cmp_fake_mem(const void *x1, const void *x2)
4226 {
....@@ -50,46 +34,45 @@
5034 return 0;
5135 }
5236
53
-void __init efi_fake_memmap(void)
37
+static void __init efi_fake_range(struct efi_mem_range *efi_range)
5438 {
39
+ struct efi_memory_map_data data = { 0 };
5540 int new_nr_map = efi.memmap.nr_map;
5641 efi_memory_desc_t *md;
57
- phys_addr_t new_memmap_phy;
5842 void *new_memmap;
59
- int i;
60
-
61
- if (!nr_fake_mem)
62
- return;
6343
6444 /* 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);
7247
7348 /* 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)
7650 return;
7751
7852 /* 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);
8154 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);
8356 return;
8457 }
8558
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);
8860
8961 /* swap into new EFI memmap */
90
- early_memunmap(new_memmap, efi.memmap.desc_size * new_nr_map);
62
+ early_memunmap(new_memmap, data.size);
9163
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]);
9376
9477 /* print new EFI memmap */
9578 efi_print_memmap();
....@@ -118,22 +101,22 @@
118101 if (nr_fake_mem >= EFI_MAX_FAKEMEM)
119102 break;
120103
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;
124107 nr_fake_mem++;
125108
126109 if (*p == ',')
127110 p++;
128111 }
129112
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),
131114 cmp_fake_mem, NULL);
132115
133116 for (i = 0; i < nr_fake_mem; i++)
134117 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);
137120
138121 return *p == '\0' ? 0 : -EINVAL;
139122 }