xref: /freebsd/tools/boot/install-boot.sh (revision ecbde90073aef9166e77cecf61bd80c15a707a53)
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_gpt_zfs_uefi() {
55    dev=$1
56    dst=$2
57
58    idx=$(find-part $dev "efi")
59    if [ -z "$idx" ] ; then
60	die "No efi ESP partition found"
61    fi
62    doit gpart bootcode -p ${efi2} -i $idx $dev
63    exit 0
64}
65
66boot_nogeli_gpt_zfs_both() {
67    dev=$1
68    dst=$2
69
70    # XXX: Should this be copy/pasted, or should it call both _uefi and _legacy
71    idx=$(find-part $dev "efi")
72    if [ -z "$idx" ] ; then
73	die "No efi ESP partition found"
74    fi
75    doit gpart bootcode -p ${efi2} -i $idx $dev
76
77    idx=$(find-part $dev "freebsd-boot")
78    if [ -z "$idx" ] ; then
79	die "No freebsd-boot partition found"
80    fi
81    doit gpart bootcode -b ${gpt0} -p ${gptzfs2} -i $idx $dev
82    exit 0
83}
84
85boot_nogeli_mbr_zfs_legacy() {
86    dev=$1
87    dst=$2
88
89    # search to find the BSD slice
90    s=$(findpart $dev "freebsd-zfs")
91    if [ -z "$s" ] ; then
92	die "No freebsd-zfs slice found"
93    fi
94    # search to find the freebsd-zfs partition within the slice
95    # Or just assume it is 'a' because it has to be since it fails otherwise
96    dd if=${dst}/boot/zfsboot of=/tmp/zfsboot1 count=1
97    doit gpart bootcode -b /tmp/zfsboo1 ${dev}s${s}	# Put boot1 into the start of part
98    sysctl kern.geom.debugflags=0x10		# Put boot2 into ZFS boot slot
99    doit dd if=${dst}/boot/zfsboot of=/dev/${dev}s${s} iseek=1 seek=1024
100    sysctl kern.geom.debugflags=0x0
101
102    exit 0
103}
104
105boot_nogeli_mbr_ufs_legacy() {
106    dev=$1
107    dst=$2
108
109    doit gpart bootcode -b ${mbr0} ${dev}
110    s=$(findpart $dev "freebsd-ufs")
111    if [ -z "$s" ] ; then
112	die "No freebsd-ufs slice found"
113    fi
114    doit gpart bootcode -p ${mbr2} ${dev}s${s}
115    exit 0
116}
117
118boot_nogeli_vtoc8_ufs_ofw() {
119    dev=$1
120    dst=$2
121
122    # For non-native builds, ensure that geom_part(4) supports VTOC8.
123    kldload geom_part_vtoc8.ko
124    doit gpart bootcode -p ${vtoc8} ${dev}
125    exit 0
126}
127
128DESTDIR=/
129
130# Note: we really don't support geli boot in this script yet.
131geli=nogeli
132
133while getopts "b:d:f:g:o:s:" opt; do
134    case "$opt" in
135	b)
136	    bios=${OPTARG}
137	    ;;
138	d)
139	    DESTDIR=${OPTARG}
140	    ;;
141	f)
142	    fs=${OPTARG}
143	    ;;
144	g)
145	    case ${OPTARG} in
146		[Yy][Ee][Ss]|geli) geli=geli ;;
147		*) geli=nogeli ;;
148	    esac
149	    ;;
150	o)
151	    opts=${OPTARG}
152	    ;;
153	s)
154	    scheme=${OPTARG}
155	    ;;
156    esac
157done
158
159shift $((OPTIND-1))
160dev=$1
161
162# For gpt, we need to install pmbr as the primary boot loader
163# it knows about
164gpt0=${DESTDIR}/boot/pmbr
165gpt2=${DESTDIR}/boot/gptboot
166gptzfs2=${DESTDIR}/boot/gptzfsboot
167
168# For gpt + EFI we install the ESP
169# XXX This should use newfs or makefs, but it deosn't yet
170efi2=${DESTDIR}/boot/boot1.efifat
171
172# For MBR, we have lots of choices, but select boot0
173mbr0=${DESTDIR}/boot/boot0
174mbr2=${DESTDIR}/boot/boot
175
176# VTOC8
177vtoc8=${DESTDIR}/boot/boot1
178
179# sanity check here
180
181eval boot_${geli}_${scheme}_${fs}_${bios} $dev $DESTDIR $opts || echo "Unsupported boot env: ${geli}-${scheme}-${fs}-${bios}"
182