summaryrefslogtreecommitdiff
path: root/src/Makefile
blob: f352a6c753672ebdffe7ce0c6557c054d24b1eb8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
CC := gcc
INCLUDES = -I. -Ilibc -Ihardware -Idrivers -Idrivers/hdi -Idrivers/hdi/ps2 -Ikernel
CFLAGS := -std=c99 -m32 -ffreestanding -O0 -g -Wall -Werror $(INCLUDES)
LD := ld
NASMFLAGS := -f elf32
NASM := nasm
OBJCOPY := objcopy
MAGIC := $(shell printf '%x' `date +%s`)

all: image.bin kernel.sym

# truncate to correct number of sectors, we have
# 512 (boot, stage 1) + N * 512 (N currenty is 3, stage 2) = 3072 for boot.bin
# + M * 512 (M is currently 7) = 3144 for kernel.bin
# + 1 * 512 = 512 for magic.bin
# (M + N + 1 is the number of sectors to be read in stage 2, as stage 1
# loads only the first sector, and stage 1 loads 3 sectors of stage 2,
# adapt NOF_LOAD_SECTORS to 37)
# then we make sure the image has the size of a 1.44 MB floppy
# (emulators like qemu do some guess work for CHS resolution based
# on the size of the image)
image.bin: boot.bin kernel.bin magic.bin
	cat boot.bin kernel.bin > image.tmp
	truncate -s 20480 image.tmp
	cat image.tmp magic.bin > image.bin
	truncate -s 1474560 image.bin
	
boot.bin: boot/boot.asm boot/boot_gdt.asm boot/stage1_functions.asm boot/stage2_real_functions.asm boot/stage2_pm_functions.asm boot/stage2_switch_mode.asm boot/stage2_a20.asm
	$(NASM) boot/boot.asm -DMAGIC='"$(MAGIC)"' -f bin -o boot.bin

kernel.bin: kernel.elf
	$(OBJCOPY) -O binary kernel.elf kernel.bin

kernel.sym: kernel.elf
	$(OBJCOPY) --only-keep-debug kernel.elf kernel.sym

kernel.elf: kernel/kernel.o kernel/kernel_asm.o kernel/console.o kernel/vgatext.o kernel/serial.o hardware/port.o hardware/port_asm.o hardware/interrupts.o hardware/interrupts_asm.o hardware/pci.o drivers/driver.o drivers/hdi/ps2/keyboard.o drivers/hdi/ps2/mouse.o libc/string.o libc/stdlib.o libc/stdio.o libc/setjmp.o
	$(LD) -o kernel.elf -N -n -Ttext 0x8400 --oformat elf32-i386 \
		kernel/kernel.o kernel/kernel_asm.o \
		kernel/console.o kernel/vgatext.o kernel/serial.o \
		hardware/port.o hardware/port_asm.o \
		hardware/interrupts.o hardware/interrupts_asm.o \
		hardware/pci.o \
		drivers/driver.o \
		drivers/hdi/ps2/keyboard.o drivers/hdi/ps2/mouse.o \
		libc/string.o libc/stdlib.o libc/stdio.o libc/setjmp.o

magic.bin: boot/magic.asm
	$(NASM) boot/magic.asm -DMAGIC='"$(MAGIC)"' -f bin -o magic.bin
	
kernel/kernel.o: kernel/kernel.c
	$(CC) $(CFLAGS) -c -o kernel/kernel.o kernel/kernel.c

kernel/kernel_asm.o: kernel/kernel.asm
	$(NASM) kernel/kernel.asm $(NASMFLAGS) -o kernel/kernel_asm.o

hardware/port.o: hardware/port.c hardware/port.h
	$(CC) $(CFLAGS) -c -o hardware/port.o hardware/port.c

hardware/port_asm.o: hardware/port.asm
	$(NASM) hardware/port.asm $(NASMFLAGS) -o hardware/port_asm.o

kernel/console.o: kernel/console.c kernel/console.h kernel/vgatext.h kernel/serial.h
	$(CC) $(CFLAGS) -c -o kernel/console.o kernel/console.c

kernel/vgatext.o: kernel/vgatext.c kernel/vgatext.h
	$(CC) $(CFLAGS) -c -o kernel/vgatext.o kernel/vgatext.c

kernel/serial.o: kernel/serial.c kernel/serial.h
	$(CC) $(CFLAGS) -c -o kernel/serial.o kernel/serial.c

hardware/interrupts.o: hardware/interrupts.c hardware/interrupts.h
	$(CC) $(CFLAGS) -c -o hardware/interrupts.o hardware/interrupts.c

hardware/interrupts_asm.o: hardware/interrupts.asm
	$(NASM) hardware/interrupts.asm $(NASMFLAGS) -o hardware/interrupts_asm.o

hardware/pci.o: hardware/pci.c hardware/pci.h
	$(CC) $(CFLAGS) -c -o hardware/pci.o hardware/pci.c

drivers/driver.o: drivers/driver.c drivers/driver.h
	$(CC) $(CFLAGS) -c -o drivers/driver.o drivers/driver.c

drivers/hdi/ps2/keyboard.o: drivers/hdi/ps2/keyboard.c drivers/hdi/ps2/keyboard.h
	$(CC) $(CFLAGS) -c -o drivers/hdi/ps2/keyboard.o drivers/hdi/ps2/keyboard.c

drivers/hdi/ps2/mouse.o: drivers/hdi/ps2/mouse.c drivers/hdi/ps2/mouse.h
	$(CC) $(CFLAGS) -c -o drivers/hdi/ps2/mouse.o drivers/hdi/ps2/mouse.c

libc/string.o: libc/string.c libc/string.h
	$(CC) $(CFLAGS) -c -o libc/string.o libc/string.c

libc/stdlib.o: libc/stdlib.c libc/stdlib.h
	$(CC) $(CFLAGS) -c -o libc/stdlib.o libc/stdlib.c

libc/stdio.o: libc/stdio.c libc/stdio.h
	$(CC) $(CFLAGS) -c -o libc/stdio.o libc/stdio.c

libc/setjmp.o: libc/setjmp.asm
	$(NASM) libc/setjmp.asm $(NASMFLAGS) -o libc/setjmp.o

clean:
	-rm -f boot.bin kernel.bin kernel.sym kernel.elf image.bin magic.bin boot.map image.tmp \
		serial.log \
		libc/*.o hardware/*.o kernel/*.o \
		drivers/*.o drivers/*/*.o drivers/*/*/*.o 

run-qemu-hd: image.bin
	qemu-system-i386 -net nic,model=ne2k_pci -d guest_errors -m 32 -drive "file=image.bin,if=ide,format=raw" \
		-serial file:serial.log

run-qemu-usb: image.bin
	qemu-system-i386 -net nic,model=ne2k_pci -d guest_errors -m 32 -usb -usbdevice disk:/dev/sde \
		-serial file:serial.log

run-qemu: image.bin
	qemu-system-i386 -net nic,model=ne2k_pci -d guest_errors -m 32 -drive "file=image.bin,if=floppy,format=raw" \
		-serial file:serial.log

run-qemu-debug: image.bin
	qemu-system-i386 -net nic,model=ne2k_pci -S -s -d guest_errors -m 32 -drive "file=image.bin,if=floppy,format=raw" \
		-serial file:serial.log

run-bochs:
	bochs -q -f bochs.config 'boot:floppy' 'floppya: 1_44=image.bin, status=inserted'

run-bochs-hd:
	bochs -q -f bochs.config 'boot:disk' 'ata0-master: type=disk, path=image.bin, mode=flat, cylinders=64, heads=4, spt=8, translation=none'