1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2019, Linaro Limited
8 #include <efi_loader.h>
12 DECLARE_GLOBAL_DATA_PTR;
14 const efi_guid_t efi_guid_rng_protocol = EFI_RNG_PROTOCOL_GUID;
17 * platform_get_rng_device() - retrieve random number generator
19 * This function retrieves the udevice implementing a hardware random
22 * This function may be overridden if special initialization is needed.
27 __weak efi_status_t platform_get_rng_device(struct udevice **dev)
32 ret = uclass_get_device(UCLASS_RNG, 0, &devp);
34 debug("Unable to get rng device\n");
35 return EFI_DEVICE_ERROR;
44 * rng_getinfo() - get information about random number generation
46 * This function implement the GetInfo() service of the EFI random number
47 * generator protocol. See the UEFI spec for details.
49 * @this: random number generator protocol instance
50 * @rng_algorithm_list_size: number of random number generation algorithms
51 * @rng_algorithm_list: descriptions of random number generation
55 static efi_status_t EFIAPI rng_getinfo(struct efi_rng_protocol *this,
56 efi_uintn_t *rng_algorithm_list_size,
57 efi_guid_t *rng_algorithm_list)
59 efi_status_t ret = EFI_SUCCESS;
60 efi_guid_t rng_algo_guid = EFI_RNG_ALGORITHM_RAW;
62 EFI_ENTRY("%p, %p, %p", this, rng_algorithm_list_size,
65 if (!this || !rng_algorithm_list_size) {
66 ret = EFI_INVALID_PARAMETER;
70 if (!rng_algorithm_list ||
71 *rng_algorithm_list_size < sizeof(*rng_algorithm_list)) {
72 *rng_algorithm_list_size = sizeof(*rng_algorithm_list);
73 ret = EFI_BUFFER_TOO_SMALL;
78 * For now, use EFI_RNG_ALGORITHM_RAW as the default
79 * algorithm. If a new algorithm gets added in the
80 * future through a Kconfig, rng_algo_guid will be set
81 * based on that Kconfig option
83 *rng_algorithm_list_size = sizeof(*rng_algorithm_list);
84 guidcpy(rng_algorithm_list, &rng_algo_guid);
91 * rng_getrng() - get random value
93 * This function implement the GetRng() service of the EFI random number
94 * generator protocol. See the UEFI spec for details.
96 * @this: random number generator protocol instance
97 * @rng_algorithm: random number generation algorithm
98 * @rng_value_length: number of random bytes to generate, buffer length
99 * @rng_value: buffer to receive random bytes
100 * Return: status code
102 static efi_status_t EFIAPI getrng(struct efi_rng_protocol *this,
103 efi_guid_t *rng_algorithm,
104 efi_uintn_t rng_value_length,
108 efi_status_t status = EFI_SUCCESS;
110 const efi_guid_t rng_raw_guid = EFI_RNG_ALGORITHM_RAW;
112 EFI_ENTRY("%p, %p, %zu, %p", this, rng_algorithm, rng_value_length,
115 if (!this || !rng_value || !rng_value_length) {
116 status = EFI_INVALID_PARAMETER;
121 EFI_PRINT("RNG algorithm %pUl\n", rng_algorithm);
122 if (guidcmp(rng_algorithm, &rng_raw_guid)) {
123 status = EFI_UNSUPPORTED;
128 ret = platform_get_rng_device(&dev);
129 if (ret != EFI_SUCCESS) {
130 EFI_PRINT("Rng device not found\n");
131 status = EFI_UNSUPPORTED;
135 ret = dm_rng_read(dev, rng_value, rng_value_length);
137 EFI_PRINT("Rng device read failed\n");
138 status = EFI_DEVICE_ERROR;
143 return EFI_EXIT(status);
146 const struct efi_rng_protocol efi_rng_protocol = {
147 .get_info = rng_getinfo,