blob: 432c9862046044eaa88c88102736c9bd9d96a32b [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
6#include <common.h>
7#include <dm.h>
8#include <efi_loader.h>
9#include <efi_rng.h>
10#include <rng.h>
11
12DECLARE_GLOBAL_DATA_PTR;
13
Sughosh Ganu33c37d92019-12-29 00:01:06 +053014const efi_guid_t efi_guid_rng_protocol = EFI_RNG_PROTOCOL_GUID;
15
Sughosh Ganuf552fa42019-12-29 00:01:05 +053016__weak efi_status_t platform_get_rng_device(struct udevice **dev)
17{
18 int ret;
19 struct udevice *devp;
20
21 ret = uclass_get_device(UCLASS_RNG, 0, &devp);
22 if (ret) {
23 debug("Unable to get rng device\n");
24 return EFI_DEVICE_ERROR;
25 }
26
27 *dev = devp;
28
29 return EFI_SUCCESS;
30}
31
32static efi_status_t EFIAPI rng_getinfo(struct efi_rng_protocol *this,
33 efi_uintn_t *rng_algorithm_list_size,
34 efi_guid_t *rng_algorithm_list)
35{
36 efi_status_t ret = EFI_SUCCESS;
37 efi_guid_t rng_algo_guid = EFI_RNG_ALGORITHM_RAW;
38
39 EFI_ENTRY("%p, %p, %p", this, rng_algorithm_list_size,
40 rng_algorithm_list);
41
42 if (!this || !rng_algorithm_list_size) {
43 ret = EFI_INVALID_PARAMETER;
44 goto back;
45 }
46
47 if (!rng_algorithm_list ||
48 *rng_algorithm_list_size < sizeof(*rng_algorithm_list)) {
49 *rng_algorithm_list_size = sizeof(*rng_algorithm_list);
50 ret = EFI_BUFFER_TOO_SMALL;
51 goto back;
52 }
53
54 /*
55 * For now, use EFI_RNG_ALGORITHM_RAW as the default
56 * algorithm. If a new algorithm gets added in the
57 * future through a Kconfig, rng_algo_guid will be set
58 * based on that Kconfig option
59 */
60 *rng_algorithm_list_size = sizeof(*rng_algorithm_list);
61 guidcpy(rng_algorithm_list, &rng_algo_guid);
62
63back:
64 return EFI_EXIT(ret);
65}
66
67static efi_status_t EFIAPI getrng(struct efi_rng_protocol *this,
68 efi_guid_t *rng_algorithm,
69 efi_uintn_t rng_value_length,
70 uint8_t *rng_value)
71{
72 int ret;
73 efi_status_t status = EFI_SUCCESS;
74 struct udevice *dev;
75 const efi_guid_t rng_raw_guid = EFI_RNG_ALGORITHM_RAW;
76
77 EFI_ENTRY("%p, %p, %zu, %p", this, rng_algorithm, rng_value_length,
78 rng_value);
79
80 if (!this || !rng_value || !rng_value_length) {
81 status = EFI_INVALID_PARAMETER;
82 goto back;
83 }
84
85 if (rng_algorithm) {
86 EFI_PRINT("RNG algorithm %pUl\n", rng_algorithm);
87 if (guidcmp(rng_algorithm, &rng_raw_guid)) {
88 status = EFI_UNSUPPORTED;
89 goto back;
90 }
91 }
92
93 ret = platform_get_rng_device(&dev);
94 if (ret != EFI_SUCCESS) {
95 EFI_PRINT("Rng device not found\n");
96 status = EFI_UNSUPPORTED;
97 goto back;
98 }
99
100 ret = dm_rng_read(dev, rng_value, rng_value_length);
101 if (ret < 0) {
102 EFI_PRINT("Rng device read failed\n");
103 status = EFI_DEVICE_ERROR;
104 goto back;
105 }
106
107back:
108 return EFI_EXIT(status);
109}
110
111const struct efi_rng_protocol efi_rng_protocol = {
112 .get_info = rng_getinfo,
113 .get_rng = getrng,
114};