Subversion Repositories HelenOS

Rev

Rev 3588 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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