|
| 1 | + |
| 2 | +#include <efi.h> |
| 3 | +#include <efilib.h> |
| 4 | +#include <wolfssl/wolfcrypt/settings.h> |
| 5 | +#include <wolfssl/wolfcrypt/logging.h> |
| 6 | +#include <wolfssl/wolfcrypt/test/test.h> |
| 7 | + |
| 8 | +#define STR_SIZE 512 |
| 9 | + |
| 10 | +#ifndef WAIT_FOR_GDB |
| 11 | +#define WAIT_FOR_GDB 0 |
| 12 | +#endif |
| 13 | + |
| 14 | +#define uefi_printf(_f_, ...) Print(L##_f_, ##__VA_ARGS__) |
| 15 | + |
| 16 | +void char8_to_char16(const char* str8, wchar_t* str16) |
| 17 | +{ |
| 18 | + size_t i; |
| 19 | + size_t size_str8 = strlen(str8); |
| 20 | + for (i = 0; i < size_str8; ++i) { |
| 21 | + str16[i] = (wchar_t)str8[i]; |
| 22 | + } |
| 23 | + str16[i] = '\0'; |
| 24 | +} |
| 25 | + |
| 26 | +void logging_cb(const int logLevel, const char *const logMessage) |
| 27 | +{ |
| 28 | + wchar_t str16[STR_SIZE]; |
| 29 | + char8_to_char16(logMessage, str16); |
| 30 | + uefi_printf("%s", str16); |
| 31 | +} |
| 32 | + |
| 33 | +void *XMALLOC(size_t n, void* heap, int type) |
| 34 | +{ |
| 35 | + return AllocateZeroPool(n); |
| 36 | +} |
| 37 | + |
| 38 | +void *XREALLOC(void *p, size_t n, void* heap, int type) |
| 39 | +{ |
| 40 | + FreePool(p); |
| 41 | + p = NULL; |
| 42 | + return AllocateZeroPool(n); |
| 43 | +} |
| 44 | + |
| 45 | +void XFREE(void *p, void* heap, int type) |
| 46 | +{ |
| 47 | + return FreePool(p); |
| 48 | +} |
| 49 | + |
| 50 | +/* TODO: remove dependencies in random.c to open/read/close */ |
| 51 | +int open (const char *__file, int __oflag) |
| 52 | +{ |
| 53 | + uefi_printf("open\n"); |
| 54 | + return -1; |
| 55 | +} |
| 56 | + |
| 57 | +ssize_t read (int __fd, void *__buf, size_t __nbytes) |
| 58 | +{ |
| 59 | + uefi_printf("read\n"); |
| 60 | + return -1; |
| 61 | +} |
| 62 | + |
| 63 | +int close(int __fd) |
| 64 | +{ |
| 65 | + uefi_printf("close\n"); |
| 66 | + return -1; |
| 67 | +} |
| 68 | + |
| 69 | +EFI_STATUS |
| 70 | +EFIAPI |
| 71 | +efi_main (EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable) |
| 72 | +{ |
| 73 | + EFI_LOADED_IMAGE *loaded_image = NULL; |
| 74 | + volatile int debug = 1; |
| 75 | + EFI_STATUS status; |
| 76 | + int ret; |
| 77 | + |
| 78 | + InitializeLib(ImageHandle, SystemTable); |
| 79 | + wolfSSL_Debugging_ON(); |
| 80 | + wolfSSL_SetLoggingCb(logging_cb); |
| 81 | + |
| 82 | + status = uefi_call_wrapper(SystemTable->BootServices->HandleProtocol, |
| 83 | + 3, |
| 84 | + ImageHandle, |
| 85 | + &LoadedImageProtocol, |
| 86 | + (void **)&loaded_image); |
| 87 | + Print(L"status: 0x%lx\n", status); |
| 88 | + Print(L"Image base: 0x%lx\n", loaded_image->ImageBase); |
| 89 | + |
| 90 | +#if WAIT_FOR_GDB |
| 91 | + /* to debug from gdb: |
| 92 | + * |
| 93 | + * 1. run qemu with -s option. Take note of Image base value printed by the |
| 94 | + * app. |
| 95 | + * 2. run gdb, use command: symbol-file wolfcrypt.elf -o $image_base |
| 96 | + * with image based value from the print above. |
| 97 | + * 3. set variable debug = 0 to exit the loop and continue the debugging */ |
| 98 | + while(debug) {}; |
| 99 | +#else |
| 100 | + (void)debug; |
| 101 | +#endif |
| 102 | + |
| 103 | + ret = wolfcrypt_test(NULL); |
| 104 | + Print(L"ret: %d\n", ret); |
| 105 | + |
| 106 | + return EFI_SUCCESS; |
| 107 | +} |
0 commit comments