xref: /netbsd-src/usr.sbin/sysinst/arch/i386/md.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: md.c,v 1.11 2018/06/03 18:59:45 christos 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 -- i386 machine specific routines - also used by amd64 */
36 
37 #include <sys/param.h>
38 #include <sys/sysctl.h>
39 #include <sys/exec.h>
40 #include <sys/utsname.h>
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include <machine/cpu.h>
44 #include <stdio.h>
45 #include <stddef.h>
46 #include <util.h>
47 #include <dirent.h>
48 #include <termios.h>
49 
50 #include "defs.h"
51 #include "md.h"
52 #include "endian.h"
53 #include "msg_defs.h"
54 #include "menu_defs.h"
55 
56 #ifdef NO_LBA_READS		/* for testing */
57 #undef BIFLAG_EXTINT13
58 #define BIFLAG_EXTINT13	0
59 #endif
60 
61 static struct biosdisk_info *biosdisk = NULL;
62 
63 /* prototypes */
64 
65 static int get_bios_info(char *);
66 static int mbr_root_above_chs(void);
67 static void md_upgrade_mbrtype(void);
68 static int md_read_bootcode(const char *, struct mbr_sector *);
69 static unsigned int get_bootmodel(void);
70 
71 void
72 md_init(void)
73 {
74 }
75 
76 void
77 md_init_set_status(int flags)
78 {
79 	(void)flags;
80 
81 	/* Default to install same type of kernel as we are running */
82 	set_kernel_set(get_bootmodel());
83 }
84 
85 int
86 md_get_info(void)
87 {
88 	mbr_info_t *ext;
89 	struct mbr_partition *p;
90 	const char *bootcode;
91 	int i;
92 	int names, fl, ofl;
93 #define	ACTIVE_FOUND	0x0100
94 #define	NETBSD_ACTIVE	0x0200
95 #define	NETBSD_NAMED	0x0400
96 #define	ACTIVE_NAMED	0x0800
97 
98 	if (pm->no_mbr)
99 		return 1;
100 
101 	if (read_mbr(pm->diskdev, &mbr) < 0)
102 		memset(&mbr.mbr, 0, sizeof mbr.mbr - 2);
103 	get_bios_info(pm->diskdev);
104 
105 edit:
106 	if (edit_mbr(&mbr) == 0)
107 		return 0;
108 
109 	root_limit = 0;
110 	if (biosdisk != NULL && (biosdisk->bi_flags & BIFLAG_EXTINT13) == 0) {
111 		if (mbr_root_above_chs()) {
112 			msg_display(MSG_partabovechs);
113 			if (!ask_noyes(NULL))
114 				goto edit;
115 			/* The user is shooting themselves in the foot here...*/
116 		} else
117 			root_limit = bcyl * bhead * bsec;
118 	}
119 
120 	/*
121 	 * Ensure the install partition (at sector pm->ptstart) and the active
122 	 * partition are bootable.
123 	 * Determine whether the bootselect code is needed.
124 	 * Note that MBR_BS_NEWMBR is always set, so we ignore it!
125 	 */
126 	fl = 0;
127 	names = 0;
128 	for (ext = &mbr; ext != NULL; ext = ext->extended) {
129 		p = ext->mbr.mbr_parts;
130 		for (i = 0; i < MBR_PART_COUNT; p++, i++) {
131 			if (p->mbrp_flag == MBR_PFLAG_ACTIVE) {
132 				fl |= ACTIVE_FOUND;
133 			    if (ext->sector + p->mbrp_start == pm->ptstart)
134 				fl |= NETBSD_ACTIVE;
135 			}
136 			if (ext->mbrb.mbrbs_nametab[i][0] == 0) {
137 				/* No bootmenu label... */
138 				if (ext->sector == 0)
139 					continue;
140 				if (ext->sector + p->mbrp_start == pm->ptstart)
141 					/*
142 					 * Have installed into an extended ptn
143 					 * force name & bootsel...
144 					 */
145 					names++;
146 				continue;
147 			}
148 			/* Partition has a bootmenu label... */
149 			if (ext->sector != 0)
150 				fl |= MBR_BS_EXTLBA;
151 			if (ext->sector + p->mbrp_start == pm->ptstart)
152 				fl |= NETBSD_NAMED;
153 			else if (p->mbrp_flag == MBR_PFLAG_ACTIVE)
154 				fl |= ACTIVE_NAMED;
155 			else
156 				names++;
157 		}
158 	}
159 	if (!(fl & ACTIVE_FOUND))
160 		fl |= NETBSD_ACTIVE;
161 	if (fl & NETBSD_NAMED && fl & NETBSD_ACTIVE)
162 		fl |= ACTIVE_NAMED;
163 
164 	if ((names > 0 || !(fl & NETBSD_ACTIVE)) &&
165 	    (!(fl & NETBSD_NAMED) || !(fl & ACTIVE_NAMED))) {
166 		/*
167 		 * There appear to be multiple bootable partitions, but they
168 		 * don't all have bootmenu texts.
169 		 */
170 		msg_display(MSG_missing_bootmenu_text);
171 		if (ask_yesno(NULL))
172 			goto edit;
173 	}
174 
175 	if ((fl & MBR_BS_EXTLBA) &&
176 	    (biosdisk == NULL || !(biosdisk->bi_flags & BIFLAG_EXTINT13))) {
177 		/* Need unsupported LBA reads to read boot sectors */
178 		msg_display(MSG_no_extended_bootmenu);
179 		if (!ask_noyes(NULL))
180 			goto edit;
181 	}
182 
183 	/* Sort out the name of the mbr code we need */
184 	if (names > 0 || fl & (NETBSD_NAMED | ACTIVE_NAMED)) {
185 		/* Need bootselect code */
186 		fl |= MBR_BS_ACTIVE;
187 		bootcode = fl & MBR_BS_EXTLBA ? _PATH_BOOTEXT : _PATH_BOOTSEL;
188 	} else
189 		bootcode = _PATH_MBR;
190 
191 	fl &=  MBR_BS_ACTIVE | MBR_BS_EXTLBA;
192 
193 	/* Look at what is installed */
194 	ofl = mbr.mbrb.mbrbs_flags;
195 	if (ofl == 0) {
196 		/* Check there is some bootcode at all... */
197 		if (mbr.mbr.mbr_magic != htole16(MBR_MAGIC) ||
198 		    mbr.mbr.mbr_jmpboot[0] == 0 ||
199 		    mbr_root_above_chs())
200 			/* Existing won't do, force update */
201 			fl |= MBR_BS_NEWMBR;
202 	}
203 	ofl = mbr.oflags & (MBR_BS_ACTIVE | MBR_BS_EXTLBA);
204 
205 	if (fl & ~ofl || (fl == 0 && ofl & MBR_BS_ACTIVE)) {
206 		/* Existing boot code isn't the right one... */
207 		if (fl & MBR_BS_ACTIVE)
208 			msg_display(MSG_installbootsel);
209 		else
210 			msg_display(MSG_installmbr);
211 	} else
212 		/* Existing code would (probably) be ok */
213 		msg_display(MSG_updatembr);
214 
215 	if (!ask_yesno(NULL))
216 		/* User doesn't want to update mbr code */
217 		return 1;
218 
219 	if (md_read_bootcode(bootcode, &mbr.mbr) == 0)
220 		/* update suceeded - to memory copy */
221 		return 1;
222 
223 	/* This shouldn't happen since the files are in the floppy fs... */
224 	msg_display("Can't find %s", bootcode);
225 	ask_yesno(NULL);
226 
227 	return 1;
228 }
229 
230 /*
231  * md back-end code for menu-driven BSD disklabel editor.
232  */
233 int
234 md_make_bsd_partitions(void)
235 {
236 	return make_bsd_partitions();
237 }
238 
239 /*
240  * any additional partition validation
241  */
242 int
243 md_check_partitions(void)
244 {
245 	int rval;
246 	char *bootxx;
247 
248 	/* check we have boot code for the root partition type */
249 	bootxx = bootxx_name();
250 	rval = access(bootxx, R_OK);
251 	free(bootxx);
252 	if (rval == 0)
253 		return 1;
254 	process_menu(MENU_ok, __UNCONST(MSG_No_Bootcode));
255 	return 0;
256 }
257 
258 /*
259  * hook called before writing new disklabel.
260  */
261 int
262 md_pre_disklabel(void)
263 {
264 	if (pm->no_mbr)
265 		return 0;
266 
267 	msg_display(MSG_dofdisk);
268 
269 	/* write edited MBR onto disk. */
270 	if (write_mbr(pm->diskdev, &mbr, 1) != 0) {
271 		msg_display(MSG_wmbrfail);
272 		process_menu(MENU_ok, NULL);
273 		return 1;
274 	}
275 	return 0;
276 }
277 
278 /*
279  * hook called after writing disklabel to new target disk.
280  */
281 int
282 md_post_disklabel(void)
283 {
284 	return 0;
285 }
286 
287 /*
288  * hook called after upgrade() or install() has finished setting
289  * up the target disk but immediately before the user is given the
290  * ``disks are now set up'' message.
291  */
292 int
293 md_post_newfs(void)
294 {
295 	int ret;
296 	size_t len;
297 	char boot_options[1024];
298 	char *bootxx_filename;
299 	/*
300 	 * XXX - this code retains a lot of cruft from when we went
301 	 * to great pains to exclude installboot from the ramdisk
302 	 * for size reasons and should be rewritten.
303 	 */
304 	static const char *consoles[]={
305         	"pc", /* CONSDEV_PC */
306 		"com0", /* CONSDEV_COM0 */
307 		"com1", /* CONSDEV_COM1 */
308 		"com2", /* CONSDEV_COM2 */
309 		"com3", /* CONSDEV_COM3 */
310 		"com0kbd", /* CONSDEV_COM0KBD */
311 		"com1kbd", /* CONSDEV_COM1KBD */
312 		"com2kbd", /* CONSDEV_COM2KBD */
313 		"com3kbd" /* CONSDEV_COM3KBD */ };
314 	static struct x86_boot_params boottype =
315 		{sizeof boottype, 0, 5, 0, 9600, { '\0' }, "", 0};
316 	static int conmib[] = {CTL_MACHDEP, CPU_CONSDEV};
317 	struct termios t;
318 	dev_t condev;
319 
320 	/*
321 	 * Get console device, should either be ttyE0 or tty0n.
322 	 * Too hard to double check, so just 'know' the device numbers.
323 	 */
324 	len = sizeof condev;
325 	if (sysctl(conmib, __arraycount(conmib), &condev, &len, NULL, 0) != -1
326 	    && (condev & ~3) == 0x800) {
327 		/* Motherboard serial port */
328 		boottype.bp_consdev = (condev & 3) + 1;
329 		/* Defaulting the baud rate to that of stdin should suffice */
330 		if (tcgetattr(0, &t) != -1)
331 			boottype.bp_conspeed = t.c_ispeed;
332 	}
333 
334 	if (pm == NULL || !pm->no_part) {
335 		/*
336 		 * Get console device, should either be ttyE0 or tty0n.
337 		 * Too hard to double check, so just 'know' the device numbers.
338 		 */
339 		len = sizeof condev;
340 		if (sysctl(conmib, nelem(conmib), &condev, &len, NULL, 0) != -1
341 		    && (condev & ~3) == 0x800) {
342 			/* Motherboard serial port */
343 			boottype.bp_consdev = (condev & 3) + 1;
344 			/* Defaulting the baud rate to that of stdin should suffice */
345 			if (tcgetattr(0, &t) != -1)
346 				boottype.bp_conspeed = t.c_ispeed;
347 		}
348 
349 		process_menu(MENU_getboottype, &boottype);
350 		msg_display(MSG_dobootblks, pm->diskdev);
351 		if (boottype.bp_consdev == ~0u)
352 			/* Use existing bootblocks */
353 			return 0;
354 	}
355 
356 	ret = cp_to_target("/usr/mdec/boot", "/boot");
357 	if (ret)
358 		return ret;
359 	if (pm && pm->no_part)
360 		return 0;
361 
362         bootxx_filename = bootxx_name();
363         if (bootxx_filename != NULL) {
364 		snprintf(boot_options, sizeof boot_options,
365 		    "console=%s,speed=%u", consoles[boottype.bp_consdev],
366 		    boottype.bp_conspeed);
367 		if (pm->isspecial) {
368                 	ret = run_program(RUN_DISPLAY,
369                 	    "/usr/sbin/installboot -o %s /dev/r%s %s",
370 			    boot_options, pm->diskdev, bootxx_filename);
371 		} else {
372                 	ret = run_program(RUN_DISPLAY,
373                 	    "/usr/sbin/installboot -o %s /dev/r%s%c %s",
374 			    boot_options, pm->diskdev, 'a' + pm->rootpart,
375 			    bootxx_filename);
376 		}
377                 free(bootxx_filename);
378         } else
379                 ret = -1;
380 
381         if (ret != 0)
382                 process_menu(MENU_ok,
383                     __UNCONST("Warning: disk is probably not bootable"));
384 
385 	return ret;
386 }
387 
388 int
389 md_post_extract(void)
390 {
391 	return 0;
392 }
393 
394 void
395 md_cleanup_install(void)
396 {
397 #ifndef DEBUG
398 	enable_rc_conf();
399 	add_rc_conf("wscons=YES\n");
400 
401 # if defined(__i386__) && defined(SET_KERNEL_TINY)
402 	/*
403 	 * For GENERIC_TINY, do not enable any extra screens or wsmux.
404 	 * Otherwise, run getty on 4 VTs.
405 	 */
406 	if (get_kernel_set() == SET_KERNEL_TINY)
407 		run_program(RUN_CHROOT,
408                             "sed -an -e '/^screen/s/^/#/;/^mux/s/^/#/;"
409 			    "H;$!d;g;w /etc/wscons.conf' /etc/wscons.conf");
410 	else
411 # endif
412 		run_program(RUN_CHROOT,
413 			    "sed -an -e '/^ttyE[1-9]/s/off/on/;"
414 			    "H;$!d;g;w /etc/ttys' /etc/ttys");
415 
416 #endif
417 }
418 
419 int
420 md_pre_update(void)
421 {
422 	return 1;
423 }
424 
425 /* Upgrade support */
426 int
427 md_update(void)
428 {
429 	md_post_newfs();
430 	md_upgrade_mbrtype();
431 	return 1;
432 }
433 
434 int
435 md_check_mbr(mbr_info_t *mbri)
436 {
437 	return 2;
438 }
439 
440 int
441 md_mbr_use_wholedisk(mbr_info_t *mbri)
442 {
443 	return mbr_use_wholedisk(mbri);
444 }
445 
446 static int
447 get_bios_info(char *dev)
448 {
449 	static struct disklist *disklist = NULL;
450 	static int mib[2] = {CTL_MACHDEP, CPU_DISKINFO};
451 	int i;
452 	size_t len;
453 	struct biosdisk_info *bip;
454 	struct nativedisk_info *nip = NULL, *nat;
455 	int cyl, head;
456 	daddr_t sec;
457 
458 	if (disklist == NULL) {
459 		if (sysctl(mib, 2, NULL, &len, NULL, 0) < 0)
460 			goto nogeom;
461 		disklist = malloc(len);
462 		if (disklist == NULL) {
463 			fprintf(stderr, "Out of memory\n");
464 			return -1;
465 		}
466 		sysctl(mib, 2, disklist, &len, NULL, 0);
467 	}
468 
469 	for (i = 0; i < disklist->dl_nnativedisks; i++) {
470 		nat = &disklist->dl_nativedisks[i];
471 		if (!strcmp(dev, nat->ni_devname)) {
472 			nip = nat;
473 			break;
474 		}
475 	}
476 	if (nip == NULL || nip->ni_nmatches == 0) {
477 nogeom:
478 		if (nip != NULL)
479 			msg_display(MSG_nobiosgeom, pm->dlcyl, pm->dlhead, pm->dlsec);
480 		if (guess_biosgeom_from_mbr(&mbr, &cyl, &head, &sec) >= 0
481 		    && nip != NULL)
482 			msg_display_add(MSG_biosguess, cyl, head, sec);
483 		biosdisk = NULL;
484 	} else {
485 		guess_biosgeom_from_mbr(&mbr, &cyl, &head, &sec);
486 		if (nip->ni_nmatches == 1) {
487 			bip = &disklist->dl_biosdisks[nip->ni_biosmatches[0]];
488 			msg_display(MSG_onebiosmatch);
489 			msg_table_add(MSG_onebiosmatch_header);
490 			msg_table_add(MSG_onebiosmatch_row, bip->bi_dev,
491 			    bip->bi_cyl, bip->bi_head, bip->bi_sec,
492 			    (unsigned)bip->bi_lbasecs,
493 			    (unsigned)(bip->bi_lbasecs / (1000000000 / 512)));
494 			msg_display_add(MSG_biosgeom_advise);
495 			biosdisk = bip;
496 			process_menu(MENU_biosonematch, &biosdisk);
497 		} else {
498 			msg_display(MSG_biosmultmatch);
499 			msg_table_add(MSG_biosmultmatch_header);
500 			for (i = 0; i < nip->ni_nmatches; i++) {
501 				bip = &disklist->dl_biosdisks[
502 							nip->ni_biosmatches[i]];
503 				msg_table_add(MSG_biosmultmatch_row, i,
504 				    bip->bi_dev, bip->bi_cyl, bip->bi_head,
505 				    bip->bi_sec, (unsigned)bip->bi_lbasecs,
506 				    (unsigned)bip->bi_lbasecs/(1000000000/512));
507 			}
508 			process_menu(MENU_biosmultmatch, &i);
509 			if (i == -1)
510 				biosdisk = NULL;
511 			else
512 				biosdisk = &disklist->dl_biosdisks[
513 							nip->ni_biosmatches[i]];
514 		}
515 	}
516 	if (biosdisk == NULL) {
517 		if (nip != NULL) {
518 			set_bios_geom(cyl, head, sec);
519 		} else {
520 			bcyl = cyl;
521 			bhead = head;
522 			bsec = sec;
523 		}
524 	} else {
525 		bcyl = biosdisk->bi_cyl;
526 		bhead = biosdisk->bi_head;
527 		bsec = biosdisk->bi_sec;
528 	}
529 	return 0;
530 }
531 
532 static int
533 mbr_root_above_chs(void)
534 {
535 	return pm->ptstart + DEFROOTSIZE * (MEG / 512) >= bcyl * bhead * bsec;
536 }
537 
538 static void
539 md_upgrade_mbrtype(void)
540 {
541 	struct mbr_partition *mbrp;
542 	int i, netbsdpart = -1, oldbsdpart = -1, oldbsdcount = 0;
543 
544 	if (pm->no_mbr)
545 		return;
546 
547 	if (read_mbr(pm->diskdev, &mbr) < 0)
548 		return;
549 
550 	mbrp = &mbr.mbr.mbr_parts[0];
551 
552 	for (i = 0; i < MBR_PART_COUNT; i++) {
553 		if (mbrp[i].mbrp_type == MBR_PTYPE_386BSD) {
554 			oldbsdpart = i;
555 			oldbsdcount++;
556 		} else if (mbrp[i].mbrp_type == MBR_PTYPE_NETBSD)
557 			netbsdpart = i;
558 	}
559 
560 	if (netbsdpart == -1 && oldbsdcount == 1) {
561 		mbrp[oldbsdpart].mbrp_type = MBR_PTYPE_NETBSD;
562 		write_mbr(pm->diskdev, &mbr, 0);
563 	}
564 }
565 
566 /*
567  * Read MBR code from a file.
568  * The existing partition table and bootselect configuration is kept.
569  */
570 static int
571 md_read_bootcode(const char *path, struct mbr_sector *mbrs)
572 {
573 	int fd;
574 	struct stat st;
575 	size_t len;
576 	struct mbr_sector new_mbr;
577 	uint32_t dsn;
578 
579 	fd = open(path, O_RDONLY);
580 	if (fd < 0)
581 		return -1;
582 
583 	if (fstat(fd, &st) < 0 || st.st_size != sizeof *mbrs) {
584 		close(fd);
585 		return -1;
586 	}
587 
588 	if (read(fd, &new_mbr, sizeof new_mbr) != sizeof new_mbr) {
589 		close(fd);
590 		return -1;
591 	}
592 	close(fd);
593 
594 	if (new_mbr.mbr_bootsel_magic != htole16(MBR_BS_MAGIC))
595 		return -1;
596 
597 	if (mbrs->mbr_bootsel_magic == htole16(MBR_BS_MAGIC)) {
598 		len = offsetof(struct mbr_sector, mbr_bootsel);
599 	} else
600 		len = offsetof(struct mbr_sector, mbr_parts);
601 
602 	/* Preserve the 'drive serial number' - especially for Vista */
603 	dsn = mbrs->mbr_dsn;
604 	memcpy(mbrs, &new_mbr, len);
605 	mbrs->mbr_dsn = dsn;
606 
607 	/* Keep flags from object file - indicate the properties */
608 	mbrs->mbr_bootsel.mbrbs_flags = new_mbr.mbr_bootsel.mbrbs_flags;
609 	mbrs->mbr_magic = htole16(MBR_MAGIC);
610 
611 	return 0;
612 }
613 
614 static unsigned int
615 get_bootmodel(void)
616 {
617 #if defined(__i386__)
618 	struct utsname ut;
619 #ifdef DEBUG
620 	char *envstr;
621 
622 	envstr = getenv("BOOTMODEL");
623 	if (envstr != NULL)
624 		return atoi(envstr);
625 #endif
626 
627 	if (uname(&ut) < 0)
628 		ut.version[0] = 0;
629 
630 #if defined(SET_KERNEL_TINY)
631 	if (strstr(ut.version, "TINY") != NULL)
632 		return SET_KERNEL_TINY;
633 #endif
634 #if defined(SET_KERNEL_PS2)
635 	if (strstr(ut.version, "PS2") != NULL)
636 		return SET_KERNEL_PS2;
637 #endif
638 #endif
639 	return SET_KERNEL_GENERIC;
640 }
641 
642 
643 int
644 md_pre_mount()
645 {
646 	return 0;
647 }
648