Subversion Repositories Kolibri OS

Rev

Rev 9958 | Details | Compare with Previous | Last modification | View Log | RSS feed

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