From d3a41ed76d93ac027e8163646042916fbb0d31a0 Mon Sep 17 00:00:00 2001 From: Greg Bowne Date: Sat, 31 Aug 2024 17:16:34 -0700 Subject: [PATCH] added a build.sh and code to kernel.c --- build.sh | 44 ++++++++++++++++++++++++++++++++++++++++++++ kernel.c | 13 +++++++++++++ 2 files changed, 57 insertions(+) diff --git a/build.sh b/build.sh index e69de29..b31e32f 100644 --- a/build.sh +++ b/build.sh @@ -0,0 +1,44 @@ +#!/bin/bash + +# Exit immediately if a command exits with a non-zero status. +set -e + +# Function to check if a command exists +command_exists() { + command -v "$1" >/dev/null 2>&1 +} + +# Check for required commands +for cmd in nasm gcc qemu-system-i386; do + if ! command_exists "$cmd"; then + echo "Error: $cmd is not installed or not in PATH" >&2 + exit 1 + fi +done + +# Compile the assembly file +echo "Compiling boot4.asm..." +nasm -f elf32 boot4.asm -o boot4.o + +# Compile and link the kernel +echo "Compiling and linking kernel..." +gcc -m32 -ffreestanding -nostdlib kernel.c boot4.o -o kernel.bin -T linker.ld + +# Check if compilation was successful +if [ -f kernel.bin ]; then + echo "Build successful. kernel.bin created." + + # Ask user if they want to run QEMU + read -p "Do you want to run QEMU now? (y/n) " -n 1 -r + echo # Move to a new line + if [[ $REPLY =~ ^[Yy]$ ]]; then + echo "Running QEMU..." + qemu-system-i386 -enable-kvm -net none -fda kernel.bin + else + echo "QEMU not started. You can run it later with:" + echo "qemu-system-i386 -enable-kvm -net none -fda kernel.bin" + fi +else + echo "Build failed. Check for errors above." >&2 + exit 1 +fi diff --git a/kernel.c b/kernel.c index e69de29..e429326 100644 --- a/kernel.c +++ b/kernel.c @@ -0,0 +1,13 @@ +#include + +void kmain(void) +{ + const uint16_t color = 0x0F00; + const char *hello = "Hello C world!"; + volatile uint16_t *vga = (volatile uint16_t *)0xb8000; + + for (int i = 0; i < 16; ++i) + { + vga[i + 80] = color | (uint16_t)hello[i]; + } +}