Subversion Repositories HelenOS

Rev

Rev 3588 | 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. #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.  
  14. //Link image as a data array into hello - usefull with network boot
  15. #define IMAGE_LINKED
  16.  
  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.  
  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;
  82.     int defaultLoad;
  83.     int imageLoad;
  84.     UINTN Size;
  85.  
  86.     StrCpy(FileName,DevicePathToStr(LoadedImage->FilePath));
  87.     for(i=StrLen(FileName);i>=0 && FileName[i]!='\\';i--);
  88.     FileName[i] = 0;
  89.    
  90.     Print(L"%s\n",LoadedImage->LoadOptions);
  91.    
  92.     i=0;
  93.     CHAR16 *LoadOptions = LoadedImage->LoadOptions;
  94.    
  95.    
  96.    
  97.     while(1) if(LoadOptions[i++]!=L' ') break;
  98.     while(LoadOptions[i]!=L' '){   
  99.         if(LoadOptions[i]==0) break;
  100.         i++;
  101.     }
  102.     while(LoadOptions[i]==L' ') if(LoadOptions[i++]==0) break;
  103.    
  104.     if(LoadOptions[i++]==0){
  105.         StrCat(FileName,L"\\image.bin");
  106.         defaultLoad=1;
  107.     }  
  108.     else{
  109.         CHAR16 buf[1024];
  110.         buf[0]='\\';
  111.         i--;
  112.         int j;
  113.         for(j=0;LoadOptions[i+j]!=L' '&&LoadOptions[i+j]!=0;j++)
  114.             buf[j+1]=LoadOptions[i+j];
  115.         buf[j+1]=0;
  116.         StrCat(FileName,buf);
  117.         defaultLoad=0;
  118.     }
  119.  
  120.     imageLoad=1;
  121. #ifdef IMAGE_LINKED
  122.     if(defaultLoad) {
  123.         Print(L"Using Linked Image\n");
  124.         imageLoad=0;
  125.     }    
  126. #endif 
  127.    
  128.  
  129.     char *  HOS;
  130.     if(imageLoad)
  131.     {
  132.         Size = 0x00400000;
  133.  
  134.             Vol->OpenVolume (Vol, &CurDir);
  135.  
  136.         EFI_STATUS stat;
  137.         stat=CurDir->Open(CurDir, &FileHandle, FileName, EFI_FILE_MODE_READ, 0);
  138.         if(EFI_ERROR(stat)){
  139.             Print(L"Error Opening Image %s\n",FileName);
  140.             return 0;
  141.         }
  142.             BS->AllocatePool(EfiLoaderData, Size, &OsKernelBuffer);
  143.         FileHandle->Read(FileHandle, &Size, OsKernelBuffer);
  144.         FileHandle->Close(FileHandle);
  145.         HOS = OsKernelBuffer;  
  146.             if(Size<1) return 0;
  147.  
  148.     }      
  149. #ifdef IMAGE_LINKED
  150.     else {
  151.         HOS = HOSimage;  
  152.         Size = HOSimagesize;
  153.         Print(L"Image start %llX\n",(long long)HOS);
  154.         Print(L"Image size %llX\n",(long long)Size);
  155.         Print(L"Image &size %llX\n",(long long)&Size);
  156.     }
  157. #endif 
  158.     int HOSSize = Size;  
  159.  
  160.  
  161.     rArg rSAL;
  162.     rArg rPAL;
  163.  
  164.     //Setup AP's wake up address
  165.     LibSalProc(0x01000000,2,0x4400200,0,0,0,0,0,&rSAL);
  166.  
  167.  
  168.     //Get System Frequency
  169.     UINT64 sys_freq;
  170.     LibSalProc(0x01000012,0,0,0,0,0,0,0,&rSAL);
  171.     sys_freq=rSAL.p1;
  172.    
  173.  
  174.     UINT64 freq_scale;
  175.     //Get CPU Frequency to System Frequency ratio
  176.     LibPalProc(14,0,0,0,&rPAL);
  177.     freq_scale=rPAL.p1;
  178.  
  179.  
  180.         UINT64 sapic;
  181.         LibGetSalIpiBlock(&sapic);
  182.         Print (L"SAPIC:%X\n", sapic);
  183.     //bootinfo->sapic=sapic;
  184.  
  185.  
  186.         UINT64 wakeup_intno;
  187.         LibGetSalWakeupVector(&wakeup_intno);
  188.         Print (L"WAKEUP INTNO:%X\n", wakeup_intno);
  189.  
  190.  
  191.  
  192.  
  193.  
  194.         UINTN cookie;
  195.         void *p=(void *)KERNEL_LOAD_ADDRESS;
  196.         UINTN mapsize,descsize;
  197.         UINT32 desver;
  198.         EFI_STATUS status;
  199.        
  200.                    
  201.        
  202.         status=BS->AllocatePages(AllocateAnyPages,EfiLoaderData,/*(HOSSize>>12)+1*/ 1,p);
  203.         if(EFI_ERROR(status)){
  204.         Print(L"Error 0\n");
  205.         if(status == EFI_OUT_OF_RESOURCES) Print(L"EFI_OUT_OF_RESOURCES\n");
  206.         if(status == EFI_INVALID_PARAMETER) Print(L"EFI_INVALID_PARAMETER\n");
  207.         if(status == EFI_NOT_FOUND) Print(L"EFI_NOT_FOUND\n");
  208.         return EFI_SUCCESS;
  209.         }
  210.        
  211.         UINTN no_entryes;
  212.         void * mds;
  213.         mds=LibMemoryMap(&no_entryes,&cookie,&descsize,&desver);
  214.        
  215.         for(i=0;i<no_entryes;i++)
  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));
  221.             Print(L"T:%02d %016llX %016llX\n",type,base,pages*EFI_PAGE_SIZE);
  222.        
  223.         }
  224.         status=BS->ExitBootServices(image,cookie); 
  225.         if(EFI_ERROR(status)){
  226.         Print(L"Error 2\n");
  227.         return EFI_SUCCESS;
  228.         }
  229.        
  230.    
  231.     int a;
  232.    
  233.     for(a=0;a<HOSSize;a++){
  234.         ((char *)(0x4400000))[a]=HOS[a];
  235.     }
  236.     bootinfo->sapic=(unsigned long *)sapic;
  237.     bootinfo->wakeup_intno=wakeup_intno;
  238.     bootinfo->sys_freq=sys_freq;
  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.  
  286.    
  287.     //Run Kernel
  288.     asm volatile(  
  289.         "nop.i 0x00 ;;\n"
  290.         "movl r15 = 0x4400000 ;;\n"
  291.         "mov b0 = r15;;"
  292.         "br.few b0;;\n"
  293.     );
  294.        
  295.    
  296.     //Not reached      
  297.     return EFI_SUCCESS;
  298. }
  299.