xref: /netbsd-src/usr.sbin/sysinst/arch/prep/md.c (revision 4204f81037ec1430b81b70ee975f04b277901e24)
1 /*	$NetBSD: md.c,v 1.14 2022/01/29 16:01:20 martin Exp $	*/
2 
3 /*
4  * Copyright 1997 Piermont Information Systems Inc.
5  * All rights reserved.
6  *
7  * Based on code written by Philip A. Nelson for Piermont Information
8  * Systems Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. The name of Piermont Information Systems Inc. may not be used to endorse
19  *    or promote products derived from this software without specific prior
20  *    written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
23  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
26  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32  * THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 /* md.c -- prep machine specific routines */
36 
37 #include <sys/param.h>
38 #include <sys/sysctl.h>
39 #include <stdio.h>
40 #include <util.h>
41 #include <machine/cpu.h>
42 
43 #include "defs.h"
44 #include "md.h"
45 #include "msg_defs.h"
46 #include "menu_defs.h"
47 #include "endian.h"
48 
49 int prep_nobootfix = 0, prep_rawdevfix = 0;
50 size_t prep_bootpart = ~0U;
51 
52 void
md_init(void)53 md_init(void)
54 {
55 }
56 
57 void
md_init_set_status(int flags)58 md_init_set_status(int flags)
59 {
60 	(void)flags;
61 }
62 
63 bool
md_get_info(struct install_partition_desc * install)64 md_get_info(struct install_partition_desc *install)
65 {
66 	int res;
67 
68 	if (pm->no_mbr || pm->no_part)
69 		return true;
70 
71 again:
72 	if (pm->parts == NULL) {
73 
74 		const struct disk_partitioning_scheme *ps =
75 		    select_part_scheme(pm, NULL, true, NULL);
76 
77 		if (!ps)
78 			return false;
79 
80 		struct disk_partitions *parts =
81 		   (*ps->create_new_for_disk)(pm->diskdev,
82 		   0, pm->dlsize, true, NULL);
83 		if (!parts)
84 			return false;
85 
86 		pm->parts = parts;
87 		if (ps->size_limit > 0 && pm->dlsize > ps->size_limit)
88 			pm->dlsize = ps->size_limit;
89 	}
90 
91 	res = set_bios_geom_with_mbr_guess(pm->parts);
92 	if (res == 0)
93 		return false;
94 	else if (res == 1)
95 		return true;
96 
97 	pm->parts->pscheme->destroy_part_scheme(pm->parts);
98 	pm->parts = NULL;
99 	goto again;
100 }
101 
102 /*
103  * md back-end code for menu-driven BSD disklabel editor.
104  */
105 int
md_make_bsd_partitions(struct install_partition_desc * install)106 md_make_bsd_partitions(struct install_partition_desc *install)
107 {
108 	return make_bsd_partitions(install);
109 }
110 
111 /*
112  * any additional partition validation
113  */
114 bool
md_check_partitions(struct install_partition_desc * install)115 md_check_partitions(struct install_partition_desc *install)
116 {
117 	size_t part;
118 
119 	/* we need to find a boot partition, otherwise we can't write our
120 	 * "bootblock".  We make the assumption that the user hasn't done
121 	 * something stupid, like move it away from the MBR partition.
122 	 */
123 	for (part = 0; part < install->num; part++)
124 		if (install->infos[part].fs_type == FS_BOOT) {
125 			prep_bootpart = part;
126 			return true;
127 		}
128 
129 	msg_display(MSG_prepnobootpart);
130 	process_menu(MENU_ok, NULL);
131 	return false;
132 }
133 
134 /*
135  * hook called before writing new disklabel.
136  */
137 bool
md_pre_disklabel(struct install_partition_desc * install,struct disk_partitions * parts)138 md_pre_disklabel(struct install_partition_desc *install,
139     struct disk_partitions *parts)
140 {
141 
142 	if (parts->parent == NULL)
143 		return true;	/* no outer partitions */
144 
145 	parts = parts->parent;
146 
147 	msg_display_subst(MSG_dofdisk, 3, parts->disk,
148 	    msg_string(parts->pscheme->name),
149 	    msg_string(parts->pscheme->short_name));
150 
151 	/* write edited "MBR" onto disk. */
152 	if (!parts->pscheme->write_to_disk(parts)) {
153 		msg_display(MSG_wmbrfail);
154 		process_menu(MENU_ok, NULL);
155 		return false;
156 	}
157 	return true;
158 }
159 
160 /*
161  * hook called after writing disklabel to new target disk.
162  */
163 bool
md_post_disklabel(struct install_partition_desc * install,struct disk_partitions * parts)164 md_post_disklabel(struct install_partition_desc *install,
165     struct disk_partitions *parts)
166 {
167 	return true;
168 }
169 
170 /*
171  * hook called after upgrade() or install() has finished setting
172  * up the target disk but immediately before the user is given the
173  * ``disks are now set up'' message.
174  */
175 int
md_post_newfs(struct install_partition_desc * install)176 md_post_newfs(struct install_partition_desc *install)
177 {
178 	return 0;
179 }
180 
181 int
md_post_extract(struct install_partition_desc * install,bool upgrade)182 md_post_extract(struct install_partition_desc *install, bool upgrade)
183 {
184 	char rawdev[100], bootpart[100], bootloader[100];
185 	int contype;
186 
187 	/* if we can't make it bootable, just punt */
188 	if (prep_nobootfix)
189 		return 0;
190 
191 	process_menu(MENU_prepconsole, &contype);
192 	if (contype == 1)
193 		snprintf(bootloader, 100, "/usr/mdec/boot_com0");
194 	else
195 		snprintf(bootloader, 100, "/usr/mdec/boot");
196 
197 	snprintf(rawdev, 100, "/dev/r%s%c", pm->diskdev,
198 		(char)('a' + getrawpartition()));
199 	snprintf(bootpart, 100, "/dev/r%s%c", pm->diskdev,
200 		(char)('a' + prep_bootpart));
201 	if (prep_rawdevfix)
202 		run_program(RUN_DISPLAY|RUN_CHROOT,
203 		    "/usr/mdec/mkbootimage -b %s -k /netbsd "
204 		    "-r %s /.bootimage", bootloader, rawdev);
205 	else
206 		run_program(RUN_DISPLAY|RUN_CHROOT,
207 		    "/usr/mdec/mkbootimage -s -b %s -k /netbsd /.bootimage",
208 		    bootloader);
209 	run_program(RUN_DISPLAY|RUN_CHROOT, "/bin/dd if=/.bootimage of=%s "
210 	    "bs=512 conv=sync", bootpart);
211 
212 	return 0;
213 }
214 
215 void
md_cleanup_install(struct install_partition_desc * install)216 md_cleanup_install(struct install_partition_desc *install)
217 {
218 #ifndef DEBUG
219 	enable_rc_conf();
220 #endif
221 	run_program(0, "rm -f %s", target_expand("/.bootimage"));
222 }
223 
224 int
md_pre_update(struct install_partition_desc * install)225 md_pre_update(struct install_partition_desc *install)
226 {
227 	size_t i;
228 
229 	/* do a sanity check of the partition table */
230 	for (i = 0; i < install->num; i++) {
231 		if (install->infos[i].fs_type != PART_BOOT_TYPE)
232 			continue;
233 		if (install->infos[i].size < (int)(MIN_PREP_BOOT/512)) {
234 			msg_display(MSG_preptoosmall);
235 			msg_fmt_display_add(MSG_prepnobootpart, "%d", 0);
236 			if (!ask_yesno(NULL))
237 				return 0;
238 			prep_nobootfix = 1;
239 		}
240 		if (install->infos[i].cur_start == 0)
241 			prep_rawdevfix = 1;
242 	}
243 	if (!md_check_partitions(install))
244 		prep_nobootfix = 1;
245 	return 1;
246 }
247 
248 /* Upgrade support */
249 int
md_update(struct install_partition_desc * install)250 md_update(struct install_partition_desc *install)
251 {
252 	md_post_newfs(install);
253 	return 1;
254 }
255 
256 int
md_check_mbr(struct disk_partitions * parts,mbr_info_t * mbri,bool quiet)257 md_check_mbr(struct disk_partitions *parts, mbr_info_t *mbri, bool quiet)
258 {
259 	mbr_info_t *ext;
260 	struct mbr_partition *part;
261 	int i;
262 
263 	for (ext = mbri; ext; ext = ext->extended) {
264 		part = ext->mbr.mbr_parts;
265 		for (i = 0; i < MBR_PART_COUNT; part++, i++) {
266 			if (part->mbrp_type != MBR_PTYPE_PREP)
267 				continue;
268 			pm->bootstart = part->mbrp_start;
269 			pm->bootsize = part->mbrp_size;
270 			break;
271 		}
272 	}
273 	if (pm->bootsize < (int)(MIN_PREP_BOOT/512)) {
274 		msg_display(MSG_preptoosmall);
275 		return ask_reedit(parts);
276 	}
277 	if (pm->bootstart == 0 || pm->bootsize == 0) {
278 		if (quiet)
279 			return 0;
280 		msg_display(MSG_nopreppart);
281 		return ask_reedit(parts);
282 	}
283 	return 2;
284 }
285 
286 bool
md_parts_use_wholedisk(struct disk_partitions * parts)287 md_parts_use_wholedisk(struct disk_partitions *parts)
288 {
289 	struct disk_part_info boot_part = {
290 		.size = PART_BOOT / 512,
291 		.fs_type = PART_BOOT_TYPE,
292 	};
293 
294 	boot_part.nat_type = parts->pscheme->get_fs_part_type(
295 	    PT_root, boot_part.fs_type, boot_part.fs_sub_type);
296 
297 	return parts_use_wholedisk(parts, 1, &boot_part);
298 }
299 
300 int
md_pre_mount(struct install_partition_desc * install,size_t ndx)301 md_pre_mount(struct install_partition_desc *install, size_t ndx)
302 {
303 	return 0;
304 }
305 
306 bool
md_mbr_update_check(struct disk_partitions * parts,mbr_info_t * mbri)307 md_mbr_update_check(struct disk_partitions *parts, mbr_info_t *mbri)
308 {
309 	return false;	/* no change, no need to write back */
310 }
311 
312 #ifdef HAVE_GPT
313 bool
md_gpt_post_write(struct disk_partitions * parts,part_id root_id,bool root_is_new,part_id efi_id,bool efi_is_new)314 md_gpt_post_write(struct disk_partitions *parts, part_id root_id,
315     bool root_is_new, part_id efi_id, bool efi_is_new)
316 {
317 	/* no GPT boot support, nothing needs to be done here */
318 	return true;
319 }
320 #endif
321 
322