1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2019, Linaro Limited
6 #define LOG_CATEGORY LOGC_EFI
10 #include <efi_loader.h>
14 #include <asm/global_data.h>
16 DECLARE_GLOBAL_DATA_PTR;
18 const efi_guid_t efi_guid_rng_protocol = EFI_RNG_PROTOCOL_GUID;
21 * platform_get_rng_device() - retrieve random number generator
23 * This function retrieves the udevice implementing a hardware random
26 * This function may be overridden if special initialization is needed.
31 __weak efi_status_t platform_get_rng_device(struct udevice **dev)
36 ret = uclass_get_device(UCLASS_RNG, 0, &devp);
38 debug("Unable to get rng device\n");
39 return EFI_DEVICE_ERROR;
48 * rng_getinfo() - get information about random number generation
50 * This function implement the GetInfo() service of the EFI random number
51 * generator protocol. See the UEFI spec for details.
53 * @this: random number generator protocol instance
54 * @rng_algorithm_list_size: number of random number generation algorithms
55 * @rng_algorithm_list: descriptions of random number generation
59 static efi_status_t EFIAPI rng_getinfo(struct efi_rng_protocol *this,
60 efi_uintn_t *rng_algorithm_list_size,
61 efi_guid_t *rng_algorithm_list)
63 efi_status_t ret = EFI_SUCCESS;
64 efi_guid_t rng_algo_guid = EFI_RNG_ALGORITHM_RAW;
66 EFI_ENTRY("%p, %p, %p", this, rng_algorithm_list_size,
69 if (!this || !rng_algorithm_list_size) {
70 ret = EFI_INVALID_PARAMETER;
74 if (!rng_algorithm_list ||
75 *rng_algorithm_list_size < sizeof(*rng_algorithm_list)) {
76 *rng_algorithm_list_size = sizeof(*rng_algorithm_list);
77 ret = EFI_BUFFER_TOO_SMALL;
82 * For now, use EFI_RNG_ALGORITHM_RAW as the default
83 * algorithm. If a new algorithm gets added in the
84 * future through a Kconfig, rng_algo_guid will be set
85 * based on that Kconfig option
87 *rng_algorithm_list_size = sizeof(*rng_algorithm_list);
88 guidcpy(rng_algorithm_list, &rng_algo_guid);
95 * rng_getrng() - get random value
97 * This function implement the GetRng() service of the EFI random number
98 * generator protocol. See the UEFI spec for details.
100 * @this: random number generator protocol instance
101 * @rng_algorithm: random number generation algorithm
102 * @rng_value_length: number of random bytes to generate, buffer length
103 * @rng_value: buffer to receive random bytes
104 * Return: status code
106 static efi_status_t EFIAPI getrng(struct efi_rng_protocol *this,
107 efi_guid_t *rng_algorithm,
108 efi_uintn_t rng_value_length,
112 efi_status_t status = EFI_SUCCESS;
114 const efi_guid_t rng_raw_guid = EFI_RNG_ALGORITHM_RAW;
116 EFI_ENTRY("%p, %p, %zu, %p", this, rng_algorithm, rng_value_length,
119 if (!this || !rng_value || !rng_value_length) {
120 status = EFI_INVALID_PARAMETER;
125 EFI_PRINT("RNG algorithm %pUl\n", rng_algorithm);
126 if (guidcmp(rng_algorithm, &rng_raw_guid)) {
127 status = EFI_UNSUPPORTED;
132 ret = platform_get_rng_device(&dev);
133 if (ret != EFI_SUCCESS) {
134 EFI_PRINT("Rng device not found\n");
135 status = EFI_UNSUPPORTED;
139 ret = dm_rng_read(dev, rng_value, rng_value_length);
141 EFI_PRINT("Rng device read failed\n");
142 status = EFI_DEVICE_ERROR;
147 return EFI_EXIT(status);
150 static const struct efi_rng_protocol efi_rng_protocol = {
151 .get_info = rng_getinfo,
156 * efi_rng_register() - register EFI_RNG_PROTOCOL
158 * If a RNG device is available, the Random Number Generator Protocol is
161 * Return: An error status is only returned if adding the protocol fails.
163 efi_status_t efi_rng_register(void)
168 ret = platform_get_rng_device(&dev);
169 if (ret != EFI_SUCCESS) {
170 log_warning("Missing RNG device for EFI_RNG_PROTOCOL\n");
173 ret = efi_add_protocol(efi_root, &efi_guid_rng_protocol,
174 (void *)&efi_rng_protocol);
175 if (ret != EFI_SUCCESS)
176 log_err("Cannot install EFI_RNG_PROTOCOL\n");