Subversion Repositories Kolibri OS

Rev

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

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                              ;;
  3. ;; Copyright (C) KolibriOS team 2020. All rights reserved.      ;;
  4. ;; Distributed under terms of the GNU General Public License    ;;
  5. ;; Version 2, or (at your option) any later version.            ;;
  6. ;;                                                              ;;
  7. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  8.  
  9. format pe64 efi
  10. entry main
  11.  
  12. section '.text' code executable readable
  13.  
  14. include '../../struct.inc'
  15. include '../../macros.inc'
  16. include '../../const.inc'
  17.  
  18. purge DQ
  19. include 'uefi64.inc'
  20.  
  21. MEMORY_MAP_SIZE = 0x10000
  22. GOP_BUFFER_SIZE = 0x100
  23. LIP_BUFFER_SIZE = 0x100
  24. FILE_BUFFER_SIZE = 0x1000
  25.  
  26. KERNEL_TRAMPOLINE = 0x8f80      ; just before BOOT_LO
  27. KERNEL_BASE  =  0x10000
  28. RAMDISK_BASE = 0x100000
  29. MAX_FILE_SIZE = 0x10000000
  30.  
  31. CODE_32_SELECTOR = 8
  32. DATA_32_SELECTOR = 16
  33. CODE_64_SELECTOR = 24
  34.  
  35. ; linux/arch/x86/include/uapi/asm/e820.h
  36. E820_RAM       = 1
  37. E820_RESERVED  = 2
  38. E820_ACPI      = 3
  39. E820_NVS       = 4
  40. E820_UNUSABLE  = 5
  41. E820_PMEM      = 7
  42.  
  43. load_file:
  44. virtual at rsp+8
  45.   .root   dq ?
  46.   .name   dq ?
  47.   .buffer dq ?
  48.   .size   dq ?
  49.   .fatal  dq ?
  50. end virtual
  51.         eficall [.root], EFI_FILE_PROTOCOL.Open, [.root], file_handle, \
  52.                 [.name], EFI_FILE_MODE_READ, 0
  53.         test    eax, eax
  54.         jz      @f
  55.         xor     eax, eax
  56.         cmp     [.fatal], 1
  57.         jnz     .done
  58.         mov     rbx, [efi_table]
  59.         mov     rbx, [rbx+EFI_SYSTEM_TABLE.ConOut]
  60.         eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString, rbx, \
  61.                 msg_error_open_file
  62.         mov     rbx, [efi_table]
  63.         mov     rbx, [rbx+EFI_SYSTEM_TABLE.ConOut]
  64.         eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString, rbx, \
  65.                 [.name]
  66.         jmp     $
  67. @@:
  68.  
  69.         lea     rax, [.size]
  70.         eficall [file_handle], EFI_FILE_PROTOCOL.Read, [file_handle], rax, \
  71.                 [.buffer]
  72.         eficall [file_handle], EFI_FILE_PROTOCOL.Close, [file_handle]
  73.         mov     rax, [.size]
  74. .done:
  75.         push    rax
  76.         call    clearbuf
  77.         mov     rdi, msg
  78.         call    num2dec
  79.         push    rbx
  80.         mov     rbx, [efi_table]
  81.         mov     rbx, [rbx+EFI_SYSTEM_TABLE.ConOut]
  82.         eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString, rbx, \
  83.                 msg_file_size
  84.         pop     rbx
  85.         push    rbx
  86.         mov     rbx, [efi_table]
  87.         mov     rbx, [rbx+EFI_SYSTEM_TABLE.ConOut]
  88.         eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString, rbx, msg
  89.         pop     rbx
  90.         pop     rax
  91.         ret     8*5
  92.  
  93. skip_whitespace:
  94. .next_char:
  95.         cmp     byte[rsi], 0
  96.         jz      .done
  97.         cmp     byte[rsi], 0x20 ; ' '
  98.         jz      .whitespace
  99.         cmp     byte[rsi], 9    ; '\t'
  100.         jz      .whitespace
  101.         jmp     .done
  102. .whitespace:
  103.         inc     rsi
  104.         jmp     .next_char
  105. .done:
  106.         ret
  107.  
  108. skip_until_newline:
  109. .next_char:
  110.         cmp     byte[rsi], 0
  111.         jz      .done
  112.         cmp     byte[rsi], 0xd  ; '\r'
  113.         jz      .done
  114.         cmp     byte[rsi], 0xa  ; '\n'
  115.         jz      .done
  116.         inc     rsi
  117.         jmp     .next_char
  118. .done:
  119.         ret
  120.  
  121. skip_newline:
  122. .next_char:
  123.         cmp     byte[rsi], 0xd  ; '\r'
  124.         jz      .newline
  125.         cmp     byte[rsi], 0xa  ; '\n'
  126.         jz      .newline
  127.         jmp     .done
  128. .newline:
  129.         inc     rsi
  130.         jmp     .next_char
  131. .done:
  132.         ret
  133.  
  134. skip_line:
  135.         call    skip_until_newline
  136.         call    skip_newline
  137.         ret
  138.  
  139. dec2bin:
  140.         mov     edx, 0
  141. .next_char:
  142.         movzx   eax, byte[rsi]
  143.         test    eax, eax
  144.         jz      .done
  145.         sub     eax, '0'
  146.         jb      .done
  147.         cmp     eax, 9
  148.         ja      .done
  149.         inc     rsi
  150.         imul    edx, 10
  151.         add     edx, eax
  152.         jmp     .next_char
  153. .done:
  154.         mov     eax, edx
  155.         ret
  156.  
  157. parse_option:
  158.         mov     rbx, config_options-3*8
  159. .try_next_option:
  160.         add     rbx, 3*8
  161.         mov     rdi, rsi
  162.         mov     rdx, [rbx]      ; option name
  163.         test    rdx, rdx
  164.         jz      .done
  165. .next_char:
  166.         cmp     byte[rdx], 0
  167.         jnz     @f
  168.         cmp     byte[rdi], '='
  169.         jz      .opt_name_ok
  170. @@:
  171.         cmp     byte[rdi], 0
  172.         jz      .done
  173.         movzx   eax, byte[rdi]
  174.         cmp     [rdx], al
  175.         jnz     .try_next_option
  176.         inc     rdi
  177.         inc     rdx
  178.         jmp     .next_char
  179. .opt_name_ok:
  180.         inc     rdi
  181.         mov     rsi, rdi
  182.         call    qword[rbx+8]
  183. .done:
  184.         ret
  185.  
  186. parse_line:
  187. .next_line:
  188.         cmp     byte[rsi], 0
  189.         jz      .done
  190.         cmp     byte[rsi], 0xd  ; '\r'
  191.         jz      .skip
  192.         cmp     byte[rsi], 0xa  ; '\n'
  193.         jz      .skip
  194.         cmp     byte[rsi], '#'
  195.         jz      .skip
  196.         call    parse_option
  197.         call    skip_line
  198.         jmp     .next_line
  199. .skip:
  200.         call    skip_line
  201.         jmp     .next_line
  202. .done:
  203.         ret
  204.  
  205. cfg_opt_func_resolution:
  206.         call    dec2bin
  207.         xor     edx, edx
  208.         mov     [rdx+BOOT_LO.x_res], ax
  209.         cmp     byte[rsi], 'x'
  210.         jz      @f
  211.         cmp     byte[rsi], '*'
  212.         jz      @f
  213.         jmp     .done
  214. @@:
  215.         inc     rsi
  216.         call    dec2bin
  217.         xor     edx, edx
  218.         mov     [rdx+BOOT_LO.y_res], ax
  219.         mov     [cfg_opt_used_resolution], 1
  220. .done:
  221.         ret
  222.  
  223. cfg_opt_func_acpi:
  224.         call    dec2bin
  225.         mov     [cfg_opt_used_acpi], 1
  226.         mov     [cfg_opt_value_acpi], al
  227.         ret
  228.  
  229. cfg_opt_func_debug_print:
  230.         call    dec2bin
  231.         mov     [cfg_opt_used_debug_print], 1
  232.         mov     [cfg_opt_value_debug_print], al
  233.         ret
  234.  
  235. cfg_opt_func_launcher_start:
  236.         call    dec2bin
  237.         mov     [cfg_opt_used_launcher_start], 1
  238.         mov     [cfg_opt_value_launcher_start], al
  239.         ret
  240.  
  241. cfg_opt_func_mtrr:
  242.         call    dec2bin
  243.         mov     [cfg_opt_used_mtrr], 1
  244.         mov     [cfg_opt_value_mtrr], al
  245.         ret
  246.  
  247. cfg_opt_func_ask_params:
  248.         call    dec2bin
  249.         mov     [cfg_opt_used_ask_params], 1
  250.         mov     [cfg_opt_value_ask_params], al
  251.         ret
  252.  
  253. cfg_opt_func_imgfrom:
  254.         call    dec2bin
  255.         mov     [cfg_opt_used_imgfrom], 1
  256.         mov     [cfg_opt_value_imgfrom], al
  257.         ret
  258.  
  259. cfg_opt_func_syspath:
  260.         mov     rdi, cfg_opt_value_syspath
  261. .next_char:
  262.         movzx   eax, byte[rsi]
  263.         cmp     al, 0xd ; \r
  264.         jz      .done
  265.         cmp     al, 0xa ; \n
  266.         jz      .done
  267.         inc     rsi
  268.         stosb
  269.         jmp     .next_char
  270. .done:
  271.         mov     byte[rdi], 0
  272.         ret
  273.  
  274. parse_config:
  275. virtual at rsp+8
  276.   .buffer      dq ?
  277. end virtual
  278. ;        mov     rsi, [.buffer]
  279.         push    rbx
  280.         mov     rbx, [efi_table]
  281.         mov     rbx, [rbx+EFI_SYSTEM_TABLE.ConOut]
  282.         eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString, rbx, \
  283.                 msg_parsing_config
  284.         pop     rbx
  285.         mov     rsi, KERNEL_BASE
  286. .next_line:
  287.         call    parse_line
  288.         cmp     byte[rsi], 0
  289.         jnz     .next_line
  290.         ret     1*8
  291.  
  292. read_options_from_config:
  293.         mov     rbx, [efi_table]
  294.         mov     rbx, [rbx+EFI_SYSTEM_TABLE.BootServices]
  295.         eficall rbx, EFI_BOOT_SERVICES.HandleProtocol, [efi_handle], lipuuid, \
  296.                 lip_interface
  297.         test    eax, eax
  298.         jz      @f
  299.         push    rbx
  300.         mov     rbx, [efi_table]
  301.         mov     rbx, [rbx+EFI_SYSTEM_TABLE.ConOut]
  302.         eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString, rbx, \
  303.                 msg_error_efi_lip_handle
  304.         pop     rbx
  305.         jmp     $
  306. @@:
  307.         mov     rax, [lip_interface]
  308.  
  309.         mov     rbx, [efi_table]
  310.         mov     rbx, [rbx+EFI_SYSTEM_TABLE.BootServices]
  311.         eficall rbx, EFI_BOOT_SERVICES.HandleProtocol, \
  312.                 [rax+EFI_LOADED_IMAGE_PROTOCOL.DeviceHandle], sfspguid, \
  313.                 sfsp_interface
  314.         test    eax, eax
  315.         jz      @f
  316.         push    rbx
  317.         mov     rbx, [efi_table]
  318.         mov     rbx, [rbx+EFI_SYSTEM_TABLE.ConOut]
  319.         eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString, rbx, \
  320.                 msg_error_lip_dev_sfsp
  321.         pop     rbx
  322.         jmp     $
  323. @@:
  324.         eficall [sfsp_interface], EFI_SIMPLE_FILE_SYSTEM_PROTOCOL.OpenVolume, \
  325.                 [sfsp_interface], esp_root
  326.         test    eax, eax
  327.         jz      @f
  328.         push    rbx
  329.         mov     rbx, [efi_table]
  330.         mov     rbx, [rbx+EFI_SYSTEM_TABLE.ConOut]
  331.         eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString, rbx, \
  332.                 msg_error_sfsp_openvolume
  333.         pop     rbx
  334.         jmp     $
  335. @@:
  336.         push    0 ; not fatal, i.e. it's ok to not find this file
  337.         push    FILE_BUFFER_SIZE
  338.         push    KERNEL_BASE
  339. ;        push    file_name
  340.         mov     rax, file_name
  341.         push    rax
  342.         push    [esp_root]
  343.         call    load_file
  344.  
  345.         test    eax, eax
  346.         jz      @f
  347.         push    KERNEL_BASE
  348.         call    parse_config
  349. @@:
  350.  
  351. .error:
  352.         ret
  353.  
  354. print_vmode:
  355.         push    rax rbx rcx rdx rsi rdi
  356.         mov     rbx, rcx
  357.         call    clearbuf
  358.         mov     eax, [rbx+EFI_GRAPHICS_OUTPUT_MODE_INFORMATION.HorizontalResolution]
  359.         mov     rdi, msg
  360.         call    num2dec
  361.         mov     eax, [rbx+EFI_GRAPHICS_OUTPUT_MODE_INFORMATION.VerticalResolution]
  362.         mov     rdi, msg+8*2
  363.         call    num2dec
  364.  
  365.         mov     eax, [rbx+EFI_GRAPHICS_OUTPUT_MODE_INFORMATION.PixelFormat]
  366.         mov     rdi, msg+16*2
  367.         call    num2dec
  368.  
  369.         mov     eax, [rbx+EFI_GRAPHICS_OUTPUT_MODE_INFORMATION.PixelsPerScanLine]
  370.         mov     rdi, msg+24*2
  371.         call    num2dec
  372.         mov     rbx, [efi_table]
  373.         mov     rbx, [rbx+EFI_SYSTEM_TABLE.ConOut]
  374.         eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString, rbx, msg
  375.         pop     rdi rsi rdx rcx rbx rax
  376.         ret
  377.  
  378. find_vmode_index_by_resolution:
  379.         mov     [cfg_opt_used_resolution], 1
  380.         mov     [cfg_opt_value_vmode], 0
  381. .next_mode:
  382.         movzx   eax, [cfg_opt_value_vmode]
  383.         eficall [gop_interface], EFI_GRAPHICS_OUTPUT_PROTOCOL.QueryMode, \
  384.                 [gop_interface], rax, gop_info_size, gop_info
  385.         cmp     rax, EFI_SUCCESS
  386.         jnz     .error
  387.         mov     rcx, [gop_info]
  388.         call    print_vmode
  389.         ; PixelBlueGreenRedReserved8BitPerColor
  390.         cmp     [rcx+EFI_GRAPHICS_OUTPUT_MODE_INFORMATION.PixelFormat], 1
  391.         jnz     .skip_mode
  392.         xor     edx, edx
  393.         mov     eax, [rcx+EFI_GRAPHICS_OUTPUT_MODE_INFORMATION.HorizontalResolution]
  394.         cmp     ax, [rdx+BOOT_LO.x_res]
  395.         jnz     .skip_mode
  396.         mov     eax, [rcx+EFI_GRAPHICS_OUTPUT_MODE_INFORMATION.VerticalResolution]
  397.         cmp     ax, [rdx+BOOT_LO.y_res]
  398.         jnz     .skip_mode
  399.         jmp     .done
  400. .skip_mode:
  401.         inc     [cfg_opt_value_vmode]
  402.         movzx   eax, [cfg_opt_value_vmode]
  403.         mov     rcx, [gop_interface]
  404.         mov     rdx, [rcx+EFI_GRAPHICS_OUTPUT_PROTOCOL.Mode]
  405.         cmp     eax, [rdx+EFI_GRAPHICS_OUTPUT_PROTOCOL_MODE.MaxMode]
  406.         jnz     .next_mode
  407.         mov     [cfg_opt_used_resolution], 0
  408.         mov     [cfg_opt_value_ask_params], 1
  409.  
  410.         mov     rbx, [efi_table]
  411.         mov     rbx, [rbx+EFI_SYSTEM_TABLE.ConOut]
  412.         eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString, rbx, \
  413.                 msg_error_no_such_vmode
  414.         mov     rbx, [efi_table]
  415.         mov     rbx, [rbx+EFI_SYSTEM_TABLE.ConOut]
  416.         eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString, rbx, \
  417.                 msg_error
  418.         jmp     $
  419. .error:
  420. .done:
  421.         ret
  422.  
  423. ask_for_params:
  424.         ret
  425.         mov     rbx, [efi_table]
  426.         mov     rbx, [rbx+EFI_SYSTEM_TABLE.BootServices]
  427.         eficall rbx, EFI_BOOT_SERVICES.HandleProtocol, [rax], gopuuid, \
  428.                 msg_ask_for_params
  429.         jmp     $
  430.  
  431.         xor     ebx, ebx
  432. .next_mode:
  433.         call    clearbuf
  434.         mov     eax, ebx
  435.         lea     rdi, [msg]
  436.         call    num2dec
  437.  
  438.         push    rbx
  439.         eficall [gop_interface], EFI_GRAPHICS_OUTPUT_PROTOCOL.QueryMode, \
  440.                 [gop_interface], rbx, gop_info_size, gop_info
  441.         cmp     rax, EFI_SUCCESS
  442.         jnz     .error
  443.         mov     rcx, [gop_info]
  444.         ; PixelBlueGreenRedReserved8BitPerColor
  445.         cmp     [rcx+EFI_GRAPHICS_OUTPUT_MODE_INFORMATION.PixelFormat], 1
  446.         jnz     .skip
  447.         mov     eax, [rcx+EFI_GRAPHICS_OUTPUT_MODE_INFORMATION.HorizontalResolution]
  448.         lea     rdi, [msg+4*2]
  449.         call    num2dec
  450.         mov     eax, [rcx+EFI_GRAPHICS_OUTPUT_MODE_INFORMATION.VerticalResolution]
  451.         lea     rdi, [msg+9*2]
  452.         call    num2dec
  453. ;        mov     eax, [rcx+EFI_GRAPHICS_OUTPUT_MODE_INFORMATION.PixelsPerScanLine]
  454. ;        lea     rdi, [msg+14*2]
  455. ;        call    num2dec
  456. .skip:
  457.         mov     rbx, [efi_table]
  458.         mov     rbx, [rbx+EFI_SYSTEM_TABLE.ConOut]
  459.         eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString, rbx, msg
  460.         cmp     rax, EFI_SUCCESS
  461.         jnz     .error
  462.  
  463.         pop     rbx
  464.         inc     rbx
  465.         mov     rcx, [gop_interface]
  466.         mov     rdx, [rcx+EFI_GRAPHICS_OUTPUT_PROTOCOL.Mode]
  467.         cmp     ebx, [rdx+EFI_GRAPHICS_OUTPUT_PROTOCOL_MODE.MaxMode]
  468.         jnz     .next_mode
  469.  
  470.  
  471.         mov     rbx, [efi_table]
  472.         mov     rbx, [rbx+EFI_SYSTEM_TABLE.ConIn]
  473.         eficall rbx, SIMPLE_INPUT_INTERFACE.Reset, rbx, 1
  474.         cmp     rax, EFI_SUCCESS
  475.         jnz     .error
  476.         xor     ecx, ecx
  477.     @@:
  478.         push    rcx
  479.         mov     rbx, [efi_table]
  480.         mov     rbx, [rbx+EFI_SYSTEM_TABLE.ConIn]
  481.         eficall rbx, SIMPLE_INPUT_INTERFACE.ReadKeyStroke, rbx, msg
  482.         pop     rcx
  483.         mov     rdx, EFI_DEVICE_ERROR
  484.         cmp     rax, rdx
  485.         jz      .error
  486.         mov     rdx, EFI_NOT_READY
  487.         cmp     rax, rdx
  488.         jz      @b
  489. ;        cmp     rax, EFI_SUCCESS
  490.         movzx   eax, word[msg+2]
  491. ;jmp .key_done
  492.         cmp     al, 0x0D
  493.         jz      .key_done
  494.         imul    ecx, 10
  495.         sub     eax, '0'
  496.         add     ecx, eax
  497.         jmp     @b
  498. .key_done:
  499.         mov     [cfg_opt_value_vmode], cl
  500. .error:
  501. .done:
  502.         ret
  503.  
  504. main:
  505.         mov     [efi_handle], rcx
  506.         mov     [efi_table], rdx
  507.  
  508.         mov     rbx, [efi_table]
  509.         mov     rbx, [rbx+EFI_SYSTEM_TABLE.ConOut]
  510.         eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.Reset, rbx, 1
  511.         test    eax, eax
  512.         jz      @f
  513.         jmp     $       ; what can I do here?
  514. @@:
  515.         mov     rbx, [efi_table]
  516.         mov     rbx, [rbx+EFI_SYSTEM_TABLE.ConOut]
  517.         eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString, rbx, \
  518.                 msg_u4k_loaded
  519.  
  520.         mov     rbx, [efi_table]
  521.         mov     rbx, [rbx+EFI_SYSTEM_TABLE.ConOut]
  522.         eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString, rbx, \
  523.                 msg_read_options
  524.         call    read_options_from_config
  525.  
  526.         ; read kernel file
  527.         mov     rbx, [efi_table]
  528.         mov     rbx, [rbx+EFI_SYSTEM_TABLE.ConOut]
  529.         eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString, rbx, \
  530.                 msg_load_kernel
  531.         push    1       ; fatal
  532.         push    MAX_FILE_SIZE
  533.         push    KERNEL_BASE
  534. ;        push    kernel_name
  535.         mov     rax, kernel_name
  536.         push    rax
  537.         push    [esp_root]
  538.         call    load_file
  539.  
  540.         ; read ramdisk image
  541.         mov     rbx, [efi_table]
  542.         mov     rbx, [rbx+EFI_SYSTEM_TABLE.ConOut]
  543.         eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString, rbx, \
  544.                 msg_load_ramdisk
  545.         push    1       ; fatal
  546.         push    MAX_FILE_SIZE
  547.         push    RAMDISK_BASE
  548. ;        push    ramdisk_name
  549.         mov     rax, ramdisk_name
  550.         push    rax
  551.         push    [esp_root]
  552.         call    load_file
  553.  
  554.         ; alloc buffer for devices.dat
  555.         mov     rbx, [efi_table]
  556.         mov     rbx, [rbx+EFI_SYSTEM_TABLE.ConOut]
  557.         eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString, rbx, \
  558.                 msg_alloc_devicesdat
  559.         mov     rbx, [efi_table]
  560.         mov     rbx, [rbx+EFI_SYSTEM_TABLE.BootServices]
  561.         eficall rbx, EFI_BOOT_SERVICES.AllocatePages, \
  562.                 EFI_ALLOCATE_MAX_ADDRESS, EFI_RESERVED_MEMORY_TYPE, 1, \
  563.                 devicesdat_data
  564.         cmp     eax, EFI_SUCCESS
  565.         jnz     .error
  566.  
  567.         ; read devices.dat
  568.         mov     rbx, [efi_table]
  569.         mov     rbx, [rbx+EFI_SYSTEM_TABLE.ConOut]
  570.         eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString, rbx, \
  571.                 msg_load_devicesdat
  572.  
  573.         push    0 ; not fatal
  574.         push    [devicesdat_size]
  575.         push    [devicesdat_data]
  576. ;        push    devicesdat_name
  577.         mov     rax, devicesdat_name
  578.         push    rax
  579.         push    [esp_root]
  580.         call    load_file
  581.         mov     [devicesdat_size], rax
  582.  
  583.         mov     rbx, [efi_table]
  584.         mov     rbx, [rbx+EFI_SYSTEM_TABLE.ConOut]
  585.         eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString, rbx, \
  586.                 msg_locate_gop_handlers
  587.  
  588.         mov     rbx, [efi_table]
  589.         mov     rbx, [rbx+EFI_SYSTEM_TABLE.BootServices]
  590.         eficall rbx, EFI_BOOT_SERVICES.LocateHandle, \
  591.                 EFI_LOCATE_SEARCH_TYPE.ByProtocol, gopuuid, 0, \
  592.                 gop_buffer_size, gop_buffer
  593.         mov     [status], rax
  594.  
  595.         mov     rbx, [efi_table]
  596.         mov     rbx, [rbx+EFI_SYSTEM_TABLE.ConOut]
  597.         eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString, rbx, \
  598.                 msg_gop_buffer_size
  599.         call    clearbuf
  600.         mov     rax, [gop_buffer_size]
  601.         mov     rdi, msg
  602.         call    num2hex
  603.         mov     rbx, [efi_table]
  604.         mov     rbx, [rbx+EFI_SYSTEM_TABLE.ConOut]
  605.         eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString, rbx, msg
  606.  
  607.         mov     rax, [status]
  608.         test    eax, eax
  609.         jz      @f
  610.         call    clearbuf
  611.         mov     rdi, msg
  612.         call    num2hex
  613.         mov     rbx, [efi_table]
  614.         mov     rbx, [rbx+EFI_SYSTEM_TABLE.ConOut]
  615.         eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString, rbx, \
  616.                 msg_error
  617.         mov     rbx, [efi_table]
  618.         mov     rbx, [rbx+EFI_SYSTEM_TABLE.ConOut]
  619.         eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString, rbx, msg
  620.         jmp     $
  621. @@:
  622.  
  623.         mov     rbx, [efi_table]
  624.         mov     rbx, [rbx+EFI_SYSTEM_TABLE.ConOut]
  625.         eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString, rbx, \
  626.                 msg_look_for_gop_handler
  627.  
  628.         mov     rbx, gop_buffer
  629. .next_gop_handle:
  630.         mov     rax, rbx
  631.         mov     rcx, gop_buffer
  632.         sub     rax, rcx
  633.         cmp     rax, [gop_buffer_size]
  634.         jb      @f
  635.         push    rbx
  636.         mov     rbx, [efi_table]
  637.         mov     rbx, [rbx+EFI_SYSTEM_TABLE.ConOut]
  638.         eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString, rbx, \
  639.                 msg_error_out_of_handlers
  640.         pop     rbx
  641.         push    rbx
  642.         mov     rbx, [efi_table]
  643.         mov     rbx, [rbx+EFI_SYSTEM_TABLE.ConOut]
  644.         eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString, rbx, msg_error
  645.         pop     rbx
  646.         jmp     $
  647. @@:
  648.         push    rbx
  649.         mov     rbx, [efi_table]
  650.         mov     rbx, [rbx+EFI_SYSTEM_TABLE.ConOut]
  651.         eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString, rbx, \
  652.                 msg_query_handler
  653.         pop     rbx
  654.  
  655.         mov     rax, rbx
  656.         push    rbx
  657.         mov     rbx, [efi_table]
  658.         mov     rbx, [rbx+EFI_SYSTEM_TABLE.BootServices]
  659.         eficall rbx, EFI_BOOT_SERVICES.HandleProtocol, [rax], gopuuid, \
  660.                 gop_interface
  661.         pop     rbx
  662. ;mov rax, 0x8000_0000_0000_0003
  663.         test    eax, eax
  664.         jz      @f
  665.         call    clearbuf
  666.         mov     rdi, msg
  667.         call    num2hex
  668.         push    rbx
  669.         mov     rbx, [efi_table]
  670.         mov     rbx, [rbx+EFI_SYSTEM_TABLE.ConOut]
  671.         eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString, rbx, msg
  672.         pop     rbx
  673.  
  674.         add     rbx, 8
  675.         jmp     .next_gop_handle
  676. @@:
  677.  
  678.         call    find_rsdp
  679.  
  680.         mov     rbx, [efi_table]
  681.         mov     rbx, [rbx+EFI_SYSTEM_TABLE.ConOut]
  682.         eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString, rbx, \
  683.                 msg_acpi_tables_done
  684.  
  685.         cmp     [cfg_opt_used_resolution], 0
  686.         jz      .not_used_resolution
  687.         mov     rbx, [efi_table]
  688.         mov     rbx, [rbx+EFI_SYSTEM_TABLE.ConOut]
  689.         eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString, rbx, \
  690.                 msg_opt_resolution
  691.         call    clearbuf
  692.         xor     edx, edx
  693.         movzx   eax, [rdx+BOOT_LO.x_res]
  694.         mov     rdi, msg
  695.         call    num2dec
  696.         xor     edx, edx
  697.         movzx   eax, [rdx+BOOT_LO.y_res]
  698.         mov     rdi, msg+8*2
  699.         call    num2dec
  700.         mov     rbx, [efi_table]
  701.         mov     rbx, [rbx+EFI_SYSTEM_TABLE.ConOut]
  702.         eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString, rbx, msg
  703.         call    find_vmode_index_by_resolution
  704. .not_used_resolution:
  705.         cmp     [cfg_opt_used_debug_print], 0
  706.         jz      .not_used_debug_print
  707.         movzx   eax, [cfg_opt_value_debug_print]
  708.         xor     edx, edx
  709.         mov     [rdx+BOOT_LO.debug_print], al
  710. .not_used_debug_print:
  711.  
  712.         cmp     [cfg_opt_value_ask_params], 0
  713.         jz      @f
  714.         call    ask_for_params
  715. @@:
  716.  
  717.         movzx   ecx, [cfg_opt_value_vmode]
  718.         eficall [gop_interface], EFI_GRAPHICS_OUTPUT_PROTOCOL.SetMode, \
  719.                 [gop_interface], rcx
  720.         test    eax, eax
  721.         jz      @f
  722.         call    clearbuf
  723.         mov     rdi, msg
  724.         call    num2hex
  725.         mov     rbx, [efi_table]
  726.         mov     rbx, [rbx+EFI_SYSTEM_TABLE.ConOut]
  727.         eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString, rbx, msg
  728.         mov     rbx, [efi_table]
  729.         mov     rbx, [rbx+EFI_SYSTEM_TABLE.ConOut]
  730.         eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString, rbx, \
  731.                 msg_error
  732.         jmp     $
  733. @@:
  734.  
  735.         mov     rcx, [gop_interface]
  736.         mov     rdx, [rcx+EFI_GRAPHICS_OUTPUT_PROTOCOL.Mode]
  737.         mov     rdi, [rdx+EFI_GRAPHICS_OUTPUT_PROTOCOL_MODE.FrameBufferBase]
  738.         mov     [fb_base], rdi
  739.  
  740.         mov     ebx, [rdx+EFI_GRAPHICS_OUTPUT_PROTOCOL_MODE.Mode]
  741.         eficall [gop_interface], EFI_GRAPHICS_OUTPUT_PROTOCOL.QueryMode, \
  742.                 [gop_interface], rbx, gop_info_size, gop_info
  743.         test    eax, eax
  744.         jz      @f
  745.         jmp     .error
  746. @@:
  747.         mov     rcx, [gop_info]
  748.         mov     eax, [rcx+EFI_GRAPHICS_OUTPUT_MODE_INFORMATION.HorizontalResolution]
  749.         xor     rdx, rdx
  750.         mov     [rdx+BOOT_LO.x_res], ax
  751.         mov     eax, [rcx+EFI_GRAPHICS_OUTPUT_MODE_INFORMATION.VerticalResolution]
  752.         mov     [rdx+BOOT_LO.y_res], ax
  753.         mov     eax, [rcx+EFI_GRAPHICS_OUTPUT_MODE_INFORMATION.PixelsPerScanLine]
  754.         shl     eax, 2
  755.         mov     [rdx+BOOT_LO.pitch], ax
  756.  
  757.         mov     byte[rdx+BOOT_LO.pci_data+0], 1    ; PCI access mechanism
  758.         mov     byte[rdx+BOOT_LO.pci_data+1], 8    ; last bus, don't know how to count them
  759.         mov     byte[rdx+BOOT_LO.pci_data+2], 0x10 ; PCI version
  760.         mov     byte[rdx+BOOT_LO.pci_data+3], 0x02
  761.         mov     dword[rdx+BOOT_LO.pci_data+4], 0xe3
  762.  
  763.         ; kernel
  764. ;        eficall BootServices, AllocatePages, EFI_RESERVED_MEMORY_TYPE, \
  765. ;                450000/0x1000, EFI_ALLOCATE_ADDRESS
  766.  
  767.         ; ramdisk
  768. ;        eficall BootServices, AllocatePages, EFI_RESERVED_MEMORY_TYPE, \
  769. ;                2880*512/0x1000, EFI_ALLOCATE_ADDRESS
  770.  
  771.         call    calc_memmap
  772. ;        call    dump_memmap
  773.  
  774.         mov     rbx, [efi_table]
  775.         mov     rbx, [rbx+EFI_SYSTEM_TABLE.BootServices]
  776.         eficall rbx, EFI_BOOT_SERVICES.ExitBootServices, [efi_handle], \
  777.                 [memory_map_key]
  778.         call    halt_on_error
  779.  
  780.         cli
  781.  
  782.         xor     edx, edx
  783.         xor     esi, esi
  784.         mov     [esi+BOOT_LO.bpp], 32
  785.         mov     [esi+BOOT_LO.vesa_mode], dx
  786.         mov     [esi+BOOT_LO.bank_switch], edx
  787.         mov     rdi, [fb_base]
  788.         mov     [esi+BOOT_LO.lfb], edi
  789.  
  790.         movzx   eax, [cfg_opt_value_mtrr]
  791.         mov     [esi+BOOT_LO.mtrr], al
  792.  
  793.         movzx   eax, [cfg_opt_value_launcher_start]
  794.         mov     [esi+BOOT_LO.launcher_start], al
  795.  
  796.         movzx   eax, [cfg_opt_value_debug_print]
  797.         mov     [esi+BOOT_LO.debug_print], al
  798.  
  799.         mov     [esi+BOOT_LO.dma], dl
  800. ;        mov     qword[esi+BOOT_LO.pci_data], 0
  801.         mov     [esi+BOOT_LO.apm_entry], edx
  802.         mov     [esi+BOOT_LO.apm_version], dx
  803.         mov     [esi+BOOT_LO.apm_flags], dx
  804.         mov     [esi+BOOT_LO.apm_code_32], dx
  805.         mov     [esi+BOOT_LO.apm_code_16], dx
  806.         mov     [esi+BOOT_LO.apm_data_16], dx
  807.         mov     [esi+BOOT_LO.bios_hd_cnt], dl
  808.  
  809.         movzx   eax, [cfg_opt_value_imgfrom]
  810.         mov     [esi+BOOT_LO.rd_load_from], al
  811.  
  812.         mov     eax, dword[devicesdat_size]
  813.         mov     [rdx+BOOT_LO.devicesdat_size], eax
  814.         mov     eax, dword[devicesdat_data]
  815.         mov     [rdx+BOOT_LO.devicesdat_data], eax
  816.  
  817.         mov     rsi, cfg_opt_value_syspath
  818.         mov     rdi, BOOT_LO.syspath
  819.         mov     ecx, 0x17
  820.         rep movsb
  821.  
  822.         ; kernel trampoline
  823.         mov     rsi, kernel_trampoline
  824.         mov     rdi, KERNEL_TRAMPOLINE
  825.         mov     ecx, kernel_trampoline.size
  826.         rep movsb
  827.  
  828.         mov     rax, GDTR
  829.         lgdt    [cs:rax]
  830.  
  831.         mov     ax, DATA_32_SELECTOR
  832.         mov     ds, ax
  833.         mov     es, ax
  834.         mov     fs, ax
  835.         mov     gs, ax
  836.         mov     ss, ax
  837.  
  838.         push    CODE_32_SELECTOR
  839.         mov     rax, KERNEL_TRAMPOLINE
  840.         push    rax
  841.         retf
  842.  
  843. .error:
  844.         mov     rbx, [efi_table]
  845.         mov     rbx, [rbx+EFI_SYSTEM_TABLE.ConOut]
  846.         eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString, rbx, \
  847.                 msg_error
  848.         jmp     $
  849.  
  850.  
  851. halt_on_error:
  852.         test    eax, eax
  853.         jz      @f
  854.         call    clearbuf
  855.         mov     rdi, msg
  856.         call    num2hex
  857.         mov     rbx, [efi_table]
  858.         mov     rbx, [rbx+EFI_SYSTEM_TABLE.ConOut]
  859.         eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString, rbx, \
  860.                 msg_error
  861.         mov     rbx, [efi_table]
  862.         mov     rbx, [rbx+EFI_SYSTEM_TABLE.ConOut]
  863.         eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString, rbx, msg
  864.         jmp     $
  865. @@:
  866.         ret
  867.  
  868. find_rsdp:
  869.         push    rbx
  870.         mov     rbx, [efi_table]
  871.         mov     rbx, [rbx+EFI_SYSTEM_TABLE.ConOut]
  872.         eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString, rbx, \
  873.                 msg_look_for_rsdp
  874.         pop     rbx
  875.  
  876.         mov     rbx, [efi_table]
  877.         mov     rdi, [rbx+EFI_SYSTEM_TABLE.ConfigurationTable]
  878.         mov     rcx, [rbx+EFI_SYSTEM_TABLE.NumberOfTableEntries]
  879.         mov     rax, 0x11d3e4f18868e871
  880.         mov     rdx, 0x81883cc7800022bc
  881. .next_table:
  882.         dec     ecx
  883.         js      .all_tables_done
  884.         cmp     [rdi+0], rax
  885.         jnz     .not_acpi20
  886.         cmp     [rdi+8], rdx
  887.         jnz     .not_acpi20
  888.         mov     rax, [rdi+16]
  889.         mov     rdx, BOOT_LO.acpi_rsdp
  890.         mov     [rdx], eax
  891.         jmp     .all_tables_done
  892. .not_acpi20:
  893.         add     rdi, 24
  894.         jmp     .next_table
  895. .all_tables_done:
  896.         ret
  897.  
  898. calc_memmap:
  899.         mov     rbx, [efi_table]
  900.         mov     rbx, [rbx+EFI_SYSTEM_TABLE.BootServices]
  901.         eficall rbx, EFI_BOOT_SERVICES.AllocatePages, EFI_ALLOCATE_ANY_PAGES, \
  902.                 EFI_RESERVED_MEMORY_TYPE, MEMORY_MAP_SIZE/0x1000, memory_map
  903.         call    halt_on_error
  904.  
  905.         mov     rbx, [efi_table]
  906.         mov     rbx, [rbx+EFI_SYSTEM_TABLE.BootServices]
  907.         eficall rbx, EFI_BOOT_SERVICES.GetMemoryMap, memory_map_size, \
  908.                 [memory_map], memory_map_key, descriptor_size, descriptor_ver
  909.         call    halt_on_error
  910.  
  911.         mov     rdi, BOOT_LO.memmap_blocks
  912.         mov     dword[rdi-4], 0 ; memmap_block_cnt
  913.         mov     rsi, [memory_map]
  914.         mov     rbx, rsi
  915.         add     rbx, [memory_map_size]
  916. .next_descr:
  917.         call    add_uefi_memmap
  918.         add     rsi, [descriptor_size]
  919.         cmp     rsi, rbx
  920.         jb      .next_descr
  921.         ret
  922.  
  923. dump_memmap:
  924.         xor     eax, eax
  925.         mov     rsi, BOOT_LO.memmap_blocks
  926.         mov     ebx, [rax+BOOT_LO.memmap_block_cnt]
  927.  
  928.         call    clearbuf
  929.         mov     eax, ebx
  930.         mov     rdi, msg
  931.         call    num2dec
  932.         push    rbx
  933.         mov     rbx, [efi_table]
  934.         mov     rbx, [rbx+EFI_SYSTEM_TABLE.ConOut]
  935.         eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString, rbx, \
  936.                 msg_memmap
  937.         pop     rbx
  938.         push    rbx
  939.         mov     rbx, [efi_table]
  940.         mov     rbx, [rbx+EFI_SYSTEM_TABLE.ConOut]
  941.         eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString, rbx, msg
  942.         pop     rbx
  943.         call    clearbuf
  944. .next_mapping:
  945.         dec     ebx
  946.         js      .done
  947.         mov     rax, rsi
  948.         mov     rcx, BOOT_LO.memmap_blocks
  949.         sub     rax, rcx
  950.         mov     ecx, sizeof.e820entry
  951.         xor     edx, edx
  952.         div     ecx
  953.         mov     rdi, msg
  954.         call    num2dec
  955.         mov     rax, [rsi+e820entry.addr]
  956.         mov     rdi, msg+4*2
  957.         call    num2hex
  958.         mov     rax, [rsi+e820entry.size]
  959.         mov     rdi, msg+24*2
  960.         call    num2hex
  961.         push    rbx
  962.         mov     rbx, [efi_table]
  963.         mov     rbx, [rbx+EFI_SYSTEM_TABLE.ConOut]
  964.         eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString, rbx, msg
  965.         pop     rbx
  966.         add     rsi, sizeof.e820entry
  967.         jmp     .next_mapping
  968. .done:
  969.         ret
  970.  
  971.  
  972. ; linux/arch/x86/platform/efi/efi.c
  973. ; do_add_efi_memmap
  974. add_uefi_memmap:
  975.         xor     eax, eax
  976.         cmp     [rax+BOOT_LO.memmap_block_cnt], MAX_MEMMAP_BLOCKS
  977.         jz      .done
  978.  
  979.         mov     rax, [rsi+EFI_MEMORY_DESCRIPTOR.PhysicalStart]
  980.         mov     [rdi+e820entry.addr], rax
  981.  
  982.         mov     rax, [rsi+EFI_MEMORY_DESCRIPTOR.NumberOfPages]
  983.         shl     rax, 12
  984.         mov     [rdi+e820entry.size], rax
  985.  
  986.         mov     ecx, [rsi+EFI_MEMORY_DESCRIPTOR.Type]
  987.         cmp     ecx, EFI_LOADER_CODE
  988.         jz      .mem_ram_if_wb
  989.         cmp     ecx, EFI_LOADER_DATA
  990.         jz      .mem_ram_if_wb
  991.         cmp     ecx, EFI_BOOT_SERVICES_CODE
  992.         jz      .mem_ram_if_wb
  993.         cmp     ecx, EFI_BOOT_SERVICES_DATA
  994.         jz      .mem_ram_if_wb
  995.         cmp     ecx, EFI_CONVENTIONAL_MEMORY
  996.         jz      .mem_ram_if_wb
  997.         cmp     ecx, EFI_ACPI_RECLAIM_MEMORY
  998.         mov     eax, E820_ACPI
  999.         jz      .type_done
  1000.         cmp     ecx, EFI_ACPI_MEMORY_NVS
  1001.         mov     eax, E820_NVS
  1002.         jz      .type_done
  1003.         cmp     ecx, EFI_UNUSABLE_MEMORY
  1004.         mov     eax, E820_UNUSABLE
  1005.         jz      .type_done
  1006.         cmp     ecx, EFI_PERSISTENT_MEMORY
  1007.         mov     eax, E820_PMEM
  1008.         jz      .type_done
  1009.         jmp     .reserved
  1010. .mem_ram_if_wb:
  1011.         test    [rsi+EFI_MEMORY_DESCRIPTOR.Attribute], dword EFI_MEMORY_WB
  1012.         mov     eax, E820_RAM
  1013.         jnz     .type_done
  1014. .reserved:
  1015.         mov     eax, E820_RESERVED
  1016. .type_done:
  1017.         mov     [rdi+e820entry.type], eax
  1018.         cmp     eax, E820_RAM
  1019.         jnz     @f
  1020.         xor     eax, eax
  1021.         inc     [rax+BOOT_LO.memmap_block_cnt]
  1022.         add     rdi, sizeof.e820entry
  1023. @@:
  1024. .done:
  1025.         ret
  1026.  
  1027.  
  1028. num2dec:
  1029.         push    rax rbx rcx rdx rsi rdi
  1030.  
  1031.         xor     ecx, ecx
  1032.         mov     ebx, 10
  1033. .next_digit:
  1034.         xor     edx, edx
  1035.         div     ebx
  1036.         push    rdx
  1037.         inc     ecx
  1038.         test    eax, eax
  1039.         jnz     .next_digit
  1040.  
  1041. .next_char:
  1042.         pop     rax
  1043.         add     eax, '0'
  1044.         stosw
  1045.         loop    .next_char
  1046.  
  1047.         pop     rdi rsi rdx rcx rbx rax
  1048.         ret
  1049.  
  1050.  
  1051. num2hex:
  1052.         push    rax rbx rcx rdx rsi rdi
  1053.  
  1054.         xchg    rdx, rax
  1055.         mov     ecx, 16
  1056. .next_tetra:
  1057.         rol     rdx, 4
  1058.         movzx   eax, dl
  1059.         and     eax, 0x0f
  1060.         movzx   eax, byte[hex+eax]
  1061.         stosw
  1062.         loop    .next_tetra
  1063.  
  1064.         pop     rdi rsi rdx rcx rbx rax
  1065.         ret
  1066.  
  1067. hex db '0123456789ABCDEF'
  1068.  
  1069. clearbuf:
  1070.         push    rax rbx rcx rdx rsi rdi
  1071.         mov     eax, 0x0020
  1072.         mov     ecx, 79
  1073.         mov     rdi, msg
  1074.         rep stosw
  1075.         pop     rdi rsi rdx rcx rbx rax
  1076.         ret
  1077.  
  1078. use32
  1079. kernel_trampoline:
  1080. org KERNEL_TRAMPOLINE
  1081.         mov     eax, cr0
  1082.         and     eax, not CR0_PG
  1083.         mov     cr0, eax
  1084.  
  1085.         mov     ecx, MSR_AMD_EFER
  1086.         rdmsr
  1087.         btr     eax, 8                  ; LME
  1088.         wrmsr
  1089.  
  1090.         mov     eax, cr4
  1091.         and     eax, not CR4_PAE
  1092.         mov     cr4, eax
  1093.  
  1094.         push    KERNEL_BASE
  1095.         retn
  1096.  
  1097. align 16
  1098. GDTR:
  1099.         dw 4*8-1
  1100.         dq GDT
  1101. align 16
  1102. GDT:
  1103.         dw 0, 0, 0, 0
  1104.         dw 0FFFFh,0,9A00h,0CFh          ; 32-bit code
  1105.         dw 0FFFFh,0,9200h,0CFh          ; flat data
  1106.         dw 0FFFFh,0,9A00h,0AFh          ; 64-bit code
  1107. assert $ < BOOT_LO
  1108. kernel_trampoline.size = $ - KERNEL_TRAMPOLINE
  1109.  
  1110. section '.rodata' data readable
  1111. gopuuid         db EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID
  1112. lipuuid         db EFI_LOADED_IMAGE_PROTOCOL_GUID
  1113. sfspguid        db EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID
  1114.  
  1115. file_name       du '\EFI\KOLIBRIOS\KOLIBRI.INI',0
  1116. kernel_name     du '\EFI\KOLIBRIOS\KOLIBRI.KRN',0
  1117. ramdisk_name    du '\EFI\KOLIBRIOS\KOLIBRI.IMG',0
  1118. devicesdat_name du '\EFI\KOLIBRIOS\DEVICES.DAT',0
  1119.  
  1120. config_options  dq cfg_opt_name_resolution, cfg_opt_func_resolution, \
  1121.                    cfg_opt_cmnt_resolution, \
  1122.                    cfg_opt_name_acpi,  cfg_opt_func_acpi, cfg_opt_cmnt_acpi, \
  1123.                    cfg_opt_name_debug_print, cfg_opt_func_debug_print, \
  1124.                    cfg_opt_cmnt_debug_print, \
  1125.                    cfg_opt_name_launcher_start, cfg_opt_func_launcher_start, \
  1126.                    cfg_opt_cmnt_launcher_start, \
  1127.                    cfg_opt_name_mtrr,  cfg_opt_func_mtrr, cfg_opt_cmnt_mtrr, \
  1128.                    cfg_opt_name_ask_params,  cfg_opt_func_ask_params, \
  1129.                    cfg_opt_cmnt_ask_params, \
  1130.                    cfg_opt_name_imgfrom, cfg_opt_func_imgfrom, \
  1131.                    cfg_opt_cmnt_imgfrom, \
  1132.                    cfg_opt_name_syspath, cfg_opt_func_syspath, \
  1133.                    cfg_opt_cmnt_syspath, \
  1134.                    0
  1135.  
  1136. cfg_opt_name_resolution     db "resolution",0
  1137. cfg_opt_name_acpi           db "acpi",0
  1138. cfg_opt_name_debug_print    db "debug_print",0
  1139. cfg_opt_name_launcher_start db "launcher_start",0
  1140. cfg_opt_name_mtrr           db "mtrr",0
  1141. cfg_opt_name_ask_params     db "ask_params",0
  1142. cfg_opt_name_imgfrom        db "imgfrom",0
  1143. cfg_opt_name_syspath        db "syspath",0
  1144.  
  1145. cfg_opt_cmnt_resolution     db "# Graphic mode",0
  1146. cfg_opt_cmnt_acpi           db "# ACPI settings",0xa, \
  1147.                                "#   0: don't use",0xa, \
  1148.                                "#   1: parse ACPI tables",0xa, \
  1149.                                "#   2: + call _PIC method",0xa, \
  1150.                                "#   3: + get APIC interrupts",0xa,0
  1151. cfg_opt_cmnt_debug_print    db "# Duplicate debug output to the screen",0
  1152. cfg_opt_cmnt_launcher_start db "# Start LAUNCHER app after kernel is loaded",0
  1153. cfg_opt_cmnt_mtrr           db "# Configure MTRR's",0
  1154. cfg_opt_cmnt_ask_params     db "# Interrupt booting to ask the user for boot", \
  1155.                                " params",0
  1156. cfg_opt_cmnt_imgfrom        db "# Where to load ramdisk image from",0
  1157. cfg_opt_cmnt_syspath        db "# Path to /sys directory",0
  1158.  
  1159. msg_u4k_loaded            du "uefi64kos loaded",13,10,0
  1160. msg_read_options          du "Read options from config file",13,10,0
  1161. msg_file_size             du "File size:",13,10,0
  1162. msg_parsing_config        du "Parsing config file",13,10,0
  1163. msg_load_kernel           du "Load kernel",13,10,0
  1164. msg_load_ramdisk          du "Load ramdisk",13,10,0
  1165. msg_load_devicesdat       du "Load DEVICES.DAT",13,10,0
  1166. msg_alloc_devicesdat      du "Allocate memory for DEVICES.DAT",13,10,0
  1167. msg_locate_gop_handlers   du "Locate GOP handlers",13,10,0
  1168. msg_look_for_gop_handler  du "Look for GOP handler",13,10,0
  1169. msg_query_handler         du "Query handler",13,10,0
  1170. msg_query_vmode           du "Query vmode",13,10,0
  1171. msg_vmode_found           du "Video mode found",13,10,0
  1172. msg_look_for_rsdp         du "Look for RSDP",13,10,0
  1173. msg_rsdp_found            du "RSDP found",13,10,0
  1174. msg_acpi_tables_done      du "ACPI tables done",13,10,0
  1175. msg_ask_for_params        du "Ask for params",13,10,0
  1176. msg_set_graphic_mode      du "Set graphic mode",13,10,0
  1177. msg_success               du "Success!",13,10,0
  1178. msg_gop_buffer_size       du "GOP buffer size",13,10,0
  1179. msg_opt_resolution        du "Option resolution: ",0
  1180. msg_memmap                du "Memmap",13,10,0
  1181. msg_error                 du "Error!",13,10,0
  1182. msg_error_efi_lip_handle  du "efi_handle can't handle LIP",13,10,0
  1183. msg_error_lip_dev_sfsp    du "LIP device handle can't handle SFSP",13,10,0
  1184. msg_error_sfsp_openvolume du "SFSP OpenVolume failed",13,10,0
  1185. msg_error_no_such_vmode   du "No such vmode",13,10,0
  1186. msg_error_out_of_handlers du "Out of handlers",13,10,0
  1187. msg_error_open_file       du "Error: can't open file ",0
  1188. msg_error_exit_boot_services du "Error: Exit boot services",13,10,0
  1189. msg                       du 79 dup " ",13,10,0
  1190.  
  1191.  
  1192. section '.data' data readable writeable
  1193. efi_handle  dq 0
  1194. efi_table   dq 0
  1195. uefi_rsptmp dq 0
  1196.  
  1197. fb_base         dq 0
  1198.  
  1199. gop_buffer_size dq GOP_BUFFER_SIZE
  1200. gop_handle      dq 0
  1201. gop_interface   dq 0
  1202. gop_info_size   dq 0
  1203. gop_info        dq 0
  1204.  
  1205. lip_buffer_size dq LIP_BUFFER_SIZE
  1206. lip_handle      dq 0
  1207. lip_interface   dq 0
  1208.  
  1209. sfsp_interface  dq 0
  1210.  
  1211. esp_root        dq ?
  1212. file_handle     dq ?
  1213. file_buffer_size dq FILE_BUFFER_SIZE-1  ; leave the last byte for \0
  1214.  
  1215. cfg_opt_used_resolution     db 0
  1216. cfg_opt_used_acpi           db 0
  1217. cfg_opt_used_debug_print    db 0
  1218. cfg_opt_used_launcher_start db 0
  1219. cfg_opt_used_mtrr           db 0
  1220. cfg_opt_used_ask_params     db 0
  1221. cfg_opt_used_imgfrom        db 0
  1222. cfg_opt_used_syspath        db 0
  1223.  
  1224. cfg_opt_value_vmode          db 0
  1225. cfg_opt_value_acpi           db 0
  1226. cfg_opt_value_debug_print    db 0
  1227. cfg_opt_value_launcher_start db 1
  1228. cfg_opt_value_mtrr           db 0
  1229. cfg_opt_value_ask_params     db 0
  1230. cfg_opt_value_imgfrom        db RD_LOAD_FROM_MEMORY
  1231. cfg_opt_value_syspath        db "/RD/1",0
  1232.                              rb 20
  1233.  
  1234. memory_map_key  dq 0
  1235. descriptor_size dq 0
  1236. descriptor_ver  dq 0
  1237. memory_map_size dq MEMORY_MAP_SIZE
  1238.  
  1239. efi_fs_info_id db EFI_FILE_SYSTEM_INFO_ID
  1240. efi_fs_info_size dq sizeof.EFI_FILE_SYSTEM_INFO
  1241. efi_fs_info EFI_FILE_SYSTEM_INFO
  1242.  
  1243. memory_map      dq ?
  1244. gop_buffer      rq GOP_BUFFER_SIZE/8
  1245. devicesdat_data dq 0xffffffff
  1246. devicesdat_size dq 0x1000
  1247. status          dq ?
  1248.  
  1249. section '.reloc' fixups data discardable
  1250.