8 extern boot_mem_map_size
19 mov fs, ax ; this will store the original real mode segment
21 ; modify the return to real mode jump segment
22 mov [.jmpcs16 + 3], ax
27 ; check for VM86 and abort
41 ; calculate GDT linear address
48 ; set tmp segment bases to match the linear address of our current seg
52 mov word [gdt + 18h + 2], ax ; tmp pm code base
53 mov word [gdt + 20h + 2], ax ; tmp pm data base
54 mov word [gdt + 28h + 2], ax ; ret-to-realmode code
56 mov byte [gdt + 18h + 4], al
57 mov byte [gdt + 20h + 4], al
58 mov byte [gdt + 28h + 4], al
72 .pm: mov ax, 20h ; tmp data selector
74 mov ax, 10h ; dest data selector
77 ; copy main program high
79 mov esi, _ldr_main_start
81 mov ecx, _main_size + 3
86 mov eax, [mem_map_size]
87 mov [es:boot_mem_map_size], eax
90 mov ecx, 32 ; 128 bytes
108 ; return to real mode
116 jmp 42h:.loadcs16 ; 42 seg is modifed at the start
126 dbg_inpm db 'DBG: in pmode',10,0
127 dbg_retrm db 'DBG: returning to real mode...',10,0
129 str_errvm86 db 'Error: memory manager detected! Stop it and try again (e.g. emm386 off)',10,0
130 str_enterpm db 'Entering 32bit protected mode ...',10,0
137 call printstr ; print "Enable A20 line ... "
149 .done: mov si, .okstr
153 .infomsg db 'Enable A20 line:',0
154 .failstr db ' failed.',10,0
155 .okstr db ' success.',10,0
157 ; CF = 1 if A20 test fails (not enabled)
176 .done: mov [ds:si], dl
189 .info db ' fast ...',0
191 KBC_DATA_PORT equ 0x60
192 KBC_CMD_PORT equ 0x64
193 KBC_STATUS_PORT equ 0x64
194 KBC_CMD_WR_OUTPORT equ 0xd1
196 KBC_STAT_IN_FULL equ 2
202 mov al, KBC_CMD_WR_OUTPORT
206 out KBC_DATA_PORT, al
208 .info db ' kbd ...',0
211 in al, KBC_STATUS_PORT
212 and al, KBC_STAT_IN_FULL
217 ; ---------------------- memory detection -----------------------
220 mov si, memdet_e820_msg
227 mov si, memdet_e801_msg
234 mov esi, memdet_88_msg
241 mov esi, memdet_cmos_msg
248 mov si, memdet_fail_msg
257 str_fail db 'failed',10,0
258 memdet_fail_msg db 'Failed to detect available memory!',10,0
259 memdet_e820_msg db 'Detecting RAM (BIOS 15h/0xe820)... ',0
260 memdet_e801_msg db 'Detecting RAM (BIOS 15h/0xe801)... ',0
261 memdet_88_msg db 'Detecting RAM (BIOS 15h/0x88, max 64mb)... ',0
262 memdet_cmos_msg db 'Detecting RAM (CMOS)...',0
264 ; detect extended memory using BIOS call 15h/e820
266 mov dword [mem_map_size], 0
280 ; skip areas starting above 4GB as we won't be able to use them
281 cmp dword [di + 4], 0
284 ; only care for type 1 (usable ram), otherwise ignore
285 cmp dword [di + 16], 1
290 mov ebp, [mem_map_size]
291 mov [ebp * 8 + esi], eax
293 ; skip areas with 0 size (also clamp size to 4gb)
295 cmp dword [edi + 12], 0
297 ; high part is non-zero, make low part ffffffff
303 ; if both high and low parts are zero, ignore
308 .skiph0:mov [ebp * 8 + esi + 4], eax
309 inc dword [mem_map_size]
312 ; terminate the loop if ebx was reset to 0
320 .fail: ; if size > 0, then it's not a failure, just the end
321 cmp dword [mem_map_size], 0
326 .buffer times 32 db 0
328 ; detect extended memory using BIOS call 15h/e801
331 mov ebp, [mem_map_size]
347 .foo1: mov dword [si], 100000h
349 ; first size is in KB, convert to bytes
352 ; overflow means it's >4GB, clamp to 4GB
354 .foo2: mov [si + 4], eax
355 inc dword [mem_map_size]
358 mov dword [si + 8], 1000000h
360 ; second size is in 64kb blocks, convert to bytes
363 ; overflow means it's >4GB, clamp to 4GB
365 .foo3: mov [si + 12], eax
366 inc dword [mem_map_size]
375 ; reportedly some BIOS implementations fail to clear CF on success
384 ; ax has size in KB, convert to bytes in eax
389 mov dword [si], 100000h
392 mov dword [mem_map_size], 1
410 ; ax has size in KB, convert to bytes in eax
415 mov dword [si], 100000h
417 mov dword [mem_map_size], 1
426 mem_map times 128 db 0
429 ; ----------------------- serial console ------------------------
433 mov dx, UART_BASE + 3 ; LCTL
436 mov dx, UART_BASE ; DIVLO
437 mov al, UART_DIVISOR & 0xff
440 mov al, UART_DIVISOR >> 8
442 mov dx, UART_BASE + 3 ; LCTL
446 mov al, 0xb ; DTR/RTS/OUT2
464 .nolf: call ser_putchar
472 cmp al, 10 ; check for line-feed and insert CR before it
481 .nolf: call ser_putchar
501 call ser_putchar_pmode
503 .nolf: call ser_putchar_pmode
509 enterpm dd 0xbad00d ; space for linear address for far jump to pmode
510 enterpm_sel dw 8 ; selector for far jump to protected mode
512 gdt_lim dw 47 ; GDT limit
513 gdt_base dd 0xbadf00d ; space for GDT linear address
516 gdt: ; 0: null segment
519 ; 1: code - 0/lim:4g, G:4k, 32bit, avl, pres|app, dpl:0, type:code/non-conf/rd (sel: 8)
522 ; 2: data - 0/lim:4g, G:4k, 32bit, avl, pres|app, dpl:0, type:data/rw (sel: 10h)
525 ; 3: tmp code (will set base before entering pmode) (sel: 18h)
528 ; 4: tmp data (will set base before entering pmode) (sel: 20h)
531 ; 5: return to real-mode 16bit code segment (sel: 28h)
536 ; vi:set ts=8 sts=8 sw=8 ft=nasm: