Subversion Repositories Kolibri OS

Rev

Rev 8150 | Rev 8220 | 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 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
8206 dunkaist 642
        cmp     ecx, MAX_MEMMAP_BLOCKS
643
        jbe     @f
644
        mov     ecx, MAX_MEMMAP_BLOCKS
645
@@:
646
        xor     eax, eax
647
        mov     [eax+BOOT_LO.memmap_block_cnt], 0
8150 dunkaist 648
        mov     esi, [memory_map]
649
.next_descr:
650
        call    add_uefi_memmap
651
        add     esi, [descriptor_size]
652
        add     edi, sizeof.e820entry
653
        dec     ecx
654
        jnz     .next_descr
655
 
656
        ; kernel
657
;        eficall BootServices, AllocatePages, EFI_RESERVED_MEMORY_TYPE, \
658
;                450000/0x1000, EFI_ALLOCATE_ADDRESS
659
 
660
        ; ramdisk
661
;        eficall BootServices, AllocatePages, EFI_RESERVED_MEMORY_TYPE, \
662
;                2880*512/0x1000, EFI_ALLOCATE_ADDRESS
663
 
664
        mov     eax, [efi_table]
665
        mov     eax, [eax+EFI_SYSTEM_TABLE.BootServices]
666
        ccall   [eax+EFI_BOOT_SERVICES.ExitBootServices], [efi_handle], \
667
                [memory_map_key]
668
        test    eax, eax
669
        jnz     .error
670
 
671
 
672
        cli
673
 
674
        xor     edx, edx
675
        xor     esi, esi
676
        mov     [esi+BOOT_LO.bpp], 32
677
        mov     [esi+BOOT_LO.vesa_mode], dx
678
        mov     [esi+BOOT_LO.bank_switch], edx
679
        mov     edi, [fb_base]
680
        mov     [esi+BOOT_LO.lfb], edi
681
 
682
        movzx   eax, [cfg_opt_value_mtrr]
683
        mov     [esi+BOOT_LO.mtrr], al
684
 
685
        movzx   eax, [cfg_opt_value_launcher_start]
686
        mov     [esi+BOOT_LO.launcher_start], al
687
 
688
        movzx   eax, [cfg_opt_value_debug_print]
689
        mov     [esi+BOOT_LO.debug_print], al
690
 
691
        mov     [esi+BOOT_LO.dma], dl
692
;        mov     qword[esi+BOOT_LO.pci_data], 0
693
        mov     [esi+BOOT_LO.apm_entry], edx
694
        mov     [esi+BOOT_LO.apm_version], dx
695
        mov     [esi+BOOT_LO.apm_flags], dx
696
        mov     [esi+BOOT_LO.apm_code_32], dx
697
        mov     [esi+BOOT_LO.apm_code_16], dx
698
        mov     [esi+BOOT_LO.apm_data_16], dx
699
        mov     [esi+BOOT_LO.bios_hd_cnt], dl
700
 
701
        movzx   eax, [cfg_opt_value_imgfrom]
702
        mov     [esi+BOOT_LO.rd_load_from], al
703
 
704
        mov     eax, dword[devicesdat_size]
705
        mov     [edx+BOOT_LO.devicesdat_size], eax
706
        mov     eax, dword[devicesdat_data]
707
        mov     [edx+BOOT_LO.devicesdat_data], eax
708
 
709
        mov     esi, cfg_opt_value_syspath
710
        mov     edi, BOOT_LO.syspath
711
        mov     ecx, 0x17
712
        rep movsb
713
 
714
        lgdt    [cs:GDTR]
715
 
716
        mov     ax, DATA_32_SELECTOR
717
        mov     ds, ax
718
        mov     es, ax
719
        mov     fs, ax
720
        mov     gs, ax
721
        mov     ss, ax
722
 
723
        push    CODE_32_SELECTOR
724
        lea     eax, [.next]
725
        push    eax
726
        retf
727
 
728
.next:
729
        mov     eax, cr0
