inode.c 4.1 KB

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