Johannes Zellner пре 8 месеци
комит
30f1d31b5d
4 измењених фајлова са 121 додато и 0 уклоњено
  1. 46 0
      Makefile
  2. 40 0
      boot.S
  3. 20 0
      kernel.c
  4. 15 0
      linker.ld

+ 46 - 0
Makefile

@@ -0,0 +1,46 @@
+# Minimal x86 floppy boot: build boot.bin and optionally write to floppy.
+# Usage: make [all] && [sudo] make floppy
+
+CC     = gcc
+AS     = as
+LD     = ld
+OBJCOPY = objcopy
+
+CFLAGS  = -ffreestanding -fno-pie -m16 -nostdlib -fno-stack-protector -fno-asynchronous-unwind-tables -Wall -Wextra -O2 -c
+ASFLAGS = --32
+LDFLAGS = -T linker.ld -nostdlib -m elf_i386
+
+FLOPPY ?= /dev/fd0
+
+OBJS = boot.o kernel.o
+
+.PHONY: all clean floppy run
+
+all: boot.bin
+
+boot.o: boot.S
+	$(AS) $(ASFLAGS) -o $@ $<
+
+kernel.o: kernel.c
+	$(CC) $(CFLAGS) -o $@ $<
+
+boot.elf: $(OBJS)
+	$(LD) $(LDFLAGS) -o $@ $(OBJS)
+
+boot.bin: boot.elf
+	$(OBJCOPY) -O binary --only-section=.boot $< $@.tmp
+	dd if=$@.tmp of=$@ bs=1 count=510 2>/dev/null
+	printf '\x55\xaa' >> $@
+	rm -f $@.tmp
+
+# Write first sector to floppy (often requires: sudo make floppy)
+floppy: boot.bin
+	dd if=boot.bin of=$(FLOPPY) bs=512 count=1 conv=notrunc
+
+# Test in QEMU (optional: install qemu-system-i386 or qemu-system-x86)
+# Use -display gtk for a window; install qemu-system-gtk or run in a graphical session
+run: boot.bin
+	qemu-system-i386 -display gtk -drive file=boot.bin,format=raw,if=floppy || qemu-system-x86_64 -display gtk -drive file=boot.bin,format=raw,if=floppy
+
+clean:
+	rm -f $(OBJS) boot.elf boot.bin boot.bin.tmp

+ 40 - 0
boot.S

@@ -0,0 +1,40 @@
+/* Boot sector stub: run at 0x7C00, set VGA mode 0x03, print message, call kernel_main. */
+.code16
+.section .text
+
+.globl _start
+_start:
+	xorw  %ax, %ax
+	movw  %ax, %ds
+	movw  %ax, %ss
+	movw  $0x7c00, %sp
+
+	/* Set VGA mode 0x03 (80x25 color) via BIOS int 0x10 */
+	movw  $0x0003, %ax
+	int   $0x10
+
+	/* Print "Welcome to nebos" in green by writing to VGA text buffer at 0xB8000 */
+	movw  $0xb800, %ax
+	movw  %ax, %es		/* ES = VGA segment */
+	xorw  %di, %di		/* start at offset 0 (top-left) */
+	movw  $msg, %si
+	movb  $0x02, %ah	/* attribute: green on black */
+1:	lodsb
+	cmpb  $0, %al
+	je    2f
+	movw  %ax, %es:(%di)	/* write (attr<<8)|char to screen */
+	addw  $2, %di
+	jmp   1b
+2:
+
+	call  kernel_main
+
+	cli
+3:	hlt
+	jmp   3b
+
+msg:
+	.asciz "Welcome to nebos"
+
+.section .bootsig
+.byte 0x55, 0xaa

+ 20 - 0
kernel.c

@@ -0,0 +1,20 @@
+/* Freestanding C: print "Welcome to nebos" via BIOS int 0x10 teletype. */
+
+static void bios_putchar(int c)
+{
+	__asm__ volatile (
+		"int $0x10"
+		:
+		: "a" ((0x0E << 8) | (unsigned char)c),
+		  "b" (0x0000)
+	);
+}
+
+void kernel_main(void)
+{
+	const char msg[] = "Welcome to nebos";
+	unsigned i;
+
+	for (i = 0; msg[i] != '\0'; i++)
+		bios_putchar((unsigned char)msg[i]);
+}

+ 15 - 0
linker.ld

@@ -0,0 +1,15 @@
+/* Boot sector: load at 0x7C00, exactly 512 bytes, signature at 510-511. */
+ENTRY(_start)
+
+SECTIONS
+{
+	. = 0x7C00;
+	.boot : AT(0x7C00) {
+		*(.text)
+		*(.rodata*)
+		. = 0x7C00 + 0x1FE;
+		*(.bootsig)
+		. = 0x7C00 + 0x200;
+	}
+	/DISCARD/ : { *(.eh_frame) *(.note.GNU-stack) *(.note.gnu.property) }
+}