xref: /openbmc/linux/arch/powerpc/kernel/secvar-sysfs.c (revision ec2f40bd004b4b9142469282d4a6ce9afa22f9c0)
1bd5d9c74SNayna Jain // SPDX-License-Identifier: GPL-2.0+
2bd5d9c74SNayna Jain /*
3bd5d9c74SNayna Jain  * Copyright (C) 2019 IBM Corporation <nayna@linux.ibm.com>
4bd5d9c74SNayna Jain  *
5bd5d9c74SNayna Jain  * This code exposes secure variables to user via sysfs
6bd5d9c74SNayna Jain  */
7bd5d9c74SNayna Jain 
8bd5d9c74SNayna Jain #define pr_fmt(fmt) "secvar-sysfs: "fmt
9bd5d9c74SNayna Jain 
10bd5d9c74SNayna Jain #include <linux/slab.h>
11bd5d9c74SNayna Jain #include <linux/compat.h>
12bd5d9c74SNayna Jain #include <linux/string.h>
13bd5d9c74SNayna Jain #include <linux/of.h>
14bd5d9c74SNayna Jain #include <asm/secvar.h>
15bd5d9c74SNayna Jain 
16bd5d9c74SNayna Jain #define NAME_MAX_SIZE	   1024
17bd5d9c74SNayna Jain 
18bd5d9c74SNayna Jain static struct kobject *secvar_kobj;
19bd5d9c74SNayna Jain static struct kset *secvar_kset;
20bd5d9c74SNayna Jain 
21bd5d9c74SNayna Jain static ssize_t format_show(struct kobject *kobj, struct kobj_attribute *attr,
22bd5d9c74SNayna Jain 			   char *buf)
23bd5d9c74SNayna Jain {
24*ec2f40bdSRussell Currey 	char tmp[32];
25*ec2f40bdSRussell Currey 	ssize_t len = secvar_ops->format(tmp, sizeof(tmp));
26bd5d9c74SNayna Jain 
27*ec2f40bdSRussell Currey 	if (len > 0)
28*ec2f40bdSRussell Currey 		return sysfs_emit(buf, "%s\n", tmp);
29*ec2f40bdSRussell Currey 	else if (len < 0)
30*ec2f40bdSRussell Currey 		pr_err("Error %zd reading format string\n", len);
31*ec2f40bdSRussell Currey 	else
32*ec2f40bdSRussell Currey 		pr_err("Got empty format string from backend\n");
33bd5d9c74SNayna Jain 
34*ec2f40bdSRussell Currey 	return -EIO;
35bd5d9c74SNayna Jain }
36bd5d9c74SNayna Jain 
37bd5d9c74SNayna Jain 
38bd5d9c74SNayna Jain static ssize_t size_show(struct kobject *kobj, struct kobj_attribute *attr,
39bd5d9c74SNayna Jain 			 char *buf)
40bd5d9c74SNayna Jain {
4153cea34bSMichael Ellerman 	u64 dsize;
42bd5d9c74SNayna Jain 	int rc;
43bd5d9c74SNayna Jain 
44bd5d9c74SNayna Jain 	rc = secvar_ops->get(kobj->name, strlen(kobj->name) + 1, NULL, &dsize);
45bd5d9c74SNayna Jain 	if (rc) {
46bd5d9c74SNayna Jain 		pr_err("Error retrieving %s variable size %d\n", kobj->name,
47bd5d9c74SNayna Jain 		       rc);
48bd5d9c74SNayna Jain 		return rc;
49bd5d9c74SNayna Jain 	}
50bd5d9c74SNayna Jain 
5116943a2fSRussell Currey 	return sysfs_emit(buf, "%llu\n", dsize);
52bd5d9c74SNayna Jain }
53bd5d9c74SNayna Jain 
54bd5d9c74SNayna Jain static ssize_t data_read(struct file *filep, struct kobject *kobj,
55bd5d9c74SNayna Jain 			 struct bin_attribute *attr, char *buf, loff_t off,
56bd5d9c74SNayna Jain 			 size_t count)
57bd5d9c74SNayna Jain {
58bd5d9c74SNayna Jain 	char *data;
5953cea34bSMichael Ellerman 	u64 dsize;
60bd5d9c74SNayna Jain 	int rc;
61bd5d9c74SNayna Jain 
62bd5d9c74SNayna Jain 	rc = secvar_ops->get(kobj->name, strlen(kobj->name) + 1, NULL, &dsize);
63bd5d9c74SNayna Jain 	if (rc) {
64bd5d9c74SNayna Jain 		pr_err("Error getting %s variable size %d\n", kobj->name, rc);
65bd5d9c74SNayna Jain 		return rc;
66bd5d9c74SNayna Jain 	}
67bd5d9c74SNayna Jain 	pr_debug("dsize is %llu\n", dsize);
68bd5d9c74SNayna Jain 
69bd5d9c74SNayna Jain 	data = kzalloc(dsize, GFP_KERNEL);
70bd5d9c74SNayna Jain 	if (!data)
71bd5d9c74SNayna Jain 		return -ENOMEM;
72bd5d9c74SNayna Jain 
73bd5d9c74SNayna Jain 	rc = secvar_ops->get(kobj->name, strlen(kobj->name) + 1, data, &dsize);
74bd5d9c74SNayna Jain 	if (rc) {
75bd5d9c74SNayna Jain 		pr_err("Error getting %s variable %d\n", kobj->name, rc);
76bd5d9c74SNayna Jain 		goto data_fail;
77bd5d9c74SNayna Jain 	}
78bd5d9c74SNayna Jain 
79bd5d9c74SNayna Jain 	rc = memory_read_from_buffer(buf, count, &off, data, dsize);
80bd5d9c74SNayna Jain 
81bd5d9c74SNayna Jain data_fail:
82bd5d9c74SNayna Jain 	kfree(data);
83bd5d9c74SNayna Jain 	return rc;
84bd5d9c74SNayna Jain }
85bd5d9c74SNayna Jain 
86bd5d9c74SNayna Jain static ssize_t update_write(struct file *filep, struct kobject *kobj,
87bd5d9c74SNayna Jain 			    struct bin_attribute *attr, char *buf, loff_t off,
88bd5d9c74SNayna Jain 			    size_t count)
89bd5d9c74SNayna Jain {
90bd5d9c74SNayna Jain 	int rc;
91bd5d9c74SNayna Jain 
92bd5d9c74SNayna Jain 	pr_debug("count is %ld\n", count);
93bd5d9c74SNayna Jain 	rc = secvar_ops->set(kobj->name, strlen(kobj->name) + 1, buf, count);
94bd5d9c74SNayna Jain 	if (rc) {
95bd5d9c74SNayna Jain 		pr_err("Error setting the %s variable %d\n", kobj->name, rc);
96bd5d9c74SNayna Jain 		return rc;
97bd5d9c74SNayna Jain 	}
98bd5d9c74SNayna Jain 
99bd5d9c74SNayna Jain 	return count;
100bd5d9c74SNayna Jain }
101bd5d9c74SNayna Jain 
102bd5d9c74SNayna Jain static struct kobj_attribute format_attr = __ATTR_RO(format);
103bd5d9c74SNayna Jain 
104bd5d9c74SNayna Jain static struct kobj_attribute size_attr = __ATTR_RO(size);
105bd5d9c74SNayna Jain 
106bd5d9c74SNayna Jain static struct bin_attribute data_attr = __BIN_ATTR_RO(data, 0);
107bd5d9c74SNayna Jain 
108bd5d9c74SNayna Jain static struct bin_attribute update_attr = __BIN_ATTR_WO(update, 0);
109bd5d9c74SNayna Jain 
110bd5d9c74SNayna Jain static struct bin_attribute *secvar_bin_attrs[] = {
111bd5d9c74SNayna Jain 	&data_attr,
112bd5d9c74SNayna Jain 	&update_attr,
113bd5d9c74SNayna Jain 	NULL,
114bd5d9c74SNayna Jain };
115bd5d9c74SNayna Jain 
116bd5d9c74SNayna Jain static struct attribute *secvar_attrs[] = {
117bd5d9c74SNayna Jain 	&size_attr.attr,
118bd5d9c74SNayna Jain 	NULL,
119bd5d9c74SNayna Jain };
120bd5d9c74SNayna Jain 
121bd5d9c74SNayna Jain static const struct attribute_group secvar_attr_group = {
122bd5d9c74SNayna Jain 	.attrs = secvar_attrs,
123bd5d9c74SNayna Jain 	.bin_attrs = secvar_bin_attrs,
124bd5d9c74SNayna Jain };
125bd5d9c74SNayna Jain __ATTRIBUTE_GROUPS(secvar_attr);
126bd5d9c74SNayna Jain 
127bd5d9c74SNayna Jain static struct kobj_type secvar_ktype = {
128bd5d9c74SNayna Jain 	.sysfs_ops	= &kobj_sysfs_ops,
129bd5d9c74SNayna Jain 	.default_groups = secvar_attr_groups,
130bd5d9c74SNayna Jain };
131bd5d9c74SNayna Jain 
132bd5d9c74SNayna Jain static int update_kobj_size(void)
133bd5d9c74SNayna Jain {
134bd5d9c74SNayna Jain 
135bd5d9c74SNayna Jain 	struct device_node *node;
136bd5d9c74SNayna Jain 	u64 varsize;
137bd5d9c74SNayna Jain 	int rc = 0;
138bd5d9c74SNayna Jain 
139bd5d9c74SNayna Jain 	node = of_find_compatible_node(NULL, NULL, "ibm,secvar-backend");
140bd5d9c74SNayna Jain 	if (!of_device_is_available(node)) {
141bd5d9c74SNayna Jain 		rc = -ENODEV;
142bd5d9c74SNayna Jain 		goto out;
143bd5d9c74SNayna Jain 	}
144bd5d9c74SNayna Jain 
145bd5d9c74SNayna Jain 	rc = of_property_read_u64(node, "max-var-size", &varsize);
146bd5d9c74SNayna Jain 	if (rc)
147bd5d9c74SNayna Jain 		goto out;
148bd5d9c74SNayna Jain 
149bd5d9c74SNayna Jain 	data_attr.size = varsize;
150bd5d9c74SNayna Jain 	update_attr.size = varsize;
151bd5d9c74SNayna Jain 
152bd5d9c74SNayna Jain out:
153bd5d9c74SNayna Jain 	of_node_put(node);
154bd5d9c74SNayna Jain 
155bd5d9c74SNayna Jain 	return rc;
156bd5d9c74SNayna Jain }
157bd5d9c74SNayna Jain 
158bd5d9c74SNayna Jain static int secvar_sysfs_load(void)
159bd5d9c74SNayna Jain {
160bd5d9c74SNayna Jain 	struct kobject *kobj;
16153cea34bSMichael Ellerman 	u64 namesize = 0;
16253cea34bSMichael Ellerman 	char *name;
163bd5d9c74SNayna Jain 	int rc;
164bd5d9c74SNayna Jain 
165bd5d9c74SNayna Jain 	name = kzalloc(NAME_MAX_SIZE, GFP_KERNEL);
166bd5d9c74SNayna Jain 	if (!name)
167bd5d9c74SNayna Jain 		return -ENOMEM;
168bd5d9c74SNayna Jain 
169bd5d9c74SNayna Jain 	do {
170bd5d9c74SNayna Jain 		rc = secvar_ops->get_next(name, &namesize, NAME_MAX_SIZE);
171bd5d9c74SNayna Jain 		if (rc) {
172bd5d9c74SNayna Jain 			if (rc != -ENOENT)
173c9fd2952SRussell Currey 				pr_err("error getting secvar from firmware %d\n", rc);
174c9fd2952SRussell Currey 			else
175c9fd2952SRussell Currey 				rc = 0;
176c9fd2952SRussell Currey 
177bd5d9c74SNayna Jain 			break;
178bd5d9c74SNayna Jain 		}
179bd5d9c74SNayna Jain 
180bd5d9c74SNayna Jain 		kobj = kzalloc(sizeof(*kobj), GFP_KERNEL);
181bd5d9c74SNayna Jain 		if (!kobj) {
182bd5d9c74SNayna Jain 			rc = -ENOMEM;
183bd5d9c74SNayna Jain 			break;
184bd5d9c74SNayna Jain 		}
185bd5d9c74SNayna Jain 
186bd5d9c74SNayna Jain 		kobject_init(kobj, &secvar_ktype);
187bd5d9c74SNayna Jain 
188bd5d9c74SNayna Jain 		rc = kobject_add(kobj, &secvar_kset->kobj, "%s", name);
189bd5d9c74SNayna Jain 		if (rc) {
190bd5d9c74SNayna Jain 			pr_warn("kobject_add error %d for attribute: %s\n", rc,
191bd5d9c74SNayna Jain 				name);
192bd5d9c74SNayna Jain 			kobject_put(kobj);
193bd5d9c74SNayna Jain 			kobj = NULL;
194bd5d9c74SNayna Jain 		}
195bd5d9c74SNayna Jain 
196bd5d9c74SNayna Jain 		if (kobj)
197bd5d9c74SNayna Jain 			kobject_uevent(kobj, KOBJ_ADD);
198bd5d9c74SNayna Jain 
199bd5d9c74SNayna Jain 	} while (!rc);
200bd5d9c74SNayna Jain 
201bd5d9c74SNayna Jain 	kfree(name);
202bd5d9c74SNayna Jain 	return rc;
203bd5d9c74SNayna Jain }
204bd5d9c74SNayna Jain 
205bd5d9c74SNayna Jain static int secvar_sysfs_init(void)
206bd5d9c74SNayna Jain {
207bd5d9c74SNayna Jain 	int rc;
208bd5d9c74SNayna Jain 
209bd5d9c74SNayna Jain 	if (!secvar_ops) {
210bd5d9c74SNayna Jain 		pr_warn("secvar: failed to retrieve secvar operations.\n");
211bd5d9c74SNayna Jain 		return -ENODEV;
212bd5d9c74SNayna Jain 	}
213bd5d9c74SNayna Jain 
214bd5d9c74SNayna Jain 	secvar_kobj = kobject_create_and_add("secvar", firmware_kobj);
215bd5d9c74SNayna Jain 	if (!secvar_kobj) {
216bd5d9c74SNayna Jain 		pr_err("secvar: Failed to create firmware kobj\n");
217bd5d9c74SNayna Jain 		return -ENOMEM;
218bd5d9c74SNayna Jain 	}
219bd5d9c74SNayna Jain 
220bd5d9c74SNayna Jain 	rc = sysfs_create_file(secvar_kobj, &format_attr.attr);
221bd5d9c74SNayna Jain 	if (rc) {
222bd5d9c74SNayna Jain 		kobject_put(secvar_kobj);
223bd5d9c74SNayna Jain 		return -ENOMEM;
224bd5d9c74SNayna Jain 	}
225bd5d9c74SNayna Jain 
226bd5d9c74SNayna Jain 	secvar_kset = kset_create_and_add("vars", NULL, secvar_kobj);
227bd5d9c74SNayna Jain 	if (!secvar_kset) {
228bd5d9c74SNayna Jain 		pr_err("secvar: sysfs kobject registration failed.\n");
229bd5d9c74SNayna Jain 		kobject_put(secvar_kobj);
230bd5d9c74SNayna Jain 		return -ENOMEM;
231bd5d9c74SNayna Jain 	}
232bd5d9c74SNayna Jain 
233bd5d9c74SNayna Jain 	rc = update_kobj_size();
234bd5d9c74SNayna Jain 	if (rc) {
235bd5d9c74SNayna Jain 		pr_err("Cannot read the size of the attribute\n");
236bd5d9c74SNayna Jain 		return rc;
237bd5d9c74SNayna Jain 	}
238bd5d9c74SNayna Jain 
239bd5d9c74SNayna Jain 	secvar_sysfs_load();
240bd5d9c74SNayna Jain 
241bd5d9c74SNayna Jain 	return 0;
242bd5d9c74SNayna Jain }
243bd5d9c74SNayna Jain 
244bd5d9c74SNayna Jain late_initcall(secvar_sysfs_init);
245