Subversion Repositories HelenOS

Rev

Rev 2726 | Rev 3616 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. #include <efi.h>
  2. #include <efilib.h>
  3.  
  4. #include <../../../../../../kernel/arch/ia64/include/bootinfo.h>
  5.  
  6. #define KERNEL_LOAD_ADDRESS 0x4400000
  7.  
  8. //Link image as a data array into hello - usefull with network boot
  9. //#define IMAGE_LINKED
  10.  
  11. bootinfo_t *bootinfo=(bootinfo_t *)BOOTINFO_ADDRESS;
  12.  
  13.  
  14. #ifdef IMAGE_LINKED
  15. extern char HOSimage[];
  16. extern int HOSimagesize;
  17. #endif
  18.  
  19.  
  20.  
  21. static CHAR16 *
  22. a2u (char *str)
  23. {
  24.     static CHAR16 mem[2048];
  25.     int i;
  26.  
  27.     for (i = 0; str[i]; ++i)
  28.         mem[i] = (CHAR16) str[i];
  29.     mem[i] = 0;
  30.     return mem;
  31. }
  32.  
  33. EFI_STATUS
  34. efi_main (EFI_HANDLE image, EFI_SYSTEM_TABLE *systab)
  35. {
  36.     SIMPLE_TEXT_OUTPUT_INTERFACE *conout;
  37.  
  38.     EFI_INPUT_KEY efi_input_key;
  39.     EFI_STATUS efi_status;
  40.  
  41.     InitializeLib(image, systab);
  42.  
  43.     Print(L"HelloLib application started\n");
  44.  
  45.     EFI_GUID LoadedImageProtocol=LOADED_IMAGE_PROTOCOL;
  46.     EFI_GUID DevicePathProtocol=DEVICE_PATH_PROTOCOL;
  47.     EFI_GUID FileSystemProtocol=SIMPLE_FILE_SYSTEM_PROTOCOL;
  48.    
  49.    
  50.     EFI_LOADED_IMAGE *LoadedImage;
  51.     EFI_DEVICE_PATH *DevicePath;
  52.    
  53.     BS->HandleProtocol(image,
  54.     &LoadedImageProtocol,
  55.     &LoadedImage);
  56.     BS->HandleProtocol(LoadedImage->DeviceHandle,
  57.     &DevicePathProtocol,
  58.     &DevicePath);
  59.     Print (L"Image device : %s\n", DevicePathToStr (DevicePath));
  60.     Print (L"Image file   : %s\n", DevicePathToStr (LoadedImage->FilePath));
  61.     Print (L"Image Base   : %X\n", LoadedImage->ImageBase);
  62.     Print (L"Image Size   : %X\n", LoadedImage->ImageSize);
  63.  
  64.  
  65.  
  66.     EFI_FILE_IO_INTERFACE *Vol;
  67.  
  68.     EFI_FILE *CurDir;
  69.     EFI_FILE *FileHandle;
  70.  
  71.     BS->HandleProtocol(LoadedImage->DeviceHandle, &FileSystemProtocol, &Vol);
  72.  
  73.     char FileName[1024];
  74.     char *OsKernelBuffer;
  75.     int i;
  76.     int defaultLoad;
  77.     int imageLoad;
  78.     UINTN Size;
  79.  
  80.     StrCpy(FileName,DevicePathToStr(LoadedImage->FilePath));
  81.     for(i=StrLen(FileName);i>=0 && FileName[i]!='\\';i--);
  82.     FileName[i] = 0;
  83.    
  84.     Print(L"%s\n",LoadedImage->LoadOptions);
  85.    
  86.     i=0;
  87.     CHAR16 *LoadOptions = LoadedImage->LoadOptions;
  88.    
  89.    
  90.    
  91.     while(1) if(LoadOptions[i++]!=L' ') break;
  92.     while(LoadOptions[i]!=L' '){   
  93.         if(LoadOptions[i]==0) break;
  94.         i++;
  95.     }
  96.     while(LoadOptions[i]==L' ') if(LoadOptions[i++]==0) break;
  97.    
  98.     if(LoadOptions[i++]==0){
  99.         StrCat(FileName,L"\\image.bin");
  100.         defaultLoad=1;
  101.     }  
  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.         defaultLoad=0;
  112.     }
  113.  
  114.     imageLoad=1;
  115. #ifdef IMAGE_LINKED
  116.     if(defaultLoad) {
  117.         Print(L"Using Linked Image\n");
  118.         imageLoad=0;
  119.     }    
  120. #endif 
  121.    
  122.  
  123.     char *  HOS;
  124.     if(imageLoad)
  125.     {
  126.         Size = 0x00400000;
  127.  
  128.             Vol->OpenVolume (Vol, &CurDir);
  129.  
  130.         EFI_STATUS stat;
  131.         stat=CurDir->Open(CurDir, &FileHandle, FileName, EFI_FILE_MODE_READ, 0);
  132.         if(EFI_ERROR(stat)){
  133.             Print(L"Error Opening Image %s\n",FileName);
  134.             return 0;
  135.         }
  136.             BS->AllocatePool(EfiLoaderData, Size, &OsKernelBuffer);
  137.         FileHandle->Read(FileHandle, &Size, OsKernelBuffer);
  138.         FileHandle->Close(FileHandle);
  139.         HOS = OsKernelBuffer;  
  140.             if(Size<1) return 0;
  141.  
  142.     }      
  143. #ifdef IMAGE_LINKED
  144.     else {
  145.         HOS = HOSimage;  
  146.         Size = HOSimagesize;
  147.         Print(L"Image start %llX\n",(long long)HOS);
  148.         Print(L"Image size %llX\n",(long long)Size);
  149.         Print(L"Image &size %llX\n",(long long)&Size);
  150.     }
  151. #endif 
  152.     int HOSSize = Size;  
  153.  
  154.  
  155.     rArg rSAL;
  156.     //Setup AP's wake up address
  157.  
  158.     LibSalProc(0x01000000,2,0x4400200,0,0,0,0,0,&rSAL);
  159.  
  160.  
  161.         UINT64 sapic;
  162.         LibGetSalIpiBlock(&sapic);
  163.         Print (L"SAPIC:%X\n", sapic);
  164.     bootinfo->sapic=sapic;
  165.  
  166.  
  167.         int wakeup_intno;
  168.         wakeup_intno=0xf0;
  169.         Print (L"WAKEUP INTNO:%X\n", wakeup_intno);
  170.     bootinfo->wakeup_intno=wakeup_intno;
  171.  
  172.  
  173.  
  174.  
  175.  
  176.     {
  177.         UINTN cookie;
  178.         void *p=(void *)KERNEL_LOAD_ADDRESS;
  179.         UINTN mapsize,descsize;
  180.         UINT32 desver;
  181.         EFI_STATUS status;
  182.         EFI_MEMORY_DESCRIPTOR emd[1024];
  183.        
  184.                    
  185.         mapsize=1024*sizeof(emd);
  186.        
  187.         status=BS->AllocatePages(AllocateAnyPages,EfiLoaderData,/*(HOSSize>>12)+1*/ 1,p);
  188.         if(EFI_ERROR(status)){
  189.         Print(L"Error 0\n");
  190.         if(status == EFI_OUT_OF_RESOURCES) Print(L"EFI_OUT_OF_RESOURCES\n");
  191.         if(status == EFI_INVALID_PARAMETER) Print(L"EFI_INVALID_PARAMETER\n");
  192.         if(status == EFI_NOT_FOUND) Print(L"EFI_NOT_FOUND\n");
  193.         return EFI_SUCCESS;
  194.         }
  195.        
  196.         status=BS->GetMemoryMap(&mapsize,emd,&cookie,&descsize,&desver);
  197.         if(EFI_ERROR(status)){
  198.         Print(L"Error 1\n");
  199.         return EFI_SUCCESS;
  200.         }
  201.         status=BS->ExitBootServices(image,cookie); 
  202.         if(EFI_ERROR(status)){
  203.         Print(L"Error 2\n");
  204.         return EFI_SUCCESS;
  205.         }
  206.        
  207.     }
  208.     int a;
  209.    
  210.     for(a=0;a<HOSSize;a++){
  211.         ((char *)(0x4400000))[a]=HOS[a];
  212.     }
  213.     bootinfo->sapic=(unsigned long *)sapic;
  214.     bootinfo->wakeup_intno=wakeup_intno;
  215.    
  216.     //Run Kernel
  217.     asm volatile(  
  218.         "nop.i 0x00 ;;\n"
  219.         "movl r15 = 0x4400000 ;;\n"
  220.         "mov b0 = r15;;"
  221.         "br.few b0;;\n"
  222.     );
  223.        
  224.    
  225.     //Not reached      
  226.     return EFI_SUCCESS;
  227. }
  228.