1#	$OpenBSD: install.md,v 1.78 2023/05/26 11:41:50 kn Exp $
2#
3# Copyright (c) 1996 The NetBSD Foundation, Inc.
4# All rights reserved.
5#
6# This code is derived from software contributed to The NetBSD Foundation
7# by Jason R. Thorpe.
8#
9# Redistribution and use in source and binary forms, with or without
10# modification, are permitted provided that the following conditions
11# are met:
12# 1. Redistributions of source code must retain the above copyright
13#    notice, this list of conditions and the following disclaimer.
14# 2. Redistributions in binary form must reproduce the above copyright
15#    notice, this list of conditions and the following disclaimer in the
16#    documentation and/or other materials provided with the distribution.
17#
18# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
22# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28# POSSIBILITY OF SUCH DAMAGE.
29#
30#
31# machine dependent section of installation/upgrade script.
32#
33
34MDXAPERTURE=2
35MDXDM=y
36NCPU=$(sysctl -n hw.ncpufound)
37
38md_installboot() {
39	if ! installboot -r /mnt ${1}; then
40		echo "\nFailed to install bootblocks."
41		echo "You will not be able to boot OpenBSD from ${1}."
42		exit
43	fi
44}
45
46md_prep_MBR() {
47	local _disk=$1 _q _d
48
49	if disk_has $_disk hfs; then
50		cat <<__EOT
51
52WARNING: Putting an MBR partition table on $_disk will DESTROY the existing HFS
53         partitions and HFS partition table:
54$(pdisk -l $_disk)
55
56__EOT
57		ask_yn "Are you *sure* you want an MBR partition table on $_disk?" || return 1
58	fi
59
60	while :; do
61		_d=whole
62
63		if disk_has $_disk mbr; then
64			fdisk $_disk
65			if disk_has $_disk mbr openbsd; then
66				_q=", use the (O)penBSD area"
67				_d=OpenBSD
68			fi
69		else
70			echo "MBR has invalid signature; not showing it."
71		fi
72		ask "Use (W)hole disk$_q or (E)dit the MBR?" "$_d"
73		case $resp in
74		[wW]*)
75			echo -n "Creating a 1MB DOS partition and an OpenBSD partition for rest of $_disk..."
76			dd if=/dev/zero of=/dev/r${_disk}c bs=1m count=1 status=none
77			fdisk -iy -b "2048@1:06" $_disk >/dev/null
78			echo "done."
79			break ;;
80		[eE]*)
81			# Manually configure the MBR.
82			cat <<__EOT
83
84You will now create one MBR partition to contain your OpenBSD data
85and one MBR partition to contain the program that Open Firmware uses
86to boot OpenBSD. Neither partition will overlap any other partition.
87
88The OpenBSD MBR partition will have an id of 'A6' and the boot MBR
89partition will have an id of '06' (DOS). The boot partition will be
90at least 1MB and be marked as the *only* active partition.
91
92$(fdisk $_disk)
93__EOT
94			fdisk -e $_disk
95			disk_has $_disk mbr dos ||
96				{ echo "\nNo DOS (id 06) partition!\n"; continue; }
97			disk_has $_disk mbr dos_active ||
98				{ echo "\nNo active DOS partition!\n"; continue; }
99			disk_has $_disk mbr openbsd ||
100				{ echo "\nNo OpenBSD (id A6) partition!\n"; continue; }
101			break ;;
102		[oO]*)
103			[[ $_d == OpenBSD ]] || continue
104			break ;;
105		esac
106	done
107
108	installboot -p $_disk
109}
110
111md_prep_HFS() {
112	local _disk=$1 _d _q
113
114	while :; do
115		_q=
116		_d=Modify
117		disk_has $_disk hfs openbsd &&
118			{ _q="Use the (O)penBSD partition, "; _d=OpenBSD; }
119		pdisk -l $_disk
120		ask "$_q(M)odify a partition or (A)bort?" "$_d"
121		case $resp in
122		[aA]*)	return 1 ;;
123		[oO]*)	return 0 ;;
124		[mM]*)	pdisk $_disk
125			disk_has $_disk hfs openbsd && break
126			echo "\nNo 'OpenBSD'-type partition named 'OpenBSD'!"
127		esac
128	done
129
130	return 0;
131}
132
133md_prep_disklabel() {
134	local _disk=$1 _f=/tmp/i/fstab.$1
135
136	PARTTABLE=
137	while [[ -z $PARTTABLE ]]; do
138		resp=MBR
139		disk_has $_disk hfs && ask "Use (H)FS or (M)BR partition table?" MBR
140		case $resp in
141		[mM]*)	md_prep_MBR $_disk && PARTTABLE=MBR ;;
142		[hH]*)	md_prep_HFS $_disk && PARTTABLE=HFS ;;
143		esac
144	done
145
146	disklabel_autolayout $_disk $_f || return
147	[[ -s $_f ]] && return
148
149	# Edit disklabel manually.
150	# Abandon all hope, ye who enter here.
151	disklabel -F $_f -E $_disk
152}
153
154md_congrats() {
155	cat <<__EOT
156
157INSTALL.$ARCH describes how to configure Open Firmware to boot OpenBSD. The
158command to boot OpenBSD will be something like 'boot hd:,ofwboot /bsd'.
159__EOT
160	if [[ $PARTTABLE == HFS ]]; then
161		cat <<__EOT
162
163NOTE: You must use MacOS to copy 'ofwboot' from the OpenBSD install media to
164the first HFS partition of $ROOTDISK.
165__EOT
166	fi
167
168}
169
170md_consoleinfo() {
171	local _u _d=zstty
172
173	for _u in $(scan_dmesg "/^$_d\([0-9]\) .*/s//\1/p"); do
174		if [[ $_d$_u == $CONSOLE || -z $CONSOLE ]]; then
175			CDEV=$_d$_u
176			: ${CSPEED:=57600}
177			set -- a b c d e f g h i j
178			shift $_u
179			CTTY=tty$1
180			return
181		fi
182	done
183}
184