Subversion Repositories Kolibri OS

Rev

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

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