summaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authorsalmonik <salmonik@tuta.io>2026-07-31 02:18:30 +0200
committersalmonik <salmonik@tuta.io>2026-07-31 02:18:30 +0200
commit76103215711d268b4b02f62e900f2be3032cf295 (patch)
treedcd86b5b02ae99c99ca833668055f4f53211be6b /tools
downloadrs-dos-76103215711d268b4b02f62e900f2be3032cf295.tar.gz
rs-dos-76103215711d268b4b02f62e900f2be3032cf295.zip
Initial commit: raisernics toy kernel
- Bare-metal x86_64 UEFI kernel (PE32+, no libc) - GOP framebuffer + 8x16 bitmap font - PS/2 keyboard (polling, scancode set 1) - UART 16550 serial driver (COM1, 115200 8N1) - Bump allocator as #[global_allocator] (alloc::String, Vec, format!) - UEFI memory map capture (static buffer, ADR 0001) - Shell with 7 commands (help, echo, clear, hello, info, mem, halt) - Dual output: GOP framebuffer + serial console - Makefile: run, run-gui, debug, release, image targets - tools/: qemu.sh, boot-image.sh, debug-kernel.py
Diffstat (limited to '')
-rwxr-xr-xtools/boot-image.sh62
-rwxr-xr-xtools/debug-kernel.py64
-rwxr-xr-xtools/qemu.sh96
3 files changed, 222 insertions, 0 deletions
diff --git a/tools/boot-image.sh b/tools/boot-image.sh
new file mode 100755
index 0000000..2592afa
--- /dev/null
+++ b/tools/boot-image.sh
@@ -0,0 +1,62 @@
1#!/usr/bin/env bash
2# =============================================================================
3# tools/boot-image.sh — Create a bootable disk image from the ESP directory
4#
5# Produces a raw disk image (bootable via UEFI) for dd-ing to a USB stick
6# or attaching to QEMU as a hard disk.
7#
8# Usage:
9# ./tools/boot-image.sh [output.img]
10#
11# Default output: raisernics.img
12# =============================================================================
13set -euo pipefail
14
15SCRIPT_DIR="$(cd "$(dirname "$0")/.." && pwd)"
16cd "$SCRIPT_DIR"
17
18OUTPUT="${1:-raisernics.img}"
19ESP_DIR="${ESP_DIR:-esp}"
20MBR_SZ=1 # 1 MB reserved at start for GPT/pmbr
21SECTOR_SZ=512
22
23# Rebuild ESP if stale
24make all
25
26if [ ! -d "$ESP_DIR" ]; then
27 echo "ERROR: ESP directory '$ESP_DIR' not found. Run 'make all' first."
28 exit 1
29fi
30
31# Calculate required size: ESP contents + MBR gap + some slack
32ESP_SIZE_MB=$(du -sm "$ESP_DIR" 2>/dev/null | cut -f1 || echo 1)
33TOTAL_MB=$((ESP_SIZE_MB + MBR_SZ + 1)) # +1 MB slack
34
35echo "Creating ${TOTAL_MB} MB disk image: ${OUTPUT}"
36
37# Create empty image with MBR gap
38dd if=/dev/zero of="$OUTPUT" bs=1M count="$TOTAL_MB" status=none 2>/dev/null
39
40# Partition table: single GPT partition, type EFI System (C12A7328-F81F-11D2-BA4B-00A0C93EC93B)
41sgdisk --clear \
42 --new=1:${MBR_SZ}M:0 \
43 --typecode=1:C12A7328-F81F-11D2-BA4B-00A0C93EC93B \
44 --change-name=1:"raisernics" \
45 "$OUTPUT" >/dev/null
46
47# Find partition offset
48PART_START=$(sgdisk --info=1 "$OUTPUT" 2>/dev/null | grep "Partition start sector" | awk '{print $NF}')
49if [ -z "$PART_START" ]; then
50 PART_START=$((MBR_SZ * 1024 * 1024 / SECTOR_SZ))
51fi
52
53# Format as FAT32
54mformat -i "$OUTPUT"@$((PART_START * SECTOR_SZ)) -h 32 -s 32 -t "$((TOTAL_MB * 2))" >/dev/null 2>&1 || true
55
56# Copy the ESP contents onto the partition
57mcopy -i "$OUTPUT"@$((PART_START * SECTOR_SZ)) -s "$ESP_DIR"/* ::/ 2>/dev/null
58
59echo " -> ${OUTPUT} ($(ls -lh "$OUTPUT" | awk '{print $5}'))"
60echo ""
61echo "To write to USB: sudo dd if=${OUTPUT} of=/dev/sdX bs=4M status=progress"
62echo "To test with QEMU: qemu-system-x86_64 -bios <OVMF> -drive file=${OUTPUT},format=raw"
diff --git a/tools/debug-kernel.py b/tools/debug-kernel.py
new file mode 100755
index 0000000..c794ad6
--- /dev/null
+++ b/tools/debug-kernel.py
@@ -0,0 +1,64 @@
1"""
2GDB Python script for debugging raisernics under QEMU.
3
4Usage:
5 1. Terminal 1: ./tools/qemu.sh --debug
6 2. Terminal 2: gdb -x tools/debug-kernel.py target/x86_64-unknown-uefi/debug/raisernics.efi
7"""
8
9import gdb
10import os
11
12# Find the kernel binary
13script_dir = os.path.dirname(os.path.abspath(__file__))
14project_root = os.path.dirname(script_dir)
15binary = os.path.join(
16 project_root,
17 "target/x86_64-unknown-uefi/debug/raisernics.efi"
18)
19
20# Connect to QEMU's GDB stub
21gdb.execute("target remote :1234")
22
23# Load the binary (add symbol table)
24if os.path.exists(binary):
25 gdb.execute(f"symbol-file {binary}")
26 print(f"Loaded symbols from {binary}")
27else:
28 print(f"WARNING: {binary} not found. Build with debug symbols first.")
29
30gdb.execute("set breakpoint pending on")
31
32
33# Helper commands
34class DumpBootServices(gdb.Command):
35 """Dump EFI boot services signature and key pointers"""
36
37 def __init__(self):
38 super().__init__("dump-bs", gdb.COMMAND_USER)
39
40 def invoke(self, argument, from_tty):
41 del argument, from_tty
42 gdb.execute("printf \"SystemTable, BootServices, and key function pointers\\n\"")
43 gdb.execute("info functions efi_main")
44
45
46class DumpGop(gdb.Command):
47 """Dump GOP framebuffer info"""
48
49 def __init__(self):
50 super().__init__("dump-gop", gdb.COMMAND_USER)
51
52 def invoke(self, argument, from_tty):
53 del argument, from_tty
54 gdb.execute("printf \"Check the gop::Framebuffer static\\n\"")
55
56
57DumpBootServices()
58DumpGop()
59
60print("")
61print("raisernics GDB session ready.")
62print(" target remote :1234 (connected)")
63print(" Commands: continue, stepi, bt, info registers, etc.")
64print(" Custom: dump-bs, dump-gop")
diff --git a/tools/qemu.sh b/tools/qemu.sh
new file mode 100755
index 0000000..428954f
--- /dev/null
+++ b/tools/qemu.sh
@@ -0,0 +1,96 @@
1#!/usr/bin/env bash
2# =============================================================================
3# tools/qemu.sh — Run raisernics in QEMU with various options
4#
5# Usage:
6# ./tools/qemu.sh # serial console (no graphics)
7# ./tools/qemu.sh --gui # graphical window
8# ./tools/qemu.sh --debug # GDB stub on :1234
9# ./tools/qemu.sh --image foo.img # boot from raw disk image
10# ./tools/qemu.sh --bios /path/to.fd # custom OVMF
11# ./tools/qemu.sh --mem 512M # memory size
12# ./tools/qemu.sh --smp 2 # SMP cores
13# =============================================================================
14set -euo pipefail
15
16SCRIPT_DIR="$(cd "$(dirname "$0")/.." && pwd)"
17cd "$SCRIPT_DIR"
18
19# Defaults
20GUI_FLAG=""
21DEBUG_FLAG=""
22DRIVE_ARGS=(-drive file=fat:rw:esp,format=raw)
23OVMF="${OVMF:-/usr/share/edk2/x64/OVMF.4m.fd}"
24MEM="${MEM:-256M}"
25SMP="${SMP:-1}"
26
27# Pre-build
28make all
29
30# Parse args
31while [ $# -gt 0 ]; do
32 case "$1" in
33 --gui | -g)
34 GUI_FLAG="-vga std"
35 shift
36 ;;
37 --debug | -d)
38 DEBUG_FLAG="-s -S"
39 shift
40 ;;
41 --image | -i)
42 _img_file="$(printf -- '-drive file=%s,format=raw,if=none,id=hd' "$2")"
43 DRIVE_ARGS=("$_img_file" '-device' 'ide-hd,drive=hd')
44 shift 2
45 ;;
46 --bios | -b)
47 OVMF="$2"
48 shift 2
49 ;;
50 --mem)
51 MEM="$2"
52 shift 2
53 ;;
54 --smp)
55 SMP="$2"
56 shift 2
57 ;;
58 --help | -h)
59 head -20 "$0" | grep "^# " | sed 's/^# //'
60 exit 0
61 ;;
62 *)
63 echo "Unknown option: $1"
64 exit 1
65 ;;
66 esac
67done
68
69QEMU_CMD=(
70 qemu-system-x86_64
71 -bios "$OVMF"
72 "${DRIVE_ARGS[@]}"
73 -serial stdio
74 -m "$MEM"
75 -machine q35
76 -cpu qemu64
77 -no-reboot
78 -smp "$SMP"
79)
80
81[ -n "$GUI_FLAG" ] && QEMU_CMD+=("$GUI_FLAG")
82if [ -n "$DEBUG_FLAG" ]; then
83 # Split the flag string safely into words
84 read -ra dbg <<< "$DEBUG_FLAG"
85 QEMU_CMD+=("${dbg[@]}")
86fi
87
88echo " OVMF : $OVMF"
89echo " Drive : ${DRIVE_ARGS[*]}"
90echo " Mem : $MEM"
91echo " SMP : $SMP"
92echo " GDB : $([ -n "$DEBUG_FLAG" ] && echo ':1234' || echo 'off')"
93echo " GUI : $([ -n "$GUI_FLAG" ] && echo 'yes' || echo 'no')"
94echo ""
95
96exec "${QEMU_CMD[@]}"