xref: /dragonfly/stand/boot/pc32/boot2/Makefile (revision 655933d6)
1# $FreeBSD: src/sys/boot/i386/boot2/Makefile,v 1.47 2003/06/26 03:51:57 peter Exp $
2
3BINDIR?=	/boot
4BINMODE=	444
5CLEANFILES=	boot \
6		sio.o
7
8CSTD?=		c99
9
10NM?=		nm
11NXCFLAGS=
12NXLDFLAGS=
13
14.PATH: ${.CURDIR}/..
15.PATH: ${.CURDIR}/../../common
16.PATH: ${.CURDIR}/../../../lib
17.PATH: ${.CURDIR}/../../../../sys/libkern
18
19# A value of 0x80 enables LBA support.
20B1FLAGS=	0x80
21
22BOOT_COMCONSOLE_PORT?= 0x3f8
23BOOT_COMCONSOLE_SPEED?= 115200
24B2SIOFMT?=	0x3
25
26.if exists(${.OBJDIR}/../btx)
27BTX=	${.OBJDIR}/../btx
28.else
29BTX=	${.CURDIR}/../btx
30.endif
31
32# These origins are taken from bootasm.h.
33#
34REL1 = `${.OBJDIR}/../asmdef/bootasmdef.nx BOOT1_ORIGIN`
35REL1D = `${.OBJDIR}/../asmdef/bootasmdef.nx -d BOOT1_ORIGIN`
36ORG1 = `${.OBJDIR}/../asmdef/bootasmdef.nx MEM_BIOS_LADDR`
37ORG1D = `${.OBJDIR}/../asmdef/bootasmdef.nx -d MEM_BIOS_LADDR`
38
39# WARNING!  The ORG2 calculation is the origin of boot2.bin relative to
40# the start of the BTX *USER* address space, not the start of physical
41# memory.
42#
43ORG2=	`${.OBJDIR}/../asmdef/bootasmdef.nx BOOT2_VORIGIN`
44
45WORLD_CCOPTLEVEL=	s	# -Os for size constraints in boot2_32
46CFLAGS=	-Os \
47	-fomit-frame-pointer \
48	-fno-unwind-tables \
49	-fno-asynchronous-unwind-tables \
50	-DBOOT2 \
51	-I${.CURDIR}/../../common \
52	-I${.CURDIR}/../../../lib \
53	-I${.CURDIR}/../btx/lib -I. \
54	-Wall -Waggregate-return -Wbad-function-cast -Wcast-align \
55	-Wmissing-declarations -Wmissing-prototypes -Wnested-externs \
56	-Wpointer-arith -Wshadow -Wstrict-prototypes -Wwrite-strings
57
58DEBUG_FLAGS=
59
60# Tell gcc that it shouldn't do fancy optimizations for newer processors.
61# Otherwise it winds up creating larger code and we won't be able to fit boot2.
62CFLAGS+= -march=i386
63
64# Unfortunately, unit-at-a-time creates issues as yet
65# not tracked down, in boot2, so we have to turn it off.  Note that
66# the problem has been narrowed down to the 'boot2' code.  The /boot/loader
67# doesn't seem to have a problem.  Added to BOOT2_GCC_FLAGS since clang
68# doesn't recognize this flag.  The no-guess-branch-probability flag is also
69# gcc-specific so it was moved to BOOT2_GCC_FLAGS as well.
70#
71CFLAGS+= ${BOOT2_GCC_FLAGS}
72
73# boot2 area in 32 bit disklabel is 16 sectors (8K)
74#
75# boot2 area in 64 bit disklabel is 64 sectors (32K).
76# However, the boot1 btx loader puts boot2 at a physical address
77# of MEM_BTX_USR+BOOT2_VORIGIN which is typically 0xC000, so we
78# cannot make boot2 any larger then 16KB or we overflow the segment.
79#
80NSECT_32?=	16
81NSECT_64?=	30
82LDFLAGS=	-nostdlib -static -N --gc-sections
83LINKSCRIPT=	--script ${.CURDIR}/boot2.ldscript
84
85all: boot
86
87boot: boot1_32 boot2_32
88	cat ${.ALLSRC} > boot
89
90.for s in _32 _64
91
92_ADDCFLAGS$s:= -DNSECT=${NSECT$s}
93_ADDCFLAGS$s+= ${BOOT_CCASM_FLAGS}
94_ts=	$s
95.if ${_ts} == "_64"
96_ADDCFLAGS$s+= -DDISKLABEL64
97_ADDCFLAGS$s+= -DHAMMER2FS -DUFS
98_ADDOBJS$s+= hammer2$s.o ufsread$s.o scrc32$s.o
99.else
100_ADDCFLAGS$s+= -DUFS -DUFS1_ONLY
101_ADDOBJS$s+= ufsread$s.o
102.endif
103
104all: boot1$s boot2$s
105
106boot1$s: boot1$s.out
107	objcopy -S -O binary boot1$s.out ${.TARGET}
108
109boot1$s.out: boot1$s.o bootasm.h
110	${LD} ${LDFLAGS:N-Wl,*} -e start -Ttext ${ORG1} -o ${.TARGET} ${.ALLSRC:M*.o}
111
112boot1$s.o: boot1.S
113	${CC} ${CFLAGS} ${_ADDCFLAGS$s} -DFLAGS=${B1FLAGS} \
114		${BOOT_CCASM_FLAGS} ${.ALLSRC} -o ${.TARGET} -c
115
116#${.CURDIR}/../../common/ufsread.c ${.CURDIR}/../../../lib/hammer2.c
117
118boot2$s.s: boot2.c boot2$s.h ufsread.c hammer2.c scrc32.c
119	${CC} ${CFLAGS} ${_ADDCFLAGS$s} -S -o boot2$s.s.tmp ${.CURDIR}/boot2.c
120	sed -e '/align/d' -e '/nop/d' < ${.TARGET}.tmp > ${.TARGET}
121	rm -f ${.TARGET}.tmp
122
123boot2$s.h: boot1$s.out bootasm.h
124	@echo "NOTE: boot2 using xread from boot1 at ${REL1}"
125	${NM} -t d ${.ALLSRC:M*.out} | awk '/([0-9])+ T xread/ \
126	 	{ x = $$1 - ORG1; \
127		printf("#define XREADORG %#x\n", REL1 + x) }' \
128	 	ORG1=${ORG1D} REL1=${REL1D} > boot2$s.h
129
130boot2$s: boot2$s.ldr boot2$s.bin ${BTX}/btx/btx
131	${.OBJDIR}/../btxld/btxld.nx -v -E ${ORG2} -f bin -b ${BTX}/btx/btx \
132		-l boot2$s.ldr -o boot2$s.ld -P 1 boot2$s.bin
133	stat boot2$s.ld | awk '{ x = (${NSECT$s} - 1) * 512 - $$8; \
134		print x " bytes available"; if (x < 0) exit 1 }'
135	dd if=boot2$s.ld of=${.TARGET} obs=$$(( (${NSECT$s} - 1) * 512)) conv=osync 2>/dev/null
136
137boot2$s.ldr:
138	dd if=/dev/zero of=${.TARGET} bs=512 count=1 2>/dev/null
139
140boot2$s.bin: boot2$s.out
141	objcopy -S -O binary boot2$s.out ${.TARGET}
142
143boot2$s.out: boot2$s.o sio.o ${_ADDOBJS$s}
144	${LD} ${LDFLAGS:N-Wl,*} -Ttext ${ORG2} ${LINKSCRIPT} -o ${.TARGET} \
145		${BTX}/lib/crt0.o ${.ALLSRC}
146
147CLEANFILES+=	boot1$s boot1$s.out boot1$s.o \
148		boot2$s boot2$s.ldr boot2$s.bin \
149		boot2$s.ld boot2$s.out boot2$s.o boot2$s.h boot2$s.s \
150		${_ADDOBJS$s}
151
152hammer2$s.o: hammer2.c
153	${CC} ${CFLAGS} ${_ADDCFLAGS$s} ${.ALLSRC} -o ${.TARGET} -c
154
155ufsread$s.o: ufsread.c
156	${CC} ${CFLAGS} ${_ADDCFLAGS$s} ${.ALLSRC} -o ${.TARGET} -c
157
158scrc32$s.o: scrc32.c
159	${CC} ${CFLAGS} ${_ADDCFLAGS$s} ${.ALLSRC} -o ${.TARGET} -c
160
161.endfor
162
163sio.o: sio.S
164	${CC} ${CFLAGS} -DSIOPRT=${BOOT_COMCONSOLE_PORT} -DSIOFMT=${B2SIOFMT} \
165		-DSIOSPD=${BOOT_COMCONSOLE_SPEED} \
166		${.ALLSRC} -o ${.TARGET} -c
167
168install:
169	${INSTALL} -o ${BINOWN} -g ${BINGRP} -m ${BINMODE} \
170		boot ${DESTDIR}${BINDIR}/boot
171	${INSTALL} -o ${BINOWN} -g ${BINGRP} -m ${BINMODE} \
172		boot1_32 ${DESTDIR}${BINDIR}/boot1
173	${INSTALL} -o ${BINOWN} -g ${BINGRP} -m ${BINMODE} \
174		boot2_32 ${DESTDIR}${BINDIR}/boot2
175	${INSTALL} -o ${BINOWN} -g ${BINGRP} -m ${BINMODE} \
176		boot1_64 ${DESTDIR}${BINDIR}/boot1_64
177	${INSTALL} -o ${BINOWN} -g ${BINGRP} -m ${BINMODE} \
178		boot2_64 ${DESTDIR}${BINDIR}/boot2_64
179
180.include <bsd.prog.mk>
181