blob: 9bad7ed69317379a63136af703856de55ef8cfc6 [file] [log] [blame]
Sughosh Ganuf552fa42019-12-29 00:01:05 +05301// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (c) 2019, Linaro Limited
4 */
5
Heinrich Schuchardtb59c13d2020-09-25 12:50:19 +02006#define LOG_CATEGORY LOGC_EFI
7
Sughosh Ganuf552fa42019-12-29 00:01:05 +05308#include <dm.h>
9#include <efi_loader.h>
10#include <efi_rng.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060011#include <log.h>
Sughosh Ganuf552fa42019-12-29 00:01:05 +053012#include <rng.h>
Simon Glass401d1c42020-10-30 21:38:53 -060013#include <asm/global_data.h>
Sughosh Ganuf552fa42019-12-29 00:01:05 +053014
15DECLARE_GLOBAL_DATA_PTR;
16
Sughosh Ganu33c37d92019-12-29 00:01:06 +053017const efi_guid_t efi_guid_rng_protocol = EFI_RNG_PROTOCOL_GUID;
18
Heinrich Schuchardtd417b942020-01-09 20:49:44 +010019/**
20 * platform_get_rng_device() - retrieve random number generator
21 *
22 * This function retrieves the udevice implementing a hardware random
23 * number generator.
24 *
25 * This function may be overridden if special initialization is needed.
26 *
27 * @dev: udevice
28 * Return: status code
29 */
Sughosh Ganuf552fa42019-12-29 00:01:05 +053030__weak efi_status_t platform_get_rng_device(struct udevice **dev)
31{
32 int ret;
33 struct udevice *devp;
34
35 ret = uclass_get_device(UCLASS_RNG, 0, &devp);
36 if (ret) {
37 debug("Unable to get rng device\n");
38 return EFI_DEVICE_ERROR;
39 }
40
41 *dev = devp;
42
43 return EFI_SUCCESS;
44}
45
Heinrich Schuchardtd417b942020-01-09 20:49:44 +010046/**
47 * rng_getinfo() - get information about random number generation
48 *
49 * This function implement the GetInfo() service of the EFI random number
50 * generator protocol. See the UEFI spec for details.
51 *
52 * @this: random number generator protocol instance
53 * @rng_algorithm_list_size: number of random number generation algorithms
54 * @rng_algorithm_list: descriptions of random number generation
55 * algorithms
56 * Return: status code
57 */
Sughosh Ganuf552fa42019-12-29 00:01:05 +053058static efi_status_t EFIAPI rng_getinfo(struct efi_rng_protocol *this,
59 efi_uintn_t *rng_algorithm_list_size,
60 efi_guid_t *rng_algorithm_list)
61{
62 efi_status_t ret = EFI_SUCCESS;
63 efi_guid_t rng_algo_guid = EFI_RNG_ALGORITHM_RAW;
64
65 EFI_ENTRY("%p, %p, %p", this, rng_algorithm_list_size,
66 rng_algorithm_list);
67
68 if (!this || !rng_algorithm_list_size) {
69 ret = EFI_INVALID_PARAMETER;
70 goto back;
71 }
72
73 if (!rng_algorithm_list ||
74 *rng_algorithm_list_size < sizeof(*rng_algorithm_list)) {
75 *rng_algorithm_list_size = sizeof(*rng_algorithm_list);
76 ret = EFI_BUFFER_TOO_SMALL;
77 goto back;
78 }
79
80 /*
81 * For now, use EFI_RNG_ALGORITHM_RAW as the default
82 * algorithm. If a new algorithm gets added in the
83 * future through a Kconfig, rng_algo_guid will be set
84 * based on that Kconfig option
85 */
86 *rng_algorithm_list_size = sizeof(*rng_algorithm_list);
87 guidcpy(rng_algorithm_list, &rng_algo_guid);
88
89back:
90 return EFI_EXIT(ret);
91}
92
Heinrich Schuchardtd417b942020-01-09 20:49:44 +010093/**
94 * rng_getrng() - get random value
95 *
96 * This function implement the GetRng() service of the EFI random number
97 * generator protocol. See the UEFI spec for details.
98 *
99 * @this: random number generator protocol instance
100 * @rng_algorithm: random number generation algorithm
101 * @rng_value_length: number of random bytes to generate, buffer length
102 * @rng_value: buffer to receive random bytes
103 * Return: status code
104 */
Sughosh Ganuf552fa42019-12-29 00:01:05 +0530105static efi_status_t EFIAPI getrng(struct efi_rng_protocol *this,
106 efi_guid_t *rng_algorithm,
107 efi_uintn_t rng_value_length,
108 uint8_t *rng_value)
109{
110 int ret;
111 efi_status_t status = EFI_SUCCESS;
112 struct udevice *dev;
113 const efi_guid_t rng_raw_guid = EFI_RNG_ALGORITHM_RAW;
114
115 EFI_ENTRY("%p, %p, %zu, %p", this, rng_algorithm, rng_value_length,
116 rng_value);
117
118 if (!this || !rng_value || !rng_value_length) {
119 status = EFI_INVALID_PARAMETER;
120 goto back;
121 }
122
123 if (rng_algorithm) {
Heinrich Schuchardtce00a742022-01-16 14:15:31 +0100124 EFI_PRINT("RNG algorithm %pUs\n", rng_algorithm);
Sughosh Ganuf552fa42019-12-29 00:01:05 +0530125 if (guidcmp(rng_algorithm, &rng_raw_guid)) {
126 status = EFI_UNSUPPORTED;
127 goto back;
128 }
129 }
130
131 ret = platform_get_rng_device(&dev);
132 if (ret != EFI_SUCCESS) {
133 EFI_PRINT("Rng device not found\n");
134 status = EFI_UNSUPPORTED;
135 goto back;
136 }
137
138 ret = dm_rng_read(dev, rng_value, rng_value_length);
139 if (ret < 0) {
140 EFI_PRINT("Rng device read failed\n");
141 status = EFI_DEVICE_ERROR;
142 goto back;
143 }
144
145back:
146 return EFI_EXIT(status);
147}
148
Heinrich Schuchardtb59c13d2020-09-25 12:50:19 +0200149static const struct efi_rng_protocol efi_rng_protocol = {
Sughosh Ganuf552fa42019-12-29 00:01:05 +0530150 .get_info = rng_getinfo,
151 .get_rng = getrng,
152};
Heinrich Schuchardtb59c13d2020-09-25 12:50:19 +0200153
154/**
155 * efi_rng_register() - register EFI_RNG_PROTOCOL
156 *
157 * If a RNG device is available, the Random Number Generator Protocol is
158 * registered.
159 *
160 * Return: An error status is only returned if adding the protocol fails.
161 */
162efi_status_t efi_rng_register(void)
163{
164 efi_status_t ret;
165 struct udevice *dev;
166
167 ret = platform_get_rng_device(&dev);
168 if (ret != EFI_SUCCESS) {
Paulo Alcantara337c97d2020-11-06 13:52:43 -0300169 log_warning("Missing RNG device for EFI_RNG_PROTOCOL\n");
Heinrich Schuchardtb59c13d2020-09-25 12:50:19 +0200170 return EFI_SUCCESS;
171 }
172 ret = efi_add_protocol(efi_root, &efi_guid_rng_protocol,
173 (void *)&efi_rng_protocol);
174 if (ret != EFI_SUCCESS)
Paulo Alcantara337c97d2020-11-06 13:52:43 -0300175 log_err("Cannot install EFI_RNG_PROTOCOL\n");
Heinrich Schuchardtb59c13d2020-09-25 12:50:19 +0200176
177 return ret;
178}