efi_var_common.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * UEFI runtime variable services
  4. *
  5. * Copyright (c) 2020, Heinrich Schuchardt <xypron.glpk@gmx.de>
  6. */
  7. #include <common.h>
  8. #include <efi_loader.h>
  9. #include <efi_variable.h>
  10. /**
  11. * efi_efi_get_variable() - retrieve value of a UEFI variable
  12. *
  13. * This function implements the GetVariable runtime service.
  14. *
  15. * See the Unified Extensible Firmware Interface (UEFI) specification for
  16. * details.
  17. *
  18. * @variable_name: name of the variable
  19. * @vendor: vendor GUID
  20. * @attributes: attributes of the variable
  21. * @data_size: size of the buffer to which the variable value is copied
  22. * @data: buffer to which the variable value is copied
  23. * Return: status code
  24. */
  25. efi_status_t EFIAPI efi_get_variable(u16 *variable_name,
  26. const efi_guid_t *vendor, u32 *attributes,
  27. efi_uintn_t *data_size, void *data)
  28. {
  29. efi_status_t ret;
  30. EFI_ENTRY("\"%ls\" %pUl %p %p %p", variable_name, vendor, attributes,
  31. data_size, data);
  32. ret = efi_get_variable_int(variable_name, vendor, attributes,
  33. data_size, data, NULL);
  34. /* Remove EFI_VARIABLE_READ_ONLY flag */
  35. if (attributes)
  36. *attributes &= EFI_VARIABLE_MASK;
  37. return EFI_EXIT(ret);
  38. }
  39. /**
  40. * efi_set_variable() - set value of a UEFI variable
  41. *
  42. * This function implements the SetVariable runtime service.
  43. *
  44. * See the Unified Extensible Firmware Interface (UEFI) specification for
  45. * details.
  46. *
  47. * @variable_name: name of the variable
  48. * @vendor: vendor GUID
  49. * @attributes: attributes of the variable
  50. * @data_size: size of the buffer with the variable value
  51. * @data: buffer with the variable value
  52. * Return: status code
  53. */
  54. efi_status_t EFIAPI efi_set_variable(u16 *variable_name,
  55. const efi_guid_t *vendor, u32 attributes,
  56. efi_uintn_t data_size, const void *data)
  57. {
  58. efi_status_t ret;
  59. EFI_ENTRY("\"%ls\" %pUl %x %zu %p", variable_name, vendor, attributes,
  60. data_size, data);
  61. /* Make sure that the EFI_VARIABLE_READ_ONLY flag is not set */
  62. if (attributes & ~(u32)EFI_VARIABLE_MASK)
  63. ret = EFI_INVALID_PARAMETER;
  64. else
  65. ret = efi_set_variable_int(variable_name, vendor, attributes,
  66. data_size, data, true);
  67. return EFI_EXIT(ret);
  68. }
  69. /**
  70. * efi_get_next_variable_name() - enumerate the current variable names
  71. *
  72. * @variable_name_size: size of variable_name buffer in byte
  73. * @variable_name: name of uefi variable's name in u16
  74. * @vendor: vendor's guid
  75. *
  76. * See the Unified Extensible Firmware Interface (UEFI) specification for
  77. * details.
  78. *
  79. * Return: status code
  80. */
  81. efi_status_t EFIAPI efi_get_next_variable_name(efi_uintn_t *variable_name_size,
  82. u16 *variable_name,
  83. efi_guid_t *vendor)
  84. {
  85. efi_status_t ret;
  86. EFI_ENTRY("%p \"%ls\" %pUl", variable_name_size, variable_name, vendor);
  87. ret = efi_get_next_variable_name_int(variable_name_size, variable_name,
  88. vendor);
  89. return EFI_EXIT(ret);
  90. }
  91. /**
  92. * efi_query_variable_info() - get information about EFI variables
  93. *
  94. * This function implements the QueryVariableInfo() runtime service.
  95. *
  96. * See the Unified Extensible Firmware Interface (UEFI) specification for
  97. * details.
  98. *
  99. * @attributes: bitmask to select variables to be
  100. * queried
  101. * @maximum_variable_storage_size: maximum size of storage area for the
  102. * selected variable types
  103. * @remaining_variable_storage_size: remaining size of storage are for the
  104. * selected variable types
  105. * @maximum_variable_size: maximum size of a variable of the
  106. * selected type
  107. * Returns: status code
  108. */
  109. efi_status_t EFIAPI efi_query_variable_info(
  110. u32 attributes, u64 *maximum_variable_storage_size,
  111. u64 *remaining_variable_storage_size,
  112. u64 *maximum_variable_size)
  113. {
  114. efi_status_t ret;
  115. EFI_ENTRY("%x %p %p %p", attributes, maximum_variable_storage_size,
  116. remaining_variable_storage_size, maximum_variable_size);
  117. ret = efi_query_variable_info_int(attributes,
  118. maximum_variable_storage_size,
  119. remaining_variable_storage_size,
  120. maximum_variable_size);
  121. return EFI_EXIT(ret);
  122. }