1 /*
2 * Copyright (C) 2024 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18 #include "runtime_service_provider.h"
19
20 #include <platform.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <uefi/types.h>
24
25 #include "variable_mem.h"
26
27 namespace {
28
29 constexpr auto &&kSecureBoot = "SecureBoot";
30
GetVariable(char16_t * VariableName,EfiGuid * VendorGuid,uint32_t * Attributes,size_t * DataSize,void * Data)31 EFI_STATUS GetVariable(char16_t *VariableName, EfiGuid *VendorGuid,
32 uint32_t *Attributes, size_t *DataSize, void *Data) {
33
34 if (!VariableName || !VendorGuid || !DataSize) {
35 return INVALID_PARAMETER;
36 }
37
38 char buffer[512];
39 size_t i = 0;
40 while (VariableName[i] && i < sizeof(buffer)) {
41 size_t j = 0;
42 for (j = 0; j < sizeof(buffer) - 1 && VariableName[i + j]; j++) {
43 buffer[j] = VariableName[i + j];
44 }
45 i += j;
46 }
47 buffer[i] = 0;
48 if (strncmp(buffer, kSecureBoot, sizeof(kSecureBoot)) == 0 || strcmp(buffer, "SetupMode") == 0) {
49 if (DataSize) {
50 *DataSize = 1;
51 }
52 if (Data) {
53 memset(Data, 0, 1);
54 }
55 return SUCCESS;
56 }
57
58 char *data_in_mem;
59 size_t data_in_mem_size;
60 if (efi_get_variable(VariableName, VendorGuid, Attributes, &data_in_mem, &data_in_mem_size) == SUCCESS) {
61 if (*DataSize == 0 && !Data) {
62 *DataSize = data_in_mem_size;
63 return BUFFER_TOO_SMALL;
64 }
65 if (data_in_mem_size > *DataSize) {
66 return BUFFER_TOO_SMALL;
67 }
68 *DataSize = data_in_mem_size;
69 memcpy(Data, data_in_mem, data_in_mem_size);
70 return SUCCESS;
71 }
72
73 printf("%s(%s) is unsupported\n", __FUNCTION__, buffer);
74 return UNSUPPORTED;
75 }
76
SetVirtualAddressMap(size_t MemoryMapSize,size_t DescriptorSize,uint32_t DescriptorVersion,EfiMemoryDescriptor * VirtualMap)77 EFI_STATUS SetVirtualAddressMap(size_t MemoryMapSize, size_t DescriptorSize,
78 uint32_t DescriptorVersion,
79 EfiMemoryDescriptor *VirtualMap) {
80 printf("%s is unsupported\n", __FUNCTION__);
81 return UNSUPPORTED;
82 }
83
SetVariable(char16_t * VariableName,EfiGuid * VendorGuid,uint32_t Attributes,size_t DataSize,void * Data)84 EFI_STATUS SetVariable(char16_t *VariableName, EfiGuid *VendorGuid,
85 uint32_t Attributes, size_t DataSize, void *Data) {
86 if (!VariableName || VariableName[0] == 0) {
87 return INVALID_PARAMETER;
88 }
89
90 /* Only allow setting non-volatile variables */
91 if ((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) {
92 efi_set_variable(VariableName,
93 VendorGuid,
94 Attributes,
95 reinterpret_cast<const char *>(Data),
96 DataSize);
97 return SUCCESS;
98 }
99
100 printf("%s: Only non-volatile is supported. Attributes = 0x%x\n",
101 __FUNCTION__,
102 Attributes);
103 return UNSUPPORTED;
104 }
105
ResetSystem(EFI_RESET_TYPE ResetType,EFI_STATUS ResetStatus,size_t DataSize,void * ResetData)106 void ResetSystem(EFI_RESET_TYPE ResetType, EFI_STATUS ResetStatus,
107 size_t DataSize, void *ResetData) {
108 platform_halt(HALT_ACTION_REBOOT, HALT_REASON_SW_RESET);
109 }
110
111 } // namespace
112
setup_runtime_service_table(EfiRuntimeService * service)113 void setup_runtime_service_table(EfiRuntimeService *service) {
114 service->GetVariable = GetVariable;
115 service->SetVariable = SetVariable;
116 service->SetVirtualAddressMap = SetVirtualAddressMap;
117 service->ResetSystem = ResetSystem;
118 }
119