Johannes Zellner пре 8 месеци
родитељ
комит
5bd95800b3
5 измењених фајлова са 135 додато и 61 уклоњено
  1. 26 24
      Makefile
  2. 71 20
      boot.S
  3. 18 12
      kernel.c
  4. 15 0
      kernel.ld
  5. 5 5
      linker.ld

+ 26 - 24
Makefile

@@ -1,22 +1,19 @@
-# Minimal x86 floppy boot: build boot.bin and optionally write to floppy.
-# Usage: make [all] && [sudo] make floppy
+# Bootloader: 512-byte boot sector + kernel at sector 2. Builds image.bin for QEMU.
 
-CC     = gcc
-AS     = as
-LD     = ld
+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
+CFLAGS   = -ffreestanding -fno-pie -m32 -nostdlib -fno-stack-protector \
+           -fno-asynchronous-unwind-tables -Wall -Wextra -O2 -c
+ASFLAGS  = --32
+BOOT_LDFLAGS   = -T linker.ld -nostdlib -m elf_i386
+KERNEL_LDFLAGS = -T kernel.ld -nostdlib -m elf_i386
 
-FLOPPY ?= /dev/fd0
+.PHONY: all clean run
 
-OBJS = boot.o kernel.o
-
-.PHONY: all clean floppy run
-
-all: boot.bin
+all: image.bin
 
 boot.o: boot.S
 	$(AS) $(ASFLAGS) -o $@ $<
@@ -24,8 +21,11 @@ boot.o: boot.S
 kernel.o: kernel.c
 	$(CC) $(CFLAGS) -o $@ $<
 
-boot.elf: $(OBJS)
-	$(LD) $(LDFLAGS) -o $@ $(OBJS)
+boot.elf: boot.o
+	$(LD) $(BOOT_LDFLAGS) -o $@ $<
+
+kernel.elf: kernel.o
+	$(LD) $(KERNEL_LDFLAGS) -o $@ $<
 
 boot.bin: boot.elf
 	$(OBJCOPY) -O binary --only-section=.boot $< $@.tmp
@@ -33,14 +33,16 @@ boot.bin: boot.elf
 	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
+kernel.bin: kernel.elf
+	$(OBJCOPY) -O binary $< $@
+
+image.bin: boot.bin kernel.bin
+	dd if=/dev/zero of=$@ bs=512 count=2880 2>/dev/null
+	dd if=boot.bin of=$@ bs=512 conv=notrunc 2>/dev/null
+	dd if=kernel.bin of=$@ bs=512 seek=1 conv=notrunc 2>/dev/null
 
-# 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
+run: image.bin
+	qemu-system-i386 -fda image.bin 2>/dev/null || qemu-system-x86_64 -fda image.bin
 
 clean:
-	rm -f $(OBJS) boot.elf boot.bin boot.bin.tmp
+	rm -f boot.o kernel.o boot.elf kernel.elf boot.bin kernel.bin boot.bin.tmp image.bin

+ 71 - 20
boot.S

@@ -1,40 +1,91 @@
-/* Boot sector stub: run at 0x7C00, set VGA mode 0x03, print message, call kernel_main. */
+/* Minimal x86 boot sector: load kernel from sector 2 to 0x7E00,
+ * switch to protected mode, jump to kernel (C code at 0x7E00). */
+
 .code16
 .section .text
 
 .globl _start
 _start:
+	/* Segment and stack for real mode */
 	xorw  %ax, %ax
 	movw  %ax, %ds
 	movw  %ax, %ss
 	movw  $0x7c00, %sp
 
-	/* Set VGA mode 0x03 (80x25 color) via BIOS int 0x10 */
+	/* Save boot drive (BIOS puts it in DL) */
+	movb  %dl, boot_drive
+
+	/* Set VGA mode 0x03 (80x25 color text) so 0xB8000 is the visible framebuffer */
 	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:
