2 * Copyright (C) 2012 Red Hat, Inc.
3 * Copyright (C) 2012 Jeremy Kerr <jeremy.kerr@canonical.com>
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.
10 #include <linux/efi.h>
15 static ssize_t efivarfs_file_write(struct file *file,
16 const char __user *userbuf, size_t count, loff_t *ppos)
18 struct efivar_entry *var = file->private_data;
21 struct inode *inode = file->f_mapping->host;
22 unsigned long datasize = count - sizeof(attributes);
26 if (count < sizeof(attributes))
29 if (copy_from_user(&attributes, userbuf, sizeof(attributes)))
32 if (attributes & ~(EFI_VARIABLE_MASK))
35 data = kmalloc(datasize, GFP_KERNEL);
39 if (copy_from_user(data, userbuf + sizeof(attributes), datasize)) {
44 bytes = efivar_entry_set_get_size(var, attributes, &datasize,
49 if (bytes == -ENOENT) {
51 d_delete(file->f_dentry);
54 mutex_lock(&inode->i_mutex);
55 i_size_write(inode, datasize + sizeof(attributes));
56 mutex_unlock(&inode->i_mutex);
67 static ssize_t efivarfs_file_read(struct file *file, char __user *userbuf,
68 size_t count, loff_t *ppos)
70 struct efivar_entry *var = file->private_data;
71 unsigned long datasize = 0;
77 err = efivar_entry_size(var, &datasize);
81 data = kmalloc(datasize + sizeof(attributes), GFP_KERNEL);
86 size = efivar_entry_get(var, &attributes, &datasize,
87 data + sizeof(attributes));
91 memcpy(data, &attributes, sizeof(attributes));
92 size = simple_read_from_buffer(userbuf, count, ppos,
93 data, datasize + sizeof(attributes));
100 const struct file_operations efivarfs_file_operations = {
102 .read = efivarfs_file_read,
103 .write = efivarfs_file_write,