summaryrefslogtreecommitdiffstats
path: root/efi_memtest/MemtestEfi.c
blob: 0d2d47290859758c006e67153a423849e0f62722 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126

#include "Uefi.h"
#include "Library/UefiLib.h"
#include "Library/UefiApplicationEntryPoint.h"
#include "Guid/FileSystemInfo.h"
#include "Protocol/LoadedImage.h"

#include "main.h" // TODO move into main dir
#include "memtest86+/logger.h"
#include "memtest86+/display.h"
#include "logger_config.h"

extern EFI_SYSTEM_TABLE *gST;

// TODO remove
int global = 1;

void stack_check() {
    int i;
    int *p = &i;
    char log[32] = "stack ptr = ";
    int length = 12;
    int_to_charr((unsigned long) p, log, &length);
    print_log(log, length);
}

EFI_STATUS
EFIAPI
UefiMain (
    IN EFI_HANDLE        ImageHandle,
    IN EFI_SYSTEM_TABLE  *SystemTable
    )
{

    SystemTable->ConOut->ClearScreen(SystemTable->ConOut);
    SystemTable->ConOut->SetCursorPosition(SystemTable->ConOut, 0, 0);

    trace_init("Starting MemtestEfi");

    init_logger();
    
    if (logflag) {
        char msg[] = "MemtestEfi started";
        print_log(msg, sizeof(msg) - 1);
    }

  
    if (logflag) {
        char log[41] = "Address of UefiMain: ";
        int length = 21;
        int_to_charr((unsigned long)UefiMain, log, &length);
        print_log(log, length);
    }

    if (logflag) {
        char log[43] = "Address of test_start: ";
        int length = 23;
        int_to_charr((unsigned long)test_start, log, &length);
        print_log(log, length);
    }

    VOID *LoadedImage;
    EFI_STATUS status = SystemTable->BootServices->OpenProtocol(
        ImageHandle,
        &gEfiLoadedImageProtocolGuid,
        &LoadedImage,
        ImageHandle,
        NULL,
        EFI_OPEN_PROTOCOL_GET_PROTOCOL);
    if (status != EFI_SUCCESS) {
        if (logflag) print_log("Failed to open LoadedImage.", 27);
    } else {
        VOID *base = ((EFI_LOADED_IMAGE_PROTOCOL *) LoadedImage)->ImageBase;
        UINT64 size = ((EFI_LOADED_IMAGE_PROTOCOL *) LoadedImage)->ImageSize;
        if (logflag) {
            char log[31] = "ImageBase: ";
            int length = 11;
            int_to_charr((unsigned long) base, log, &length);
            print_log(log, length);
        }

        if (logflag) {
            char log[31] = "ImageSize: ";
            int length = 11;
            int_to_charr(size, log, &length);
            print_log(log, length);
        }
    }

    if (logflag) {
        char log[45] = "Address of SystemTable = ";
        int length = 25;
        int_to_charr((unsigned long) SystemTable, log, &length);
        print_log(log, length);
    }

    if (logflag) {
        char log[45] = "Current return address = ";
        int length = 25;
        int_to_charr((unsigned long) ((unsigned long *) __builtin_return_address(0)), log, &length);
        print_log(log, length);
    }

    stack_check();

    if (logflag) {
        char log[49] = "Address of global variable = ";
        int length = 29;
        int_to_charr((unsigned long) &global, log, &length);
        print_log(log, length);
    }
  
    test_start();

    if (logflag) {
        char msg[] = "MemtestEfi finished!\n";
        print_log(msg, sizeof(msg) - 1);
    }

    //while(1) {}

    SystemTable->ConOut->SetCursorPosition(SystemTable->ConOut, 0, 25);

    return EFI_SUCCESS;
}