1 /*
2  * Copyright (C) 2016 Linaro Ltd;  <ard.biesheuvel@linaro.org>
3  *
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.
7  *
8  */
9 
10 #include <linux/efi.h>
11 #include <asm/efi.h>
12 
13 #include "efistub.h"
14 
15 struct efi_rng_protocol {
16 	efi_status_t (*get_info)(struct efi_rng_protocol *,
17 				 unsigned long *, efi_guid_t *);
18 	efi_status_t (*get_rng)(struct efi_rng_protocol *,
19 				efi_guid_t *, unsigned long, u8 *out);
20 };
21 
22 efi_status_t efi_get_random_bytes(efi_system_table_t *sys_table_arg,
23 				  unsigned long size, u8 *out)
24 {
25 	efi_guid_t rng_proto = EFI_RNG_PROTOCOL_GUID;
26 	efi_status_t status;
27 	struct efi_rng_protocol *rng;
28 
29 	status = efi_call_early(locate_protocol, &rng_proto, NULL,
30 				(void **)&rng);
31 	if (status != EFI_SUCCESS)
32 		return status;
33 
34 	return rng->get_rng(rng, NULL, size, out);
35 }
36 
37 /*
38  * Return the number of slots covered by this entry, i.e., the number of
39  * addresses it covers that are suitably aligned and supply enough room
40  * for the allocation.
41  */
42 static unsigned long get_entry_num_slots(efi_memory_desc_t *md,
43 					 unsigned long size,
44 					 unsigned long align)
45 {
46 	u64 start, end;
47 
48 	if (md->type != EFI_CONVENTIONAL_MEMORY)
49 		return 0;
50 
51 	start = round_up(md->phys_addr, align);
52 	end = round_down(md->phys_addr + md->num_pages * EFI_PAGE_SIZE - size,
53 			 align);
54 
55 	if (start > end)
56 		return 0;
57 
58 	return (end - start + 1) / align;
59 }
60 
61 /*
62  * The UEFI memory descriptors have a virtual address field that is only used
63  * when installing the virtual mapping using SetVirtualAddressMap(). Since it
64  * is unused here, we can reuse it to keep track of each descriptor's slot
65  * count.
66  */
67 #define MD_NUM_SLOTS(md)	((md)->virt_addr)
68 
69 efi_status_t efi_random_alloc(efi_system_table_t *sys_table_arg,
70 			      unsigned long size,
71 			      unsigned long align,
72 			      unsigned long *addr,
73 			      unsigned long random_seed)
74 {
75 	unsigned long map_size, desc_size, total_slots = 0, target_slot;
76 	unsigned long buff_size;
77 	efi_status_t status;
78 	efi_memory_desc_t *memory_map;
79 	int map_offset;
80 	struct efi_boot_memmap map;
81 
82 	map.map =	&memory_map;
83 	map.map_size =	&map_size;
84 	map.desc_size =	&desc_size;
85 	map.desc_ver =	NULL;
86 	map.key_ptr =	NULL;
87 	map.buff_size =	&buff_size;
88 
89 	status = efi_get_memory_map(sys_table_arg, &map);
90 	if (status != EFI_SUCCESS)
91 		return status;
92 
93 	if (align < EFI_ALLOC_ALIGN)
94 		align = EFI_ALLOC_ALIGN;
95 
96 	/* count the suitable slots in each memory map entry */
97 	for (map_offset = 0; map_offset < map_size; map_offset += desc_size) {
98 		efi_memory_desc_t *md = (void *)memory_map + map_offset;
99 		unsigned long slots;
100 
101 		slots = get_entry_num_slots(md, size, align);
102 		MD_NUM_SLOTS(md) = slots;
103 		total_slots += slots;
104 	}
105 
106 	/* find a random number between 0 and total_slots */
107 	target_slot = (total_slots * (u16)random_seed) >> 16;
108 
109 	/*
110 	 * target_slot is now a value in the range [0, total_slots), and so
111 	 * it corresponds with exactly one of the suitable slots we recorded
112 	 * when iterating over the memory map the first time around.
113 	 *
114 	 * So iterate over the memory map again, subtracting the number of
115 	 * slots of each entry at each iteration, until we have found the entry
116 	 * that covers our chosen slot. Use the residual value of target_slot
117 	 * to calculate the randomly chosen address, and allocate it directly
118 	 * using EFI_ALLOCATE_ADDRESS.
119 	 */
120 	for (map_offset = 0; map_offset < map_size; map_offset += desc_size) {
121 		efi_memory_desc_t *md = (void *)memory_map + map_offset;
122 		efi_physical_addr_t target;
123 		unsigned long pages;
124 
125 		if (target_slot >= MD_NUM_SLOTS(md)) {
126 			target_slot -= MD_NUM_SLOTS(md);
127 			continue;
128 		}
129 
130 		target = round_up(md->phys_addr, align) + target_slot * align;
131 		pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
132 
133 		status = efi_call_early(allocate_pages, EFI_ALLOCATE_ADDRESS,
134 					EFI_LOADER_DATA, pages, &target);
135 		if (status == EFI_SUCCESS)
136 			*addr = target;
137 		break;
138 	}
139 
140 	efi_call_early(free_pool, memory_map);
141 
142 	return status;
143 }
144