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 | 147d5d3 | 2019-10-26 23:53:48 +0200 | [diff] [blame] | 8 | #include <common.h> |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 9 | #include <efi_loader.h> |
Simon Glass | 9d1f619 | 2019-08-02 09:44:25 -0600 | [diff] [blame] | 10 | #include <env_internal.h> |
Heinrich Schuchardt | 147d5d3 | 2019-10-26 23:53:48 +0200 | [diff] [blame] | 11 | #include <hexdump.h> |
| 12 | #include <malloc.h> |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 13 | #include <rtc.h> |
AKASHI Takahiro | ad3f36c | 2019-01-21 12:43:13 +0100 | [diff] [blame] | 14 | #include <search.h> |
AKASHI Takahiro | 6ec6767 | 2020-04-21 09:38:17 +0900 | [diff] [blame] | 15 | #include <crypto/pkcs7_parser.h> |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 16 | #include <linux/compat.h> |
Simon Glass | 48b6c6b | 2019-11-14 12:57:16 -0700 | [diff] [blame] | 17 | #include <u-boot/crc.h> |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 18 | |
AKASHI Takahiro | c78dc19 | 2020-04-14 11:51:42 +0900 | [diff] [blame] | 19 | enum efi_secure_mode { |
| 20 | EFI_MODE_SETUP, |
| 21 | EFI_MODE_USER, |
| 22 | EFI_MODE_AUDIT, |
| 23 | EFI_MODE_DEPLOYED, |
| 24 | }; |
| 25 | |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 26 | const efi_guid_t efi_guid_cert_type_pkcs7 = EFI_CERT_TYPE_PKCS7_GUID; |
| 27 | static bool efi_secure_boot; |
AKASHI Takahiro | c78dc19 | 2020-04-14 11:51:42 +0900 | [diff] [blame] | 28 | static int efi_secure_mode; |
AKASHI Takahiro | 94b139a | 2020-04-14 11:51:43 +0900 | [diff] [blame] | 29 | static u8 efi_vendor_keys; |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 30 | |
| 31 | #define READ_ONLY BIT(31) |
| 32 | |
Heinrich Schuchardt | da0a791 | 2020-05-06 01:51:04 +0200 | [diff] [blame] | 33 | static efi_status_t efi_get_variable_common(u16 *variable_name, |
| 34 | const efi_guid_t *vendor, |
| 35 | u32 *attributes, |
| 36 | efi_uintn_t *data_size, void *data); |
| 37 | |
| 38 | static efi_status_t efi_set_variable_common(u16 *variable_name, |
| 39 | const efi_guid_t *vendor, |
| 40 | u32 attributes, |
| 41 | efi_uintn_t data_size, |
| 42 | const void *data, |
| 43 | bool ro_check); |
| 44 | |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 45 | /* |
| 46 | * Mapping between EFI variables and u-boot variables: |
| 47 | * |
| 48 | * efi_$guid_$varname = {attributes}(type)value |
| 49 | * |
| 50 | * For example: |
| 51 | * |
| 52 | * efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_OsIndicationsSupported= |
| 53 | * "{ro,boot,run}(blob)0000000000000000" |
| 54 | * efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_BootOrder= |
| 55 | * "(blob)00010000" |
| 56 | * |
| 57 | * The attributes are a comma separated list of these possible |
| 58 | * attributes: |
| 59 | * |
| 60 | * + ro - read-only |
| 61 | * + boot - boot-services access |
| 62 | * + run - runtime access |
| 63 | * |
| 64 | * NOTE: with current implementation, no variables are available after |
| 65 | * ExitBootServices, and all are persisted (if possible). |
| 66 | * |
| 67 | * If not specified, the attributes default to "{boot}". |
| 68 | * |
| 69 | * The required type is one of: |
| 70 | * |
| 71 | * + utf8 - raw utf8 string |
| 72 | * + blob - arbitrary length hex string |
| 73 | * |
| 74 | * Maybe a utf16 type would be useful to for a string value to be auto |
| 75 | * converted to utf16? |
| 76 | */ |
| 77 | |
Heinrich Schuchardt | 44389cb | 2018-09-23 04:08:09 +0200 | [diff] [blame] | 78 | #define PREFIX_LEN (strlen("efi_xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx_")) |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 79 | |
Heinrich Schuchardt | 0ffda47 | 2019-01-18 19:52:05 +0100 | [diff] [blame] | 80 | /** |
| 81 | * efi_to_native() - convert the UEFI variable name and vendor GUID to U-Boot |
| 82 | * variable name |
| 83 | * |
| 84 | * The U-Boot variable name is a concatenation of prefix 'efi', the hexstring |
| 85 | * encoded vendor GUID, and the UTF-8 encoded UEFI variable name separated by |
| 86 | * underscores, e.g. 'efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_BootOrder'. |
| 87 | * |
| 88 | * @native: pointer to pointer to U-Boot variable name |
| 89 | * @variable_name: UEFI variable name |
| 90 | * @vendor: vendor GUID |
| 91 | * Return: status code |
| 92 | */ |
Heinrich Schuchardt | af1a920 | 2018-08-31 21:31:31 +0200 | [diff] [blame] | 93 | static efi_status_t efi_to_native(char **native, const u16 *variable_name, |
Heinrich Schuchardt | e06ae19 | 2018-12-30 20:53:51 +0100 | [diff] [blame] | 94 | const efi_guid_t *vendor) |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 95 | { |
| 96 | size_t len; |
Heinrich Schuchardt | af1a920 | 2018-08-31 21:31:31 +0200 | [diff] [blame] | 97 | char *pos; |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 98 | |
Heinrich Schuchardt | af1a920 | 2018-08-31 21:31:31 +0200 | [diff] [blame] | 99 | len = PREFIX_LEN + utf16_utf8_strlen(variable_name) + 1; |
| 100 | *native = malloc(len); |
| 101 | if (!*native) |
| 102 | return EFI_OUT_OF_RESOURCES; |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 103 | |
Heinrich Schuchardt | af1a920 | 2018-08-31 21:31:31 +0200 | [diff] [blame] | 104 | pos = *native; |
| 105 | pos += sprintf(pos, "efi_%pUl_", vendor); |
| 106 | utf16_utf8_strcpy(&pos, variable_name); |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 107 | |
| 108 | return EFI_SUCCESS; |
| 109 | } |
| 110 | |
Heinrich Schuchardt | 0ffda47 | 2019-01-18 19:52:05 +0100 | [diff] [blame] | 111 | /** |
| 112 | * prefix() - skip over prefix |
| 113 | * |
| 114 | * Skip over a prefix string. |
| 115 | * |
| 116 | * @str: string with prefix |
| 117 | * @prefix: prefix string |
| 118 | * Return: string without prefix, or NULL if prefix not found |
| 119 | */ |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 120 | static const char *prefix(const char *str, const char *prefix) |
| 121 | { |
| 122 | size_t n = strlen(prefix); |
| 123 | if (!strncmp(prefix, str, n)) |
| 124 | return str + n; |
| 125 | return NULL; |
| 126 | } |
| 127 | |
Heinrich Schuchardt | 0ffda47 | 2019-01-18 19:52:05 +0100 | [diff] [blame] | 128 | /** |
| 129 | * parse_attr() - decode attributes part of variable value |
| 130 | * |
| 131 | * Convert the string encoded attributes of a UEFI variable to a bit mask. |
| 132 | * TODO: Several attributes are not supported. |
| 133 | * |
| 134 | * @str: value of U-Boot variable |
| 135 | * @attrp: pointer to UEFI attributes |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 136 | * @timep: pointer to time attribute |
Heinrich Schuchardt | 0ffda47 | 2019-01-18 19:52:05 +0100 | [diff] [blame] | 137 | * Return: pointer to remainder of U-Boot variable value |
| 138 | */ |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 139 | static const char *parse_attr(const char *str, u32 *attrp, u64 *timep) |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 140 | { |
| 141 | u32 attr = 0; |
| 142 | char sep = '{'; |
| 143 | |
| 144 | if (*str != '{') { |
| 145 | *attrp = EFI_VARIABLE_BOOTSERVICE_ACCESS; |
| 146 | return str; |
| 147 | } |
| 148 | |
| 149 | while (*str == sep) { |
| 150 | const char *s; |
| 151 | |
| 152 | str++; |
| 153 | |
| 154 | if ((s = prefix(str, "ro"))) { |
| 155 | attr |= READ_ONLY; |
AKASHI Takahiro | 7f33433 | 2019-06-04 15:52:06 +0900 | [diff] [blame] | 156 | } else if ((s = prefix(str, "nv"))) { |
| 157 | attr |= EFI_VARIABLE_NON_VOLATILE; |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 158 | } else if ((s = prefix(str, "boot"))) { |
| 159 | attr |= EFI_VARIABLE_BOOTSERVICE_ACCESS; |
| 160 | } else if ((s = prefix(str, "run"))) { |
| 161 | attr |= EFI_VARIABLE_RUNTIME_ACCESS; |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 162 | } else if ((s = prefix(str, "time="))) { |
| 163 | attr |= EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS; |
| 164 | hex2bin((u8 *)timep, s, sizeof(*timep)); |
| 165 | s += sizeof(*timep) * 2; |
| 166 | } else if (*str == '}') { |
| 167 | break; |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 168 | } else { |
| 169 | printf("invalid attribute: %s\n", str); |
| 170 | break; |
| 171 | } |
| 172 | |
| 173 | str = s; |
| 174 | sep = ','; |
| 175 | } |
| 176 | |
| 177 | str++; |
| 178 | |
| 179 | *attrp = attr; |
| 180 | |
| 181 | return str; |
| 182 | } |
| 183 | |
AKASHI Takahiro | c78dc19 | 2020-04-14 11:51:42 +0900 | [diff] [blame] | 184 | /** |
AKASHI Takahiro | dae117a | 2020-04-21 09:39:20 +0900 | [diff] [blame] | 185 | * efi_set_secure_state - modify secure boot state variables |
| 186 | * @sec_boot: value of SecureBoot |
| 187 | * @setup_mode: value of SetupMode |
| 188 | * @audit_mode: value of AuditMode |
| 189 | * @deployed_mode: value of DeployedMode |
| 190 | * |
| 191 | * Modify secure boot stat-related variables as indicated. |
| 192 | * |
| 193 | * Return: status code |
| 194 | */ |
| 195 | static efi_status_t efi_set_secure_state(int sec_boot, int setup_mode, |
| 196 | int audit_mode, int deployed_mode) |
| 197 | { |
| 198 | u32 attributes; |
| 199 | efi_status_t ret; |
| 200 | |
| 201 | attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 202 | EFI_VARIABLE_RUNTIME_ACCESS | |
| 203 | READ_ONLY; |
Heinrich Schuchardt | 37ab736 | 2020-05-03 10:02:20 +0200 | [diff] [blame] | 204 | ret = efi_set_variable_common(L"SecureBoot", &efi_global_variable_guid, |
| 205 | attributes, sizeof(sec_boot), &sec_boot, |
| 206 | false); |
AKASHI Takahiro | dae117a | 2020-04-21 09:39:20 +0900 | [diff] [blame] | 207 | if (ret != EFI_SUCCESS) |
| 208 | goto err; |
| 209 | |
Heinrich Schuchardt | 37ab736 | 2020-05-03 10:02:20 +0200 | [diff] [blame] | 210 | ret = efi_set_variable_common(L"SetupMode", &efi_global_variable_guid, |
| 211 | attributes, sizeof(setup_mode), |
| 212 | &setup_mode, false); |
AKASHI Takahiro | dae117a | 2020-04-21 09:39:20 +0900 | [diff] [blame] | 213 | if (ret != EFI_SUCCESS) |
| 214 | goto err; |
| 215 | |
Heinrich Schuchardt | 37ab736 | 2020-05-03 10:02:20 +0200 | [diff] [blame] | 216 | ret = efi_set_variable_common(L"AuditMode", &efi_global_variable_guid, |
| 217 | attributes, sizeof(audit_mode), |
| 218 | &audit_mode, false); |
AKASHI Takahiro | dae117a | 2020-04-21 09:39:20 +0900 | [diff] [blame] | 219 | if (ret != EFI_SUCCESS) |
| 220 | goto err; |
| 221 | |
Heinrich Schuchardt | 37ab736 | 2020-05-03 10:02:20 +0200 | [diff] [blame] | 222 | ret = efi_set_variable_common(L"DeployedMode", |
| 223 | &efi_global_variable_guid, attributes, |
| 224 | sizeof(deployed_mode), &deployed_mode, |
| 225 | false); |
AKASHI Takahiro | dae117a | 2020-04-21 09:39:20 +0900 | [diff] [blame] | 226 | err: |
| 227 | return ret; |
| 228 | } |
| 229 | |
| 230 | /** |
AKASHI Takahiro | c78dc19 | 2020-04-14 11:51:42 +0900 | [diff] [blame] | 231 | * efi_transfer_secure_state - handle a secure boot state transition |
| 232 | * @mode: new state |
| 233 | * |
| 234 | * Depending on @mode, secure boot related variables are updated. |
Heinrich Schuchardt | 37ab736 | 2020-05-03 10:02:20 +0200 | [diff] [blame] | 235 | * Those variables are *read-only* for users, efi_set_variable_common() |
AKASHI Takahiro | c78dc19 | 2020-04-14 11:51:42 +0900 | [diff] [blame] | 236 | * is called here. |
| 237 | * |
Heinrich Schuchardt | e2c43da | 2020-05-03 16:29:00 +0200 | [diff] [blame] | 238 | * Return: status code |
AKASHI Takahiro | c78dc19 | 2020-04-14 11:51:42 +0900 | [diff] [blame] | 239 | */ |
| 240 | static efi_status_t efi_transfer_secure_state(enum efi_secure_mode mode) |
| 241 | { |
AKASHI Takahiro | c78dc19 | 2020-04-14 11:51:42 +0900 | [diff] [blame] | 242 | efi_status_t ret; |
| 243 | |
AKASHI Takahiro | dae117a | 2020-04-21 09:39:20 +0900 | [diff] [blame] | 244 | debug("Switching secure state from %d to %d\n", efi_secure_mode, mode); |
AKASHI Takahiro | c78dc19 | 2020-04-14 11:51:42 +0900 | [diff] [blame] | 245 | |
AKASHI Takahiro | c78dc19 | 2020-04-14 11:51:42 +0900 | [diff] [blame] | 246 | if (mode == EFI_MODE_DEPLOYED) { |
AKASHI Takahiro | dae117a | 2020-04-21 09:39:20 +0900 | [diff] [blame] | 247 | ret = efi_set_secure_state(1, 0, 0, 1); |
AKASHI Takahiro | c78dc19 | 2020-04-14 11:51:42 +0900 | [diff] [blame] | 248 | if (ret != EFI_SUCCESS) |
| 249 | goto err; |
| 250 | |
| 251 | efi_secure_boot = true; |
| 252 | } else if (mode == EFI_MODE_AUDIT) { |
Heinrich Schuchardt | 37ab736 | 2020-05-03 10:02:20 +0200 | [diff] [blame] | 253 | ret = efi_set_variable_common(L"PK", &efi_global_variable_guid, |
| 254 | EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 255 | EFI_VARIABLE_RUNTIME_ACCESS, |
| 256 | 0, NULL, false); |
AKASHI Takahiro | c78dc19 | 2020-04-14 11:51:42 +0900 | [diff] [blame] | 257 | if (ret != EFI_SUCCESS) |
| 258 | goto err; |
AKASHI Takahiro | dae117a | 2020-04-21 09:39:20 +0900 | [diff] [blame] | 259 | |
| 260 | ret = efi_set_secure_state(0, 1, 1, 0); |
AKASHI Takahiro | c78dc19 | 2020-04-14 11:51:42 +0900 | [diff] [blame] | 261 | if (ret != EFI_SUCCESS) |
| 262 | goto err; |
| 263 | |
| 264 | efi_secure_boot = true; |
| 265 | } else if (mode == EFI_MODE_USER) { |
AKASHI Takahiro | dae117a | 2020-04-21 09:39:20 +0900 | [diff] [blame] | 266 | ret = efi_set_secure_state(1, 0, 0, 0); |
AKASHI Takahiro | c78dc19 | 2020-04-14 11:51:42 +0900 | [diff] [blame] | 267 | if (ret != EFI_SUCCESS) |
| 268 | goto err; |
AKASHI Takahiro | c78dc19 | 2020-04-14 11:51:42 +0900 | [diff] [blame] | 269 | |
| 270 | efi_secure_boot = true; |
| 271 | } else if (mode == EFI_MODE_SETUP) { |
AKASHI Takahiro | dae117a | 2020-04-21 09:39:20 +0900 | [diff] [blame] | 272 | ret = efi_set_secure_state(0, 1, 0, 0); |
AKASHI Takahiro | c78dc19 | 2020-04-14 11:51:42 +0900 | [diff] [blame] | 273 | if (ret != EFI_SUCCESS) |
| 274 | goto err; |
| 275 | } else { |
| 276 | return EFI_INVALID_PARAMETER; |
| 277 | } |
| 278 | |
AKASHI Takahiro | 94b139a | 2020-04-14 11:51:43 +0900 | [diff] [blame] | 279 | efi_secure_mode = mode; |
| 280 | |
AKASHI Takahiro | c78dc19 | 2020-04-14 11:51:42 +0900 | [diff] [blame] | 281 | return EFI_SUCCESS; |
| 282 | |
| 283 | err: |
| 284 | /* TODO: What action should be taken here? */ |
| 285 | printf("ERROR: Secure state transition failed\n"); |
| 286 | return ret; |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * efi_init_secure_state - initialize secure boot state |
| 291 | * |
Heinrich Schuchardt | e2c43da | 2020-05-03 16:29:00 +0200 | [diff] [blame] | 292 | * Return: status code |
AKASHI Takahiro | c78dc19 | 2020-04-14 11:51:42 +0900 | [diff] [blame] | 293 | */ |
| 294 | static efi_status_t efi_init_secure_state(void) |
| 295 | { |
AKASHI Takahiro | 94b139a | 2020-04-14 11:51:43 +0900 | [diff] [blame] | 296 | enum efi_secure_mode mode; |
| 297 | efi_uintn_t size; |
AKASHI Takahiro | c78dc19 | 2020-04-14 11:51:42 +0900 | [diff] [blame] | 298 | efi_status_t ret; |
| 299 | |
AKASHI Takahiro | 94b139a | 2020-04-14 11:51:43 +0900 | [diff] [blame] | 300 | /* |
| 301 | * TODO: |
| 302 | * Since there is currently no "platform-specific" installation |
| 303 | * method of Platform Key, we can't say if VendorKeys is 0 or 1 |
| 304 | * precisely. |
| 305 | */ |
| 306 | |
| 307 | size = 0; |
Heinrich Schuchardt | da0a791 | 2020-05-06 01:51:04 +0200 | [diff] [blame] | 308 | ret = efi_get_variable_common(L"PK", &efi_global_variable_guid, |
| 309 | NULL, &size, NULL); |
AKASHI Takahiro | 94b139a | 2020-04-14 11:51:43 +0900 | [diff] [blame] | 310 | if (ret == EFI_BUFFER_TOO_SMALL) { |
| 311 | if (IS_ENABLED(CONFIG_EFI_SECURE_BOOT)) |
| 312 | mode = EFI_MODE_USER; |
| 313 | else |
| 314 | mode = EFI_MODE_SETUP; |
AKASHI Takahiro | c78dc19 | 2020-04-14 11:51:42 +0900 | [diff] [blame] | 315 | |
AKASHI Takahiro | 94b139a | 2020-04-14 11:51:43 +0900 | [diff] [blame] | 316 | efi_vendor_keys = 0; |
| 317 | } else if (ret == EFI_NOT_FOUND) { |
| 318 | mode = EFI_MODE_SETUP; |
| 319 | efi_vendor_keys = 1; |
| 320 | } else { |
| 321 | goto err; |
| 322 | } |
| 323 | |
| 324 | ret = efi_transfer_secure_state(mode); |
| 325 | if (ret == EFI_SUCCESS) |
Heinrich Schuchardt | 37ab736 | 2020-05-03 10:02:20 +0200 | [diff] [blame] | 326 | ret = efi_set_variable_common(L"VendorKeys", |
| 327 | &efi_global_variable_guid, |
| 328 | EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 329 | EFI_VARIABLE_RUNTIME_ACCESS | |
| 330 | READ_ONLY, |
| 331 | sizeof(efi_vendor_keys), |
| 332 | &efi_vendor_keys, false); |
AKASHI Takahiro | 94b139a | 2020-04-14 11:51:43 +0900 | [diff] [blame] | 333 | |
| 334 | err: |
AKASHI Takahiro | c78dc19 | 2020-04-14 11:51:42 +0900 | [diff] [blame] | 335 | return ret; |
| 336 | } |
| 337 | |
Heinrich Schuchardt | 0ffda47 | 2019-01-18 19:52:05 +0100 | [diff] [blame] | 338 | /** |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 339 | * efi_secure_boot_enabled - return if secure boot is enabled or not |
Heinrich Schuchardt | 0ffda47 | 2019-01-18 19:52:05 +0100 | [diff] [blame] | 340 | * |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 341 | * Return: true if enabled, false if disabled |
| 342 | */ |
| 343 | bool efi_secure_boot_enabled(void) |
| 344 | { |
| 345 | return efi_secure_boot; |
| 346 | } |
| 347 | |
| 348 | #ifdef CONFIG_EFI_SECURE_BOOT |
| 349 | static u8 pkcs7_hdr[] = { |
| 350 | /* SEQUENCE */ |
| 351 | 0x30, 0x82, 0x05, 0xc7, |
| 352 | /* OID: pkcs7-signedData */ |
| 353 | 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x02, |
| 354 | /* Context Structured? */ |
| 355 | 0xa0, 0x82, 0x05, 0xb8, |
| 356 | }; |
| 357 | |
| 358 | /** |
| 359 | * efi_variable_parse_signature - parse a signature in variable |
| 360 | * @buf: Pointer to variable's value |
| 361 | * @buflen: Length of @buf |
Heinrich Schuchardt | 0ffda47 | 2019-01-18 19:52:05 +0100 | [diff] [blame] | 362 | * |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 363 | * Parse a signature embedded in variable's value and instantiate |
| 364 | * a pkcs7_message structure. Since pkcs7_parse_message() accepts only |
| 365 | * pkcs7's signedData, some header needed be prepended for correctly |
| 366 | * parsing authentication data, particularly for variable's. |
Heinrich Schuchardt | 0ffda47 | 2019-01-18 19:52:05 +0100 | [diff] [blame] | 367 | * |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 368 | * Return: Pointer to pkcs7_message structure on success, NULL on error |
Heinrich Schuchardt | 0ffda47 | 2019-01-18 19:52:05 +0100 | [diff] [blame] | 369 | */ |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 370 | static struct pkcs7_message *efi_variable_parse_signature(const void *buf, |
| 371 | size_t buflen) |
| 372 | { |
| 373 | u8 *ebuf; |
| 374 | size_t ebuflen, len; |
| 375 | struct pkcs7_message *msg; |
| 376 | |
| 377 | /* |
| 378 | * This is the best assumption to check if the binary is |
| 379 | * already in a form of pkcs7's signedData. |
| 380 | */ |
| 381 | if (buflen > sizeof(pkcs7_hdr) && |
| 382 | !memcmp(&((u8 *)buf)[4], &pkcs7_hdr[4], 11)) { |
| 383 | msg = pkcs7_parse_message(buf, buflen); |
| 384 | goto out; |
| 385 | } |
| 386 | |
| 387 | /* |
| 388 | * Otherwise, we should add a dummy prefix sequence for pkcs7 |
| 389 | * message parser to be able to process. |
| 390 | * NOTE: EDK2 also uses similar hack in WrapPkcs7Data() |
| 391 | * in CryptoPkg/Library/BaseCryptLib/Pk/CryptPkcs7VerifyCommon.c |
| 392 | * TODO: |
| 393 | * The header should be composed in a more refined manner. |
| 394 | */ |
| 395 | debug("Makeshift prefix added to authentication data\n"); |
| 396 | ebuflen = sizeof(pkcs7_hdr) + buflen; |
| 397 | if (ebuflen <= 0x7f) { |
| 398 | debug("Data is too short\n"); |
| 399 | return NULL; |
| 400 | } |
| 401 | |
| 402 | ebuf = malloc(ebuflen); |
| 403 | if (!ebuf) { |
| 404 | debug("Out of memory\n"); |
| 405 | return NULL; |
| 406 | } |
| 407 | |
| 408 | memcpy(ebuf, pkcs7_hdr, sizeof(pkcs7_hdr)); |
| 409 | memcpy(ebuf + sizeof(pkcs7_hdr), buf, buflen); |
| 410 | len = ebuflen - 4; |
| 411 | ebuf[2] = (len >> 8) & 0xff; |
| 412 | ebuf[3] = len & 0xff; |
| 413 | len = ebuflen - 0x13; |
| 414 | ebuf[0x11] = (len >> 8) & 0xff; |
| 415 | ebuf[0x12] = len & 0xff; |
| 416 | |
| 417 | msg = pkcs7_parse_message(ebuf, ebuflen); |
| 418 | |
| 419 | free(ebuf); |
| 420 | |
| 421 | out: |
| 422 | if (IS_ERR(msg)) |
| 423 | return NULL; |
| 424 | |
| 425 | return msg; |
| 426 | } |
| 427 | |
| 428 | /** |
| 429 | * efi_variable_authenticate - authenticate a variable |
| 430 | * @variable: Variable name in u16 |
| 431 | * @vendor: Guid of variable |
| 432 | * @data_size: Size of @data |
| 433 | * @data: Pointer to variable's value |
| 434 | * @given_attr: Attributes to be given at SetVariable() |
| 435 | * @env_attr: Attributes that an existing variable holds |
| 436 | * @time: signed time that an existing variable holds |
| 437 | * |
| 438 | * Called by efi_set_variable() to verify that the input is correct. |
| 439 | * Will replace the given data pointer with another that points to |
| 440 | * the actual data to store in the internal memory. |
| 441 | * On success, @data and @data_size will be replaced with variable's |
| 442 | * actual data, excluding authentication data, and its size, and variable's |
| 443 | * attributes and signed time will also be returned in @env_attr and @time, |
| 444 | * respectively. |
| 445 | * |
Heinrich Schuchardt | e2c43da | 2020-05-03 16:29:00 +0200 | [diff] [blame] | 446 | * Return: status code |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 447 | */ |
| 448 | static efi_status_t efi_variable_authenticate(u16 *variable, |
| 449 | const efi_guid_t *vendor, |
| 450 | efi_uintn_t *data_size, |
| 451 | const void **data, u32 given_attr, |
| 452 | u32 *env_attr, u64 *time) |
| 453 | { |
| 454 | const struct efi_variable_authentication_2 *auth; |
| 455 | struct efi_signature_store *truststore, *truststore2; |
| 456 | struct pkcs7_message *var_sig; |
| 457 | struct efi_image_regions *regs; |
| 458 | struct efi_time timestamp; |
| 459 | struct rtc_time tm; |
| 460 | u64 new_time; |
| 461 | efi_status_t ret; |
| 462 | |
| 463 | var_sig = NULL; |
| 464 | truststore = NULL; |
| 465 | truststore2 = NULL; |
| 466 | regs = NULL; |
| 467 | ret = EFI_SECURITY_VIOLATION; |
| 468 | |
| 469 | if (*data_size < sizeof(struct efi_variable_authentication_2)) |
| 470 | goto err; |
| 471 | |
| 472 | /* authentication data */ |
| 473 | auth = *data; |
| 474 | if (*data_size < (sizeof(auth->time_stamp) |
| 475 | + auth->auth_info.hdr.dwLength)) |
| 476 | goto err; |
| 477 | |
| 478 | if (guidcmp(&auth->auth_info.cert_type, &efi_guid_cert_type_pkcs7)) |
| 479 | goto err; |
| 480 | |
| 481 | *data += sizeof(auth->time_stamp) + auth->auth_info.hdr.dwLength; |
| 482 | *data_size -= (sizeof(auth->time_stamp) |
| 483 | + auth->auth_info.hdr.dwLength); |
| 484 | |
| 485 | memcpy(×tamp, &auth->time_stamp, sizeof(timestamp)); |
| 486 | memset(&tm, 0, sizeof(tm)); |
| 487 | tm.tm_year = timestamp.year; |
| 488 | tm.tm_mon = timestamp.month; |
| 489 | tm.tm_mday = timestamp.day; |
| 490 | tm.tm_hour = timestamp.hour; |
| 491 | tm.tm_min = timestamp.minute; |
| 492 | tm.tm_sec = timestamp.second; |
| 493 | new_time = rtc_mktime(&tm); |
| 494 | |
| 495 | if (!efi_secure_boot_enabled()) { |
| 496 | /* finished checking */ |
| 497 | *time = new_time; |
| 498 | return EFI_SUCCESS; |
| 499 | } |
| 500 | |
| 501 | if (new_time <= *time) |
| 502 | goto err; |
| 503 | |
| 504 | /* data to be digested */ |
| 505 | regs = calloc(sizeof(*regs) + sizeof(struct image_region) * 5, 1); |
| 506 | if (!regs) |
| 507 | goto err; |
| 508 | regs->max = 5; |
| 509 | efi_image_region_add(regs, (uint8_t *)variable, |
| 510 | (uint8_t *)variable |
| 511 | + u16_strlen(variable) * sizeof(u16), 1); |
| 512 | efi_image_region_add(regs, (uint8_t *)vendor, |
| 513 | (uint8_t *)vendor + sizeof(*vendor), 1); |
| 514 | efi_image_region_add(regs, (uint8_t *)&given_attr, |
| 515 | (uint8_t *)&given_attr + sizeof(given_attr), 1); |
| 516 | efi_image_region_add(regs, (uint8_t *)×tamp, |
| 517 | (uint8_t *)×tamp + sizeof(timestamp), 1); |
| 518 | efi_image_region_add(regs, (uint8_t *)*data, |
| 519 | (uint8_t *)*data + *data_size, 1); |
| 520 | |
| 521 | /* variable's signature list */ |
| 522 | if (auth->auth_info.hdr.dwLength < sizeof(auth->auth_info)) |
| 523 | goto err; |
| 524 | var_sig = efi_variable_parse_signature(auth->auth_info.cert_data, |
| 525 | auth->auth_info.hdr.dwLength |
| 526 | - sizeof(auth->auth_info)); |
Patrick Wildt | cd8a55b | 2020-05-07 02:13:18 +0200 | [diff] [blame] | 527 | if (!var_sig) { |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 528 | debug("Parsing variable's signature failed\n"); |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 529 | goto err; |
| 530 | } |
| 531 | |
| 532 | /* signature database used for authentication */ |
| 533 | if (u16_strcmp(variable, L"PK") == 0 || |
| 534 | u16_strcmp(variable, L"KEK") == 0) { |
| 535 | /* with PK */ |
| 536 | truststore = efi_sigstore_parse_sigdb(L"PK"); |
| 537 | if (!truststore) |
| 538 | goto err; |
| 539 | } else if (u16_strcmp(variable, L"db") == 0 || |
| 540 | u16_strcmp(variable, L"dbx") == 0) { |
| 541 | /* with PK and KEK */ |
| 542 | truststore = efi_sigstore_parse_sigdb(L"KEK"); |
| 543 | truststore2 = efi_sigstore_parse_sigdb(L"PK"); |
| 544 | |
| 545 | if (!truststore) { |
| 546 | if (!truststore2) |
| 547 | goto err; |
| 548 | |
| 549 | truststore = truststore2; |
| 550 | truststore2 = NULL; |
| 551 | } |
| 552 | } else { |
| 553 | /* TODO: support private authenticated variables */ |
| 554 | goto err; |
| 555 | } |
| 556 | |
| 557 | /* verify signature */ |
| 558 | if (efi_signature_verify_with_sigdb(regs, var_sig, truststore, NULL)) { |
| 559 | debug("Verified\n"); |
| 560 | } else { |
| 561 | if (truststore2 && |
| 562 | efi_signature_verify_with_sigdb(regs, var_sig, |
| 563 | truststore2, NULL)) { |
| 564 | debug("Verified\n"); |
| 565 | } else { |
| 566 | debug("Verifying variable's signature failed\n"); |
| 567 | goto err; |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | /* finished checking */ |
| 572 | *time = rtc_mktime(&tm); |
| 573 | ret = EFI_SUCCESS; |
| 574 | |
| 575 | err: |
| 576 | efi_sigstore_free(truststore); |
| 577 | efi_sigstore_free(truststore2); |
| 578 | pkcs7_free_message(var_sig); |
| 579 | free(regs); |
| 580 | |
| 581 | return ret; |
| 582 | } |
| 583 | #else |
| 584 | static efi_status_t efi_variable_authenticate(u16 *variable, |
| 585 | const efi_guid_t *vendor, |
| 586 | efi_uintn_t *data_size, |
| 587 | const void **data, u32 given_attr, |
| 588 | u32 *env_attr, u64 *time) |
| 589 | { |
| 590 | return EFI_SUCCESS; |
| 591 | } |
| 592 | #endif /* CONFIG_EFI_SECURE_BOOT */ |
| 593 | |
Heinrich Schuchardt | da0a791 | 2020-05-06 01:51:04 +0200 | [diff] [blame] | 594 | static efi_status_t efi_get_variable_common(u16 *variable_name, |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 595 | const efi_guid_t *vendor, |
| 596 | u32 *attributes, |
Heinrich Schuchardt | 87ab0b2 | 2020-04-18 12:31:17 +0200 | [diff] [blame] | 597 | efi_uintn_t *data_size, void *data) |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 598 | { |
Heinrich Schuchardt | af1a920 | 2018-08-31 21:31:31 +0200 | [diff] [blame] | 599 | char *native_name; |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 600 | efi_status_t ret; |
| 601 | unsigned long in_size; |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 602 | const char *val = NULL, *s; |
| 603 | u64 time = 0; |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 604 | u32 attr; |
| 605 | |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 606 | if (!variable_name || !vendor || !data_size) |
| 607 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
| 608 | |
Heinrich Schuchardt | af1a920 | 2018-08-31 21:31:31 +0200 | [diff] [blame] | 609 | ret = efi_to_native(&native_name, variable_name, vendor); |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 610 | if (ret) |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 611 | return ret; |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 612 | |
Heinrich Schuchardt | 55111c3 | 2019-04-04 21:36:44 +0200 | [diff] [blame] | 613 | EFI_PRINT("get '%s'\n", native_name); |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 614 | |
| 615 | val = env_get(native_name); |
Heinrich Schuchardt | af1a920 | 2018-08-31 21:31:31 +0200 | [diff] [blame] | 616 | free(native_name); |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 617 | if (!val) |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 618 | return EFI_NOT_FOUND; |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 619 | |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 620 | val = parse_attr(val, &attr, &time); |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 621 | |
| 622 | in_size = *data_size; |
| 623 | |
| 624 | if ((s = prefix(val, "(blob)"))) { |
Heinrich Schuchardt | 2d8aedc | 2019-01-18 12:31:54 +0100 | [diff] [blame] | 625 | size_t len = strlen(s); |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 626 | |
Ivan Gorinov | d3ea6b7 | 2018-05-11 13:18:25 -0700 | [diff] [blame] | 627 | /* number of hexadecimal digits must be even */ |
| 628 | if (len & 1) |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 629 | return EFI_DEVICE_ERROR; |
Ivan Gorinov | d3ea6b7 | 2018-05-11 13:18:25 -0700 | [diff] [blame] | 630 | |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 631 | /* two characters per byte: */ |
Ivan Gorinov | d3ea6b7 | 2018-05-11 13:18:25 -0700 | [diff] [blame] | 632 | len /= 2; |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 633 | *data_size = len; |
| 634 | |
Heinrich Schuchardt | 05f728f | 2019-05-15 19:32:43 +0200 | [diff] [blame] | 635 | if (in_size < len) { |
| 636 | ret = EFI_BUFFER_TOO_SMALL; |
| 637 | goto out; |
| 638 | } |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 639 | |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 640 | if (!data) { |
| 641 | debug("Variable with no data shouldn't exist.\n"); |
| 642 | return EFI_INVALID_PARAMETER; |
| 643 | } |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 644 | |
Heinrich Schuchardt | 2d8aedc | 2019-01-18 12:31:54 +0100 | [diff] [blame] | 645 | if (hex2bin(data, s, len)) |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 646 | return EFI_DEVICE_ERROR; |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 647 | |
Heinrich Schuchardt | 55111c3 | 2019-04-04 21:36:44 +0200 | [diff] [blame] | 648 | EFI_PRINT("got value: \"%s\"\n", s); |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 649 | } else if ((s = prefix(val, "(utf8)"))) { |
| 650 | unsigned len = strlen(s) + 1; |
| 651 | |
| 652 | *data_size = len; |
| 653 | |
Heinrich Schuchardt | 05f728f | 2019-05-15 19:32:43 +0200 | [diff] [blame] | 654 | if (in_size < len) { |
| 655 | ret = EFI_BUFFER_TOO_SMALL; |
| 656 | goto out; |
| 657 | } |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 658 | |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 659 | if (!data) { |
| 660 | debug("Variable with no data shouldn't exist.\n"); |
| 661 | return EFI_INVALID_PARAMETER; |
| 662 | } |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 663 | |
| 664 | memcpy(data, s, len); |
| 665 | ((char *)data)[len] = '\0'; |
| 666 | |
Heinrich Schuchardt | 55111c3 | 2019-04-04 21:36:44 +0200 | [diff] [blame] | 667 | EFI_PRINT("got value: \"%s\"\n", (char *)data); |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 668 | } else { |
Heinrich Schuchardt | 55111c3 | 2019-04-04 21:36:44 +0200 | [diff] [blame] | 669 | EFI_PRINT("invalid value: '%s'\n", val); |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 670 | return EFI_DEVICE_ERROR; |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 671 | } |
| 672 | |
Heinrich Schuchardt | 05f728f | 2019-05-15 19:32:43 +0200 | [diff] [blame] | 673 | out: |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 674 | if (attributes) |
| 675 | *attributes = attr & EFI_VARIABLE_MASK; |
| 676 | |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 677 | return ret; |
| 678 | } |
| 679 | |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 680 | /** |
| 681 | * efi_efi_get_variable() - retrieve value of a UEFI variable |
| 682 | * |
| 683 | * This function implements the GetVariable runtime service. |
| 684 | * |
| 685 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 686 | * details. |
| 687 | * |
| 688 | * @variable_name: name of the variable |
| 689 | * @vendor: vendor GUID |
| 690 | * @attributes: attributes of the variable |
| 691 | * @data_size: size of the buffer to which the variable value is copied |
| 692 | * @data: buffer to which the variable value is copied |
| 693 | * Return: status code |
| 694 | */ |
| 695 | efi_status_t EFIAPI efi_get_variable(u16 *variable_name, |
| 696 | const efi_guid_t *vendor, u32 *attributes, |
| 697 | efi_uintn_t *data_size, void *data) |
| 698 | { |
| 699 | efi_status_t ret; |
| 700 | |
| 701 | EFI_ENTRY("\"%ls\" %pUl %p %p %p", variable_name, vendor, attributes, |
| 702 | data_size, data); |
| 703 | |
Heinrich Schuchardt | 87ab0b2 | 2020-04-18 12:31:17 +0200 | [diff] [blame] | 704 | ret = efi_get_variable_common(variable_name, vendor, attributes, |
| 705 | data_size, data); |
Heinrich Schuchardt | 05f728f | 2019-05-15 19:32:43 +0200 | [diff] [blame] | 706 | return EFI_EXIT(ret); |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 707 | } |
| 708 | |
AKASHI Takahiro | ad3f36c | 2019-01-21 12:43:13 +0100 | [diff] [blame] | 709 | static char *efi_variables_list; |
| 710 | static char *efi_cur_variable; |
| 711 | |
| 712 | /** |
| 713 | * parse_uboot_variable() - parse a u-boot variable and get uefi-related |
| 714 | * information |
| 715 | * @variable: whole data of u-boot variable (ie. name=value) |
| 716 | * @variable_name_size: size of variable_name buffer in byte |
| 717 | * @variable_name: name of uefi variable in u16, null-terminated |
| 718 | * @vendor: vendor's guid |
| 719 | * @attributes: attributes |
| 720 | * |
| 721 | * A uefi variable is encoded into a u-boot variable as described above. |
| 722 | * This function parses such a u-boot variable and retrieve uefi-related |
| 723 | * information into respective parameters. In return, variable_name_size |
| 724 | * is the size of variable name including NULL. |
| 725 | * |
| 726 | * Return: EFI_SUCCESS if parsing is OK, EFI_NOT_FOUND when |
Heinrich Schuchardt | ee1f0e4 | 2019-07-14 12:11:16 +0200 | [diff] [blame] | 727 | * the entire variable list has been returned, |
| 728 | * otherwise non-zero status code |
AKASHI Takahiro | ad3f36c | 2019-01-21 12:43:13 +0100 | [diff] [blame] | 729 | */ |
| 730 | static efi_status_t parse_uboot_variable(char *variable, |
| 731 | efi_uintn_t *variable_name_size, |
| 732 | u16 *variable_name, |
| 733 | const efi_guid_t *vendor, |
| 734 | u32 *attributes) |
| 735 | { |
| 736 | char *guid, *name, *end, c; |
Heinrich Schuchardt | 31d9b3a | 2020-03-20 19:04:34 +0100 | [diff] [blame] | 737 | size_t name_len; |
| 738 | efi_uintn_t old_variable_name_size; |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 739 | u64 time; |
AKASHI Takahiro | ad3f36c | 2019-01-21 12:43:13 +0100 | [diff] [blame] | 740 | u16 *p; |
| 741 | |
| 742 | guid = strchr(variable, '_'); |
| 743 | if (!guid) |
| 744 | return EFI_INVALID_PARAMETER; |
| 745 | guid++; |
| 746 | name = strchr(guid, '_'); |
| 747 | if (!name) |
| 748 | return EFI_INVALID_PARAMETER; |
| 749 | name++; |
| 750 | end = strchr(name, '='); |
| 751 | if (!end) |
| 752 | return EFI_INVALID_PARAMETER; |
| 753 | |
| 754 | name_len = end - name; |
Heinrich Schuchardt | 31d9b3a | 2020-03-20 19:04:34 +0100 | [diff] [blame] | 755 | old_variable_name_size = *variable_name_size; |
| 756 | *variable_name_size = sizeof(u16) * (name_len + 1); |
| 757 | if (old_variable_name_size < *variable_name_size) |
AKASHI Takahiro | ad3f36c | 2019-01-21 12:43:13 +0100 | [diff] [blame] | 758 | return EFI_BUFFER_TOO_SMALL; |
Heinrich Schuchardt | 31d9b3a | 2020-03-20 19:04:34 +0100 | [diff] [blame] | 759 | |
AKASHI Takahiro | ad3f36c | 2019-01-21 12:43:13 +0100 | [diff] [blame] | 760 | end++; /* point to value */ |
| 761 | |
| 762 | /* variable name */ |
| 763 | p = variable_name; |
| 764 | utf8_utf16_strncpy(&p, name, name_len); |
| 765 | variable_name[name_len] = 0; |
AKASHI Takahiro | ad3f36c | 2019-01-21 12:43:13 +0100 | [diff] [blame] | 766 | |
| 767 | /* guid */ |
| 768 | c = *(name - 1); |
| 769 | *(name - 1) = '\0'; /* guid need be null-terminated here */ |
AKASHI Takahiro | 4854f78 | 2020-05-08 14:51:21 +0900 | [diff] [blame] | 770 | if (uuid_str_to_bin(guid, (unsigned char *)vendor, |
| 771 | UUID_STR_FORMAT_GUID)) |
| 772 | /* The only error would be EINVAL. */ |
| 773 | return EFI_INVALID_PARAMETER; |
AKASHI Takahiro | ad3f36c | 2019-01-21 12:43:13 +0100 | [diff] [blame] | 774 | *(name - 1) = c; |
| 775 | |
| 776 | /* attributes */ |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 777 | parse_attr(end, attributes, &time); |
AKASHI Takahiro | ad3f36c | 2019-01-21 12:43:13 +0100 | [diff] [blame] | 778 | |
| 779 | return EFI_SUCCESS; |
| 780 | } |
| 781 | |
Heinrich Schuchardt | 0ffda47 | 2019-01-18 19:52:05 +0100 | [diff] [blame] | 782 | /** |
AKASHI Takahiro | ad3f36c | 2019-01-21 12:43:13 +0100 | [diff] [blame] | 783 | * efi_get_next_variable_name() - enumerate the current variable names |
Heinrich Schuchardt | ee1f0e4 | 2019-07-14 12:11:16 +0200 | [diff] [blame] | 784 | * |
AKASHI Takahiro | ad3f36c | 2019-01-21 12:43:13 +0100 | [diff] [blame] | 785 | * @variable_name_size: size of variable_name buffer in byte |
| 786 | * @variable_name: name of uefi variable's name in u16 |
| 787 | * @vendor: vendor's guid |
Heinrich Schuchardt | 0ffda47 | 2019-01-18 19:52:05 +0100 | [diff] [blame] | 788 | * |
AKASHI Takahiro | ad3f36c | 2019-01-21 12:43:13 +0100 | [diff] [blame] | 789 | * This function implements the GetNextVariableName service. |
Heinrich Schuchardt | 0ffda47 | 2019-01-18 19:52:05 +0100 | [diff] [blame] | 790 | * |
| 791 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
Heinrich Schuchardt | ee1f0e4 | 2019-07-14 12:11:16 +0200 | [diff] [blame] | 792 | * details. |
Heinrich Schuchardt | 0ffda47 | 2019-01-18 19:52:05 +0100 | [diff] [blame] | 793 | * |
AKASHI Takahiro | ad3f36c | 2019-01-21 12:43:13 +0100 | [diff] [blame] | 794 | * Return: status code |
Heinrich Schuchardt | 0ffda47 | 2019-01-18 19:52:05 +0100 | [diff] [blame] | 795 | */ |
Heinrich Schuchardt | d6a6baa | 2018-05-17 07:57:05 +0200 | [diff] [blame] | 796 | efi_status_t EFIAPI efi_get_next_variable_name(efi_uintn_t *variable_name_size, |
| 797 | u16 *variable_name, |
Heinrich Schuchardt | 82cd6c4 | 2020-03-22 18:28:20 +0100 | [diff] [blame] | 798 | efi_guid_t *vendor) |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 799 | { |
AKASHI Takahiro | ad3f36c | 2019-01-21 12:43:13 +0100 | [diff] [blame] | 800 | char *native_name, *variable; |
| 801 | ssize_t name_len, list_len; |
| 802 | char regex[256]; |
| 803 | char * const regexlist[] = {regex}; |
| 804 | u32 attributes; |
| 805 | int i; |
| 806 | efi_status_t ret; |
| 807 | |
Rob Clark | 238f88c | 2017-09-13 18:05:41 -0400 | [diff] [blame] | 808 | EFI_ENTRY("%p \"%ls\" %pUl", variable_name_size, variable_name, vendor); |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 809 | |
AKASHI Takahiro | ad3f36c | 2019-01-21 12:43:13 +0100 | [diff] [blame] | 810 | if (!variable_name_size || !variable_name || !vendor) |
Heinrich Schuchardt | 793a625 | 2019-03-19 18:36:21 +0100 | [diff] [blame] | 811 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
AKASHI Takahiro | ad3f36c | 2019-01-21 12:43:13 +0100 | [diff] [blame] | 812 | |
| 813 | if (variable_name[0]) { |
| 814 | /* check null-terminated string */ |
| 815 | for (i = 0; i < *variable_name_size; i++) |
| 816 | if (!variable_name[i]) |
| 817 | break; |
| 818 | if (i >= *variable_name_size) |
| 819 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
| 820 | |
| 821 | /* search for the last-returned variable */ |
| 822 | ret = efi_to_native(&native_name, variable_name, vendor); |
| 823 | if (ret) |
| 824 | return EFI_EXIT(ret); |
| 825 | |
| 826 | name_len = strlen(native_name); |
| 827 | for (variable = efi_variables_list; variable && *variable;) { |
| 828 | if (!strncmp(variable, native_name, name_len) && |
| 829 | variable[name_len] == '=') |
| 830 | break; |
| 831 | |
| 832 | variable = strchr(variable, '\n'); |
| 833 | if (variable) |
| 834 | variable++; |
| 835 | } |
| 836 | |
| 837 | free(native_name); |
| 838 | if (!(variable && *variable)) |
| 839 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
| 840 | |
| 841 | /* next variable */ |
| 842 | variable = strchr(variable, '\n'); |
| 843 | if (variable) |
| 844 | variable++; |
| 845 | if (!(variable && *variable)) |
| 846 | return EFI_EXIT(EFI_NOT_FOUND); |
| 847 | } else { |
| 848 | /* |
| 849 | *new search: free a list used in the previous search |
| 850 | */ |
| 851 | free(efi_variables_list); |
| 852 | efi_variables_list = NULL; |
| 853 | efi_cur_variable = NULL; |
| 854 | |
| 855 | snprintf(regex, 256, "efi_.*-.*-.*-.*-.*_.*"); |
| 856 | list_len = hexport_r(&env_htab, '\n', |
| 857 | H_MATCH_REGEX | H_MATCH_KEY, |
| 858 | &efi_variables_list, 0, 1, regexlist); |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 859 | |
Heinrich Schuchardt | fd73848 | 2019-01-22 20:10:46 +0100 | [diff] [blame] | 860 | if (list_len <= 1) |
AKASHI Takahiro | ad3f36c | 2019-01-21 12:43:13 +0100 | [diff] [blame] | 861 | return EFI_EXIT(EFI_NOT_FOUND); |
| 862 | |
| 863 | variable = efi_variables_list; |
| 864 | } |
| 865 | |
| 866 | ret = parse_uboot_variable(variable, variable_name_size, variable_name, |
| 867 | vendor, &attributes); |
| 868 | |
| 869 | return EFI_EXIT(ret); |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 870 | } |
| 871 | |
Heinrich Schuchardt | 37ab736 | 2020-05-03 10:02:20 +0200 | [diff] [blame] | 872 | static efi_status_t efi_set_variable_common(u16 *variable_name, |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 873 | const efi_guid_t *vendor, |
| 874 | u32 attributes, |
| 875 | efi_uintn_t data_size, |
| 876 | const void *data, |
Heinrich Schuchardt | 37ab736 | 2020-05-03 10:02:20 +0200 | [diff] [blame] | 877 | bool ro_check) |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 878 | { |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 879 | char *native_name = NULL, *old_data = NULL, *val = NULL, *s; |
| 880 | efi_uintn_t old_size; |
| 881 | bool append, delete; |
| 882 | u64 time = 0; |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 883 | u32 attr; |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 884 | efi_status_t ret = EFI_SUCCESS; |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 885 | |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 886 | debug("%s: set '%s'\n", __func__, native_name); |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 887 | |
Heinrich Schuchardt | adba396 | 2019-06-12 23:28:42 +0200 | [diff] [blame] | 888 | if (!variable_name || !*variable_name || !vendor || |
| 889 | ((attributes & EFI_VARIABLE_RUNTIME_ACCESS) && |
AKASHI Takahiro | e5023b7 | 2019-09-06 15:09:52 +0900 | [diff] [blame] | 890 | !(attributes & EFI_VARIABLE_BOOTSERVICE_ACCESS))) { |
Heinrich Schuchardt | af1a920 | 2018-08-31 21:31:31 +0200 | [diff] [blame] | 891 | ret = EFI_INVALID_PARAMETER; |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 892 | goto err; |
Heinrich Schuchardt | af1a920 | 2018-08-31 21:31:31 +0200 | [diff] [blame] | 893 | } |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 894 | |
Heinrich Schuchardt | af1a920 | 2018-08-31 21:31:31 +0200 | [diff] [blame] | 895 | ret = efi_to_native(&native_name, variable_name, vendor); |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 896 | if (ret) |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 897 | goto err; |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 898 | |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 899 | /* check if a variable exists */ |
| 900 | old_size = 0; |
| 901 | attr = 0; |
Heinrich Schuchardt | da0a791 | 2020-05-06 01:51:04 +0200 | [diff] [blame] | 902 | ret = efi_get_variable_common(variable_name, vendor, &attr, |
| 903 | &old_size, NULL); |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 904 | append = !!(attributes & EFI_VARIABLE_APPEND_WRITE); |
| 905 | attributes &= ~(u32)EFI_VARIABLE_APPEND_WRITE; |
| 906 | delete = !append && (!data_size || !attributes); |
| 907 | |
| 908 | /* check attributes */ |
| 909 | if (old_size) { |
| 910 | if (ro_check && (attr & READ_ONLY)) { |
| 911 | ret = EFI_WRITE_PROTECTED; |
| 912 | goto err; |
AKASHI Takahiro | e5023b7 | 2019-09-06 15:09:52 +0900 | [diff] [blame] | 913 | } |
| 914 | |
| 915 | /* attributes won't be changed */ |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 916 | if (!delete && |
| 917 | ((ro_check && attr != attributes) || |
| 918 | (!ro_check && ((attr & ~(u32)READ_ONLY) |
| 919 | != (attributes & ~(u32)READ_ONLY))))) { |
AKASHI Takahiro | 186b5a4 | 2019-05-24 15:59:03 +0900 | [diff] [blame] | 920 | ret = EFI_INVALID_PARAMETER; |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 921 | goto err; |
AKASHI Takahiro | e5023b7 | 2019-09-06 15:09:52 +0900 | [diff] [blame] | 922 | } |
| 923 | } else { |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 924 | if (delete || append) { |
Heinrich Schuchardt | 8021bbe | 2019-09-26 21:40:18 +0200 | [diff] [blame] | 925 | /* |
| 926 | * Trying to delete or to update a non-existent |
| 927 | * variable. |
| 928 | */ |
AKASHI Takahiro | e5023b7 | 2019-09-06 15:09:52 +0900 | [diff] [blame] | 929 | ret = EFI_NOT_FOUND; |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 930 | goto err; |
AKASHI Takahiro | e5023b7 | 2019-09-06 15:09:52 +0900 | [diff] [blame] | 931 | } |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 932 | } |
AKASHI Takahiro | e5023b7 | 2019-09-06 15:09:52 +0900 | [diff] [blame] | 933 | |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 934 | if (((!u16_strcmp(variable_name, L"PK") || |
| 935 | !u16_strcmp(variable_name, L"KEK")) && |
| 936 | !guidcmp(vendor, &efi_global_variable_guid)) || |
| 937 | ((!u16_strcmp(variable_name, L"db") || |
| 938 | !u16_strcmp(variable_name, L"dbx")) && |
| 939 | !guidcmp(vendor, &efi_guid_image_security_database))) { |
| 940 | /* authentication is mandatory */ |
| 941 | if (!(attributes & |
| 942 | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)) { |
| 943 | debug("%ls: AUTHENTICATED_WRITE_ACCESS required\n", |
| 944 | variable_name); |
| 945 | ret = EFI_INVALID_PARAMETER; |
| 946 | goto err; |
| 947 | } |
| 948 | } |
| 949 | |
| 950 | /* authenticate a variable */ |
| 951 | if (IS_ENABLED(CONFIG_EFI_SECURE_BOOT)) { |
| 952 | if (attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) { |
| 953 | ret = EFI_INVALID_PARAMETER; |
| 954 | goto err; |
| 955 | } |
| 956 | if (attributes & |
| 957 | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) { |
| 958 | ret = efi_variable_authenticate(variable_name, vendor, |
| 959 | &data_size, &data, |
| 960 | attributes, &attr, |
| 961 | &time); |
| 962 | if (ret != EFI_SUCCESS) |
| 963 | goto err; |
| 964 | |
| 965 | /* last chance to check for delete */ |
| 966 | if (!data_size) |
| 967 | delete = true; |
| 968 | } |
| 969 | } else { |
| 970 | if (attributes & |
| 971 | (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS | |
| 972 | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)) { |
| 973 | debug("Secure boot is not configured\n"); |
| 974 | ret = EFI_INVALID_PARAMETER; |
| 975 | goto err; |
| 976 | } |
| 977 | } |
| 978 | |
| 979 | /* delete a variable */ |
| 980 | if (delete) { |
| 981 | /* !old_size case has been handled before */ |
| 982 | val = NULL; |
| 983 | ret = EFI_SUCCESS; |
| 984 | goto out; |
| 985 | } |
| 986 | |
| 987 | if (append) { |
| 988 | old_data = malloc(old_size); |
| 989 | if (!old_data) { |
Heinrich Schuchardt | ebbda7b | 2020-05-06 01:37:25 +0200 | [diff] [blame] | 990 | ret = EFI_OUT_OF_RESOURCES; |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 991 | goto err; |
| 992 | } |
Heinrich Schuchardt | da0a791 | 2020-05-06 01:51:04 +0200 | [diff] [blame] | 993 | ret = efi_get_variable_common(variable_name, vendor, |
| 994 | &attr, &old_size, old_data); |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 995 | if (ret != EFI_SUCCESS) |
| 996 | goto err; |
| 997 | } else { |
AKASHI Takahiro | e5023b7 | 2019-09-06 15:09:52 +0900 | [diff] [blame] | 998 | old_size = 0; |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 999 | } |
| 1000 | |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 1001 | val = malloc(2 * old_size + 2 * data_size |
| 1002 | + strlen("{ro,run,boot,nv,time=0123456701234567}(blob)") |
| 1003 | + 1); |
Heinrich Schuchardt | bd095f5 | 2018-10-02 05:30:05 +0200 | [diff] [blame] | 1004 | if (!val) { |
| 1005 | ret = EFI_OUT_OF_RESOURCES; |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 1006 | goto err; |
Heinrich Schuchardt | bd095f5 | 2018-10-02 05:30:05 +0200 | [diff] [blame] | 1007 | } |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 1008 | |
| 1009 | s = val; |
| 1010 | |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 1011 | /* |
| 1012 | * store attributes |
| 1013 | */ |
| 1014 | attributes &= (READ_ONLY | |
| 1015 | EFI_VARIABLE_NON_VOLATILE | |
AKASHI Takahiro | 7f33433 | 2019-06-04 15:52:06 +0900 | [diff] [blame] | 1016 | EFI_VARIABLE_BOOTSERVICE_ACCESS | |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 1017 | EFI_VARIABLE_RUNTIME_ACCESS | |
| 1018 | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS); |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 1019 | s += sprintf(s, "{"); |
| 1020 | while (attributes) { |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 1021 | attr = 1 << (ffs(attributes) - 1); |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 1022 | |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 1023 | if (attr == READ_ONLY) { |
| 1024 | s += sprintf(s, "ro"); |
| 1025 | } else if (attr == EFI_VARIABLE_NON_VOLATILE) { |
AKASHI Takahiro | 7f33433 | 2019-06-04 15:52:06 +0900 | [diff] [blame] | 1026 | s += sprintf(s, "nv"); |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 1027 | } else if (attr == EFI_VARIABLE_BOOTSERVICE_ACCESS) { |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 1028 | s += sprintf(s, "boot"); |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 1029 | } else if (attr == EFI_VARIABLE_RUNTIME_ACCESS) { |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 1030 | s += sprintf(s, "run"); |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 1031 | } else if (attr == |
| 1032 | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) { |
| 1033 | s += sprintf(s, "time="); |
| 1034 | s = bin2hex(s, (u8 *)&time, sizeof(time)); |
| 1035 | } |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 1036 | |
| 1037 | attributes &= ~attr; |
| 1038 | if (attributes) |
| 1039 | s += sprintf(s, ","); |
| 1040 | } |
| 1041 | s += sprintf(s, "}"); |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 1042 | s += sprintf(s, "(blob)"); |
AKASHI Takahiro | e5023b7 | 2019-09-06 15:09:52 +0900 | [diff] [blame] | 1043 | |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 1044 | /* store payload: */ |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 1045 | if (append) |
| 1046 | s = bin2hex(s, old_data, old_size); |
Heinrich Schuchardt | 807899d | 2019-01-18 18:54:26 +0100 | [diff] [blame] | 1047 | s = bin2hex(s, data, data_size); |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 1048 | *s = '\0'; |
| 1049 | |
Heinrich Schuchardt | 55111c3 | 2019-04-04 21:36:44 +0200 | [diff] [blame] | 1050 | EFI_PRINT("setting: %s=%s\n", native_name, val); |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 1051 | |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 1052 | out: |
AKASHI Takahiro | c78dc19 | 2020-04-14 11:51:42 +0900 | [diff] [blame] | 1053 | if (env_set(native_name, val)) { |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 1054 | ret = EFI_DEVICE_ERROR; |
AKASHI Takahiro | c78dc19 | 2020-04-14 11:51:42 +0900 | [diff] [blame] | 1055 | } else { |
AKASHI Takahiro | 94b139a | 2020-04-14 11:51:43 +0900 | [diff] [blame] | 1056 | bool vendor_keys_modified = false; |
| 1057 | |
AKASHI Takahiro | c78dc19 | 2020-04-14 11:51:42 +0900 | [diff] [blame] | 1058 | if ((u16_strcmp(variable_name, L"PK") == 0 && |
| 1059 | guidcmp(vendor, &efi_global_variable_guid) == 0)) { |
| 1060 | ret = efi_transfer_secure_state( |
| 1061 | (delete ? EFI_MODE_SETUP : |
| 1062 | EFI_MODE_USER)); |
| 1063 | if (ret != EFI_SUCCESS) |
| 1064 | goto err; |
AKASHI Takahiro | 94b139a | 2020-04-14 11:51:43 +0900 | [diff] [blame] | 1065 | |
| 1066 | if (efi_secure_mode != EFI_MODE_SETUP) |
| 1067 | vendor_keys_modified = true; |
| 1068 | } else if ((u16_strcmp(variable_name, L"KEK") == 0 && |
| 1069 | guidcmp(vendor, &efi_global_variable_guid) == 0)) { |
| 1070 | if (efi_secure_mode != EFI_MODE_SETUP) |
| 1071 | vendor_keys_modified = true; |
AKASHI Takahiro | c78dc19 | 2020-04-14 11:51:42 +0900 | [diff] [blame] | 1072 | } |
AKASHI Takahiro | 94b139a | 2020-04-14 11:51:43 +0900 | [diff] [blame] | 1073 | |
| 1074 | /* update VendorKeys */ |
| 1075 | if (vendor_keys_modified & efi_vendor_keys) { |
| 1076 | efi_vendor_keys = 0; |
Heinrich Schuchardt | 37ab736 | 2020-05-03 10:02:20 +0200 | [diff] [blame] | 1077 | ret = efi_set_variable_common( |
AKASHI Takahiro | 94b139a | 2020-04-14 11:51:43 +0900 | [diff] [blame] | 1078 | L"VendorKeys", |
| 1079 | &efi_global_variable_guid, |
| 1080 | EFI_VARIABLE_BOOTSERVICE_ACCESS |
| 1081 | | EFI_VARIABLE_RUNTIME_ACCESS |
| 1082 | | READ_ONLY, |
| 1083 | sizeof(efi_vendor_keys), |
| 1084 | &efi_vendor_keys, |
| 1085 | false); |
| 1086 | } else { |
| 1087 | ret = EFI_SUCCESS; |
| 1088 | } |
AKASHI Takahiro | c78dc19 | 2020-04-14 11:51:42 +0900 | [diff] [blame] | 1089 | } |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 1090 | |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 1091 | err: |
Heinrich Schuchardt | af1a920 | 2018-08-31 21:31:31 +0200 | [diff] [blame] | 1092 | free(native_name); |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 1093 | free(old_data); |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 1094 | free(val); |
| 1095 | |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 1096 | return ret; |
| 1097 | } |
| 1098 | |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 1099 | /** |
| 1100 | * efi_set_variable() - set value of a UEFI variable |
| 1101 | * |
| 1102 | * This function implements the SetVariable runtime service. |
| 1103 | * |
| 1104 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 1105 | * details. |
| 1106 | * |
| 1107 | * @variable_name: name of the variable |
| 1108 | * @vendor: vendor GUID |
| 1109 | * @attributes: attributes of the variable |
| 1110 | * @data_size: size of the buffer with the variable value |
| 1111 | * @data: buffer with the variable value |
| 1112 | * Return: status code |
| 1113 | */ |
| 1114 | efi_status_t EFIAPI efi_set_variable(u16 *variable_name, |
| 1115 | const efi_guid_t *vendor, u32 attributes, |
| 1116 | efi_uintn_t data_size, const void *data) |
| 1117 | { |
| 1118 | EFI_ENTRY("\"%ls\" %pUl %x %zu %p", variable_name, vendor, attributes, |
| 1119 | data_size, data); |
| 1120 | |
| 1121 | /* READ_ONLY bit is not part of API */ |
| 1122 | attributes &= ~(u32)READ_ONLY; |
| 1123 | |
Heinrich Schuchardt | 37ab736 | 2020-05-03 10:02:20 +0200 | [diff] [blame] | 1124 | return EFI_EXIT(efi_set_variable_common(variable_name, vendor, |
| 1125 | attributes, data_size, data, |
| 1126 | true)); |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 1127 | } |
Heinrich Schuchardt | 9b19dae | 2019-06-20 12:13:05 +0200 | [diff] [blame] | 1128 | |
| 1129 | /** |
| 1130 | * efi_query_variable_info() - get information about EFI variables |
| 1131 | * |
| 1132 | * This function implements the QueryVariableInfo() runtime service. |
| 1133 | * |
| 1134 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 1135 | * details. |
| 1136 | * |
| 1137 | * @attributes: bitmask to select variables to be |
| 1138 | * queried |
| 1139 | * @maximum_variable_storage_size: maximum size of storage area for the |
| 1140 | * selected variable types |
| 1141 | * @remaining_variable_storage_size: remaining size of storage are for the |
| 1142 | * selected variable types |
| 1143 | * @maximum_variable_size: maximum size of a variable of the |
| 1144 | * selected type |
| 1145 | * Returns: status code |
| 1146 | */ |
| 1147 | efi_status_t __efi_runtime EFIAPI efi_query_variable_info( |
| 1148 | u32 attributes, |
| 1149 | u64 *maximum_variable_storage_size, |
| 1150 | u64 *remaining_variable_storage_size, |
| 1151 | u64 *maximum_variable_size) |
| 1152 | { |
| 1153 | return EFI_UNSUPPORTED; |
| 1154 | } |
Heinrich Schuchardt | cf3b118 | 2019-06-20 13:52:16 +0200 | [diff] [blame] | 1155 | |
| 1156 | /** |
Heinrich Schuchardt | 2ac6258 | 2019-06-20 15:25:48 +0200 | [diff] [blame] | 1157 | * efi_get_variable_runtime() - runtime implementation of GetVariable() |
Heinrich Schuchardt | ee1f0e4 | 2019-07-14 12:11:16 +0200 | [diff] [blame] | 1158 | * |
| 1159 | * @variable_name: name of the variable |
| 1160 | * @vendor: vendor GUID |
| 1161 | * @attributes: attributes of the variable |
| 1162 | * @data_size: size of the buffer to which the variable value is copied |
| 1163 | * @data: buffer to which the variable value is copied |
| 1164 | * Return: status code |
Heinrich Schuchardt | 2ac6258 | 2019-06-20 15:25:48 +0200 | [diff] [blame] | 1165 | */ |
| 1166 | static efi_status_t __efi_runtime EFIAPI |
| 1167 | efi_get_variable_runtime(u16 *variable_name, const efi_guid_t *vendor, |
| 1168 | u32 *attributes, efi_uintn_t *data_size, void *data) |
| 1169 | { |
| 1170 | return EFI_UNSUPPORTED; |
| 1171 | } |
| 1172 | |
| 1173 | /** |
| 1174 | * efi_get_next_variable_name_runtime() - runtime implementation of |
| 1175 | * GetNextVariable() |
Heinrich Schuchardt | ee1f0e4 | 2019-07-14 12:11:16 +0200 | [diff] [blame] | 1176 | * |
| 1177 | * @variable_name_size: size of variable_name buffer in byte |
| 1178 | * @variable_name: name of uefi variable's name in u16 |
| 1179 | * @vendor: vendor's guid |
| 1180 | * Return: status code |
Heinrich Schuchardt | 2ac6258 | 2019-06-20 15:25:48 +0200 | [diff] [blame] | 1181 | */ |
| 1182 | static efi_status_t __efi_runtime EFIAPI |
| 1183 | efi_get_next_variable_name_runtime(efi_uintn_t *variable_name_size, |
Heinrich Schuchardt | 82cd6c4 | 2020-03-22 18:28:20 +0100 | [diff] [blame] | 1184 | u16 *variable_name, efi_guid_t *vendor) |
Heinrich Schuchardt | 2ac6258 | 2019-06-20 15:25:48 +0200 | [diff] [blame] | 1185 | { |
| 1186 | return EFI_UNSUPPORTED; |
| 1187 | } |
| 1188 | |
| 1189 | /** |
| 1190 | * efi_set_variable_runtime() - runtime implementation of SetVariable() |
Heinrich Schuchardt | ee1f0e4 | 2019-07-14 12:11:16 +0200 | [diff] [blame] | 1191 | * |
| 1192 | * @variable_name: name of the variable |
| 1193 | * @vendor: vendor GUID |
| 1194 | * @attributes: attributes of the variable |
| 1195 | * @data_size: size of the buffer with the variable value |
| 1196 | * @data: buffer with the variable value |
| 1197 | * Return: status code |
Heinrich Schuchardt | 2ac6258 | 2019-06-20 15:25:48 +0200 | [diff] [blame] | 1198 | */ |
| 1199 | static efi_status_t __efi_runtime EFIAPI |
| 1200 | efi_set_variable_runtime(u16 *variable_name, const efi_guid_t *vendor, |
| 1201 | u32 attributes, efi_uintn_t data_size, |
| 1202 | const void *data) |
| 1203 | { |
| 1204 | return EFI_UNSUPPORTED; |
| 1205 | } |
| 1206 | |
| 1207 | /** |
| 1208 | * efi_variables_boot_exit_notify() - notify ExitBootServices() is called |
| 1209 | */ |
| 1210 | void efi_variables_boot_exit_notify(void) |
| 1211 | { |
| 1212 | efi_runtime_services.get_variable = efi_get_variable_runtime; |
| 1213 | efi_runtime_services.get_next_variable_name = |
| 1214 | efi_get_next_variable_name_runtime; |
| 1215 | efi_runtime_services.set_variable = efi_set_variable_runtime; |
| 1216 | efi_update_table_header_crc32(&efi_runtime_services.hdr); |
| 1217 | } |
| 1218 | |
| 1219 | /** |
Heinrich Schuchardt | cf3b118 | 2019-06-20 13:52:16 +0200 | [diff] [blame] | 1220 | * efi_init_variables() - initialize variable services |
| 1221 | * |
| 1222 | * Return: status code |
| 1223 | */ |
| 1224 | efi_status_t efi_init_variables(void) |
| 1225 | { |
AKASHI Takahiro | c78dc19 | 2020-04-14 11:51:42 +0900 | [diff] [blame] | 1226 | efi_status_t ret; |
| 1227 | |
| 1228 | ret = efi_init_secure_state(); |
| 1229 | |
| 1230 | return ret; |
Heinrich Schuchardt | cf3b118 | 2019-06-20 13:52:16 +0200 | [diff] [blame] | 1231 | } |