Subversion Repositories Kolibri OS

Rev

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