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 | #include <linux/ctype.h> |
Linus Torvalds | 20b4fb4 | 2013-05-01 17:51:54 -0700 | [diff] [blame] | 13 | #include <linux/slab.h> |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 14 | |
| 15 | #include "internal.h" |
| 16 | |
| 17 | struct inode *efivarfs_get_inode(struct super_block *sb, |
| 18 | const struct inode *dir, int mode, dev_t dev) |
| 19 | { |
| 20 | struct inode *inode = new_inode(sb); |
| 21 | |
| 22 | if (inode) { |
| 23 | inode->i_ino = get_next_ino(); |
| 24 | inode->i_mode = mode; |
| 25 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
| 26 | switch (mode & S_IFMT) { |
| 27 | case S_IFREG: |
| 28 | inode->i_fop = &efivarfs_file_operations; |
| 29 | break; |
| 30 | case S_IFDIR: |
| 31 | inode->i_op = &efivarfs_dir_inode_operations; |
| 32 | inode->i_fop = &simple_dir_operations; |
| 33 | inc_nlink(inode); |
| 34 | break; |
| 35 | } |
| 36 | } |
| 37 | return inode; |
| 38 | } |
| 39 | |
| 40 | /* |
| 41 | * Return true if 'str' is a valid efivarfs filename of the form, |
| 42 | * |
| 43 | * VariableName-12345678-1234-1234-1234-1234567891bc |
| 44 | */ |
| 45 | bool efivarfs_valid_name(const char *str, int len) |
| 46 | { |
| 47 | static const char dashes[EFI_VARIABLE_GUID_LEN] = { |
| 48 | [8] = 1, [13] = 1, [18] = 1, [23] = 1 |
| 49 | }; |
| 50 | const char *s = str + len - EFI_VARIABLE_GUID_LEN; |
| 51 | int i; |
| 52 | |
| 53 | /* |
| 54 | * We need a GUID, plus at least one letter for the variable name, |
| 55 | * plus the '-' separator |
| 56 | */ |
| 57 | if (len < EFI_VARIABLE_GUID_LEN + 2) |
| 58 | return false; |
| 59 | |
| 60 | /* GUID must be preceded by a '-' */ |
| 61 | if (*(s - 1) != '-') |
| 62 | return false; |
| 63 | |
| 64 | /* |
| 65 | * Validate that 's' is of the correct format, e.g. |
| 66 | * |
| 67 | * 12345678-1234-1234-1234-123456789abc |
| 68 | */ |
| 69 | for (i = 0; i < EFI_VARIABLE_GUID_LEN; i++) { |
| 70 | if (dashes[i]) { |
| 71 | if (*s++ != '-') |
| 72 | return false; |
| 73 | } else { |
| 74 | if (!isxdigit(*s++)) |
| 75 | return false; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | return true; |
| 80 | } |
| 81 | |
| 82 | static void efivarfs_hex_to_guid(const char *str, efi_guid_t *guid) |
| 83 | { |
| 84 | guid->b[0] = hex_to_bin(str[6]) << 4 | hex_to_bin(str[7]); |
| 85 | guid->b[1] = hex_to_bin(str[4]) << 4 | hex_to_bin(str[5]); |
| 86 | guid->b[2] = hex_to_bin(str[2]) << 4 | hex_to_bin(str[3]); |
| 87 | guid->b[3] = hex_to_bin(str[0]) << 4 | hex_to_bin(str[1]); |
| 88 | guid->b[4] = hex_to_bin(str[11]) << 4 | hex_to_bin(str[12]); |
| 89 | guid->b[5] = hex_to_bin(str[9]) << 4 | hex_to_bin(str[10]); |
| 90 | guid->b[6] = hex_to_bin(str[16]) << 4 | hex_to_bin(str[17]); |
| 91 | guid->b[7] = hex_to_bin(str[14]) << 4 | hex_to_bin(str[15]); |
| 92 | guid->b[8] = hex_to_bin(str[19]) << 4 | hex_to_bin(str[20]); |
| 93 | guid->b[9] = hex_to_bin(str[21]) << 4 | hex_to_bin(str[22]); |
| 94 | guid->b[10] = hex_to_bin(str[24]) << 4 | hex_to_bin(str[25]); |
| 95 | guid->b[11] = hex_to_bin(str[26]) << 4 | hex_to_bin(str[27]); |
| 96 | guid->b[12] = hex_to_bin(str[28]) << 4 | hex_to_bin(str[29]); |
| 97 | guid->b[13] = hex_to_bin(str[30]) << 4 | hex_to_bin(str[31]); |
| 98 | guid->b[14] = hex_to_bin(str[32]) << 4 | hex_to_bin(str[33]); |
| 99 | guid->b[15] = hex_to_bin(str[34]) << 4 | hex_to_bin(str[35]); |
| 100 | } |
| 101 | |
| 102 | static int efivarfs_create(struct inode *dir, struct dentry *dentry, |
| 103 | umode_t mode, bool excl) |
| 104 | { |
| 105 | struct inode *inode; |
| 106 | struct efivar_entry *var; |
| 107 | int namelen, i = 0, err = 0; |
| 108 | |
| 109 | if (!efivarfs_valid_name(dentry->d_name.name, dentry->d_name.len)) |
| 110 | return -EINVAL; |
| 111 | |
| 112 | inode = efivarfs_get_inode(dir->i_sb, dir, mode, 0); |
| 113 | if (!inode) |
| 114 | return -ENOMEM; |
| 115 | |
| 116 | var = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL); |
| 117 | if (!var) { |
| 118 | err = -ENOMEM; |
| 119 | goto out; |
| 120 | } |
| 121 | |
| 122 | /* length of the variable name itself: remove GUID and separator */ |
| 123 | namelen = dentry->d_name.len - EFI_VARIABLE_GUID_LEN - 1; |
| 124 | |
| 125 | efivarfs_hex_to_guid(dentry->d_name.name + namelen + 1, |
| 126 | &var->var.VendorGuid); |
| 127 | |
| 128 | for (i = 0; i < namelen; i++) |
| 129 | var->var.VariableName[i] = dentry->d_name.name[i]; |
| 130 | |
| 131 | var->var.VariableName[i] = '\0'; |
| 132 | |
| 133 | inode->i_private = var; |
| 134 | |
| 135 | efivar_entry_add(var, &efivarfs_list); |
| 136 | d_instantiate(dentry, inode); |
| 137 | dget(dentry); |
| 138 | out: |
| 139 | if (err) { |
| 140 | kfree(var); |
| 141 | iput(inode); |
| 142 | } |
| 143 | return err; |
| 144 | } |
| 145 | |
| 146 | static int efivarfs_unlink(struct inode *dir, struct dentry *dentry) |
| 147 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 148 | struct efivar_entry *var = d_inode(dentry)->i_private; |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 149 | |
| 150 | if (efivar_entry_delete(var)) |
| 151 | return -EINVAL; |
| 152 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 153 | drop_nlink(d_inode(dentry)); |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 154 | dput(dentry); |
| 155 | return 0; |
| 156 | }; |
| 157 | |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 158 | const struct inode_operations efivarfs_dir_inode_operations = { |
Al Viro | 6e8cd2c | 2013-07-14 17:48:35 +0400 | [diff] [blame] | 159 | .lookup = simple_lookup, |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 160 | .unlink = efivarfs_unlink, |
| 161 | .create = efivarfs_create, |
| 162 | }; |