efi_selftest_rtc.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * efi_selftest_rtc
  4. *
  5. * Copyright (c) 2018 Heinrich Schuchardt <xypron.glpk@gmx.de>
  6. *
  7. * Test the real time clock runtime services.
  8. */
  9. #include <efi_selftest.h>
  10. #define EFI_ST_NO_RTC "Could not read real time clock\n"
  11. #define EFI_ST_NO_RTC_SET "Could not set real time clock\n"
  12. static struct efi_runtime_services *runtime;
  13. /*
  14. * Setup unit test.
  15. *
  16. * @handle: handle of the loaded image
  17. * @systable: system table
  18. * @return: EFI_ST_SUCCESS for success
  19. */
  20. static int setup(const efi_handle_t handle,
  21. const struct efi_system_table *systable)
  22. {
  23. runtime = systable->runtime;
  24. return EFI_ST_SUCCESS;
  25. }
  26. /*
  27. * Execute unit test.
  28. *
  29. * Read and display current time.
  30. * Set a new value and read it back.
  31. * Set the real time clock back the current time.
  32. *
  33. * @return: EFI_ST_SUCCESS for success
  34. */
  35. static int execute(void)
  36. {
  37. efi_status_t ret;
  38. struct efi_time tm_old;
  39. #ifdef CONFIG_EFI_SET_TIME
  40. struct efi_time tm, tm_new = {
  41. .year = 2017,
  42. .month = 5,
  43. .day = 19,
  44. .hour = 13,
  45. .minute = 47,
  46. .second = 53,
  47. };
  48. #endif
  49. /* Display current time */
  50. ret = runtime->get_time(&tm_old, NULL);
  51. if (ret != EFI_SUCCESS) {
  52. efi_st_error(EFI_ST_NO_RTC);
  53. return EFI_ST_FAILURE;
  54. }
  55. efi_st_printf("Time according to real time clock: "
  56. "%.4u-%.2u-%.2u %.2u:%.2u:%.2u\n",
  57. tm_old.year, tm_old.month, tm_old.day,
  58. tm_old.hour, tm_old.minute, tm_old.second);
  59. #ifdef CONFIG_EFI_SET_TIME
  60. ret = runtime->set_time(&tm_new);
  61. if (ret != EFI_SUCCESS) {
  62. efi_st_error(EFI_ST_NO_RTC_SET);
  63. return EFI_ST_FAILURE;
  64. }
  65. ret = runtime->get_time(&tm, NULL);
  66. if (ret != EFI_SUCCESS) {
  67. efi_st_error(EFI_ST_NO_RTC);
  68. return EFI_ST_FAILURE;
  69. }
  70. if (tm.year != tm_new.year ||
  71. tm.month != tm_new.month ||
  72. tm.day != tm_new.day ||
  73. tm.hour != tm_new.hour ||
  74. tm.minute != tm_new.minute ||
  75. tm.second < tm_new.second ||
  76. tm.second > tm_new.second + 2) {
  77. efi_st_error(EFI_ST_NO_RTC_SET);
  78. return EFI_ST_FAILURE;
  79. }
  80. /* Set time back to old value */
  81. ret = runtime->set_time(&tm_old);
  82. if (ret != EFI_SUCCESS) {
  83. efi_st_error(EFI_ST_NO_RTC_SET);
  84. return EFI_ST_FAILURE;
  85. }
  86. #endif
  87. return EFI_ST_SUCCESS;
  88. }
  89. EFI_UNIT_TEST(rtc) = {
  90. .name = "real time clock",
  91. .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
  92. .setup = setup,
  93. .execute = execute,
  94. };