Subversion Repositories Kolibri OS

Rev

Rev 9253 | Rev 9958 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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