Subversion Repositories HelenOS

Rev

Rev 3675 | 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
 
3597 svoboda 4
#include <../../../../../../kernel/arch/ia64/include/bootinfo.h>
5
 
2726 vana 6
#define KERNEL_LOAD_ADDRESS 0x4400000
7
 
3623 svoboda 8
#define MEM_MAP_DESCRIPTOR_OFFSET_TYPE 0        
9
#define MEM_MAP_DESCRIPTOR_OFFSET_BASE 8    
10
#define MEM_MAP_DESCRIPTOR_OFFSET_PAGES 24    
11
 
12
 
13
 
3597 svoboda 14
//Link image as a data array into hello - usefull with network boot
4377 svoboda 15
//#define IMAGE_LINKED
2726 vana 16
 
3597 svoboda 17
bootinfo_t *bootinfo=(bootinfo_t *)BOOTINFO_ADDRESS;
18
 
19
 
20
#ifdef IMAGE_LINKED
21
extern char HOSimage[];
22
extern int HOSimagesize;
23
#endif
24
 
25
 
26
 
2726 vana 27
static CHAR16 *
28
a2u (char *str)
29
{
30
    static CHAR16 mem[2048];
31
    int i;
32
 
33
    for (i = 0; str[i]; ++i)
34
        mem[i] = (CHAR16) str[i];
35
    mem[i] = 0;
36
    return mem;
37
}
38
 
