Sughosh Ganu | 7064a5d | 2019-12-29 00:01:05 +0530 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (c) 2019, Linaro Limited |
| 4 | */ |
| 5 | |
Heinrich Schuchardt | c63f68f | 2020-09-25 12:50:19 +0200 | [diff] [blame] | 6 | #define LOG_CATEGORY LOGC_EFI |
| 7 | |
Sughosh Ganu | 7064a5d | 2019-12-29 00:01:05 +0530 | [diff] [blame] | 8 | #include <common.h> |
| 9 | #include <dm.h> |
| 10 | #include <efi_loader.h> |
| 11 | #include <efi_rng.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Sughosh Ganu | 7064a5d | 2019-12-29 00:01:05 +0530 | [diff] [blame] | 13 | #include <rng.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 14 | #include <asm/global_data.h> |
Sughosh Ganu | 7064a5d | 2019-12-29 00:01:05 +0530 | [diff] [blame] | 15 | |
| 16 | DECLARE_GLOBAL_DATA_PTR; |
| 17 | |
Sughosh Ganu | 693a4f4 | 2019-12-29 00:01:06 +0530 | [diff] [blame] | 18 | const efi_guid_t efi_guid_rng_protocol = EFI_RNG_PROTOCOL_GUID; |
| 19 | |
Heinrich Schuchardt | 0410be2 | 2020-01-09 20:49:44 +0100 | [diff] [blame] | 20 | /** |
| 21 | * platform_get_rng_device() - retrieve random number generator |
| 22 | * |
| 23 | * This function retrieves the udevice implementing a hardware random |
| 24 | * number generator. |
| 25 | * |
| 26 | * This function may be overridden if special initialization is needed. |
| 27 | * |
| 28 | * @dev: udevice |
| 29 | * Return: status code |
| 30 | */ |
Sughosh Ganu | 7064a5d | 2019-12-29 00:01:05 +0530 | [diff] [blame] | 31 | __weak efi_status_t platform_get_rng_device(struct udevice **dev) |
| 32 | { |
| 33 | int ret; |
| 34 | struct udevice *devp; |
| 35 | |
| 36 | ret = uclass_get_device(UCLASS_RNG, 0, &devp); |
| 37 | if (ret) { |
| 38 | debug("Unable to get rng device\n"); |
| 39 | return EFI_DEVICE_ERROR; |
| 40 | } |
| 41 | |
| 42 | *dev = devp; |
| 43 | |
| 44 | return EFI_SUCCESS; |
| 45 | } |
| 46 | |
Heinrich Schuchardt | 0410be2 | 2020-01-09 20:49:44 +0100 | [diff] [blame] | 47 | /** |
| 48 | * rng_getinfo() - get information about random number generation |
| 49 | * |
| 50 | * This function implement the GetInfo() service of the EFI random number |
| 51 | * generator protocol. See the UEFI spec for details. |
| 52 | * |
| 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 |
| 56 | * algorithms |
| 57 | * Return: status code |
| 58 | */ |
Sughosh Ganu | 7064a5d | 2019-12-29 00:01:05 +0530 | [diff] [blame] | 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) |
| 62 | { |
| 63 | efi_status_t ret = EFI_SUCCESS; |
| 64 | efi_guid_t rng_algo_guid = EFI_RNG_ALGORITHM_RAW; |
| 65 | |
| 66 | EFI_ENTRY("%p, %p, %p", this, rng_algorithm_list_size, |
| 67 | rng_algorithm_list); |
| 68 | |
| 69 | if (!this || !rng_algorithm_list_size) { |
| 70 | ret = EFI_INVALID_PARAMETER; |
| 71 | goto back; |
| 72 | } |
| 73 | |
| 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; |
| 78 | goto back; |
| 79 | } |
| 80 | |
| 81 | /* |
| 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 |
| 86 | */ |
| 87 | *rng_algorithm_list_size = sizeof(*rng_algorithm_list); |
| 88 | guidcpy(rng_algorithm_list, &rng_algo_guid); |
| 89 | |
| 90 | back: |
| 91 | return EFI_EXIT(ret); |
| 92 | } |
| 93 | |
Heinrich Schuchardt | 0410be2 | 2020-01-09 20:49:44 +0100 | [diff] [blame] | 94 | /** |
| 95 | * rng_getrng() - get random value |
| 96 | * |
| 97 | * This function implement the GetRng() service of the EFI random number |
| 98 | * generator protocol. See the UEFI spec for details. |
| 99 | * |
| 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 |
| 105 | */ |
Sughosh Ganu | 7064a5d | 2019-12-29 00:01:05 +0530 | [diff] [blame] | 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, |
| 109 | uint8_t *rng_value) |
| 110 | { |
| 111 | int ret; |
| 112 | efi_status_t status = EFI_SUCCESS; |
| 113 | struct udevice *dev; |
| 114 | const efi_guid_t rng_raw_guid = EFI_RNG_ALGORITHM_RAW; |
| 115 | |
| 116 | EFI_ENTRY("%p, %p, %zu, %p", this, rng_algorithm, rng_value_length, |
| 117 | rng_value); |
| 118 | |
| 119 | if (!this || !rng_value || !rng_value_length) { |
| 120 | status = EFI_INVALID_PARAMETER; |
| 121 | goto back; |
| 122 | } |
| 123 | |
| 124 | if (rng_algorithm) { |
Heinrich Schuchardt | 282249d | 2022-01-16 14:15:31 +0100 | [diff] [blame^] | 125 | EFI_PRINT("RNG algorithm %pUs\n", rng_algorithm); |
Sughosh Ganu | 7064a5d | 2019-12-29 00:01:05 +0530 | [diff] [blame] | 126 | if (guidcmp(rng_algorithm, &rng_raw_guid)) { |
| 127 | status = EFI_UNSUPPORTED; |
| 128 | goto back; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | ret = platform_get_rng_device(&dev); |
| 133 | if (ret != EFI_SUCCESS) { |
| 134 | EFI_PRINT("Rng device not found\n"); |
| 135 | status = EFI_UNSUPPORTED; |
| 136 | goto back; |
| 137 | } |
| 138 | |
| 139 | ret = dm_rng_read(dev, rng_value, rng_value_length); |
| 140 | if (ret < 0) { |
| 141 | EFI_PRINT("Rng device read failed\n"); |
| 142 | status = EFI_DEVICE_ERROR; |
| 143 | goto back; |
| 144 | } |
| 145 | |
| 146 | back: |
| 147 | return EFI_EXIT(status); |
| 148 | } |
| 149 | |
Heinrich Schuchardt | c63f68f | 2020-09-25 12:50:19 +0200 | [diff] [blame] | 150 | static const struct efi_rng_protocol efi_rng_protocol = { |
Sughosh Ganu | 7064a5d | 2019-12-29 00:01:05 +0530 | [diff] [blame] | 151 | .get_info = rng_getinfo, |
| 152 | .get_rng = getrng, |
| 153 | }; |
Heinrich Schuchardt | c63f68f | 2020-09-25 12:50:19 +0200 | [diff] [blame] | 154 | |
| 155 | /** |
| 156 | * efi_rng_register() - register EFI_RNG_PROTOCOL |
| 157 | * |
| 158 | * If a RNG device is available, the Random Number Generator Protocol is |
| 159 | * registered. |
| 160 | * |
| 161 | * Return: An error status is only returned if adding the protocol fails. |
| 162 | */ |
| 163 | efi_status_t efi_rng_register(void) |
| 164 | { |
| 165 | efi_status_t ret; |
| 166 | struct udevice *dev; |
| 167 | |
| 168 | ret = platform_get_rng_device(&dev); |
| 169 | if (ret != EFI_SUCCESS) { |
Paulo Alcantara | 77794eb | 2020-11-06 13:52:43 -0300 | [diff] [blame] | 170 | log_warning("Missing RNG device for EFI_RNG_PROTOCOL\n"); |
Heinrich Schuchardt | c63f68f | 2020-09-25 12:50:19 +0200 | [diff] [blame] | 171 | return EFI_SUCCESS; |
| 172 | } |
| 173 | ret = efi_add_protocol(efi_root, &efi_guid_rng_protocol, |
| 174 | (void *)&efi_rng_protocol); |
| 175 | if (ret != EFI_SUCCESS) |
Paulo Alcantara | 77794eb | 2020-11-06 13:52:43 -0300 | [diff] [blame] | 176 | log_err("Cannot install EFI_RNG_PROTOCOL\n"); |
Heinrich Schuchardt | c63f68f | 2020-09-25 12:50:19 +0200 | [diff] [blame] | 177 | |
| 178 | return ret; |
| 179 | } |