1 // SPDX-License-Identifier: GPL-2.0+
5 * Copyright (c) 2018 Heinrich Schuchardt <xypron.glpk@gmx.de>
7 * This unit test checks the CalculateCrc32 bootservice and checks the
8 * headers of the system table, the boot services tablle, and the runtime
9 * services table before and after ExitBootServices().
12 #include <efi_selftest.h>
14 const struct efi_system_table *st;
15 efi_status_t (EFIAPI *bs_crc32)(const void *data, efi_uintn_t data_size,
18 static int check_table(const void *table)
22 /* Casting from const to not const */
23 struct efi_table_hdr *hdr = (struct efi_table_hdr *)table;
25 if (!hdr->signature) {
26 efi_st_error("Missing header signature\n");
27 return EFI_ST_FAILURE;
30 efi_st_error("Missing header revision\n");
31 return EFI_ST_FAILURE;
33 if (hdr->headersize <= sizeof(struct efi_table_hdr)) {
34 efi_st_error("Incorrect headersize value\n");
35 return EFI_ST_FAILURE;
38 efi_st_error("Reserved header field is not zero\n");
39 return EFI_ST_FAILURE;
44 * Setting the crc32 of the 'const' table to zero is easier than
48 ret = bs_crc32(table, hdr->headersize, &res);
49 /* Reset table crc32 so it stays constant */
51 if (ret != EFI_ST_SUCCESS) {
52 efi_st_error("CalculateCrc32 failed\n");
53 return EFI_ST_FAILURE;
56 efi_st_error("Incorrect CRC32\n");
57 // return EFI_ST_FAILURE;
59 return EFI_ST_SUCCESS;
65 * Check that CalculateCrc32 is working correctly.
66 * Check tables before ExitBootServices().
68 * @handle: handle of the loaded image
69 * @systable: system table
70 * @return: EFI_ST_SUCCESS for success
72 static int setup(const efi_handle_t handle,
73 const struct efi_system_table *systable)
79 bs_crc32 = systable->boottime->calculate_crc32;
81 /* Check that CalculateCrc32 is working */
82 ret = bs_crc32("U-Boot", 6, &res);
83 if (ret != EFI_ST_SUCCESS) {
84 efi_st_error("CalculateCrc32 failed\n");
85 return EFI_ST_FAILURE;
87 if (res != 0x134b0db4) {
88 efi_st_error("Incorrect CRC32\n");
89 return EFI_ST_FAILURE;
92 /* Check tables before ExitBootServices() */
93 if (check_table(st) != EFI_ST_SUCCESS) {
94 efi_st_error("Checking system table\n");
95 return EFI_ST_FAILURE;
97 if (check_table(st->boottime) != EFI_ST_SUCCESS) {
98 efi_st_error("Checking boottime table\n");
99 return EFI_ST_FAILURE;
101 if (check_table(st->runtime) != EFI_ST_SUCCESS) {
102 efi_st_error("Checking runtime table\n");
103 return EFI_ST_FAILURE;
106 return EFI_ST_SUCCESS;
112 * Check tables after ExitBootServices()
114 * @return: EFI_ST_SUCCESS for success
116 static int execute(void)
118 if (check_table(st) != EFI_ST_SUCCESS) {
119 efi_st_error("Checking system table\n");
120 return EFI_ST_FAILURE;
122 if (check_table(st->runtime) != EFI_ST_SUCCESS) {
123 efi_st_error("Checking runtime table\n");
124 return EFI_ST_FAILURE;
128 * We cannot call SetVirtualAddressMap() and recheck the runtime
129 * table afterwards because this would invalidate the addresses of the
133 return EFI_ST_SUCCESS;
136 EFI_UNIT_TEST(crc32) = {
138 .phase = EFI_SETUP_BEFORE_BOOTTIME_EXIT,