2 * Copyright (C) 2016 Linaro Ltd; <ard.biesheuvel@linaro.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
10 #include <linux/efi.h>
11 #include <linux/log2.h>
16 struct efi_rng_protocol
{
17 efi_status_t (*get_info
)(struct efi_rng_protocol
*,
18 unsigned long *, efi_guid_t
*);
19 efi_status_t (*get_rng
)(struct efi_rng_protocol
*,
20 efi_guid_t
*, unsigned long, u8
*out
);
23 efi_status_t
efi_get_random_bytes(efi_system_table_t
*sys_table_arg
,
24 unsigned long size
, u8
*out
)
26 efi_guid_t rng_proto
= EFI_RNG_PROTOCOL_GUID
;
28 struct efi_rng_protocol
*rng
;
30 status
= efi_call_early(locate_protocol
, &rng_proto
, NULL
,
32 if (status
!= EFI_SUCCESS
)
35 return rng
->get_rng(rng
, NULL
, size
, out
);
39 * Return the number of slots covered by this entry, i.e., the number of
40 * addresses it covers that are suitably aligned and supply enough room
43 static unsigned long get_entry_num_slots(efi_memory_desc_t
*md
,
45 unsigned long align_shift
)
47 unsigned long align
= 1UL << align_shift
;
48 u64 first_slot
, last_slot
, region_end
;
50 if (md
->type
!= EFI_CONVENTIONAL_MEMORY
)
53 region_end
= min((u64
)ULONG_MAX
, md
->phys_addr
+ md
->num_pages
*EFI_PAGE_SIZE
- 1);
55 first_slot
= round_up(md
->phys_addr
, align
);
56 last_slot
= round_down(region_end
- size
+ 1, align
);
58 if (first_slot
> last_slot
)
61 return ((unsigned long)(last_slot
- first_slot
) >> align_shift
) + 1;
65 * The UEFI memory descriptors have a virtual address field that is only used
66 * when installing the virtual mapping using SetVirtualAddressMap(). Since it
67 * is unused here, we can reuse it to keep track of each descriptor's slot
70 #define MD_NUM_SLOTS(md) ((md)->virt_addr)
72 efi_status_t
efi_random_alloc(efi_system_table_t
*sys_table_arg
,
76 unsigned long random_seed
)
78 unsigned long map_size
, desc_size
, total_slots
= 0, target_slot
;
79 unsigned long buff_size
;
81 efi_memory_desc_t
*memory_map
;
83 struct efi_boot_memmap map
;
85 map
.map
= &memory_map
;
86 map
.map_size
= &map_size
;
87 map
.desc_size
= &desc_size
;
90 map
.buff_size
= &buff_size
;
92 status
= efi_get_memory_map(sys_table_arg
, &map
);
93 if (status
!= EFI_SUCCESS
)
96 if (align
< EFI_ALLOC_ALIGN
)
97 align
= EFI_ALLOC_ALIGN
;
99 /* count the suitable slots in each memory map entry */
100 for (map_offset
= 0; map_offset
< map_size
; map_offset
+= desc_size
) {
101 efi_memory_desc_t
*md
= (void *)memory_map
+ map_offset
;
104 slots
= get_entry_num_slots(md
, size
, ilog2(align
));
105 MD_NUM_SLOTS(md
) = slots
;
106 total_slots
+= slots
;
109 /* find a random number between 0 and total_slots */
110 target_slot
= (total_slots
* (u16
)random_seed
) >> 16;
113 * target_slot is now a value in the range [0, total_slots), and so
114 * it corresponds with exactly one of the suitable slots we recorded
115 * when iterating over the memory map the first time around.
117 * So iterate over the memory map again, subtracting the number of
118 * slots of each entry at each iteration, until we have found the entry
119 * that covers our chosen slot. Use the residual value of target_slot
120 * to calculate the randomly chosen address, and allocate it directly
121 * using EFI_ALLOCATE_ADDRESS.
123 for (map_offset
= 0; map_offset
< map_size
; map_offset
+= desc_size
) {
124 efi_memory_desc_t
*md
= (void *)memory_map
+ map_offset
;
125 efi_physical_addr_t target
;
128 if (target_slot
>= MD_NUM_SLOTS(md
)) {
129 target_slot
-= MD_NUM_SLOTS(md
);
133 target
= round_up(md
->phys_addr
, align
) + target_slot
* align
;
134 pages
= round_up(size
, EFI_PAGE_SIZE
) / EFI_PAGE_SIZE
;
136 status
= efi_call_early(allocate_pages
, EFI_ALLOCATE_ADDRESS
,
137 EFI_LOADER_DATA
, pages
, &target
);
138 if (status
== EFI_SUCCESS
)
143 efi_call_early(free_pool
, memory_map
);
148 #define RANDOM_SEED_SIZE 32
150 efi_status_t
efi_random_get_seed(efi_system_table_t
*sys_table_arg
)
152 efi_guid_t rng_proto
= EFI_RNG_PROTOCOL_GUID
;
153 efi_guid_t rng_algo_raw
= EFI_RNG_ALGORITHM_RAW
;
154 efi_guid_t rng_table_guid
= LINUX_EFI_RANDOM_SEED_TABLE_GUID
;
155 struct efi_rng_protocol
*rng
;
156 struct linux_efi_random_seed
*seed
;
159 status
= efi_call_early(locate_protocol
, &rng_proto
, NULL
,
161 if (status
!= EFI_SUCCESS
)
164 status
= efi_call_early(allocate_pool
, EFI_RUNTIME_SERVICES_DATA
,
165 sizeof(*seed
) + RANDOM_SEED_SIZE
,
167 if (status
!= EFI_SUCCESS
)
170 status
= rng
->get_rng(rng
, &rng_algo_raw
, RANDOM_SEED_SIZE
,
172 if (status
== EFI_UNSUPPORTED
)
174 * Use whatever algorithm we have available if the raw algorithm
175 * is not implemented.
177 status
= rng
->get_rng(rng
, NULL
, RANDOM_SEED_SIZE
,
180 if (status
!= EFI_SUCCESS
)
183 seed
->size
= RANDOM_SEED_SIZE
;
184 status
= efi_call_early(install_configuration_table
, &rng_table_guid
,
186 if (status
!= EFI_SUCCESS
)
192 efi_call_early(free_pool
, seed
);