efi_loader: validate load option
[oweals/u-boot.git] / lib / efi_selftest / efi_selftest_mem.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * efi_selftest_memory
4  *
5  * Copyright (c) 2018 Heinrich Schuchardt <xypron.glpk@gmx.de>
6  *
7  * This unit test checks the following boottime services:
8  * CopyMem, SetMem, CalculateCrc32
9  *
10  * The memory type used for the device tree is checked.
11  */
12
13 #include <efi_selftest.h>
14
15 static struct efi_boot_services *boottime;
16
17 /**
18  * setup() - setup unit test
19  *
20  * @handle:     handle of the loaded image
21  * @systable:   system table
22  * Return:      EFI_ST_SUCCESS for success
23  */
24 static int setup(const efi_handle_t handle,
25                  const struct efi_system_table *systable)
26 {
27         boottime = systable->boottime;
28
29         return EFI_ST_SUCCESS;
30 }
31
32 /*
33  * execute() - execute unit test
34  *
35  * Return:      EFI_ST_SUCCESS for success
36  */
37 static int execute(void)
38 {
39         u8 c1[] = "abcdefghijklmnop";
40         u8 c2[] = "abcdefghijklmnop";
41         u32 crc32;
42         efi_status_t ret;
43
44         ret = boottime->calculate_crc32(c1, 16, &crc32);
45         if (ret != EFI_SUCCESS) {
46                 efi_st_error("CalculateCrc32 failed\n");
47                 return EFI_ST_FAILURE;
48         }
49         if (crc32 != 0x943ac093) {
50                 efi_st_error("CalculateCrc32 returned wrong value\n");
51                 return EFI_ST_FAILURE;
52         }
53         boottime->copy_mem(&c1[5], &c1[3], 8);
54         if (memcmp(c1, "abcdedefghijknop", 16)) {
55                 efi_st_error("CopyMem forward copy failed: %s\n", c1);
56                 return EFI_ST_FAILURE;
57         }
58         boottime->copy_mem(&c2[3], &c2[5], 8);
59         if (memcmp(c2, "abcfghijklmlmnop", 16)) {
60                 efi_st_error("CopyMem backward copy failed: %s\n", c2);
61                 return EFI_ST_FAILURE;
62         }
63         boottime->set_mem(&c1[3], 8, 'x');
64         if (memcmp(c1, "abcxxxxxxxxjknop", 16)) {
65                 efi_st_error("SetMem failed: %s\n", c1);
66                 return EFI_ST_FAILURE;
67         }
68
69         return EFI_ST_SUCCESS;
70 }
71
72 EFI_UNIT_TEST(mem) = {
73         .name = "mem",
74         .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
75         .setup = setup,
76         .execute = execute,
77 };