xref: /OK3568_Linux_fs/kernel/drivers/firmware/efi/libstub/random.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (C) 2016 Linaro Ltd;  <ard.biesheuvel@linaro.org>
4*4882a593Smuzhiyun  */
5*4882a593Smuzhiyun 
6*4882a593Smuzhiyun #include <linux/efi.h>
7*4882a593Smuzhiyun #include <asm/efi.h>
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun #include "efistub.h"
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun typedef union efi_rng_protocol efi_rng_protocol_t;
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun union efi_rng_protocol {
14*4882a593Smuzhiyun 	struct {
15*4882a593Smuzhiyun 		efi_status_t (__efiapi *get_info)(efi_rng_protocol_t *,
16*4882a593Smuzhiyun 						  unsigned long *,
17*4882a593Smuzhiyun 						  efi_guid_t *);
18*4882a593Smuzhiyun 		efi_status_t (__efiapi *get_rng)(efi_rng_protocol_t *,
19*4882a593Smuzhiyun 						 efi_guid_t *, unsigned long,
20*4882a593Smuzhiyun 						 u8 *out);
21*4882a593Smuzhiyun 	};
22*4882a593Smuzhiyun 	struct {
23*4882a593Smuzhiyun 		u32 get_info;
24*4882a593Smuzhiyun 		u32 get_rng;
25*4882a593Smuzhiyun 	} mixed_mode;
26*4882a593Smuzhiyun };
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun /**
29*4882a593Smuzhiyun  * efi_get_random_bytes() - fill a buffer with random bytes
30*4882a593Smuzhiyun  * @size:	size of the buffer
31*4882a593Smuzhiyun  * @out:	caller allocated buffer to receive the random bytes
32*4882a593Smuzhiyun  *
33*4882a593Smuzhiyun  * The call will fail if either the firmware does not implement the
34*4882a593Smuzhiyun  * EFI_RNG_PROTOCOL or there are not enough random bytes available to fill
35*4882a593Smuzhiyun  * the buffer.
36*4882a593Smuzhiyun  *
37*4882a593Smuzhiyun  * Return:	status code
38*4882a593Smuzhiyun  */
efi_get_random_bytes(unsigned long size,u8 * out)39*4882a593Smuzhiyun efi_status_t efi_get_random_bytes(unsigned long size, u8 *out)
40*4882a593Smuzhiyun {
41*4882a593Smuzhiyun 	efi_guid_t rng_proto = EFI_RNG_PROTOCOL_GUID;
42*4882a593Smuzhiyun 	efi_status_t status;
43*4882a593Smuzhiyun 	efi_rng_protocol_t *rng = NULL;
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun 	status = efi_bs_call(locate_protocol, &rng_proto, NULL, (void **)&rng);
46*4882a593Smuzhiyun 	if (status != EFI_SUCCESS)
47*4882a593Smuzhiyun 		return status;
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun 	return efi_call_proto(rng, get_rng, NULL, size, out);
50*4882a593Smuzhiyun }
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun /**
53*4882a593Smuzhiyun  * efi_random_get_seed() - provide random seed as configuration table
54*4882a593Smuzhiyun  *
55*4882a593Smuzhiyun  * The EFI_RNG_PROTOCOL is used to read random bytes. These random bytes are
56*4882a593Smuzhiyun  * saved as a configuration table which can be used as entropy by the kernel
57*4882a593Smuzhiyun  * for the initialization of its pseudo random number generator.
58*4882a593Smuzhiyun  *
59*4882a593Smuzhiyun  * If the EFI_RNG_PROTOCOL is not available or there are not enough random bytes
60*4882a593Smuzhiyun  * available, the configuration table will not be installed and an error code
61*4882a593Smuzhiyun  * will be returned.
62*4882a593Smuzhiyun  *
63*4882a593Smuzhiyun  * Return:	status code
64*4882a593Smuzhiyun  */
efi_random_get_seed(void)65*4882a593Smuzhiyun efi_status_t efi_random_get_seed(void)
66*4882a593Smuzhiyun {
67*4882a593Smuzhiyun 	efi_guid_t rng_proto = EFI_RNG_PROTOCOL_GUID;
68*4882a593Smuzhiyun 	efi_guid_t rng_algo_raw = EFI_RNG_ALGORITHM_RAW;
69*4882a593Smuzhiyun 	efi_guid_t rng_table_guid = LINUX_EFI_RANDOM_SEED_TABLE_GUID;
70*4882a593Smuzhiyun 	efi_rng_protocol_t *rng = NULL;
71*4882a593Smuzhiyun 	struct linux_efi_random_seed *seed = NULL;
72*4882a593Smuzhiyun 	efi_status_t status;
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun 	status = efi_bs_call(locate_protocol, &rng_proto, NULL, (void **)&rng);
75*4882a593Smuzhiyun 	if (status != EFI_SUCCESS)
76*4882a593Smuzhiyun 		return status;
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun 	/*
79*4882a593Smuzhiyun 	 * Use EFI_ACPI_RECLAIM_MEMORY here so that it is guaranteed that the
80*4882a593Smuzhiyun 	 * allocation will survive a kexec reboot (although we refresh the seed
81*4882a593Smuzhiyun 	 * beforehand)
82*4882a593Smuzhiyun 	 */
83*4882a593Smuzhiyun 	status = efi_bs_call(allocate_pool, EFI_ACPI_RECLAIM_MEMORY,
84*4882a593Smuzhiyun 			     sizeof(*seed) + EFI_RANDOM_SEED_SIZE,
85*4882a593Smuzhiyun 			     (void **)&seed);
86*4882a593Smuzhiyun 	if (status != EFI_SUCCESS)
87*4882a593Smuzhiyun 		return status;
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun 	status = efi_call_proto(rng, get_rng, &rng_algo_raw,
90*4882a593Smuzhiyun 				 EFI_RANDOM_SEED_SIZE, seed->bits);
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun 	if (status == EFI_UNSUPPORTED)
93*4882a593Smuzhiyun 		/*
94*4882a593Smuzhiyun 		 * Use whatever algorithm we have available if the raw algorithm
95*4882a593Smuzhiyun 		 * is not implemented.
96*4882a593Smuzhiyun 		 */
97*4882a593Smuzhiyun 		status = efi_call_proto(rng, get_rng, NULL,
98*4882a593Smuzhiyun 					EFI_RANDOM_SEED_SIZE, seed->bits);
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun 	if (status != EFI_SUCCESS)
101*4882a593Smuzhiyun 		goto err_freepool;
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun 	seed->size = EFI_RANDOM_SEED_SIZE;
104*4882a593Smuzhiyun 	status = efi_bs_call(install_configuration_table, &rng_table_guid, seed);
105*4882a593Smuzhiyun 	if (status != EFI_SUCCESS)
106*4882a593Smuzhiyun 		goto err_freepool;
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun 	return EFI_SUCCESS;
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun err_freepool:
111*4882a593Smuzhiyun 	efi_bs_call(free_pool, seed);
112*4882a593Smuzhiyun 	return status;
113*4882a593Smuzhiyun }
114