Heinrich Schuchardt | 09a8d50 | 2020-03-19 18:21:58 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * File interface for UEFI variables |
| 4 | * |
| 5 | * Copyright (c) 2020, Heinrich Schuchardt |
| 6 | */ |
| 7 | |
| 8 | #define LOG_CATEGORY LOGC_EFI |
| 9 | |
Heinrich Schuchardt | 09a8d50 | 2020-03-19 18:21:58 +0000 | [diff] [blame] | 10 | #include <charset.h> |
| 11 | #include <fs.h> |
| 12 | #include <log.h> |
| 13 | #include <malloc.h> |
| 14 | #include <mapmem.h> |
| 15 | #include <efi_loader.h> |
| 16 | #include <efi_variable.h> |
| 17 | #include <u-boot/crc.h> |
| 18 | |
| 19 | #define PART_STR_LEN 10 |
| 20 | |
Heinrich Schuchardt | dfa2278 | 2021-10-06 14:10:14 +0200 | [diff] [blame] | 21 | /* GUID used by Shim to store the MOK database */ |
| 22 | #define SHIM_LOCK_GUID \ |
| 23 | EFI_GUID(0x605dab50, 0xe046, 0x4300, \ |
| 24 | 0xab, 0xb6, 0x3d, 0xd8, 0x10, 0xdd, 0x8b, 0x23) |
| 25 | |
| 26 | static const efi_guid_t shim_lock_guid = SHIM_LOCK_GUID; |
| 27 | |
Heinrich Schuchardt | 09a8d50 | 2020-03-19 18:21:58 +0000 | [diff] [blame] | 28 | /** |
| 29 | * efi_set_blk_dev_to_system_partition() - select EFI system partition |
| 30 | * |
| 31 | * Set the EFI system partition as current block device. |
| 32 | * |
| 33 | * Return: status code |
| 34 | */ |
| 35 | static efi_status_t __maybe_unused efi_set_blk_dev_to_system_partition(void) |
| 36 | { |
| 37 | char part_str[PART_STR_LEN]; |
| 38 | int r; |
| 39 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 40 | if (efi_system_partition.uclass_id == UCLASS_INVALID) { |
Heinrich Schuchardt | 09a8d50 | 2020-03-19 18:21:58 +0000 | [diff] [blame] | 41 | log_err("No EFI system partition\n"); |
| 42 | return EFI_DEVICE_ERROR; |
| 43 | } |
Heinrich Schuchardt | 54f180d | 2021-05-25 18:00:13 +0200 | [diff] [blame] | 44 | snprintf(part_str, PART_STR_LEN, "%x:%x", |
Heinrich Schuchardt | 09a8d50 | 2020-03-19 18:21:58 +0000 | [diff] [blame] | 45 | efi_system_partition.devnum, efi_system_partition.part); |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 46 | r = fs_set_blk_dev(blk_get_uclass_name(efi_system_partition.uclass_id), |
Heinrich Schuchardt | 09a8d50 | 2020-03-19 18:21:58 +0000 | [diff] [blame] | 47 | part_str, FS_TYPE_ANY); |
| 48 | if (r) { |
| 49 | log_err("Cannot read EFI system partition\n"); |
| 50 | return EFI_DEVICE_ERROR; |
| 51 | } |
| 52 | return EFI_SUCCESS; |
| 53 | } |
| 54 | |
Ilias Apalodimas | a4d1b1b | 2020-07-23 15:49:49 +0300 | [diff] [blame] | 55 | efi_status_t __maybe_unused efi_var_collect(struct efi_var_file **bufp, loff_t *lenp, |
| 56 | u32 check_attr_mask) |
Heinrich Schuchardt | 09a8d50 | 2020-03-19 18:21:58 +0000 | [diff] [blame] | 57 | { |
| 58 | size_t len = EFI_VAR_BUF_SIZE; |
| 59 | struct efi_var_file *buf; |
| 60 | struct efi_var_entry *var, *old_var; |
| 61 | size_t old_var_name_length = 2; |
| 62 | |
| 63 | *bufp = NULL; /* Avoid double free() */ |
| 64 | buf = calloc(1, len); |
| 65 | if (!buf) |
| 66 | return EFI_OUT_OF_RESOURCES; |
| 67 | var = buf->var; |
| 68 | old_var = var; |
| 69 | for (;;) { |
| 70 | efi_uintn_t data_length, var_name_length; |
| 71 | u8 *data; |
| 72 | efi_status_t ret; |
| 73 | |
| 74 | if ((uintptr_t)buf + len <= |
| 75 | (uintptr_t)var->name + old_var_name_length) |
| 76 | return EFI_BUFFER_TOO_SMALL; |
| 77 | |
| 78 | var_name_length = (uintptr_t)buf + len - (uintptr_t)var->name; |
| 79 | memcpy(var->name, old_var->name, old_var_name_length); |
| 80 | guidcpy(&var->guid, &old_var->guid); |
| 81 | ret = efi_get_next_variable_name_int( |
| 82 | &var_name_length, var->name, &var->guid); |
| 83 | if (ret == EFI_NOT_FOUND) |
| 84 | break; |
| 85 | if (ret != EFI_SUCCESS) { |
| 86 | free(buf); |
| 87 | return ret; |
| 88 | } |
| 89 | old_var_name_length = var_name_length; |
| 90 | old_var = var; |
| 91 | |
| 92 | data = (u8 *)var->name + old_var_name_length; |
| 93 | data_length = (uintptr_t)buf + len - (uintptr_t)data; |
| 94 | ret = efi_get_variable_int(var->name, &var->guid, |
| 95 | &var->attr, &data_length, data, |
| 96 | &var->time); |
| 97 | if (ret != EFI_SUCCESS) { |
| 98 | free(buf); |
| 99 | return ret; |
| 100 | } |
Ilias Apalodimas | a4d1b1b | 2020-07-23 15:49:49 +0300 | [diff] [blame] | 101 | if ((var->attr & check_attr_mask) == check_attr_mask) { |
| 102 | var->length = data_length; |
| 103 | var = (struct efi_var_entry *)ALIGN((uintptr_t)data + data_length, 8); |
| 104 | } |
Heinrich Schuchardt | 09a8d50 | 2020-03-19 18:21:58 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | buf->reserved = 0; |
| 108 | buf->magic = EFI_VAR_FILE_MAGIC; |
| 109 | len = (uintptr_t)var - (uintptr_t)buf; |
| 110 | buf->crc32 = crc32(0, (u8 *)buf->var, |
| 111 | len - sizeof(struct efi_var_file)); |
| 112 | buf->length = len; |
| 113 | *bufp = buf; |
| 114 | *lenp = len; |
| 115 | |
| 116 | return EFI_SUCCESS; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * efi_var_to_file() - save non-volatile variables as file |
| 121 | * |
| 122 | * File ubootefi.var is created on the EFI system partion. |
| 123 | * |
| 124 | * Return: status code |
| 125 | */ |
| 126 | efi_status_t efi_var_to_file(void) |
| 127 | { |
| 128 | #ifdef CONFIG_EFI_VARIABLE_FILE_STORE |
| 129 | efi_status_t ret; |
| 130 | struct efi_var_file *buf; |
| 131 | loff_t len; |
| 132 | loff_t actlen; |
| 133 | int r; |
| 134 | |
Ilias Apalodimas | a4d1b1b | 2020-07-23 15:49:49 +0300 | [diff] [blame] | 135 | ret = efi_var_collect(&buf, &len, EFI_VARIABLE_NON_VOLATILE); |
Heinrich Schuchardt | 09a8d50 | 2020-03-19 18:21:58 +0000 | [diff] [blame] | 136 | if (ret != EFI_SUCCESS) |
| 137 | goto error; |
| 138 | |
| 139 | ret = efi_set_blk_dev_to_system_partition(); |
| 140 | if (ret != EFI_SUCCESS) |
| 141 | goto error; |
| 142 | |
| 143 | r = fs_write(EFI_VAR_FILE_NAME, map_to_sysmem(buf), 0, len, &actlen); |
| 144 | if (r || len != actlen) |
| 145 | ret = EFI_DEVICE_ERROR; |
| 146 | |
| 147 | error: |
| 148 | if (ret != EFI_SUCCESS) |
| 149 | log_err("Failed to persist EFI variables\n"); |
| 150 | free(buf); |
| 151 | return ret; |
| 152 | #else |
| 153 | return EFI_SUCCESS; |
| 154 | #endif |
| 155 | } |
| 156 | |
Heinrich Schuchardt | 211317c | 2021-08-25 19:13:24 +0200 | [diff] [blame] | 157 | efi_status_t efi_var_restore(struct efi_var_file *buf, bool safe) |
Heinrich Schuchardt | 09a8d50 | 2020-03-19 18:21:58 +0000 | [diff] [blame] | 158 | { |
| 159 | struct efi_var_entry *var, *last_var; |
Heinrich Schuchardt | 211317c | 2021-08-25 19:13:24 +0200 | [diff] [blame] | 160 | u16 *data; |
Heinrich Schuchardt | 09a8d50 | 2020-03-19 18:21:58 +0000 | [diff] [blame] | 161 | efi_status_t ret; |
| 162 | |
| 163 | if (buf->reserved || buf->magic != EFI_VAR_FILE_MAGIC || |
| 164 | buf->crc32 != crc32(0, (u8 *)buf->var, |
| 165 | buf->length - sizeof(struct efi_var_file))) { |
| 166 | log_err("Invalid EFI variables file\n"); |
| 167 | return EFI_INVALID_PARAMETER; |
| 168 | } |
| 169 | |
Heinrich Schuchardt | 09a8d50 | 2020-03-19 18:21:58 +0000 | [diff] [blame] | 170 | last_var = (struct efi_var_entry *)((u8 *)buf + buf->length); |
Heinrich Schuchardt | 211317c | 2021-08-25 19:13:24 +0200 | [diff] [blame] | 171 | for (var = buf->var; var < last_var; |
| 172 | var = (struct efi_var_entry *) |
| 173 | ALIGN((uintptr_t)data + var->length, 8)) { |
Heinrich Schuchardt | 09a8d50 | 2020-03-19 18:21:58 +0000 | [diff] [blame] | 174 | |
Heinrich Schuchardt | 211317c | 2021-08-25 19:13:24 +0200 | [diff] [blame] | 175 | data = var->name + u16_strlen(var->name) + 1; |
| 176 | |
| 177 | /* |
Heinrich Schuchardt | 50413bb | 2022-12-29 09:23:03 +0100 | [diff] [blame] | 178 | * Secure boot related and volatile variables shall only be |
Heinrich Schuchardt | 211317c | 2021-08-25 19:13:24 +0200 | [diff] [blame] | 179 | * restored from U-Boot's preseed. |
| 180 | */ |
| 181 | if (!safe && |
| 182 | (efi_auth_var_get_type(var->name, &var->guid) != |
| 183 | EFI_AUTH_VAR_NONE || |
Heinrich Schuchardt | dfa2278 | 2021-10-06 14:10:14 +0200 | [diff] [blame] | 184 | !guidcmp(&var->guid, &shim_lock_guid) || |
Heinrich Schuchardt | 211317c | 2021-08-25 19:13:24 +0200 | [diff] [blame] | 185 | !(var->attr & EFI_VARIABLE_NON_VOLATILE))) |
| 186 | continue; |
| 187 | if (!var->length) |
| 188 | continue; |
Ilias Apalodimas | 3a83082 | 2022-12-29 10:13:22 +0200 | [diff] [blame] | 189 | if (efi_var_mem_find(&var->guid, var->name, NULL)) |
| 190 | continue; |
Heinrich Schuchardt | 211317c | 2021-08-25 19:13:24 +0200 | [diff] [blame] | 191 | ret = efi_var_mem_ins(var->name, &var->guid, var->attr, |
| 192 | var->length, data, 0, NULL, |
| 193 | var->time); |
| 194 | if (ret != EFI_SUCCESS) |
| 195 | log_err("Failed to set EFI variable %ls\n", var->name); |
Heinrich Schuchardt | 09a8d50 | 2020-03-19 18:21:58 +0000 | [diff] [blame] | 196 | } |
| 197 | return EFI_SUCCESS; |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * efi_var_from_file() - read variables from file |
| 202 | * |
| 203 | * File ubootefi.var is read from the EFI system partitions and the variables |
| 204 | * stored in the file are created. |
| 205 | * |
Heinrich Schuchardt | 6fde198 | 2023-11-13 15:50:16 +0100 | [diff] [blame] | 206 | * On first boot the file ubootefi.var does not exist yet. This is why we must |
| 207 | * return EFI_SUCCESS in this case. |
| 208 | * |
| 209 | * If the variable file is corrupted, e.g. incorrect CRC32, we do not want to |
| 210 | * stop the boot process. We deliberately return EFI_SUCCESS in this case, too. |
Heinrich Schuchardt | 09a8d50 | 2020-03-19 18:21:58 +0000 | [diff] [blame] | 211 | * |
| 212 | * Return: status code |
| 213 | */ |
| 214 | efi_status_t efi_var_from_file(void) |
| 215 | { |
| 216 | #ifdef CONFIG_EFI_VARIABLE_FILE_STORE |
| 217 | struct efi_var_file *buf; |
| 218 | loff_t len; |
| 219 | efi_status_t ret; |
| 220 | int r; |
| 221 | |
| 222 | buf = calloc(1, EFI_VAR_BUF_SIZE); |
| 223 | if (!buf) { |
| 224 | log_err("Out of memory\n"); |
| 225 | return EFI_OUT_OF_RESOURCES; |
| 226 | } |
| 227 | |
| 228 | ret = efi_set_blk_dev_to_system_partition(); |
| 229 | if (ret != EFI_SUCCESS) |
| 230 | goto error; |
| 231 | r = fs_read(EFI_VAR_FILE_NAME, map_to_sysmem(buf), 0, EFI_VAR_BUF_SIZE, |
| 232 | &len); |
| 233 | if (r || len < sizeof(struct efi_var_file)) { |
| 234 | log_err("Failed to load EFI variables\n"); |
| 235 | goto error; |
| 236 | } |
Heinrich Schuchardt | 211317c | 2021-08-25 19:13:24 +0200 | [diff] [blame] | 237 | if (buf->length != len || efi_var_restore(buf, false) != EFI_SUCCESS) |
Heinrich Schuchardt | 09a8d50 | 2020-03-19 18:21:58 +0000 | [diff] [blame] | 238 | log_err("Invalid EFI variables file\n"); |
| 239 | error: |
| 240 | free(buf); |
| 241 | #endif |
| 242 | return EFI_SUCCESS; |
| 243 | } |