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
94277639Sluigi    printf "\n*** %s\n" "$*" >&2
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"
167277639Sluigi
168277639Sluigi    no="-DNO_CLEAN -DNO_PROFILE -DNO_GAMES -DNO_LIBC_R" # WITHOUT_CDDL=1"
169277639Sluigi    no="$no -DWITHOUT_CASPER"
170277639Sluigi    no="$no -DMALLOC_PRODUCTION"
171277639Sluigi
17299946Sluigi    ( cd ${SRC};
173155136Sluigi    # make -DNOCLEAN -DNOPROFILE -DNOGAMES -DNOLIBC_R -DPICOBSD buildworld
174190411Sluigi    if [ -d "$1" ] ; then
175218359Sluigi	cd $1 ; ${BINMAKE} ${o_par} $2	# specific target, e.g. ld-elf.so
176190411Sluigi    else
177250289Sluigi	export MAKEOBJDIRPREFIX=${l_objtree}
178250289Sluigi	make ${o_par} $no toolchain
179277639Sluigi
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
188127266Sluigi
189190378Sluigi# set_type <the_type> [the_site] looks in user or system directories
190190378Sluigi# for the directory named as the first argument, reads the configuration
191190378Sluigi# files and sets variables according to the config.
192190378Sluigi# Also sets MY_TREE and BUILDDIR and SITE
19372770Sluigi
194190378Sluigiset_type() {	# the_type the_site
195127266Sluigi    local a i
19684377Sluigi
197173602Sluigi    log "set_type() : Type '$1' site '$2'"
19884377Sluigi    THETYPE=$1
19984377Sluigi    SITE=$2
20072770Sluigi    a=$1
201173602Sluigi    name=""	# clear in case of errors
20284377Sluigi    for i in ${c_startdir}/${a} ${PICO_TREE}/${a} ; do
20378494Sluigi	log "set_type: checking $i"
204229511Sluigi	[ -d $i -a -f $i/crunch.conf ] || continue
205229511Sluigi	# look for a kernel config file, privilege arch-specific
206229511Sluigi	l_kernconf=$i/PICOBSD.${o_arch}
207229511Sluigi	[ -f $l_kernconf ] || l_kernconf=$i/PICOBSD
208229511Sluigi	[ -f $l_kernconf ] || continue
209229511Sluigi	set -- `cat $l_kernconf | \
21072770Sluigi	    awk '/^#PicoBSD/ {print $2, $3, $4, $5, $6}'`
211173602Sluigi	[ x"$1" != "x" ] || continue
212229511Sluigi	MFS_SIZE=$1
213173602Sluigi	name=`(cd $i ; pwd) `
214173602Sluigi	name=`basename $name`
215173602Sluigi	MY_TREE=$i
216229511Sluigi	BUILDDIR=${c_startdir}/build_dir-${name}-${o_arch}
217173602Sluigi	log "Matching file $name in $i"
218173602Sluigi	return ;
21972770Sluigi    done
220173602Sluigi    logverbose "Type $a NOT FOUND"
22172770Sluigi}
22272770Sluigi
22372770Sluigiclean_tree() {
22484377Sluigi    log "clean_tree()"
225190411Sluigi    if [ -z "${name}" ] ; then
22684377Sluigi	echo "---> Wrong floppy type"
22772770Sluigi	exit 3
22872770Sluigi    fi
22972770Sluigi    rm -rf ${BUILDDIR}
23072770Sluigi}
23172770Sluigi
23272770Sluigi# prepare a message to be printed in the dialog menus.
23372770Sluigiset_msgs() {		# OK
23484377Sluigi    log "set_msgs()"
23584377Sluigi
23672770Sluigi    MSG1="Type: ${THETYPE} name $name"
23772770Sluigi
23872770Sluigi    MSG="PicoBSD build -- Current parameters:\n\n\t1.  ${MSG1}\n\
23972770Sluigi\t2.  MFS size: ${MFS_SIZE} kB\n\
24072770Sluigi\t3.  Site-info: ${SITE}\n\t4.  Full-path: ${MY_TREE}\n"
24172770Sluigi}
24272770Sluigi
243194631Sluigi# Main build procedure. Builds both the disk image and the ISO
24472770Sluigibuild_image() {
24584377Sluigi    log "build_image() <${name}>"
246190411Sluigi    [ -n "${name}" ] || fail $? bad_type
24772770Sluigi    clear
24872770Sluigi    set_msgs
24984377Sluigi    printf "${MSG}---> We'll use the sources living in ${SRC}\n\n"
25072770Sluigi
25184377Sluigi    # read config variables from a global and then a type-specific file
25284377Sluigi    # basically STAND_LINKS and MY_DEVS, but can also override other
25384377Sluigi    # variables.
25484377Sluigi    # 
25584377Sluigi    . ${PICO_TREE}/build/config
256190411Sluigi    [ -f "${MY_TREE}/config" ]		&& . ${MY_TREE}/config
257190411Sluigi    [ -f "${o_additional_config}" ]	&& . ${o_additional_config}
25884377Sluigi
25984377Sluigi    # location of the object directory
26084377Sluigi    PICO_OBJ=${l_objtree}/picobsd/${THETYPE}
26184377Sluigi    log "PICO_OBJ is ${PICO_OBJ}"
26284377Sluigi
26384377Sluigi    # create build directory and subtree
26484377Sluigi    mkdir -p ${BUILDDIR}/crunch
26584377Sluigi    # remove any old stuff
26684377Sluigi    rm -f ${BUILDDIR}/kernel.gz ${BUILDDIR}/${c_fs}
26784377Sluigi    # invoke commands to build a kernel
26872770Sluigi    do_kernel
26984377Sluigi    # fill a subdirectory with things that go into the floppy
27084377Sluigi    # (mostly /etc and similar stuff)
27184377Sluigi    populate_floppy_fs
27284377Sluigi    # populate it and produce a file with the MFS image
27384377Sluigi    populate_mfs_tree		# things which go into mfs
27484377Sluigi    # create, mount and fill a filesystem with floppy image
27572770Sluigi    fill_floppy_image # copies everything into the floppy
27672770Sluigi}
27772770Sluigi
27872770Sluigi# Set build parameters interactively
27972770Sluigi
28072770Sluigimain_dialog() {
28184377Sluigi  local ans i l
28284377Sluigi
28384377Sluigi  log "main_dialog()"
284190411Sluigi  while true ; do
28572770Sluigi    set_msgs
28684377Sluigi    rm ${c_reply}
28784377Sluigi    dialog --menu "PicoBSD build menu -- (29 sep 2001)" 19 70 12 \
28872770Sluigi	N "--> READY, build it <---" \
28972770Sluigi	T "${MSG1}" \
29072770Sluigi	K "edit Kernel config file" \
29172770Sluigi	E "Edit crunch.conf file" \
29272770Sluigi	S "MFS Size: ${MFS_SIZE}kB" \
29384377Sluigi	F "Floppy size: ${fd_size}kB" \
29472770Sluigi	$ "Site-info: ${SITE}" \
29572770Sluigi	Q "Quit" \
29684377Sluigi	2> ${c_reply}
29784377Sluigi    ans=`cat ${c_reply}`
29884377Sluigi    rm ${c_reply}
29972770Sluigi    case ${ans} in
30072770Sluigi    T)
30172770Sluigi	l=""
30284377Sluigi	for i in ${c_startdir} ${c_startdir}/* ${PICO_TREE}/* ; do
30372770Sluigi	    if [ -d $i -a -f $i/PICOBSD -a -f $i/crunch.conf ]; then
30472770Sluigi		l="$l `basename $i` `basename $i`"
30572770Sluigi	    fi
30672770Sluigi	done
30772770Sluigi	log $l
30890659Sluigi	{ dialog --menu "Setup the type of configuration" 12 70 5 $l \
30990659Sluigi		2> ${c_reply} && set_type "`cat ${c_reply}`" ${SITE} ; } || true
31072770Sluigi	;;
31172770Sluigi
31272770Sluigi    K) ${EDITOR} ${MY_TREE}/PICOBSD ;;
31372770Sluigi
31472770Sluigi    E) ${EDITOR} ${MY_TREE}/crunch.conf ;;
31572770Sluigi
31672770Sluigi    S)
31790659Sluigi	{ dialog --title "MFS Size setup" --inputbox \
31872770Sluigi"MFS size depends on what you need to put on the MFS image. Typically \
31972770Sluigiranges between 820kB (for very small bridge/router images) to \
32072770Sluigias much as 2500kB kB for a densely packed image. \
32172770SluigiKeep in mind that this memory is \
32272770Sluigitotally lost to other programs. Usually you want to keep \
32384377Sluigithis as small as possible. " 10 70 2> ${c_reply} \
32490659Sluigi	&& MFS_SIZE=`cat ${c_reply}` ; } || true
32572770Sluigi	;;
32672770Sluigi
32772770Sluigi    \$)
32890659Sluigi	{ dialog --title "Site info setup" --inputbox \
32972770Sluigi	"Please enter the full path to the directory \
33072770Sluigi	containing site-specific setup. \
33172770Sluigi	This directory tree must contain files that replace \
33272770Sluigi	standard ones in floppy.tree/ and mfs.tree/ . " \
33390659Sluigi	10 70 2> ${c_reply} && SITE=`cat ${c_reply}` ; } || true
33472770Sluigi	;;
33572770Sluigi
33672770Sluigi    F)
33791846Sluigi	{ dialog --menu "Set floppy size" 15 70 4 \
33884377Sluigi	    1440 "1.44MB" 1720 "1.72MB" 2880 "2.88MB" 4096 "4MB" \
33991846Sluigi		 2> ${c_reply} && fd_size=`cat ${c_reply}` ; } || true
34072770Sluigi	;;
34172770Sluigi
34272770Sluigi    N) break 2
34372770Sluigi	;;
34472770Sluigi
34572770Sluigi    Q) exit 0 ;;
34672770Sluigi
34772770Sluigi    *) echo "Unknown option \"${ans}\". Try again."
34872770Sluigi	sleep 2
34972770Sluigi	clear
35072770Sluigi	;;
35172770Sluigi    esac
35272770Sluigi  done
35372770Sluigi}
35472770Sluigi
35572770Sluigi# Call the build procedure
35672770Sluigi# Install image
35772770Sluigido_install() {
35884377Sluigi    log "do_install()"
35984377Sluigi
36085833Sluigi    if [ "${o_interactive}" = "NO" ] ; then
36185833Sluigi	echo "+++ Build completed +++"
362155136Sluigi	cat .build.reply || true
36385833Sluigi	return
36485833Sluigi    fi
36582747Sluigi    dialog --title "Build ${THETYPE} completed" --inputbox \
36672770Sluigi"\nThe build process was completed successfuly.\n\
36772770Sluigi`cat .build.reply` \n\n\
36872770SluigiNow we are going to install the image on the floppy.\n\
36972770SluigiPlease insert a blank floppy in /dev/fd0.\\n
37072770SluigiWARNING: the contents of the floppy will be permanently erased!\n\
37172770Sluigi\n\
37272770SluigiYour options:\n\
37372770Sluigi	* ^C or [Cancel] to abort,\n\
37484377Sluigi	* Enter to install ${c_img},\n\
37584377Sluigi" 20 80 2> ${c_reply}
37672770Sluigi    if [ "$?" = "0" ]; then
37784377Sluigi	echo "Writing ${c_img}..."
37884377Sluigi	dd if=${BUILDDIR}/${c_img} of=/dev/fd0.${fd_size}
37972770Sluigi    else
38084377Sluigi	echo "Ok, the image is in ${c_img}"
38172770Sluigi    fi
38272770Sluigi    echo "Done."
38372770Sluigi}
38472770Sluigi
38572770Sluigi
38672770Sluigi#-------------------------------------------------------------------
38772770Sluigi
388173602Sluigi# invoke the picobsd Makefile to compile the kernel.
389173602Sluigi# if MODULES is set (value is irrelevant) the makefile will build modules.
39072770Sluigido_kernel() {		# OK
39184377Sluigi    log "do_kernel() Preparing kernel \"$name\" in $MY_TREE"
39299946Sluigi    (cd $MY_TREE; export name SRC BUILDDIR # used in this makefile ;
39399946Sluigi	# export CONFIG
394227878Sluigi	export WARNS CWARNFLAGS
395173602Sluigi	[ "${o_do_modules}" = "yes" ] && export MODULES=""
396250289Sluigi	# kernel build not parallelizable yet
397250289Sluigi	${BINMAKE} KERNCONF=${l_kernconf}	\
398277639Sluigi		-f ${PICO_TREE}/build/Makefile.conf ) || \
399229511Sluigi	    fail $? missing_kernel
40072770Sluigi}
40172770Sluigi
40284377Sluigi# Populate the variable part of the floppy filesystem. Must be done before
40384377Sluigi# the MFS because its content might need to be copied there as well.
40472770Sluigi#
40584377Sluigi# This involves fetching files from three subtrees, in this order:
40672770Sluigi#
40784377Sluigi#  1. a standard one, from which type-specific files are excluded;
40872770Sluigi#  2. a type-specific one;
40972770Sluigi#  3. a site-specific one.
41072770Sluigi#
41172770Sluigi# Files are first copied to a local tree and then compressed.
41272770Sluigi
41372770Sluigipopulate_floppy_fs() {		# OK
41483692Sluigi    local dst excl srcdir
41572770Sluigi
41684377Sluigi    log "populate_floppy_fs()"
41772770Sluigi    dst=${BUILDDIR}/floppy.tree
41884377Sluigi    log "pwd=`pwd` Populating floppy filesystem..."
41972770Sluigi
420188835Sluigi    rm -rf ${dst} || true	# clean relics from old compilations.
421188835Sluigi    mkdir ${dst}		# create a clean tree
42272770Sluigi
423188835Sluigi    # compute exclude list for generic tree
42472770Sluigi    excl=${MY_TREE}/floppy.tree.exclude
42572770Sluigi    if [ -f ${excl} ] ; then
426173597Sluigi	log "Files excluded from generic tree: `echo;cat ${excl}`"
42772770Sluigi	excl="--exclude-from ${excl}"
42872770Sluigi    else
42972770Sluigi	excl=""
43072770Sluigi    fi
431188835Sluigi    # copy from the floppy trees into the destination
432188835Sluigi    for FLOPPY_TREE in ${PICO_TREE}/floppy.tree ${MY_TREE}/floppy.tree \
433188835Sluigi		${MY_TREE}/floppy.tree.${SITE} ; do
434188835Sluigi	if [ -d ${FLOPPY_TREE} ] ; then
435262761Sgjb	    (cd ${FLOPPY_TREE} ; tar -cf - \
436188835Sluigi		    --exclude .svn ${excl} . ) | \
43784377Sluigi		(cd ${dst} ; tar x${o_tarv}f - )
438188835Sluigi	    log "Copied from ${FLOPPY_TREE}"
439188835Sluigi	fi
440188835Sluigi	excl="" # reset the exclude list.
441188835Sluigi    done
44272770Sluigi
443188835Sluigi    # add local manipulation
444188835Sluigi    if [ -f ${MY_TREE}/buildtree.mk ] ; then
445188835Sluigi	log "building local floppy tree"
446188835Sluigi	${BINMAKE} -C ${dst} -f ${MY_TREE}/buildtree.mk floppy.tree
44772770Sluigi    fi
448188835Sluigi 
449188835Sluigi    # compress the files in etc/, just in case
450188835Sluigi    # XXX this should be done in the makefile.
45172770Sluigi    # gzip returns an error if it fails to compress some file
45284377Sluigi    (cd $dst ; gzip -9 etc/*
45384377Sluigi	    log "Compressed files in etc/ `echo; ls -l etc`"
45472770Sluigi    ) || true
45572770Sluigi}
45672770Sluigi
457189978Sluigi# Copy the specified files to the destination filesystem.
458189978Sluigi# Each file is specified as a pair "src dst", dst is assumed to be
459189978Sluigi# a directory (and created with mkdir -p) if it has a trailing /
460189978Sluigi# Be careful to escape metacharacters.
461189978Sluigi# You can use ${CROSS} to point to the root of the cross build
462189978Sluigi# (remember that it might be incomplete)
463189978Sluigi
464189978Sluigido_copyfiles() {	# rootdir varname
465189978Sluigi	log Copy files to $1
466189978Sluigi	local root=$1
467189978Sluigi	local srcs dst
468189978Sluigi	local CROSS=${_SHLIBDIRPREFIX}
469189978Sluigi	eval set "\${${2}}"
470189978Sluigi        srcs=""
471189978Sluigi	for dst in $* ; do
472190411Sluigi		[ -z "$srcs" ] && srcs=$dst && continue
473189978Sluigi		eval srcs="$srcs"	# expand wildcard and vars
474189978Sluigi		case x"$dst" in
475189978Sluigi		*/ )	mkdir -p ${root}/${dst} ;;
476189978Sluigi		# * )	mkdir -p `dirname ${root}/${dst}` ;;
477189978Sluigi		esac
478189978Sluigi		cp -p ${srcs} ${root}/${dst} || true
479189978Sluigi		srcs=""
480189978Sluigi        done
481189978Sluigi}
482189978Sluigi
483190411Sluigi# do_links is a helper function to create links between programs
484190411Sluigi# in stand/
485190411Sluigi# This is done reading the names and destination from variable
486190411Sluigi# links in a config file, in the format
487190411Sluigi#	: dst names
488190411Sluigi
489190411Sluigido_links() {	# rootdir varname
490190411Sluigi	local root=$1
491190411Sluigi	local l i dst
492190411Sluigi	eval l="\${${2}}"
493190411Sluigi        dst=""
494190411Sluigi	log "Create links for ${l}"
495190411Sluigi	(cd ${root}/stand
496190411Sluigi	for i in $l ; do
497190411Sluigi	    if [ "$dst" = ":" -o "$i" = ":" ] ; then
498190411Sluigi		dst=$i
499190411Sluigi	    elif [ -n "${dst}" ] ; then
500190411Sluigi		ln -s ${dst} ${i}
501190411Sluigi	    fi
502190411Sluigi	done
503190411Sluigi	)
504190411Sluigi}
505190411Sluigi
506190383Sluigi# find_progs is a helper function to locate the named programs
507190411Sluigi# or libraries in ${o_objdir} or ${_SHLIBDIRPREFIX},
508190411Sluigi# and return the full pathnames.
509229532Sluigi# Called as "find_progs [[-L libpath] [-P binpath]] prog1 prog2 ... "
510201072Sluigi# On return it sets ${u_progs} to the list of programs, and ${u_libs}
511190383Sluigi# to the list of shared libraries used.
512201072Sluigi# 
513201072Sluigi# '-L path' can be used to specify a search path for libraries
514201072Sluigi#    (which searches in $path/lib:$path/usr/lib:$path/usr/local/lib
515201072Sluigi# '-P binpath' can be used to specify a search path for programs
516201072Sluigi#    (which searches in a lot of places in the subtree)
517201072Sluigi# -L must be the first, followed by -P
518190383Sluigi#
519201072Sluigi# You can use it e.g. in a local confign file by writing
520190383Sluigi#
521190383Sluigi#  do_copyfiles_user() {
522190383Sluigi#	local dst=$1
523190383Sluigi#	find_progs nvi sed less grep
524190383Sluigi#	cp -p ${u_progs} ${dst}/bin
525190383Sluigi#	cp -p ${u_libs} ${dst}/lib
526190383Sluigi#	mkdir -p ${dst}/libexec
527190383Sluigi#	find_progs ld-elf.so.1
528201072Sluigi#	cp -p ${u_progs} ${dst}/libexec # ignore errors
529190383Sluigi#  }
530190383Sluigi
531277639Sluigi# find programs and required libraries. Accept -L libs -P path <progs>
532277639Sluigi# if no argument default to objdir/SHLIBDIRPREFIX for both
533190383Sluigifind_progs() {	# programs
534277639Sluigi	# logverbose "find_progs: called with $*"
535277639Sluigi	local i=`realpath ${o_objdir:-${_SHLIBDIRPREFIX}/..}`
536277639Sluigi	# default values for -L and -P
537277639Sluigi	local dir="-P $i"
538277639Sluigi	local ldir="-L $i"
539277639Sluigi
540277639Sluigi	while [ "$1" != "" ] ; do
541277639Sluigi		if [ x"$1" = "x-L" -a -d "$2" ] ; then # set lib search path
542277639Sluigi			ldir="-L $2"; shift; shift
543277639Sluigi		elif [ x"$1" = "x-P" -a -d "$2" ] ; then # set prog search path
544277639Sluigi			dir="-P $2"; shift; shift
545277639Sluigi		else
546277639Sluigi			break
547277639Sluigi		fi
548277639Sluigi	done
549277639Sluigi
550277639Sluigi	# Results are returned in global variables
551277639Sluigi	u_libs=""
552277639Sluigi	u_progs="`find_progs_helper $dir $*`"
553190383Sluigi	[ -z "${u_progs}" ] && return 1	# not found, error
554277639Sluigi
555277639Sluigi	# use objdump to find libraries.
556277639Sluigi	# Iterate to fetch recursive dependencies.
557277639Sluigi	local tmp="${u_progs}"
558277639Sluigi	local old_libs=""
559277639Sluigi	local pass=1
560229532Sluigi	while [ $pass -lt 10 ] ; do
561229532Sluigi		pass=$(($pass + 1))
562229532Sluigi		i="`objdump -x ${tmp} | \
563277639Sluigi			awk '$1 == "NEEDED" { print $2 }' | sort | uniq | tr '\n' ' '`"
564229532Sluigi		if [ "$old_libs" = "$i" ] ; then
565277639Sluigi			# logverbose "find_progs: have `echo ${u_libs} | wc -w`/`echo ${i} | wc -w` libraries for: $my_progs ($u_progs)"
566277639Sluigi			# logverbose "they are ($i) $u_libs"
567229532Sluigi			return 0
568229532Sluigi		else
569229532Sluigi			# logverbose "old--- $old_libs --- new +++ $i +++"
570229532Sluigi		fi
571277639Sluigi		u_libs="`find_progs_helper $ldir $i`"
572229532Sluigi		old_libs="$i"
573229532Sluigi		tmp="$tmp $u_libs"
574229532Sluigi	done
575229532Sluigi	log "WARNING: Too many passes, giving up"
576190383Sluigi}
577190383Sluigi
578277639Sluigi# prints to stdout files and libs in the search paths
579277639Sluigifind_progs_helper() {	# first arg is either -P or -L
580277639Sluigi	local ty=$1 dir=$2 ; shift; shift
581277639Sluigi	local progs="`echo $* | tr ' ' '\n' | sort -u | tr '\n' ' '`"
582277639Sluigi	# first, extract absolute pathnames or files in this directory
583277639Sluigi
584277639Sluigi	# accumulate others in $names
585277639Sluigi	local names=""
586201072Sluigi	local i
587190383Sluigi	for i in $progs ; do
588277639Sluigi		[ -f "$i" ] && echo `realpath $i` && continue
589277639Sluigi		names="${names} $i"
590190383Sluigi	done
591277639Sluigi	# if nothing left, we are done
592190411Sluigi	[ -z "${names}" ] && return 0
593277639Sluigi
594277639Sluigi	local depth p
595277639Sluigi	local places=""			# places to search
596277639Sluigi	if [ x-P = "x$ty" ] ; then # search programs
597277639Sluigi		depth=2
598277639Sluigi		p=". local/bin local/sbin local/libexec \
599277639Sluigi		    bin sbin usr/bin usr/sbin libexec gnu/usr.bin \
600277639Sluigi		    secure/usr.bin secure/usr.sbin secure/libexec "
601277639Sluigi	else
602277639Sluigi		depth=3
603277639Sluigi		p="lib usr/lib gnu/lib secure/lib"
604201072Sluigi	fi
605277639Sluigi	for i in $p ; do
606277639Sluigi		i="${dir}/${i}"
607277639Sluigi		[ -d "${i}" ] && places="${places} `realpath ${i}`"
608229511Sluigi	done
609277639Sluigi	# logverbose "--- looking into $places"
610277639Sluigi	places=`echo ${places} | tr ' ' '\n' | sort -u`
611277639Sluigi	for i in $names ; do
612277639Sluigi	    find ${places} -maxdepth $depth -type f -name ${i} | head -1
613277639Sluigi	done
614190383Sluigi}
615190411Sluigi
61672770Sluigi# Populate the memory filesystem with binaries and non-variable
61772770Sluigi# configuration files.
61872770Sluigi# First do an mtree pass, then create directory links and device entries,
61972770Sluigi# then run crunchgen etc. to build the binary and create links.
62072770Sluigi# Then copy the specific/generic mfs_tree.
62172770Sluigi# Finally, if required, make a copy of the floppy.tree onto /fd
62272770Sluigi
62384377Sluigipopulate_mfs_tree() {
624189978Sluigi    local i j a dst MFS_TREE
62572770Sluigi
62684377Sluigi    log "populate_mfs_tree()"
627155136Sluigi    dst=${BUILDDIR}/mfs.tree
628188835Sluigi    rm -rf ${dst} || true	# clean relics from old compilations.
629188835Sluigi    mkdir ${dst}		# create a fresh tree
63072770Sluigi
63184377Sluigi    log "pwd=`pwd`, Populating MFS tree..."
63284377Sluigi
63384377Sluigi    # use type-specific mfs.mtree, default to generic one.
63484377Sluigi    a=${MY_TREE}/mfs.mtree
63584377Sluigi    [ -f ${a} ] || a=${PICO_TREE}/build/mfs.mtree
63684377Sluigi    log "Running mtree using $a..."
63784377Sluigi    mtree -deU -f $a -p ${dst} > /dev/null || fail $? mtree
63884377Sluigi
639189978Sluigi    # Create symlinks using relative pathnames, so it is possible
640189978Sluigi    # to follow them also when building the image.
641189978Sluigi    # Note that names in STAND_LINKS should not have a leading /
64272770Sluigi    for i in ${STAND_LINKS}; do
643189978Sluigi	j=`echo $i | sed -E 's:^[^/]+::;s:/[^/]+:../:g'`
644189978Sluigi	ln -s ${j}stand ${dst}/$i
64572770Sluigi    done
646189978Sluigi    ln -s ../../dev/null ${dst}/var/run/log
647189978Sluigi    ln -s ../../../etc/termcap ${dst}/usr/share/misc/termcap
64872770Sluigi
649188835Sluigi    ### now build the crunched binaries ###
65072770Sluigi    (
65172770Sluigi    cd ${BUILDDIR}/crunch
65284377Sluigi    log "Making and installing crunch1 from `pwd` src ${SRC}..."
65372770Sluigi    a=${BUILDDIR}/crunch1.conf
65478541Sluigi    ( export BUILDDIR SRC MY_TREE PICO_OBJ ;
655173597Sluigi	${BINMAKE} \
656277639Sluigi		-f ${PICO_TREE}/build/Makefile.conf ${BUILDDIR}/crunch.mk )
65784377Sluigi    log "Libs are ${LIBS} "
65899946Sluigi    export SRC # used by crunch.mk
65999946Sluigi    # export LIBS CFLAGS
66072770Sluigi    log "Now make -f crunch.mk"
661173597Sluigi    ${BINMAKE} ${o_makeopts} -f ${BUILDDIR}/crunch.mk
66272770Sluigi    strip --remove-section=.note --remove-section=.comment crunch1
66384377Sluigi    mv crunch1 ${dst}/stand/crunch
66484377Sluigi    chmod 555 ${dst}/stand/crunch
66584377Sluigi    log "Making links for binaries..."
66672770Sluigi    for i in `crunchgen -l $a` ; do
66784377Sluigi	ln ${dst}/stand/crunch ${dst}/stand/${i};
66872770Sluigi    done
66978541Sluigi    # rm $a # do not remove!
67072770Sluigi    ) || fail $? crunch
67172770Sluigi
672255314Sluigi    log "Setting up host key for sshd:"
673255314Sluigi    for K in rsa1 rsa dsa ; do
674255314Sluigi	if [ $K = rsa1 ] ; then
675255314Sluigi	    i=ssh_host_key
67684627Sluigi	else
677255314Sluigi	    i=ssh_host_${K}_key
67884627Sluigi	fi
679255314Sluigi	if [ -f ${BUILDDIR}/floppy.tree/etc/$i.gz ] ; then
680255314Sluigi	    log "Using existing host key $i"
681255314Sluigi	else
682255314Sluigi	    log "Generating new host key $i" 
683255314Sluigi	    ssh-keygen -t $K -f ${BUILDDIR}/floppy.tree/etc/$i \
684255314Sluigi		     -N "" -C "root@picobsd"
685255314Sluigi	    gzip -9 ${BUILDDIR}/floppy.tree/etc/${i}* || true
686255314Sluigi	fi
687255314Sluigi    done
68872770Sluigi
68991846Sluigi    log "Copy generic and site-specific MFS tree..."
69091846Sluigi    for MFS_TREE in ${PICO_TREE}/mfs_tree ${MY_TREE}/mfs_tree ; do
69191846Sluigi	if [ -d ${MFS_TREE} ] ; then
69291846Sluigi	    log "Copy ${MFS_TREE} ..."
693262761Sgjb	    (cd ${MFS_TREE} ; tar -cf - --exclude .svn . ) | \
69492063Sluigi		    (cd ${dst} ; tar x${o_tarv}f - )
69591846Sluigi	fi
69691846Sluigi    done
69772770Sluigi
698188835Sluigi    if [ -f ${MY_TREE}/buildtree.mk ] ; then
699188835Sluigi	log "building local floppy tree"
700188835Sluigi	${BINMAKE} -C ${dst} -f ${MY_TREE}/buildtree.mk mfs.tree
701188835Sluigi    fi
702188835Sluigi
70384377Sluigi    if [ "${o_all_in_mfs}" = "yes" ]; then
70484377Sluigi	log "Copy generic floppy_tree into MFS..."
705188835Sluigi	# ignore failure in case the floppy is empty
706155136Sluigi	cp -Rp ${BUILDDIR}/floppy.tree/* ${dst}/fd || true
70772770Sluigi    fi
70884377Sluigi
709188835Sluigi    # 4.x compatibility - create device nodes
710190411Sluigi    if [ -n "${o_no_devfs}" ] ; then
71184377Sluigi	# create device entries using MAKEDEV
71284377Sluigi	(cd ${dst}/dev
713155136Sluigi	ln -s ${SRC}/etc/MAKEDEV ; chmod 555 MAKEDEV
714155136Sluigi	# log `pwd`
715155136Sluigi	sh ./MAKEDEV ${MY_DEVS}
71684377Sluigi	rm MAKEDEV
71784377Sluigi	)
71884377Sluigi    fi
719155136Sluigi    if [ "`id -u`" = "0" ] ; then
720155136Sluigi	log "Fixing permissions"
721155136Sluigi	(cd ${dst}; chown -R root . )
722155136Sluigi    fi
72384377Sluigi
724200301Sluigi    log "for a shared 'crunch' take libraries and dynamic loader as well"
725277639Sluigi    # /stand/crunch is our main binary, we extract its libs
726190411Sluigi    find_progs ${dst}/stand/crunch
727190411Sluigi    if [ -n "${u_libs}" ] ; then
728190411Sluigi	mkdir -p ${dst}/lib && cp -p ${u_libs} ${dst}/lib
729190411Sluigi	mkdir -p ${dst}/libexec
730190411Sluigi        create_includes_and_libraries2 libexec/rtld-elf
731190411Sluigi        find_progs ld-elf.so.1 && cp -p ${u_progs} ${dst}/libexec
732189978Sluigi    fi
733190411Sluigi
734190411Sluigi    [ -n "${copy_files}" ] && do_copyfiles ${dst} copy_files
735190378Sluigi    do_copyfiles_user ${dst} || true
736190411Sluigi    [ -n "${links}" ] && do_links ${dst} links
737190411Sluigi    strip ${dst}/libexec/* ${dst}/lib/* ${dst}/stand/* 2> /dev/null || true
738189978Sluigi
739189978Sluigi    # The 'import_files' mechanism is deprecated, as it requires
740189978Sluigi    # root permissions to follow the symlinks, and also does
741189978Sluigi    # not let you rename the entries.
74292853Sluigi    if [ -n "${import_files}" ] ; then
74392853Sluigi	log "importing ${import_files} into mfs"
74492853Sluigi	# We do it in a chroot environment on the target so
74592853Sluigi	# symlinks are followed correctly.
746188835Sluigi	# Make sure we have a statically linked tar there.
747188835Sluigi	mkdir -p ${dst}/rescue
748188835Sluigi	cp /rescue/tar ${dst}/rescue
74992853Sluigi	(cd ${l_usrtree}/.. ; tar cf - ${import_files} ) | \
750188835Sluigi	    (chroot ${dst} /rescue/tar xPf - )
751188835Sluigi	rm -rf ${dst}/rescue
75292853Sluigi    fi
75392853Sluigi
754189978Sluigi    # final step -- build the mfs image
755155136Sluigi    (cd ${BUILDDIR}
756155136Sluigi	# override the owner
757155136Sluigi	echo "/set uid=0 gid=0" > mtree.out
758188835Sluigi	mtree -ic -p ${dst} -k "" >> mtree.out
759155136Sluigi	log "mtre.out at ${BUILDDIR}/mtree.out"
760155136Sluigi	makefs -t ffs -o bsize=4096 -o fsize=512 \
761188835Sluigi		-s ${MFS_SIZE}k -f 1000 -F mtree.out ${c_fs} ${dst}
762155136Sluigi	ls -l ${c_fs} )
763155136Sluigi    log "done mfs image"
76472770Sluigi}
76572770Sluigi
76672770Sluigifinal_cleanup() {
76784377Sluigi    log "final_cleanup()"
76884377Sluigi    rm -rf ${c_mnt} ${c_reply} 2> /dev/null || true
76972770Sluigi}
77072770Sluigi
77172770Sluigi# fail errno errcode
77272770Sluigi# This function is used to trap errors and print msgs
77372770Sluigi#
77472770Sluigifail() {
77584377Sluigi    local errno errocode where
77684377Sluigi
77772770Sluigi    errno=$1
77872770Sluigi    errcode=$2
77984377Sluigi    where=$3
78084377Sluigi    echo "---> fail: Error <${errno}> error code <${errcode}> in <${where}>"
78184377Sluigi    case ${errcode} in
78272770Sluigi    mtree)
78384377Sluigi	echo "Error while making hierarchy in ${c_mnt}"
78472770Sluigi	;;
78572770Sluigi    crunch)
78672770Sluigi	echo "Error while building ${name}."
78772770Sluigi	;;
78872770Sluigi    missing_kernel)
78975880Sjoe	echo "Error: you must build PICOBSD${suffix} kernel first"
79072770Sluigi	;;
79182917Sluigi    includes)
79282917Sluigi	echo "Error: failed while making includes"
79382917Sluigi	;;
79482917Sluigi    libraries)
79582917Sluigi	echo "Error: failed while making libraries"
79682917Sluigi	;;
79784377Sluigi    bad_type)
79884377Sluigi	echo "Error: unknown floppy type ${name}"
79984377Sluigi	;;
80084377Sluigi    no_space)
80184377Sluigi	echo "Error: no space left on device (${where})"
80284377Sluigi	;;
80392853Sluigi    no_mfs)
80492853Sluigi	echo "Error: while writing MFS into the kernel."
80592853Sluigi	;;
80672770Sluigi    "")
80772770Sluigi	echo "User break"
80884377Sluigi	errcode="userbreak"
80972770Sluigi	;;
81072770Sluigi    *)
81172770Sluigi	echo "unknown error, maybe user break: $errno $errcode"
81272770Sluigi	;;
81372770Sluigi    esac
81472770Sluigi    echo "---> Aborting $0"
81572770Sluigi    # try to cleanup the vnode.
81672770Sluigi    final_cleanup
81772770Sluigi    exit 2
81872770Sluigi}
81972770Sluigi
82072770Sluigifill_floppy_image() {
821155136Sluigi    local blocks dst mfs_start mfs_end mfs_size img_size
82272770Sluigi
82384377Sluigi    log "fill_floppy_image()"
82484377Sluigi    dst=${c_mnt}	# where to create the image
82584377Sluigi
82684377Sluigi    log "Preparing ${fd_size}kB floppy filesystem..."
82784377Sluigi
828155136Sluigi    # correct blocks according to size.
829155136Sluigi    blocks=${fd_size};
83072770Sluigi    if [ "${blocks}" = "1720" ]; then
831155136Sluigi	blocks=1722
83272770Sluigi    elif [ "${blocks}" = "1480" ]; then
833155136Sluigi	blocks=1476
83472770Sluigi    fi
83572770Sluigi
83684377Sluigi    log "Labeling floppy image"
837155136Sluigi
838155136Sluigi    dst=${BUILDDIR}/image.tree
839155136Sluigi    rm -rf ${dst}
840155136Sluigi    mkdir -p ${dst}
84172770Sluigi    (
84272770Sluigi    cd ${BUILDDIR}
843155136Sluigi    set 0 0 # reset variables
84499946Sluigi    # $1 takes the offset of the MFS filesystem
84599946Sluigi    set `strings -at d kernel | grep "MFS Filesystem goes here"`
846155136Sluigi    mfs_start=$1
847155136Sluigi    set 0 0 # reset variables
848155136Sluigi    set `strings -at d kernel | grep "MFS Filesystem had better"`
849155136Sluigi    mfs_end=$1
850155136Sluigi    mfs_size="$((${mfs_end} - ${mfs_start}))"
851155136Sluigi    set -- `ls -l ${c_fs}`; imgsize="$5"
852155136Sluigi    if [ ${mfs_start} -gt 0 -a ${mfs_size} -ge ${imgsize} ] ; then
853155136Sluigi	mfs_ofs=$((${mfs_start} + 8192))
854155136Sluigi	log "Preload kernel with file ${c_fs} at ${mfs_ofs}"
855203877Sluigi	log "`ls -l ${c_fs}` to fit in ${mfs_size}"
856155136Sluigi	dd if=${c_fs} ibs=8192 iseek=1 of=kernel obs=${mfs_ofs} \
857188835Sluigi	    oseek=1 conv=notrunc # 2> /dev/null
858155136Sluigi    else
859155136Sluigi    	log "not loading mfs, size ${mfs_size} img ${imgsize}"
860155136Sluigi    fi
861277639Sluigi    log "Compress with gzip and copy to floppy image"
86272770Sluigi
863229511Sluigi    mkdir -p  ${dst}/boot/kernel
864234983Sluigi    # XXX loader.conf does not work unless we also load the .4th files
865255315Sluigi    # echo "hint.acpi.0.disabled=\"1\"" > ${dst}/boot/loader.conf
866255315Sluigi    # echo "console=\"comconsole\"" >> ${dst}/boot/loader.conf
867255315Sluigi    local blf="loader* *.4th" # loader.rc loader.4th support.4th"
868255315Sluigi    (cd /boot; cp -p loader ${dst}/boot) || fail $? no_space "copying bootloader"
869255315Sluigi    cp ${MY_TREE}/floppy.tree/boot/loader.conf ${dst}/boot || true
870229511Sluigi    gzip -c kernel > ${dst}/boot/kernel/kernel.gz || fail $? no_space "copying kernel"
871229511Sluigi
87272770Sluigi    # now transfer the floppy tree. If it is already in mfs, dont bother.
87384377Sluigi    if [ "${o_all_in_mfs}" != "yes" ] ; then
874188835Sluigi	log "Now transfer floppy tree if not already in MFS image"
87584377Sluigi	cp -Rp floppy.tree/* ${dst} || \
87684377Sluigi		fail $? no_space "copying floppy tree"
87772770Sluigi    fi
87872770Sluigi    )
879188835Sluigi
880188835Sluigi    # add local manipulation to the image
881188835Sluigi    if [ -f ${MY_TREE}/buildtree.mk ] ; then
882188835Sluigi	${BINMAKE} -C ${dst} -f ${MY_TREE}/buildtree.mk image.tree
883188835Sluigi    fi
884188835Sluigi
885188835Sluigi    log "image used `du -s ${dst}` of ${blocks}k"
886194631Sluigi    if [ "${generate_iso}" = "YES" ]; then
887194631Sluigi	logverbose "generate_iso ${generate_iso}"
888194631Sluigi	# build_iso_image	# XXX not implemented yet
889194631Sluigi	(cd ${BUILDDIR}
890194631Sluigi	cp -p /boot/cdboot ${dst}/boot || fail $? no_space "copying cdboot"
891194631Sluigi	mkisofs -b boot/cdboot -no-emul-boot -J -r -ldots -l -L \
892194631Sluigi		-o ${c_iso} ${dst}
893194631Sluigi	)
894194631Sluigi    fi
895194631Sluigi
896155136Sluigi    (cd ${BUILDDIR}
897155136Sluigi    makefs -t ffs -o bsize=4096 -o fsize=512 \
898155136Sluigi	-s ${blocks}k -f 50 ${c_img} ${dst}
899190378Sluigi
900190378Sluigi    ${c_label} -w -f `pwd`/${c_img} auto # write in a label
901155136Sluigi    # copy partition c: into a: with some sed magic
902190378Sluigi    ${c_label} -f `pwd`/${c_img} | sed -e '/  c:/{p;s/c:/a:/;}' | \
903190378Sluigi	${c_label} -R -f `pwd`/${c_img} /dev/stdin
904190378Sluigi    ${c_label} -f `pwd`/${c_img}
905188835Sluigi
906155136Sluigi    ls -l ${c_img}
907190378Sluigi    ${c_label} -f `pwd`/${c_img}
908203877Sluigi    log "after disklabel"
909188835Sluigi    )
910188835Sluigi
911188835Sluigi    echo "BUILDDIR ${BUILDDIR}"
912188835Sluigi
913155136Sluigi    # dump the primary and secondary boot
914155136Sluigi    # XXX primary is 512 bytes
915155136Sluigi    dd if=${c_boot1} of=${BUILDDIR}/${c_img} conv=notrunc 2>/dev/null
916155136Sluigi    # XXX secondary starts after the 0x114 = dec 276 bytes of the label
917155136Sluigi    # so we skip 276 from the source, and 276+512=788 from dst
918155136Sluigi    # the old style blocks used 512 and 1024 respectively
91972770Sluigi
920229511Sluigi    dd if=${c_boot2} iseek=1 ibs=276 2> /dev/null | \
921155136Sluigi	dd of=${BUILDDIR}/${c_img} oseek=1 obs=788 conv=notrunc 2>/dev/null
922203877Sluigi    log "done disk image"
923155136Sluigi    # XXX (log "Fixing permissions"; cd ${dst}; chown -R root *)
924277639Sluigi    df -ik ${dst} | colrm 70 > .build.reply
925229511Sluigi    # leave build stuff if verbose
926229511Sluigi    [ ${o_verbose} -gt 0 ] && return
927229511Sluigi
928155136Sluigi    rm -rf ${BUILDDIR}/floppy.tree || true # cleanup
92984377Sluigi    rm -rf ${dst}
930188835Sluigi    rm ${BUILDDIR}/${c_fs}
931188835Sluigi    # rm ${BUILDDIR}/kernel.gz
93272770Sluigi}
93372770Sluigi
93484377Sluigi# This function creates variables which depend on the source tree in use:
93599946Sluigi# SRC, l_usrtree, l_objtree
93684377Sluigi# Optionally creates libraries, includes and the like (for cross compiles,
93784377Sluigi# needs to be done once).
93884377Sluigi
93984377Sluigiset_build_parameters() {
94084377Sluigi    if [ "${SRC}" = "/usr/src" ] ; then
94184377Sluigi	l_usrtree=${USR:-/usr}
94284377Sluigi    else
94384377Sluigi	l_usrtree=${USR:-${SRC}/../usr}
94484093Sluigi    fi
945227878Sluigi    l_objtree=${l_usrtree}/obj-pico-${o_arch}
946215177Sluigi
94784377Sluigi    PICO_TREE=${PICO_TREE:-${SRC}/release/picobsd}
94899946Sluigi    set `grep "#define[\t ]__FreeBSD_version" ${SRC}/sys/sys/param.h`
94999946Sluigi    OSVERSION=$3
95099951Sluigi    log "OSVERSION is ${OSVERSION}"
951277639Sluigi
952215177Sluigi	export MAKEOBJDIRPREFIX=${l_objtree}
953227878Sluigi	export TARGET_ARCH=${o_arch} TARGET=${o_arch}
954277639Sluigi	# XXX 20131001 see if CLANG fixes the build
955277639Sluigi	export WITHOUT_CLANG_IS_CC=yes
956277639Sluigi	export WITHOUT_CLANG_BOOTSTRAP=yes
957277639Sluigi	export WITH_GCC=yes
958277639Sluigi	export WITH_GCC_BOOTSTRAP=yes
959277639Sluigi	export WITH_GNUCXX=yes
960277639Sluigi	export WITHOUT_CLANG=yes
961277639Sluigi	export WITHOUT_ICONV=yes
962277639Sluigi
963229511Sluigi	# XXX why change machine_arch ?
964229511Sluigi	#-- export MACHINE_ARCH=`uname -m` MACHINE=`uname -m`
965227878Sluigi	# export CWARNFLAGS="-Wextra -Wno-sign-compare -Wno-missing-field-initializers"
966277639Sluigi	# XXX BINMAKE does not really exist anymore
967215177Sluigi	eval "export BINMAKE=\"`cd ${SRC}; make -f Makefile -V BINMAKE`\""
968255313Sluigi	[ "$BINMAKE" = "" ] && \
969255313Sluigi	   eval "export BINMAKE=\"`cd ${SRC}; make -f Makefile -V SUB_MAKE`\""
970215177Sluigi
97184377Sluigi    if [ "${o_init_src}" != "" ] ; then
972277639Sluigi	create_includes_and_libraries2
973229511Sluigi    else
974229511Sluigi	eval export `cd ${SRC}; ${BINMAKE} -f Makefile.inc1 -V WMAKEENV`
97584093Sluigi    fi
976190383Sluigi
977190383Sluigi    # if we have o_objdir, find where bin/ is
978190383Sluigi    if [ ! -z "${o_objdir}" ] ; then
979190383Sluigi	if [ -d ${o_objdir}/bin ] ; then
980190383Sluigi	    # fine
981190383Sluigi	elif [ -d "${o_objdir}${SRC}/bin" ] ; then
982190383Sluigi	    o_objdir="${o_objdir}${SRC}"
983190383Sluigi	    log "Changing objdir to ${o_objdir}"
984190383Sluigi	else
985190383Sluigi	    log "Cannot find objdir in ${o_objdir}, sorry"
986190383Sluigi	    o_objdir=""
987190383Sluigi	fi
988190383Sluigi    fi
98984093Sluigi}
99084093Sluigi
99184377Sluigi#-------------------------------------------------------------------
99284377Sluigi# Main entry of the script. Initialize variables, parse command line
99384377Sluigi# arguments.
99484093Sluigi
995218359Sluigi
99684377Sluigiset_defaults
997188835Sluigiwhile [ true ]; do
998190383Sluigi    log "Parsing $1"
99972770Sluigi    case $1 in
1000277639Sluigi    -j)
1001277639Sluigi	o_par="-j $2"
1002277639Sluigi	shift
1003277639Sluigi	;;
1004277639Sluigi
1005229511Sluigi    --par)
1006277639Sluigi	o_par="-j 8"	# watch out, this might be too large
1007229511Sluigi	;;
1008229511Sluigi
100978494Sluigi    --src)	# set the source path instead of /usr/src
1010190383Sluigi	SRC=`realpath $2`
101184093Sluigi	shift
101278494Sluigi	;;
1013229511Sluigi
1014229511Sluigi    --init)	# run a partial buildworld on the source tree
101584377Sluigi	o_init_src="YES"
101684377Sluigi	;;
101782917Sluigi
1018229511Sluigi    --arch)	# override the target architecture
1019227878Sluigi	o_arch=$2
1020227878Sluigi	shift
1021227878Sluigi	;;
1022227878Sluigi
1023229511Sluigi    --floppy_size)	# image size
102484377Sluigi	fd_size=$2
102572770Sluigi	shift
102672770Sluigi	;;
102784377Sluigi
102884377Sluigi    --all_in_mfs)
102984377Sluigi	o_all_in_mfs="yes"
103084377Sluigi	;;
103184377Sluigi
103284377Sluigi    --no_all_in_mfs)
1033188835Sluigi	o_all_in_mfs="no"
103484377Sluigi	;;
103584377Sluigi
103685833Sluigi    --modules)	# also build kernel modules
103785833Sluigi	o_do_modules="yes"
103885833Sluigi	;;
1039229511Sluigi
104072770Sluigi    -n)
104184377Sluigi	o_interactive="NO"
104272770Sluigi	;;
104384377Sluigi
104484377Sluigi    -clear|-clean|-c) # clean
104584377Sluigi	o_clean="YES"
104684377Sluigi	o_interactive="NO"
104772770Sluigi	;;
104884377Sluigi
104984377Sluigi    -v) # need -v -v to wait for user input
105084377Sluigi	o_verbose=$((${o_verbose}+1))	# verbose level
105184377Sluigi	o_tarv="v"			# tar verbose flag
105284377Sluigi	o_makeopts="-d l" # be verbose
105372770Sluigi	;;
1054188835Sluigi
1055188835Sluigi    --iso) # generate iso image
1056188835Sluigi	generate_iso="YES"
1057188835Sluigi	;;
1058188835Sluigi
1059189978Sluigi    --cfg) # read additional config from this file
1060189978Sluigi	o_additional_config=`realpath $2`
1061189978Sluigi	shift
1062189978Sluigi	;;
1063189978Sluigi
1064190383Sluigi    --objdir)	# Place with results of a previous buildworld
1065190383Sluigi		# useful if you want to copy shared binaries and libs
1066190383Sluigi	o_objdir=`realpath $2`
1067190383Sluigi	shift
1068190383Sluigi	;;
1069190383Sluigi
107072770Sluigi    *)
1071188835Sluigi	break
107272770Sluigi	;;
107372770Sluigi
107472770Sluigi    esac
107572770Sluigi    shift
107672770Sluigidone
1077190378Sluigi
107884377Sluigiset_build_parameters	# things that depend on ${SRC}
1079188835Sluigiset_type $1 $2		# type and site, respectively
108072770Sluigi
1081173602Sluigi[ "${o_interactive}" != "NO" ] && main_dialog
1082173602Sluigi
108384377Sluigiif [ "${o_clean}" = "YES" ] ; then
108472770Sluigi    clean_tree
108572770Sluigielse
108672770Sluigi    build_image
108772770Sluigi    do_install
108872770Sluigifi
108972770Sluigifinal_cleanup
109072770Sluigiexit 0
1091