730
        and     eax, not CR0_PG
731
        mov     cr0, eax
732
 
733
        mov     eax, cr4
734
        and     eax, not CR4_PAE
735
        mov     cr4, eax
736
 
737
        push    KERNEL_BASE
738
        retn
739
 
740
.error:
741
        mov     esi, [efi_table]
742
        mov     eax, [esi+EFI_SYSTEM_TABLE.ConOut]
743
        ccall   [eax+EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString], eax, \
744
                msg_error
745
        jmp     $
746
 
747
; linux/arch/x86/platform/efi/efi.c
748
; do_add_efi_memmap
749
proc add_uefi_memmap
750
locals
751
  pew dd ?
752
endl
753
        pushad
754
 
755
        mov     edx, [esi+EFI_MEMORY_DESCRIPTOR.Type]
756
 
757
        mov     eax, [esi+EFI_MEMORY_DESCRIPTOR.PhysicalStart.lo] ; FIXME hi
758
        mov     [edi+e820entry.addr], eax
759
 
760
        mov     eax, [esi+EFI_MEMORY_DESCRIPTOR.NumberOfPages]
761
        shl     eax, 12
762
        mov     [edi+e820entry.size], eax
763
 
764
 
765
        cmp     edx, EFI_LOADER_CODE
766
        jz      .case0
767
        cmp     edx, EFI_LOADER_DATA
768
        jz      .case0
769
        cmp     edx, EFI_BOOT_SERVICES_CODE
770
        jz      .case0
771
        cmp     edx, EFI_BOOT_SERVICES_DATA
772
        jz      .case0
773
        cmp     edx, EFI_CONVENTIONAL_MEMORY
774
        jz      .case0
775
        cmp     edx, EFI_ACPI_RECLAIM_MEMORY
776
        jz      .case1
777
        cmp     edx, EFI_ACPI_MEMORY_NVS
778
        jz      .case2
779
        cmp     edx, EFI_UNUSABLE_MEMORY
780
        jz      .case3
781
        cmp     edx, EFI_PERSISTENT_MEMORY
782
        jz      .case4
783
        jmp     .default
784
 
785
.case0:
786
        test    [esi+EFI_MEMORY_DESCRIPTOR.Attribute.lo], EFI_MEMORY_WB
787
        jz      @f
788
        mov     eax, E820_RAM
789
        jmp     .done
790
    @@:
791
        mov     eax, E820_RESERVED
792
        jmp     .done
793
.case1:
794
        mov     eax, E820_ACPI
795
        jmp     .done
796
.case2:
797
        mov     eax, E820_NVS
798
        jmp     .done
799
.case3:
800
        mov     eax, E820_UNUSABLE
801
        jmp     .done
802
.case4:
803
        mov     eax, E820_PMEM
804
        jmp     .done
805
.default:
806
        mov     eax, E820_RESERVED
807
        jmp     .done
808
 
809
.done:
810
        mov     [edi+e820entry.type], eax
811
 
812
        mov     eax, BOOT_LO.memmap_block_cnt
813
        inc     word[eax]
814
 
815
        popad
816
        ret
817
endp
818
 
819
 
820
proc num2dec
821
        pushad
822
 
823
        xor     ecx, ecx
824
        mov     ebx, 10
825
.next_digit:
826
        xor     edx, edx
827
        div     ebx
828
        push    edx
829
        inc     ecx
830
        test    eax, eax
831
        jnz     .next_digit
832
 
833
.next_char:
834
        pop     eax
835
        add     eax, '0'
836
        stosw
837
        loop    .next_char
838
 
839
        popad
840
        ret
841
endp
842
 
843
 
844
proc num2hex
845
        pushad
846
 
847
        xchg    edx, eax
848
        mov     ecx, 8
849
.next_tetra:
850
        rol     edx, 4
851
        movzx   eax, dl
852
        and     eax, 0x0f
853
        movzx   eax, byte[hex+eax]
