1
2
3#include <linux/efi.h>
4#include <asm/efi.h>
5
6#include "efistub.h"
7
8static inline bool mmap_has_headroom(unsigned long buff_size,
9 unsigned long map_size,
10 unsigned long desc_size)
11{
12 unsigned long slack = buff_size - map_size;
13
14 return slack / desc_size >= EFI_MMAP_NR_SLACK_SLOTS;
15}
16
17
18
19
20
21
22
23
24
25
26efi_status_t efi_get_memory_map(struct efi_boot_memmap *map)
27{
28 efi_memory_desc_t *m = NULL;
29 efi_status_t status;
30 unsigned long key;
31 u32 desc_version;
32
33 *map->desc_size = sizeof(*m);
34 *map->map_size = *map->desc_size * 32;
35 *map->buff_size = *map->map_size;
36again:
37 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA,
38 *map->map_size, (void **)&m);
39 if (status != EFI_SUCCESS)
40 goto fail;
41
42 *map->desc_size = 0;
43 key = 0;
44 status = efi_bs_call(get_memory_map, map->map_size, m,
45 &key, map->desc_size, &desc_version);
46 if (status == EFI_BUFFER_TOO_SMALL ||
47 !mmap_has_headroom(*map->buff_size, *map->map_size,
48 *map->desc_size)) {
49 efi_bs_call(free_pool, m);
50
51
52
53
54
55
56
57 *map->map_size += *map->desc_size * EFI_MMAP_NR_SLACK_SLOTS;
58 *map->buff_size = *map->map_size;
59 goto again;
60 }
61
62 if (status == EFI_SUCCESS) {
63 if (map->key_ptr)
64 *map->key_ptr = key;
65 if (map->desc_ver)
66 *map->desc_ver = desc_version;
67 } else {
68 efi_bs_call(free_pool, m);
69 }
70
71fail:
72 *map->map = m;
73 return status;
74}
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91efi_status_t efi_allocate_pages(unsigned long size, unsigned long *addr,
92 unsigned long max)
93{
94 efi_physical_addr_t alloc_addr;
95 efi_status_t status;
96
97 if (EFI_ALLOC_ALIGN > EFI_PAGE_SIZE)
98 return efi_allocate_pages_aligned(size, addr, max,
99 EFI_ALLOC_ALIGN);
100
101 alloc_addr = ALIGN_DOWN(max + 1, EFI_ALLOC_ALIGN) - 1;
102 status = efi_bs_call(allocate_pages, EFI_ALLOCATE_MAX_ADDRESS,
103 EFI_LOADER_DATA, DIV_ROUND_UP(size, EFI_PAGE_SIZE),
104 &alloc_addr);
105 if (status != EFI_SUCCESS)
106 return status;
107
108 *addr = alloc_addr;
109 return EFI_SUCCESS;
110}
111
112
113
114
115
116
117
118
119
120
121
122
123void efi_free(unsigned long size, unsigned long addr)
124{
125 unsigned long nr_pages;
126
127 if (!size)
128 return;
129
130 nr_pages = round_up(size, EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE;
131 efi_bs_call(free_pages, addr, nr_pages);
132}
133