2022-11-17 22:37:30 +01:00
|
|
|
; Copyright 2022 Mattia Giambirtone & Contributors
|
|
|
|
;
|
|
|
|
; Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
; you may not use this file except in compliance with the License.
|
|
|
|
; You may obtain a copy of the License at
|
|
|
|
;
|
|
|
|
; http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
;
|
|
|
|
; Unless required by applicable law or agreed to in writing, software
|
|
|
|
; distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
; See the License for the specific language governing permissions and
|
|
|
|
; limitations under the License.
|
|
|
|
|
2022-11-15 15:47:33 +01:00
|
|
|
; Definition of the MBR (Master Boot Record). This is basically our bootloader and
|
|
|
|
; is located in the first 512 bytes of the drive we're booting from. From here, we
|
|
|
|
; do some basic setup and then call into the kernel
|
|
|
|
|
|
|
|
[org 0x7c00] ; Address where the code expects to be loaded in. The BIOS always loads us here
|
2022-11-18 22:19:40 +01:00
|
|
|
[bits 16] ; All x86 CPUs start in 16 bit (aka "real") mode, so we tell nasm to emit 16-bit code
|
2022-11-15 15:47:33 +01:00
|
|
|
|
2022-11-18 22:19:40 +01:00
|
|
|
kernel_offset: equ 0x1000
|
|
|
|
reserved_sectors: equ 4
|
2022-11-15 15:47:33 +01:00
|
|
|
|
2022-11-18 17:24:45 +01:00
|
|
|
; This isn't needed inside the qemu emulator, but
|
|
|
|
; real hardware is unlikely to start up with the
|
|
|
|
; segment registers zeroed, so we do it here
|
|
|
|
mov ax, 0
|
|
|
|
mov ds, ax
|
|
|
|
mov ss, ax
|
|
|
|
mov es, ax
|
|
|
|
xor ax, ax
|
|
|
|
|
2022-11-18 22:19:40 +01:00
|
|
|
; We save the value of the current boot drive
|
|
|
|
mov [boot_drive], dl
|
|
|
|
|
2022-11-16 15:25:53 +01:00
|
|
|
; Now we setup the stack by setting the
|
2022-11-18 17:24:45 +01:00
|
|
|
; base pointer to a location that's far
|
|
|
|
; enough from where the code for the BIOS
|
|
|
|
; is located
|
|
|
|
mov sp, 0x9000
|
|
|
|
mov bp, sp
|
2022-11-16 15:25:53 +01:00
|
|
|
call bios_cls
|
2022-11-18 22:19:40 +01:00
|
|
|
mov si, loading_stage2_msg
|
2022-11-16 15:25:53 +01:00
|
|
|
call bios_println
|
2022-11-18 22:19:40 +01:00
|
|
|
mov bx, stage2
|
|
|
|
mov cl, 2
|
|
|
|
mov dh, reserved_sectors
|
|
|
|
mov dl, [boot_drive]
|
|
|
|
call load_disk
|
|
|
|
jmp stage2
|
2022-11-15 15:47:33 +01:00
|
|
|
|
|
|
|
|
2022-11-18 22:19:40 +01:00
|
|
|
; Variables needed in the boot
|
|
|
|
; sector
|
|
|
|
boot_drive: db 0
|
|
|
|
loading_stage2_msg: db "TSBL - INFO: Stage 1 loaded, loading stage 2", 0
|
|
|
|
|
|
|
|
; We include just the bare minimum needed to load the
|
|
|
|
; second stage of our bootloader
|
2022-11-16 20:24:23 +01:00
|
|
|
%include "src/boot/util/disk.s"
|
|
|
|
%include "src/boot/util/io.s"
|
2022-11-15 15:47:33 +01:00
|
|
|
|
2022-11-18 22:19:40 +01:00
|
|
|
; Padding and magic number
|
|
|
|
times 510 - ($ - $$) db 0
|
|
|
|
dw 0xaa55
|
2022-11-17 12:42:45 +01:00
|
|
|
|
2022-11-18 22:19:40 +01:00
|
|
|
[bits 16]
|
|
|
|
stage2:
|
|
|
|
; Here we're no longer limited by the size of the
|
|
|
|
; boot sector, so we can perform the more complex
|
|
|
|
; part of the boot process
|
|
|
|
mov si, stage2_loaded_msg
|
|
|
|
call bios_println
|
|
|
|
call load_kernel
|
|
|
|
call switch_to_protected_mode
|
2022-11-17 12:42:45 +01:00
|
|
|
|
|
|
|
|
2022-11-18 22:19:40 +01:00
|
|
|
load_kernel:
|
|
|
|
; Loads the kernel into memory
|
2022-11-16 15:25:53 +01:00
|
|
|
mov bx, kernel_offset
|
2022-11-18 22:19:40 +01:00
|
|
|
mov cl, reserved_sectors + 1
|
2022-11-18 02:40:46 +01:00
|
|
|
mov dh, 4
|
2022-11-16 15:25:53 +01:00
|
|
|
mov dl, [boot_drive]
|
|
|
|
call load_disk
|
2022-11-18 22:19:40 +01:00
|
|
|
mov si, kernel_loaded_msg
|
|
|
|
call bios_println
|
2022-11-16 15:25:53 +01:00
|
|
|
ret
|
2022-11-15 15:47:33 +01:00
|
|
|
|
|
|
|
|
2022-11-16 15:25:53 +01:00
|
|
|
[bits 32]
|
|
|
|
BEGIN_32BIT: ; After the switch we will get here
|
2022-11-18 22:19:40 +01:00
|
|
|
call enableA20
|
2022-11-16 15:25:53 +01:00
|
|
|
call kernel_offset
|
2022-11-18 22:19:40 +01:00
|
|
|
cli
|
|
|
|
hlt
|
2022-11-16 15:25:53 +01:00
|
|
|
jmp $
|
2022-11-15 15:47:33 +01:00
|
|
|
|
2022-11-18 22:19:40 +01:00
|
|
|
|
|
|
|
; Now we include the necessary files to switch to
|
|
|
|
; 32-bit (aka protected) mode
|
|
|
|
%include "src/boot/gdt.s"
|
|
|
|
%include "src/boot/switch32.s"
|
|
|
|
%include "src/boot/util/enablea20.s"
|
|
|
|
|
|
|
|
|
|
|
|
; Here we define our variables used in the second stage
|
|
|
|
kernel_loaded_msg: db "TSBL - INFO: Kernel loaded, switching to protected mode", 0
|
|
|
|
stage2_loaded_msg: db "TSBL - INFO: Stage 2 loaded, loading kernel", 0
|
|
|
|
|
|
|
|
|
|
|
|
times (512 * reserved_sectors) - ($ - $$) db 0 ; Pads the section to exactly reserved_sectors
|