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