/src/u-boot/lib/efi_selftest/efi_selftest_rtc.c
Line | Count | Source |
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 | | |
10 | | #include <efi_selftest.h> |
11 | | |
12 | | #define EFI_ST_NO_RTC "Could not read real time clock\n" |
13 | | #define EFI_ST_NO_RTC_SET "Could not set real time clock\n" |
14 | | |
15 | | /* |
16 | | * Execute unit test. |
17 | | * |
18 | | * Read and display current time. |
19 | | * Set a new value and read it back. |
20 | | * Set the real time clock back the current time. |
21 | | * |
22 | | * Return: EFI_ST_SUCCESS for success |
23 | | */ |
24 | | static int execute(void) |
25 | 0 | { |
26 | 0 | efi_status_t ret; |
27 | 0 | struct efi_time tm_old; |
28 | 0 | #ifdef CONFIG_EFI_SET_TIME |
29 | 0 | struct efi_time tm, tm_new = { |
30 | 0 | .year = 2017, |
31 | 0 | .month = 5, |
32 | 0 | .day = 19, |
33 | 0 | .hour = 13, |
34 | 0 | .minute = 47, |
35 | 0 | .second = 53, |
36 | 0 | }; |
37 | 0 | #endif |
38 | | |
39 | | /* Display current time */ |
40 | 0 | ret = st_runtime->get_time(&tm_old, NULL); |
41 | 0 | if (ret != EFI_SUCCESS) { |
42 | 0 | efi_st_error(EFI_ST_NO_RTC); |
43 | 0 | return EFI_ST_FAILURE; |
44 | 0 | } |
45 | 0 | efi_st_printf("Time according to real time clock: " |
46 | 0 | "%.4u-%.2u-%.2u %.2u:%.2u:%.2u\n", |
47 | 0 | tm_old.year, tm_old.month, tm_old.day, |
48 | 0 | tm_old.hour, tm_old.minute, tm_old.second); |
49 | 0 | #ifdef CONFIG_EFI_SET_TIME |
50 | 0 | ret = st_runtime->set_time(&tm_new); |
51 | 0 | if (ret != EFI_SUCCESS) { |
52 | 0 | efi_st_error(EFI_ST_NO_RTC_SET); |
53 | 0 | return EFI_ST_FAILURE; |
54 | 0 | } |
55 | 0 | ret = st_runtime->get_time(&tm, NULL); |
56 | 0 | if (ret != EFI_SUCCESS) { |
57 | 0 | efi_st_error(EFI_ST_NO_RTC); |
58 | 0 | return EFI_ST_FAILURE; |
59 | 0 | } |
60 | 0 | if (tm.year != tm_new.year || |
61 | 0 | tm.month != tm_new.month || |
62 | 0 | tm.day != tm_new.day || |
63 | 0 | tm.hour != tm_new.hour || |
64 | 0 | tm.minute != tm_new.minute || |
65 | 0 | tm.second < tm_new.second || |
66 | 0 | tm.second > tm_new.second + 2) { |
67 | 0 | efi_st_error(EFI_ST_NO_RTC_SET); |
68 | 0 | return EFI_ST_FAILURE; |
69 | 0 | } |
70 | | /* Set time back to old value */ |
71 | 0 | ret = st_runtime->set_time(&tm_old); |
72 | 0 | if (ret != EFI_SUCCESS) { |
73 | 0 | efi_st_error(EFI_ST_NO_RTC_SET); |
74 | 0 | return EFI_ST_FAILURE; |
75 | 0 | } |
76 | 0 | #endif |
77 | | |
78 | 0 | return EFI_ST_SUCCESS; |
79 | 0 | } |
80 | | |
81 | | EFI_UNIT_TEST(rtc) = { |
82 | | .name = "real time clock", |
83 | | .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT, |
84 | | .execute = execute, |
85 | | }; |