Tom Rini | 70df9d6 | 2018-05-07 17:02:21 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 2 | /* |
Heinrich Schuchardt | c69addd | 2020-03-19 17:15:18 +0000 | [diff] [blame] | 3 | * UEFI runtime variable services |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 4 | * |
Heinrich Schuchardt | c69addd | 2020-03-19 17:15:18 +0000 | [diff] [blame] | 5 | * Copyright (c) 2017 Rob Clark |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 6 | */ |
| 7 | |
Heinrich Schuchardt | 4b7d5c1 | 2020-07-14 21:25:28 +0200 | [diff] [blame] | 8 | #define LOG_CATEGORY LOGC_EFI |
| 9 | |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 10 | #include <efi_loader.h> |
Heinrich Schuchardt | 9827e84 | 2020-06-22 18:10:27 +0200 | [diff] [blame] | 11 | #include <efi_variable.h> |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 12 | #include <env.h> |
Simon Glass | 9d1f619 | 2019-08-02 09:44:25 -0600 | [diff] [blame] | 13 | #include <env_internal.h> |
Heinrich Schuchardt | 147d5d3 | 2019-10-26 23:53:48 +0200 | [diff] [blame] | 14 | #include <hexdump.h> |
Heinrich Schuchardt | 4b7d5c1 | 2020-07-14 21:25:28 +0200 | [diff] [blame] | 15 | #include <log.h> |
Heinrich Schuchardt | 147d5d3 | 2019-10-26 23:53:48 +0200 | [diff] [blame] | 16 | #include <malloc.h> |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 17 | #include <rtc.h> |
AKASHI Takahiro | ad3f36c | 2019-01-21 12:43:13 +0100 | [diff] [blame] | 18 | #include <search.h> |
Simon Glass | 6f04498 | 2020-05-10 11:39:52 -0600 | [diff] [blame] | 19 | #include <uuid.h> |
AKASHI Takahiro | 6ec6767 | 2020-04-21 09:38:17 +0900 | [diff] [blame] | 20 | #include <crypto/pkcs7_parser.h> |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 21 | #include <linux/compat.h> |
Simon Glass | 48b6c6b | 2019-11-14 12:57:16 -0700 | [diff] [blame] | 22 | #include <u-boot/crc.h> |
Heinrich Schuchardt | 4b7d5c1 | 2020-07-14 21:25:28 +0200 | [diff] [blame] | 23 | #include <asm/sections.h> |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 24 | |
| 25 | #ifdef CONFIG_EFI_SECURE_BOOT |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 26 | |
| 27 | /** |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 28 | * efi_variable_authenticate - authenticate a variable |
| 29 | * @variable: Variable name in u16 |
| 30 | * @vendor: Guid of variable |
| 31 | * @data_size: Size of @data |
| 32 | * @data: Pointer to variable's value |
| 33 | * @given_attr: Attributes to be given at SetVariable() |
| 34 | * @env_attr: Attributes that an existing variable holds |
| 35 | * @time: signed time that an existing variable holds |
| 36 | * |
| 37 | * Called by efi_set_variable() to verify that the input is correct. |
| 38 | * Will replace the given data pointer with another that points to |
| 39 | * the actual data to store in the internal memory. |
| 40 | * On success, @data and @data_size will be replaced with variable's |
| 41 | * actual data, excluding authentication data, and its size, and variable's |
| 42 | * attributes and signed time will also be returned in @env_attr and @time, |
| 43 | * respectively. |
| 44 | * |
Heinrich Schuchardt | e2c43da | 2020-05-03 16:29:00 +0200 | [diff] [blame] | 45 | * Return: status code |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 46 | */ |
Heinrich Schuchardt | 1ad2f0d | 2021-09-09 07:12:14 +0200 | [diff] [blame] | 47 | static efi_status_t efi_variable_authenticate(const u16 *variable, |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 48 | const efi_guid_t *vendor, |
| 49 | efi_uintn_t *data_size, |
| 50 | const void **data, u32 given_attr, |
| 51 | u32 *env_attr, u64 *time) |
| 52 | { |
| 53 | const struct efi_variable_authentication_2 *auth; |
| 54 | struct efi_signature_store *truststore, *truststore2; |
| 55 | struct pkcs7_message *var_sig; |
| 56 | struct efi_image_regions *regs; |
| 57 | struct efi_time timestamp; |
| 58 | struct rtc_time tm; |
| 59 | u64 new_time; |
AKASHI Takahiro | 1bb6edf | 2020-08-12 09:37:50 +0900 | [diff] [blame] | 60 | u8 *ebuf; |
Heinrich Schuchardt | 3a28033 | 2020-07-15 12:40:35 +0200 | [diff] [blame] | 61 | enum efi_auth_var_type var_type; |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 62 | efi_status_t ret; |
| 63 | |
| 64 | var_sig = NULL; |
| 65 | truststore = NULL; |
| 66 | truststore2 = NULL; |
| 67 | regs = NULL; |
AKASHI Takahiro | 1bb6edf | 2020-08-12 09:37:50 +0900 | [diff] [blame] | 68 | ebuf = NULL; |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 69 | ret = EFI_SECURITY_VIOLATION; |
| 70 | |
| 71 | if (*data_size < sizeof(struct efi_variable_authentication_2)) |
| 72 | goto err; |
| 73 | |
| 74 | /* authentication data */ |
| 75 | auth = *data; |
| 76 | if (*data_size < (sizeof(auth->time_stamp) |
| 77 | + auth->auth_info.hdr.dwLength)) |
| 78 | goto err; |
| 79 | |
| 80 | if (guidcmp(&auth->auth_info.cert_type, &efi_guid_cert_type_pkcs7)) |
| 81 | goto err; |
| 82 | |
Heinrich Schuchardt | 7a76a3f | 2020-07-01 12:44:00 +0200 | [diff] [blame] | 83 | memcpy(×tamp, &auth->time_stamp, sizeof(timestamp)); |
| 84 | if (timestamp.pad1 || timestamp.nanosecond || timestamp.timezone || |
| 85 | timestamp.daylight || timestamp.pad2) |
| 86 | goto err; |
| 87 | |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 88 | *data += sizeof(auth->time_stamp) + auth->auth_info.hdr.dwLength; |
| 89 | *data_size -= (sizeof(auth->time_stamp) |
| 90 | + auth->auth_info.hdr.dwLength); |
| 91 | |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 92 | memset(&tm, 0, sizeof(tm)); |
| 93 | tm.tm_year = timestamp.year; |
| 94 | tm.tm_mon = timestamp.month; |
| 95 | tm.tm_mday = timestamp.day; |
| 96 | tm.tm_hour = timestamp.hour; |
| 97 | tm.tm_min = timestamp.minute; |
| 98 | tm.tm_sec = timestamp.second; |
| 99 | new_time = rtc_mktime(&tm); |
| 100 | |
| 101 | if (!efi_secure_boot_enabled()) { |
| 102 | /* finished checking */ |
| 103 | *time = new_time; |
| 104 | return EFI_SUCCESS; |
| 105 | } |
| 106 | |
| 107 | if (new_time <= *time) |
| 108 | goto err; |
| 109 | |
| 110 | /* data to be digested */ |
| 111 | regs = calloc(sizeof(*regs) + sizeof(struct image_region) * 5, 1); |
| 112 | if (!regs) |
| 113 | goto err; |
| 114 | regs->max = 5; |
| 115 | efi_image_region_add(regs, (uint8_t *)variable, |
| 116 | (uint8_t *)variable |
| 117 | + u16_strlen(variable) * sizeof(u16), 1); |
| 118 | efi_image_region_add(regs, (uint8_t *)vendor, |
| 119 | (uint8_t *)vendor + sizeof(*vendor), 1); |
| 120 | efi_image_region_add(regs, (uint8_t *)&given_attr, |
| 121 | (uint8_t *)&given_attr + sizeof(given_attr), 1); |
| 122 | efi_image_region_add(regs, (uint8_t *)×tamp, |
| 123 | (uint8_t *)×tamp + sizeof(timestamp), 1); |
| 124 | efi_image_region_add(regs, (uint8_t *)*data, |
| 125 | (uint8_t *)*data + *data_size, 1); |
| 126 | |
| 127 | /* variable's signature list */ |
| 128 | if (auth->auth_info.hdr.dwLength < sizeof(auth->auth_info)) |
| 129 | goto err; |
AKASHI Takahiro | 1bb6edf | 2020-08-12 09:37:50 +0900 | [diff] [blame] | 130 | |
| 131 | /* ebuf should be kept valid during the authentication */ |
Sughosh Ganu | ea35008 | 2020-12-30 19:27:07 +0530 | [diff] [blame] | 132 | var_sig = efi_parse_pkcs7_header(auth->auth_info.cert_data, |
| 133 | auth->auth_info.hdr.dwLength |
| 134 | - sizeof(auth->auth_info), |
| 135 | &ebuf); |
Patrick Wildt | cd8a55b | 2020-05-07 02:13:18 +0200 | [diff] [blame] | 136 | if (!var_sig) { |
AKASHI Takahiro | 3712373 | 2020-06-09 14:09:34 +0900 | [diff] [blame] | 137 | EFI_PRINT("Parsing variable's signature failed\n"); |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 138 | goto err; |
| 139 | } |
| 140 | |
| 141 | /* signature database used for authentication */ |
Heinrich Schuchardt | 3a28033 | 2020-07-15 12:40:35 +0200 | [diff] [blame] | 142 | var_type = efi_auth_var_get_type(variable, vendor); |
| 143 | switch (var_type) { |
| 144 | case EFI_AUTH_VAR_PK: |
| 145 | case EFI_AUTH_VAR_KEK: |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 146 | /* with PK */ |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 147 | truststore = efi_sigstore_parse_sigdb(u"PK"); |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 148 | if (!truststore) |
| 149 | goto err; |
Heinrich Schuchardt | 3a28033 | 2020-07-15 12:40:35 +0200 | [diff] [blame] | 150 | break; |
| 151 | case EFI_AUTH_VAR_DB: |
| 152 | case EFI_AUTH_VAR_DBX: |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 153 | /* with PK and KEK */ |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 154 | truststore = efi_sigstore_parse_sigdb(u"KEK"); |
| 155 | truststore2 = efi_sigstore_parse_sigdb(u"PK"); |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 156 | if (!truststore) { |
| 157 | if (!truststore2) |
| 158 | goto err; |
| 159 | |
| 160 | truststore = truststore2; |
| 161 | truststore2 = NULL; |
| 162 | } |
Heinrich Schuchardt | 3a28033 | 2020-07-15 12:40:35 +0200 | [diff] [blame] | 163 | break; |
| 164 | default: |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 165 | /* TODO: support private authenticated variables */ |
| 166 | goto err; |
| 167 | } |
| 168 | |
| 169 | /* verify signature */ |
AKASHI Takahiro | 14afd06 | 2020-07-21 19:35:22 +0900 | [diff] [blame] | 170 | if (efi_signature_verify(regs, var_sig, truststore, NULL)) { |
AKASHI Takahiro | 3712373 | 2020-06-09 14:09:34 +0900 | [diff] [blame] | 171 | EFI_PRINT("Verified\n"); |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 172 | } else { |
| 173 | if (truststore2 && |
AKASHI Takahiro | 14afd06 | 2020-07-21 19:35:22 +0900 | [diff] [blame] | 174 | efi_signature_verify(regs, var_sig, truststore2, NULL)) { |
AKASHI Takahiro | 3712373 | 2020-06-09 14:09:34 +0900 | [diff] [blame] | 175 | EFI_PRINT("Verified\n"); |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 176 | } else { |
AKASHI Takahiro | 3712373 | 2020-06-09 14:09:34 +0900 | [diff] [blame] | 177 | EFI_PRINT("Verifying variable's signature failed\n"); |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 178 | goto err; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | /* finished checking */ |
Heinrich Schuchardt | 5106dc7 | 2020-06-30 12:02:14 +0200 | [diff] [blame] | 183 | *time = new_time; |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 184 | ret = EFI_SUCCESS; |
| 185 | |
| 186 | err: |
| 187 | efi_sigstore_free(truststore); |
| 188 | efi_sigstore_free(truststore2); |
| 189 | pkcs7_free_message(var_sig); |
AKASHI Takahiro | 1bb6edf | 2020-08-12 09:37:50 +0900 | [diff] [blame] | 190 | free(ebuf); |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 191 | free(regs); |
| 192 | |
| 193 | return ret; |
| 194 | } |
| 195 | #else |
Heinrich Schuchardt | 1ad2f0d | 2021-09-09 07:12:14 +0200 | [diff] [blame] | 196 | static efi_status_t efi_variable_authenticate(const u16 *variable, |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 197 | const efi_guid_t *vendor, |
| 198 | efi_uintn_t *data_size, |
| 199 | const void **data, u32 given_attr, |
| 200 | u32 *env_attr, u64 *time) |
| 201 | { |
| 202 | return EFI_SUCCESS; |
| 203 | } |
| 204 | #endif /* CONFIG_EFI_SECURE_BOOT */ |
| 205 | |
Heinrich Schuchardt | 047276c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 206 | efi_status_t __efi_runtime |
Heinrich Schuchardt | 1ad2f0d | 2021-09-09 07:12:14 +0200 | [diff] [blame] | 207 | efi_get_variable_int(const u16 *variable_name, const efi_guid_t *vendor, |
Heinrich Schuchardt | 047276c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 208 | u32 *attributes, efi_uintn_t *data_size, void *data, |
| 209 | u64 *timep) |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 210 | { |
Ilias Apalodimas | a4d1b1b | 2020-07-23 15:49:49 +0300 | [diff] [blame] | 211 | return efi_get_variable_mem(variable_name, vendor, attributes, data_size, data, timep); |
AKASHI Takahiro | ad3f36c | 2019-01-21 12:43:13 +0100 | [diff] [blame] | 212 | } |
| 213 | |
Heinrich Schuchardt | 047276c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 214 | efi_status_t __efi_runtime |
| 215 | efi_get_next_variable_name_int(efi_uintn_t *variable_name_size, |
| 216 | u16 *variable_name, efi_guid_t *vendor) |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 217 | { |
Ilias Apalodimas | a4d1b1b | 2020-07-23 15:49:49 +0300 | [diff] [blame] | 218 | return efi_get_next_variable_name_mem(variable_name_size, variable_name, vendor); |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 219 | } |
| 220 | |
Heinrich Schuchardt | 1ad2f0d | 2021-09-09 07:12:14 +0200 | [diff] [blame] | 221 | efi_status_t efi_set_variable_int(const u16 *variable_name, |
| 222 | const efi_guid_t *vendor, |
Heinrich Schuchardt | 9827e84 | 2020-06-22 18:10:27 +0200 | [diff] [blame] | 223 | u32 attributes, efi_uintn_t data_size, |
| 224 | const void *data, bool ro_check) |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 225 | { |
Heinrich Schuchardt | 047276c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 226 | struct efi_var_entry *var; |
| 227 | efi_uintn_t ret; |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 228 | bool append, delete; |
| 229 | u64 time = 0; |
Heinrich Schuchardt | 3a28033 | 2020-07-15 12:40:35 +0200 | [diff] [blame] | 230 | enum efi_auth_var_type var_type; |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 231 | |
Masahisa Kojima | fa419cf | 2023-02-21 11:33:17 +0900 | [diff] [blame] | 232 | if (!variable_name || !*variable_name || !vendor) |
| 233 | return EFI_INVALID_PARAMETER; |
| 234 | |
| 235 | if (data_size && !data) |
| 236 | return EFI_INVALID_PARAMETER; |
| 237 | |
| 238 | /* EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS is deprecated */ |
| 239 | if (attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) |
| 240 | return EFI_UNSUPPORTED; |
| 241 | |
| 242 | /* Make sure if runtime bit is set, boot service bit is set also */ |
| 243 | if ((attributes & |
| 244 | (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == |
| 245 | EFI_VARIABLE_RUNTIME_ACCESS) |
| 246 | return EFI_INVALID_PARAMETER; |
| 247 | |
| 248 | /* only EFI_VARIABLE_NON_VOLATILE attribute is invalid */ |
| 249 | if ((attributes & EFI_VARIABLE_MASK) == EFI_VARIABLE_NON_VOLATILE) |
| 250 | return EFI_INVALID_PARAMETER; |
| 251 | |
| 252 | /* Make sure HR is set with NV, BS and RT */ |
| 253 | if (attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD && |
| 254 | (!(attributes & EFI_VARIABLE_NON_VOLATILE) || |
| 255 | !(attributes & EFI_VARIABLE_RUNTIME_ACCESS) || |
Heinrich Schuchardt | 047276c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 256 | !(attributes & EFI_VARIABLE_BOOTSERVICE_ACCESS))) |
| 257 | return EFI_INVALID_PARAMETER; |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 258 | |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 259 | /* check if a variable exists */ |
Heinrich Schuchardt | 047276c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 260 | var = efi_var_mem_find(vendor, variable_name, NULL); |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 261 | append = !!(attributes & EFI_VARIABLE_APPEND_WRITE); |
| 262 | attributes &= ~(u32)EFI_VARIABLE_APPEND_WRITE; |
| 263 | delete = !append && (!data_size || !attributes); |
| 264 | |
| 265 | /* check attributes */ |
Heinrich Schuchardt | 4b7d5c1 | 2020-07-14 21:25:28 +0200 | [diff] [blame] | 266 | var_type = efi_auth_var_get_type(variable_name, vendor); |
Heinrich Schuchardt | 047276c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 267 | if (var) { |
| 268 | if (ro_check && (var->attr & EFI_VARIABLE_READ_ONLY)) |
| 269 | return EFI_WRITE_PROTECTED; |
AKASHI Takahiro | e5023b7 | 2019-09-06 15:09:52 +0900 | [diff] [blame] | 270 | |
Heinrich Schuchardt | 4b7d5c1 | 2020-07-14 21:25:28 +0200 | [diff] [blame] | 271 | if (IS_ENABLED(CONFIG_EFI_VARIABLES_PRESEED)) { |
Heinrich Schuchardt | 65b616f | 2021-08-26 04:30:24 +0200 | [diff] [blame] | 272 | if (var_type >= EFI_AUTH_VAR_PK) |
Heinrich Schuchardt | 4b7d5c1 | 2020-07-14 21:25:28 +0200 | [diff] [blame] | 273 | return EFI_WRITE_PROTECTED; |
| 274 | } |
| 275 | |
AKASHI Takahiro | e5023b7 | 2019-09-06 15:09:52 +0900 | [diff] [blame] | 276 | /* attributes won't be changed */ |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 277 | if (!delete && |
Heinrich Schuchardt | 047276c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 278 | ((ro_check && var->attr != attributes) || |
| 279 | (!ro_check && ((var->attr & ~(u32)EFI_VARIABLE_READ_ONLY) |
Heinrich Schuchardt | 9827e84 | 2020-06-22 18:10:27 +0200 | [diff] [blame] | 280 | != (attributes & ~(u32)EFI_VARIABLE_READ_ONLY))))) { |
Heinrich Schuchardt | 047276c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 281 | return EFI_INVALID_PARAMETER; |
AKASHI Takahiro | e5023b7 | 2019-09-06 15:09:52 +0900 | [diff] [blame] | 282 | } |
Heinrich Schuchardt | 047276c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 283 | time = var->time; |
AKASHI Takahiro | e5023b7 | 2019-09-06 15:09:52 +0900 | [diff] [blame] | 284 | } else { |
Heinrich Schuchardt | 047276c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 285 | if (delete || append) |
Heinrich Schuchardt | 8021bbe | 2019-09-26 21:40:18 +0200 | [diff] [blame] | 286 | /* |
| 287 | * Trying to delete or to update a non-existent |
| 288 | * variable. |
| 289 | */ |
Heinrich Schuchardt | 047276c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 290 | return EFI_NOT_FOUND; |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 291 | } |
AKASHI Takahiro | e5023b7 | 2019-09-06 15:09:52 +0900 | [diff] [blame] | 292 | |
Heinrich Schuchardt | 65b616f | 2021-08-26 04:30:24 +0200 | [diff] [blame] | 293 | if (var_type >= EFI_AUTH_VAR_PK) { |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 294 | /* authentication is mandatory */ |
| 295 | if (!(attributes & |
| 296 | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)) { |
Heinrich Schuchardt | 047276c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 297 | EFI_PRINT("%ls: TIME_BASED_AUTHENTICATED_WRITE_ACCESS required\n", |
AKASHI Takahiro | 3712373 | 2020-06-09 14:09:34 +0900 | [diff] [blame] | 298 | variable_name); |
Heinrich Schuchardt | 047276c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 299 | return EFI_INVALID_PARAMETER; |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 300 | } |
| 301 | } |
| 302 | |
| 303 | /* authenticate a variable */ |
| 304 | if (IS_ENABLED(CONFIG_EFI_SECURE_BOOT)) { |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 305 | if (attributes & |
| 306 | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) { |
Heinrich Schuchardt | 047276c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 307 | u32 env_attr; |
| 308 | |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 309 | ret = efi_variable_authenticate(variable_name, vendor, |
| 310 | &data_size, &data, |
Heinrich Schuchardt | 047276c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 311 | attributes, &env_attr, |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 312 | &time); |
| 313 | if (ret != EFI_SUCCESS) |
Heinrich Schuchardt | 047276c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 314 | return ret; |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 315 | |
| 316 | /* last chance to check for delete */ |
| 317 | if (!data_size) |
| 318 | delete = true; |
| 319 | } |
| 320 | } else { |
| 321 | if (attributes & |
Masahisa Kojima | fa419cf | 2023-02-21 11:33:17 +0900 | [diff] [blame] | 322 | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) { |
AKASHI Takahiro | 3712373 | 2020-06-09 14:09:34 +0900 | [diff] [blame] | 323 | EFI_PRINT("Secure boot is not configured\n"); |
Heinrich Schuchardt | 047276c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 324 | return EFI_INVALID_PARAMETER; |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 325 | } |
| 326 | } |
| 327 | |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 328 | if (delete) { |
Heinrich Schuchardt | 047276c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 329 | /* EFI_NOT_FOUND has been handled before */ |
Heinrich Schuchardt | 0f7a680 | 2020-11-02 19:32:24 +0100 | [diff] [blame] | 330 | attributes = var->attr; |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 331 | ret = EFI_SUCCESS; |
Heinrich Schuchardt | 047276c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 332 | } else if (append) { |
| 333 | u16 *old_data = var->name; |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 334 | |
Heinrich Schuchardt | 047276c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 335 | for (; *old_data; ++old_data) |
| 336 | ; |
| 337 | ++old_data; |
| 338 | ret = efi_var_mem_ins(variable_name, vendor, attributes, |
| 339 | var->length, old_data, data_size, data, |
| 340 | time); |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 341 | } else { |
Heinrich Schuchardt | 047276c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 342 | ret = efi_var_mem_ins(variable_name, vendor, attributes, |
| 343 | data_size, data, 0, NULL, time); |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 344 | } |
Heinrich Schuchardt | 047276c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 345 | efi_var_mem_del(var); |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 346 | |
Heinrich Schuchardt | 047276c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 347 | if (ret != EFI_SUCCESS) |
| 348 | return ret; |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 349 | |
Heinrich Schuchardt | 3a28033 | 2020-07-15 12:40:35 +0200 | [diff] [blame] | 350 | if (var_type == EFI_AUTH_VAR_PK) |
Heinrich Schuchardt | 047276c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 351 | ret = efi_init_secure_state(); |
| 352 | else |
| 353 | ret = EFI_SUCCESS; |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 354 | |
Heinrich Schuchardt | 09bc8e0 | 2023-01-19 14:49:33 +0100 | [diff] [blame] | 355 | /* |
| 356 | * Write non-volatile EFI variables to file |
| 357 | * TODO: check if a value change has occured to avoid superfluous writes |
| 358 | */ |
| 359 | if (attributes & EFI_VARIABLE_NON_VOLATILE) |
Heinrich Schuchardt | 09a8d50 | 2020-03-19 18:21:58 +0000 | [diff] [blame] | 360 | efi_var_to_file(); |
| 361 | |
Heinrich Schuchardt | 047276c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 362 | return EFI_SUCCESS; |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 363 | } |
| 364 | |
Heinrich Schuchardt | 276c61d | 2020-06-26 17:57:48 +0200 | [diff] [blame] | 365 | efi_status_t efi_query_variable_info_int(u32 attributes, |
| 366 | u64 *maximum_variable_storage_size, |
| 367 | u64 *remaining_variable_storage_size, |
| 368 | u64 *maximum_variable_size) |
| 369 | { |
Masahisa Kojima | 1fd781f | 2023-02-02 22:53:35 +0900 | [diff] [blame] | 370 | if (attributes == 0) |
| 371 | return EFI_INVALID_PARAMETER; |
| 372 | |
| 373 | /* EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS is deprecated */ |
| 374 | if ((attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) || |
| 375 | ((attributes & EFI_VARIABLE_MASK) == 0)) |
| 376 | return EFI_UNSUPPORTED; |
| 377 | |
| 378 | if ((attributes & EFI_VARIABLE_MASK) == EFI_VARIABLE_NON_VOLATILE) |
| 379 | return EFI_INVALID_PARAMETER; |
| 380 | |
| 381 | /* Make sure if runtime bit is set, boot service bit is set also. */ |
| 382 | if ((attributes & |
| 383 | (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == |
| 384 | EFI_VARIABLE_RUNTIME_ACCESS) |
| 385 | return EFI_INVALID_PARAMETER; |
| 386 | |
Masahisa Kojima | 1fd781f | 2023-02-02 22:53:35 +0900 | [diff] [blame] | 387 | if (attributes & ~(u32)EFI_VARIABLE_MASK) |
| 388 | return EFI_INVALID_PARAMETER; |
| 389 | |
Heinrich Schuchardt | 047276c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 390 | *maximum_variable_storage_size = EFI_VAR_BUF_SIZE - |
| 391 | sizeof(struct efi_var_file); |
| 392 | *remaining_variable_storage_size = efi_var_mem_free(); |
| 393 | *maximum_variable_size = EFI_VAR_BUF_SIZE - |
| 394 | sizeof(struct efi_var_file) - |
| 395 | sizeof(struct efi_var_entry); |
| 396 | return EFI_SUCCESS; |
Heinrich Schuchardt | 276c61d | 2020-06-26 17:57:48 +0200 | [diff] [blame] | 397 | } |
| 398 | |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 399 | /** |
Heinrich Schuchardt | 276c61d | 2020-06-26 17:57:48 +0200 | [diff] [blame] | 400 | * efi_query_variable_info_runtime() - runtime implementation of |
| 401 | * QueryVariableInfo() |
Heinrich Schuchardt | 9b19dae | 2019-06-20 12:13:05 +0200 | [diff] [blame] | 402 | * |
| 403 | * @attributes: bitmask to select variables to be |
| 404 | * queried |
| 405 | * @maximum_variable_storage_size: maximum size of storage area for the |
| 406 | * selected variable types |
| 407 | * @remaining_variable_storage_size: remaining size of storage are for the |
| 408 | * selected variable types |
| 409 | * @maximum_variable_size: maximum size of a variable of the |
| 410 | * selected type |
| 411 | * Returns: status code |
| 412 | */ |
Heinrich Schuchardt | 5c34604 | 2023-02-10 09:06:17 +0100 | [diff] [blame] | 413 | static efi_status_t __efi_runtime EFIAPI efi_query_variable_info_runtime( |
Heinrich Schuchardt | 9b19dae | 2019-06-20 12:13:05 +0200 | [diff] [blame] | 414 | u32 attributes, |
| 415 | u64 *maximum_variable_storage_size, |
| 416 | u64 *remaining_variable_storage_size, |
| 417 | u64 *maximum_variable_size) |
| 418 | { |
| 419 | return EFI_UNSUPPORTED; |
| 420 | } |
Heinrich Schuchardt | cf3b118 | 2019-06-20 13:52:16 +0200 | [diff] [blame] | 421 | |
| 422 | /** |
Heinrich Schuchardt | 2ac6258 | 2019-06-20 15:25:48 +0200 | [diff] [blame] | 423 | * efi_set_variable_runtime() - runtime implementation of SetVariable() |
Heinrich Schuchardt | ee1f0e4 | 2019-07-14 12:11:16 +0200 | [diff] [blame] | 424 | * |
| 425 | * @variable_name: name of the variable |
| 426 | * @vendor: vendor GUID |
| 427 | * @attributes: attributes of the variable |
| 428 | * @data_size: size of the buffer with the variable value |
| 429 | * @data: buffer with the variable value |
| 430 | * Return: status code |
Heinrich Schuchardt | 2ac6258 | 2019-06-20 15:25:48 +0200 | [diff] [blame] | 431 | */ |
| 432 | static efi_status_t __efi_runtime EFIAPI |
| 433 | efi_set_variable_runtime(u16 *variable_name, const efi_guid_t *vendor, |
| 434 | u32 attributes, efi_uintn_t data_size, |
| 435 | const void *data) |
| 436 | { |
| 437 | return EFI_UNSUPPORTED; |
| 438 | } |
| 439 | |
| 440 | /** |
| 441 | * efi_variables_boot_exit_notify() - notify ExitBootServices() is called |
| 442 | */ |
| 443 | void efi_variables_boot_exit_notify(void) |
| 444 | { |
Heinrich Schuchardt | 09a8d50 | 2020-03-19 18:21:58 +0000 | [diff] [blame] | 445 | /* Switch variable services functions to runtime version */ |
Heinrich Schuchardt | 2ac6258 | 2019-06-20 15:25:48 +0200 | [diff] [blame] | 446 | efi_runtime_services.get_variable = efi_get_variable_runtime; |
| 447 | efi_runtime_services.get_next_variable_name = |
| 448 | efi_get_next_variable_name_runtime; |
| 449 | efi_runtime_services.set_variable = efi_set_variable_runtime; |
Heinrich Schuchardt | 276c61d | 2020-06-26 17:57:48 +0200 | [diff] [blame] | 450 | efi_runtime_services.query_variable_info = |
| 451 | efi_query_variable_info_runtime; |
Heinrich Schuchardt | 2ac6258 | 2019-06-20 15:25:48 +0200 | [diff] [blame] | 452 | efi_update_table_header_crc32(&efi_runtime_services.hdr); |
| 453 | } |
| 454 | |
| 455 | /** |
Heinrich Schuchardt | cf3b118 | 2019-06-20 13:52:16 +0200 | [diff] [blame] | 456 | * efi_init_variables() - initialize variable services |
| 457 | * |
| 458 | * Return: status code |
| 459 | */ |
| 460 | efi_status_t efi_init_variables(void) |
| 461 | { |
AKASHI Takahiro | c78dc19 | 2020-04-14 11:51:42 +0900 | [diff] [blame] | 462 | efi_status_t ret; |
| 463 | |
Heinrich Schuchardt | 047276c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 464 | ret = efi_var_mem_init(); |
| 465 | if (ret != EFI_SUCCESS) |
| 466 | return ret; |
| 467 | |
Ilias Apalodimas | 3a83082 | 2022-12-29 10:13:22 +0200 | [diff] [blame] | 468 | ret = efi_var_from_file(); |
| 469 | if (ret != EFI_SUCCESS) |
| 470 | return ret; |
Heinrich Schuchardt | 4b7d5c1 | 2020-07-14 21:25:28 +0200 | [diff] [blame] | 471 | if (IS_ENABLED(CONFIG_EFI_VARIABLES_PRESEED)) { |
| 472 | ret = efi_var_restore((struct efi_var_file *) |
Heinrich Schuchardt | 211317c | 2021-08-25 19:13:24 +0200 | [diff] [blame] | 473 | __efi_var_file_begin, true); |
Heinrich Schuchardt | 4b7d5c1 | 2020-07-14 21:25:28 +0200 | [diff] [blame] | 474 | if (ret != EFI_SUCCESS) |
| 475 | log_err("Invalid EFI variable seed\n"); |
| 476 | } |
| 477 | |
AKASHI Takahiro | 2e0d94a | 2020-08-13 17:05:29 +0900 | [diff] [blame] | 478 | |
| 479 | return efi_init_secure_state(); |
Heinrich Schuchardt | cf3b118 | 2019-06-20 13:52:16 +0200 | [diff] [blame] | 480 | } |