From b14387c40a6c6c458e7eec96135f8e5dabc8f5f4 Mon Sep 17 00:00:00 2001 From: Niklas Elsbrock Date: Thu, 24 Apr 2025 17:20:18 +0200 Subject: [PATCH] initial commit --- .editorconfig | 10 ++++++++++ LICENSE | 18 +++++++++++++++++ Makefile | 43 +++++++++++++++++++++++++++++++++++++++++ README.md | 41 +++++++++++++++++++++++++++++++++++++++ src/rainbowboot.s | 49 +++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 161 insertions(+) create mode 100644 .editorconfig create mode 100644 LICENSE create mode 100644 Makefile create mode 100644 README.md create mode 100644 src/rainbowboot.s diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..4e51bf4 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,10 @@ +root = true + +[*] +end_of_line = lf +charset = utf-8 +insert_final_newline = true + +[{*.s,Makefile}] +indent_style = tab +indent_size = 8 \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..b860796 --- /dev/null +++ b/LICENSE @@ -0,0 +1,18 @@ +Copyright © 2025 Niklas Elsbrock + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the “Software”), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..e38b15a --- /dev/null +++ b/Makefile @@ -0,0 +1,43 @@ +src_dir := src +build_dir := build + +asm := nasm + +qemu := qemu-system-i386 +qemu_flags := -drive file=$(build_dir)/rainbowboot.img,format=raw +qemu_flags_gdb := -s -S + +dosbox := dosbox + +.PHONY: all run run-gdb run-dosbox attach clean + +all: $(build_dir)/rainbowboot.img + +run: $(build_dir)/rainbowboot.img + $(qemu) $(qemu_flags) + +run-gdb: $(build_dir)/rainbowboot.img + $(qemu) $(qemu_flags_gdb) $(qemu_flags) + +run-dosbox: $(build_dir)/rainbowboot.img + $(dosbox) -c "BOOT $(build_dir)/rainbowboot.img" + +attach: $(build_dir)/rainbowboot.elf + gdb $(build_dir)/rainbowboot.elf \ + -ex "target remote localhost:1234" \ + -ex "set architecture i8086" + +clean: + rm -rf $(build_dir) + +$(build_dir)/rainbowboot.img: $(build_dir)/rainbowboot.elf | $(build_dir) + objcopy -O binary $(build_dir)/rainbowboot.elf $(build_dir)/rainbowboot.img + +$(build_dir)/rainbowboot.elf: $(build_dir)/rainbowboot.o | $(build_dir) + ld -Ttext=0x7c00 -melf_i386 $(build_dir)/rainbowboot.o -o $(build_dir)/rainbowboot.elf + +$(build_dir)/rainbowboot.o: $(src_dir)/rainbowboot.s | $(build_dir) + $(asm) -f elf32 -g3 -F dwarf $(src_dir)/rainbowboot.s -o $(build_dir)/rainbowboot.o + +$(build_dir): + mkdir -p $(build_dir) diff --git a/README.md b/README.md new file mode 100644 index 0000000..bcd97b0 --- /dev/null +++ b/README.md @@ -0,0 +1,41 @@ +# rainbowboot + +This is a x86 boot sector demo that prints a rolling rainbow pattern to screen. + +Licensed under + +## How to use + +Make sure you have NASM (and QEMU, GDB and DOSBox if applicable) installed. + +### Building the disk image + +```sh +make +``` + +### Running on QEMU + +```sh +make run +``` + +### Running on DOSBox + +```sh +make run-dosbox +``` + +### Debugging using QEMU and GDB + +Starting QEMU in debug mode: + +```sh +make run-gdb +``` + +Attaching to the virtual machine: + +```sh +make attach +``` diff --git a/src/rainbowboot.s b/src/rainbowboot.s new file mode 100644 index 0000000..3d399e2 --- /dev/null +++ b/src/rainbowboot.s @@ -0,0 +1,49 @@ +%define CHAR '#' ; printed ASCII character +%define DELAY 40000 ; time delay between lines in microseconds +%define CCOUNT 7 ; amount of colors +;; colors (see https://en.wikipedia.org/wiki/BIOS_color_attributes): +%define COLORS 0x04, 0x0C, 0x0E, 0x0A, 0x0B, 0x09, 0x05 + +%define DELAY_L (DELAY & 0xFFFF) +%define DELAY_H (DELAY >> 16) + +bits 16 + +section .text + global _start + +_start: + cli ; disable hardware interrupts + xor bh, bh ; set page number to 0 + mov ax, 0x0003 ; set video mode + int 0x10 ; --- interrupt + mov ah, 0x01 ; hide cursor + mov ch, 0x3F ; --- ... + int 0x10 ; --- interrupt + mov ah, 0x02 ; move cursor to bottom + mov dh, 24 ; --- row 24 + xor dl, dl ; --- column 0 + int 0x10 ; --- interrupt + xor si, si ; set color index to 0 +.loop mov ah, 0x09 ; print 80 colored characters + mov cx, 80 ; --- set count to 80 + mov al, CHAR ; --- set character + mov bl, [colors+si] ; --- set attribute (color) + int 0x10 ; --- interrupt + mov ah, 0x86 ; sleep + mov cx, DELAY_H ; --- set microseconds (cx:dx) + mov dx, DELAY_L ; --- ... + int 0x15 ; --- interrupt + mov ah, 0x0E ; print line feed + mov al, 0x0A ; --- set character to line feed + int 0x10 ; --- interrupt + inc si ; increment color index + cmp si, CCOUNT ; --- if color index is inside bounds + jl .loop ; --- -> repeat loop + xor si, si ; --- else, set color index to 0 + jmp .loop ; repeat loop + +colors: db COLORS + + times 510 - ($ - $$) db 0 + dw 0xAA55