Subversion Repositories Kolibri OS

Rev

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