efi_selftest_mem.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. #include <efi_selftest.h>
  13. static struct efi_boot_services *boottime;
  14. /**
  15. * setup() - setup unit test
  16. *
  17. * @handle: handle of the loaded image
  18. * @systable: system table
  19. * Return: EFI_ST_SUCCESS for success
  20. */
  21. static int setup(const efi_handle_t handle,
  22. const struct efi_system_table *systable)
  23. {
  24. boottime = systable->boottime;
  25. return EFI_ST_SUCCESS;
  26. }
  27. /*
  28. * execute() - execute unit test
  29. *
  30. * Return: EFI_ST_SUCCESS for success
  31. */
  32. static int execute(void)
  33. {
  34. u8 c1[] = "abcdefghijklmnop";
  35. u8 c2[] = "abcdefghijklmnop";
  36. u32 crc32;
  37. efi_status_t ret;
  38. ret = boottime->calculate_crc32(c1, 16, &crc32);
  39. if (ret != EFI_SUCCESS) {
  40. efi_st_error("CalculateCrc32 failed\n");
  41. return EFI_ST_FAILURE;
  42. }
  43. if (crc32 != 0x943ac093) {
  44. efi_st_error("CalculateCrc32 returned wrong value\n");
  45. return EFI_ST_FAILURE;
  46. }
  47. boottime->copy_mem(&c1[5], &c1[3], 8);
  48. if (memcmp(c1, "abcdedefghijknop", 16)) {
  49. efi_st_error("CopyMem forward copy failed: %s\n", c1);
  50. return EFI_ST_FAILURE;
  51. }
  52. boottime->copy_mem(&c2[3], &c2[5], 8);
  53. if (memcmp(c2, "abcfghijklmlmnop", 16)) {
  54. efi_st_error("CopyMem backward copy failed: %s\n", c2);
  55. return EFI_ST_FAILURE;
  56. }
  57. boottime->set_mem(&c1[3], 8, 'x');
  58. if (memcmp(c1, "abcxxxxxxxxjknop", 16)) {
  59. efi_st_error("SetMem failed: %s\n", c1);
  60. return EFI_ST_FAILURE;
  61. }
  62. return EFI_ST_SUCCESS;
  63. }
  64. EFI_UNIT_TEST(mem) = {
  65. .name = "mem",
  66. .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
  67. .setup = setup,
  68. .execute = execute,
  69. };