xref: /linux/drivers/firmware/efi/libstub/random.c (revision 966291f6344d7eb6fc3204381a426bafa20a3d18)
14febfb8dSArd Biesheuvel // SPDX-License-Identifier: GPL-2.0
2e4fbf476SArd Biesheuvel /*
3e4fbf476SArd Biesheuvel  * Copyright (C) 2016 Linaro Ltd;  <ard.biesheuvel@linaro.org>
4e4fbf476SArd Biesheuvel  */
5e4fbf476SArd Biesheuvel 
6e4fbf476SArd Biesheuvel #include <linux/efi.h>
7a6a14469SArd Biesheuvel #include <linux/log2.h>
8e4fbf476SArd Biesheuvel #include <asm/efi.h>
9e4fbf476SArd Biesheuvel 
10e4fbf476SArd Biesheuvel #include "efistub.h"
11e4fbf476SArd Biesheuvel 
121786e830SArd Biesheuvel typedef union efi_rng_protocol efi_rng_protocol_t;
1341e8a7c2SDominik Brodowski 
141786e830SArd Biesheuvel union efi_rng_protocol {
151786e830SArd Biesheuvel 	struct {
168f24f8c2SArd Biesheuvel 		efi_status_t (__efiapi *get_info)(efi_rng_protocol_t *,
178f24f8c2SArd Biesheuvel 						  unsigned long *,
188f24f8c2SArd Biesheuvel 						  efi_guid_t *);
198f24f8c2SArd Biesheuvel 		efi_status_t (__efiapi *get_rng)(efi_rng_protocol_t *,
208f24f8c2SArd Biesheuvel 						 efi_guid_t *, unsigned long,
218f24f8c2SArd Biesheuvel 						 u8 *out);
22e4fbf476SArd Biesheuvel 	};
231786e830SArd Biesheuvel 	struct {
241786e830SArd Biesheuvel 		u32 get_info;
251786e830SArd Biesheuvel 		u32 get_rng;
261786e830SArd Biesheuvel 	} mixed_mode;
271786e830SArd Biesheuvel };
28e4fbf476SArd Biesheuvel 
29cd33a5c1SArd Biesheuvel efi_status_t efi_get_random_bytes(unsigned long size, u8 *out)
30e4fbf476SArd Biesheuvel {
31e4fbf476SArd Biesheuvel 	efi_guid_t rng_proto = EFI_RNG_PROTOCOL_GUID;
32e4fbf476SArd Biesheuvel 	efi_status_t status;
331786e830SArd Biesheuvel 	efi_rng_protocol_t *rng = NULL;
34e4fbf476SArd Biesheuvel 
35*966291f6SArd Biesheuvel 	status = efi_bs_call(locate_protocol, &rng_proto, NULL, (void **)&rng);
36e4fbf476SArd Biesheuvel 	if (status != EFI_SUCCESS)
37e4fbf476SArd Biesheuvel 		return status;
38e4fbf476SArd Biesheuvel 
3947c0fd39SArd Biesheuvel 	return efi_call_proto(rng, get_rng, NULL, size, out);
40e4fbf476SArd Biesheuvel }
412ddbfc81SArd Biesheuvel 
422ddbfc81SArd Biesheuvel /*
432ddbfc81SArd Biesheuvel  * Return the number of slots covered by this entry, i.e., the number of
442ddbfc81SArd Biesheuvel  * addresses it covers that are suitably aligned and supply enough room
452ddbfc81SArd Biesheuvel  * for the allocation.
462ddbfc81SArd Biesheuvel  */
472ddbfc81SArd Biesheuvel static unsigned long get_entry_num_slots(efi_memory_desc_t *md,
482ddbfc81SArd Biesheuvel 					 unsigned long size,
49a6a14469SArd Biesheuvel 					 unsigned long align_shift)
502ddbfc81SArd Biesheuvel {
51a6a14469SArd Biesheuvel 	unsigned long align = 1UL << align_shift;
52018edcfaSArd Biesheuvel 	u64 first_slot, last_slot, region_end;
532ddbfc81SArd Biesheuvel 
542ddbfc81SArd Biesheuvel 	if (md->type != EFI_CONVENTIONAL_MEMORY)
552ddbfc81SArd Biesheuvel 		return 0;
562ddbfc81SArd Biesheuvel 
5716993c0fSDan Williams 	if (efi_soft_reserve_enabled() &&
5816993c0fSDan Williams 	    (md->attribute & EFI_MEMORY_SP))
5916993c0fSDan Williams 		return 0;
6016993c0fSDan Williams 
61018edcfaSArd Biesheuvel 	region_end = min((u64)ULONG_MAX, md->phys_addr + md->num_pages*EFI_PAGE_SIZE - 1);
622ddbfc81SArd Biesheuvel 
63018edcfaSArd Biesheuvel 	first_slot = round_up(md->phys_addr, align);
64018edcfaSArd Biesheuvel 	last_slot = round_down(region_end - size + 1, align);
65018edcfaSArd Biesheuvel 
66018edcfaSArd Biesheuvel 	if (first_slot > last_slot)
672ddbfc81SArd Biesheuvel 		return 0;
682ddbfc81SArd Biesheuvel 
69018edcfaSArd Biesheuvel 	return ((unsigned long)(last_slot - first_slot) >> align_shift) + 1;
702ddbfc81SArd Biesheuvel }
712ddbfc81SArd Biesheuvel 
722ddbfc81SArd Biesheuvel /*
732ddbfc81SArd Biesheuvel  * The UEFI memory descriptors have a virtual address field that is only used
742ddbfc81SArd Biesheuvel  * when installing the virtual mapping using SetVirtualAddressMap(). Since it
752ddbfc81SArd Biesheuvel  * is unused here, we can reuse it to keep track of each descriptor's slot
762ddbfc81SArd Biesheuvel  * count.
772ddbfc81SArd Biesheuvel  */
782ddbfc81SArd Biesheuvel #define MD_NUM_SLOTS(md)	((md)->virt_addr)
792ddbfc81SArd Biesheuvel 
80cd33a5c1SArd Biesheuvel efi_status_t efi_random_alloc(unsigned long size,
812ddbfc81SArd Biesheuvel 			      unsigned long align,
822ddbfc81SArd Biesheuvel 			      unsigned long *addr,
832ddbfc81SArd Biesheuvel 			      unsigned long random_seed)
842ddbfc81SArd Biesheuvel {
852ddbfc81SArd Biesheuvel 	unsigned long map_size, desc_size, total_slots = 0, target_slot;
86dadb57abSJeffrey Hugo 	unsigned long buff_size;
872ddbfc81SArd Biesheuvel 	efi_status_t status;
882ddbfc81SArd Biesheuvel 	efi_memory_desc_t *memory_map;
892ddbfc81SArd Biesheuvel 	int map_offset;
90dadb57abSJeffrey Hugo 	struct efi_boot_memmap map;
912ddbfc81SArd Biesheuvel 
92dadb57abSJeffrey Hugo 	map.map =	&memory_map;
93dadb57abSJeffrey Hugo 	map.map_size =	&map_size;
94dadb57abSJeffrey Hugo 	map.desc_size =	&desc_size;
95dadb57abSJeffrey Hugo 	map.desc_ver =	NULL;
96dadb57abSJeffrey Hugo 	map.key_ptr =	NULL;
97dadb57abSJeffrey Hugo 	map.buff_size =	&buff_size;
98dadb57abSJeffrey Hugo 
99cd33a5c1SArd Biesheuvel 	status = efi_get_memory_map(&map);
1002ddbfc81SArd Biesheuvel 	if (status != EFI_SUCCESS)
1012ddbfc81SArd Biesheuvel 		return status;
1022ddbfc81SArd Biesheuvel 
1032ddbfc81SArd Biesheuvel 	if (align < EFI_ALLOC_ALIGN)
1042ddbfc81SArd Biesheuvel 		align = EFI_ALLOC_ALIGN;
1052ddbfc81SArd Biesheuvel 
1062ddbfc81SArd Biesheuvel 	/* count the suitable slots in each memory map entry */
1072ddbfc81SArd Biesheuvel 	for (map_offset = 0; map_offset < map_size; map_offset += desc_size) {
1082ddbfc81SArd Biesheuvel 		efi_memory_desc_t *md = (void *)memory_map + map_offset;
1092ddbfc81SArd Biesheuvel 		unsigned long slots;
1102ddbfc81SArd Biesheuvel 
111a6a14469SArd Biesheuvel 		slots = get_entry_num_slots(md, size, ilog2(align));
1122ddbfc81SArd Biesheuvel 		MD_NUM_SLOTS(md) = slots;
1132ddbfc81SArd Biesheuvel 		total_slots += slots;
1142ddbfc81SArd Biesheuvel 	}
1152ddbfc81SArd Biesheuvel 
1162ddbfc81SArd Biesheuvel 	/* find a random number between 0 and total_slots */
1172ddbfc81SArd Biesheuvel 	target_slot = (total_slots * (u16)random_seed) >> 16;
1182ddbfc81SArd Biesheuvel 
1192ddbfc81SArd Biesheuvel 	/*
1202ddbfc81SArd Biesheuvel 	 * target_slot is now a value in the range [0, total_slots), and so
1212ddbfc81SArd Biesheuvel 	 * it corresponds with exactly one of the suitable slots we recorded
1222ddbfc81SArd Biesheuvel 	 * when iterating over the memory map the first time around.
1232ddbfc81SArd Biesheuvel 	 *
1242ddbfc81SArd Biesheuvel 	 * So iterate over the memory map again, subtracting the number of
1252ddbfc81SArd Biesheuvel 	 * slots of each entry at each iteration, until we have found the entry
1262ddbfc81SArd Biesheuvel 	 * that covers our chosen slot. Use the residual value of target_slot
1272ddbfc81SArd Biesheuvel 	 * to calculate the randomly chosen address, and allocate it directly
1282ddbfc81SArd Biesheuvel 	 * using EFI_ALLOCATE_ADDRESS.
1292ddbfc81SArd Biesheuvel 	 */
1302ddbfc81SArd Biesheuvel 	for (map_offset = 0; map_offset < map_size; map_offset += desc_size) {
1312ddbfc81SArd Biesheuvel 		efi_memory_desc_t *md = (void *)memory_map + map_offset;
1322ddbfc81SArd Biesheuvel 		efi_physical_addr_t target;
1332ddbfc81SArd Biesheuvel 		unsigned long pages;
1342ddbfc81SArd Biesheuvel 
1352ddbfc81SArd Biesheuvel 		if (target_slot >= MD_NUM_SLOTS(md)) {
1362ddbfc81SArd Biesheuvel 			target_slot -= MD_NUM_SLOTS(md);
1372ddbfc81SArd Biesheuvel 			continue;
1382ddbfc81SArd Biesheuvel 		}
1392ddbfc81SArd Biesheuvel 
1402ddbfc81SArd Biesheuvel 		target = round_up(md->phys_addr, align) + target_slot * align;
1412ddbfc81SArd Biesheuvel 		pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
1422ddbfc81SArd Biesheuvel 
143*966291f6SArd Biesheuvel 		status = efi_bs_call(allocate_pages, EFI_ALLOCATE_ADDRESS,
1442ddbfc81SArd Biesheuvel 				     EFI_LOADER_DATA, pages, &target);
1452ddbfc81SArd Biesheuvel 		if (status == EFI_SUCCESS)
1462ddbfc81SArd Biesheuvel 			*addr = target;
1472ddbfc81SArd Biesheuvel 		break;
1482ddbfc81SArd Biesheuvel 	}
1492ddbfc81SArd Biesheuvel 
150*966291f6SArd Biesheuvel 	efi_bs_call(free_pool, memory_map);
1512ddbfc81SArd Biesheuvel 
1522ddbfc81SArd Biesheuvel 	return status;
1532ddbfc81SArd Biesheuvel }
154568bc4e8SArd Biesheuvel 
155cd33a5c1SArd Biesheuvel efi_status_t efi_random_get_seed(void)
156568bc4e8SArd Biesheuvel {
157568bc4e8SArd Biesheuvel 	efi_guid_t rng_proto = EFI_RNG_PROTOCOL_GUID;
158568bc4e8SArd Biesheuvel 	efi_guid_t rng_algo_raw = EFI_RNG_ALGORITHM_RAW;
159568bc4e8SArd Biesheuvel 	efi_guid_t rng_table_guid = LINUX_EFI_RANDOM_SEED_TABLE_GUID;
1601786e830SArd Biesheuvel 	efi_rng_protocol_t *rng = NULL;
161818c7ce7SHans de Goede 	struct linux_efi_random_seed *seed = NULL;
162568bc4e8SArd Biesheuvel 	efi_status_t status;
163568bc4e8SArd Biesheuvel 
164*966291f6SArd Biesheuvel 	status = efi_bs_call(locate_protocol, &rng_proto, NULL, (void **)&rng);
165568bc4e8SArd Biesheuvel 	if (status != EFI_SUCCESS)
166568bc4e8SArd Biesheuvel 		return status;
167568bc4e8SArd Biesheuvel 
168*966291f6SArd Biesheuvel 	status = efi_bs_call(allocate_pool, EFI_RUNTIME_SERVICES_DATA,
169c2ceb5fdSArd Biesheuvel 			     sizeof(*seed) + EFI_RANDOM_SEED_SIZE,
170568bc4e8SArd Biesheuvel 			     (void **)&seed);
171568bc4e8SArd Biesheuvel 	if (status != EFI_SUCCESS)
172568bc4e8SArd Biesheuvel 		return status;
173568bc4e8SArd Biesheuvel 
17447c0fd39SArd Biesheuvel 	status = efi_call_proto(rng, get_rng, &rng_algo_raw,
17541e8a7c2SDominik Brodowski 				 EFI_RANDOM_SEED_SIZE, seed->bits);
17641e8a7c2SDominik Brodowski 
177568bc4e8SArd Biesheuvel 	if (status == EFI_UNSUPPORTED)
178568bc4e8SArd Biesheuvel 		/*
179568bc4e8SArd Biesheuvel 		 * Use whatever algorithm we have available if the raw algorithm
180568bc4e8SArd Biesheuvel 		 * is not implemented.
181568bc4e8SArd Biesheuvel 		 */
18247c0fd39SArd Biesheuvel 		status = efi_call_proto(rng, get_rng, NULL,
18341e8a7c2SDominik Brodowski 					EFI_RANDOM_SEED_SIZE, seed->bits);
184568bc4e8SArd Biesheuvel 
185568bc4e8SArd Biesheuvel 	if (status != EFI_SUCCESS)
186568bc4e8SArd Biesheuvel 		goto err_freepool;
187568bc4e8SArd Biesheuvel 
188c2ceb5fdSArd Biesheuvel 	seed->size = EFI_RANDOM_SEED_SIZE;
189*966291f6SArd Biesheuvel 	status = efi_bs_call(install_configuration_table, &rng_table_guid, seed);
190568bc4e8SArd Biesheuvel 	if (status != EFI_SUCCESS)
191568bc4e8SArd Biesheuvel 		goto err_freepool;
192568bc4e8SArd Biesheuvel 
193568bc4e8SArd Biesheuvel 	return EFI_SUCCESS;
194568bc4e8SArd Biesheuvel 
195568bc4e8SArd Biesheuvel err_freepool:
196*966291f6SArd Biesheuvel 	efi_bs_call(free_pool, seed);
197568bc4e8SArd Biesheuvel 	return status;
198568bc4e8SArd Biesheuvel }
199