Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Heinrich Schuchardt | 630d134 | 2017-09-15 10:06:19 +0200 | [diff] [blame] | 2 | /* |
Heinrich Schuchardt | 1afe5f7 | 2017-10-04 15:31:26 +0200 | [diff] [blame] | 3 | * efi_selftest_exitbootservices |
Heinrich Schuchardt | 630d134 | 2017-09-15 10:06:19 +0200 | [diff] [blame] | 4 | * |
| 5 | * Copyright (c) 2017 Heinrich Schuchardt <xypron.glpk@gmx.de> |
| 6 | * |
Heinrich Schuchardt | 630d134 | 2017-09-15 10:06:19 +0200 | [diff] [blame] | 7 | * This unit test checks that the notification function of an |
| 8 | * EVT_SIGNAL_EXIT_BOOT_SERVICES event is called exactly once. |
| 9 | */ |
| 10 | |
| 11 | #include <efi_selftest.h> |
| 12 | |
Heinrich Schuchardt | 4419c1d | 2021-11-17 18:52:35 +0100 | [diff] [blame] | 13 | static efi_guid_t guid_before_exit_boot_services = |
| 14 | EFI_GUID(0x8be0e274, 0x3970, 0x4b44, 0x80, 0xc5, |
| 15 | 0x1a, 0xb9, 0x50, 0x2f, 0x3b, 0xfc); |
| 16 | #define CAPACITY 4 |
| 17 | |
| 18 | struct notification_record { |
| 19 | unsigned int count; |
| 20 | unsigned int type[CAPACITY]; |
| 21 | }; |
| 22 | |
| 23 | struct notification_context { |
| 24 | struct notification_record *record; |
| 25 | unsigned int type; |
| 26 | }; |
| 27 | |
Heinrich Schuchardt | 630d134 | 2017-09-15 10:06:19 +0200 | [diff] [blame] | 28 | static struct efi_boot_services *boottime; |
| 29 | static struct efi_event *event_notify; |
Heinrich Schuchardt | 4419c1d | 2021-11-17 18:52:35 +0100 | [diff] [blame] | 30 | struct notification_record record; |
| 31 | |
| 32 | struct notification_context context_before = { |
| 33 | .record = &record, |
| 34 | .type = 1, |
| 35 | }; |
| 36 | |
| 37 | struct notification_context context = { |
| 38 | .record = &record, |
| 39 | .type = 2, |
| 40 | }; |
Heinrich Schuchardt | 630d134 | 2017-09-15 10:06:19 +0200 | [diff] [blame] | 41 | |
| 42 | /* |
Heinrich Schuchardt | 1afe5f7 | 2017-10-04 15:31:26 +0200 | [diff] [blame] | 43 | * Notification function, increments the notification count. |
Heinrich Schuchardt | 630d134 | 2017-09-15 10:06:19 +0200 | [diff] [blame] | 44 | * |
| 45 | * @event notified event |
Heinrich Schuchardt | 1afe5f7 | 2017-10-04 15:31:26 +0200 | [diff] [blame] | 46 | * @context pointer to the notification count |
Heinrich Schuchardt | 630d134 | 2017-09-15 10:06:19 +0200 | [diff] [blame] | 47 | */ |
Heinrich Schuchardt | 4419c1d | 2021-11-17 18:52:35 +0100 | [diff] [blame] | 48 | static void EFIAPI ebs_notify(struct efi_event *event, void *context) |
Heinrich Schuchardt | 630d134 | 2017-09-15 10:06:19 +0200 | [diff] [blame] | 49 | { |
Heinrich Schuchardt | 4419c1d | 2021-11-17 18:52:35 +0100 | [diff] [blame] | 50 | struct notification_context *ctx = context; |
| 51 | |
| 52 | if (ctx->record->count >= CAPACITY) |
| 53 | return; |
Heinrich Schuchardt | 1afe5f7 | 2017-10-04 15:31:26 +0200 | [diff] [blame] | 54 | |
Heinrich Schuchardt | 4419c1d | 2021-11-17 18:52:35 +0100 | [diff] [blame] | 55 | ctx->record->type[ctx->record->count] = ctx->type; |
| 56 | ctx->record->count++; |
Heinrich Schuchardt | 630d134 | 2017-09-15 10:06:19 +0200 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | /* |
| 60 | * Setup unit test. |
| 61 | * |
| 62 | * Create an EVT_SIGNAL_EXIT_BOOT_SERVICES event. |
| 63 | * |
| 64 | * @handle: handle of the loaded image |
| 65 | * @systable: system table |
Heinrich Schuchardt | fbe9021 | 2022-01-20 19:48:20 +0100 | [diff] [blame] | 66 | * Return: EFI_ST_SUCCESS for success |
Heinrich Schuchardt | 630d134 | 2017-09-15 10:06:19 +0200 | [diff] [blame] | 67 | */ |
| 68 | static int setup(const efi_handle_t handle, |
| 69 | const struct efi_system_table *systable) |
| 70 | { |
| 71 | efi_status_t ret; |
| 72 | |
| 73 | boottime = systable->boottime; |
| 74 | |
Heinrich Schuchardt | 630d134 | 2017-09-15 10:06:19 +0200 | [diff] [blame] | 75 | ret = boottime->create_event(EVT_SIGNAL_EXIT_BOOT_SERVICES, |
Heinrich Schuchardt | 4419c1d | 2021-11-17 18:52:35 +0100 | [diff] [blame] | 76 | TPL_CALLBACK, ebs_notify, |
| 77 | &context, |
Heinrich Schuchardt | 630d134 | 2017-09-15 10:06:19 +0200 | [diff] [blame] | 78 | &event_notify); |
| 79 | if (ret != EFI_SUCCESS) { |
| 80 | efi_st_error("could not create event\n"); |
Heinrich Schuchardt | 1afe5f7 | 2017-10-04 15:31:26 +0200 | [diff] [blame] | 81 | return EFI_ST_FAILURE; |
Heinrich Schuchardt | 630d134 | 2017-09-15 10:06:19 +0200 | [diff] [blame] | 82 | } |
Heinrich Schuchardt | 4419c1d | 2021-11-17 18:52:35 +0100 | [diff] [blame] | 83 | ret = boottime->create_event_ex(0, TPL_CALLBACK, ebs_notify, |
| 84 | &context_before, |
| 85 | &guid_before_exit_boot_services, |
| 86 | &event_notify); |
| 87 | if (ret != EFI_SUCCESS) { |
| 88 | efi_st_error("could not create event\n"); |
| 89 | return EFI_ST_FAILURE; |
| 90 | } |
| 91 | |
Heinrich Schuchardt | 1afe5f7 | 2017-10-04 15:31:26 +0200 | [diff] [blame] | 92 | return EFI_ST_SUCCESS; |
Heinrich Schuchardt | 630d134 | 2017-09-15 10:06:19 +0200 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | /* |
Heinrich Schuchardt | 630d134 | 2017-09-15 10:06:19 +0200 | [diff] [blame] | 96 | * Execute unit test. |
| 97 | * |
| 98 | * Check that the notification function of the EVT_SIGNAL_EXIT_BOOT_SERVICES |
| 99 | * event has been called. |
| 100 | * |
| 101 | * Call ExitBootServices again and check that the notification function is |
| 102 | * not called again. |
Heinrich Schuchardt | 1afe5f7 | 2017-10-04 15:31:26 +0200 | [diff] [blame] | 103 | * |
Heinrich Schuchardt | fbe9021 | 2022-01-20 19:48:20 +0100 | [diff] [blame] | 104 | * Return: EFI_ST_SUCCESS for success |
Heinrich Schuchardt | 630d134 | 2017-09-15 10:06:19 +0200 | [diff] [blame] | 105 | */ |
| 106 | static int execute(void) |
| 107 | { |
Heinrich Schuchardt | 4419c1d | 2021-11-17 18:52:35 +0100 | [diff] [blame] | 108 | if (record.count != 2) { |
| 109 | efi_st_error("Incorrect event count %u\n", record.count); |
| 110 | return EFI_ST_FAILURE; |
| 111 | } |
| 112 | if (record.type[0] != 1) { |
| 113 | efi_st_error("EFI_GROUP_BEFORE_EXIT_BOOT_SERVICE not notified\n"); |
| 114 | return EFI_ST_FAILURE; |
| 115 | } |
| 116 | if (record.type[1] != 2) { |
| 117 | efi_st_error("EVT_SIGNAL_EXIT_BOOT_SERVICES was not notified\n"); |
Heinrich Schuchardt | 1afe5f7 | 2017-10-04 15:31:26 +0200 | [diff] [blame] | 118 | return EFI_ST_FAILURE; |
Heinrich Schuchardt | 630d134 | 2017-09-15 10:06:19 +0200 | [diff] [blame] | 119 | } |
| 120 | efi_st_exit_boot_services(); |
Heinrich Schuchardt | 4419c1d | 2021-11-17 18:52:35 +0100 | [diff] [blame] | 121 | if (record.count != 2) { |
| 122 | efi_st_error("Incorrect event count %u\n", record.count); |
Heinrich Schuchardt | 1afe5f7 | 2017-10-04 15:31:26 +0200 | [diff] [blame] | 123 | return EFI_ST_FAILURE; |
Heinrich Schuchardt | 630d134 | 2017-09-15 10:06:19 +0200 | [diff] [blame] | 124 | } |
Heinrich Schuchardt | 1afe5f7 | 2017-10-04 15:31:26 +0200 | [diff] [blame] | 125 | return EFI_ST_SUCCESS; |
Heinrich Schuchardt | 630d134 | 2017-09-15 10:06:19 +0200 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | EFI_UNIT_TEST(exitbootservices) = { |
| 129 | .name = "ExitBootServices", |
| 130 | .phase = EFI_SETUP_BEFORE_BOOTTIME_EXIT, |
| 131 | .setup = setup, |
| 132 | .execute = execute, |
Heinrich Schuchardt | 630d134 | 2017-09-15 10:06:19 +0200 | [diff] [blame] | 133 | }; |