39
EFI_STATUS
40
efi_main (EFI_HANDLE image, EFI_SYSTEM_TABLE *systab)
41
{
42
    SIMPLE_TEXT_OUTPUT_INTERFACE *conout;
43
 
44
    EFI_INPUT_KEY efi_input_key;
45
    EFI_STATUS efi_status;
46
 
47
    InitializeLib(image, systab);
48
 
49
    Print(L"HelloLib application started\n");
50
 
51
    EFI_GUID LoadedImageProtocol=LOADED_IMAGE_PROTOCOL;
52
    EFI_GUID DevicePathProtocol=DEVICE_PATH_PROTOCOL;
53
    EFI_GUID FileSystemProtocol=SIMPLE_FILE_SYSTEM_PROTOCOL;
54
 
55
 
56
    EFI_LOADED_IMAGE *LoadedImage;
57
    EFI_DEVICE_PATH *DevicePath;
58
 
59
    BS->HandleProtocol(image,
60
    &LoadedImageProtocol,
61
    &LoadedImage);
62
    BS->HandleProtocol(LoadedImage->DeviceHandle,
63
    &DevicePathProtocol,
64
    &DevicePath);
65
    Print (L"Image device : %s\n", DevicePathToStr (DevicePath));
66
    Print (L"Image file   : %s\n", DevicePathToStr (LoadedImage->FilePath));
67
    Print (L"Image Base   : %X\n", LoadedImage->ImageBase);
68
    Print (L"Image Size   : %X\n", LoadedImage->ImageSize);
69
 
70
 
71
 
72
    EFI_FILE_IO_INTERFACE *Vol;
73
 
74
    EFI_FILE *CurDir;
75
    EFI_FILE *FileHandle;
76
 
77
    BS->HandleProtocol(LoadedImage->DeviceHandle, &FileSystemProtocol, &Vol);
78
 
79
    char FileName[1024];
80
    char *OsKernelBuffer;
81
    int i;
3597 svoboda 82
    int defaultLoad;
83
    int imageLoad;
2726 vana 84
    UINTN Size;
85
 
86
    StrCpy(FileName,DevicePathToStr(LoadedImage->FilePath));
87
    for(i=StrLen(FileName);i>=0 && FileName[i]!='\\';i--);
88
    FileName[i] = 0;
4377 svoboda 89
    FileName[0] = 0;
2726 vana 90
 
91
    Print(L"%s\n",LoadedImage->LoadOptions);
92
 
93
    i=0;
94
    CHAR16 *LoadOptions = LoadedImage->LoadOptions;
95
 
96
 
97
 
98
    while(1) if(LoadOptions[i++]!=L' ') break;
99
    while(LoadOptions[i]!=L' '){   
100
        if(LoadOptions[i]==0) break;
101
        i++;
102
    }
103
    while(LoadOptions[i]==L' ') if(LoadOptions[i++]==0) break;
104
 
3597 svoboda 105
    if(LoadOptions[i++]==0){
2726 vana 106
        StrCat(FileName,L"\\image.bin");
3597 svoboda 107
        defaultLoad=1;
108
    }  
4377 svoboda 109
/*  else{
2726 vana 110
        CHAR16 buf[1024];
4377 svoboda 111
        //buf[0]='\\';
2726 vana 112
        i--;
113
        int j;
114
        for(j=0;LoadOptions[i+j]!=L' '&&LoadOptions[i+j]!=0;j++)
115
            buf[j+1]=LoadOptions[i+j];
116
        buf[j+1]=0;
117
        StrCat(FileName,buf);
3597 svoboda 118
        defaultLoad=0;
4377 svoboda 119
    }*/
120
    else{
121
        CHAR16 buf[1024];
122
        //buf[0]='\\';
123
        i--;
124
        int j;
125
        for(j=0;LoadOptions[i+j]!=L' '&&LoadOptions[i+j]!=0;j++)
126
            buf[j]=LoadOptions[i+j];
127
        buf[j+1]=0;
128
        StrCat(FileName,buf);
129
        defaultLoad=0;
2726 vana 130
    }
131
 
3597 svoboda 132
    imageLoad=1;
133
#ifdef IMAGE_LINKED
134
    if(defaultLoad) {
135
        Print(L"Using Linked Image\n");
136
        imageLoad=0;
2726 vana 137
    }    
3597 svoboda 138
#endif  
139
 
2726 vana 140
 
3597 svoboda 141
    char *  HOS;
142
    if(imageLoad)
143
    {
144
        Size = 0x00400000;
2726 vana 145
 
3597 svoboda 146
            Vol->OpenVolume (Vol, &CurDir);
2726 vana 147
 
3597 svoboda 148
        EFI_STATUS stat;
149
        stat=CurDir->Open(CurDir, &FileHandle, FileName, EFI_FILE_MODE_READ, 0);
150
        if(EFI_ERROR(stat)){
151
            Print(L"Error Opening Image %s\n",FileName);
152
            return 0;
153
        }
154
            BS->AllocatePool(EfiLoaderData, Size, &OsKernelBuffer);
155
        FileHandle->Read(FileHandle, &Size, OsKernelBuffer);
156
        FileHandle->Close(FileHandle);
157
        HOS = OsKernelBuffer;  
158
            if(Size<1) return 0;
159
 
160
    }      
161
#ifdef IMAGE_LINKED
162
    else {
163
        HOS = HOSimage;  
164
        Size = HOSimagesize;
165
        Print(L"Image start %llX\n",(long long)HOS);
166
        Print(L"Image size %llX\n",(long long)Size);
167
        Print(L"Image &size %llX\n",(long long)&Size);
168
    }
169
#endif  
2726 vana 170
    int HOSSize = Size;  
171
 
172
 
3597 svoboda 173
    rArg rSAL;
174
    rArg rPAL;
175
 
176
    //Setup AP's wake up address
177
    LibSalProc(0x01000000,2,0x4400200,0,0,0,0,0,&rSAL);
178
 
179
 
180
    //Get System Frequency
181
    UINT64 sys_freq;
182
    LibSalProc(0x01000012,0,0,0,0,0,0,0,&rSAL);
183
    sys_freq=rSAL.p1;
184
 
185
 
186
    UINT64 freq_scale;
187
    //Get CPU Frequency to System Frequency ratio
188
    LibPalProc(14,0,0,0,&rPAL);
189
    freq_scale=rPAL.p1;
190
 
191
 
192
        UINT64 sapic;
193
        LibGetSalIpiBlock(&sapic);
194
        Print (L"SAPIC:%X\n", sapic);
195
    //bootinfo->sapic=sapic;
196
 
197
 
3623 svoboda 198
        UINT64 wakeup_intno;
199
        LibGetSalWakeupVector(&wakeup_intno);
3597 svoboda 200
        Print (L"WAKEUP INTNO:%X\n", wakeup_intno);
201
 
202
 
203
 
204
 
205
 
2726 vana 206
        UINTN cookie;
207
        void *p=(void *)KERNEL_LOAD_ADDRESS;
208
        UINTN mapsize,descsize;
209
        UINT32 desver;
210
        EFI_STATUS status;
211
 
212
 
213
 
214
        status=BS->AllocatePages(AllocateAnyPages,EfiLoaderData,/*(HOSSize>>12)+1*/ 1,p);
215
        if(EFI_ERROR(status)){
216
        Print(L"Error 0\n");
217
        if(status == EFI_OUT_OF_RESOURCES) Print(L"EFI_OUT_OF_RESOURCES\n");
218
        if(status == EFI_INVALID_PARAMETER) Print(L"EFI_INVALID_PARAMETER\n");
219
        if(status == EFI_NOT_FOUND) Print(L"EFI_NOT_FOUND\n");
220
        return EFI_SUCCESS;
221
        }
222
 
3623 svoboda 223
        UINTN no_entryes;
224
        void * mds;
225
        mds=LibMemoryMap(&no_entryes,&cookie,&descsize,&desver);
226
 
227
        for(i=0;i<no_entryes;i++)
228
        {
229
 
230
            unsigned int type=*((unsigned int *)(mds+i*descsize+MEM_MAP_DESCRIPTOR_OFFSET_TYPE));
231
            unsigned long long base=*((unsigned long long *)(mds+i*descsize+MEM_MAP_DESCRIPTOR_OFFSET_BASE));
232
            unsigned long long pages=*((unsigned long long *)(mds+i*descsize+MEM_MAP_DESCRIPTOR_OFFSET_PAGES));
233
            Print(L"T:%02d %016llX %016llX\n",type,base,pages*EFI_PAGE_SIZE);
234
 
2726 vana 235
        }
236
        status=BS->ExitBootServices(image,cookie); 
237
        if(EFI_ERROR(status)){
238
        Print(L"Error 2\n");
239
        return EFI_SUCCESS;
240
        }
241
 
3623 svoboda 242
 
2726 vana 243
    int a;
244
 
245
    for(a=0;a<HOSSize;a++){
246
        ((char *)(0x4400000))[a]=HOS[a];
247
    }
3597 svoboda 248
    bootinfo->sapic=(unsigned long *)sapic;
249
    bootinfo->wakeup_intno=wakeup_intno;
250
    bootinfo->sys_freq=sys_freq;
251
    bootinfo->freq_scale=freq_scale;
3675 svoboda 252
    bootinfo->hello_configured=1;
3623 svoboda 253
 
254
 
255
        bootinfo->memmap_items=0;
256
 
257
        for(i=0;i<no_entryes;i++)
258
        {
259
 
260
            unsigned int type=*((unsigned int *)(mds+i*descsize+MEM_MAP_DESCRIPTOR_OFFSET_TYPE));
261
            unsigned long long base=*((unsigned long long *)(mds+i*descsize+MEM_MAP_DESCRIPTOR_OFFSET_BASE));
262
            unsigned long long pages=*((unsigned long long *)(mds+i*descsize+MEM_MAP_DESCRIPTOR_OFFSET_PAGES));
263
 
264
 
265
 
266
            switch (type)
267
            {
268
                case EfiConventionalMemory:
269
                bootinfo->memmap[bootinfo->memmap_items].type=EFI_MEMMAP_FREE_MEM;      
270
                bootinfo->memmap[bootinfo->memmap_items].base=base;    
271
                bootinfo->memmap[bootinfo->memmap_items].size=pages*EFI_PAGE_SIZE;      
272
                    bootinfo->memmap_items++;
273
                    break;
274
                case EfiMemoryMappedIO:
275
                bootinfo->memmap[bootinfo->memmap_items].type=EFI_MEMMAP_IO;    
276
                bootinfo->memmap[bootinfo->memmap_items].base=base;    
277
                bootinfo->memmap[bootinfo->memmap_items].size=pages*EFI_PAGE_SIZE;      
278
                    bootinfo->memmap_items++;
279
                    break;
280
                case EfiMemoryMappedIOPortSpace:
281
                bootinfo->memmap[bootinfo->memmap_items].type=EFI_MEMMAP_IO_PORTS;      
282
                bootinfo->memmap[bootinfo->memmap_items].base=base;    
283
                bootinfo->memmap[bootinfo->memmap_items].size=pages*EFI_PAGE_SIZE;      
284
                    bootinfo->memmap_items++;
285
                    break;
286
 
287
                default :
288
                    break;
289
            }
290
 
291
 
292
 
293
 
294
 
295
        }
296
 
297
 
2726 vana 298
 
299
    //Run Kernel
300
    asm volatile(  
301
        "nop.i 0x00 ;;\n"
302
        "movl r15 = 0x4400000 ;;\n"
303
        "mov b0 = r15;;"
304
        "br.few b0;;\n"
305
    );
306
 
307
 
3597 svoboda 308
    //Not reached      
2726 vana 309
    return EFI_SUCCESS;
310
}