inode.c 4.4 KB

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