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
21 .set main_load_addr, 0x100000
23 # make sure any BIOS call didn't re-enable interrupts
34 # enable A20 address line
37 # load the whole program into memory starting at 1MB
48 # enter protected mode for the first time
52 # inter-segment jump to set cs selector to segment 1
56 # set all data selectors to segment 2
82 gdt: # 0: null segment
85 # 1: code - base:0, lim:4g, G:4k, 32bit, avl, pres|app, dpl:0, type:code/non-conf/rd
88 # 2: data - base:0, lim:4g, G:4k, 32bit, avl, pres|app, dpl:0, type:data/rw
95 # trap gate 13: general protection fault
98 # type: trap, present, default
102 gpf_msg: .asciz "GP fault "
118 # use the same GDT above, will use data segment: 2
145 mainsz_msg: .asciz "Main program size: "
146 mainsz_msg2: .asciz " ("
147 mainsz_msg3: .asciz " sectors)\n"
156 movl $main_load_addr, dest_ptr
158 # calculate first sector
159 mov $_boot2_size, %eax
162 # add 1 to account for the boot sector
166 # calculate the first track (first_sect / sect_per_track)
167 movzxw sect_per_track, %ecx
171 # remainder is sector within track
174 mov $mainsz_msg, %esi
176 mov $_main_size, %eax
180 mov $mainsz_msg2, %esi
183 # calculate sector count
189 mov $mainsz_msg3, %esi
192 # read a whole track into the buffer (or partial first track)
194 movzxw sect_per_track, %ecx
199 # debug: print the first 32bits of the track
205 # copy to high memory
215 # other than the first track which might be partial, all the rest start from 0
222 # the BIOS might have enabled interrupts
225 # just in case we were loaded from floppy, turn all floppy motors off
236 rdtrk_msg: .asciz "Reading track: "
237 rdcyl_msg: .asciz " - cyl: "
238 rdhead_msg: .asciz " head: "
239 rdsect_msg: .asciz " start sect: "
240 rdlast_msg: .asciz " ... "
241 rdok_msg: .asciz "OK\n"
242 rdfail_msg: .asciz "failed\n"
244 read_retries: .short 0
246 .set drive_number, 0x7bec
248 # set es to the start of the destination buffer to allow reading in
249 # full 64k chunks if necessary
255 movw $3, read_retries
266 # calc cylinder (cur_track / num_heads) and head (cur_track % num_heads)
268 movzxw num_heads, %ecx
276 mov $rdhead_msg, %esi
285 # cylinder low byte at ch and high bits at cl[7, 6]
292 mov $rdsect_msg, %esi
296 mov $rdlast_msg, %esi
299 # start sector (1-based) in cl[0, 5]
305 # number of sectors in al
307 # call number (2) in ah
310 movb drive_number, %dl
314 # abort after 3 attempts
318 # error, reset controller and retry
324 mov $rdfail_msg, %esi
332 # reset es to 0 before returning
337 str_read_error: .asciz "Read error while reading track: "
340 mov $str_read_error, %esi
352 # better print routines, since we're not constrainted by the 512b of
378 # this looks retarded. in nasm: [ebx * 2 + edx]
379 mov %al, (%edx, %ebx, 2)
380 movb $7, 1(%edx, %ebx, 2)
389 # expects string pointer in esi
399 # expects number in eax
404 mov $numbuf + 16, %esi
434 # move 80 * 24 lines from b80a0 -> b8000
439 # clear last line (b8f00)
454 .set UART_DATA, 0x3f8
455 .set UART_LSTAT, 0x3fd
456 .set LST_TREG_EMPTY, 0x20
469 # wait until the transmit register is empty
472 and $LST_TREG_EMPTY, %al
483 ena20_msg: .asciz "A20 line enabled\n"
494 # keep trying ... we can't do anything useful without A20 anyway
501 # CF = 1 if A20 test fails (not enabled)
505 movl $0xbaadf00d, (%ebx)
506 movl $0xaabbcc42, (%edx)
507 subl $0xbaadf00d, (%ebx)
510 # enable A20 line through port 0x92 (fast A20)
512 mov $ena20_fast_msg, %esi
520 ena20_fast_msg: .asciz "Attempting fast A20 enable\n"
523 # enable A20 line through the keyboard controller
524 .set KBC_DATA_PORT, 0x60
525 .set KBC_CMD_PORT, 0x64
526 .set KBC_STATUS_PORT, 0x64
527 .set KBC_CMD_RD_OUTPORT, 0xd0
528 .set KBC_CMD_WR_OUTPORT, 0xd1
530 .set KBC_STAT_OUT_RDY, 0x01
531 .set KBC_STAT_IN_FULL, 0x02
534 mov $ena20_kbd_msg, %esi
538 mov $KBC_CMD_WR_OUTPORT, %al
539 out %al, $KBC_CMD_PORT
542 out %al, $KBC_DATA_PORT
545 ena20_kbd_msg: .asciz "Attempting KBD A20 enable\n"
547 # wait until the keyboard controller is ready to accept another byte
549 in $KBC_STATUS_PORT, %al
550 and $KBC_STAT_IN_FULL, %al
557 # this is not boot loader code. It's called later on by the main kernel
558 # code in 32bit protected mode. It's placed here because it needs to be
559 # located in base memory as it returns and runs in real mode.
562 # place to save the protected mode IDTR pseudo-descriptor
563 # with sidt, so that it can be restored before returning
568 # real mode IDTR pseudo-descriptor pointing to the IVT at addr 0
576 # drop back to unreal mode to call 16bit interrupt
583 # save protected mode IDTR and replace it with the real mode vectors
587 # modify the int instruction do this here before the
588 # cs-load jumps, to let them flush the instruction cache
593 # long jump to load code selector for 16bit code (6)
603 0: # zero data segments
610 # load registers from the int86regs struct
617 # call 16bit interrupt
627 # re-enable protection
631 # long jump to load code selector for 32bit code (1)
635 # set data selector (2) to all segment regs
642 # restore 32bit interrupt descriptor table
650 # buffer used by the track loader ... to load tracks.