efivar.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // SPDX-License-Identifier: GPL-2.0 or BSD-3-Clause
  2. /*
  3. * Copyright(c) 2015, 2016 Intel Corporation.
  4. */
  5. #include <linux/string.h>
  6. #include <linux/string_helpers.h>
  7. #include "efivar.h"
  8. /* GUID for HFI1 variables in EFI */
  9. #define HFI1_EFIVAR_GUID EFI_GUID(0xc50a953e, 0xa8b2, 0x42a6, \
  10. 0xbf, 0x89, 0xd3, 0x33, 0xa6, 0xe9, 0xe6, 0xd4)
  11. /* largest EFI data size we expect */
  12. #define EFI_DATA_SIZE 4096
  13. /*
  14. * Read the named EFI variable. Return the size of the actual data in *size
  15. * and a kmalloc'ed buffer in *return_data. The caller must free the
  16. * data. It is guaranteed that *return_data will be NULL and *size = 0
  17. * if this routine fails.
  18. *
  19. * Return 0 on success, -errno on failure.
  20. */
  21. static int read_efi_var(const char *name, unsigned long *size,
  22. void **return_data)
  23. {
  24. efi_status_t status;
  25. efi_char16_t *uni_name;
  26. efi_guid_t guid;
  27. unsigned long temp_size;
  28. void *temp_buffer;
  29. void *data;
  30. int i;
  31. int ret;
  32. /* set failure return values */
  33. *size = 0;
  34. *return_data = NULL;
  35. if (!efi_rt_services_supported(EFI_RT_SUPPORTED_GET_VARIABLE))
  36. return -EOPNOTSUPP;
  37. uni_name = kcalloc(strlen(name) + 1, sizeof(efi_char16_t), GFP_KERNEL);
  38. temp_buffer = kzalloc(EFI_DATA_SIZE, GFP_KERNEL);
  39. if (!uni_name || !temp_buffer) {
  40. ret = -ENOMEM;
  41. goto fail;
  42. }
  43. /* input: the size of the buffer */
  44. temp_size = EFI_DATA_SIZE;
  45. /* convert ASCII to unicode - it is a 1:1 mapping */
  46. for (i = 0; name[i]; i++)
  47. uni_name[i] = name[i];
  48. /* need a variable for our GUID */
  49. guid = HFI1_EFIVAR_GUID;
  50. /* call into EFI runtime services */
  51. status = efi.get_variable(
  52. uni_name,
  53. &guid,
  54. NULL,
  55. &temp_size,
  56. temp_buffer);
  57. /*
  58. * It would be nice to call efi_status_to_err() here, but that
  59. * is in the EFIVAR_FS code and may not be compiled in.
  60. * However, even that is insufficient since it does not cover
  61. * EFI_BUFFER_TOO_SMALL which could be an important return.
  62. * For now, just split out success or not found.
  63. */
  64. ret = status == EFI_SUCCESS ? 0 :
  65. status == EFI_NOT_FOUND ? -ENOENT :
  66. -EINVAL;
  67. if (ret)
  68. goto fail;
  69. /*
  70. * We have successfully read the EFI variable into our
  71. * temporary buffer. Now allocate a correctly sized
  72. * buffer.
  73. */
  74. data = kmemdup(temp_buffer, temp_size, GFP_KERNEL);
  75. if (!data) {
  76. ret = -ENOMEM;
  77. goto fail;
  78. }
  79. *size = temp_size;
  80. *return_data = data;
  81. fail:
  82. kfree(uni_name);
  83. kfree(temp_buffer);
  84. return ret;
  85. }
  86. /*
  87. * Read an HFI1 EFI variable of the form:
  88. * <PCIe address>-<kind>
  89. * Return an kalloc'ed array and size of the data.
  90. *
  91. * Returns 0 on success, -errno on failure.
  92. */
  93. int read_hfi1_efi_var(struct hfi1_devdata *dd, const char *kind,
  94. unsigned long *size, void **return_data)
  95. {
  96. char prefix_name[64];
  97. char name[128];
  98. int result;
  99. /* create a common prefix */
  100. snprintf(prefix_name, sizeof(prefix_name), "%04x:%02x:%02x.%x",
  101. pci_domain_nr(dd->pcidev->bus),
  102. dd->pcidev->bus->number,
  103. PCI_SLOT(dd->pcidev->devfn),
  104. PCI_FUNC(dd->pcidev->devfn));
  105. snprintf(name, sizeof(name), "%s-%s", prefix_name, kind);
  106. result = read_efi_var(name, size, return_data);
  107. /*
  108. * If reading the lowercase EFI variable fail, read the uppercase
  109. * variable.
  110. */
  111. if (result) {
  112. string_upper(prefix_name, prefix_name);
  113. snprintf(name, sizeof(name), "%s-%s", prefix_name, kind);
  114. result = read_efi_var(name, size, return_data);
  115. }
  116. return result;
  117. }