Subversion Repositories HelenOS

Rev

Rev 2726 | Rev 3588 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2726 vana 1
#include <efi.h>
2
#include <efilib.h>
3
 
4
#define KERNEL_LOAD_ADDRESS 0x4400000
5
 
6
 
7
static CHAR16 *
8
a2u (char *str)
9
{
10
    static CHAR16 mem[2048];
11
    int i;
12
 
13
    for (i = 0; str[i]; ++i)
14
        mem[i] = (CHAR16) str[i];
15
    mem[i] = 0;
16
    return mem;
17
}
18
char HEX[256];
19
 
20
char hexs[]="0123456789ABCDEF";
21
/*
22
void to_hex(unsigned long long num)
23
{
24
    int a;
25
    for(a=15;a>=0;a--)    
26
    {
27
    char c=num - (num & 0xfffffffffffffff0LL);
28
    num/=16;
29
    c=hexs[c];
30
    HEX[a]=c;
31
    }
32
 
33
}
34
*/
35
 
36
EFI_STATUS
37
efi_main (EFI_HANDLE image, EFI_SYSTEM_TABLE *systab)
38
{
39
    SIMPLE_TEXT_OUTPUT_INTERFACE *conout;
40
 
41
    EFI_INPUT_KEY efi_input_key;
42
    EFI_STATUS efi_status;
43
 
44
    InitializeLib(image, systab);
45
 
46
    Print(L"HelloLib application started\n");
47
 
48
    EFI_GUID LoadedImageProtocol=LOADED_IMAGE_PROTOCOL;
49
    EFI_GUID DevicePathProtocol=DEVICE_PATH_PROTOCOL;
50
    EFI_GUID FileSystemProtocol=SIMPLE_FILE_SYSTEM_PROTOCOL;
51
 
52
 
53
    EFI_LOADED_IMAGE *LoadedImage;
54
    EFI_DEVICE_PATH *DevicePath;
55
 
56
    BS->HandleProtocol(image,
57
    &LoadedImageProtocol,
58
    &LoadedImage);
59
    BS->HandleProtocol(LoadedImage->DeviceHandle,
60
    &DevicePathProtocol,
61
    &DevicePath);
62
    Print (L"Image device : %s\n", DevicePathToStr (DevicePath));
63
    Print (L"Image file   : %s\n", DevicePathToStr (LoadedImage->FilePath));
64
    Print (L"Image Base   : %X\n", LoadedImage->ImageBase);
65
    Print (L"Image Size   : %X\n", LoadedImage->ImageSize);
66
 
67
 
68
 
69
    EFI_FILE_IO_INTERFACE *Vol;
70
 
71
    EFI_FILE *CurDir;
72
    EFI_FILE *FileHandle;
73
 
74
    BS->HandleProtocol(LoadedImage->DeviceHandle, &FileSystemProtocol, &Vol);
75
    Vol->OpenVolume (Vol, &CurDir);
76
 
77
    char FileName[1024];
78
    char *OsKernelBuffer;
79
    int i;
80
    UINTN Size;
81
 
82
    StrCpy(FileName,DevicePathToStr(LoadedImage->FilePath));
83
    for(i=StrLen(FileName);i>=0 && FileName[i]!='\\';i--);
84
    FileName[i] = 0;
85
 
86
    Print(L"%s\n",LoadedImage->LoadOptions);
87
 
88
    i=0;
89
    CHAR16 *LoadOptions = LoadedImage->LoadOptions;
90
 
91
 
92
 
93
    while(1) if(LoadOptions[i++]!=L' ') break;
94
    while(LoadOptions[i]!=L' '){   
95
        if(LoadOptions[i]==0) break;
96
        i++;
97
    }
98
    while(LoadOptions[i]==L' ') if(LoadOptions[i++]==0) break;
99
 
100
    if(LoadOptions[i++]==0)
101
        StrCat(FileName,L"\\image.bin");
102
    else{
103
        CHAR16 buf[1024];
104
        buf[0]='\\';
105
        i--;
106
        int j;
107
        for(j=0;LoadOptions[i+j]!=L' '&&LoadOptions[i+j]!=0;j++)
108
            buf[j+1]=LoadOptions[i+j];
109
        buf[j+1]=0;
110
        StrCat(FileName,buf);
111
    }
112
 
113
    //Print(L"%s\n",FileName);
114
 
115
    EFI_STATUS stat;
116
    stat=CurDir->Open(CurDir, &FileHandle, FileName, EFI_FILE_MODE_READ, 0);
117
    if(EFI_ERROR(stat)){
118
        Print(L"Error Opening Image %s\n",FileName);
119
        return 0;
120
    }    
121
    Size = 0x00400000;
122
    BS->AllocatePool(EfiLoaderData, Size, &OsKernelBuffer);
123
    FileHandle->Read(FileHandle, &Size, OsKernelBuffer);
124
    FileHandle->Close(FileHandle);
125
 
126
    if(Size<1) return 0;
127
 
128
 
129
    char *  HOS = OsKernelBuffer;  
130
    int HOSSize = Size;  
131
 
132
 
133
    {
134
        UINTN cookie;
135
        void *p=(void *)KERNEL_LOAD_ADDRESS;
136
        UINTN mapsize,descsize;
137
        UINT32 desver;
138
        EFI_STATUS status;
139
        EFI_MEMORY_DESCRIPTOR emd[1024];
140
 
141
 
142
        mapsize=1024*sizeof(emd);
143
 
144
        status=BS->AllocatePages(AllocateAnyPages,EfiLoaderData,/*(HOSSize>>12)+1*/ 1,p);
145
        if(EFI_ERROR(status)){
146
        Print(L"Error 0\n");
147
        if(status == EFI_OUT_OF_RESOURCES) Print(L"EFI_OUT_OF_RESOURCES\n");
148
        if(status == EFI_INVALID_PARAMETER) Print(L"EFI_INVALID_PARAMETER\n");
149
        if(status == EFI_NOT_FOUND) Print(L"EFI_NOT_FOUND\n");
150
        return EFI_SUCCESS;
151
        }
152
 
153
        status=BS->GetMemoryMap(&mapsize,emd,&cookie,&descsize,&desver);
154
        if(EFI_ERROR(status)){
155
        Print(L"Error 1\n");
156
        return EFI_SUCCESS;
157
        }
158
        status=BS->ExitBootServices(image,cookie); 
159
        if(EFI_ERROR(status)){
160
        Print(L"Error 2\n");
161
        return EFI_SUCCESS;
162
        }
163
 
164
    }
165
    int a;
166
 
167
    for(a=0;a<HOSSize;a++){
168
        ((char *)(0x4400000))[a]=HOS[a];
169
    }
170
 
171
    //Run Kernel
172
    asm volatile(  
173
        "nop.i 0x00 ;;\n"
174
        "movl r15 = 0x4400000 ;;\n"
175
        "mov b0 = r15;;"
176
        "br.few b0;;\n"
177
    );
178
 
179
 
180
    while(1){
181
        ((volatile int *)(0x80000000000b8000))[0]++;
182
    }
183
    return EFI_SUCCESS;
184
}