From 61a8ca90e858f669af97cdcc3bbfd2f51b191457 Mon Sep 17 00:00:00 2001 From: Eric-Paul Ickhorn Date: Fri, 26 May 2023 21:24:50 +0200 Subject: [PATCH] Current status of the code --- README.md | 2 +- build.bash | 64 +++++++++++++++++++++---- code/legacy-bios/bootsector/build.sh | 3 +- code/legacy-bios/bootsector/fat32.asm | 63 +++++++++++++++++++++++- code/legacy-bios/bootsector/gdt.asm | 54 +++++++++++++++++++++ code/legacy-bios/bootsector/start.asm | 24 ++++++---- code/legacy-bios/bootsector/utility.asm | 1 + code/legacy-bios/starter/build.sh | 2 +- run.bash | 2 +- 9 files changed, 192 insertions(+), 23 deletions(-) create mode 100644 code/legacy-bios/bootsector/gdt.asm diff --git a/README.md b/README.md index 9963afb..71b5be0 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ # nightloader -A slightly bloated bootloader. \ No newline at end of file +A slightly bloated bootloader. diff --git a/build.bash b/build.bash index 89e6f12..9d7f9af 100755 --- a/build.bash +++ b/build.bash @@ -1,12 +1,58 @@ -#!/bin/env bash +#!/bin/bash -cd bootsector -./build.sh -cd ../ +echo "|==== Building the NightLoader with Build-Script v0.1 ====|" +echo " " -cd stage_2 -./build.sh -cd ../ -cat bootsector/stage_1.bin > nightloader.bin -# cat stage_2/stage_2.bin >> nightloader.bin + +# Legacy Bootloader for old BIOS-platforms +echo "STARTING TO BUILD LEGACY-BIOS LOADER!" + +# Build the stage-1 bootloader; the first disk-sector. +cd code/legacy-bios/bootsector +bash build.sh $1 $2 +mv bootsector.bin ../../../ +cd .. + +# Build the stage 2 - starter; it is just there to jump +# to the *executable file* which the UEFI-codepath also uses. +cd starter +bash build.sh $1 $2 +mv starter.bin ../../../ +cd ../../../ + +mv bootsector.bin build/objects/ +mv starter.bin build/objects/ + +echo "FINISHED BUILDING LEGACY-BIOS LOADER" +echo " " +echo " " +echo " " + + + +echo "Creating disk image for a basic NightLoader-installation" + +# Create the partition's image-file by allocating 256MB and +# creating a FAT32 - partition which then resides there. +truncate -s 256M partition.bin +mkfs.fat -F32 -nNIGHT partition.bin + +# Mount the partition-image to a specific directory (needs +# root-permissions), copy over the freshly built binaries to +# the path they belobng to and unmount the partition. +mount partition.bin partition/ +mkdir -p partition/nightloader/ +cp starter.bin partition/ +umount partition.bin +mv partition.bin build/objects/ + +# Create a disk image by including the first disk sector. +cat build/objects/bootsector.bin > nightloader.bin +cat build/objects/partition.bin >> nightloader.bin + +echo " " +echo " " +echo "Finished building the NightLoader!" +echo " " + diff --git a/code/legacy-bios/bootsector/build.sh b/code/legacy-bios/bootsector/build.sh index f0ad606..3c4eb55 100755 --- a/code/legacy-bios/bootsector/build.sh +++ b/code/legacy-bios/bootsector/build.sh @@ -1,3 +1,2 @@ -#!/bin/env bash -nasm -fbin start.asm -o stage_1.bin +nasm -fbin start.asm -o bootsector.bin diff --git a/code/legacy-bios/bootsector/fat32.asm b/code/legacy-bios/bootsector/fat32.asm index 80f5455..39c9ba6 100644 --- a/code/legacy-bios/bootsector/fat32.asm +++ b/code/legacy-bios/bootsector/fat32.asm @@ -1,4 +1,65 @@ -; TODO: Read FAT32 filesystem and load raw binary frompath nightloader/stage_2.bin +; TODO: Read FAT32 filesystem and load raw binary from path nightloader/stage_2.bin +FAT32_HEADER_ROOT_DIRECTORY_FAT_INDEX equ 0x2c +find_stage_2: + + ; Set extra-segment to the address where the Stage 2 - partition is located. + mov bx, 0x2000 + mov es, bx + mov ds, bx + + + + ; PART 1: FIND THE STAGE 2 - FILE'S OFFSET IN THE PARTITION + + ; Read cluster size (in bytes) into dx + mov bx, 0x000b + mov ax, [bx] + add bx, 2 + mov di, [bx] + mul di + mov dx, ax + + ; Read the root directory's first cluster's index into si:di (si: upper, di: lower) + mov bx, FAT32_HEADER_ROOT_DIRECTORY_FAT_INDEX + mov di, [bx] + add bx, 2 + mov si, [bx] + + push si + push di + + ; Put address of root directory into ds:bx + + ; Part 1: Get the overflow + ; mov bx, + + + + + ; Loop through the root directory's entries +.root_directory_loop: ; Loops through all the root directory's clusters + + mov di, [bx] + add bx, 2 + mov si, [bx] + + + + ; Test for the root directory's "Last Cluster" - marker. + mov ax, [bx] + ; cmp + jmp .root_directory_loop + +; Arguments: +; bx: +; Return Values: +; ax: The index of the FAT-entry in which the searched file was found. +find_file_in_directory: + + + + + diff --git a/code/legacy-bios/bootsector/gdt.asm b/code/legacy-bios/bootsector/gdt.asm new file mode 100644 index 0000000..3dd3118 --- /dev/null +++ b/code/legacy-bios/bootsector/gdt.asm @@ -0,0 +1,54 @@ + + +create_gdt: + mov bx, STAGE_1_GDT_MEMORY_ADDRESS + mov ds, bx + + mov bx, 8 + mov di, 8 +.set_loop: + + mov ax, bx + and ax, 0b111 + + mov [bx], byte 0xff + cmp ax, 0 + je .set_loop_condition + + mov [bx], byte 0xff + cmp ax, 1 + je .set_loop_condition + + mov [bx], byte 0xff + cmp ax, 2 + je .set_loop_condition + + mov [bx], byte 0xff + cmp ax, 3 + je .set_loop_condition + + mov [bx], byte 0xff + cmp ax, 4 + je .set_loop_condition + + mov [bx], byte 0xff + cmp ax, 5 + je .set_loop_condition + + mov [bx], byte 0xff + cmp ax, 6 + je .set_loop_condition + + mov [bx], byte 0xff + cmp ax, 7 + je .set_loop_condition + +.set_loop_condition: + cmp bx, 48 + jb .set_loop + + + + + ret + diff --git a/code/legacy-bios/bootsector/start.asm b/code/legacy-bios/bootsector/start.asm index 2c02bf5..71b9bd6 100644 --- a/code/legacy-bios/bootsector/start.asm +++ b/code/legacy-bios/bootsector/start.asm @@ -2,7 +2,8 @@ STAGE_1_CODE_MEMORY_ADDRESS equ 0x7c00 -STAGE_1_STACK_MEMORY_ADDRESS equ 0x07e0 +STAGE_1_STACK_MEMORY_ADDRESS equ 0x7e00 ; grows downwards +STAGE_1_GDT_MEMORY_ADDRESS equ 0x7e00 NIGHTLOADER_PARTITION_TYPE equ 0x9d @@ -23,9 +24,9 @@ stage_0: mov bx, STAGE_1_STACK_MEMORY_ADDRESS mov ss, bx - mov bx, 0x0800 - mov bp, 256 - mov sp, 256 + mov bx, 0x2000 + mov bp, 512 + mov sp, 512 mov bx, 0x0000 mov ds, bx @@ -71,6 +72,8 @@ stage_0: .load_partition: + call create_gdt ; TODO: Doesn't belong here! + mov si, cx ; address of the partition table entry of the nightloader-partition ; TODO: Make this more flexible; that's urgent. @@ -80,23 +83,25 @@ stage_0: mov cl, 2 ; sector (starts at 1, max.: 63) mov dx, di ; storage device id mov dh, 0 ; head - mov bx, 0x0800 + mov bx, 0x2000 mov es, bx ; read destination segment mov bx, 0x0000 ; read destination address int 0x13 - mov si, 0x8000 + mov bx, 0x2000 + mov ds, bx + + mov si, 0x0000 mov cx, 8 mov dh, 12 mov dl, 36 call print_string ; jmp .wait - jmp 0x0800:0x0000 + jmp 0x2000:0x0000 - .no_boot_partition: mov si, errors.no_boot_partition @@ -105,6 +110,8 @@ stage_0: mov dh, 12 call print_string + + .wait: cli hlt @@ -119,5 +126,6 @@ errors: db "0123456789abcdef" %include "utility.asm" +%include "gdt.asm" %include "fat32.asm" %include "partition_table.asm" diff --git a/code/legacy-bios/bootsector/utility.asm b/code/legacy-bios/bootsector/utility.asm index be8a760..c5d8348 100644 --- a/code/legacy-bios/bootsector/utility.asm +++ b/code/legacy-bios/bootsector/utility.asm @@ -1,4 +1,5 @@ + ; cx: len_buffer ; si: buffer ; dl: start_x NOTE: Will contain the end of the text on-screen diff --git a/code/legacy-bios/starter/build.sh b/code/legacy-bios/starter/build.sh index efe51ec..7a693aa 100755 --- a/code/legacy-bios/starter/build.sh +++ b/code/legacy-bios/starter/build.sh @@ -1,3 +1,3 @@ -#!/bin/env bash +#!/bin/bash diff --git a/run.bash b/run.bash index 1ebbecf..8958ae2 100755 --- a/run.bash +++ b/run.bash @@ -1,3 +1,3 @@ -#!/bin/env bash +#!/bin/bash qemu-system-x86_64 nightloader.bin