Subversion Repositories HelenOS

Rev

Rev 3695 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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