#include #include UINTN stor(CHAR8*); #define KERNEL_LOAD_ADDRESS 0x101000 static CHAR16 *OsLoaderMemoryTypeDesc[EfiMaxMemoryType] = { L"reserved ", L"LoaderCode", L"LoaderData", L"BS_code ", L"BS_data ", L"RT_code ", L"RT_data ", L"available ", L"Unusable ", L"ACPI_recl ", L"ACPI_NVS ", L"MemMapIO ", L"MemPortIO ", L"PAL_code " }; VOID WriteVGAString(CHAR16 * str); EFI_STATUS efi_main (EFI_HANDLE ImageHandle,EFI_SYSTEM_TABLE *pSystemTable) { EFI_STATUS status; CHAR16 *pDevicePathAsString; EFI_LOADED_IMAGE *pLoadedImage; EFI_DEVICE_PATH *pDevicePath; EFI_FILE_IO_INTERFACE *pVol; EFI_FILE_HANDLE RootFs; EFI_FILE_HANDLE CurDir; EFI_FILE_HANDLE FileHandle; CHAR16 FileName[100]; UINTN i; UINTN Size; CHAR8 *pOsKernelBuffer; InitializeLib(ImageHandle, pSystemTable); Print(L"OS loader application started\n"); status = BS->HandleProtocol(ImageHandle,&LoadedImageProtocol,(VOID*)&pLoadedImage); if(EFI_ERROR(status)){ Print(L"BS->HandleProtocol(ImageHandle,&LoadedImageProtocol,(VOID*)&pLoadedImage) - failed\n"); BS->Exit(ImageHandle,EFI_SUCCESS,0,NULL); } status = BS->HandleProtocol(pLoadedImage->DeviceHandle,&DevicePathProtocol,(VOID*)&pDevicePath); if(EFI_ERROR(status)||pDevicePath == NULL){ Print(L"BS->HandleProtocol(pLoadedImage->DeviceHandle,&DevicePathProtocol,(VOID*)&pDevicePath)-failed\n"); BS->Exit(ImageHandle,EFI_SUCCESS,0,NULL); } pDevicePathAsString = DevicePathToStr(pDevicePath); if(pDevicePathAsString != NULL){ Print(L"Image device : %s\n",pDevicePathAsString); FreePool(pDevicePathAsString); } pDevicePathAsString = DevicePathToStr(pLoadedImage->FilePath); if(pDevicePathAsString != NULL) { Print(L"Image file : %s\n", DevicePathToStr (pLoadedImage->FilePath)); FreePool(pDevicePathAsString); } Print (L"Image Base : %X\n", pLoadedImage->ImageBase); Print (L"Image Size : %X\n", pLoadedImage->ImageSize); // // Open the volume for the device where the EFI OS Loader was loaded from. // status = BS->HandleProtocol (pLoadedImage->DeviceHandle, &FileSystemProtocol, (VOID*)&pVol ); if(EFI_ERROR(status)){ Print(L"Can not get a FileSystem handle for LoadedImage->DeviceHandle\n"); BS->Exit(ImageHandle,EFI_SUCCESS,0,NULL); } status = pVol->OpenVolume (pVol, &RootFs); if(EFI_ERROR(status)){ Print(L"Can not open the volume for the file system\n"); BS->Exit(ImageHandle,EFI_SUCCESS,0,NULL); } CurDir = RootFs; pDevicePathAsString = DevicePathToStr(pLoadedImage->FilePath); if (pDevicePathAsString!=NULL) { StrCpy(FileName,pDevicePathAsString); FreePool(pDevicePathAsString); } for(i=StrLen(FileName);i>0 && FileName[i]!='/';i--); if( FileName[i-1] == '\\' )i-- ; FileName[i] = 0; StrCat(FileName,L"\\boot.bin"); Print(FileName); status = CurDir->Open (CurDir, &FileHandle, FileName, EFI_FILE_MODE_READ, 0 ); if (EFI_ERROR(status)) { Print(L"Can not open the file %s\n",FileName); BS->Exit(ImageHandle,EFI_SUCCESS,0,NULL); } Print(L"Opened %s\n",FileName); Size = 0x10000; BS->AllocatePool(EfiRuntimeServicesCode, Size, (VOID*)&pOsKernelBuffer ); if (pOsKernelBuffer == NULL) { Print(L"Can not allocate a buffer for the file %s\n",FileName); BS->Exit(ImageHandle,EFI_SUCCESS,0,NULL); } Print(L"Pool allocated at addres %X\n",pOsKernelBuffer); status = FileHandle->Read(FileHandle, &Size, pOsKernelBuffer ); if (EFI_ERROR(status)) { Print(L"Can not read the file %s\n",FileName); BS->Exit(ImageHandle,EFI_SUCCESS,0,NULL); } Print(L"%X bytes of boot.bin read into memory at %X\n",Size,pOsKernelBuffer); /* int a; for (a = 0;a<0x2000;a++){ ((CHAR8*)(0x4400000))[a]=pOsKernelBuffer[a+0x2000]; } */ /* EFI_PHYSICAL_ADDRESS PhysBuff = (EFI_PHYSICAL_ADDRESS)(UINTN)0x4410000; status = BS->AllocatePages(AllocateMaxAddress,EfiLoaderCode,0x10,&PhysBuff); if(EFI_ERROR(status)){ Print(L"asd\n"); return status; } Print(L"Allocate Pages at addr %X\n",(UINTN)PhysBuff); */ /*int a; for(a=0;aFreePages(PhysBuff,0x10000); UINTN cookie; UINTN mapsize,descsize; UINT32 desver; UINTN no_entryes; EFI_MEMORY_DESCRIPTOR * MemMap,*MapEntry; MemMap = LibMemoryMap(&no_entryes,&cookie,&descsize,&desver); MapEntry = MemMap; UINTN index; for(index = 0;index < no_entryes;index++){ Print(L" %s %lX %lX %lX\n", OsLoaderMemoryTypeDesc[MapEntry->Type], MapEntry->PhysicalStart, MapEntry->PhysicalStart+LShiftU64(MapEntry->NumberOfPages,EFI_PAGE_SHIFT)-1, MapEntry->Attribute); MapEntry = NextMemoryDescriptor(MapEntry,descsize); } UINTN a = stor(pOsKernelBuffer); Print(L"stor = %X\n",a); // WriteVGAString(L"privet huyna"); /* status = BS->ExitBootServices(ImageHandle,cookie); if(EFI_ERROR(status)){ Print(L"asdf\n"); return EFI_SUCCESS; } */ /* asm volatile( "nop.i 0x00 ;;\n" "movl r15 = 0x4400000;;\n" "mov b6 = r15\n ;;" "br.call.sptk.many b0 = b6 ;;" ); */ /* asm volatile( "br.sptk.few %0" :: "b"((VOID*)PhysBuff + 0x2000) ); */ Print(L"returned\n"); // BS->FreePages(PhysBuff,0x10); // // Close OSKERNEL.BIN // status = FileHandle->Close(FileHandle); if (EFI_ERROR(status)) { Print(L"Can not close the file %s\n",FileName); BS->Exit(ImageHandle,EFI_SUCCESS,0,NULL); } // // Free the resources allocated from pool. // /* EFI_PHYSICAL_ADDRESS PhysAddr; status = BS->AllocatePages(AllocateAnyPages,EfiLoaderCode,(Size>>12)+1,&PhysAddr); if(EFI_ERROR(status)){ Print(L"Error 0\n"); if(status == EFI_OUT_OF_RESOURCES) Print(L"EFI_OUT_OF_RESOURCES\n"); if(status == EFI_INVALID_PARAMETER) Print(L"EFI_INVALID_PARAMETER\n"); if(status == EFI_NOT_FOUND) Print(L"EFI_NOT_FOUND\n"); return EFI_SUCCESS; } Print(L"PageAllocation of size = %X at addr = %X\n",Size,(UINTN)PhysAddr); VOID * point; point = (VOID*)(UINTN)PhysAddr; Print(L"point = %X\n",point); int a; for (a = 0;aFreePages(PhysAddr,(Size>>12)+1); */ FreePool(pOsKernelBuffer); return EFI_SUCCESS; } VOID WriteVGAString( CHAR16 *string ){ UINTN CursorRow = 10; UINTN CursorColumn = 10; volatile UINTN i; for(i=0;string[i]!=0; i++){ switch(string[i]){ case '\n': case '\r': break; default: *(UINT8*)(0xb8000+CursorRow*160+CursorColumn*2)=(UINT8)(string[i]); *(UINT8*)(0xb8000+CursorRow*160+CursorColumn*2)=0x0f; CursorColumn++; break; } if(CursorColumn >= 80){ CursorColumn = 0; CursorRow++; } } }