blob: ba0bf33ffbd11a2d54bf37469c25d03f420f5e27 [file] [log] [blame]
Heinrich Schuchardt09a8d502020-03-19 18:21:58 +00001// 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 Schuchardt09a8d502020-03-19 18:21:58 +000010#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 Schuchardtdfa22782021-10-06 14:10:14 +020021/* 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
26static const efi_guid_t shim_lock_guid = SHIM_LOCK_GUID;
27
Heinrich Schuchardt09a8d502020-03-19 18:21:58 +000028/**
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 */
35static 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 Schuchardt963d7cc2024-10-18 03:18:36 +020040 if (efi_system_partition.uclass_id == UCLASS_INVALID)
Heinrich Schuchardt09a8d502020-03-19 18:21:58 +000041 return EFI_DEVICE_ERROR;
Heinrich Schuchardt963d7cc2024-10-18 03:18:36 +020042
Heinrich Schuchardt54f180d2021-05-25 18:00:13 +020043 snprintf(part_str, PART_STR_LEN, "%x:%x",
Heinrich Schuchardt09a8d502020-03-19 18:21:58 +000044 efi_system_partition.devnum, efi_system_partition.part);
Simon Glassfada3f92022-09-17 09:00:09 -060045 r = fs_set_blk_dev(blk_get_uclass_name(efi_system_partition.uclass_id),
Heinrich Schuchardt09a8d502020-03-19 18:21:58 +000046 part_str, FS_TYPE_ANY);
Heinrich Schuchardt963d7cc2024-10-18 03:18:36 +020047 if (r)
Heinrich Schuchardt09a8d502020-03-19 18:21:58 +000048 return EFI_DEVICE_ERROR;
Heinrich Schuchardt963d7cc2024-10-18 03:18:36 +020049
Heinrich Schuchardt09a8d502020-03-19 18:21:58 +000050 return EFI_SUCCESS;
51}
52
Heinrich Schuchardt09a8d502020-03-19 18:21:58 +000053/**
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 */
60efi_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 Schuchardt963d7cc2024-10-18 03:18:36 +020068 static bool once;
Heinrich Schuchardt09a8d502020-03-19 18:21:58 +000069
Ilias Apalodimasa4d1b1b2020-07-23 15:49:49 +030070 ret = efi_var_collect(&buf, &len, EFI_VARIABLE_NON_VOLATILE);
Heinrich Schuchardt09a8d502020-03-19 18:21:58 +000071 if (ret != EFI_SUCCESS)
72 goto error;
73
74 ret = efi_set_blk_dev_to_system_partition();
Heinrich Schuchardt963d7cc2024-10-18 03:18:36 +020075 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 Schuchardt09a8d502020-03-19 18:21:58 +000083
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
88error:
89 if (ret != EFI_SUCCESS)
90 log_err("Failed to persist EFI variables\n");
Heinrich Schuchardt963d7cc2024-10-18 03:18:36 +020091out:
Heinrich Schuchardt09a8d502020-03-19 18:21:58 +000092 free(buf);
93 return ret;
94#else
95 return EFI_SUCCESS;
96#endif
97}
98
Heinrich Schuchardt211317c2021-08-25 19:13:24 +020099efi_status_t efi_var_restore(struct efi_var_file *buf, bool safe)
Heinrich Schuchardt09a8d502020-03-19 18:21:58 +0000100{
101 struct efi_var_entry *var, *last_var;
Heinrich Schuchardt211317c2021-08-25 19:13:24 +0200102 u16 *data;
Heinrich Schuchardt09a8d502020-03-19 18:21:58 +0000103 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 Schuchardt09a8d502020-03-19 18:21:58 +0000112 last_var = (struct efi_var_entry *)((u8 *)buf + buf->length);
Heinrich Schuchardt211317c2021-08-25 19:13:24 +0200113 for (var = buf->var; var < last_var;
114 var = (struct efi_var_entry *)
115 ALIGN((uintptr_t)data + var->length, 8)) {
Heinrich Schuchardt09a8d502020-03-19 18:21:58 +0000116
Heinrich Schuchardt211317c2021-08-25 19:13:24 +0200117 data = var->name + u16_strlen(var->name) + 1;
118
119 /*
Heinrich Schuchardt50413bb2022-12-29 09:23:03 +0100120 * Secure boot related and volatile variables shall only be
Heinrich Schuchardt211317c2021-08-25 19:13:24 +0200121 * 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 Schuchardtdfa22782021-10-06 14:10:14 +0200126 !guidcmp(&var->guid, &shim_lock_guid) ||
Heinrich Schuchardt211317c2021-08-25 19:13:24 +0200127 !(var->attr & EFI_VARIABLE_NON_VOLATILE)))
128 continue;
129 if (!var->length)
130 continue;
Ilias Apalodimas3a830822022-12-29 10:13:22 +0200131 if (efi_var_mem_find(&var->guid, var->name, NULL))
132 continue;
Heinrich Schuchardt211317c2021-08-25 19:13:24 +0200133 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 Schuchardt09a8d502020-03-19 18:21:58 +0000138 }
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 Schuchardt6fde1982023-11-13 15:50:16 +0100148 * 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 Schuchardt09a8d502020-03-19 18:21:58 +0000153 *
154 * Return: status code
155 */
156efi_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 Schuchardt211317c2021-08-25 19:13:24 +0200179 if (buf->length != len || efi_var_restore(buf, false) != EFI_SUCCESS)
Heinrich Schuchardt09a8d502020-03-19 18:21:58 +0000180 log_err("Invalid EFI variables file\n");
181error:
182 free(buf);
183#endif
184 return EFI_SUCCESS;
185}