Subversion Repositories Kolibri OS

Rev

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

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