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