854
        stosw
855
        loop    .next_tetra
856
 
857
        popad
858
        ret
859
endp
860
 
861
 
862
hex db '0123456789ABCDEF'
863
 
864
proc clearbuf
865
        pushad
866
        mov     eax, 0x0020
867
        mov     ecx, 79
868
        mov     edi, msg
869
        rep stosw
870
        popad
871
        ret
872
endp
873
 
874
section '.data' data readable writeable
875
efi_handle dd 0
876
efi_table  dd 0
877
 
878
GDTR:
879
        dw 3*8-1
880
        dq GDT
881
GDT:
882
        dw 0, 0, 0, 0
883
        dw 0FFFFh,0,9A00h,0CFh          ; 32-bit code
884
        dw 0FFFFh,0,9200h,0CFh          ; flat data
885
 
886
 
887
fb_base         dd 0
888
 
889
gopuuid         db EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID
890
gop_buffer_size dd GOP_BUFFER_SIZE
891
gop_handle      dd 0
892
gop_interface   dd 0
893
gop_info_size   dd 0
894
gop_info        dd 0
895
 
896
lipuuid         db EFI_LOADED_IMAGE_PROTOCOL_GUID
897
lip_buffer_size dd LIP_BUFFER_SIZE
898
lip_handle      dd 0
899
lip_interface   dd 0
900
 
901
sfspguid        db EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID
902
sfsp_interface  dd 0
903
 
904
esp_root        dd ?
905
file_handle     dd ?
906
file_name       du "\EFI\KOLIBRIOS\KOLIBRI.INI",0
907
kernel_name     du "\EFI\KOLIBRIOS\KOLIBRI.KRN",0
908
ramdisk_name    du "\EFI\KOLIBRIOS\KOLIBRI.IMG",0
909
devicesdat_name du "\EFI\KOLIBRIOS\DEVICES.DAT",0
910
file_buffer_size dd FILE_BUFFER_SIZE-1  ; leave the last byte for \0
911
 
912
config_options  dd cfg_opt_name_resolution, cfg_opt_func_resolution, \
913
                   cfg_opt_cmnt_resolution, \
914
                   cfg_opt_name_acpi,  cfg_opt_func_acpi, cfg_opt_cmnt_acpi, \
915
                   cfg_opt_name_debug_print, cfg_opt_func_debug_print, \
916
                   cfg_opt_cmnt_debug_print, \
917
                   cfg_opt_name_launcher_start, cfg_opt_func_launcher_start, \
918
                   cfg_opt_cmnt_launcher_start, \
919
                   cfg_opt_name_mtrr,  cfg_opt_func_mtrr, cfg_opt_cmnt_mtrr, \
920
                   cfg_opt_name_ask_params,  cfg_opt_func_ask_params, \
921
                   cfg_opt_cmnt_ask_params, \
922
                   cfg_opt_name_imgfrom, cfg_opt_func_imgfrom, \
923
                   cfg_opt_cmnt_imgfrom, \
924
                   cfg_opt_name_syspath, cfg_opt_func_syspath, \
925
                   cfg_opt_cmnt_syspath, \
926
 
927
 
928
cfg_opt_name_resolution     db "resolution",0
929
cfg_opt_name_acpi           db "acpi",0
930
cfg_opt_name_debug_print    db "debug_print",0
931
cfg_opt_name_launcher_start db "launcher_start",0
932
cfg_opt_name_mtrr           db "mtrr",0
933
cfg_opt_name_ask_params     db "ask_params",0
934
cfg_opt_name_imgfrom        db "imgfrom",0
935
cfg_opt_name_syspath        db "syspath",0
936
 
937
cfg_opt_cmnt_resolution     db "# Graphic mode",0
938
cfg_opt_cmnt_acpi           db "# ACPI settings",0xa, \
939
                               "#   0: don't use",0xa, \
940
                               "#   1: parse ACPI tables",0xa, \
941
                               "#   2: + call _PIC method",0xa, \
