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 | |
Heinrich Schuchardt | 963d7cc | 2024-10-18 03:18:36 +0200 | [diff] [blame] | 40 | if (efi_system_partition.uclass_id == UCLASS_INVALID) |
Heinrich Schuchardt | 09a8d50 | 2020-03-19 18:21:58 +0000 | [diff] [blame] | 41 | return EFI_DEVICE_ERROR; |
Heinrich Schuchardt | 963d7cc | 2024-10-18 03:18:36 +0200 | [diff] [blame] | 42 | |
Heinrich Schuchardt | 54f180d | 2021-05-25 18:00:13 +0200 | [diff] [blame] | 43 | snprintf(part_str, PART_STR_LEN, "%x:%x", |
Heinrich Schuchardt | 09a8d50 | 2020-03-19 18:21:58 +0000 | [diff] [blame] | 44 | efi_system_partition.devnum, efi_system_partition.part); |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 45 | 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] | 46 | part_str, FS_TYPE_ANY); |
Heinrich Schuchardt | 963d7cc | 2024-10-18 03:18:36 +0200 | [diff] [blame] | 47 | if (r) |
Heinrich Schuchardt | 09a8d50 | 2020-03-19 18:21:58 +0000 | [diff] [blame] | 48 | return EFI_DEVICE_ERROR; |
Heinrich Schuchardt | 963d7cc | 2024-10-18 03:18:36 +0200 | [diff] [blame] | 49 | |
Heinrich Schuchardt | 09a8d50 | 2020-03-19 18:21:58 +0000 | [diff] [blame] | 50 | return EFI_SUCCESS; |
| 51 | } |
| 52 | |
Heinrich Schuchardt | 09a8d50 | 2020-03-19 18:21:58 +0000 | [diff] [blame] | 53 | /** |
| 54 | * efi_var_to_file() - save non-volatile variables as file |
| 55 | * |
| 56 | * File ubootefi.var is created on the EFI system partion. |
| 57 | * |
| 58 | * Return: status code |
| 59 | */ |
| 60 | efi_status_t efi_var_to_file(void) |
| 61 | { |
| 62 | #ifdef CONFIG_EFI_VARIABLE_FILE_STORE |
| 63 | efi_status_t ret; |
| 64 | struct efi_var_file *buf; |
| 65 | loff_t len; |
| 66 | loff_t actlen; |
| 67 | int r; |
Heinrich Schuchardt | 963d7cc | 2024-10-18 03:18:36 +0200 | [diff] [blame] | 68 | static bool once; |
Heinrich Schuchardt | 09a8d50 | 2020-03-19 18:21:58 +0000 | [diff] [blame] | 69 | |
Ilias Apalodimas | a4d1b1b | 2020-07-23 15:49:49 +0300 | [diff] [blame] | 70 | ret = efi_var_collect(&buf, &len, EFI_VARIABLE_NON_VOLATILE); |
Heinrich Schuchardt | 09a8d50 | 2020-03-19 18:21:58 +0000 | [diff] [blame] | 71 | if (ret != EFI_SUCCESS) |
| 72 | goto error; |
| 73 | |
| 74 | ret = efi_set_blk_dev_to_system_partition(); |
Heinrich Schuchardt | 963d7cc | 2024-10-18 03:18:36 +0200 | [diff] [blame] | 75 | if (ret != EFI_SUCCESS) { |
| 76 | if (!once) { |
| 77 | log_warning("Cannot persist EFI variables without system partition\n"); |
| 78 | once = true; |
| 79 | } |
| 80 | goto out; |
| 81 | } |
| 82 | once = false; |
Heinrich Schuchardt | 09a8d50 | 2020-03-19 18:21:58 +0000 | [diff] [blame] | 83 | |
| 84 | r = fs_write(EFI_VAR_FILE_NAME, map_to_sysmem(buf), 0, len, &actlen); |
| 85 | if (r || len != actlen) |
| 86 | ret = EFI_DEVICE_ERROR; |
| 87 | |
| 88 | error: |
| 89 | if (ret != EFI_SUCCESS) |
| 90 | log_err("Failed to persist EFI variables\n"); |
Heinrich Schuchardt | 963d7cc | 2024-10-18 03:18:36 +0200 | [diff] [blame] | 91 | out: |
Heinrich Schuchardt | 09a8d50 | 2020-03-19 18:21:58 +0000 | [diff] [blame] | 92 | free(buf); |
| 93 | return ret; |
| 94 | #else |
| 95 | return EFI_SUCCESS; |
| 96 | #endif |
| 97 | } |
| 98 | |
Heinrich Schuchardt | 211317c | 2021-08-25 19:13:24 +0200 | [diff] [blame] | 99 | 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] | 100 | { |
| 101 | struct efi_var_entry *var, *last_var; |
Heinrich Schuchardt | 211317c | 2021-08-25 19:13:24 +0200 | [diff] [blame] | 102 | u16 *data; |
Heinrich Schuchardt | 09a8d50 | 2020-03-19 18:21:58 +0000 | [diff] [blame] | 103 | efi_status_t ret; |
| 104 | |
| 105 | if (buf->reserved || buf->magic != EFI_VAR_FILE_MAGIC || |
| 106 | buf->crc32 != crc32(0, (u8 *)buf->var, |
| 107 | buf->length - sizeof(struct efi_var_file))) { |
| 108 | log_err("Invalid EFI variables file\n"); |
| 109 | return EFI_INVALID_PARAMETER; |
| 110 | } |
| 111 | |
Heinrich Schuchardt | 09a8d50 | 2020-03-19 18:21:58 +0000 | [diff] [blame] | 112 | last_var = (struct efi_var_entry *)((u8 *)buf + buf->length); |
Heinrich Schuchardt | 211317c | 2021-08-25 19:13:24 +0200 | [diff] [blame] | 113 | for (var = buf->var; var < last_var; |
| 114 | var = (struct efi_var_entry *) |
| 115 | ALIGN((uintptr_t)data + var->length, 8)) { |
Heinrich Schuchardt | 09a8d50 | 2020-03-19 18:21:58 +0000 | [diff] [blame] | 116 | |
Heinrich Schuchardt | 211317c | 2021-08-25 19:13:24 +0200 | [diff] [blame] | 117 | data = var->name + u16_strlen(var->name) + 1; |
| 118 | |
| 119 | /* |
Heinrich Schuchardt | 50413bb | 2022-12-29 09:23:03 +0100 | [diff] [blame] | 120 | * Secure boot related and volatile variables shall only be |
Heinrich Schuchardt | 211317c | 2021-08-25 19:13:24 +0200 | [diff] [blame] | 121 | * restored from U-Boot's preseed. |
| 122 | */ |
| 123 | if (!safe && |
| 124 | (efi_auth_var_get_type(var->name, &var->guid) != |
| 125 | EFI_AUTH_VAR_NONE || |
Heinrich Schuchardt | dfa2278 | 2021-10-06 14:10:14 +0200 | [diff] [blame] | 126 | !guidcmp(&var->guid, &shim_lock_guid) || |
Heinrich Schuchardt | 211317c | 2021-08-25 19:13:24 +0200 | [diff] [blame] | 127 | !(var->attr & EFI_VARIABLE_NON_VOLATILE))) |
| 128 | continue; |
| 129 | if (!var->length) |
| 130 | continue; |
Ilias Apalodimas | 3a83082 | 2022-12-29 10:13:22 +0200 | [diff] [blame] | 131 | if (efi_var_mem_find(&var->guid, var->name, NULL)) |
| 132 | continue; |
Heinrich Schuchardt | 211317c | 2021-08-25 19:13:24 +0200 | [diff] [blame] | 133 | ret = efi_var_mem_ins(var->name, &var->guid, var->attr, |
| 134 | var->length, data, 0, NULL, |
| 135 | var->time); |
| 136 | if (ret != EFI_SUCCESS) |
| 137 | log_err("Failed to set EFI variable %ls\n", var->name); |
Heinrich Schuchardt | 09a8d50 | 2020-03-19 18:21:58 +0000 | [diff] [blame] | 138 | } |
| 139 | return EFI_SUCCESS; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * efi_var_from_file() - read variables from file |
| 144 | * |
| 145 | * File ubootefi.var is read from the EFI system partitions and the variables |
| 146 | * stored in the file are created. |
| 147 | * |
Heinrich Schuchardt | 6fde198 | 2023-11-13 15:50:16 +0100 | [diff] [blame] | 148 | * On first boot the file ubootefi.var does not exist yet. This is why we must |
| 149 | * return EFI_SUCCESS in this case. |
| 150 | * |
| 151 | * If the variable file is corrupted, e.g. incorrect CRC32, we do not want to |
| 152 | * 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] | 153 | * |
| 154 | * Return: status code |
| 155 | */ |
| 156 | efi_status_t efi_var_from_file(void) |
| 157 | { |
| 158 | #ifdef CONFIG_EFI_VARIABLE_FILE_STORE |
| 159 | struct efi_var_file *buf; |
| 160 | loff_t len; |
| 161 | efi_status_t ret; |
| 162 | int r; |
| 163 | |
| 164 | buf = calloc(1, EFI_VAR_BUF_SIZE); |
| 165 | if (!buf) { |
| 166 | log_err("Out of memory\n"); |
| 167 | return EFI_OUT_OF_RESOURCES; |
| 168 | } |
| 169 | |
| 170 | ret = efi_set_blk_dev_to_system_partition(); |
| 171 | if (ret != EFI_SUCCESS) |
| 172 | goto error; |
| 173 | r = fs_read(EFI_VAR_FILE_NAME, map_to_sysmem(buf), 0, EFI_VAR_BUF_SIZE, |
| 174 | &len); |
| 175 | if (r || len < sizeof(struct efi_var_file)) { |
| 176 | log_err("Failed to load EFI variables\n"); |
| 177 | goto error; |
| 178 | } |
Heinrich Schuchardt | 211317c | 2021-08-25 19:13:24 +0200 | [diff] [blame] | 179 | if (buf->length != len || efi_var_restore(buf, false) != EFI_SUCCESS) |
Heinrich Schuchardt | 09a8d50 | 2020-03-19 18:21:58 +0000 | [diff] [blame] | 180 | log_err("Invalid EFI variables file\n"); |
| 181 | error: |
| 182 | free(buf); |
| 183 | #endif |
| 184 | return EFI_SUCCESS; |
| 185 | } |