xref: /freebsd/tools/boot/install-boot.sh (revision 488ab515d6cc02f6f743f0badfc8e94eb553cd30)
1#!/bin/sh
2
3# $FreeBSD$
4
5#
6# Installs/updates the necessary boot blocks for the desired boot environment
7#
8# Lightly tested.. Intended to be installed, but until it matures, it will just
9# be a boot tool for regression testing.
10
11# insert code here to guess what you have -- yikes!
12
13die() {
14    echo $*
15    exit 1
16}
17
18doit() {
19    echo $*
20    eval $*
21}
22
23find-part() {
24    dev=$1
25    part=$2
26
27    gpart show $dev | tail +2 | awk '$4 == "'$part'" { print $3; }'
28}
29
30boot_nogeli_gpt_zfs_legacy() {
31    dev=$1
32    dst=$2
33
34    idx=$(find-part $dev "freebsd-boot")
35    if [ -z "$idx" ] ; then
36	die "No freebsd-boot partition found"
37    fi
38    doit gpart bootcode -b ${gpt0} -p ${gptzfs2} -i $idx $dev
39    exit 0
40}
41
42boot_nogeli_gpt_ufs_legacy() {
43    dev=$1
44    dst=$2
45
46    idx=$(find-part $dev "freebsd-boot")
47    if [ -z "$idx" ] ; then
48	die "No freebsd-boot partition found"
49    fi
50    doit gpart bootcode -b ${gpt0} -p ${gpt2} -i $idx $dev
51    exit 0
52}
53
54boot_nogeli_mbr_zfs_legacy() {
55    dev=$1
56    dst=$2
57
58    # search to find the BSD slice
59    s=$(findpart $dev "freebsd-zfs")
60    if [ -z "$s" ] ; then
61	die "No freebsd-zfs slice found"
62    fi
63    # search to find the freebsd-zfs partition within the slice
64    # Or just assume it is 'a' because it has to be since it fails otherwise
65    dd if=${dst}/boot/zfsboot of=/tmp/zfsboot1 count=1
66    doit gpart bootcode -b /tmp/zfsboo1 ${dev}s${s}	# Put boot1 into the start of part
67    sysctl kern.geom.debugflags=0x10		# Put boot2 into ZFS boot slot
68    doit dd if=${dst}/boot/zfsboot of=/dev/${dev}s${s} iseek=1 seek=1024
69    sysctl kern.geom.debugflags=0x0
70
71    exit 0
72}
73
74boot_nogeli_mbr_ufs_legacy() {
75    dev=$1
76    dst=$2
77
78    doit gpart bootcode -b ${mbr0} ${dev}
79    s=$(findpart $dev "freebsd-ufs")
80    if [ -z "$s" ] ; then
81	die "No freebsd-ufs slice found"
82    fi
83    doit gpart bootcode -p ${mbr2} ${dev}s${s}
84    exit 0
85}
86
87boot_nogeli_vtoc8_ufs_ofw() {
88    dev=$1
89    dst=$2
90
91    # For non-native builds, ensure that geom_part(4) supports VTOC8.
92    kldload geom_part_vtoc8.ko
93    doit gpart bootcode -p ${vtoc8} ${dev}
94    exit 0
95}
96
97DESTDIR=/
98
99# Note: we really don't support geli boot in this script yet.
100geli=nogeli
101
102while getopts "b:d:f:g:o:s:" opt; do
103    case "$opt" in
104	b)
105	    bios=${OPTARG}
106	    ;;
107	d)
108	    DESTDIR=${OPTARG}
109	    ;;
110	f)
111	    fs=${OPTARG}
112	    ;;
113	g)
114	    case ${OPTARG} in
115		[Yy][Ee][Ss]|geli) geli=geli ;;
116		*) geli=nogeli ;;
117	    esac
118	    ;;
119	o)
120	    opts=${OPTARG}
121	    ;;
122	s)
123	    scheme=${OPTARG}
124	    ;;
125    esac
126done
127
128shift $((OPTIND-1))
129dev=$1
130
131# For gpt, we need to install pmbr as the primary boot loader
132# it knows about
133gpt0=${DESTDIR}/boot/pmbr
134gpt2=${DESTDIR}/boot/gptboot
135gptzfs2=${DESTDIR}/boot/gptzfsboot
136
137# For MBR, we have lots of choices, but select boot0
138mbr0=${DESTDIR}/boot/boot0
139mbr2=${DESTDIR}/boot/boot
140
141# VTOC8
142vtoc8=${DESTDIR}/boot/boot1
143
144# sanity check here
145
146eval boot_${geli}_${scheme}_${fs}_${bios} $dev $DESTDIR $opts || echo "Unsupported boot env: ${geli}-${scheme}-${fs}-${bios}"
147