efivar.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * Copyright(c) 2015, 2016 Intel Corporation.
  3. *
  4. * This file is provided under a dual BSD/GPLv2 license. When using or
  5. * redistributing this file, you may do so under either license.
  6. *
  7. * GPL LICENSE SUMMARY
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of version 2 of the GNU General Public License as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * BSD LICENSE
  19. *
  20. * Redistribution and use in source and binary forms, with or without
  21. * modification, are permitted provided that the following conditions
  22. * are met:
  23. *
  24. * - Redistributions of source code must retain the above copyright
  25. * notice, this list of conditions and the following disclaimer.
  26. * - Redistributions in binary form must reproduce the above copyright
  27. * notice, this list of conditions and the following disclaimer in
  28. * the documentation and/or other materials provided with the
  29. * distribution.
  30. * - Neither the name of Intel Corporation nor the names of its
  31. * contributors may be used to endorse or promote products derived
  32. * from this software without specific prior written permission.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  35. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  36. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  37. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  38. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  39. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  40. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  41. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  42. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  43. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  44. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. *
  46. */
  47. #include "efivar.h"
  48. /* GUID for HFI1 variables in EFI */
  49. #define HFI1_EFIVAR_GUID EFI_GUID(0xc50a953e, 0xa8b2, 0x42a6, \
  50. 0xbf, 0x89, 0xd3, 0x33, 0xa6, 0xe9, 0xe6, 0xd4)
  51. /* largest EFI data size we expect */
  52. #define EFI_DATA_SIZE 4096
  53. /*
  54. * Read the named EFI variable. Return the size of the actual data in *size
  55. * and a kmalloc'ed buffer in *return_data. The caller must free the
  56. * data. It is guaranteed that *return_data will be NULL and *size = 0
  57. * if this routine fails.
  58. *
  59. * Return 0 on success, -errno on failure.
  60. */
  61. static int read_efi_var(const char *name, unsigned long *size,
  62. void **return_data)
  63. {
  64. efi_status_t status;
  65. efi_char16_t *uni_name;
  66. efi_guid_t guid;
  67. unsigned long temp_size;
  68. void *temp_buffer;
  69. void *data;
  70. int i;
  71. int ret;
  72. /* set failure return values */
  73. *size = 0;
  74. *return_data = NULL;
  75. if (!efi_enabled(EFI_RUNTIME_SERVICES))
  76. return -EOPNOTSUPP;
  77. uni_name = kcalloc(strlen(name) + 1, sizeof(efi_char16_t), GFP_KERNEL);
  78. temp_buffer = kzalloc(EFI_DATA_SIZE, GFP_KERNEL);
  79. if (!uni_name || !temp_buffer) {
  80. ret = -ENOMEM;
  81. goto fail;
  82. }
  83. /* input: the size of the buffer */
  84. temp_size = EFI_DATA_SIZE;
  85. /* convert ASCII to unicode - it is a 1:1 mapping */
  86. for (i = 0; name[i]; i++)
  87. uni_name[i] = name[i];
  88. /* need a variable for our GUID */
  89. guid = HFI1_EFIVAR_GUID;
  90. /* call into EFI runtime services */
  91. status = efi.get_variable(
  92. uni_name,
  93. &guid,
  94. NULL,
  95. &temp_size,
  96. temp_buffer);
  97. /*
  98. * It would be nice to call efi_status_to_err() here, but that
  99. * is in the EFIVAR_FS code and may not be compiled in.
  100. * However, even that is insufficient since it does not cover
  101. * EFI_BUFFER_TOO_SMALL which could be an important return.
  102. * For now, just split out succces or not found.
  103. */
  104. ret = status == EFI_SUCCESS ? 0 :
  105. status == EFI_NOT_FOUND ? -ENOENT :
  106. -EINVAL;
  107. if (ret)
  108. goto fail;
  109. /*
  110. * We have successfully read the EFI variable into our
  111. * temporary buffer. Now allocate a correctly sized
  112. * buffer.
  113. */
  114. data = kmemdup(temp_buffer, temp_size, GFP_KERNEL);
  115. if (!data) {
  116. ret = -ENOMEM;
  117. goto fail;
  118. }
  119. *size = temp_size;
  120. *return_data = data;
  121. fail:
  122. kfree(uni_name);
  123. kfree(temp_buffer);
  124. return ret;
  125. }
  126. /*
  127. * Read an HFI1 EFI variable of the form:
  128. * <PCIe address>-<kind>
  129. * Return an kalloc'ed array and size of the data.
  130. *
  131. * Returns 0 on success, -errno on failure.
  132. */
  133. int read_hfi1_efi_var(struct hfi1_devdata *dd, const char *kind,
  134. unsigned long *size, void **return_data)
  135. {
  136. char name[64];
  137. /* create a common prefix */
  138. snprintf(name, sizeof(name), "%04x:%02x:%02x.%x-%s",
  139. pci_domain_nr(dd->pcidev->bus),
  140. dd->pcidev->bus->number,
  141. PCI_SLOT(dd->pcidev->devfn),
  142. PCI_FUNC(dd->pcidev->devfn),
  143. kind);
  144. return read_efi_var(name, size, return_data);
  145. }