Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. ;*********************************************************************
  2. ;*                                                                   *
  3. ;*          UEFI library for fasm by bzt, Public Domain              *
  4. ;*                                                                   *
  5. ;*********************************************************************
  6.  
  7. ; EFI_MEMORY_TYPE
  8. EFI_RESERVED_MEMORY_TYPE        = 0
  9. EFI_LOADER_CODE                 = 1
  10. EFI_LOADER_DATA                 = 2
  11. EFI_BOOT_SERVICES_CODE          = 3
  12. EFI_BOOT_SERVICES_DATA          = 4
  13. EFI_RUNTIME_SERVICES_CODE       = 5
  14. EFI_RUNTIME_SERVICES_DATA       = 6
  15. EFI_CONVENTIONAL_MEMORY         = 7
  16. EFI_UNUSABLE_MEMORY             = 8
  17. EFI_ACPI_RECLAIM_MEMORY         = 9
  18. EFI_ACPI_MEMORY_NVS             = 10
  19. EFI_MEMORY_MAPPED_IO            = 11
  20. EFI_MEMORY_MAPPED_IO_PORT_SPACE = 12
  21. EFI_PAL_CODE                    = 13
  22. EFI_PERSISTENT_MEMORY           = 14
  23. EFI_MAX_MEMORY_TYPE             = 15
  24.  
  25. EFI_MEMORY_UC                   = 0x0000000000000001
  26. EFI_MEMORY_WC                   = 0x0000000000000002
  27. EFI_MEMORY_WT                   = 0x0000000000000004
  28. EFI_MEMORY_WB                   = 0x0000000000000008
  29. EFI_MEMORY_UCE                  = 0x0000000000000010
  30. EFI_MEMORY_WP                   = 0x0000000000001000
  31. EFI_MEMORY_RP                   = 0x0000000000002000
  32. EFI_MEMORY_XP                   = 0x0000000000004000
  33. EFI_MEMORY_NV                   = 0x0000000000008000
  34. EFI_MEMORY_MORE_RELIABLE        = 0x0000000000010000
  35. EFI_MEMORY_RO                   = 0x0000000000020000
  36. EFI_MEMORY_RUNTIME              = 0x8000000000000000
  37.  
  38. EFIERR                          = 0x8000000000000000
  39. EFI_SUCCESS                     = 0
  40. EFI_LOAD_ERROR                  = EFIERR or 1
  41. EFI_INVALID_PARAMETER           = EFIERR or 2
  42. EFI_UNSUPPORTED                 = EFIERR or 3
  43. EFI_BAD_BUFFER_SIZE             = EFIERR or 4
  44. EFI_BUFFER_TOO_SMALL            = EFIERR or 5
  45. EFI_NOT_READY                   = EFIERR or 6
  46. EFI_DEVICE_ERROR                = EFIERR or 7
  47. EFI_WRITE_PROTECTED             = EFIERR or 8
  48. EFI_OUT_OF_RESOURCES            = EFIERR or 9
  49. EFI_VOLUME_CORRUPTED            = EFIERR or 10
  50. EFI_VOLUME_FULL                 = EFIERR or 11
  51. EFI_NO_MEDIA                    = EFIERR or 12
  52. EFI_MEDIA_CHANGED               = EFIERR or 13
  53. EFI_NOT_FOUND                   = EFIERR or 14
  54. EFI_ACCESS_DENIED               = EFIERR or 15
  55. EFI_NO_RESPONSE                 = EFIERR or 16
  56. EFI_NO_MAPPING                  = EFIERR or 17
  57. EFI_TIMEOUT                     = EFIERR or 18
  58. EFI_NOT_STARTED                 = EFIERR or 19
  59. EFI_ALREADY_STARTED             = EFIERR or 20
  60. EFI_ABORTED                     = EFIERR or 21
  61. EFI_ICMP_ERROR                  = EFIERR or 22
  62. EFI_TFTP_ERROR                  = EFIERR or 23
  63. EFI_PROTOCOL_ERROR              = EFIERR or 24
  64.  
  65. EFI_SYSTEM_TABLE_SIGNATURE      equ     0x49,0x42,0x49,0x20,0x53,0x59,0x53,0x54
  66. struct EFI_TABLE_HEADER
  67.   Signature             dq ?
  68.   Revision              dd ?
  69.   HeaderSize            dd ?
  70.   CRC32                 dd ?
  71.   Reserved              dd ?
  72. ends
  73.  
  74. struct EFI_SYSTEM_TABLE
  75.   Hdr                   EFI_TABLE_HEADER
  76.   FirmwareVendor        dq ?
  77.   FirmwareRevision      dd ?
  78.                         dd ?
  79.   ConsoleInHandle       dq ?
  80.   ConIn                 dq ?
  81.   ConsoleOutHandle      dq ?
  82.   ConOut                dq ?
  83.   StandardErrorHandle   dq ?
  84.   StdErr                dq ?
  85.   RuntimeServices       dq ?
  86.   BootServices          dq ?
  87.   NumberOfTableEntries  dq ?
  88.   ConfigurationTable    dq ?
  89. ends
  90.  
  91. struct SIMPLE_TEXT_OUTPUT_INTERFACE
  92.   Reset                 dq ?
  93.   OutputString          dq ?
  94.   TestString            dq ?
  95.   QueryMode             dq ?
  96.   SetMode               dq ?
  97.   SetAttribute          dq ?
  98.   ClearScreen           dq ?
  99.   SetCursorPosition     dq ?
  100.   EnableCursor          dq ?
  101.   Mode                  dq ?
  102. ends
  103.  
  104.  
  105. struct SIMPLE_INPUT_INTERFACE
  106.   Reset                 dq ?
  107.   ReadKeyStroke         dq ?
  108.   WaitForKey            dq ?
  109. ends
  110.  
  111. struct EFI_BOOT_SERVICES_TABLE
  112.   Hdr                   EFI_TABLE_HEADER
  113.   RaisePriority         dq ?
  114.   RestorePriority       dq ?
  115.   AllocatePages         dq ?
  116.   FreePages             dq ?
  117.   GetMemoryMap          dq ?
  118.   AllocatePool          dq ?
  119.   FreePool              dq ?
  120.   CreateEvent           dq ?
  121.   SetTimer              dq ?
  122.   WaitForEvent          dq ?
  123.   SignalEvent           dq ?
  124.   CloseEvent            dq ?
  125.   CheckEvent            dq ?
  126.   InstallProtocolInterface dq ?
  127.   ReInstallProtocolInterface dq ?
  128.   UnInstallProtocolInterface dq ?
  129.   HandleProtocol        dq ?
  130.   Void                  dq ?
  131.   RegisterProtocolNotify dq ?
  132.   LocateHandle          dq ?
  133.   LocateDevicePath      dq ?
  134.   InstallConfigurationTable dq ?
  135.   ImageLoad             dq ?
  136.   ImageStart            dq ?
  137.   Exit                  dq ?
  138.   ImageUnLoad           dq ?
  139.   ExitBootServices      dq ?
  140.   GetNextMonotonicCount dq ?
  141.   Stall                 dq ?
  142.   SetWatchdogTimer      dq ?
  143.   ConnectController     dq ?
  144.   DisConnectController  dq ?
  145.   OpenProtocol          dq ?
  146.   CloseProtocol         dq ?
  147.   OpenProtocolInformation dq ?
  148.   ProtocolsPerHandle    dq ?
  149.   LocateHandleBuffer    dq ?
  150.   LocateProtocol        dq ?
  151.   InstallMultipleProtocolInterfaces dq ?
  152.   UnInstallMultipleProtocolInterfaces dq ?
  153.   CalculateCrc32        dq ?
  154.   CopyMem               dq ?
  155.   SetMem                dq ?
  156. ends
  157.  
  158. struct EFI_RUNTIME_SERVICES_TABLE
  159.   Hdr                   EFI_TABLE_HEADER
  160.   GetTime               dq ?
  161.   SetTime               dq ?
  162.   GetWakeUpTime         dq ?
  163.   SetWakeUpTime         dq ?
  164.   SetVirtualAddressMap  dq ?
  165.   ConvertPointer        dq ?
  166.   GetVariable           dq ?
  167.   GetNextVariableName   dq ?
  168.   SetVariable           dq ?
  169.   GetNextHighMonoCount  dq ?
  170.   ResetSystem           dq ?
  171. ends
  172.  
  173. struct EFI_TIME
  174.   Year                  dw ?
  175.   Month                 db ?
  176.   Day                   db ?
  177.   Hour                  db ?
  178.   Minute                db ?
  179.   Second                db ?
  180.   Pad1                  db ?
  181.   Nanosecond            dd ?
  182.   TimeZone              dw ?
  183.   Daylight              db ?
  184.   Pad2                  db ?
  185.   sizeof                db ?
  186. ends
  187.  
  188. EFI_LOADED_IMAGE_PROTOCOL_UUID  equ     0xA1,0x31,0x1b,0x5b,0x62,0x95,0xd2,0x11,0x8E,0x3F,0x00,0xA0,0xC9,0x69,0x72,0x3B
  189. struct EFI_LOADED_IMAGE_PROTOCOL
  190.   Revision              dd ?
  191.                         dd ?
  192.   ParentHandle          dq ?
  193.   SystemTable           dq ?
  194.   DeviceHandle          dq ?
  195.   FilePath              dq ?
  196.   Reserved              dq ?
  197.   LoadOptionsSize       dd ?
  198.                         dd ?
  199.   ImageBase             dq ?
  200.   ImageSize             dq ?
  201.   ImageCodeType         dd ?
  202.   ImageDataType         dd ?
  203.   UnLoad                dq ?
  204. ends
  205.  
  206. EFI_BLOCK_IO_PROTOCOL_UUID      equ     0x21,0x5b,0x4e,0x96,0x59,0x64,0xd2,0x11,0x8e,0x39,0x00,0xa0,0xc9,0x69,0x72,0x3b
  207. struct EFI_BLOCK_IO_PROTOCOL
  208.   Revision              dq ?
  209.   Media                 dq ?
  210.   Reset                 dq ?
  211.   ReadBlocks            dq ?
  212.   WriteBlocks           dq ?
  213.   FlushBlocks           dq ?
  214. ends
  215.  
  216. struct EFI_BLOCK_IO_MEDIA
  217.   MediaId               dd ?
  218.   RemovableMedia        db ?
  219.   MediaPresent          db ?
  220.   LogicalPartition      db ?
  221.   ReadOnly              db ?
  222.   WriteCaching          db ?
  223.                         rb 3
  224.   BlockSize             dd ?
  225.   IoAlign               dd ?
  226.   LastBlock             dq ?
  227. ends
  228.  
  229. EFI_GRAPHICS_OUTPUT_PROTOCOL_UUID equ   0xde,0xa9,0x42,0x90,0xdc,0x23,0x38,0x4a,0x96,0xfb,0x7a,0xde,0xd0,0x80,0x51,0x6a
  230. struct EFI_GRAPHICS_OUTPUT_PROTOCOL
  231.   QueryMode             dq ?
  232.   SetMode               dq ?
  233.   Blt                   dq ?
  234.   Mode                  dq ?
  235. ends
  236.  
  237. struct EFI_GRAPHICS_OUTPUT_PROTOCOL_MODE
  238.   MaxMode               dd ?
  239.   Mode                  dd ?
  240.   nfo                   dq ?
  241.   SizeOfInfo            dq ?
  242.   FrameBufferBase       dq ?
  243.   FrameBufferSize       dq ?
  244. ends
  245.  
  246. struct EFI_GRAPHICS_OUTPUT_MODE_INFORMATION
  247.   Version               dd ?
  248.   HorizontalResolution  dd ?
  249.   VerticalResolution    dd ?
  250.   PixelFormat           dd ?
  251.   RedMask               dd ?
  252.   GreenMask             dd ?
  253.   BlueMask              dd ?
  254.   Reserved              dd ?
  255.   PixelsPerScanLine     dd ?
  256. ends
  257.  
  258. ;---macros to make life easier---
  259. ;call it early, after entry point is the best
  260. macro InitializeLib
  261. {
  262.         clc
  263.         test    rdx, rdx
  264.         jz      .badout
  265.         cmp     dword[rdx], 'IBI '      ; 20494249h
  266.         jz      @f
  267.   .badout:
  268.         xor     ecx, ecx
  269.         xor     edx, edx
  270.         stc
  271.     @@:
  272.         mov     [efi_handler], rcx      ; ImageHandle
  273.         mov     [efi_ptr], rdx          ; pointer to SystemTable
  274. }
  275.  
  276. ;invoke an UEFI function
  277. macro uefi_call_wrapper interface,function,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10,arg11
  278. {
  279. numarg = 0
  280.  
  281. if ~ arg11 eq
  282.   numarg = numarg + 1
  283.   if ~ arg11 eq rdi
  284.         mov     rdi, arg11
  285.   end if
  286. end if
  287.  
  288. if ~ arg10 eq
  289.   numarg = numarg + 1
  290.   if ~ arg10 eq rsi
  291.         mov     rsi, arg10
  292.   end if
  293. end if
  294.  
  295. if ~ arg9 eq
  296.   numarg = numarg + 1
  297.   if ~ arg9 eq r14
  298.         mov     r14, arg9
  299.   end if
  300. end if
  301.  
  302. if ~ arg8 eq
  303.   numarg = numarg + 1
  304.   if ~ arg8 eq r13
  305.         mov     r13, arg8
  306.   end if
  307. end if
  308.  
  309. if ~ arg7 eq
  310.   numarg = numarg + 1
  311.   if ~ arg7 eq r12
  312.         mov     r12, arg7
  313.   end if
  314. end if
  315.  
  316. if ~ arg6 eq
  317.   numarg = numarg + 1
  318.   if ~ arg6 eq r11
  319.         mov     r11, arg6
  320.   end if
  321. end if
  322.  
  323. if ~ arg5 eq
  324.   numarg = numarg + 1
  325.   if ~ arg5 eq r10
  326.         mov     r10, arg5
  327.   end if
  328. end if
  329.  
  330. if ~ arg4 eq
  331.   numarg = numarg + 1
  332.   if ~ arg4 eq r9
  333.         mov     r9, arg4
  334.   end if
  335. end if
  336.  
  337. if ~ arg3 eq
  338.   numarg = numarg + 1
  339.   if ~ arg3 eq r8
  340.         mov     r8, arg3
  341.   end if
  342. end if
  343.  
  344. if ~ arg2 eq
  345.   numarg = numarg + 1
  346.   if ~ arg2 eq rdx
  347.         mov     rdx, arg2
  348.   end if
  349. end if
  350.  
  351. if ~ arg1 eq
  352.   numarg = numarg + 1
  353.   if ~ arg1 eq rcx
  354.     if ~ arg1 in <ConsoleInHandle,ConIn,ConsoleOutHandle,ConOut,StandardErrorHandle,StdErr,RuntimeServices,BootServices>
  355.         mov     rcx, arg1
  356.     end if
  357.   end if
  358. end if
  359.  
  360.         xor     rax, rax
  361.         mov     al, numarg
  362.  
  363. if interface in <ConsoleInHandle,ConIn,ConsoleOutHandle,ConOut,StandardErrorHandle,StdErr,RuntimeServices,BootServices>
  364.         mov     rbx, [efi_ptr]
  365.         mov     rbx, [rbx + EFI_SYSTEM_TABLE.#interface]
  366. else
  367.   if ~ interface eq rbx
  368.         mov     rbx, interface
  369.   end if
  370. end if
  371.  
  372. if arg1 in <ConsoleInHandle,ConIn,ConsoleOutHandle,ConOut,StandardErrorHandle,StdErr,RuntimeServices,BootServices>
  373.         mov     rcx, rbx
  374. end if
  375.  
  376. if defined SIMPLE_INPUT_INTERFACE.#function
  377.         mov     rbx, [rbx + SIMPLE_INPUT_INTERFACE.#function]
  378. else
  379.  if defined SIMPLE_TEXT_OUTPUT_INTERFACE.#function
  380.         mov     rbx, [rbx + SIMPLE_TEXT_OUTPUT_INTERFACE.#function]
  381.  else
  382.   if defined EFI_BOOT_SERVICES_TABLE.#function
  383.         mov     rbx, [rbx + EFI_BOOT_SERVICES_TABLE.#function]
  384.   else
  385.    if defined EFI_RUNTIME_SERVICES_TABLE.#function
  386.         mov     rbx, [rbx + EFI_RUNTIME_SERVICES_TABLE.#function]
  387.    else
  388.     if defined EFI_GRAPHICS_OUTPUT_PROTOCOL.#function
  389.         mov     rbx, [rbx + EFI_GRAPHICS_OUTPUT_PROTOCOL.#function]
  390.     else
  391.      if defined EFI_GRAPHICS_OUTPUT_PROTOCOL_MODE.#function
  392.         mov     rbx, [rbx + EFI_GRAPHICS_OUTPUT_PROTOCOL_MODE.#function]
  393.      else
  394.         mov     rbx, [rbx + function]
  395.      end if
  396.     end if
  397.    end if
  398.   end if
  399.  end if
  400. end if
  401.         call    uefifunc
  402. }
  403.  
  404. ;*********************************************************************
  405. ;*                       Library functions                           *
  406. ;*********************************************************************
  407.  
  408. section '.text' code executable readable
  409.  
  410. uefifunc:
  411.         ;save stack pointer
  412.         mov     qword [uefi_rsptmp], rsp
  413.         ;set up new aligned stack
  414.         and     esp, 0xFFFFFFF0
  415.         ;alignment check on arguments
  416.         bt      eax, 0
  417.         jnc     @f
  418.         push    rax
  419.         ;arguments
  420. @@:
  421.         cmp     al, 11
  422.         jb      @f
  423.         push    rdi
  424. @@:
  425.         cmp     al, 10
  426.         jb      @f
  427.         push    rsi
  428. @@:
  429.         cmp     al, 9
  430.         jb      @f
  431.         push    r14
  432. @@:
  433.         cmp     al, 8
  434.         jb      @f
  435.         push    r13
  436. @@:
  437.         cmp     al, 7
  438.         jb      @f
  439.         push    r12
  440. @@:
  441.         cmp     al, 6
  442.         jb      @f
  443.         push    r11
  444. @@:
  445.         cmp     al, 5
  446.         jb      @f
  447.         push    r10
  448. @@:
  449.         ;space for
  450.         ;r9
  451.         ;r8
  452.         ;rdx
  453.         ;rcx
  454.         sub     rsp, 4*8
  455.         ;call function
  456.         call    rbx
  457.         ;restore old stack
  458.         mov     rsp, qword [uefi_rsptmp]
  459.         ret
  460.  
  461. section '.data' data readable writeable
  462. efi_handler     dq 0
  463. efi_ptr         dq 0
  464. uefi_rsptmp     dq 0
  465.