Skip to content

Commit

Permalink
Added MBR implementation
Browse files Browse the repository at this point in the history
This adds the a MBR partition implementation. 
Refactored some file system code to have a common FS helper functions
This also enables the using the MBR and FAT32 to create the boot image removing the echfs dependency.
Moved file system boot sector to common x86 arch.
As this boot sector is written in x86, where would need to be an ARM version, so would be arch dependent.
Added MAP file path
Fixed running the x86_64 port as this wasn't making the boot image, just the elf
  • Loading branch information
DrDeano committed May 7, 2021
1 parent 70ecc5a commit b3ac0aa
Show file tree
Hide file tree
Showing 13 changed files with 1,201 additions and 377 deletions.
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
[submodule "limine"]
path = limine
url = https://github.com/limine-bootloader/limine.git
[submodule "echfs"]
path = echfs
url = https://github.com/echfs/echfs.git
315 changes: 251 additions & 64 deletions build.zig

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion echfs
Submodule echfs deleted from 14da98
42 changes: 9 additions & 33 deletions makeiso_64.sh
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
#!/usr/bin/env bash

which readelf > /dev/null || exit_missing
MAP_FILE=$1
PLUTO_ELF=$2
OUTPUT_FILE=$3
RAMDISK=$4

INSTALL_PATH=$1
BIN_PATH=$2
PLUTO_ELF=$3
OUTPUT_FILE=$4
RAMDISK=$5
exit_missing() {
printf "$_ must be installed\n";
exit 1;
}

if [[ ! -f ./echfs/echfs-utils ]]
then
echo "Building echfs-utils"
cd echfs
make echfs-utils
cd ..
fi
which readelf > /dev/null || exit_missing

if [[ ! -f ./limine/limine-install ]]
then
Expand All @@ -24,26 +20,6 @@ then
cd ..
fi

MAP_FILE=$INSTALL_PATH/"kernel.map"

# Read the symbols from the binary, remove all the unnecessary columns with awk and emit to a map file
readelf -s --wide $PLUTO_ELF | grep -F "FUNC" | awk '{$1=$3=$4=$5=$6=$7=""; print $0}' | sort -k 1 > $MAP_FILE
echo "" >> $MAP_FILE

if [[ -f $OUTPUT_FILE ]]
then
rm $OUTPUT_FILE
fi

mkdir -p $BIN_PATH

dd if=/dev/zero bs=1M count=0 seek=64 of=$OUTPUT_FILE
parted -s $OUTPUT_FILE mklabel msdos
parted -s $OUTPUT_FILE mkpart primary 1 100%
./echfs/echfs-utils -m -p0 $OUTPUT_FILE quick-format 32768
./echfs/echfs-utils -m -p0 $OUTPUT_FILE import limine.cfg limine.cfg
./echfs/echfs-utils -m -p0 $OUTPUT_FILE import limine/limine.sys limine.sys
./echfs/echfs-utils -m -p0 $OUTPUT_FILE import $PLUTO_ELF pluto.elf
./echfs/echfs-utils -m -p0 $OUTPUT_FILE import $RAMDISK initrd.ramdisk
./echfs/echfs-utils -m -p0 $OUTPUT_FILE import $MAP_FILE kernel.map
./limine/limine-install $OUTPUT_FILE
2 changes: 1 addition & 1 deletion src/kernel/arch/x86/64bit/boot.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const arch = @import("arch.zig");
const Header = packed struct {
/// The kernel entry point or 0 for entry point of Elf file.
entry_point: u64,
/// THe stack address.
/// The stack address.
stack: *u8,
/// Unused flags
flags: u64,
Expand Down
91 changes: 86 additions & 5 deletions src/kernel/arch/x86/common/arch.zig
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
const builtin = @import("builtin");
const gdt = switch (builtin.arch) {
.i386 => @import("../32bit/gdt.zig"),
.x86_64 => @import("../64bit/gdt.zig"),
else => unreachable,
};
const gdt = @import("gdt.zig");
const idt = switch (builtin.arch) {
.i386 => @import("../32bit/idt.zig"),
.x86_64 => @import("../64bit/idt.zig"),
Expand All @@ -21,6 +17,91 @@ const BootPayload = switch (builtin.arch) {
else => unreachable,
};

// This is the assembly for the FAT bootloader.
// [bits 16]
// [org 0x7C00]
//
// jmp short _start
// nop
//
// times 87 db 0xAA
//
// _start:
// jmp long 0x0000:start_16bit
//
// start_16bit:
// cli
// mov ax, cs
// mov ds, ax
// mov es, ax
// mov ss, ax
// mov sp, 0x7C00
// lea si, [message]
// .print_string_with_new_line:
// mov ah, 0x0E
// xor bx, bx
// .print_string_loop:
// lodsb
// cmp al, 0
// je .print_string_done
// int 0x10
// jmp short .print_string_loop
// .print_string_done:
// mov al, 0x0A
// int 0x10
// mov al, 0x0D
// int 0x10
//
// .reboot:
// xor ah, ah
// int 0x16
// int 0x19
//
// .loop_forever:
// hlt
// jmp .loop_forever
// message db "This is not a bootable disk. Please insert a bootable floppy and press any key to try again", 0
// times 510 - ($ - $$) db 0
// dw 0xAA55

/// Basic boot code that will just print to the scream to insert a bootable image. This is intended
/// as a place holder to be over written with real boot code if needed.
/// This assumes the 512 sector size and includes the 0xAA55 boot signature.
pub const filesystem_bootsector_boot_code = [512]u8{
0xEB, 0x58, 0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0xEA, 0x62, 0x7C, 0x00, 0x00,
0x00, 0x00, 0xFA, 0x8C, 0xC8, 0x8E, 0xD8, 0x8E, 0xC0, 0x8E, 0xD0, 0xBC, 0x00, 0x7C, 0x8D, 0x36,
0x8F, 0x7C, 0xB4, 0x0E, 0x31, 0xDB, 0xAC, 0x3C, 0x00, 0x74, 0x04, 0xCD, 0x10, 0xEB, 0xF7, 0xB0,
0x0A, 0xCD, 0x10, 0xB0, 0x0D, 0xCD, 0x10, 0x30, 0xE4, 0xCD, 0x16, 0xCD, 0x19, 0xEB, 0xFE, 0x54,
0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x6E, 0x6F, 0x74, 0x20, 0x61, 0x20, 0x62, 0x6F, 0x6F,
0x74, 0x61, 0x62, 0x6C, 0x65, 0x20, 0x64, 0x69, 0x73, 0x6B, 0x2E, 0x20, 0x50, 0x6C, 0x65, 0x61,
0x73, 0x65, 0x20, 0x69, 0x6E, 0x73, 0x65, 0x72, 0x74, 0x20, 0x61, 0x20, 0x62, 0x6F, 0x6F, 0x74,
0x61, 0x62, 0x6C, 0x65, 0x20, 0x66, 0x6C, 0x6F, 0x70, 0x70, 0x79, 0x20, 0x61, 0x6E, 0x64, 0x20,
0x70, 0x72, 0x65, 0x73, 0x73, 0x20, 0x61, 0x6E, 0x79, 0x20, 0x6B, 0x65, 0x79, 0x20, 0x74, 0x6F,
0x20, 0x74, 0x72, 0x79, 0x20, 0x61, 0x67, 0x61, 0x69, 0x6E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0xAA,
};

///
/// Assembly that reads data from a given port and returns its value.
///
Expand Down
Loading

0 comments on commit b3ac0aa

Please sign in to comment.