Added BPB and minor changes
This commit is contained in:
parent
512f348d74
commit
946f7e72c3
|
@ -69,5 +69,6 @@ _deps
|
||||||
*.bin
|
*.bin
|
||||||
*.img
|
*.img
|
||||||
dist/
|
dist/
|
||||||
|
obj/
|
||||||
|
build/
|
||||||
|
|
||||||
|
|
|
@ -10,7 +10,8 @@
|
||||||
"compilerPath": "/usr/bin/gcc",
|
"compilerPath": "/usr/bin/gcc",
|
||||||
"cStandard": "gnu17",
|
"cStandard": "gnu17",
|
||||||
"cppStandard": "gnu++17",
|
"cppStandard": "gnu++17",
|
||||||
"intelliSenseMode": "linux-gcc-x64"
|
"intelliSenseMode": "linux-gcc-x64",
|
||||||
|
"configurationProvider": "ms-vscode.makefile-tools"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"version": 4
|
"version": 4
|
||||||
|
|
1
Makefile
1
Makefile
|
@ -67,6 +67,7 @@ $(OBJDIR)/%.o: prepare $(SRCDIR)/%.c
|
||||||
# Compile kernel drivers
|
# Compile kernel drivers
|
||||||
drivers: $(DRIVERS_OBJS)
|
drivers: $(DRIVERS_OBJS)
|
||||||
|
|
||||||
|
|
||||||
# Compile and link the kernel
|
# Compile and link the kernel
|
||||||
$(BUILDDIR)/kernel.bin: $(OBJDIR)/entrypoint.o $(KERNEL_OBJS) $(DRIVERS_OBJS)
|
$(BUILDDIR)/kernel.bin: $(OBJDIR)/entrypoint.o $(KERNEL_OBJS) $(DRIVERS_OBJS)
|
||||||
$(LD) -o $@ -Ttext 0x1000 $^ --oformat binary
|
$(LD) -o $@ -Ttext 0x1000 $^ --oformat binary
|
||||||
|
|
102
src/boot/mbr.s
102
src/boot/mbr.s
|
@ -12,43 +12,82 @@
|
||||||
; See the License for the specific language governing permissions and
|
; See the License for the specific language governing permissions and
|
||||||
; limitations under the License.
|
; limitations under the License.
|
||||||
|
|
||||||
; Definition of the MBR (Master Boot Record). This is basically our bootloader and
|
; Welcome to TSBL: The Simple Bootloader
|
||||||
; 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
|
[org 0x7c00] ; Address where the code expects to be loaded in. The BIOS always loads us here
|
||||||
[bits 16] ; All x86 CPUs start in 16 bit (aka "real") mode, so we tell nasm to emit 16-bit code
|
[bits 16] ; All x86 CPUs start in 16 bit (aka "real") mode, so we tell nasm to emit 16-bit code
|
||||||
|
|
||||||
kernel_offset: equ 0x1000
|
; Code copied from the limine bootloader
|
||||||
reserved_sectors: equ 4
|
|
||||||
|
|
||||||
; This isn't needed inside the qemu emulator, but
|
jmp skip_bpb ; Workaround for some BIOSes that require this stub
|
||||||
; real hardware is unlikely to start up with the
|
nop
|
||||||
; segment registers zeroed, so we do it here
|
|
||||||
mov ax, 0
|
|
||||||
mov ds, ax
|
|
||||||
mov ss, ax
|
|
||||||
mov es, ax
|
|
||||||
xor ax, ax
|
|
||||||
|
|
||||||
; We save the value of the current boot drive
|
; Some BIOSes will do a funny and decide to overwrite bytes of code in
|
||||||
mov [boot_drive], dl
|
; the section where a FAT BPB would be, potentially overwriting
|
||||||
|
; bootsector code.
|
||||||
|
; Avoid that by filling the BPB area with dummy values.
|
||||||
|
; Some of the values have to be set to certain values in order
|
||||||
|
; to boot on even quirkier machines.
|
||||||
|
; Source: https://github.com/freebsd/freebsd-src/blob/82a21151cf1d7a3e9e95b9edbbf74ac10f386d6a/stand/i386/boot2/boot1.S
|
||||||
|
|
||||||
; Now we setup the stack by setting the
|
bpb:
|
||||||
; base pointer to a location that's far
|
times 3-($-$$) db 0
|
||||||
; enough from where the code for the BIOS
|
bpb_oem_id: db "TSBL "
|
||||||
; is located
|
bpb_sector_size: dw 512
|
||||||
mov sp, 0x9000
|
bpb_sects_per_cluster: db 0
|
||||||
mov bp, sp
|
bpb_reserved_sects: dw 0
|
||||||
call bios_cls
|
bpb_fat_count: db 0
|
||||||
mov si, loading_stage2_msg
|
bpb_root_dir_entries: dw 0
|
||||||
call bios_println
|
bpb_sector_count: dw 0
|
||||||
mov bx, stage2
|
bpb_media_type: db 0
|
||||||
mov cl, 2
|
bpb_sects_per_fat: dw 0
|
||||||
mov dh, reserved_sectors
|
bpb_sects_per_track: dw 18
|
||||||
mov dl, [boot_drive]
|
bpb_heads_count: dw 2
|
||||||
call load_disk
|
bpb_hidden_sects: dd 0
|
||||||
jmp stage2
|
bpb_sector_count_big: dd 0
|
||||||
|
bpb_drive_num: db 0
|
||||||
|
bpb_reserved: db 0
|
||||||
|
bpb_signature: db 0
|
||||||
|
bpb_volume_id: dd 0
|
||||||
|
bpb_volume_label: db "TSBL "
|
||||||
|
bpb_filesystem_type: times 8 db 0
|
||||||
|
|
||||||
|
|
||||||
|
kernel_offset: equ 0x1000 ; Address in memory where kernel will be loaded
|
||||||
|
reserved_sectors: equ 4 ; How many disk sectors we reserve for our second stage
|
||||||
|
|
||||||
|
|
||||||
|
skip_bpb:
|
||||||
|
cli
|
||||||
|
cld
|
||||||
|
; 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
|
||||||
|
xor si, si
|
||||||
|
mov ds, si
|
||||||
|
mov ss, si
|
||||||
|
mov es, si
|
||||||
|
|
||||||
|
|
||||||
|
; We save the value of the current boot drive
|
||||||
|
mov [boot_drive], dl
|
||||||
|
|
||||||
|
; Now we setup the stack by setting the
|
||||||
|
; base pointer to a location that's far
|
||||||
|
; enough from where the code for the BIOS
|
||||||
|
; is located
|
||||||
|
mov sp, 0x7c00
|
||||||
|
mov bp, sp
|
||||||
|
sti
|
||||||
|
call bios_cls
|
||||||
|
mov si, loading_stage2_msg
|
||||||
|
call bios_println
|
||||||
|
mov bx, stage2
|
||||||
|
mov cl, 2
|
||||||
|
mov dh, reserved_sectors
|
||||||
|
mov dl, [boot_drive]
|
||||||
|
call load_disk
|
||||||
|
jmp stage2
|
||||||
|
|
||||||
|
|
||||||
; Variables needed in the boot
|
; Variables needed in the boot
|
||||||
|
@ -66,6 +105,7 @@ times 510 - ($ - $$) db 0
|
||||||
dw 0xaa55
|
dw 0xaa55
|
||||||
|
|
||||||
[bits 16]
|
[bits 16]
|
||||||
|
|
||||||
stage2:
|
stage2:
|
||||||
; Here we're no longer limited by the size of the
|
; Here we're no longer limited by the size of the
|
||||||
; boot sector, so we can perform the more complex
|
; boot sector, so we can perform the more complex
|
||||||
|
@ -76,7 +116,7 @@ stage2:
|
||||||
call switch_to_protected_mode
|
call switch_to_protected_mode
|
||||||
|
|
||||||
|
|
||||||
load_kernel:
|
load_kernel:
|
||||||
; Loads the kernel into memory
|
; Loads the kernel into memory
|
||||||
mov bx, kernel_offset
|
mov bx, kernel_offset
|
||||||
mov cl, reserved_sectors + 1
|
mov cl, reserved_sectors + 1
|
||||||
|
|
|
@ -59,7 +59,7 @@ void kprintAt(char* message, i32 col, i32 row) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void kprint(char* message) {
|
void inline kprint(char* message) {
|
||||||
/*
|
/*
|
||||||
Prints a null-terminated string to the
|
Prints a null-terminated string to the
|
||||||
VGA text buffer
|
VGA text buffer
|
||||||
|
|
|
@ -21,7 +21,8 @@ limitations under the License.
|
||||||
|
|
||||||
i32 kmain(void) {
|
i32 kmain(void) {
|
||||||
/*
|
/*
|
||||||
The kernel entry point of TSOS
|
The kernel entry point of TSKL: The
|
||||||
|
Simple Kernel
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
@ -29,5 +30,5 @@ i32 kmain(void) {
|
||||||
// log messages like this is just awful
|
// log messages like this is just awful
|
||||||
kprintln("\n\n\nTSKL - INFO: Kernel booted successfully");
|
kprintln("\n\n\nTSKL - INFO: Kernel booted successfully");
|
||||||
// TODO...
|
// TODO...
|
||||||
return 0x022172; // :D
|
return 0x202172; // :D
|
||||||
}
|
}
|
|
@ -82,6 +82,4 @@ void itoa(i32 n, char* a) {
|
||||||
|
|
||||||
if (sign < 0) a[--i] = '-';
|
if (sign < 0) a[--i] = '-';
|
||||||
a[--i] = '\0';
|
a[--i] = '\0';
|
||||||
|
|
||||||
/* TODO: implement "reverse" */
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue