First code; I don't know if it works

This commit is contained in:
Eric-Paul Ickhorn 2023-04-24 22:00:51 +02:00
parent a45d5c4902
commit 9af34bc8c7
8 changed files with 199 additions and 1 deletions

View File

@ -1,6 +1,6 @@
MIT License MIT License
Copyright (c) <year> <copyright holders> Copyright (c) 2023 Eric-Paul Ickhorn
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

11
build.sh Executable file
View File

@ -0,0 +1,11 @@
#!/bin/env bash
cd stage_1
./build.sh
cd ../
cd stage_2
./build.sh
cd ../

3
run.sh Normal file
View File

@ -0,0 +1,3 @@
#!/bin/env bash

3
stage_1/build.sh Executable file
View File

@ -0,0 +1,3 @@
#!/bin/env bash
nasm -fbin start.asm -o stage_1.bin

BIN
stage_1/stage_1.bin Normal file

Binary file not shown.

78
stage_1/start.asm Normal file
View File

@ -0,0 +1,78 @@
section .text
org 0x7c00
start:
push dx
mov ax, 0
mov ds, ax
mov es, ax
mov gs, ax
mov fs, ax
mov ax, 0x7e00
mov ss, ax
mov sp, 256
mov bp, 256
mov ax, 0x7c00
add ax, 446
; ax now points to the start of the partition table
mov di, 0
.search_partition:
mov bx, ax
add bx, 4
mov cl, [bx]
cmp cl, 0x9d
je .found_partition
inc di
cmp di, 4
je .no_boot_partition
add ax, 16
.found_partition:
mov bx, ax
add bx, 14
mov cx, [bx] ; more than 65536 sectors will NOT be
; supported for the bootloader's partition!
mov bx, 0x8000
mov es, bx
mov di, 0x0000
mov bx, ax
inc bx
; bx now points to the CHS-address of the first sector
; of the bootloader's partition.
mov dx, [bx]
mov ah, dh ; head
mov bl, dl ; sector
add bx, 2
; bx now points to the CHS-cylinder of the first sector
; of the bootloader's partition.
mov al, [bx] ; cylinder
call load_sectors
.no_boot_partition:
hlt
%include "gdt.asm"
%include "utility.asm"

100
stage_1/utility.asm Normal file
View File

@ -0,0 +1,100 @@
section .text
; ax: start_sector (lower)
; bl: start_sector (upper)
; cx: sector_count
; dh: drive
; es: buffer_address (upper)
; di: buffer_address (lower)
load_sectors:
.prolog:
push ax
push bx
push cx
push dx
xor si, si ; current sector index
; sets the arguments for the first run of '.load_data'
; which loads until the first CHS - cylinder boundary
.set_first_load_args:
; the first read only reads up to the next cylinder boundary,
; but potentially less (if not that many sectors have been requested)
; TODO================================================================================
mov bx, sp
add bx, 4
mov cx, [bx] ; get sector count
sub cx, si ; cx = num_missing_sectors
mov bx, sp
mov bx, [bx]
cmp cx, bx
js .load_data ; if there are more sectors needed after the
; next cylinder boundary
; if not that many sectors are needed, just read as
; many sectors as are needed in total
mov cx, bx
jmp .load_data
; sets the arguments for a run of '.load_data' after the
; first one; aligned on a CHS - cylinder boundary
.set_load_args:
mov bx, sp
add bx, 4
mov cx, [bx]
sub cx, si ; cx = num_missing_sectors
cmp cx, 64
js .load_data
; if this goes here, there are more than 64 sectors missing
; -> clamp them to 64 so they can be loaded.
mov cl, 64
jmp .load_data ; redundant
; loads at most 64 sectors from the disk into the main memory
; ax: scratchpad
; bx: start_sector (low part)
; ch: start_sector (high part)
; cl: num_sectors
.load_data:
mov bx, sp
mov ah, 0x02
mov al, cl
mov dl, [bx] ; drive
mov dh, bh ; head
mov cl, ch ; sector
mov ch, bl ; cylinder
mov bx, di ; buffer address (lower)
int 0x13
.continue_if_needed:
; check if there still are sectors missing to load
mov bx, sp
add bx, 4
mov cx, [bx] ; sector_count
cmp si, cx
js .set_load_args
.epilog:
pop dx
pop cx
pop bx
pop ax
ret

3
stage_2/build.sh Executable file
View File

@ -0,0 +1,3 @@
#!/bin/env bash