blob: 71fccccf317e8849580457680d6bd6ac3376c170 [file] [log] [blame]
Matt Flemingd68772b2013-02-08 16:27:24 +00001/*
2 * Copyright (C) 2012 Red Hat, Inc.
3 * Copyright (C) 2012 Jeremy Kerr <jeremy.kerr@canonical.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 */
9
10#include <linux/efi.h>
11#include <linux/fs.h>
12#include <linux/ctype.h>
Linus Torvalds20b4fb42013-05-01 17:51:54 -070013#include <linux/slab.h>
Andy Shevchenko82364312016-05-20 17:01:21 -070014#include <linux/uuid.h>
Matt Flemingd68772b2013-02-08 16:27:24 +000015
16#include "internal.h"
17
18struct inode *efivarfs_get_inode(struct super_block *sb,
Peter Jonesed8b0de2016-02-08 14:48:15 -050019 const struct inode *dir, int mode,
20 dev_t dev, bool is_removable)
Matt Flemingd68772b2013-02-08 16:27:24 +000021{
22 struct inode *inode = new_inode(sb);
23
24 if (inode) {
25 inode->i_ino = get_next_ino();
26 inode->i_mode = mode;
Deepa Dinamani078cd822016-09-14 07:48:04 -070027 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
Peter Jonesed8b0de2016-02-08 14:48:15 -050028 inode->i_flags = is_removable ? 0 : S_IMMUTABLE;
Matt Flemingd68772b2013-02-08 16:27:24 +000029 switch (mode & S_IFMT) {
30 case S_IFREG:
31 inode->i_fop = &efivarfs_file_operations;
32 break;
33 case S_IFDIR:
34 inode->i_op = &efivarfs_dir_inode_operations;
35 inode->i_fop = &simple_dir_operations;
36 inc_nlink(inode);
37 break;
38 }
39 }
40 return inode;
41}
42
43/*
44 * Return true if 'str' is a valid efivarfs filename of the form,
45 *
46 * VariableName-12345678-1234-1234-1234-1234567891bc
47 */
48bool efivarfs_valid_name(const char *str, int len)
49{
Matt Flemingd68772b2013-02-08 16:27:24 +000050 const char *s = str + len - EFI_VARIABLE_GUID_LEN;
Matt Flemingd68772b2013-02-08 16:27:24 +000051
52 /*
53 * We need a GUID, plus at least one letter for the variable name,
54 * plus the '-' separator
55 */
56 if (len < EFI_VARIABLE_GUID_LEN + 2)
57 return false;
58
59 /* GUID must be preceded by a '-' */
60 if (*(s - 1) != '-')
61 return false;
62
63 /*
64 * Validate that 's' is of the correct format, e.g.
65 *
66 * 12345678-1234-1234-1234-123456789abc
67 */
Andy Shevchenko82364312016-05-20 17:01:21 -070068 return uuid_is_valid(s);
Matt Flemingd68772b2013-02-08 16:27:24 +000069}
70
71static int efivarfs_create(struct inode *dir, struct dentry *dentry,
72 umode_t mode, bool excl)
73{
Peter Jonesed8b0de2016-02-08 14:48:15 -050074 struct inode *inode = NULL;
Matt Flemingd68772b2013-02-08 16:27:24 +000075 struct efivar_entry *var;
76 int namelen, i = 0, err = 0;
Peter Jonesed8b0de2016-02-08 14:48:15 -050077 bool is_removable = false;
Matt Flemingd68772b2013-02-08 16:27:24 +000078
79 if (!efivarfs_valid_name(dentry->d_name.name, dentry->d_name.len))
80 return -EINVAL;
81
Matt Flemingd68772b2013-02-08 16:27:24 +000082 var = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL);
Peter Jonesed8b0de2016-02-08 14:48:15 -050083 if (!var)
84 return -ENOMEM;
Matt Flemingd68772b2013-02-08 16:27:24 +000085
86 /* length of the variable name itself: remove GUID and separator */
87 namelen = dentry->d_name.len - EFI_VARIABLE_GUID_LEN - 1;
88
Andy Shevchenko82364312016-05-20 17:01:21 -070089 uuid_le_to_bin(dentry->d_name.name + namelen + 1, &var->var.VendorGuid);
Matt Flemingd68772b2013-02-08 16:27:24 +000090
Peter Jonesed8b0de2016-02-08 14:48:15 -050091 if (efivar_variable_is_removable(var->var.VendorGuid,
92 dentry->d_name.name, namelen))
93 is_removable = true;
94
95 inode = efivarfs_get_inode(dir->i_sb, dir, mode, 0, is_removable);
96 if (!inode) {
97 err = -ENOMEM;
98 goto out;
99 }
100
Matt Flemingd68772b2013-02-08 16:27:24 +0000101 for (i = 0; i < namelen; i++)
102 var->var.VariableName[i] = dentry->d_name.name[i];
103
104 var->var.VariableName[i] = '\0';
105
106 inode->i_private = var;
107
Sylvain Chouleur21b3ddd2016-07-15 21:36:30 +0200108 err = efivar_entry_add(var, &efivarfs_list);
109 if (err)
110 goto out;
111
Matt Flemingd68772b2013-02-08 16:27:24 +0000112 d_instantiate(dentry, inode);
113 dget(dentry);
114out:
115 if (err) {
116 kfree(var);
Peter Jonesed8b0de2016-02-08 14:48:15 -0500117 if (inode)
118 iput(inode);
Matt Flemingd68772b2013-02-08 16:27:24 +0000119 }
120 return err;
121}
122
123static int efivarfs_unlink(struct inode *dir, struct dentry *dentry)
124{
David Howells2b0143b2015-03-17 22:25:59 +0000125 struct efivar_entry *var = d_inode(dentry)->i_private;
Matt Flemingd68772b2013-02-08 16:27:24 +0000126
127 if (efivar_entry_delete(var))
128 return -EINVAL;
129
David Howells2b0143b2015-03-17 22:25:59 +0000130 drop_nlink(d_inode(dentry));
Matt Flemingd68772b2013-02-08 16:27:24 +0000131 dput(dentry);
132 return 0;
133};
134
Matt Flemingd68772b2013-02-08 16:27:24 +0000135const struct inode_operations efivarfs_dir_inode_operations = {
Al Viro6e8cd2c2013-07-14 17:48:35 +0400136 .lookup = simple_lookup,
Matt Flemingd68772b2013-02-08 16:27:24 +0000137 .unlink = efivarfs_unlink,
138 .create = efivarfs_create,
139};