From 9af34bc8c7722c26e93c6628d131bca43ce356a3 Mon Sep 17 00:00:00 2001 From: Eric-Paul Ickhorn Date: Mon, 24 Apr 2023 22:00:51 +0200 Subject: [PATCH] First code; I don't know if it works --- LICENSE | 2 +- build.sh | 11 +++++ run.sh | 3 ++ stage_1/build.sh | 3 ++ stage_1/stage_1.bin | Bin 0 -> 165 bytes stage_1/start.asm | 78 ++++++++++++++++++++++++++++++++++ stage_1/utility.asm | 100 ++++++++++++++++++++++++++++++++++++++++++++ stage_2/build.sh | 3 ++ 8 files changed, 199 insertions(+), 1 deletion(-) create mode 100755 build.sh create mode 100644 run.sh create mode 100755 stage_1/build.sh create mode 100644 stage_1/stage_1.bin create mode 100644 stage_1/start.asm create mode 100644 stage_1/utility.asm create mode 100755 stage_2/build.sh diff --git a/LICENSE b/LICENSE index 2071b23..bd413a4 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) +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: diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..4bdb45a --- /dev/null +++ b/build.sh @@ -0,0 +1,11 @@ +#!/bin/env bash + +cd stage_1 +./build.sh +cd ../ + +cd stage_2 +./build.sh +cd ../ + + diff --git a/run.sh b/run.sh new file mode 100644 index 0000000..efe51ec --- /dev/null +++ b/run.sh @@ -0,0 +1,3 @@ +#!/bin/env bash + + diff --git a/stage_1/build.sh b/stage_1/build.sh new file mode 100755 index 0000000..f0ad606 --- /dev/null +++ b/stage_1/build.sh @@ -0,0 +1,3 @@ +#!/bin/env bash + +nasm -fbin start.asm -o stage_1.bin diff --git a/stage_1/stage_1.bin b/stage_1/stage_1.bin new file mode 100644 index 0000000000000000000000000000000000000000..88e056a4c49e3ca878415c2dcbbef72fb7f6132a GIT binary patch literal 165 zcmWIA!NAaWqwhf9i@pat80z{i>|tQs%fPsUp@wxI<9-H)&cn@zS-SWee$Fl7bZ`F8 zQli{^Kma7j*Ui71p`q_ESdDYHc*mEH%gu+Gy4YVZGJFXL4h%B<*7+Eyx0_${W9Q>; zdCQv>Vx2c%3&91Oe>zmKY;<_d(D`@^Q^$!e@s58TFFWpb{yr-VRbzRs;$l=}bi`o* DUmH#< literal 0 HcmV?d00001 diff --git a/stage_1/start.asm b/stage_1/start.asm new file mode 100644 index 0000000..08c814c --- /dev/null +++ b/stage_1/start.asm @@ -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" diff --git a/stage_1/utility.asm b/stage_1/utility.asm new file mode 100644 index 0000000..3ff0d87 --- /dev/null +++ b/stage_1/utility.asm @@ -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 diff --git a/stage_2/build.sh b/stage_2/build.sh new file mode 100755 index 0000000..efe51ec --- /dev/null +++ b/stage_2/build.sh @@ -0,0 +1,3 @@ +#!/bin/env bash + +