Subversion Repositories Kolibri OS

Rev

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