1 // SPDX-License-Identifier: GPL-2.0+
5 * This code exposes secure variables to user via sysfs
8 #define pr_fmt(fmt) "secvar-sysfs: "fmt
10 #include <linux/slab.h>
11 #include <linux/compat.h>
12 #include <linux/string.h>
14 #include <asm/secvar.h>
16 #define NAME_MAX_SIZE 1024
18 static struct kobject *secvar_kobj;
19 static struct kset *secvar_kset;
21 static ssize_t format_show(struct kobject *kobj, struct kobj_attribute *attr,
25 ssize_t len = secvar_ops->format(tmp, sizeof(tmp));
28 return sysfs_emit(buf, "%s\n", tmp);
30 pr_err("Error %zd reading format string\n", len);
32 pr_err("Got empty format string from backend\n");
38 static ssize_t size_show(struct kobject *kobj, struct kobj_attribute *attr,
44 rc = secvar_ops->get(kobj->name, strlen(kobj->name) + 1, NULL, &dsize);
47 pr_err("Error retrieving %s variable size %d\n", kobj->name, rc);
51 return sysfs_emit(buf, "%llu\n", dsize);
54 static ssize_t data_read(struct file *filep, struct kobject *kobj,
55 struct bin_attribute *attr, char *buf, loff_t off,
62 rc = secvar_ops->get(kobj->name, strlen(kobj->name) + 1, NULL, &dsize);
65 pr_err("Error getting %s variable size %d\n", kobj->name, rc);
68 pr_debug("dsize is %llu\n", dsize);
70 data = kzalloc(dsize, GFP_KERNEL);
74 rc = secvar_ops->get(kobj->name, strlen(kobj->name) + 1, data, &dsize);
76 pr_err("Error getting %s variable %d\n", kobj->name, rc);
80 rc = memory_read_from_buffer(buf, count, &off, data, dsize);
87 static ssize_t update_write(struct file *filep, struct kobject *kobj,
88 struct bin_attribute *attr, char *buf, loff_t off,
93 pr_debug("count is %ld\n", count);
94 rc = secvar_ops->set(kobj->name, strlen(kobj->name) + 1, buf, count);
96 pr_err("Error setting the %s variable %d\n", kobj->name, rc);
103 static struct kobj_attribute format_attr = __ATTR_RO(format);
105 static struct kobj_attribute size_attr = __ATTR_RO(size);
107 static struct bin_attribute data_attr = __BIN_ATTR_RO(data, 0);
109 static struct bin_attribute update_attr = __BIN_ATTR_WO(update, 0);
111 static struct bin_attribute *secvar_bin_attrs[] = {
117 static struct attribute *secvar_attrs[] = {
122 static const struct attribute_group secvar_attr_group = {
123 .attrs = secvar_attrs,
124 .bin_attrs = secvar_bin_attrs,
126 __ATTRIBUTE_GROUPS(secvar_attr);
128 static struct kobj_type secvar_ktype = {
129 .sysfs_ops = &kobj_sysfs_ops,
130 .default_groups = secvar_attr_groups,
133 static int update_kobj_size(void)
137 int rc = secvar_ops->max_size(&varsize);
142 data_attr.size = varsize;
143 update_attr.size = varsize;
148 static int secvar_sysfs_config(struct kobject *kobj)
150 struct attribute_group config_group = {
152 .attrs = (struct attribute **)secvar_ops->config_attrs,
155 if (secvar_ops->config_attrs)
156 return sysfs_create_group(kobj, &config_group);
161 static int add_var(const char *name)
163 struct kobject *kobj;
166 kobj = kzalloc(sizeof(*kobj), GFP_KERNEL);
170 kobject_init(kobj, &secvar_ktype);
172 rc = kobject_add(kobj, &secvar_kset->kobj, "%s", name);
174 pr_warn("kobject_add error %d for attribute: %s\n", rc,
180 kobject_uevent(kobj, KOBJ_ADD);
184 static int secvar_sysfs_load(void)
190 name = kzalloc(NAME_MAX_SIZE, GFP_KERNEL);
195 rc = secvar_ops->get_next(name, &namesize, NAME_MAX_SIZE);
198 pr_err("error getting secvar from firmware %d\n", rc);
212 static int secvar_sysfs_load_static(void)
214 const char * const *name_ptr = secvar_ops->var_names;
218 rc = add_var(*name_ptr);
227 static int secvar_sysfs_init(void)
233 pr_warn("Failed to retrieve secvar operations\n");
237 secvar_kobj = kobject_create_and_add("secvar", firmware_kobj);
239 pr_err("Failed to create firmware kobj\n");
243 rc = sysfs_create_file(secvar_kobj, &format_attr.attr);
245 pr_err("Failed to create format object\n");
250 secvar_kset = kset_create_and_add("vars", NULL, secvar_kobj);
252 pr_err("sysfs kobject registration failed\n");
257 rc = update_kobj_size();
259 pr_err("Cannot read the size of the attribute\n");
263 rc = secvar_sysfs_config(secvar_kobj);
265 pr_err("Failed to create config directory\n");
269 if (secvar_ops->get_next)
270 rc = secvar_sysfs_load();
272 rc = secvar_sysfs_load_static();
275 pr_err("Failed to create variable attributes\n");
279 // Due to sysfs limitations, we will only ever get a write buffer of
280 // up to 1 page in size. Print a warning if this is potentially going
281 // to cause problems, so that the user is aware.
282 secvar_ops->max_size(&max_size);
283 if (max_size > PAGE_SIZE)
284 pr_warn_ratelimited("PAGE_SIZE (%lu) is smaller than maximum object size (%llu), writes are limited to PAGE_SIZE\n",
285 PAGE_SIZE, max_size);
289 kobject_put(secvar_kobj);
293 late_initcall(secvar_sysfs_init);