blob: d12a21b2dd9d06de14e05caff9ac7bb114473383 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// 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
17#include "internal.h"
18
19LIST_HEAD(efivarfs_list);
20
21static void efivarfs_evict_inode(struct inode *inode)
22{
23 clear_inode(inode);
24}
25
26static const struct super_operations efivarfs_ops = {
27 .statfs = simple_statfs,
28 .drop_inode = generic_delete_inode,
29 .evict_inode = efivarfs_evict_inode,
30};
31
32/*
33 * Compare two efivarfs file names.
34 *
35 * An efivarfs filename is composed of two parts,
36 *
37 * 1. A case-sensitive variable name
38 * 2. A case-insensitive GUID
39 *
40 * So we need to perform a case-sensitive match on part 1 and a
41 * case-insensitive match on part 2.
42 */
43static int efivarfs_d_compare(const struct dentry *dentry,
44 unsigned int len, const char *str,
45 const struct qstr *name)
46{
47 int guid = len - EFI_VARIABLE_GUID_LEN;
48
49 if (name->len != len)
50 return 1;
51
52 /* Case-sensitive compare for the variable name */
53 if (memcmp(str, name->name, guid))
54 return 1;
55
56 /* Case-insensitive compare for the GUID */
57 return strncasecmp(name->name + guid, str + guid, EFI_VARIABLE_GUID_LEN);
58}
59
60static int efivarfs_d_hash(const struct dentry *dentry, struct qstr *qstr)
61{
62 unsigned long hash = init_name_hash(dentry);
63 const unsigned char *s = qstr->name;
64 unsigned int len = qstr->len;
65
66 while (len-- > EFI_VARIABLE_GUID_LEN)
67 hash = partial_name_hash(*s++, hash);
68
69 /* GUID is case-insensitive. */
70 while (len--)
71 hash = partial_name_hash(tolower(*s++), hash);
72
73 qstr->hash = end_name_hash(hash);
74 return 0;
75}
76
77static const struct dentry_operations efivarfs_d_ops = {
78 .d_compare = efivarfs_d_compare,
79 .d_hash = efivarfs_d_hash,
80 .d_delete = always_delete_dentry,
81};
82
83static struct dentry *efivarfs_alloc_dentry(struct dentry *parent, char *name)
84{
85 struct dentry *d;
86 struct qstr q;
87 int err;
88
89 q.name = name;
90 q.len = strlen(name);
91
92 err = efivarfs_d_hash(parent, &q);
93 if (err)
94 return ERR_PTR(err);
95
96 d = d_alloc(parent, &q);
97 if (d)
98 return d;
99
100 return ERR_PTR(-ENOMEM);
101}
102
103static int efivarfs_callback(efi_char16_t *name16, efi_guid_t vendor,
104 unsigned long name_size, void *data)
105{
106 struct super_block *sb = (struct super_block *)data;
107 struct efivar_entry *entry;
108 struct inode *inode = NULL;
109 struct dentry *dentry, *root = sb->s_root;
110 unsigned long size = 0;
111 char *name;
112 int len;
113 int err = -ENOMEM;
114 bool is_removable = false;
115
116 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
117 if (!entry)
118 return err;
119
120 memcpy(entry->var.VariableName, name16, name_size);
121 memcpy(&(entry->var.VendorGuid), &vendor, sizeof(efi_guid_t));
122
123 len = ucs2_utf8size(entry->var.VariableName);
124
125 /* name, plus '-', plus GUID, plus NUL*/
126 name = kmalloc(len + 1 + EFI_VARIABLE_GUID_LEN + 1, GFP_KERNEL);
127 if (!name)
128 goto fail;
129
130 ucs2_as_utf8(name, entry->var.VariableName, len);
131
132 if (efivar_variable_is_removable(entry->var.VendorGuid, name, len))
133 is_removable = true;
134
135 name[len] = '-';
136
137 efi_guid_to_str(&entry->var.VendorGuid, name + len + 1);
138
139 name[len + EFI_VARIABLE_GUID_LEN+1] = '\0';
140
141 /* replace invalid slashes like kobject_set_name_vargs does for /sys/firmware/efi/vars. */
142 strreplace(name, '/', '!');
143
144 inode = efivarfs_get_inode(sb, d_inode(root), S_IFREG | 0644, 0,
145 is_removable);
146 if (!inode)
147 goto fail_name;
148
149 dentry = efivarfs_alloc_dentry(root, name);
150 if (IS_ERR(dentry)) {
151 err = PTR_ERR(dentry);
152 goto fail_inode;
153 }
154
155 efivar_entry_size(entry, &size);
156 err = efivar_entry_add(entry, &efivarfs_list);
157 if (err)
158 goto fail_inode;
159
160 /* copied by the above to local storage in the dentry. */
161 kfree(name);
162
163 inode_lock(inode);
164 inode->i_private = entry;
165 i_size_write(inode, size + sizeof(entry->var.Attributes));
166 inode_unlock(inode);
167 d_add(dentry, inode);
168
169 return 0;
170
171fail_inode:
172 iput(inode);
173fail_name:
174 kfree(name);
175fail:
176 kfree(entry);
177 return err;
178}
179
180static int efivarfs_destroy(struct efivar_entry *entry, void *data)
181{
182 int err = efivar_entry_remove(entry);
183
184 if (err)
185 return err;
186 kfree(entry);
187 return 0;
188}
189
190static int efivarfs_fill_super(struct super_block *sb, struct fs_context *fc)
191{
192 struct inode *inode = NULL;
193 struct dentry *root;
194 int err;
195
196 sb->s_maxbytes = MAX_LFS_FILESIZE;
197 sb->s_blocksize = PAGE_SIZE;
198 sb->s_blocksize_bits = PAGE_SHIFT;
199 sb->s_magic = EFIVARFS_MAGIC;
200 sb->s_op = &efivarfs_ops;
201 sb->s_d_op = &efivarfs_d_ops;
202 sb->s_time_gran = 1;
203
204 inode = efivarfs_get_inode(sb, NULL, S_IFDIR | 0755, 0, true);
205 if (!inode)
206 return -ENOMEM;
207 inode->i_op = &efivarfs_dir_inode_operations;
208
209 root = d_make_root(inode);
210 sb->s_root = root;
211 if (!root)
212 return -ENOMEM;
213
214 INIT_LIST_HEAD(&efivarfs_list);
215
216 err = efivar_init(efivarfs_callback, (void *)sb, true, &efivarfs_list);
217 if (err)
218 __efivar_entry_iter(efivarfs_destroy, &efivarfs_list, NULL, NULL);
219
220 return err;
221}
222
223static int efivarfs_get_tree(struct fs_context *fc)
224{
225 return get_tree_single(fc, efivarfs_fill_super);
226}
227
228static const struct fs_context_operations efivarfs_context_ops = {
229 .get_tree = efivarfs_get_tree,
230};
231
232static int efivarfs_init_fs_context(struct fs_context *fc)
233{
234 fc->ops = &efivarfs_context_ops;
235 return 0;
236}
237
238static void efivarfs_kill_sb(struct super_block *sb)
239{
240 kill_litter_super(sb);
241
242 /* Remove all entries and destroy */
243 __efivar_entry_iter(efivarfs_destroy, &efivarfs_list, NULL, NULL);
244}
245
246static struct file_system_type efivarfs_type = {
247 .owner = THIS_MODULE,
248 .name = "efivarfs",
249 .init_fs_context = efivarfs_init_fs_context,
250 .kill_sb = efivarfs_kill_sb,
251};
252
253static __init int efivarfs_init(void)
254{
255 if (!efi_enabled(EFI_RUNTIME_SERVICES))
256 return -ENODEV;
257
258 if (!efivars_kobject())
259 return -ENODEV;
260
261 return register_filesystem(&efivarfs_type);
262}
263
264static __exit void efivarfs_exit(void)
265{
266 unregister_filesystem(&efivarfs_type);
267}
268
269MODULE_AUTHOR("Matthew Garrett, Jeremy Kerr");
270MODULE_DESCRIPTION("EFI Variable Filesystem");
271MODULE_LICENSE("GPL");
272MODULE_ALIAS_FS("efivarfs");
273
274module_init(efivarfs_init);
275module_exit(efivarfs_exit);