172770Sluigi#!/bin/sh -
272770Sluigi#
372770Sluigi# $FreeBSD$
4155136Sluigi# This file requires sysutils/makefs to run
572770Sluigi#
6190378Sluigi# The PicoBSD build script. Invoked as
772770Sluigi#
8190378Sluigi#	picobsd [options] image_type [site_name]
972770Sluigi#
10239738Sluigi# CWARNFLAGS can be used to pass -Wall or similar options
11239738Sluigi#
12190378Sluigi# Where image_type is a directory with the picobsd config info,
13190378Sluigi# and ${image_type}/floppy.tree.${site_name} contains
1472770Sluigi# optional site-specific configuration.
1572770Sluigi#
1672770Sluigi# For Options, see the bottom of the file where the processing is
1778494Sluigi# done. The picobsd(8) manpage might be of some help, but code and docs
18190378Sluigi# tend to lose sync over time.
1972770Sluigi#
2072770Sluigi# This script depends on the following files:
2172770Sluigi#
2272770Sluigi# in ${PICO_TREE} :
2372770Sluigi#   Makefile.conf	Makefile used to build the kernel
2472770Sluigi#   config		shell variables, sourced here.
2572770Sluigi#   mfs.mtree		mtree config file
2672770Sluigi#   floppy.tree/	files which go on the floppy
2772770Sluigi#   mfs_tree/		files which go onto the mfs
2872770Sluigi#
2972770Sluigi# in ${MY_TREE} :
3072770Sluigi#   PICOBSD		kernel config file
3172770Sluigi#   config		shell variables, sourced here.
3272770Sluigi#   crunch.conf		crunchgen configuration
33188835Sluigi#   mfs.mtree		overrides ${PICO_TREE}/mfs.mtree
3472770Sluigi#   floppy.tree.exclude	files from floppy.tree/ which we do not need here.
35188835Sluigi#   floppy.tree/	local additions to ${PICO_TREE}/mfs_free
3672770Sluigi#   floppy.tree.${site}/ same as above, site specific.
37122229Ssimokawa#   mfs_tree/		local additions to the mfs_free
38190378Sluigi#   buildtree.mk	optional Makefile to build an extension for floppy tree
39188835Sluigi#			(generated in buildtree/ )
4072770Sluigi
4172770Sluigi#
4272770Sluigi#--- The main entry point is at the end.
4372770Sluigi#
4472770Sluigi
45190378Sluigi# There are two initialization functions:
4684377Sluigi#
47190378Sluigi# + set_defaults
48190378Sluigi#   is run on entry to the script, and is used to set default values
49190378Sluigi#   for all variables that do not depend on image type and source tree.
5084377Sluigi#
51190378Sluigi# + set_build_parameters
52190378Sluigi#   is run after command line parsing
53190378Sluigi#
54190378Sluigi# VARIABLE NAMES:
5584377Sluigi# + variables that control operation (e.g. verbosity) and are generally
5684377Sluigi#   set from the command line have o_ ("option") as a name prefix
5784377Sluigi#
58190378Sluigi# + variables that contain pathnames and values that should not change
5984377Sluigi#   have c_ ("constant") as a name prefix
6084377Sluigi#
6184377Sluigi# + variables exported to Makefiles and subshells are CAPITAL
6284377Sluigi#
6384377Sluigi# + variables local to the script are lowercase, possibly with
64190378Sluigi#   an l_ ("local") prefix.
65190378Sluigi#
66190378Sluigi# There are unfortunately exceptions:
67190378Sluigi# name, l_usrtree, l_objtree
6872770Sluigi
6984377Sluigi# SRC points to your FreeBSD source tree.
7084377Sluigi# l_usrtree points to the /usr subdir for the source tree.
7184377Sluigi#     Normally /usr or ${SRC}/../usr
72227878Sluigi# l_objtree points to the obj tree. Normally ${l_usrtree}/obj-pico-${o_arch}
73190378Sluigi# c_label is either bsdlabel or disklabel
7484377Sluigi# PICO_TREE is where standard picobsd stuff resides.
7584377Sluigi#     Normally ${SRC}/release/picobsd
7684377Sluigi# You can set SRC with --src <directory>
7784377Sluigi# It is not recommended to override the other variables.
7872770Sluigi
7984377Sluigi# MY_TREE (set later) is where this floppy type resides.
8084377Sluigi# BUILDDIR is the build directory
8172770Sluigi
8284377Sluigi# log something on stdout if verbose.
8384377Sluigio_verbose=0	# this needs to be here!
84190378Sluigilog() {	#	message
85173602Sluigi    local foo
86173602Sluigi    [ ${o_verbose} -gt 0 ] && printf "\n*** %s\n" "$*"
87173602Sluigi    [ ${o_verbose}  -gt 1 ] && read -p "=== Press enter to continue" foo
88173602Sluigi    return 0
8984377Sluigi}
9083723Sjoe
91190378Sluigi# unconditionally log and wait for input
92190378Sluigilogverbose() {	# message
93127266Sluigi    local foo
9491846Sluigi    printf "\n*** %s\n" "$*"
9591846Sluigi    read -p "=== Press enter to continue" foo
96173602Sluigi    return 0
9791846Sluigi}
9891846Sluigi
99190378Sluigi# set some default values for variables.
100190378Sluigi# needs to be done as the first thing in the script.
10172770Sluigi
102190378Sluigiset_defaults() {	# no arguments
10372770Sluigi    # EDITOR is the editor you use
10484377Sluigi    # fd_size  floppy size in KB (default to 1440). You can use 1480,
10572770Sluigi    #	1720, 2880, etc. but beware that only 1440 and 1480 will boot
10672770Sluigi    #	from 1.44M floppy drives (1480 will not work on vmware).
10772770Sluigi    EDITOR=${EDITOR:-vi}
10884377Sluigi    fd_size=${fd_size:-1440}
10972770Sluigi
110190378Sluigi    o_all_in_mfs="yes"		# put all files in mfs so you can boot
111190378Sluigi				# and run the image via diskless boot.
112190378Sluigi    o_clean=""			# set if you want to clean prev.builds.
11384377Sluigi    o_interactive=""		# default is interactive
11484377Sluigi    o_verbose=0			# verbose level, 0 is silent
11584377Sluigi    o_tarv=""			# tar verbose flag, "" or "v"
116190378Sluigi    o_init_src=""		# set to build libs and includes.
11784377Sluigi    o_makeopts=${MAKEOPTS:--s}	# make options, be silent by default
118190378Sluigi    o_no_devfs=			# default is use devfs.
119190378Sluigi	# You should only set it when building 4.x images
12085833Sluigi    o_do_modules=""		# do not build modules
121227878Sluigi    o_arch=`uname -m`		# default to amd64 or i386 ...
12272770Sluigi
12384377Sluigi    SRC="/usr/src"		# default location for sources
124155136Sluigi    c_startdir=`pwd`		# directory where we start
125155136Sluigi				# used to lookup config and create BUILDDIR
12684377Sluigi
127155136Sluigi    # XXX 6.x/7.x have a single /boot/boot block, which is the concatenation
128155136Sluigi    # of the old two. For the time being, we keep these, but this should
129155136Sluigi    # be fixed at some point.
130155136Sluigi
131155136Sluigi    # blocks
132127266Sluigi    c_boot1=/boot/boot1		# boot blocks (in case you want custom ones)
13384377Sluigi    c_boot2=/boot/boot2
13484377Sluigi
13584377Sluigi    c_reply=${c_reply:-`mktemp "/tmp/reply.XXXXXXXXXX"`}
136155136Sluigi    				# file where User replies will be put
13784377Sluigi    c_mnt=`mktemp -d "/tmp/picobsd.XXXXXXXXXX"`
138155136Sluigi    				# mountpoint used to build memory filesystems
139155136Sluigi    c_fs=fs.PICOBSD		# filename used for the memory filesystem
140155136Sluigi    c_img=picobsd.bin		# filename used for the picobsd image
141194635Sluigi    c_iso=picobsd.iso		# filename used for the ISO image
142188835Sluigi    generate_iso="NO"		# don't generate the iso image
14384377Sluigi
144190378Sluigi    # select the right disklabel program
14584377Sluigi    case `uname -r` in
146190378Sluigi	4.*)
147190378Sluigi	    c_label="disklabel"
14884377Sluigi	    ;;
14984377Sluigi	*)
150190378Sluigi	    c_label="bsdlabel"
151127266Sluigi	    ;;
15284377Sluigi    esac
15372770Sluigi
15472770Sluigi    set -e
15572770Sluigi
15684377Sluigi    trap fail 2
15784377Sluigi    #trap fail 3
15884377Sluigi    #trap fail 6
15984377Sluigi    trap fail 15
16072770Sluigi}
16172770Sluigi
162190411Sluigi# use the new build infrastructure to create libraries
163190411Sluigi# and also to build a specific target
164190411Sluigicreate_includes_and_libraries2() { # opt_dir opt_target
165155136Sluigi    local no
166229532Sluigi    log "create_includes_and_libraries2() for ${SRC} $1"
167155136Sluigi    if [ ${OSVERSION} -ge 600000 ] ; then
168201059Sluigi	no="-DNO_CLEAN -DNO_PROFILE -DNO_GAMES -DNO_LIBC_R" # WITHOUT_CDDL=1"
169239738Sluigi	no="$no -DWITHOUT_CLANG -DMALLOC_PRODUCTION"
170155136Sluigi    else
171155136Sluigi	no="-DNOCLEAN -DNOPROFILE -DNOGAMES -DNOLIBC_R"
172155136Sluigi    fi
17399946Sluigi    ( cd ${SRC};
174155136Sluigi    # make -DNOCLEAN -DNOPROFILE -DNOGAMES -DNOLIBC_R -DPICOBSD buildworld
175190411Sluigi    if [ -d "$1" ] ; then
176218359Sluigi	cd $1 ; ${BINMAKE} ${o_par} $2	# specific target, e.g. ld-elf.so
177190411Sluigi    else
178250289Sluigi	export MAKEOBJDIRPREFIX=${l_objtree}
179250289Sluigi	make ${o_par} $no toolchain
180250289Sluigi	# XXX do we need any of these ?
181229511Sluigi        eval export `cd ${SRC}; ${BINMAKE} -f Makefile.inc1 -V WMAKEENV`
182229511Sluigi	[ ${o_arch} != `uname -m` ] && \
183229511Sluigi	    (cd ${l_objtree}; ln -s . ${o_arch}.${o_arch} || true )
184190411Sluigi    fi
18599946Sluigi    )
18699946Sluigi}
18799946Sluigi
188173602Sluigi# entry for 4.x and earlier trees
18984377Sluigicreate_includes_and_libraries() {
190127266Sluigi    local e i
191127266Sluigi
19284377Sluigi    log "create_includes_and_libraries() for ${SRC}"
19384377Sluigi    # Optionally creates include directory and libraries.
194190378Sluigi    mkdir -p ${l_usrtree}/include	# the include directory...
19584377Sluigi    mkdir -p ${l_usrtree}/share/misc	# a few things go here
19684377Sluigi    mkdir -p ${l_usrtree}/lib		# libraries
19784377Sluigi    mkdir -p ${l_usrtree}/sbin		# some binaries
198127266Sluigi    # override variables for ownershiip and destinations
199127266Sluigi    # BINOWN:BINGRP are also used for include files
200127266Sluigi    (cd ${SRC}; \
201127266Sluigi	BINOWN=`id -un` BINGRP=`id -gn` \
202127266Sluigi	DESTDIR=${l_usrtree}/.. \
203127266Sluigi	make -m ${SRC}/share/mk includes ) || fail $? includes
20492964Sru    # Pick up the correct headers for libraries.
20592964Sru    CFLAGS="-nostdinc -I${l_usrtree}/include" ; export CFLAGS
20684377Sluigi
20784377Sluigi    (cd ${SRC}
20884377Sluigi	# $e is the invocation of make with correct environment
209155136Sluigi	# XXX check the NO* options below, maybe system dependent.
21084377Sluigi	e="MAKEOBJDIRPREFIX=${l_objtree}/picobsd/libraries \
211127266Sluigi	    BINOWN=`id -un` BINGRP=`id -gn` \
212127266Sluigi	    DESTDIR=${l_usrtree}/.. \
21386876Sluigi	    make -m ${SRC}/share/mk \
214155136Sluigi		-DNOHTML -DNOINFO -DNOMAN -DNOSHARE -DNOFSCHG "
21592063Sluigi	log "do a 'make obj' in a few places."
21692063Sluigi	# This is very version-specific... The following works for 5.0
217158687Sphk	for i in lib secure/lib gnu/lib \
21884377Sluigi		gnu/usr.bin/perl usr.bin/lex usr.sbin/config ; do
21984377Sluigi	    (cd ${i}; eval $e obj)
22084377Sluigi	done
22192063Sluigi	log "now make the static libraries"
222155136Sluigi	eval $e -DNOPROFILE -DNOPIC libraries
22384377Sluigi	(cd ${SRC}/usr.sbin/config
22484377Sluigi	eval $e		# build binary
22584377Sluigi	eval $e	install	# install it
22684377Sluigi	)
22784377Sluigi    ) || fail $? "libraries"
22884377Sluigi    log "Libraries done"
22972770Sluigi}
23072770Sluigi
231190378Sluigi# set_type <the_type> [the_site] looks in user or system directories
232190378Sluigi# for the directory named as the first argument, reads the configuration
233190378Sluigi# files and sets variables according to the config.
234190378Sluigi# Also sets MY_TREE and BUILDDIR and SITE
23572770Sluigi
236190378Sluigiset_type() {	# the_type the_site
237127266Sluigi    local a i
23884377Sluigi
239173602Sluigi    log "set_type() : Type '$1' site '$2'"
24084377Sluigi    THETYPE=$1
24184377Sluigi    SITE=$2
24272770Sluigi    a=$1
243173602Sluigi    name=""	# clear in case of errors
24484377Sluigi    for i in ${c_startdir}/${a} ${PICO_TREE}/${a} ; do
24578494Sluigi	log "set_type: checking $i"
246229511Sluigi	[ -d $i -a -f $i/crunch.conf ] || continue
247229511Sluigi	# look for a kernel config file, privilege arch-specific
248229511Sluigi	l_kernconf=$i/PICOBSD.${o_arch}
249229511Sluigi	[ -f $l_kernconf ] || l_kernconf=$i/PICOBSD
250229511Sluigi	[ -f $l_kernconf ] || continue
251229511Sluigi	set -- `cat $l_kernconf | \
25272770Sluigi	    awk '/^#PicoBSD/ {print $2, $3, $4, $5, $6}'`
253173602Sluigi	[ x"$1" != "x" ] || continue
254229511Sluigi	MFS_SIZE=$1
255173602Sluigi	name=`(cd $i ; pwd) `
256173602Sluigi	name=`basename $name`
257173602Sluigi	MY_TREE=$i
258229511Sluigi	BUILDDIR=${c_startdir}/build_dir-${name}-${o_arch}
259173602Sluigi	log "Matching file $name in $i"
260173602Sluigi	return ;
26172770Sluigi    done
262173602Sluigi    logverbose "Type $a NOT FOUND"
26372770Sluigi}
26472770Sluigi
26572770Sluigiclean_tree() {
26684377Sluigi    log "clean_tree()"
267190411Sluigi    if [ -z "${name}" ] ; then
26884377Sluigi	echo "---> Wrong floppy type"
26972770Sluigi	exit 3
27072770Sluigi    fi
27172770Sluigi    rm -rf ${BUILDDIR}
27272770Sluigi}
27372770Sluigi
27472770Sluigi# prepare a message to be printed in the dialog menus.
27572770Sluigiset_msgs() {		# OK
27684377Sluigi    log "set_msgs()"
27784377Sluigi
27872770Sluigi    MSG1="Type: ${THETYPE} name $name"
27972770Sluigi
28072770Sluigi    MSG="PicoBSD build -- Current parameters:\n\n\t1.  ${MSG1}\n\
28172770Sluigi\t2.  MFS size: ${MFS_SIZE} kB\n\
28272770Sluigi\t3.  Site-info: ${SITE}\n\t4.  Full-path: ${MY_TREE}\n"
28372770Sluigi}
28472770Sluigi
285194631Sluigi# Main build procedure. Builds both the disk image and the ISO
28672770Sluigibuild_image() {
28784377Sluigi    log "build_image() <${name}>"
288190411Sluigi    [ -n "${name}" ] || fail $? bad_type
28972770Sluigi    clear
29072770Sluigi    set_msgs
29184377Sluigi    printf "${MSG}---> We'll use the sources living in ${SRC}\n\n"
29272770Sluigi
29384377Sluigi    # read config variables from a global and then a type-specific file
29484377Sluigi    # basically STAND_LINKS and MY_DEVS, but can also override other
29584377Sluigi    # variables.
29684377Sluigi    # 
29784377Sluigi    . ${PICO_TREE}/build/config
298190411Sluigi    [ -f "${MY_TREE}/config" ]		&& . ${MY_TREE}/config
299190411Sluigi    [ -f "${o_additional_config}" ]	&& . ${o_additional_config}
30084377Sluigi
30184377Sluigi    # location of the object directory
30284377Sluigi    PICO_OBJ=${l_objtree}/picobsd/${THETYPE}
30384377Sluigi    log "PICO_OBJ is ${PICO_OBJ}"
30484377Sluigi
30584377Sluigi    # create build directory and subtree
30684377Sluigi    mkdir -p ${BUILDDIR}/crunch
30784377Sluigi    # remove any old stuff
30884377Sluigi    rm -f ${BUILDDIR}/kernel.gz ${BUILDDIR}/${c_fs}
30984377Sluigi    # invoke commands to build a kernel
31072770Sluigi    do_kernel
31184377Sluigi    # fill a subdirectory with things that go into the floppy
31284377Sluigi    # (mostly /etc and similar stuff)
31384377Sluigi    populate_floppy_fs
31484377Sluigi    # populate it and produce a file with the MFS image
31584377Sluigi    populate_mfs_tree		# things which go into mfs
31684377Sluigi    # create, mount and fill a filesystem with floppy image
31772770Sluigi    fill_floppy_image # copies everything into the floppy
31872770Sluigi}
31972770Sluigi
32072770Sluigi# Set build parameters interactively
32172770Sluigi
32272770Sluigimain_dialog() {
32384377Sluigi  local ans i l
32484377Sluigi
32584377Sluigi  log "main_dialog()"
326190411Sluigi  while true ; do
32772770Sluigi    set_msgs
32884377Sluigi    rm ${c_reply}
32984377Sluigi    dialog --menu "PicoBSD build menu -- (29 sep 2001)" 19 70 12 \
33072770Sluigi	N "--> READY, build it <---" \
33172770Sluigi	T "${MSG1}" \
33272770Sluigi	K "edit Kernel config file" \
33372770Sluigi	E "Edit crunch.conf file" \
33472770Sluigi	S "MFS Size: ${MFS_SIZE}kB" \
33584377Sluigi	F "Floppy size: ${fd_size}kB" \
33672770Sluigi	$ "Site-info: ${SITE}" \
33772770Sluigi	Q "Quit" \
33884377Sluigi	2> ${c_reply}
33984377Sluigi    ans=`cat ${c_reply}`
34084377Sluigi    rm ${c_reply}
34172770Sluigi    case ${ans} in
34272770Sluigi    T)
34372770Sluigi	l=""
34484377Sluigi	for i in ${c_startdir} ${c_startdir}/* ${PICO_TREE}/* ; do
34572770Sluigi	    if [ -d $i -a -f $i/PICOBSD -a -f $i/crunch.conf ]; then
34672770Sluigi		l="$l `basename $i` `basename $i`"
34772770Sluigi	    fi
34872770Sluigi	done
34972770Sluigi	log $l
35090659Sluigi	{ dialog --menu "Setup the type of configuration" 12 70 5 $l \
35190659Sluigi		2> ${c_reply} && set_type "`cat ${c_reply}`" ${SITE} ; } || true
35272770Sluigi	;;
35372770Sluigi
35472770Sluigi    K) ${EDITOR} ${MY_TREE}/PICOBSD ;;
35572770Sluigi
35672770Sluigi    E) ${EDITOR} ${MY_TREE}/crunch.conf ;;
35772770Sluigi
35872770Sluigi    S)
35990659Sluigi	{ dialog --title "MFS Size setup" --inputbox \
36072770Sluigi"MFS size depends on what you need to put on the MFS image. Typically \
36172770Sluigiranges between 820kB (for very small bridge/router images) to \
36272770Sluigias much as 2500kB kB for a densely packed image. \
36372770SluigiKeep in mind that this memory is \
36472770Sluigitotally lost to other programs. Usually you want to keep \
36584377Sluigithis as small as possible. " 10 70 2> ${c_reply} \
36690659Sluigi	&& MFS_SIZE=`cat ${c_reply}` ; } || true
36772770Sluigi	;;
36872770Sluigi
36972770Sluigi    \$)
37090659Sluigi	{ dialog --title "Site info setup" --inputbox \
37172770Sluigi	"Please enter the full path to the directory \
37272770Sluigi	containing site-specific setup. \
37372770Sluigi	This directory tree must contain files that replace \
37472770Sluigi	standard ones in floppy.tree/ and mfs.tree/ . " \
37590659Sluigi	10 70 2> ${c_reply} && SITE=`cat ${c_reply}` ; } || true
37672770Sluigi	;;
37772770Sluigi
37872770Sluigi    F)
37991846Sluigi	{ dialog --menu "Set floppy size" 15 70 4 \
38084377Sluigi	    1440 "1.44MB" 1720 "1.72MB" 2880 "2.88MB" 4096 "4MB" \
38191846Sluigi		 2> ${c_reply} && fd_size=`cat ${c_reply}` ; } || true
38272770Sluigi	;;
38372770Sluigi
38472770Sluigi    N) break 2
38572770Sluigi	;;
38672770Sluigi
38772770Sluigi    Q) exit 0 ;;
38872770Sluigi
38972770Sluigi    *) echo "Unknown option \"${ans}\". Try again."
39072770Sluigi	sleep 2
39172770Sluigi	clear
39272770Sluigi	;;
39372770Sluigi    esac
39472770Sluigi  done
39572770Sluigi}
39672770Sluigi
39772770Sluigi# Call the build procedure
39872770Sluigi# Install image
39972770Sluigido_install() {
40084377Sluigi    log "do_install()"
40184377Sluigi
40285833Sluigi    if [ "${o_interactive}" = "NO" ] ; then
40385833Sluigi	echo "+++ Build completed +++"
404155136Sluigi	cat .build.reply || true
40585833Sluigi	return
40685833Sluigi    fi
40782747Sluigi    dialog --title "Build ${THETYPE} completed" --inputbox \
40872770Sluigi"\nThe build process was completed successfuly.\n\
40972770Sluigi`cat .build.reply` \n\n\
41072770SluigiNow we are going to install the image on the floppy.\n\
41172770SluigiPlease insert a blank floppy in /dev/fd0.\\n
41272770SluigiWARNING: the contents of the floppy will be permanently erased!\n\
41372770Sluigi\n\
41472770SluigiYour options:\n\
41572770Sluigi	* ^C or [Cancel] to abort,\n\
41684377Sluigi	* Enter to install ${c_img},\n\
41784377Sluigi" 20 80 2> ${c_reply}
41872770Sluigi    if [ "$?" = "0" ]; then
41984377Sluigi	echo "Writing ${c_img}..."
42084377Sluigi	dd if=${BUILDDIR}/${c_img} of=/dev/fd0.${fd_size}
42172770Sluigi    else
42284377Sluigi	echo "Ok, the image is in ${c_img}"
42372770Sluigi    fi
42472770Sluigi    echo "Done."
42572770Sluigi}
42672770Sluigi
42772770Sluigi
42872770Sluigi#-------------------------------------------------------------------
42972770Sluigi
430173602Sluigi# invoke the picobsd Makefile to compile the kernel.
431173602Sluigi# if MODULES is set (value is irrelevant) the makefile will build modules.
43272770Sluigido_kernel() {		# OK
43384377Sluigi    log "do_kernel() Preparing kernel \"$name\" in $MY_TREE"
43499946Sluigi    (cd $MY_TREE; export name SRC BUILDDIR # used in this makefile ;
43599946Sluigi	# export CONFIG
436227878Sluigi	export WARNS CWARNFLAGS
437173602Sluigi	[ "${o_do_modules}" = "yes" ] && export MODULES=""
438250289Sluigi	# kernel build not parallelizable yet
439250289Sluigi	${BINMAKE} KERNCONF=${l_kernconf}	\
440229511Sluigi		-v -f ${PICO_TREE}/build/Makefile.conf ) || \
441229511Sluigi	    fail $? missing_kernel
44272770Sluigi}
44372770Sluigi
44484377Sluigi# Populate the variable part of the floppy filesystem. Must be done before
44584377Sluigi# the MFS because its content might need to be copied there as well.
44672770Sluigi#
44784377Sluigi# This involves fetching files from three subtrees, in this order:
44872770Sluigi#
44984377Sluigi#  1. a standard one, from which type-specific files are excluded;
45072770Sluigi#  2. a type-specific one;
45172770Sluigi#  3. a site-specific one.
45272770Sluigi#
45372770Sluigi# Files are first copied to a local tree and then compressed.
45472770Sluigi
45572770Sluigipopulate_floppy_fs() {		# OK
45683692Sluigi    local dst excl srcdir
45772770Sluigi
45884377Sluigi    log "populate_floppy_fs()"
45972770Sluigi    dst=${BUILDDIR}/floppy.tree
46084377Sluigi    log "pwd=`pwd` Populating floppy filesystem..."
46172770Sluigi
462188835Sluigi    rm -rf ${dst} || true	# clean relics from old compilations.
463188835Sluigi    mkdir ${dst}		# create a clean tree
46472770Sluigi
465188835Sluigi    # compute exclude list for generic tree
46672770Sluigi    excl=${MY_TREE}/floppy.tree.exclude
46772770Sluigi    if [ -f ${excl} ] ; then
468173597Sluigi	log "Files excluded from generic tree: `echo;cat ${excl}`"
46972770Sluigi	excl="--exclude-from ${excl}"
47072770Sluigi    else
47172770Sluigi	excl=""
47272770Sluigi    fi
473188835Sluigi    # copy from the floppy trees into the destination
474188835Sluigi    for FLOPPY_TREE in ${PICO_TREE}/floppy.tree ${MY_TREE}/floppy.tree \
475188835Sluigi		${MY_TREE}/floppy.tree.${SITE} ; do
476188835Sluigi	if [ -d ${FLOPPY_TREE} ] ; then
477262761Sgjb	    (cd ${FLOPPY_TREE} ; tar -cf - \
478188835Sluigi		    --exclude .svn ${excl} . ) | \
47984377Sluigi		(cd ${dst} ; tar x${o_tarv}f - )
480188835Sluigi	    log "Copied from ${FLOPPY_TREE}"
481188835Sluigi	fi
482188835Sluigi	excl="" # reset the exclude list.
483188835Sluigi    done
48472770Sluigi
485188835Sluigi    # add local manipulation
486188835Sluigi    if [ -f ${MY_TREE}/buildtree.mk ] ; then
487188835Sluigi	log "building local floppy tree"
488188835Sluigi	${BINMAKE} -C ${dst} -f ${MY_TREE}/buildtree.mk floppy.tree
48972770Sluigi    fi
490188835Sluigi 
491188835Sluigi    # compress the files in etc/, just in case
492188835Sluigi    # XXX this should be done in the makefile.
49372770Sluigi    # gzip returns an error if it fails to compress some file
49484377Sluigi    (cd $dst ; gzip -9 etc/*
49584377Sluigi	    log "Compressed files in etc/ `echo; ls -l etc`"
49672770Sluigi    ) || true
49772770Sluigi}
49872770Sluigi
499189978Sluigi# Copy the specified files to the destination filesystem.
500189978Sluigi# Each file is specified as a pair "src dst", dst is assumed to be
501189978Sluigi# a directory (and created with mkdir -p) if it has a trailing /
502189978Sluigi# Be careful to escape metacharacters.
503189978Sluigi# You can use ${CROSS} to point to the root of the cross build
504189978Sluigi# (remember that it might be incomplete)
505189978Sluigi
506189978Sluigido_copyfiles() {	# rootdir varname
507189978Sluigi	log Copy files to $1
508189978Sluigi	local root=$1
509189978Sluigi	local srcs dst
510189978Sluigi	local CROSS=${_SHLIBDIRPREFIX}
511189978Sluigi	eval set "\${${2}}"
512189978Sluigi        srcs=""
513189978Sluigi	for dst in $* ; do
514190411Sluigi		[ -z "$srcs" ] && srcs=$dst && continue
515189978Sluigi		eval srcs="$srcs"	# expand wildcard and vars
516189978Sluigi		case x"$dst" in
517189978Sluigi		*/ )	mkdir -p ${root}/${dst} ;;
518189978Sluigi		# * )	mkdir -p `dirname ${root}/${dst}` ;;
519189978Sluigi		esac
520189978Sluigi		cp -p ${srcs} ${root}/${dst} || true
521189978Sluigi		srcs=""
522189978Sluigi        done
523189978Sluigi}
524189978Sluigi
525190411Sluigi# do_links is a helper function to create links between programs
526190411Sluigi# in stand/
527190411Sluigi# This is done reading the names and destination from variable
528190411Sluigi# links in a config file, in the format
529190411Sluigi#	: dst names
530190411Sluigi
531190411Sluigido_links() {	# rootdir varname
532190411Sluigi	local root=$1
533190411Sluigi	local l i dst
534190411Sluigi	eval l="\${${2}}"
535190411Sluigi        dst=""
536190411Sluigi	log "Create links for ${l}"
537190411Sluigi	(cd ${root}/stand
538190411Sluigi	for i in $l ; do
539190411Sluigi	    if [ "$dst" = ":" -o "$i" = ":" ] ; then
540190411Sluigi		dst=$i
541190411Sluigi	    elif [ -n "${dst}" ] ; then
542190411Sluigi		ln -s ${dst} ${i}
543190411Sluigi	    fi
544190411Sluigi	done
545190411Sluigi	)
546190411Sluigi}
547190411Sluigi
548190383Sluigi# find_progs is a helper function to locate the named programs
549190411Sluigi# or libraries in ${o_objdir} or ${_SHLIBDIRPREFIX},
550190411Sluigi# and return the full pathnames.
551229532Sluigi# Called as "find_progs [[-L libpath] [-P binpath]] prog1 prog2 ... "
552201072Sluigi# On return it sets ${u_progs} to the list of programs, and ${u_libs}
553190383Sluigi# to the list of shared libraries used.
554201072Sluigi# 
555201072Sluigi# '-L path' can be used to specify a search path for libraries
556201072Sluigi#    (which searches in $path/lib:$path/usr/lib:$path/usr/local/lib
557201072Sluigi# '-P binpath' can be used to specify a search path for programs
558201072Sluigi#    (which searches in a lot of places in the subtree)
559201072Sluigi# -L must be the first, followed by -P
560190383Sluigi#
561201072Sluigi# You can use it e.g. in a local confign file by writing
562190383Sluigi#
563190383Sluigi#  do_copyfiles_user() {
564190383Sluigi#	local dst=$1
565190383Sluigi#	find_progs nvi sed less grep
566190383Sluigi#	cp -p ${u_progs} ${dst}/bin
567190383Sluigi#	cp -p ${u_libs} ${dst}/lib
568190383Sluigi#	mkdir -p ${dst}/libexec
569190383Sluigi#	find_progs ld-elf.so.1
570201072Sluigi#	cp -p ${u_progs} ${dst}/libexec # ignore errors
571190383Sluigi#  }
572190383Sluigi
573190383Sluigifind_progs() {	# programs
574229532Sluigi	local pass i old_libs="" tmp o=""
575201072Sluigi	if [ x"$1" = "x-L" -a -d "$2" ] ; then # set lib search path
576229532Sluigi		o="-P $2"; shift; shift
577201072Sluigi	fi
578229532Sluigi	# Result returned in global variables
579229532Sluigi	u_libs="" ; u_progs="`find_progs_helper $*`"
580190383Sluigi	[ -z "${u_progs}" ] && return 1	# not found, error
581229532Sluigi	# use objdump to find libraries. Iterate to fetch recursive
582229532Sluigi	# dependencies.
583229532Sluigi	tmp="${u_progs}" ; pass=1
584229532Sluigi	while [ $pass -lt 10 ] ; do
585229532Sluigi		pass=$(($pass + 1))
586229532Sluigi		i="`objdump -x ${tmp} | \
587229532Sluigi			awk '$1 == "NEEDED" { print $2 }' | sort | uniq`"
588229532Sluigi		if [ "$old_libs" = "$i" ] ; then
589229532Sluigi			log "libraries for: $my_progs ($u_progs) are ($i) $u_libs"
590229532Sluigi			log "--- done find_progs ---"
591229532Sluigi			return 0
592229532Sluigi		else
593229532Sluigi			# logverbose "old--- $old_libs --- new +++ $i +++"
594229532Sluigi		fi
595229532Sluigi		u_libs="`find_progs_helper $o $i`"
596229532Sluigi		old_libs="$i"
597229532Sluigi		tmp="$tmp $u_libs"
598229532Sluigi	done
599229532Sluigi	log "WARNING: Too many passes, giving up"
600190383Sluigi}
601190383Sluigi
602190383Sluigifind_progs_helper() {	# programs
603201072Sluigi	local dir=${o_objdir:-${_SHLIBDIRPREFIX}/..}
604201072Sluigi	local ldir=""
605201072Sluigi	if [ x"$1" = "x-P" -a -d "$2" ] ; then # set path
606201072Sluigi		ldir=$2; shift; shift
607201072Sluigi	fi
608190383Sluigi	local progs="$*"
609201072Sluigi	local subdirs=". local/bin local/sbin local/lib local/libexec \
610201072Sluigi		bin sbin usr.bin usr.sbin libexec lib \
611190383Sluigi		gnu/usr.bin gnu/lib \
612190383Sluigi		secure/usr.bin secure/usr.sbin secure/libexec secure/lib"
613201072Sluigi	local names=""	# files to search
614201072Sluigi	local o=""
615201072Sluigi	local i
616190383Sluigi	for i in $progs ; do
617201072Sluigi		# full pathnames are just listed
618190411Sluigi		[ -f "$i" ] && echo $i && continue
619190383Sluigi		names="${names} ${o} -name $i"
620190383Sluigi		o="-o"
621190383Sluigi	done
622190411Sluigi	[ -z "${names}" ] && return 0
623201072Sluigi	local places=""				# places to search
624190383Sluigi	for i in $subdirs ; do
625201072Sluigi		[ -d "${dir}/${i}" ] && places="${places} ${dir}/${i}"
626190383Sluigi	done
627201072Sluigi	if [ -n "${ldir}" ] ; then
628201072Sluigi	    for i in $subdirs ; do
629201072Sluigi		[ -d "${ldir}/${i}" ] && places="${places} ${ldir}/${i}"
630201072Sluigi	    done
631201072Sluigi	fi
632229511Sluigi	for i in $progs ; do
633229511Sluigi		# full pathnames are just listed
634229511Sluigi		[ -f "$i" ] && echo $i && continue
635229511Sluigi		find ${places} -maxdepth 3 -type f -name ${i} | head -1
636229511Sluigi	done
637201072Sluigi	# use maxdepth 3 because some libs are way down
638190383Sluigi}
639190411Sluigi
64072770Sluigi# Populate the memory filesystem with binaries and non-variable
64172770Sluigi# configuration files.
64272770Sluigi# First do an mtree pass, then create directory links and device entries,
64372770Sluigi# then run crunchgen etc. to build the binary and create links.
64472770Sluigi# Then copy the specific/generic mfs_tree.
64572770Sluigi# Finally, if required, make a copy of the floppy.tree onto /fd
64672770Sluigi
64784377Sluigipopulate_mfs_tree() {
648189978Sluigi    local i j a dst MFS_TREE
64972770Sluigi
65084377Sluigi    log "populate_mfs_tree()"
651155136Sluigi    dst=${BUILDDIR}/mfs.tree
652188835Sluigi    rm -rf ${dst} || true	# clean relics from old compilations.
653188835Sluigi    mkdir ${dst}		# create a fresh tree
65472770Sluigi
65584377Sluigi    log "pwd=`pwd`, Populating MFS tree..."
65684377Sluigi
65784377Sluigi    # use type-specific mfs.mtree, default to generic one.
65884377Sluigi    a=${MY_TREE}/mfs.mtree
65984377Sluigi    [ -f ${a} ] || a=${PICO_TREE}/build/mfs.mtree
66084377Sluigi    log "Running mtree using $a..."
66184377Sluigi    mtree -deU -f $a -p ${dst} > /dev/null || fail $? mtree
66284377Sluigi
663189978Sluigi    # Create symlinks using relative pathnames, so it is possible
664189978Sluigi    # to follow them also when building the image.
665189978Sluigi    # Note that names in STAND_LINKS should not have a leading /
66672770Sluigi    for i in ${STAND_LINKS}; do
667189978Sluigi	j=`echo $i | sed -E 's:^[^/]+::;s:/[^/]+:../:g'`
668189978Sluigi	ln -s ${j}stand ${dst}/$i
66972770Sluigi    done
670189978Sluigi    ln -s ../../dev/null ${dst}/var/run/log
671189978Sluigi    ln -s ../../../etc/termcap ${dst}/usr/share/misc/termcap
67272770Sluigi
673188835Sluigi    ### now build the crunched binaries ###
67472770Sluigi    (
67572770Sluigi    cd ${BUILDDIR}/crunch
67684377Sluigi    log "Making and installing crunch1 from `pwd` src ${SRC}..."
67772770Sluigi    a=${BUILDDIR}/crunch1.conf
67878541Sluigi    ( export BUILDDIR SRC MY_TREE PICO_OBJ ;
679173597Sluigi	${BINMAKE} \
68086876Sluigi		-v -f ${PICO_TREE}/build/Makefile.conf ${BUILDDIR}/crunch.mk )
68184377Sluigi    log "Libs are ${LIBS} "
68299946Sluigi    export SRC # used by crunch.mk
68399946Sluigi    # export LIBS CFLAGS
68472770Sluigi    log "Now make -f crunch.mk"
685173597Sluigi    ${BINMAKE} ${o_makeopts} -f ${BUILDDIR}/crunch.mk
68672770Sluigi    strip --remove-section=.note --remove-section=.comment crunch1
68784377Sluigi    mv crunch1 ${dst}/stand/crunch
68884377Sluigi    chmod 555 ${dst}/stand/crunch
68984377Sluigi    log "Making links for binaries..."
69072770Sluigi    for i in `crunchgen -l $a` ; do
69184377Sluigi	ln ${dst}/stand/crunch ${dst}/stand/${i};
69272770Sluigi    done
69378541Sluigi    # rm $a # do not remove!
69472770Sluigi    ) || fail $? crunch
69572770Sluigi
696255314Sluigi    log "Setting up host key for sshd:"
697255314Sluigi    for K in rsa1 rsa dsa ; do
698255314Sluigi	if [ $K = rsa1 ] ; then
699255314Sluigi	    i=ssh_host_key
70084627Sluigi	else
701255314Sluigi	    i=ssh_host_${K}_key
70284627Sluigi	fi
703255314Sluigi	if [ -f ${BUILDDIR}/floppy.tree/etc/$i.gz ] ; then
704255314Sluigi	    log "Using existing host key $i"
705255314Sluigi	else
706255314Sluigi	    log "Generating new host key $i" 
707255314Sluigi	    ssh-keygen -t $K -f ${BUILDDIR}/floppy.tree/etc/$i \
708255314Sluigi		     -N "" -C "root@picobsd"
709255314Sluigi	    gzip -9 ${BUILDDIR}/floppy.tree/etc/${i}* || true
710255314Sluigi	fi
711255314Sluigi    done
71272770Sluigi
71391846Sluigi    log "Copy generic and site-specific MFS tree..."
71491846Sluigi    for MFS_TREE in ${PICO_TREE}/mfs_tree ${MY_TREE}/mfs_tree ; do
71591846Sluigi	if [ -d ${MFS_TREE} ] ; then
71691846Sluigi	    log "Copy ${MFS_TREE} ..."
717262761Sgjb	    (cd ${MFS_TREE} ; tar -cf - --exclude .svn . ) | \
71892063Sluigi		    (cd ${dst} ; tar x${o_tarv}f - )
71991846Sluigi	fi
72091846Sluigi    done
72172770Sluigi
722188835Sluigi    if [ -f ${MY_TREE}/buildtree.mk ] ; then
723188835Sluigi	log "building local floppy tree"
724188835Sluigi	${BINMAKE} -C ${dst} -f ${MY_TREE}/buildtree.mk mfs.tree
725188835Sluigi    fi
726188835Sluigi
72784377Sluigi    if [ "${o_all_in_mfs}" = "yes" ]; then
72884377Sluigi	log "Copy generic floppy_tree into MFS..."
729188835Sluigi	# ignore failure in case the floppy is empty
730155136Sluigi	cp -Rp ${BUILDDIR}/floppy.tree/* ${dst}/fd || true
73172770Sluigi    fi
73284377Sluigi
733188835Sluigi    # 4.x compatibility - create device nodes
734190411Sluigi    if [ -n "${o_no_devfs}" ] ; then
73584377Sluigi	# create device entries using MAKEDEV
73684377Sluigi	(cd ${dst}/dev
737155136Sluigi	ln -s ${SRC}/etc/MAKEDEV ; chmod 555 MAKEDEV
738155136Sluigi	# log `pwd`
739155136Sluigi	sh ./MAKEDEV ${MY_DEVS}
74084377Sluigi	rm MAKEDEV
74184377Sluigi	)
74284377Sluigi    fi
743155136Sluigi    if [ "`id -u`" = "0" ] ; then
744155136Sluigi	log "Fixing permissions"
745155136Sluigi	(cd ${dst}; chown -R root . )
746155136Sluigi    fi
74784377Sluigi
748200301Sluigi    log "for a shared 'crunch' take libraries and dynamic loader as well"
749190411Sluigi    find_progs ${dst}/stand/crunch
750190411Sluigi    if [ -n "${u_libs}" ] ; then
751190411Sluigi	mkdir -p ${dst}/lib && cp -p ${u_libs} ${dst}/lib
752190411Sluigi	mkdir -p ${dst}/libexec
753190411Sluigi        create_includes_and_libraries2 libexec/rtld-elf
754190411Sluigi        find_progs ld-elf.so.1 && cp -p ${u_progs} ${dst}/libexec
755189978Sluigi    fi
756190411Sluigi
757190411Sluigi    [ -n "${copy_files}" ] && do_copyfiles ${dst} copy_files
758190378Sluigi    do_copyfiles_user ${dst} || true
759190411Sluigi    [ -n "${links}" ] && do_links ${dst} links
760190411Sluigi    strip ${dst}/libexec/* ${dst}/lib/* ${dst}/stand/* 2> /dev/null || true
761189978Sluigi
762189978Sluigi    # The 'import_files' mechanism is deprecated, as it requires
763189978Sluigi    # root permissions to follow the symlinks, and also does
764189978Sluigi    # not let you rename the entries.
76592853Sluigi    if [ -n "${import_files}" ] ; then
76692853Sluigi	log "importing ${import_files} into mfs"
76792853Sluigi	# We do it in a chroot environment on the target so
76892853Sluigi	# symlinks are followed correctly.
769188835Sluigi	# Make sure we have a statically linked tar there.
770188835Sluigi	mkdir -p ${dst}/rescue
771188835Sluigi	cp /rescue/tar ${dst}/rescue
77292853Sluigi	(cd ${l_usrtree}/.. ; tar cf - ${import_files} ) | \
773188835Sluigi	    (chroot ${dst} /rescue/tar xPf - )
774188835Sluigi	rm -rf ${dst}/rescue
77592853Sluigi    fi
77692853Sluigi
777189978Sluigi    # final step -- build the mfs image
778155136Sluigi    (cd ${BUILDDIR}
779155136Sluigi	# override the owner
780155136Sluigi	echo "/set uid=0 gid=0" > mtree.out
781188835Sluigi	mtree -ic -p ${dst} -k "" >> mtree.out
782155136Sluigi	log "mtre.out at ${BUILDDIR}/mtree.out"
783155136Sluigi	makefs -t ffs -o bsize=4096 -o fsize=512 \
784188835Sluigi		-s ${MFS_SIZE}k -f 1000 -F mtree.out ${c_fs} ${dst}
785155136Sluigi	ls -l ${c_fs} )
786155136Sluigi    log "done mfs image"
78772770Sluigi}
78872770Sluigi
78972770Sluigifinal_cleanup() {
79084377Sluigi    log "final_cleanup()"
79184377Sluigi    rm -rf ${c_mnt} ${c_reply} 2> /dev/null || true
79272770Sluigi}
79372770Sluigi
79472770Sluigi# fail errno errcode
79572770Sluigi# This function is used to trap errors and print msgs
79672770Sluigi#
79772770Sluigifail() {
79884377Sluigi    local errno errocode where
79984377Sluigi
80072770Sluigi    errno=$1
80172770Sluigi    errcode=$2
80284377Sluigi    where=$3
80384377Sluigi    echo "---> fail: Error <${errno}> error code <${errcode}> in <${where}>"
80484377Sluigi    case ${errcode} in
80572770Sluigi    mtree)
80684377Sluigi	echo "Error while making hierarchy in ${c_mnt}"
80772770Sluigi	;;
80872770Sluigi    crunch)
80972770Sluigi	echo "Error while building ${name}."
81072770Sluigi	;;
81172770Sluigi    missing_kernel)
81275880Sjoe	echo "Error: you must build PICOBSD${suffix} kernel first"
81372770Sluigi	;;
81482917Sluigi    includes)
81582917Sluigi	echo "Error: failed while making includes"
81682917Sluigi	;;
81782917Sluigi    libraries)
81882917Sluigi	echo "Error: failed while making libraries"
81982917Sluigi	;;
82084377Sluigi    bad_type)
82184377Sluigi	echo "Error: unknown floppy type ${name}"
82284377Sluigi	;;
82384377Sluigi    no_space)
82484377Sluigi	echo "Error: no space left on device (${where})"
82584377Sluigi	;;
82692853Sluigi    no_mfs)
82792853Sluigi	echo "Error: while writing MFS into the kernel."
82892853Sluigi	;;
82972770Sluigi    "")
83072770Sluigi	echo "User break"
83184377Sluigi	errcode="userbreak"
83272770Sluigi	;;
83372770Sluigi    *)
83472770Sluigi	echo "unknown error, maybe user break: $errno $errcode"
83572770Sluigi	;;
83672770Sluigi    esac
83772770Sluigi    echo "---> Aborting $0"
83872770Sluigi    # try to cleanup the vnode.
83972770Sluigi    final_cleanup
84072770Sluigi    exit 2
84172770Sluigi}
84272770Sluigi
84372770Sluigifill_floppy_image() {
844155136Sluigi    local blocks dst mfs_start mfs_end mfs_size img_size
84572770Sluigi
84684377Sluigi    log "fill_floppy_image()"
84784377Sluigi    dst=${c_mnt}	# where to create the image
84884377Sluigi
84984377Sluigi    log "Preparing ${fd_size}kB floppy filesystem..."
85084377Sluigi
851155136Sluigi    # correct blocks according to size.
852155136Sluigi    blocks=${fd_size};
85372770Sluigi    if [ "${blocks}" = "1720" ]; then
854155136Sluigi	blocks=1722
85572770Sluigi    elif [ "${blocks}" = "1480" ]; then
856155136Sluigi	blocks=1476
85772770Sluigi    fi
85872770Sluigi
85984377Sluigi    log "Labeling floppy image"
860155136Sluigi
861155136Sluigi    dst=${BUILDDIR}/image.tree
862155136Sluigi    rm -rf ${dst}
863155136Sluigi    mkdir -p ${dst}
86472770Sluigi    (
86572770Sluigi    cd ${BUILDDIR}
866155136Sluigi    set 0 0 # reset variables
86799946Sluigi    # $1 takes the offset of the MFS filesystem
86899946Sluigi    set `strings -at d kernel | grep "MFS Filesystem goes here"`
869155136Sluigi    mfs_start=$1
870155136Sluigi    set 0 0 # reset variables
871155136Sluigi    set `strings -at d kernel | grep "MFS Filesystem had better"`
872155136Sluigi    mfs_end=$1
873155136Sluigi    mfs_size="$((${mfs_end} - ${mfs_start}))"
874155136Sluigi    set -- `ls -l ${c_fs}`; imgsize="$5"
875155136Sluigi    if [ ${mfs_start} -gt 0 -a ${mfs_size} -ge ${imgsize} ] ; then
876155136Sluigi	mfs_ofs=$((${mfs_start} + 8192))
877155136Sluigi	log "Preload kernel with file ${c_fs} at ${mfs_ofs}"
878203877Sluigi	log "`ls -l ${c_fs}` to fit in ${mfs_size}"
879155136Sluigi	dd if=${c_fs} ibs=8192 iseek=1 of=kernel obs=${mfs_ofs} \
880188835Sluigi	    oseek=1 conv=notrunc # 2> /dev/null
881155136Sluigi    else
882155136Sluigi    	log "not loading mfs, size ${mfs_size} img ${imgsize}"
883155136Sluigi    fi
88484377Sluigi    log "Compress with kgzip and copy to floppy image"
88572770Sluigi
886229511Sluigi    mkdir -p  ${dst}/boot/kernel
887234983Sluigi    # XXX loader.conf does not work unless we also load the .4th files
888255315Sluigi    # echo "hint.acpi.0.disabled=\"1\"" > ${dst}/boot/loader.conf
889255315Sluigi    # echo "console=\"comconsole\"" >> ${dst}/boot/loader.conf
890255315Sluigi    local blf="loader* *.4th" # loader.rc loader.4th support.4th"
891255315Sluigi    (cd /boot; cp -p loader ${dst}/boot) || fail $? no_space "copying bootloader"
892255315Sluigi    cp ${MY_TREE}/floppy.tree/boot/loader.conf ${dst}/boot || true
893229511Sluigi    gzip -c kernel > ${dst}/boot/kernel/kernel.gz || fail $? no_space "copying kernel"
894229511Sluigi
89572770Sluigi    # now transfer the floppy tree. If it is already in mfs, dont bother.
89684377Sluigi    if [ "${o_all_in_mfs}" != "yes" ] ; then
897188835Sluigi	log "Now transfer floppy tree if not already in MFS image"
89884377Sluigi	cp -Rp floppy.tree/* ${dst} || \
89984377Sluigi		fail $? no_space "copying floppy tree"
90072770Sluigi    fi
90172770Sluigi    )
902188835Sluigi
903188835Sluigi    # add local manipulation to the image
904188835Sluigi    if [ -f ${MY_TREE}/buildtree.mk ] ; then
905188835Sluigi	${BINMAKE} -C ${dst} -f ${MY_TREE}/buildtree.mk image.tree
906188835Sluigi    fi
907188835Sluigi
908188835Sluigi    log "image used `du -s ${dst}` of ${blocks}k"
909194631Sluigi    if [ "${generate_iso}" = "YES" ]; then
910194631Sluigi	logverbose "generate_iso ${generate_iso}"
911194631Sluigi	# build_iso_image	# XXX not implemented yet
912194631Sluigi	(cd ${BUILDDIR}
913194631Sluigi	cp -p /boot/cdboot ${dst}/boot || fail $? no_space "copying cdboot"
914194631Sluigi	mkisofs -b boot/cdboot -no-emul-boot -J -r -ldots -l -L \
915194631Sluigi		-o ${c_iso} ${dst}
916194631Sluigi	)
917194631Sluigi    fi
918194631Sluigi
919155136Sluigi    (cd ${BUILDDIR}
920155136Sluigi    makefs -t ffs -o bsize=4096 -o fsize=512 \
921155136Sluigi	-s ${blocks}k -f 50 ${c_img} ${dst}
922190378Sluigi
923190378Sluigi    ${c_label} -w -f `pwd`/${c_img} auto # write in a label
924155136Sluigi    # copy partition c: into a: with some sed magic
925190378Sluigi    ${c_label} -f `pwd`/${c_img} | sed -e '/  c:/{p;s/c:/a:/;}' | \
926190378Sluigi	${c_label} -R -f `pwd`/${c_img} /dev/stdin
927190378Sluigi    ${c_label} -f `pwd`/${c_img}
928188835Sluigi
929155136Sluigi    ls -l ${c_img}
930190378Sluigi    ${c_label} -f `pwd`/${c_img}
931203877Sluigi    log "after disklabel"
932188835Sluigi    )
933188835Sluigi
934188835Sluigi    echo "BUILDDIR ${BUILDDIR}"
935188835Sluigi
936155136Sluigi    # dump the primary and secondary boot
937155136Sluigi    # XXX primary is 512 bytes
938155136Sluigi    dd if=${c_boot1} of=${BUILDDIR}/${c_img} conv=notrunc 2>/dev/null
939155136Sluigi    # XXX secondary starts after the 0x114 = dec 276 bytes of the label
940155136Sluigi    # so we skip 276 from the source, and 276+512=788 from dst
941155136Sluigi    # the old style blocks used 512 and 1024 respectively
94272770Sluigi
943229511Sluigi    dd if=${c_boot2} iseek=1 ibs=276 2> /dev/null | \
944155136Sluigi	dd of=${BUILDDIR}/${c_img} oseek=1 obs=788 conv=notrunc 2>/dev/null
945203877Sluigi    log "done disk image"
946155136Sluigi    # XXX (log "Fixing permissions"; cd ${dst}; chown -R root *)
947229511Sluigi    # leave build stuff if verbose
948229511Sluigi    [ ${o_verbose} -gt 0 ] && return
949229511Sluigi
950155136Sluigi    rm -rf ${BUILDDIR}/floppy.tree || true # cleanup
951155136Sluigi    # df -ik ${dst} | colrm 70 > .build.reply
95284377Sluigi    rm -rf ${dst}
953188835Sluigi    rm ${BUILDDIR}/${c_fs}
954188835Sluigi    # rm ${BUILDDIR}/kernel.gz
95572770Sluigi}
95672770Sluigi
95784377Sluigi# This function creates variables which depend on the source tree in use:
95899946Sluigi# SRC, l_usrtree, l_objtree
95984377Sluigi# Optionally creates libraries, includes and the like (for cross compiles,
96084377Sluigi# needs to be done once).
96184377Sluigi
96284377Sluigiset_build_parameters() {
96384377Sluigi    if [ "${SRC}" = "/usr/src" ] ; then
96484377Sluigi	l_usrtree=${USR:-/usr}
96584377Sluigi    else
96684377Sluigi	l_usrtree=${USR:-${SRC}/../usr}
96784093Sluigi    fi
968227878Sluigi    l_objtree=${l_usrtree}/obj-pico-${o_arch}
969215177Sluigi
97084377Sluigi    PICO_TREE=${PICO_TREE:-${SRC}/release/picobsd}
97199946Sluigi    set `grep "#define[\t ]__FreeBSD_version" ${SRC}/sys/sys/param.h`
97299946Sluigi    OSVERSION=$3
97399951Sluigi    log "OSVERSION is ${OSVERSION}"
974215177Sluigi    if [ ${OSVERSION} -ge 500035 ] ; then
975215177Sluigi	export MAKEOBJDIRPREFIX=${l_objtree}
976227878Sluigi	export TARGET_ARCH=${o_arch} TARGET=${o_arch}
977244642Sluigi	export WITHOUT_CLANG_IS_CC=1
978229511Sluigi	# XXX why change machine_arch ?
979229511Sluigi	#-- export MACHINE_ARCH=`uname -m` MACHINE=`uname -m`
980227878Sluigi	# export CWARNFLAGS="-Wextra -Wno-sign-compare -Wno-missing-field-initializers"
981215177Sluigi	eval "export BINMAKE=\"`cd ${SRC}; make -f Makefile -V BINMAKE`\""
982255313Sluigi	[ "$BINMAKE" = "" ] && \
983255313Sluigi	   eval "export BINMAKE=\"`cd ${SRC}; make -f Makefile -V SUB_MAKE`\""
984215177Sluigi    fi
985215177Sluigi
98684377Sluigi    if [ "${o_init_src}" != "" ] ; then
98799946Sluigi	if [ ${OSVERSION} -lt 500035 ] ; then
98899946Sluigi	    create_includes_and_libraries
98999946Sluigi	else
99099946Sluigi	    create_includes_and_libraries2
99199946Sluigi	fi
992229511Sluigi    else
993229511Sluigi	eval export `cd ${SRC}; ${BINMAKE} -f Makefile.inc1 -V WMAKEENV`
99484093Sluigi    fi
99599946Sluigi    if [ ${OSVERSION} -lt 500035 ] ; then
99699946Sluigi	# Create the right LIBS and CFLAGS for further builds.
99799946Sluigi	# and build the config program
99899946Sluigi	LIBS="-L${l_usrtree}/lib"
99999946Sluigi	CFLAGS="-nostdinc -I${l_usrtree}/include"
100099946Sluigi	export LIBS CFLAGS
100199946Sluigi	CONFIG=${l_usrtree}/sbin/config
100299946Sluigi	export CONFIG
100399946Sluigi    fi
1004190383Sluigi
1005190383Sluigi    # if we have o_objdir, find where bin/ is
1006190383Sluigi    if [ ! -z "${o_objdir}" ] ; then
1007190383Sluigi	if [ -d ${o_objdir}/bin ] ; then
1008190383Sluigi	    # fine
1009190383Sluigi	elif [ -d "${o_objdir}${SRC}/bin" ] ; then
1010190383Sluigi	    o_objdir="${o_objdir}${SRC}"
1011190383Sluigi	    log "Changing objdir to ${o_objdir}"
1012190383Sluigi	else
1013190383Sluigi	    log "Cannot find objdir in ${o_objdir}, sorry"
1014190383Sluigi	    o_objdir=""
1015190383Sluigi	fi
1016190383Sluigi    fi
101784093Sluigi}
101884093Sluigi
101984377Sluigi#-------------------------------------------------------------------
102084377Sluigi# Main entry of the script. Initialize variables, parse command line
102184377Sluigi# arguments.
102284093Sluigi
1023218359Sluigi# o_par="-j 8"	# parallel make and other make options
1024218359Sluigi
102584377Sluigiset_defaults
1026188835Sluigiwhile [ true ]; do
1027190383Sluigi    log "Parsing $1"
102872770Sluigi    case $1 in
1029229511Sluigi    --par)
1030229511Sluigi	o_par="-j 8"
1031229511Sluigi	;;
1032229511Sluigi
103378494Sluigi    --src)	# set the source path instead of /usr/src
1034190383Sluigi	SRC=`realpath $2`
103584093Sluigi	shift
103678494Sluigi	;;
1037229511Sluigi
1038229511Sluigi    --init)	# run a partial buildworld on the source tree
103984377Sluigi	o_init_src="YES"
104084377Sluigi	;;
104182917Sluigi
1042229511Sluigi    --arch)	# override the target architecture
1043227878Sluigi	o_arch=$2
1044227878Sluigi	shift
1045227878Sluigi	;;
1046227878Sluigi
1047229511Sluigi    --floppy_size)	# image size
104884377Sluigi	fd_size=$2
104972770Sluigi	shift
105072770Sluigi	;;
105184377Sluigi
105284377Sluigi    --all_in_mfs)
105384377Sluigi	o_all_in_mfs="yes"
105484377Sluigi	;;
105584377Sluigi
105684377Sluigi    --no_all_in_mfs)
1057188835Sluigi	o_all_in_mfs="no"
105884377Sluigi	;;
105984377Sluigi
106085833Sluigi    --modules)	# also build kernel modules
106185833Sluigi	o_do_modules="yes"
106285833Sluigi	;;
1063229511Sluigi
106472770Sluigi    -n)
106584377Sluigi	o_interactive="NO"
106672770Sluigi	;;
106784377Sluigi
106884377Sluigi    -clear|-clean|-c) # clean
106984377Sluigi	o_clean="YES"
107084377Sluigi	o_interactive="NO"
107172770Sluigi	;;
107284377Sluigi
107384377Sluigi    -v) # need -v -v to wait for user input
107484377Sluigi	o_verbose=$((${o_verbose}+1))	# verbose level
107584377Sluigi	o_tarv="v"			# tar verbose flag
107684377Sluigi	o_makeopts="-d l" # be verbose
107772770Sluigi	;;
1078188835Sluigi
1079188835Sluigi    --iso) # generate iso image
1080188835Sluigi	generate_iso="YES"
1081188835Sluigi	;;
1082188835Sluigi
1083189978Sluigi    --cfg) # read additional config from this file
1084189978Sluigi	o_additional_config=`realpath $2`
1085189978Sluigi	shift
1086189978Sluigi	;;
1087189978Sluigi
1088190383Sluigi    --objdir)	# Place with results of a previous buildworld
1089190383Sluigi		# useful if you want to copy shared binaries and libs
1090190383Sluigi	o_objdir=`realpath $2`
1091190383Sluigi	shift
1092190383Sluigi	;;
1093190383Sluigi
109472770Sluigi    *)
1095188835Sluigi	break
109672770Sluigi	;;
109772770Sluigi
109872770Sluigi    esac
109972770Sluigi    shift
110072770Sluigidone
1101190378Sluigi
110284377Sluigiset_build_parameters	# things that depend on ${SRC}
1103188835Sluigiset_type $1 $2		# type and site, respectively
110472770Sluigi
1105173602Sluigi[ "${o_interactive}" != "NO" ] && main_dialog
1106173602Sluigi
110784377Sluigiif [ "${o_clean}" = "YES" ] ; then
110872770Sluigi    clean_tree
110972770Sluigielse
111072770Sluigi    build_image
111172770Sluigi    do_install
111272770Sluigifi
111372770Sluigifinal_cleanup
111472770Sluigiexit 0
1115