blob: fdc1b8c8f4c81416315ca32bd971afe113833444 [file] [log] [blame]
Julian Hallead5b622021-11-23 17:31:07 +01001/*
Gabor Toth75951632023-09-06 09:17:28 +02002 * Copyright (c) 2021-2023, Arm Limited and Contributors. All rights reserved.
Julian Hallead5b622021-11-23 17:31:07 +01003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <stddef.h>
8#include <protocols/rpc/common/packed-c/encoding.h>
Julian Hall98656d52022-05-05 11:09:21 +01009#include <service/uefi/smm_variable/provider/smm_variable_provider.h>
Julian Hallead5b622021-11-23 17:31:07 +010010#include <service/secure_storage/backend/secure_storage_client/secure_storage_client.h>
11#include <service/secure_storage/backend/mock_store/mock_store.h>
12#include <service_locator.h>
13
14/* Build-time default configuration */
15
16/* Default to using the Protected Storage SP */
17#ifndef SMM_GATEWAY_NV_STORE_SN
18#define SMM_GATEWAY_NV_STORE_SN "sn:ffa:751bf801-3dde-4768-a514-0f10aeed1790:0"
19#endif
20
Gabor Tothbdc11a72023-12-13 09:02:15 +010021#if defined(UEFI_AUTH_VAR) && !defined(UEFI_INTERNAL_CRYPTO)
Gabor Toth75951632023-09-06 09:17:28 +020022/* Default to using the Crypto SP */
23#ifndef SMM_GATEWAY_CRYPTO_SN
24#define SMM_GATEWAY_CRYPTO_SN "sn:ffa:d9df52d5-16a2-4bb2-9aa4-d26d3b84e8c0:0"
25#endif
26#endif
27
Julian Hallead5b622021-11-23 17:31:07 +010028/* Default maximum number of UEFI variables */
29#ifndef SMM_GATEWAY_MAX_UEFI_VARIABLES
30#define SMM_GATEWAY_MAX_UEFI_VARIABLES (40)
31#endif
32
33/* The smm_gateway instance - it's a singleton */
34static struct smm_gateway
35{
36 struct smm_variable_provider smm_variable_provider;
37 struct secure_storage_client nv_store_client;
38 struct mock_store volatile_store;
39 struct service_context *nv_storage_service_context;
Imre Kis64721422023-07-28 15:18:30 +020040 struct rpc_caller_session *nv_storage_session;
Gabor Tothbdc11a72023-12-13 09:02:15 +010041#if defined(UEFI_AUTH_VAR) && !defined(UEFI_INTERNAL_CRYPTO)
Gabor Toth75951632023-09-06 09:17:28 +020042 struct service_context *crypto_service_context;
43 struct rpc_caller_session *crypto_session;
44#endif
Julian Hallead5b622021-11-23 17:31:07 +010045
46} smm_gateway_instance;
47
Gabor Tothbdc11a72023-12-13 09:02:15 +010048#if defined(UEFI_AUTH_VAR) && !defined(UEFI_INTERNAL_CRYPTO)
Gabor Toth75951632023-09-06 09:17:28 +020049bool create_crypto_binding(void)
50{
51 psa_status_t psa_status = PSA_ERROR_GENERIC_ERROR;
52
53 smm_gateway_instance.crypto_service_context = NULL;
54 smm_gateway_instance.crypto_session = NULL;
55
56 smm_gateway_instance.crypto_service_context = service_locator_query(SMM_GATEWAY_CRYPTO_SN);
57 if (!smm_gateway_instance.crypto_service_context)
58 goto err;
59
60 smm_gateway_instance.crypto_session =
61 service_context_open(smm_gateway_instance.crypto_service_context);
62 if (!smm_gateway_instance.crypto_session)
63 goto err;
64
65 /* Initialize the crypto client */
66 psa_status = psa_crypto_client_init(smm_gateway_instance.crypto_session);
67 if (psa_status != PSA_SUCCESS)
68 goto err;
69
70 psa_status = psa_crypto_init();
71 if (psa_status != PSA_SUCCESS)
72 goto err;
73
74 return true;
75
76err:
77 if (smm_gateway_instance.crypto_session != NULL)
78 {
79 service_context_close(smm_gateway_instance.crypto_service_context, smm_gateway_instance.crypto_session);
80 smm_gateway_instance.crypto_session = NULL;
81 }
82
83 if (smm_gateway_instance.crypto_service_context != NULL)
84 {
85 service_context_relinquish(smm_gateway_instance.crypto_service_context);
86 smm_gateway_instance.crypto_service_context = NULL;
87 }
88
89 return false;
90}
91#else
92#define create_crypto_binding(a) (true)
93#endif
Julian Hallead5b622021-11-23 17:31:07 +010094
Imre Kis64721422023-07-28 15:18:30 +020095struct rpc_service_interface *smm_gateway_create(uint32_t owner_id)
Julian Hallead5b622021-11-23 17:31:07 +010096{
Imre Kis64721422023-07-28 15:18:30 +020097 service_locator_envinit();
Julian Hallead5b622021-11-23 17:31:07 +010098
99 /* todo - add option to use configurable service location */
100 smm_gateway_instance.nv_storage_service_context =
Imre Kis64721422023-07-28 15:18:30 +0200101 service_locator_query(SMM_GATEWAY_NV_STORE_SN);
Julian Hallead5b622021-11-23 17:31:07 +0100102
Imre Kis64721422023-07-28 15:18:30 +0200103 if (!smm_gateway_instance.nv_storage_service_context)
104 return NULL;
Julian Hallead5b622021-11-23 17:31:07 +0100105
Imre Kis64721422023-07-28 15:18:30 +0200106 smm_gateway_instance.nv_storage_session = service_context_open(
107 smm_gateway_instance.nv_storage_service_context);
Julian Hallead5b622021-11-23 17:31:07 +0100108
Imre Kis64721422023-07-28 15:18:30 +0200109 if (!smm_gateway_instance.nv_storage_session)
110 return NULL;
Julian Hallead5b622021-11-23 17:31:07 +0100111
112 /* Initialize a storage client to access the remote NV store */
Julian Hallead5b622021-11-23 17:31:07 +0100113 struct storage_backend *persistent_backend = secure_storage_client_init(
114 &smm_gateway_instance.nv_store_client,
Imre Kis64721422023-07-28 15:18:30 +0200115 smm_gateway_instance.nv_storage_session);
116 if (!persistent_backend)
117 return NULL;
Julian Hallead5b622021-11-23 17:31:07 +0100118
119 /* Initialize the volatile storage backend */
120 struct storage_backend *volatile_backend = mock_store_init(
121 &smm_gateway_instance.volatile_store);
Imre Kis64721422023-07-28 15:18:30 +0200122 if (!volatile_backend)
123 return NULL;
Julian Hallead5b622021-11-23 17:31:07 +0100124
125 /* Initialize the smm_variable service provider */
Imre Kis64721422023-07-28 15:18:30 +0200126 struct rpc_service_interface *service_iface = smm_variable_provider_init(
Julian Hallead5b622021-11-23 17:31:07 +0100127 &smm_gateway_instance.smm_variable_provider,
128 owner_id,
129 SMM_GATEWAY_MAX_UEFI_VARIABLES,
130 persistent_backend,
131 volatile_backend);
132
Gabor Toth75951632023-09-06 09:17:28 +0200133 if (!create_crypto_binding())
134 return NULL;
135
Julian Hallead5b622021-11-23 17:31:07 +0100136 return service_iface;
137}