942
                               "#   3: + get APIC interrupts",0xa,0
943
cfg_opt_cmnt_debug_print    db "# Duplicate debug output to the screen",0
944
cfg_opt_cmnt_launcher_start db "# Start LAUNCHER app after kernel is loaded",0
945
cfg_opt_cmnt_mtrr           db "# Configure MTRR's",0
946
cfg_opt_cmnt_ask_params     db "# Interrupt booting to ask the user for boot", \
947
                               " params",0
948
cfg_opt_cmnt_imgfrom        db "# Where to load ramdisk image from",0
949
cfg_opt_cmnt_syspath        db "# Path to /sys directory",0
950
 
951
cfg_opt_used_resolution     db 0
952
cfg_opt_used_acpi           db 0
953
cfg_opt_used_debug_print    db 0
954
cfg_opt_used_launcher_start db 0
955
cfg_opt_used_mtrr           db 0
956
cfg_opt_used_ask_params     db 0
957
cfg_opt_used_imgfrom        db 0
958
cfg_opt_used_syspath        db 0
959
 
960
cfg_opt_value_vmode          db 0
961
cfg_opt_value_acpi           db 0
962
cfg_opt_value_debug_print    db 0
963
cfg_opt_value_launcher_start db 1
964
cfg_opt_value_mtrr           db 0
965
cfg_opt_value_ask_params     db 0
966
cfg_opt_value_imgfrom        db RD_LOAD_FROM_MEMORY
967
cfg_opt_value_syspath        db "/RD/1",0
968
                             rb 20
969
 
970
memory_map_key  dd 0
971
descriptor_size dd 0
972
descriptor_ver  dd 0
973
memory_map_size dd MEMORY_MAP_SIZE
974
 
975
msg_u4k_loaded            du "uefi32kos loaded",13,10,0
976
msg_read_options          du "Read options from config file",13,10,0
977
msg_load_kernel           du "Load kernel",13,10,0
978
msg_load_ramdisk          du "Load ramdisk",13,10,0
979
msg_load_devicesdat       du "Load DEVICES.DAT",13,10,0
980
msg_alloc_devicesdat      du "Allocate memory for DEVICES.DAT",13,10,0
981
msg_locate_gop_handlers   du "Locate GOP handlers",13,10,0
982
msg_look_for_gop_handler  du "Look for GOP handler",13,10,0
983
msg_query_handler         du "Query handler",13,10,0
984
msg_query_vmode           du "Query vmode",13,10,0
985
msg_vmode_found           du "Video mode found",13,10,0
986
msg_look_for_rsdp         du "Look for RSDP",13,10,0
987
msg_rsdp_found            du "RSDP found",13,10,0
988
msg_acpi_tables_done      du "ACPI tables done",13,10,0
989
msg_ask_for_params        du "Ask for params",13,10,0
990
msg_set_graphic_mode      du "Set graphic mode",13,10,0
991
msg_success               du "Success!",13,10,0
992
msg_gop_buffer_size       du "GOP buffer size",13,10,0
993
msg_opt_resolution        du "option resolution: ",0
994
msg_error                 du "Error!",13,10,0
995
msg_error_no_such_vmode   du "No such vmode",13,10,0
996
msg_error_out_of_handlers du "Out of handlers",13,10,0
997
msg_error_open_file       du "Error: can't open file ",0
998
msg                       du 79 dup " ",13,10,0
999
 
1000
efi_fs_info_id db EFI_FILE_SYSTEM_INFO_ID
1001
efi_fs_info_size dq sizeof.EFI_FILE_SYSTEM_INFO
1002
efi_fs_info EFI_FILE_SYSTEM_INFO
1003
 
1004
memory_map      dd ?
1005
gop_buffer      rd GOP_BUFFER_SIZE/4
1006
devicesdat_data dd 0xffffffff
1007
devicesdat_size dd 0x1000
1008
status dd ?
1009
 
1010
section '.reloc' fixups data discardable