#!/bin/sh onerr() { echo -e "\x1b[1;31merror: '$action' failed\x1b[m" exit 1 } trap onerr ERR name="uff" build_mode=debug action=help target=x86_64 test_mode=false serial_mode=false function define_vars() { target_name="$target-$name" target_path="target/" rust_target_path="$target_path/$target_name/$build_mode/" kernel_libary="$rust_target_path/libkernel.a" iso_path="$target_path/iso/" obj_path="$iso_path/obj/" src_path="src/" asm_path="$src_path/asm/" link_script="$src_path/linker.ld" grub_cfg="$src_path/grub.cfg" } define_vars print_help() { echo "usage: $0 (options) [Path]" echo " build and lauch the iso in qemu" echo echo "options:" echo " -name= set application name (standard: $name)" echo " -mode= set build mode (standard: $build_mode)" echo " -target= set target (standard: $target)" echo " -test enable test mode (standard: disabled)" echo " -serial enable serial mode (standard: disabled)" echo " -help show this help message" exit } get_rust_bin() { #if $test_mode; then # echo "$rust_target_path/$(ls -t1 $rust_target_path | grep -P '^kernel-[a-fA-F0-9]+$' | head -n1)" #else # echo "$rust_target_path/libkernel.a" #fi echo "$kernel_libary" } prepare_iso() { mkdir "$target_path" &> /dev/null mkdir "$iso_path" &> /dev/null mkdir "$obj_path" &> /dev/null mkdir "$iso_path/isofiles" &> /dev/null mkdir "$iso_path/isofiles/boot" &> /dev/null mkdir "$iso_path/isofiles/boot/grub" &> /dev/null for f in "$asm_path"/*.asm; do nasm -felf64 "$f" -o "$obj_path/$(basename -s .asm $f).o" done cp "$grub_cfg" "$iso_path/isofiles/boot/grub/grub.cfg" } build_iso() { ld -n -s -o "$iso_path/isofiles/boot/kernel.bin" -gc-sections -T "$link_script" "$obj_path"/*.o "$(get_rust_bin)" grub-mkrescue -d /usr/lib/grub/i386-pc -o "$iso_path/$name.iso" "$iso_path/isofiles" &> /dev/null echo "$kernel_libary" } build() { if test ! -d "$iso_path/isofiles"; then prepare_iso fi build_iso } run() { case "$target" in "x86_64") qemu="qemu-system-x86_64 -cdrom $iso_path/$name.iso" if $test_mode; then qemu="$qemu -device isa-debug-exit,iobase=0xf4,iosize=0x04 -serial stdio -no-reboot " fi if $serial_mode; then qemu="$qemu -display none" fi $qemu case "$?" in 33) exit 0;; 35) exit 1;; *) echo "qemu exited unexpectedly $?"; exit 1;; esac;; *) echo "error: no laucher defined for target '$target'";; esac } for arg in "$@"; do case "$arg" in -mode=*) build_mode="$(echo $arg | sed "s/^-mode=//")"; define_vars;; -name=*) name="$(echo $arg | sed "s/^-name=//")"; define_vars;; -target=*) target="$(echo $arg | sed "s/^-target=//")"; define_vars;; -test) test_mode=true; define_vars;; -serial) serial_mode=true; define_vars;; -help) print_help;; *) # echo "warn: ignoring unknown option '$arg'" kernel_libary=$arg;; esac done build run echo -e "\x1b[1;32mdone\x1b[m"