Subversion Repositories Kolibri OS

Rev

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