1 # pcboot - bootable PC demo/game kernel
2 # Copyright (C) 2018 John Tsiombikas <nuclear@member.fsf.org>
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY, without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <https://www.gnu.org/licenses/>.
17 # this is the second-stage boot loader
18 # plus some other code that needs to run below 1mb (int86 implementation).
23 .set main_load_addr, 0x100000
25 # make sure any BIOS call didn't re-enable interrupts
36 # enable A20 address line
39 # detect available memory
42 # load the whole program into memory starting at 1MB
53 # enter protected mode for the first time
57 # inter-segment jump to set cs selector to segment 1
61 # set all data selectors to segment 2
87 gdt: # 0: null segment
90 # 1: code - base:0, lim:4g, G:4k, 32bit, avl, pres|app, dpl:0, type:code/non-conf/rd
93 # 2: data - base:0, lim:4g, G:4k, 32bit, avl, pres|app, dpl:0, type:data/rw
100 # trap gate 13: general protection fault
103 # type: trap, present, default
107 gpf_msg: .asciz "GP fault "
123 # use the same GDT above, will use data segment: 2
150 mainsz_msg: .asciz "Main program size: "
151 mainsz_msg2: .asciz " ("
152 mainsz_msg3: .asciz " sectors)\n"
161 movl $main_load_addr, dest_ptr
163 # calculate first sector
164 mov $_boot2_size, %eax
167 # add 1 to account for the boot sector
171 # calculate the first track (first_sect / sect_per_track)
172 movzxw sect_per_track, %ecx
176 # remainder is sector within track
179 mov $mainsz_msg, %esi
181 mov $_main_size, %eax
185 mov $mainsz_msg2, %esi
188 # calculate sector count
194 mov $mainsz_msg3, %esi
197 # read a whole track into the buffer (or partial first track)
199 movzxw sect_per_track, %ecx
204 # debug: print the first 32bits of the track
210 # copy to high memory
220 # other than the first track which might be partial, all the rest start from 0
227 # the BIOS might have enabled interrupts
230 # just in case we were loaded from floppy, turn all floppy motors off
241 rdtrk_msg: .asciz "Reading track: "
242 rdcyl_msg: .asciz " - cyl: "
243 rdhead_msg: .asciz " head: "
244 rdsect_msg: .asciz " start sect: "
245 rdlast_msg: .asciz " ... "
246 rdok_msg: .asciz "OK\n"
247 rdfail_msg: .asciz "failed\n"
249 read_retries: .short 0
251 .set drive_number, 0x7bec
253 # set es to the start of the destination buffer to allow reading in
254 # full 64k chunks if necessary
260 movw $3, read_retries
271 # calc cylinder (cur_track / num_heads) and head (cur_track % num_heads)
273 movzxw num_heads, %ecx
281 mov $rdhead_msg, %esi
290 # cylinder low byte at ch and high bits at cl[7, 6]
297 mov $rdsect_msg, %esi
301 mov $rdlast_msg, %esi
304 # start sector (1-based) in cl[0, 5]
310 # number of sectors in al
312 # call number (2) in ah
315 movb drive_number, %dl
319 # abort after 3 attempts
323 # error, reset controller and retry
329 mov $rdfail_msg, %esi
337 # reset es to 0 before returning
342 str_read_error: .asciz "Read error while reading track: "
345 mov $str_read_error, %esi
357 # better print routines, since we're not constrainted by the 512b of
383 # this looks retarded. in nasm: [ebx * 2 + edx]
384 mov %al, (%edx, %ebx, 2)
385 movb $7, 1(%edx, %ebx, 2)
394 # expects string pointer in esi
404 # expects number in eax
409 mov $numbuf + 16, %esi
439 # move 80 * 24 lines from b80a0 -> b8000
444 # clear last line (b8f00)
459 .set UART_DATA, 0x3f8
460 .set UART_LSTAT, 0x3fd
461 .set LST_TREG_EMPTY, 0x20
474 # wait until the transmit register is empty
477 and $LST_TREG_EMPTY, %al
488 ena20_msg: .asciz "A20 line enabled\n"
499 # keep trying ... we can't do anything useful without A20 anyway
506 # CF = 1 if A20 test fails (not enabled)
510 movl $0xbaadf00d, (%ebx)
511 movl $0xaabbcc42, (%edx)
512 subl $0xbaadf00d, (%ebx)
515 # enable A20 line through port 0x92 (fast A20)
517 mov $ena20_fast_msg, %esi
525 ena20_fast_msg: .asciz "Attempting fast A20 enable\n"
528 # enable A20 line through the keyboard controller
529 .set KBC_DATA_PORT, 0x60
530 .set KBC_CMD_PORT, 0x64
531 .set KBC_STATUS_PORT, 0x64
532 .set KBC_CMD_RD_OUTPORT, 0xd0
533 .set KBC_CMD_WR_OUTPORT, 0xd1
535 .set KBC_STAT_OUT_RDY, 0x01
536 .set KBC_STAT_IN_FULL, 0x02
539 mov $ena20_kbd_msg, %esi
543 mov $KBC_CMD_WR_OUTPORT, %al
544 out %al, $KBC_CMD_PORT
547 out %al, $KBC_DATA_PORT
550 ena20_kbd_msg: .asciz "Attempting KBD A20 enable\n"
552 # wait until the keyboard controller is ready to accept another byte
554 in $KBC_STATUS_PORT, %al
555 and $KBC_STAT_IN_FULL, %al
563 mov $memdet_e820_msg, %esi
567 mov $rdfail_msg, %esi
570 mov $memdet_e801_msg, %esi
574 mov $rdfail_msg, %esi
577 mov $memdet_88_msg, %esi
581 mov $rdfail_msg, %esi
585 mov $memdet_fail_msg, %esi
595 memdet_fail_msg: .ascii "Failed to detect available memory!\n"
596 .ascii "Please file a bug report: https://github.com/jtsiomb/pcboot/issues\n"
597 .asciz " or contact me through email: nuclear@member.fsf.org\n"
598 memdet_e820_msg: .asciz "Detecting RAM (BIOS 15h/0xe820)... "
599 memdet_e801_msg: .asciz "Detecting RAM (BIOS 15h/0xe801)... "
600 memdet_88_msg: .asciz "Detecting RAM (BIOS 15h/0x88, max 64mb)... "
602 # detect extended memory using BIOS call 15h/e820
604 movl $0, boot_mem_map_size
608 mov $0x534d4150, %edx
615 cmp $0x534d4150, %eax
618 # skip areas starting above 4GB as we won't be able to use them
622 # only care for type 1 (usable ram), otherwise ignore
627 mov $boot_mem_map, %esi
628 mov boot_mem_map_size, %ebp
629 # again, that's [ebp * 8 + esi]
630 mov %eax, (%esi,%ebp,8)
632 # skip areas with 0 size (also clamp size to 4gb)
636 # high part is non-zero, make low part ffffffff
642 # if both high and low parts are zero, ignore
647 0: mov %eax, 4(%esi,%ebp,8)
648 incl boot_mem_map_size
651 # terminate the loop if ebx was reset to 0
661 # if size > 0, then it's not a failure, just the end
662 cmpl $0, boot_mem_map_size
669 # detect extended memory using BIOS call 15h/e801
671 mov $boot_mem_map, %esi
672 mov boot_mem_map_size, %ebp
688 0: movl $0x100000, (%esi)
690 # first size is in KB, convert to bytes
693 # overflow means it's >4GB, clamp to 4GB
694 mov $0xffffffff, %eax
696 incl boot_mem_map_size
699 movl $0x1000000, 8(%esi)
701 # second size is in 64kb blocks, convert to bytes
704 # overflow means it's >4GB, clamp to 4GB
705 mov $0xffffffff, %eax
706 0: mov %eax, 12(%esi)
707 incl boot_mem_map_size
716 # reportedly some BIOS implementations fail to clear CF on success
725 # ax has size in KB, convert to bytes in eax
729 mov $boot_mem_map, %esi
730 movl $0x100000, (%esi)
733 movl $1, boot_mem_map_size
742 .global boot_mem_map_size
743 boot_mem_map_size: .long 0
745 boot_mem_map: .space 128
748 # this is not boot loader code. It's called later on by the main kernel
749 # code in 32bit protected mode. It's placed here because it needs to be
750 # located in base memory as it returns and runs in real mode.
753 # place to save the protected mode IDTR pseudo-descriptor
754 # with sidt, so that it can be restored before returning
759 # real mode IDTR pseudo-descriptor pointing to the IVT at addr 0
767 # drop back to unreal mode to call 16bit interrupt
774 # save protected mode IDTR and replace it with the real mode vectors
778 # modify the int instruction do this here before the
779 # cs-load jumps, to let them flush the instruction cache
784 # long jump to load code selector for 16bit code (6)
794 0: # zero data segments
801 # load registers from the int86regs struct
808 # call 16bit interrupt
818 # re-enable protection
822 # long jump to load code selector for 32bit code (1)
826 # set data selector (2) to all segment regs
833 # restore 32bit interrupt descriptor table
841 # buffer used by the track loader ... to load tracks.
844 .global low_mem_buffer