+	/* Load kernel: 4 sectors from sector 2 into 0x7E00 (ES:BX = 0x07E0:0) */
+	movw  $0x07e0, %ax
+	movw  %ax, %es
+	xorw  %bx, %bx
+	movb  $0x02, %ah          /* int 0x13 read */
+	movb  $4, %al             /* 4 sectors */
+	xorw  %cx, %cx
+	movb  $2, %cl             /* sector 2 (1-based) */
+	xorw  %dx, %dx             /* head 0, drive in DL */
+	movb  boot_drive, %dl
+	int   $0x13
+	jc    halt                /* on error just halt */
+
+	/* Protected mode: load GDT, set CR0.PE, far jump to 32-bit code */
+	cli
+	lgdt  gdtr
+	movl  %cr0, %eax
+	orl   $1, %eax
+	movl  %eax, %cr0
+	.byte 0x66, 0xea
+	.long pm_start
+	.word 0x08
+
+	.align 4
+boot_drive:
+	.byte 0
+
+/* GDT (flat 4GB code and data) */
+	.align 8
+gdt:
+	.quad 0x0000000000000000
+	/* 0x08: 32-bit code, DPL=0, base=0, limit=0xfffff (4GB) */
+	.word 0xffff, 0x0000
+	.byte 0x00, 0x9a, 0xcf, 0x00
+	/* 0x10: 32-bit data */
+	.word 0xffff, 0x0000
+	.byte 0x00, 0x92, 0xcf, 0x00
+gdt_end:
 
-	call  kernel_main
+gdtr:
+	.word gdt_end - gdt - 1
+	.long gdt                 /* linear address (linker uses 0x7C00 base) */
 
+halt:
 	cli
-3:	hlt
-	jmp   3b
+1:	hlt
+	jmp   1b
+
+/* --- 32-bit protected mode --- */
+.code32
+pm_start:
+	movw  $0x10, %ax
+	movw  %ax, %ds
+	movw  %ax, %es
+	movw  %ax, %fs
+	movw  %ax, %gs
+	movw  %ax, %ss
+	movl  $0x8000, %esp       /* stack at 0x8000 */
+
+	/* Jump to kernel at 0x7E00 (direct; *0x7e00 would be indirect = wrong) */
+	movl  $0x7e00, %eax
+	jmp   *%eax
 
-msg:
-	.asciz "Welcome to nebos"
+1:	cli
+	hlt
+	jmp   1b
 
+/* Boot signature at offset 510 (linker places it) */
 .section .bootsig
 .byte 0x55, 0xaa

+ 18 - 12
kernel.c

@@ -1,20 +1,26 @@
-/* Freestanding C: print "Welcome to nebos" via BIOS int 0x10 teletype. */
+/* Kernel: entered from bootloader in 32-bit protected mode at 0x7E00.
+ * Must be linked so this function is at 0x7E00 (see kernel.ld). */
 
-static void bios_putchar(int c)
-{
-	__asm__ volatile (
-		"int $0x10"
-		:
-		: "a" ((0x0E << 8) | (unsigned char)c),
-		  "b" (0x0000)
-	);
-}
+#define VGA_BASE ((volatile unsigned short *)0xB8000)
+#define VGA_ATTR 0x0A  /* green on black */
+
+/* String in .text so it loads with the kernel */
+static const char msg[] __attribute__((section(".text"))) = "Hello from C (protected mode)";
 
+/* Entry point: must be first in .text so bootloader jump to 0x7E00 hits it */
+void kernel_main(void) __attribute__((section(".text.entry")));
 void kernel_main(void)
 {
-	const char msg[] = "Welcome to nebos";
 	unsigned i;
 
+	/* Clear screen */
+	for (i = 0; i < 80 * 25; i++)
+		VGA_BASE[i] = (unsigned short)(VGA_ATTR << 8);
+
+	/* Print message */
 	for (i = 0; msg[i] != '\0'; i++)
-		bios_putchar((unsigned char)msg[i]);
+		VGA_BASE[i] = (unsigned short)((VGA_ATTR << 8) | (unsigned char)msg[i]);
+
+	for (;;)
+		__asm__ volatile ("hlt");
 }

+ 15 - 0
kernel.ld

@@ -0,0 +1,15 @@
+/* Kernel runs at 0x7E00; entry section first so 0x7E00 = kernel_main */
+ENTRY(kernel_main)
+
+SECTIONS
+{
+	. = 0x7E00;
+	.text : AT(0x7E00) {
+		*(.text.entry)
+		*(.text .text.*)
+	}
+	.rodata : { *(.rodata .rodata.*) }
+	.data : { *(.data .data.*) }
+	.bss : { *(.bss .bss.*) *(COMMON) }
+	_end = .;
+}

+ 5 - 5
linker.ld

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