efi_selftest_variables_runtime.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * efi_selftest_variables_runtime
  4. *
  5. * Copyright (c) 2019 Heinrich Schuchardt <xypron.glpk@gmx.de>
  6. *
  7. * This unit test checks the runtime services for variables after
  8. * ExitBootServices():
  9. * GetVariable, GetNextVariableName, SetVariable, QueryVariableInfo.
  10. */
  11. #include <efi_selftest.h>
  12. #define EFI_ST_MAX_DATA_SIZE 16
  13. #define EFI_ST_MAX_VARNAME_SIZE 40
  14. static struct efi_boot_services *boottime;
  15. static struct efi_runtime_services *runtime;
  16. static const efi_guid_t guid_vendor0 =
  17. EFI_GUID(0x67029eb5, 0x0af2, 0xf6b1,
  18. 0xda, 0x53, 0xfc, 0xb5, 0x66, 0xdd, 0x1c, 0xe6);
  19. /*
  20. * Setup unit test.
  21. *
  22. * @handle handle of the loaded image
  23. * @systable system table
  24. */
  25. static int setup(const efi_handle_t img_handle,
  26. const struct efi_system_table *systable)
  27. {
  28. boottime = systable->boottime;
  29. runtime = systable->runtime;
  30. return EFI_ST_SUCCESS;
  31. }
  32. /**
  33. * execute() - execute unit test
  34. *
  35. * As runtime support is not implmented expect EFI_UNSUPPORTED to be returned.
  36. */
  37. static int execute(void)
  38. {
  39. efi_status_t ret;
  40. efi_uintn_t len;
  41. u32 attr;
  42. u8 v[16] = {0x5d, 0xd1, 0x5e, 0x51, 0x5a, 0x05, 0xc7, 0x0c,
  43. 0x35, 0x4a, 0xae, 0x87, 0xa5, 0xdf, 0x0f, 0x65,};
  44. u8 data[EFI_ST_MAX_DATA_SIZE];
  45. u16 varname[EFI_ST_MAX_VARNAME_SIZE];
  46. efi_guid_t guid;
  47. u64 max_storage, rem_storage, max_size;
  48. ret = runtime->query_variable_info(EFI_VARIABLE_BOOTSERVICE_ACCESS,
  49. &max_storage, &rem_storage,
  50. &max_size);
  51. if (ret != EFI_UNSUPPORTED) {
  52. efi_st_error("QueryVariableInfo failed\n");
  53. return EFI_ST_FAILURE;
  54. }
  55. ret = runtime->set_variable(L"efi_st_var0", &guid_vendor0,
  56. EFI_VARIABLE_BOOTSERVICE_ACCESS |
  57. EFI_VARIABLE_RUNTIME_ACCESS,
  58. 3, v + 4);
  59. if (ret != EFI_UNSUPPORTED) {
  60. efi_st_error("SetVariable failed\n");
  61. return EFI_ST_FAILURE;
  62. }
  63. len = 3;
  64. ret = runtime->get_variable(L"efi_st_var0", &guid_vendor0,
  65. &attr, &len, data);
  66. if (ret != EFI_UNSUPPORTED) {
  67. efi_st_error("GetVariable failed\n");
  68. return EFI_ST_FAILURE;
  69. }
  70. memset(&guid, 0, 16);
  71. *varname = 0;
  72. ret = runtime->get_next_variable_name(&len, varname, &guid);
  73. if (ret != EFI_UNSUPPORTED) {
  74. efi_st_error("GetNextVariableName failed\n");
  75. return EFI_ST_FAILURE;
  76. }
  77. return EFI_ST_SUCCESS;
  78. }
  79. EFI_UNIT_TEST(variables_run) = {
  80. .name = "variables at runtime",
  81. .phase = EFI_SETUP_BEFORE_BOOTTIME_EXIT,
  82. .setup = setup,
  83. .execute = execute,
  84. };