summaryrefslogtreecommitdiff
path: root/src/Makefile
blob: c7925bf5d0369b329fae49004c69ad77f9b7807c (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
CC := gcc
CFLAGS := -std=c99 -m32 -ffreestanding -O0 -g -Wall -Werror
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.asm boot_gdt.asm stage1_functions.asm stage2_real_functions.asm stage2_pm_functions.asm stage2_switch_mode.asm stage2_a20.asm
	$(NASM) 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.o kernel_asm.o console.o vga.o serial.o port.o port_asm.o interrupts.o interrupts_asm.o driver.o pci.o keyboard.o mouse.o string.o stdlib.o stdio.o setjmp.o
	$(LD) -o kernel.elf -N -n -Ttext 0x8400 --oformat elf32-i386 \
		kernel.o kernel_asm.o console.o vga.o serial.o \
		port.o port_asm.o interrupts.o interrupts_asm.o \
		driver.o pci.o \
		keyboard.o mouse.o \
		string.o stdlib.o stdio.o setjmp.o

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

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

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

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

console.o: console.c console.h vga.h serial.h
	$(CC) $(CFLAGS) -c -o console.o console.c

vga.o: vga.c vga.h
	$(CC) $(CFLAGS) -c -o vga.o vga.c

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

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

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

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

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

keyboard.o: keyboard.c keyboard.h
	$(CC) $(CFLAGS) -c -o keyboard.o keyboard.c

mouse.o: mouse.c mouse.h
	$(CC) $(CFLAGS) -c -o mouse.o mouse.c

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

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

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

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

clean:
	-rm -f boot.bin kernel.bin kernel.sym kernel.elf image.bin magic.bin *.o boot.map image.tmp

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'