1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2016 Linaro Ltd; <ard.biesheuvel@linaro.org>
7 #include <linux/log2.h>
12 typedef struct efi_rng_protocol efi_rng_protocol_t;
17 } efi_rng_protocol_32_t;
22 } efi_rng_protocol_64_t;
24 struct efi_rng_protocol {
25 efi_status_t (*get_info)(struct efi_rng_protocol *,
26 unsigned long *, efi_guid_t *);
27 efi_status_t (*get_rng)(struct efi_rng_protocol *,
28 efi_guid_t *, unsigned long, u8 *out);
31 efi_status_t efi_get_random_bytes(efi_system_table_t *sys_table_arg,
32 unsigned long size, u8 *out)
34 efi_guid_t rng_proto = EFI_RNG_PROTOCOL_GUID;
36 struct efi_rng_protocol *rng = NULL;
38 status = efi_call_early(locate_protocol, &rng_proto, NULL,
40 if (status != EFI_SUCCESS)
43 return efi_call_proto(efi_rng_protocol, get_rng, rng, NULL, size, out);
47 * Return the number of slots covered by this entry, i.e., the number of
48 * addresses it covers that are suitably aligned and supply enough room
51 static unsigned long get_entry_num_slots(efi_memory_desc_t *md,
53 unsigned long align_shift)
55 unsigned long align = 1UL << align_shift;
56 u64 first_slot, last_slot, region_end;
58 if (md->type != EFI_CONVENTIONAL_MEMORY)
61 if (efi_soft_reserve_enabled() &&
62 (md->attribute & EFI_MEMORY_SP))
65 region_end = min((u64)ULONG_MAX, md->phys_addr + md->num_pages*EFI_PAGE_SIZE - 1);
67 first_slot = round_up(md->phys_addr, align);
68 last_slot = round_down(region_end - size + 1, align);
70 if (first_slot > last_slot)
73 return ((unsigned long)(last_slot - first_slot) >> align_shift) + 1;
77 * The UEFI memory descriptors have a virtual address field that is only used
78 * when installing the virtual mapping using SetVirtualAddressMap(). Since it
79 * is unused here, we can reuse it to keep track of each descriptor's slot
82 #define MD_NUM_SLOTS(md) ((md)->virt_addr)
84 efi_status_t efi_random_alloc(efi_system_table_t *sys_table_arg,
88 unsigned long random_seed)
90 unsigned long map_size, desc_size, total_slots = 0, target_slot;
91 unsigned long buff_size;
93 efi_memory_desc_t *memory_map;
95 struct efi_boot_memmap map;
97 map.map = &memory_map;
98 map.map_size = &map_size;
99 map.desc_size = &desc_size;
102 map.buff_size = &buff_size;
104 status = efi_get_memory_map(sys_table_arg, &map);
105 if (status != EFI_SUCCESS)
108 if (align < EFI_ALLOC_ALIGN)
109 align = EFI_ALLOC_ALIGN;
111 /* count the suitable slots in each memory map entry */
112 for (map_offset = 0; map_offset < map_size; map_offset += desc_size) {
113 efi_memory_desc_t *md = (void *)memory_map + map_offset;
116 slots = get_entry_num_slots(md, size, ilog2(align));
117 MD_NUM_SLOTS(md) = slots;
118 total_slots += slots;
121 /* find a random number between 0 and total_slots */
122 target_slot = (total_slots * (u16)random_seed) >> 16;
125 * target_slot is now a value in the range [0, total_slots), and so
126 * it corresponds with exactly one of the suitable slots we recorded
127 * when iterating over the memory map the first time around.
129 * So iterate over the memory map again, subtracting the number of
130 * slots of each entry at each iteration, until we have found the entry
131 * that covers our chosen slot. Use the residual value of target_slot
132 * to calculate the randomly chosen address, and allocate it directly
133 * using EFI_ALLOCATE_ADDRESS.
135 for (map_offset = 0; map_offset < map_size; map_offset += desc_size) {
136 efi_memory_desc_t *md = (void *)memory_map + map_offset;
137 efi_physical_addr_t target;
140 if (target_slot >= MD_NUM_SLOTS(md)) {
141 target_slot -= MD_NUM_SLOTS(md);
145 target = round_up(md->phys_addr, align) + target_slot * align;
146 pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
148 status = efi_call_early(allocate_pages, EFI_ALLOCATE_ADDRESS,
149 EFI_LOADER_DATA, pages, &target);
150 if (status == EFI_SUCCESS)
155 efi_call_early(free_pool, memory_map);
160 efi_status_t efi_random_get_seed(efi_system_table_t *sys_table_arg)
162 efi_guid_t rng_proto = EFI_RNG_PROTOCOL_GUID;
163 efi_guid_t rng_algo_raw = EFI_RNG_ALGORITHM_RAW;
164 efi_guid_t rng_table_guid = LINUX_EFI_RANDOM_SEED_TABLE_GUID;
165 struct efi_rng_protocol *rng = NULL;
166 struct linux_efi_random_seed *seed = NULL;
169 status = efi_call_early(locate_protocol, &rng_proto, NULL,
171 if (status != EFI_SUCCESS)
174 status = efi_call_early(allocate_pool, EFI_RUNTIME_SERVICES_DATA,
175 sizeof(*seed) + EFI_RANDOM_SEED_SIZE,
177 if (status != EFI_SUCCESS)
180 status = efi_call_proto(efi_rng_protocol, get_rng, rng, &rng_algo_raw,
181 EFI_RANDOM_SEED_SIZE, seed->bits);
183 if (status == EFI_UNSUPPORTED)
185 * Use whatever algorithm we have available if the raw algorithm
186 * is not implemented.
188 status = efi_call_proto(efi_rng_protocol, get_rng, rng, NULL,
189 EFI_RANDOM_SEED_SIZE, seed->bits);
191 if (status != EFI_SUCCESS)
194 seed->size = EFI_RANDOM_SEED_SIZE;
195 status = efi_call_early(install_configuration_table, &rng_table_guid,
197 if (status != EFI_SUCCESS)
203 efi_call_early(free_pool, seed);