1 /* 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/slab.h> 13 14 #include "internal.h" 15 16 static ssize_t efivarfs_file_write(struct file *file, 17 const char __user *userbuf, size_t count, loff_t *ppos) 18 { 19 struct efivar_entry *var = file->private_data; 20 void *data; 21 u32 attributes; 22 struct inode *inode = file->f_mapping->host; 23 unsigned long datasize = count - sizeof(attributes); 24 ssize_t bytes = 0; 25 bool set = false; 26 27 if (count < sizeof(attributes)) 28 return -EINVAL; 29 30 if (copy_from_user(&attributes, userbuf, sizeof(attributes))) 31 return -EFAULT; 32 33 if (attributes & ~(EFI_VARIABLE_MASK)) 34 return -EINVAL; 35 36 data = kmalloc(datasize, GFP_KERNEL); 37 if (!data) 38 return -ENOMEM; 39 40 if (copy_from_user(data, userbuf + sizeof(attributes), datasize)) { 41 bytes = -EFAULT; 42 goto out; 43 } 44 45 bytes = efivar_entry_set_get_size(var, attributes, &datasize, 46 data, &set); 47 if (!set && bytes) 48 goto out; 49 50 if (bytes == -ENOENT) { 51 drop_nlink(inode); 52 d_delete(file->f_dentry); 53 dput(file->f_dentry); 54 } else { 55 mutex_lock(&inode->i_mutex); 56 i_size_write(inode, datasize + sizeof(attributes)); 57 mutex_unlock(&inode->i_mutex); 58 } 59 60 bytes = count; 61 62 out: 63 kfree(data); 64 65 return bytes; 66 } 67 68 static ssize_t efivarfs_file_read(struct file *file, char __user *userbuf, 69 size_t count, loff_t *ppos) 70 { 71 struct efivar_entry *var = file->private_data; 72 unsigned long datasize = 0; 73 u32 attributes; 74 void *data; 75 ssize_t size = 0; 76 int err; 77 78 err = efivar_entry_size(var, &datasize); 79 if (err) 80 return err; 81 82 data = kmalloc(datasize + sizeof(attributes), GFP_KERNEL); 83 84 if (!data) 85 return -ENOMEM; 86 87 size = efivar_entry_get(var, &attributes, &datasize, 88 data + sizeof(attributes)); 89 if (size) 90 goto out_free; 91 92 memcpy(data, &attributes, sizeof(attributes)); 93 size = simple_read_from_buffer(userbuf, count, ppos, 94 data, datasize + sizeof(attributes)); 95 out_free: 96 kfree(data); 97 98 return size; 99 } 100 101 const struct file_operations efivarfs_file_operations = { 102 .open = simple_open, 103 .read = efivarfs_file_read, 104 .write = efivarfs_file_write, 105 .llseek = no_llseek, 106 }; 107