1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2012 Red Hat, Inc.
4 * Copyright (C) 2012 Jeremy Kerr <jeremy.kerr@canonical.com>
9 #include <linux/ctype.h>
10 #include <linux/kmemleak.h>
11 #include <linux/slab.h>
12 #include <linux/uuid.h>
16 struct inode
*efivarfs_get_inode(struct super_block
*sb
,
17 const struct inode
*dir
, int mode
,
18 dev_t dev
, bool is_removable
)
20 struct inode
*inode
= new_inode(sb
);
23 inode
->i_ino
= get_next_ino();
25 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= current_time(inode
);
26 inode
->i_flags
= is_removable
? 0 : S_IMMUTABLE
;
27 switch (mode
& S_IFMT
) {
29 inode
->i_fop
= &efivarfs_file_operations
;
32 inode
->i_op
= &efivarfs_dir_inode_operations
;
33 inode
->i_fop
= &simple_dir_operations
;
42 * Return true if 'str' is a valid efivarfs filename of the form,
44 * VariableName-12345678-1234-1234-1234-1234567891bc
46 bool efivarfs_valid_name(const char *str
, int len
)
48 const char *s
= str
+ len
- EFI_VARIABLE_GUID_LEN
;
51 * We need a GUID, plus at least one letter for the variable name,
52 * plus the '-' separator
54 if (len
< EFI_VARIABLE_GUID_LEN
+ 2)
57 /* GUID must be preceded by a '-' */
62 * Validate that 's' is of the correct format, e.g.
64 * 12345678-1234-1234-1234-123456789abc
66 return uuid_is_valid(s
);
69 static int efivarfs_create(struct inode
*dir
, struct dentry
*dentry
,
70 umode_t mode
, bool excl
)
72 struct inode
*inode
= NULL
;
73 struct efivar_entry
*var
;
74 int namelen
, i
= 0, err
= 0;
75 bool is_removable
= false;
77 if (!efivarfs_valid_name(dentry
->d_name
.name
, dentry
->d_name
.len
))
80 var
= kzalloc(sizeof(struct efivar_entry
), GFP_KERNEL
);
84 /* length of the variable name itself: remove GUID and separator */
85 namelen
= dentry
->d_name
.len
- EFI_VARIABLE_GUID_LEN
- 1;
87 err
= guid_parse(dentry
->d_name
.name
+ namelen
+ 1, &var
->var
.VendorGuid
);
91 if (efivar_variable_is_removable(var
->var
.VendorGuid
,
92 dentry
->d_name
.name
, namelen
))
95 inode
= efivarfs_get_inode(dir
->i_sb
, dir
, mode
, 0, is_removable
);
101 for (i
= 0; i
< namelen
; i
++)
102 var
->var
.VariableName
[i
] = dentry
->d_name
.name
[i
];
104 var
->var
.VariableName
[i
] = '\0';
106 inode
->i_private
= var
;
107 kmemleak_ignore(var
);
109 err
= efivar_entry_add(var
, &efivarfs_list
);
113 d_instantiate(dentry
, inode
);
124 static int efivarfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
126 struct efivar_entry
*var
= d_inode(dentry
)->i_private
;
128 if (efivar_entry_delete(var
))
131 drop_nlink(d_inode(dentry
));
136 const struct inode_operations efivarfs_dir_inode_operations
= {
137 .lookup
= simple_lookup
,
138 .unlink
= efivarfs_unlink
,
139 .create
= efivarfs_create
,