efi_selftest_variables_runtime.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 = EFI_GLOBAL_VARIABLE_GUID;
  17. /*
  18. * Setup unit test.
  19. *
  20. * @handle handle of the loaded image
  21. * @systable system table
  22. */
  23. static int setup(const efi_handle_t img_handle,
  24. const struct efi_system_table *systable)
  25. {
  26. boottime = systable->boottime;
  27. runtime = systable->runtime;
  28. return EFI_ST_SUCCESS;
  29. }
  30. /**
  31. * execute() - execute unit test
  32. *
  33. * As runtime support is not implmented expect EFI_UNSUPPORTED to be returned.
  34. */
  35. static int execute(void)
  36. {
  37. efi_status_t ret;
  38. efi_uintn_t len;
  39. u32 attr;
  40. u8 v[16] = {0x5d, 0xd1, 0x5e, 0x51, 0x5a, 0x05, 0xc7, 0x0c,
  41. 0x35, 0x4a, 0xae, 0x87, 0xa5, 0xdf, 0x0f, 0x65,};
  42. u8 data[EFI_ST_MAX_DATA_SIZE];
  43. u16 varname[EFI_ST_MAX_VARNAME_SIZE];
  44. efi_guid_t guid;
  45. u64 max_storage, rem_storage, max_size;
  46. ret = runtime->query_variable_info(EFI_VARIABLE_BOOTSERVICE_ACCESS,
  47. &max_storage, &rem_storage,
  48. &max_size);
  49. if (ret != EFI_UNSUPPORTED) {
  50. efi_st_error("QueryVariableInfo failed\n");
  51. return EFI_ST_FAILURE;
  52. }
  53. ret = runtime->set_variable(L"efi_st_var0", &guid_vendor0,
  54. EFI_VARIABLE_BOOTSERVICE_ACCESS |
  55. EFI_VARIABLE_RUNTIME_ACCESS,
  56. 3, v + 4);
  57. if (ret != EFI_UNSUPPORTED) {
  58. efi_st_error("SetVariable failed\n");
  59. return EFI_ST_FAILURE;
  60. }
  61. len = EFI_ST_MAX_DATA_SIZE;
  62. ret = runtime->get_variable(L"PlatformLangCodes", &guid_vendor0,
  63. &attr, &len, data);
  64. if (ret != EFI_SUCCESS) {
  65. efi_st_error("GetVariable failed\n");
  66. return EFI_ST_FAILURE;
  67. }
  68. memset(&guid, 0, 16);
  69. *varname = 0;
  70. len = 2 * EFI_ST_MAX_VARNAME_SIZE;
  71. ret = runtime->get_next_variable_name(&len, varname, &guid);
  72. if (ret != EFI_SUCCESS) {
  73. efi_st_error("GetNextVariableName failed\n");
  74. return EFI_ST_FAILURE;
  75. }
  76. return EFI_ST_SUCCESS;
  77. }
  78. EFI_UNIT_TEST(variables_run) = {
  79. .name = "variables at runtime",
  80. .phase = EFI_SETUP_BEFORE_BOOTTIME_EXIT,
  81. .setup = setup,
  82. .execute = execute,
  83. };