xref: /openbmc/linux/fs/efivarfs/super.c (revision 55e43d6abd078ed6d219902ce8cb4d68e3c993ba)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2012 Red Hat, Inc.
4  * Copyright (C) 2012 Jeremy Kerr <jeremy.kerr@canonical.com>
5  */
6 
7 #include <linux/ctype.h>
8 #include <linux/efi.h>
9 #include <linux/fs.h>
10 #include <linux/fs_context.h>
11 #include <linux/module.h>
12 #include <linux/pagemap.h>
13 #include <linux/ucs2_string.h>
14 #include <linux/slab.h>
15 #include <linux/magic.h>
16 #include <linux/statfs.h>
17 #include <linux/printk.h>
18 
19 #include "internal.h"
20 
21 LIST_HEAD(efivarfs_list);
22 
efivarfs_evict_inode(struct inode * inode)23 static void efivarfs_evict_inode(struct inode *inode)
24 {
25 	clear_inode(inode);
26 }
27 
efivarfs_statfs(struct dentry * dentry,struct kstatfs * buf)28 static int efivarfs_statfs(struct dentry *dentry, struct kstatfs *buf)
29 {
30 	const u32 attr = EFI_VARIABLE_NON_VOLATILE |
31 			 EFI_VARIABLE_BOOTSERVICE_ACCESS |
32 			 EFI_VARIABLE_RUNTIME_ACCESS;
33 	u64 storage_space, remaining_space, max_variable_size;
34 	efi_status_t status;
35 
36 	/* Some UEFI firmware does not implement QueryVariableInfo() */
37 	storage_space = remaining_space = 0;
38 	if (efi_rt_services_supported(EFI_RT_SUPPORTED_QUERY_VARIABLE_INFO)) {
39 		status = efivar_query_variable_info(attr, &storage_space,
40 						    &remaining_space,
41 						    &max_variable_size);
42 		if (status != EFI_SUCCESS && status != EFI_UNSUPPORTED)
43 			pr_warn_ratelimited("query_variable_info() failed: 0x%lx\n",
44 					    status);
45 	}
46 
47 	/*
48 	 * This is not a normal filesystem, so no point in pretending it has a block
49 	 * size; we declare f_bsize to 1, so that we can then report the exact value
50 	 * sent by EFI QueryVariableInfo in f_blocks and f_bfree
51 	 */
52 	buf->f_bsize	= 1;
53 	buf->f_namelen	= NAME_MAX;
54 	buf->f_blocks	= storage_space;
55 	buf->f_bfree	= remaining_space;
56 	buf->f_type	= dentry->d_sb->s_magic;
57 
58 	/*
59 	 * In f_bavail we declare the free space that the kernel will allow writing
60 	 * when the storage_paranoia x86 quirk is active. To use more, users
61 	 * should boot the kernel with efi_no_storage_paranoia.
62 	 */
63 	if (remaining_space > efivar_reserved_space())
64 		buf->f_bavail = remaining_space - efivar_reserved_space();
65 	else
66 		buf->f_bavail = 0;
67 
68 	return 0;
69 }
70 static const struct super_operations efivarfs_ops = {
71 	.statfs = efivarfs_statfs,
72 	.drop_inode = generic_delete_inode,
73 	.evict_inode = efivarfs_evict_inode,
74 };
75 
76 /*
77  * Compare two efivarfs file names.
78  *
79  * An efivarfs filename is composed of two parts,
80  *
81  *	1. A case-sensitive variable name
82  *	2. A case-insensitive GUID
83  *
84  * So we need to perform a case-sensitive match on part 1 and a
85  * case-insensitive match on part 2.
86  */
efivarfs_d_compare(const struct dentry * dentry,unsigned int len,const char * str,const struct qstr * name)87 static int efivarfs_d_compare(const struct dentry *dentry,
88 			      unsigned int len, const char *str,
89 			      const struct qstr *name)
90 {
91 	int guid = len - EFI_VARIABLE_GUID_LEN;
92 
93 	if (name->len != len)
94 		return 1;
95 
96 	/* Case-sensitive compare for the variable name */
97 	if (memcmp(str, name->name, guid))
98 		return 1;
99 
100 	/* Case-insensitive compare for the GUID */
101 	return strncasecmp(name->name + guid, str + guid, EFI_VARIABLE_GUID_LEN);
102 }
103 
efivarfs_d_hash(const struct dentry * dentry,struct qstr * qstr)104 static int efivarfs_d_hash(const struct dentry *dentry, struct qstr *qstr)
105 {
106 	unsigned long hash = init_name_hash(dentry);
107 	const unsigned char *s = qstr->name;
108 	unsigned int len = qstr->len;
109 
110 	while (len-- > EFI_VARIABLE_GUID_LEN)
111 		hash = partial_name_hash(*s++, hash);
112 
113 	/* GUID is case-insensitive. */
114 	while (len--)
115 		hash = partial_name_hash(tolower(*s++), hash);
116 
117 	qstr->hash = end_name_hash(hash);
118 	return 0;
119 }
120 
121 static const struct dentry_operations efivarfs_d_ops = {
122 	.d_compare = efivarfs_d_compare,
123 	.d_hash = efivarfs_d_hash,
124 	.d_delete = always_delete_dentry,
125 };
126 
efivarfs_alloc_dentry(struct dentry * parent,char * name)127 static struct dentry *efivarfs_alloc_dentry(struct dentry *parent, char *name)
128 {
129 	struct dentry *d;
130 	struct qstr q;
131 	int err;
132 
133 	q.name = name;
134 	q.len = strlen(name);
135 
136 	err = efivarfs_d_hash(parent, &q);
137 	if (err)
138 		return ERR_PTR(err);
139 
140 	d = d_alloc(parent, &q);
141 	if (d)
142 		return d;
143 
144 	return ERR_PTR(-ENOMEM);
145 }
146 
efivarfs_callback(efi_char16_t * name16,efi_guid_t vendor,unsigned long name_size,void * data)147 static int efivarfs_callback(efi_char16_t *name16, efi_guid_t vendor,
148 			     unsigned long name_size, void *data)
149 {
150 	struct super_block *sb = (struct super_block *)data;
151 	struct efivar_entry *entry;
152 	struct inode *inode = NULL;
153 	struct dentry *dentry, *root = sb->s_root;
154 	unsigned long size = 0;
155 	char *name;
156 	int len;
157 	int err = -ENOMEM;
158 	bool is_removable = false;
159 
160 	if (guid_equal(&vendor, &LINUX_EFI_RANDOM_SEED_TABLE_GUID))
161 		return 0;
162 
163 	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
164 	if (!entry)
165 		return err;
166 
167 	memcpy(entry->var.VariableName, name16, name_size);
168 	memcpy(&(entry->var.VendorGuid), &vendor, sizeof(efi_guid_t));
169 
170 	len = ucs2_utf8size(entry->var.VariableName);
171 
172 	/* name, plus '-', plus GUID, plus NUL*/
173 	name = kmalloc(len + 1 + EFI_VARIABLE_GUID_LEN + 1, GFP_KERNEL);
174 	if (!name)
175 		goto fail;
176 
177 	ucs2_as_utf8(name, entry->var.VariableName, len);
178 
179 	if (efivar_variable_is_removable(entry->var.VendorGuid, name, len))
180 		is_removable = true;
181 
182 	name[len] = '-';
183 
184 	efi_guid_to_str(&entry->var.VendorGuid, name + len + 1);
185 
186 	name[len + EFI_VARIABLE_GUID_LEN+1] = '\0';
187 
188 	/* replace invalid slashes like kobject_set_name_vargs does for /sys/firmware/efi/vars. */
189 	strreplace(name, '/', '!');
190 
191 	inode = efivarfs_get_inode(sb, d_inode(root), S_IFREG | 0644, 0,
192 				   is_removable);
193 	if (!inode)
194 		goto fail_name;
195 
196 	dentry = efivarfs_alloc_dentry(root, name);
197 	if (IS_ERR(dentry)) {
198 		err = PTR_ERR(dentry);
199 		goto fail_inode;
200 	}
201 
202 	__efivar_entry_get(entry, NULL, &size, NULL);
203 	__efivar_entry_add(entry, &efivarfs_list);
204 
205 	/* copied by the above to local storage in the dentry. */
206 	kfree(name);
207 
208 	inode_lock(inode);
209 	inode->i_private = entry;
210 	i_size_write(inode, size + sizeof(entry->var.Attributes));
211 	inode_unlock(inode);
212 	d_add(dentry, inode);
213 
214 	return 0;
215 
216 fail_inode:
217 	iput(inode);
218 fail_name:
219 	kfree(name);
220 fail:
221 	kfree(entry);
222 	return err;
223 }
224 
efivarfs_destroy(struct efivar_entry * entry,void * data)225 static int efivarfs_destroy(struct efivar_entry *entry, void *data)
226 {
227 	efivar_entry_remove(entry);
228 	kfree(entry);
229 	return 0;
230 }
231 
efivarfs_fill_super(struct super_block * sb,struct fs_context * fc)232 static int efivarfs_fill_super(struct super_block *sb, struct fs_context *fc)
233 {
234 	struct inode *inode = NULL;
235 	struct dentry *root;
236 	int err;
237 
238 	if (!efivar_is_available())
239 		return -EOPNOTSUPP;
240 
241 	sb->s_maxbytes          = MAX_LFS_FILESIZE;
242 	sb->s_blocksize         = PAGE_SIZE;
243 	sb->s_blocksize_bits    = PAGE_SHIFT;
244 	sb->s_magic             = EFIVARFS_MAGIC;
245 	sb->s_op                = &efivarfs_ops;
246 	sb->s_d_op		= &efivarfs_d_ops;
247 	sb->s_time_gran         = 1;
248 
249 	if (!efivar_supports_writes())
250 		sb->s_flags |= SB_RDONLY;
251 
252 	inode = efivarfs_get_inode(sb, NULL, S_IFDIR | 0755, 0, true);
253 	if (!inode)
254 		return -ENOMEM;
255 	inode->i_op = &efivarfs_dir_inode_operations;
256 
257 	root = d_make_root(inode);
258 	sb->s_root = root;
259 	if (!root)
260 		return -ENOMEM;
261 
262 	INIT_LIST_HEAD(&efivarfs_list);
263 
264 	err = efivar_init(efivarfs_callback, (void *)sb, true, &efivarfs_list);
265 	if (err)
266 		efivar_entry_iter(efivarfs_destroy, &efivarfs_list, NULL);
267 
268 	return err;
269 }
270 
efivarfs_get_tree(struct fs_context * fc)271 static int efivarfs_get_tree(struct fs_context *fc)
272 {
273 	return get_tree_single(fc, efivarfs_fill_super);
274 }
275 
efivarfs_reconfigure(struct fs_context * fc)276 static int efivarfs_reconfigure(struct fs_context *fc)
277 {
278 	if (!efivar_supports_writes() && !(fc->sb_flags & SB_RDONLY)) {
279 		pr_err("Firmware does not support SetVariableRT. Can not remount with rw\n");
280 		return -EINVAL;
281 	}
282 
283 	return 0;
284 }
285 
286 static const struct fs_context_operations efivarfs_context_ops = {
287 	.get_tree	= efivarfs_get_tree,
288 	.reconfigure	= efivarfs_reconfigure,
289 };
290 
efivarfs_init_fs_context(struct fs_context * fc)291 static int efivarfs_init_fs_context(struct fs_context *fc)
292 {
293 	fc->ops = &efivarfs_context_ops;
294 	return 0;
295 }
296 
efivarfs_kill_sb(struct super_block * sb)297 static void efivarfs_kill_sb(struct super_block *sb)
298 {
299 	struct efivarfs_fs_info *sfi = sb->s_fs_info;
300 
301 	kill_litter_super(sb);
302 
303 	if (!efivar_is_available())
304 		return;
305 
306 	/* Remove all entries and destroy */
307 	efivar_entry_iter(efivarfs_destroy, &efivarfs_list, NULL);
308 	kfree(sfi);
309 }
310 
311 static struct file_system_type efivarfs_type = {
312 	.owner   = THIS_MODULE,
313 	.name    = "efivarfs",
314 	.init_fs_context = efivarfs_init_fs_context,
315 	.kill_sb = efivarfs_kill_sb,
316 };
317 
efivarfs_init(void)318 static __init int efivarfs_init(void)
319 {
320 	return register_filesystem(&efivarfs_type);
321 }
322 
efivarfs_exit(void)323 static __exit void efivarfs_exit(void)
324 {
325 	unregister_filesystem(&efivarfs_type);
326 }
327 
328 MODULE_AUTHOR("Matthew Garrett, Jeremy Kerr");
329 MODULE_DESCRIPTION("EFI Variable Filesystem");
330 MODULE_LICENSE("GPL");
331 MODULE_ALIAS_FS("efivarfs");
332 
333 module_init(efivarfs_init);
334 module_exit(efivarfs_exit);
335