e8cc95a8d9f074f29aff6cbffedb5414765ef8f9
[dragonfly.git] / sys / boot / pc32 / boot2 / Makefile
1 # $FreeBSD: src/sys/boot/i386/boot2/Makefile,v 1.47 2003/06/26 03:51:57 peter Exp $
2 # $DragonFly: src/sys/boot/pc32/boot2/Makefile,v 1.18 2008/09/13 11:45:45 corecode Exp $
3
4 BINDIR?=        /boot
5 BINMODE=        444
6 CLEANFILES=     boot \
7                 sio.o
8
9 NM?=            nm
10 NXCFLAGS=
11 NXLDFLAGS=
12
13 .PATH: ${.CURDIR}/..
14
15 # A value of 0x80 enables LBA support.
16 B1FLAGS=        0x80
17
18 BOOT_COMCONSOLE_PORT?= 0x3f8
19 BOOT_COMCONSOLE_SPEED?= 9600
20 B2SIOFMT?=      0x3
21
22 .if exists(${.OBJDIR}/../btx)
23 BTX=    ${.OBJDIR}/../btx
24 .else
25 BTX=    ${.CURDIR}/../btx
26 .endif
27
28 # These origins are taken from bootasm.h.
29 #
30 REL1 = `${.OBJDIR}/bootasmdef.nx BOOT1_ORIGIN`
31 REL1D = `${.OBJDIR}/bootasmdef.nx -d BOOT1_ORIGIN`
32 ORG1 = `${.OBJDIR}/bootasmdef.nx MEM_BIOS_LADDR`
33 ORG1D = `${.OBJDIR}/bootasmdef.nx -d MEM_BIOS_LADDR`
34
35 # WARNING!  The ORG2 calculation is the origin of boot2.bin relative to 
36 # the start of the BTX *USER* address space, not the start of physical 
37 # memory.
38 #
39 ORG2=   `${.OBJDIR}/bootasmdef.nx BOOT2_VORIGIN`
40
41 # gcc3.x
42 #       -fno-guess-branch-probability
43 #       -mno-align-long-strings
44 CFLAGS= -elf -ffreestanding -Os -fno-builtin \
45         -fomit-frame-pointer \
46         -std=c99 \
47         -DBOOT2 \
48         -I${.CURDIR}/../../common \
49         -I${.CURDIR}/../../../../lib/libstand \
50         -I${.CURDIR}/../btx/lib -I. \
51         -Wall -Waggregate-return -Wbad-function-cast -Wcast-align \
52         -Wmissing-declarations -Wmissing-prototypes -Wnested-externs \
53         -Wpointer-arith -Wshadow -Wstrict-prototypes -Wwrite-strings \
54         -D__BOOT2_HACK
55
56 # Unfortunately, unit-at-a-time creates issues as yet
57 # not tracked down, in boot2, so we have to turn it off.  Note that
58 # the problem has been narrowed down to the 'boot2' code.  The /boot/loader
59 # doesn't seem to have a problem.
60 #
61 CFLAGS+= -fno-unit-at-a-time
62
63 # Disable propolice on gcc that supports it
64 .if ${CCVER} == "gcc34"
65 CFLAGS+= -fno-stack-protector
66 .endif
67
68 NSECT_32?=      16
69 NSECT_64?=      18
70 LDFLAGS=-nostdlib -static -N --gc-sections
71
72 all: boot
73
74 boot: boot1_32 boot2_32
75         cat ${.ALLSRC} > boot
76
77 .for s in _32 _64
78
79 _ADDCFLAGS$s:= -DNSECT=${NSECT$s}
80 _ts=    $s
81 .if ${_ts} == "_64"
82 _ADDCFLAGS$s+= -DDISKLABEL64
83 _ADDCFLAGS$s+= -DHAMMERFS
84 .else
85 _ADDCFLAGS$s+= -DUFS1_ONLY
86 .endif
87
88 all: boot1$s boot2$s
89
90 boot1$s: boot1$s.out
91         objcopy -S -O binary boot1$s.out ${.TARGET}
92
93 boot1$s.out: boot1$s.o bootasmdef.nx
94         ${LD} ${LDFLAGS} -e start -Ttext ${ORG1} -o ${.TARGET} ${.ALLSRC:M*.o}
95
96 boot1$s.o: boot1.S
97         ${CC} ${CFLAGS} ${_ADDCFLAGS$s} -DFLAGS=${B1FLAGS} \
98                 ${.ALLSRC} -o ${.TARGET} -c
99
100 boot2$s.o: boot2.c ${.CURDIR}/../../common/ufsread.c ${.CURDIR}/../../../../lib/libstand/hammerread.c
101         ${CC} ${CFLAGS} ${_ADDCFLAGS$s} -S -o boot2$s.s.tmp boot2.c
102         sed -e '/align/d' -e '/nop/d' < boot2$s.s.tmp > boot2$s.s
103         rm -f boot2$s.s.tmp
104         ${AS} ${AFLAGS} -o boot2$s.o boot2$s.s
105
106 boot2$s.h: boot1$s.out bootasmdef.nx
107         @echo "NOTE: boot2 using xread from boot1 at ${REL1}"
108         ${NM} -t d ${.ALLSRC} | awk '/([0-9])+ T xread/ \
109                 { x = $$1 - ORG1; \
110                 printf("#define XREADORG %#x\n", REL1 + x) }' \
111                 ORG1=${ORG1D} REL1=${REL1D} > boot2$s.h
112
113 boot2$s: boot2$s.ldr boot2$s.bin ${BTX}/btx/btx
114         btxld -v -E ${ORG2} -f bin -b ${BTX}/btx/btx -l boot2$s.ldr \
115                 -o boot2$s.ld -P 1 boot2$s.bin
116         stat boot2$s.ld | awk '{ x = (${NSECT$s} - 1) * 512 - $$8; \
117                 print x " bytes available"; if (x < 0) exit 1 }'
118         dd if=boot2$s.ld of=${.TARGET} obs=$$(( (${NSECT$s} - 1) * 512)) conv=osync 2>/dev/null
119
120 boot2$s.ldr:
121         dd if=/dev/zero of=${.TARGET} bs=512 count=1 2>/dev/null
122
123 boot2$s.bin: boot2$s.out
124         objcopy -S -O binary boot2$s.out ${.TARGET}
125
126 boot2$s.out: boot2$s.o sio.o
127         ${LD} ${LDFLAGS} -Ttext ${ORG2} -o ${.TARGET} \
128                 ${BTX}/lib/crt0.o ${.ALLSRC}
129
130 boot2$s.o: boot2$s.h
131
132 CLEANFILES+=    boot1$s boot1$s.out boot1$s.o \
133                 boot2$s boot2$s.ldr boot2$s.bin boot2$s.ld boot2$s.out boot2$s.o boot2$s.h boot2$s.s
134
135 .endfor
136
137 sio.o: sio.S
138         ${CC} -DSIOPRT=${BOOT_COMCONSOLE_PORT} -DSIOFMT=${B2SIOFMT} \
139                 -DSIOSPD=${BOOT_COMCONSOLE_SPEED} \
140                 ${.ALLSRC} -o ${.TARGET} -c
141
142 install:
143         ${INSTALL} -o ${BINOWN} -g ${BINGRP} -m ${BINMODE} \
144                 boot ${DESTDIR}${BINDIR}/boot
145         ${INSTALL} -o ${BINOWN} -g ${BINGRP} -m ${BINMODE} \
146                 boot1_32 ${DESTDIR}${BINDIR}/boot1
147         ${INSTALL} -o ${BINOWN} -g ${BINGRP} -m ${BINMODE} \
148                 boot2_32 ${DESTDIR}${BINDIR}/boot2
149         ${INSTALL} -o ${BINOWN} -g ${BINGRP} -m ${BINMODE} \
150                 boot1_64 ${DESTDIR}${BINDIR}/boot1_64
151         ${INSTALL} -o ${BINOWN} -g ${BINGRP} -m ${BINMODE} \
152                 boot2_64 ${DESTDIR}${BINDIR}/boot2_64
153
154 .include <bsd.prog.mk>
155
156 .if defined(REALLY_AMD64)
157 boot2.o: machine
158
159 beforedepend ${OBJS}: machine
160
161 machine:
162         ${LN} -sf ${.CURDIR}/../../../i386/include machine
163
164 CLEANFILES+=    machine
165 .endif
166
167 CLEANFILES+=    bootasmdef.nx
168