Subversion Repositories Kolibri OS

Rev

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

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                              ;;
  3. ;; Copyright (C) KolibriOS team 2020-2021. 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. ;; Written by Ivan Baravy                                       ;;
  8. ;;                                                              ;;
  9. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  10.  
  11. format pe efi
  12. entry main
  13.  
  14. section '.text' code executable readable
  15.  
  16. include '../../struct.inc'
  17. include '../../macros.inc'
  18. include '../../proc32.inc'
  19. include '../../const.inc'
  20. include 'uefi32.inc'
  21.  
  22. MEMORY_MAP_SIZE = 0x4000
  23. GOP_BUFFER_SIZE = 0x100
  24. LIP_BUFFER_SIZE = 0x100
  25. FILE_BUFFER_SIZE = 0x1000
  26.  
  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.  
  34. ; linux/arch/x86/include/uapi/asm/e820.h
  35. E820_RAM       = 1
  36. E820_RESERVED  = 2
  37. E820_ACPI      = 3
  38. E820_NVS       = 4
  39. E820_UNUSABLE  = 5
  40. E820_PMEM      = 7
  41.  
  42. proc load_file stdcall uses ebx esi edi, _root, _name, _buffer, _size, _fatal
  43.         mov     eax, [_root]
  44.         ccall   [eax+EFI_FILE_PROTOCOL.Open], eax, file_handle, [_name], \
  45.                 EFI_FILE_MODE_READ, 0
  46.         test    eax, eax
  47.         jz      @f
  48.         xor     eax, eax
  49.         cmp     [_fatal], 1
  50.         jnz     .done
  51.         mov     ebx, [efi_table]
  52.         mov     ebx, [ebx+EFI_SYSTEM_TABLE.ConOut]
  53.         ccall   [ebx+EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString], ebx, \
  54.                 msg_error_open_file
  55.         ccall   [ebx+EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString], ebx, \
  56.                 [_name]
  57.         jmp     $
  58. @@:
  59.         mov     eax, [file_handle]
  60.         lea     ecx, [_size]
  61.         ccall   [eax+EFI_FILE_PROTOCOL.Read], eax, ecx, [_buffer]
  62.         mov     eax, [file_handle]
  63.         ccall   [eax+EFI_FILE_PROTOCOL.Close], eax
  64.         mov     eax, [_size]
  65. .done:
  66.         ret
  67. endp
  68.  
  69. proc skip_whitespace
  70. .next_char:
  71.         cmp     byte[esi], 0
  72.         jz      .done
  73.         cmp     byte[esi], 0x20 ; ' '
  74.         jz      .whitespace
  75.         cmp     byte[esi], 9    ; '\t'
  76.         jz      .whitespace
  77.         jmp     .done
  78. .whitespace:
  79.         inc     esi
  80.         jmp     .next_char
  81. .done:
  82.         ret
  83. endp
  84.  
  85. proc skip_until_newline
  86. .next_char:
  87.         cmp     byte[esi], 0
  88.         jz      .done
  89.         cmp     byte[esi], 0xd  ; '\r'
  90.         jz      .done
  91.         cmp     byte[esi], 0xa  ; '\n'
  92.         jz      .done
  93.         inc     esi
  94.         jmp     .next_char
  95. .done:
  96.         ret
  97. endp
  98.  
  99. proc skip_newline
  100. .next_char:
  101.         cmp     byte[esi], 0xd  ; '\r'
  102.         jz      .newline
  103.         cmp     byte[esi], 0xa  ; '\n'
  104.         jz      .newline
  105.         jmp     .done
  106. .newline:
  107.         inc     esi
  108.         jmp     .next_char
  109. .done:
  110.         ret
  111. endp
  112.  
  113. proc skip_line
  114.         call    skip_until_newline
  115.         call    skip_newline
  116.         ret
  117. endp
  118.  
  119. proc dec2bin
  120.         mov     edx, 0
  121. .next_char:
  122.         movzx   eax, byte[esi]
  123.         test    eax, eax
  124.         jz      .done
  125.         sub     eax, '0'
  126.         jb      .done
  127.         cmp     eax, 9
  128.         ja      .done
  129.         inc     esi
  130.         imul    edx, 10
  131.         add     edx, eax
  132.         jmp     .next_char
  133. .done:
  134.         mov     eax, edx
  135.         ret
  136. endp
  137.  
  138. proc parse_option
  139.         mov     ebx, config_options-3*4
  140. .try_next_option:
  141.         add     ebx, 3*4
  142.         mov     edi, esi
  143.         mov     edx, [ebx]      ; option name
  144.         test    edx, edx
  145.         jz      .done
  146. .next_char:
  147.         cmp     byte[edx], 0
  148.         jnz     @f
  149.         cmp     byte[edi], '='
  150.         jz      .opt_name_ok
  151. @@:
  152.         cmp     byte[edi], 0
  153.         jz      .done
  154.         movzx   eax, byte[edi]
  155.         cmp     [edx], al
  156.         jnz     .try_next_option
  157.         inc     edi
  158.         inc     edx
  159.         jmp     .next_char
  160. .opt_name_ok:
  161.         inc     edi
  162.         mov     esi, edi
  163.         call    dword[ebx+4]
  164. .done:
  165.         ret
  166. endp
  167.  
  168. proc parse_line
  169. .next_line:
  170.         cmp     byte[esi], 0
  171.         jz      .done
  172.         cmp     byte[esi], 0xd  ; '\r'
  173.         jz      .skip
  174.         cmp     byte[esi], 0xa  ; '\n'
  175.         jz      .skip
  176.         cmp     byte[esi], '#'
  177.         jz      .skip
  178.         call    parse_option
  179.         call    skip_line
  180.         jmp     .next_line
  181. .skip:
  182.         call    skip_line
  183.         jmp     .next_line
  184. .done:
  185.         ret
  186. endp
  187.  
  188. proc cfg_opt_func_resolution
  189.         call    dec2bin
  190.         xor     edx, edx
  191.         mov     [edx+BOOT_LO.x_res], ax
  192.         cmp     byte[esi], 'x'
  193.         jz      @f
  194.         cmp     byte[esi], '*'
  195.         jz      @f
  196.         jmp     .done
  197. @@:
  198.         inc     esi
  199.         call    dec2bin
  200.         xor     edx, edx
  201.         mov     [edx+BOOT_LO.y_res], ax
  202.         mov     [cfg_opt_used_resolution], 1
  203. .done:
  204.         ret
  205. endp
  206.  
  207. proc cfg_opt_func_acpi
  208.         call    dec2bin
  209.         mov     [cfg_opt_used_acpi], 1
  210.         mov     [cfg_opt_value_acpi], al
  211.         ret
  212. endp
  213.  
  214. proc cfg_opt_func_debug_print
  215.         call    dec2bin
  216.         mov     [cfg_opt_used_debug_print], 1
  217.         mov     [cfg_opt_value_debug_print], al
  218.         ret
  219. endp
  220.  
  221. proc cfg_opt_func_launcher_start
  222.         call    dec2bin
  223.         mov     [cfg_opt_used_launcher_start], 1
  224.         mov     [cfg_opt_value_launcher_start], al
  225.         ret
  226. endp
  227.  
  228. proc cfg_opt_func_mtrr
  229.         call    dec2bin
  230.         mov     [cfg_opt_used_mtrr], 1
  231.         mov     [cfg_opt_value_mtrr], al
  232.         ret
  233. endp
  234.  
  235. proc cfg_opt_func_ask_params
  236.         call    dec2bin
  237.         mov     [cfg_opt_used_ask_params], 1
  238.         mov     [cfg_opt_value_ask_params], al
  239.         ret
  240. endp
  241.  
  242. proc cfg_opt_func_imgfrom
  243.         call    dec2bin
  244.         mov     [cfg_opt_used_imgfrom], 1
  245.         mov     [cfg_opt_value_imgfrom], al
  246.         ret
  247. endp
  248.  
  249. proc cfg_opt_func_syspath
  250.         mov     edi, cfg_opt_value_syspath
  251. .next_char:
  252.         movzx   eax, byte[esi]
  253.         cmp     al, 0xd ; \r
  254.         jz      .done
  255.         cmp     al, 0xa ; \n
  256.         jz      .done
  257.         inc     esi
  258.         stosb
  259.         jmp     .next_char
  260. .done:
  261.         mov     byte[edi], 0
  262.         ret
  263. endp
  264.  
  265. proc parse_config stdcall uses ebx esi edi, _buffer
  266. ;        mov     esi, [_buffer]
  267.         mov     esi, KERNEL_BASE
  268. .next_line:
  269.         call    parse_line
  270.         cmp     byte[esi], 0
  271.         jnz     .next_line
  272.         ret
  273. endp
  274.  
  275. proc read_options_from_config stdcall uses ebx esi edi
  276.         mov     ebx, [efi_table]
  277.         mov     ebx, [ebx+EFI_SYSTEM_TABLE.BootServices]
  278.         ccall   [ebx+EFI_BOOT_SERVICES.HandleProtocol], [efi_handle], \
  279.                 lipuuid, lip_interface
  280.         test    eax, eax
  281.         jnz     .error
  282.         mov     eax, [lip_interface]
  283.  
  284.         mov     ebx, [efi_table]
  285.         mov     ebx, [ebx+EFI_SYSTEM_TABLE.BootServices]
  286.         ccall   [ebx+EFI_BOOT_SERVICES.HandleProtocol], \
  287.                 [eax+EFI_LOADED_IMAGE_PROTOCOL.DeviceHandle], sfspguid, \
  288.                 sfsp_interface
  289.         test    eax, eax
  290.         jnz     .error
  291.  
  292.         mov     eax, [sfsp_interface]
  293.         ccall   [eax+EFI_SIMPLE_FILE_SYSTEM_PROTOCOL.OpenVolume], eax, esp_root
  294.         test    eax, eax
  295.         jnz     .error
  296.  
  297.         stdcall load_file, [esp_root], file_name, KERNEL_BASE, \
  298.                 FILE_BUFFER_SIZE, 0
  299.         test    eax, eax
  300.         jz      @f
  301.         stdcall parse_config, KERNEL_BASE
  302. @@:
  303. .error:
  304.         ret
  305. endp
  306.  
  307. proc print_vmode uses eax ebx ecx esi edi, _gop_if
  308.         mov     ebx, [_gop_if]
  309.         call    clearbuf
  310.         mov     eax, [ebx+EFI_GRAPHICS_OUTPUT_MODE_INFORMATION.HorizontalResolution]
  311.         mov     edi, msg
  312.         call    num2dec
  313.         mov     eax, [ebx+EFI_GRAPHICS_OUTPUT_MODE_INFORMATION.VerticalResolution]
  314.         mov     edi, msg+8*2
  315.         call    num2dec
  316.  
  317.         mov     eax, [ebx+EFI_GRAPHICS_OUTPUT_MODE_INFORMATION.PixelFormat]
  318.         mov     edi, msg+16*2
  319.         call    num2dec
  320.  
  321.         mov     eax, [ebx+EFI_GRAPHICS_OUTPUT_MODE_INFORMATION.PixelsPerScanLine]
  322.         mov     edi, msg+24*2
  323.         call    num2dec
  324.         mov     eax, [esi+EFI_SYSTEM_TABLE.ConOut]
  325.         ccall   [eax+EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString], eax, msg
  326.         ret
  327. endp
  328.  
  329. proc find_vmode_index_by_resolution uses ebx esi edi
  330.         mov     [cfg_opt_value_vmode], 0
  331. .next_mode:
  332. ;        mov     eax, [esi+EFI_SYSTEM_TABLE.ConOut]
  333. ;        ccall   [eax+EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString], eax, \
  334. ;                msg_query_vmode
  335.  
  336.         movzx   ecx, [cfg_opt_value_vmode]
  337.         mov     eax, [gop_interface]
  338.         ccall   [eax+EFI_GRAPHICS_OUTPUT_PROTOCOL.QueryMode], eax, ecx, \
  339.                 gop_info_size, gop_info
  340.         test    eax, eax
  341.         jz      @f
  342.         call    clearbuf
  343.         mov     edi, msg
  344.         call    num2hex
  345.         mov     eax, [esi+EFI_SYSTEM_TABLE.ConOut]
  346.         ccall   [eax+EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString], eax, msg
  347.         jmp     .skip_mode
  348. @@:
  349.         mov     ecx, [gop_info]
  350.         stdcall print_vmode, ecx
  351.         ; PixelBlueGreenRedReserved8BitPerColor
  352.         cmp     [ecx+EFI_GRAPHICS_OUTPUT_MODE_INFORMATION.PixelFormat], 1
  353.         jnz     .skip_mode
  354.         xor     edx, edx
  355.         mov     eax, [ecx+EFI_GRAPHICS_OUTPUT_MODE_INFORMATION.HorizontalResolution]
  356.         cmp     ax, [edx+BOOT_LO.x_res]
  357.         jnz     .skip_mode
  358.         mov     eax, [ecx+EFI_GRAPHICS_OUTPUT_MODE_INFORMATION.VerticalResolution]
  359.         cmp     ax, [edx+BOOT_LO.y_res]
  360.         jnz     .skip_mode
  361.         jmp     .done
  362. .skip_mode:
  363.         inc     [cfg_opt_value_vmode]
  364.         movzx   eax, [cfg_opt_value_vmode]
  365.         mov     ecx, [gop_interface]
  366.         mov     edx, [ecx+EFI_GRAPHICS_OUTPUT_PROTOCOL.Mode]
  367.         cmp     eax, [edx+EFI_GRAPHICS_OUTPUT_PROTOCOL_MODE.MaxMode]
  368.         jnz     .next_mode
  369.         mov     [cfg_opt_used_resolution], 0
  370.         mov     [cfg_opt_value_ask_params], 1
  371.  
  372.         mov     eax, [esi+EFI_SYSTEM_TABLE.ConOut]
  373.         ccall   [eax+EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString], eax, \
  374.                 msg_error_no_such_vmode
  375.         mov     eax, [esi+EFI_SYSTEM_TABLE.ConOut]
  376.         ccall   [eax+EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString], eax, msg_error
  377.         jmp     $
  378. .error:
  379. .done:
  380.         mov     eax, [esi+EFI_SYSTEM_TABLE.ConOut]
  381.         ccall   [eax+EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString], eax, \
  382.                 msg_vmode_found
  383.         ret
  384. endp
  385.  
  386. proc ask_for_params
  387.         mov     eax, [esi+EFI_SYSTEM_TABLE.ConOut]
  388.         ccall   [eax+EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString], eax, \
  389.                 msg_ask_for_params
  390.         jmp     $
  391. .error:
  392. .done:
  393.         ret
  394. endp
  395.  
  396. main:
  397.         mov     esi, [esp+4]
  398.         mov     [efi_handle], esi
  399.         mov     esi, [esp+8]
  400.         mov     [efi_table], esi
  401.  
  402.         mov     eax, [esi+EFI_SYSTEM_TABLE.ConOut]
  403.         ccall   [eax+EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.Reset], eax, 1
  404.         mov     eax, [esi+EFI_SYSTEM_TABLE.ConOut]
  405.         ccall   [eax+EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString], eax, \
  406.                 msg_u4k_loaded
  407.  
  408.         mov     eax, [esi+EFI_SYSTEM_TABLE.ConOut]
  409.         ccall   [eax+EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString], eax, \
  410.                 msg_read_options
  411.         call    read_options_from_config
  412.  
  413.         mov     eax, [esi+EFI_SYSTEM_TABLE.ConOut]
  414.         ccall   [eax+EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString], eax, \
  415.                 msg_load_kernel
  416.         stdcall load_file, [esp_root], kernel_name, KERNEL_BASE, MAX_FILE_SIZE, 1
  417.  
  418.         mov     eax, [esi+EFI_SYSTEM_TABLE.ConOut]
  419.         ccall   [eax+EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString], eax, \
  420.                 msg_load_ramdisk
  421.         stdcall load_file, [esp_root], ramdisk_name, RAMDISK_BASE, MAX_FILE_SIZE, 1
  422.  
  423.         mov     eax, [esi+EFI_SYSTEM_TABLE.ConOut]
  424.         ccall   [eax+EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString], eax, \
  425.                 msg_alloc_devicesdat
  426.  
  427.         mov     eax, [esi+EFI_SYSTEM_TABLE.BootServices]
  428.         ccall   [eax+EFI_BOOT_SERVICES.AllocatePages], \
  429.                 EFI_ALLOCATE_MAX_ADDRESS, EFI_RESERVED_MEMORY_TYPE, 1, \
  430.                 devicesdat_data
  431.         call    halt_on_error
  432.  
  433.         mov     eax, [esi+EFI_SYSTEM_TABLE.ConOut]
  434.         ccall   [eax+EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString], eax, \
  435.                 msg_load_devicesdat
  436.  
  437.         ccall   load_file, [esp_root], devicesdat_name, [devicesdat_data], \
  438.                 [devicesdat_size], 0
  439.         mov     [devicesdat_size], eax
  440.  
  441.         mov     eax, [esi+EFI_SYSTEM_TABLE.ConOut]
  442.         ccall   [eax+EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString], eax, \
  443.                 msg_locate_gop_handlers
  444.  
  445.         mov     eax, [esi+EFI_SYSTEM_TABLE.BootServices]
  446.         ccall   [eax+EFI_BOOT_SERVICES.LocateHandle], \
  447.                 EFI_LOCATE_SEARCH_TYPE.ByProtocol, gopuuid, 0, \
  448.                 gop_buffer_size, gop_buffer
  449.         mov     [status], eax
  450.  
  451.         mov     eax, [esi+EFI_SYSTEM_TABLE.ConOut]
  452.         ccall   [eax+EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString], eax, \
  453.                 msg_gop_buffer_size
  454.         call    clearbuf
  455.         mov     eax, [gop_buffer_size]
  456.         mov     edi, msg
  457.         call    num2hex
  458.         mov     eax, [esi+EFI_SYSTEM_TABLE.ConOut]
  459.         ccall   [eax+EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString], eax, msg
  460.  
  461.         mov     eax, [status]
  462.         call    halt_on_error
  463.  
  464.         mov     eax, [esi+EFI_SYSTEM_TABLE.ConOut]
  465.         ccall   [eax+EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString], eax, \
  466.                 msg_look_for_gop_handler
  467.  
  468.         mov     ebx, gop_buffer
  469. .next_gop_handle:
  470.         mov     eax, ebx
  471.         sub     eax, gop_buffer
  472.         cmp     eax, [gop_buffer_size]
  473.         jb      @f
  474.         mov     eax, [esi+EFI_SYSTEM_TABLE.ConOut]
  475.         ccall   [eax+EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString], eax, \
  476.                 msg_error_out_of_handlers
  477.         mov     eax, [esi+EFI_SYSTEM_TABLE.ConOut]
  478.         ccall   [eax+EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString], eax, msg_error
  479.         jmp     $
  480. @@:
  481.         mov     eax, [esi+EFI_SYSTEM_TABLE.ConOut]
  482.         ccall   [eax+EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString], eax, \
  483.                 msg_query_handler
  484.  
  485.         mov     eax, [esi+EFI_SYSTEM_TABLE.BootServices]
  486.         ccall   [eax+EFI_BOOT_SERVICES.HandleProtocol], \
  487.                 [ebx], gopuuid, gop_interface
  488. ;mov eax, 0x80000003
  489.         test    eax, eax
  490.         jz      @f
  491.         call    clearbuf
  492.         mov     edi, msg
  493.         call    num2hex
  494.         mov     eax, [esi+EFI_SYSTEM_TABLE.ConOut]
  495.         ccall   [eax+EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString], eax, msg
  496.  
  497.         add     ebx, 4
  498.         jmp     .next_gop_handle
  499. @@:
  500.  
  501.         call    find_rsdp
  502.  
  503.         mov     eax, [esi+EFI_SYSTEM_TABLE.ConOut]
  504.         ccall   [eax+EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString], eax, \
  505.                 msg_acpi_tables_done
  506.  
  507.         cmp     [cfg_opt_used_resolution], 0
  508.         jz      .not_used_resolution
  509.         mov     eax, [esi+EFI_SYSTEM_TABLE.ConOut]
  510.         ccall   [eax+EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString], eax, \
  511.                 msg_opt_resolution
  512.         call    clearbuf
  513.         xor     edx, edx
  514.         movzx   eax, [edx+BOOT_LO.x_res]
  515.         mov     edi, msg
  516.         call    num2dec
  517.         xor     edx, edx
  518.         movzx   eax, [edx+BOOT_LO.y_res]
  519.         mov     edi, msg+8*2
  520.         call    num2dec
  521.         mov     eax, [esi+EFI_SYSTEM_TABLE.ConOut]
  522.         ccall   [eax+EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString], eax, msg
  523.  
  524.         call    find_vmode_index_by_resolution
  525. .not_used_resolution:
  526.         cmp     [cfg_opt_used_debug_print], 0
  527.         jz      .not_used_debug_print
  528.         movzx   eax, [cfg_opt_value_debug_print]
  529.         xor     edx, edx
  530.         mov     [edx+BOOT_LO.debug_print], al
  531. .not_used_debug_print:
  532.  
  533.         cmp     [cfg_opt_value_ask_params], 0
  534.         jz      @f
  535.         call    ask_for_params
  536. @@:
  537.  
  538.         movzx   ecx, [cfg_opt_value_vmode]
  539.         mov     eax, [gop_interface]
  540.         ccall   [eax+EFI_GRAPHICS_OUTPUT_PROTOCOL.SetMode], eax, ecx
  541.         call    halt_on_error
  542.  
  543.         mov     ecx, [gop_interface]
  544.         mov     edx, [ecx+EFI_GRAPHICS_OUTPUT_PROTOCOL.Mode]
  545.         mov     edi, [edx+EFI_GRAPHICS_OUTPUT_PROTOCOL_MODE.FrameBufferBase.lo]
  546.         mov     [fb_base], edi
  547.  
  548.  
  549.         mov     ebx, [edx+EFI_GRAPHICS_OUTPUT_PROTOCOL_MODE.Mode]
  550.         mov     eax, [gop_interface]
  551.         ccall   [eax+EFI_GRAPHICS_OUTPUT_PROTOCOL.QueryMode], eax, ebx, \
  552.                 gop_info_size, gop_info
  553.         test    eax, eax
  554.         jnz     .error
  555.         mov     ecx, [gop_info]
  556.         mov     eax, [ecx+EFI_GRAPHICS_OUTPUT_MODE_INFORMATION.HorizontalResolution]
  557.         xor     edx, edx
  558.         mov     [edx+BOOT_LO.x_res], ax
  559.         mov     eax, [ecx+EFI_GRAPHICS_OUTPUT_MODE_INFORMATION.VerticalResolution]
  560.         mov     [edx+BOOT_LO.y_res], ax
  561.         mov     eax, [ecx+EFI_GRAPHICS_OUTPUT_MODE_INFORMATION.PixelsPerScanLine]
  562.         shl     eax, 2
  563.         mov     [edx+BOOT_LO.pitch], ax
  564.  
  565.         mov     [edx+BOOT_LO.pci_data.access_mechanism], 1
  566.         movzx   eax, [pci_last_bus]
  567.         mov     [edx+BOOT_LO.pci_data.last_bus], al
  568.         mov     [edx+BOOT_LO.pci_data.version], 0x0300  ; PCI 3.0
  569.         mov     [edx+BOOT_LO.pci_data.pm_entry], 0
  570.  
  571.         ; kernel
  572. ;        eficall BootServices, AllocatePages, EFI_RESERVED_MEMORY_TYPE, \
  573. ;                450000/0x1000, EFI_ALLOCATE_ADDRESS
  574.  
  575.         ; ramdisk
  576. ;        eficall BootServices, AllocatePages, EFI_RESERVED_MEMORY_TYPE, \
  577. ;                2880*512/0x1000, EFI_ALLOCATE_ADDRESS
  578.  
  579.         call    calc_memmap
  580. ;        call    dump_memmap
  581.  
  582.         mov     eax, [efi_table]
  583.         mov     eax, [eax+EFI_SYSTEM_TABLE.BootServices]
  584.         ccall   [eax+EFI_BOOT_SERVICES.ExitBootServices], [efi_handle], \
  585.                 [memory_map_key]
  586.         call    halt_on_error
  587.  
  588.         cli
  589.  
  590.         xor     edx, edx
  591.         xor     esi, esi
  592.         mov     [esi+BOOT_LO.bpp], 32
  593.         mov     [esi+BOOT_LO.vesa_mode], dx
  594.         mov     [esi+BOOT_LO.bank_switch], edx
  595.         mov     edi, [fb_base]
  596.         mov     [esi+BOOT_LO.lfb], edi
  597.  
  598.         movzx   eax, [cfg_opt_value_mtrr]
  599.         mov     [esi+BOOT_LO.mtrr], al
  600.  
  601.         movzx   eax, [cfg_opt_value_launcher_start]
  602.         mov     [esi+BOOT_LO.launcher_start], al
  603.  
  604.         movzx   eax, [cfg_opt_value_debug_print]
  605.         mov     [esi+BOOT_LO.debug_print], al
  606.  
  607.         mov     [esi+BOOT_LO.dma], dl
  608. ;        mov     qword[esi+BOOT_LO.pci_data], 0
  609.         mov     [esi+BOOT_LO.apm_entry], edx
  610.         mov     [esi+BOOT_LO.apm_version], dx
  611.         mov     [esi+BOOT_LO.apm_flags], dx
  612.         mov     [esi+BOOT_LO.apm_code_32], dx
  613.         mov     [esi+BOOT_LO.apm_code_16], dx
  614.         mov     [esi+BOOT_LO.apm_data_16], dx
  615.         mov     [esi+BOOT_LO.bios_hd_cnt], dl
  616.  
  617.         movzx   eax, [cfg_opt_value_imgfrom]
  618.         mov     [esi+BOOT_LO.rd_load_from], al
  619.  
  620.         mov     eax, dword[devicesdat_size]
  621.         mov     [edx+BOOT_LO.devicesdat_size], eax
  622.         mov     eax, dword[devicesdat_data]
  623.         mov     [edx+BOOT_LO.devicesdat_data], eax
  624.  
  625.         mov     esi, cfg_opt_value_syspath
  626.         mov     edi, BOOT_LO.syspath
  627.         mov     ecx, 0x17
  628.         rep movsb
  629.  
  630.         lgdt    [cs:GDTR]
  631.  
  632.         mov     ax, DATA_32_SELECTOR
  633.         mov     ds, ax
  634.         mov     es, ax
  635.         mov     fs, ax
  636.         mov     gs, ax
  637.         mov     ss, ax
  638.  
  639.         push    CODE_32_SELECTOR
  640.         lea     eax, [.next]
  641.         push    eax
  642.         retf
  643.  
  644. .next:
  645.         mov     eax, cr0
  646.         and     eax, not CR0_PG
  647.         mov     cr0, eax
  648.  
  649.         mov     eax, cr4
  650.         and     eax, not CR4_PAE
  651.         mov     cr4, eax
  652.  
  653.         push    KERNEL_BASE
  654.         retn
  655.  
  656. .error:
  657.         mov     esi, [efi_table]
  658.         mov     eax, [esi+EFI_SYSTEM_TABLE.ConOut]
  659.         ccall   [eax+EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString], eax, \
  660.                 msg_error
  661.         jmp     $
  662.  
  663. halt_on_error:
  664.         test    eax, eax
  665.         jz      @f
  666.         call    clearbuf
  667.         mov     edi, msg
  668.         call    num2hex
  669.         mov     eax, [esi+EFI_SYSTEM_TABLE.ConOut]
  670.         ccall   [eax+EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString], eax, \
  671.                 msg_error
  672.         mov     eax, [esi+EFI_SYSTEM_TABLE.ConOut]
  673.         ccall   [eax+EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString], eax, msg
  674.         jmp     $
  675. @@:
  676.         ret
  677.  
  678. proc find_rsdp
  679.         mov     eax, [esi+EFI_SYSTEM_TABLE.ConOut]
  680.         ccall   [eax+EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString], eax, \
  681.                 msg_look_for_rsdp
  682.  
  683.         mov     edi, [esi+EFI_SYSTEM_TABLE.ConfigurationTable]
  684.         mov     ecx, [esi+EFI_SYSTEM_TABLE.NumberOfTableEntries]
  685. .next_table:
  686.         dec     ecx
  687.         js      .all_tables_done
  688.         ; EFI_ACPI_TABLE_GUID
  689.         cmp     dword[edi+EFI_CONFIGURATION_TABLE.VendorGUID+0x0], 0x8868e871
  690.         jnz     .not_acpi20
  691.         cmp     dword[edi+EFI_CONFIGURATION_TABLE.VendorGUID+0x4], 0x11d3e4f1
  692.         jnz     .not_acpi20
  693.         cmp     dword[edi+EFI_CONFIGURATION_TABLE.VendorGUID+0x8], 0x800022bc
  694.         jnz     .not_acpi20
  695.         cmp     dword[edi+EFI_CONFIGURATION_TABLE.VendorGUID+0xc], 0x81883cc7
  696.         jnz     .not_acpi20
  697.         mov     eax, [edi+EFI_CONFIGURATION_TABLE.VendorTable]
  698.         mov     edx, BOOT_LO.acpi_rsdp
  699.         mov     [edx], eax
  700.         mov     eax, [esi+EFI_SYSTEM_TABLE.ConOut]
  701.         ccall   [eax+EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString], eax, \
  702.                 msg_rsdp_found
  703.         jmp     .all_tables_done
  704. .not_acpi20:
  705.         add     edi, sizeof.EFI_CONFIGURATION_TABLE
  706.         jmp     .next_table
  707. .all_tables_done:
  708.         ret
  709. endp
  710.  
  711. proc calc_memmap
  712.         mov     eax, [esi+EFI_SYSTEM_TABLE.BootServices]
  713.         ccall   [eax+EFI_BOOT_SERVICES.AllocatePages], EFI_ALLOCATE_ANY_PAGES, \
  714.                 EFI_RESERVED_MEMORY_TYPE, MEMORY_MAP_SIZE/0x1000, memory_map
  715.         call    halt_on_error
  716.  
  717.         mov     eax, [esi+EFI_SYSTEM_TABLE.BootServices]
  718.         ccall   [eax+EFI_BOOT_SERVICES.GetMemoryMap], memory_map_size, \
  719.                 [memory_map], memory_map_key, descriptor_size, descriptor_ver
  720.         call    halt_on_error
  721.  
  722.         push    esi
  723.         mov     edi, BOOT_LO.memmap_blocks
  724.         mov     dword[edi-4], 0 ; memmap_block_cnt
  725.         mov     esi, [memory_map]
  726.         mov     ebx, esi
  727.         add     ebx, [memory_map_size]
  728. .next_descr:
  729.         call    add_uefi_memmap
  730.         add     esi, [descriptor_size]
  731.         cmp     esi, ebx
  732.         jb      .next_descr
  733.         pop     esi
  734.         ret
  735. endp
  736.  
  737. ; linux/arch/x86/platform/efi/efi.c
  738. ; do_add_efi_memmap
  739. proc add_uefi_memmap
  740.         cmp     [BOOT_LO.memmap_block_cnt], MAX_MEMMAP_BLOCKS
  741.         jz      .done
  742.  
  743.         mov     eax, [esi+EFI_MEMORY_DESCRIPTOR.PhysicalStart.lo]
  744.         mov     edx, [esi+EFI_MEMORY_DESCRIPTOR.PhysicalStart.hi]
  745.         mov     [edi+e820entry.addr.lo], eax
  746.         mov     [edi+e820entry.addr.hi], edx
  747.  
  748.         mov     eax, [esi+EFI_MEMORY_DESCRIPTOR.NumberOfPages.lo]
  749.         mov     edx, [esi+EFI_MEMORY_DESCRIPTOR.NumberOfPages.hi]
  750.         shld    edx, eax, 12
  751.         shl     eax, 12
  752.         mov     [edi+e820entry.size.lo], eax
  753.         mov     [edi+e820entry.size.hi], edx
  754.  
  755.         mov     ecx, [esi+EFI_MEMORY_DESCRIPTOR.Type]
  756.         cmp     ecx, EFI_LOADER_CODE
  757.         jz      .mem_ram_if_wb
  758.         cmp     ecx, EFI_LOADER_DATA
  759.         jz      .mem_ram_if_wb
  760.         cmp     ecx, EFI_BOOT_SERVICES_CODE
  761.         jz      .mem_ram_if_wb
  762.         cmp     ecx, EFI_BOOT_SERVICES_DATA
  763.         jz      .mem_ram_if_wb
  764.         cmp     ecx, EFI_CONVENTIONAL_MEMORY
  765.         jz      .mem_ram_if_wb
  766.         cmp     ecx, EFI_ACPI_RECLAIM_MEMORY
  767.         mov     eax, E820_ACPI
  768.         jz      .type_done
  769.         cmp     ecx, EFI_ACPI_MEMORY_NVS
  770.         mov     eax, E820_NVS
  771.         jz      .type_done
  772.         cmp     ecx, EFI_UNUSABLE_MEMORY
  773.         mov     eax, E820_UNUSABLE
  774.         jz      .type_done
  775.         cmp     ecx, EFI_PERSISTENT_MEMORY
  776.         mov     eax, E820_PMEM
  777.         jz      .type_done
  778.         jmp     .reserved
  779. .mem_ram_if_wb:
  780.         test    [esi+EFI_MEMORY_DESCRIPTOR.Attribute.lo], EFI_MEMORY_WB
  781.         mov     eax, E820_RAM
  782.         jnz     .type_done
  783. .reserved:
  784.         mov     eax, E820_RESERVED
  785. .type_done:
  786.         mov     [edi+e820entry.type], eax
  787.         cmp     eax, E820_RAM
  788.         jnz     @f
  789.         inc     [BOOT_LO.memmap_block_cnt]
  790.         add     edi, sizeof.e820entry
  791. @@:
  792. .done:
  793.         ret
  794. endp
  795.  
  796.  
  797. proc num2dec
  798.         pushad
  799.  
  800.         xor     ecx, ecx
  801.         mov     ebx, 10
  802. .next_digit:
  803.         xor     edx, edx
  804.         div     ebx
  805.         push    edx
  806.         inc     ecx
  807.         test    eax, eax
  808.         jnz     .next_digit
  809.  
  810. .next_char:
  811.         pop     eax
  812.         add     eax, '0'
  813.         stosw
  814.         loop    .next_char
  815.  
  816.         popad
  817.         ret
  818. endp
  819.  
  820.  
  821. proc num2hex
  822.         pushad
  823.  
  824.         xchg    edx, eax
  825.         mov     ecx, 8
  826. .next_tetra:
  827.         rol     edx, 4
  828.         movzx   eax, dl
  829.         and     eax, 0x0f
  830.         movzx   eax, byte[hex+eax]
  831.         stosw
  832.         loop    .next_tetra
  833.  
  834.         popad
  835.         ret
  836. endp
  837.  
  838.  
  839. hex db '0123456789ABCDEF'
  840.  
  841. proc clearbuf
  842.         pushad
  843.         mov     eax, 0x0020
  844.         mov     ecx, 79
  845.         mov     edi, msg
  846.         rep stosw
  847.         popad
  848.         ret
  849. endp
  850.  
  851. section '.rodata' data readable
  852. align 16
  853. GDTR:
  854.         dw 3*8-1
  855.         dq GDT
  856. align 16
  857. GDT:
  858.         dw 0, 0, 0, 0
  859.         dw 0FFFFh,0,9A00h,0CFh          ; 32-bit code
  860.         dw 0FFFFh,0,9200h,0CFh          ; flat data
  861.  
  862. gopuuid         db EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID
  863. lipuuid         db EFI_LOADED_IMAGE_PROTOCOL_GUID
  864. sfspguid        db EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID
  865. pcirbguid       db EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_GUID
  866.  
  867. file_name       du "\EFI\KOLIBRIOS\KOLIBRI.INI",0
  868. kernel_name     du "\EFI\KOLIBRIOS\KOLIBRI.KRN",0
  869. ramdisk_name    du "\EFI\KOLIBRIOS\KOLIBRI.IMG",0
  870. devicesdat_name du "\EFI\KOLIBRIOS\DEVICES.DAT",0
  871.  
  872. config_options  dd cfg_opt_name_resolution, cfg_opt_func_resolution, \
  873.                    cfg_opt_cmnt_resolution, \
  874.                    cfg_opt_name_acpi,  cfg_opt_func_acpi, cfg_opt_cmnt_acpi, \
  875.                    cfg_opt_name_debug_print, cfg_opt_func_debug_print, \
  876.                    cfg_opt_cmnt_debug_print, \
  877.                    cfg_opt_name_launcher_start, cfg_opt_func_launcher_start, \
  878.                    cfg_opt_cmnt_launcher_start, \
  879.                    cfg_opt_name_mtrr,  cfg_opt_func_mtrr, cfg_opt_cmnt_mtrr, \
  880.                    cfg_opt_name_ask_params,  cfg_opt_func_ask_params, \
  881.                    cfg_opt_cmnt_ask_params, \
  882.                    cfg_opt_name_imgfrom, cfg_opt_func_imgfrom, \
  883.                    cfg_opt_cmnt_imgfrom, \
  884.                    cfg_opt_name_syspath, cfg_opt_func_syspath, \
  885.                    cfg_opt_cmnt_syspath, \
  886.                    0
  887.  
  888. cfg_opt_name_resolution     db "resolution",0
  889. cfg_opt_name_acpi           db "acpi",0
  890. cfg_opt_name_debug_print    db "debug_print",0
  891. cfg_opt_name_launcher_start db "launcher_start",0
  892. cfg_opt_name_mtrr           db "mtrr",0
  893. cfg_opt_name_ask_params     db "ask_params",0
  894. cfg_opt_name_imgfrom        db "imgfrom",0
  895. cfg_opt_name_syspath        db "syspath",0
  896.  
  897. cfg_opt_cmnt_resolution     db "# Graphic mode",0
  898. cfg_opt_cmnt_acpi           db "# ACPI settings",0xa, \
  899.                                "#   0: don't use",0xa, \
  900.                                "#   1: parse ACPI tables",0xa, \
  901.                                "#   2: + call _PIC method",0xa, \
  902.                                "#   3: + get APIC interrupts",0xa,0
  903. cfg_opt_cmnt_debug_print    db "# Duplicate debug output to the screen",0
  904. cfg_opt_cmnt_launcher_start db "# Start LAUNCHER app after kernel is loaded",0
  905. cfg_opt_cmnt_mtrr           db "# Configure MTRR's",0
  906. cfg_opt_cmnt_ask_params     db "# Interrupt booting to ask the user for boot", \
  907.                                " params",0
  908. cfg_opt_cmnt_imgfrom        db "# Where to load ramdisk image from",0
  909. cfg_opt_cmnt_syspath        db "# Path to /sys directory",0
  910.  
  911. msg_u4k_loaded            du "uefi32kos loaded",13,10,0
  912. msg_read_options          du "Read options from config file",13,10,0
  913. msg_load_kernel           du "Load kernel",13,10,0
  914. msg_load_ramdisk          du "Load ramdisk",13,10,0
  915. msg_load_devicesdat       du "Load DEVICES.DAT",13,10,0
  916. msg_alloc_devicesdat      du "Allocate memory for DEVICES.DAT",13,10,0
  917. msg_locate_gop_handlers   du "Locate GOP handlers",13,10,0
  918. msg_look_for_gop_handler  du "Look for GOP handler",13,10,0
  919. msg_query_handler         du "Query handler",13,10,0
  920. msg_query_vmode           du "Query vmode",13,10,0
  921. msg_vmode_found           du "Video mode found",13,10,0
  922. msg_look_for_rsdp         du "Look for RSDP",13,10,0
  923. msg_rsdp_found            du "RSDP found",13,10,0
  924. msg_acpi_tables_done      du "ACPI tables done",13,10,0
  925. msg_ask_for_params        du "Ask for params",13,10,0
  926. msg_set_graphic_mode      du "Set graphic mode",13,10,0
  927. msg_success               du "Success!",13,10,0
  928. msg_gop_buffer_size       du "GOP buffer size",13,10,0
  929. msg_opt_resolution        du "option resolution: ",0
  930. msg_error                 du "Error!",13,10,0
  931. msg_error_no_such_vmode   du "No such vmode",13,10,0
  932. msg_error_out_of_handlers du "Out of handlers",13,10,0
  933. msg_error_open_file       du "Error: can't open file ",0
  934. msg                       du 79 dup " ",13,10,0
  935.  
  936.  
  937. section '.data' data readable writeable
  938. efi_handle dd 0
  939. efi_table  dd 0
  940.  
  941. fb_base         dd 0
  942.  
  943. gop_buffer_size dd GOP_BUFFER_SIZE
  944. gop_handle      dd 0
  945. gop_interface   dd 0
  946. gop_info_size   dd 0
  947. gop_info        dd 0
  948.  
  949. lip_buffer_size dd LIP_BUFFER_SIZE
  950. lip_handle      dd 0
  951. lip_interface   dd 0
  952.  
  953. sfsp_interface  dd 0
  954.  
  955. pci_last_bus db 254
  956.  
  957. esp_root        dd ?
  958. file_handle     dd ?
  959. file_buffer_size dd FILE_BUFFER_SIZE-1  ; leave the last byte for \0
  960.  
  961. cfg_opt_used_resolution     db 0
  962. cfg_opt_used_acpi           db 0
  963. cfg_opt_used_debug_print    db 0
  964. cfg_opt_used_launcher_start db 0
  965. cfg_opt_used_mtrr           db 0
  966. cfg_opt_used_ask_params     db 0
  967. cfg_opt_used_imgfrom        db 0
  968. cfg_opt_used_syspath        db 0
  969.  
  970. cfg_opt_value_vmode          db 0
  971. cfg_opt_value_acpi           db 0
  972. cfg_opt_value_debug_print    db 0
  973. cfg_opt_value_launcher_start db 1
  974. cfg_opt_value_mtrr           db 0
  975. cfg_opt_value_ask_params     db 0
  976. cfg_opt_value_imgfrom        db RD_LOAD_FROM_MEMORY
  977. cfg_opt_value_syspath        db "/RD/1",0
  978.                              rb 20
  979.  
  980. memory_map_key  dd 0
  981. descriptor_size dd 0
  982. descriptor_ver  dd 0
  983. memory_map_size dd MEMORY_MAP_SIZE
  984.  
  985. efi_fs_info_id db EFI_FILE_SYSTEM_INFO_ID
  986. efi_fs_info_size dq sizeof.EFI_FILE_SYSTEM_INFO
  987. efi_fs_info EFI_FILE_SYSTEM_INFO
  988.  
  989. memory_map      dd ?
  990. gop_buffer      rd GOP_BUFFER_SIZE/4
  991. devicesdat_data dd 0xffffffff
  992. devicesdat_size dd 0x1000
  993. status dd ?
  994.  
  995. section '.reloc' fixups data discardable
  996.