1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
38static struct efi_mem_range fake_mems[EFI_MAX_FAKEMEM];
39static int nr_fake_mem;
40
41static int __init cmp_fake_mem(const void *x1, const void *x2)
42{
43 const struct efi_mem_range *m1 = x1;
44 const struct efi_mem_range *m2 = x2;
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
53void __init efi_fake_memmap(void)
54{
55 int new_nr_map = efi.memmap.nr_map;
56 efi_memory_desc_t *md;
57 phys_addr_t new_memmap_phy;
58 void *new_memmap;
59 int i;
60
61 if (!nr_fake_mem)
62 return;
63
64
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 }
72
73
74 new_memmap_phy = efi_memmap_alloc(new_nr_map);
75 if (!new_memmap_phy)
76 return;
77
78
79 new_memmap = early_memremap(new_memmap_phy,
80 efi.memmap.desc_size * new_nr_map);
81 if (!new_memmap) {
82 memblock_free(new_memmap_phy, efi.memmap.desc_size * new_nr_map);
83 return;
84 }
85
86 for (i = 0; i < nr_fake_mem; i++)
87 efi_memmap_insert(&efi.memmap, new_memmap, &fake_mems[i]);
88
89
90 early_memunmap(new_memmap, efi.memmap.desc_size * new_nr_map);
91
92 efi_memmap_install(new_memmap_phy, new_nr_map);
93
94
95 efi_print_memmap();
96}
97
98static int __init setup_fake_mem(char *p)
99{
100 u64 start = 0, mem_size = 0, attribute = 0;
101 int i;
102
103 if (!p)
104 return -EINVAL;
105
106 while (*p != '\0') {
107 mem_size = memparse(p, &p);
108 if (*p == '@')
109 start = memparse(p+1, &p);
110 else
111 break;
112
113 if (*p == ':')
114 attribute = simple_strtoull(p+1, &p, 0);
115 else
116 break;
117
118 if (nr_fake_mem >= EFI_MAX_FAKEMEM)
119 break;
120
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;
124 nr_fake_mem++;
125
126 if (*p == ',')
127 p++;
128 }
129
130 sort(fake_mems, nr_fake_mem, sizeof(struct efi_mem_range),
131 cmp_fake_mem, NULL);
132
133 for (i = 0; i < nr_fake_mem; i++)
134 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);
137
138 return *p == '\0' ? 0 : -EINVAL;
139}
140
141early_param("efi_fake_mem", setup_fake_mem);
142