md.c revision 1.7 1 /* $NetBSD: md.c,v 1.7 2017/09/11 15:24:28 gson 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, deconst(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, nelem(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 process_menu(MENU_getboottype, &boottype);
335 msg_display(MSG_dobootblks, pm->diskdev);
336 if (boottype.bp_consdev == ~0u)
337 /* Use existing bootblocks */
338 return 0;
339
340 ret = cp_to_target("/usr/mdec/boot", "/boot");
341 if (ret)
342 return ret;
343
344 bootxx_filename = bootxx_name();
345 if (bootxx_filename != NULL) {
346 snprintf(boot_options, sizeof boot_options,
347 "console=%s,speed=%u", consoles[boottype.bp_consdev],
348 boottype.bp_conspeed);
349 if (pm->isspecial) {
350 ret = run_program(RUN_DISPLAY,
351 "/usr/sbin/installboot -o %s /dev/r%s %s",
352 boot_options, pm->diskdev, bootxx_filename);
353 } else {
354 ret = run_program(RUN_DISPLAY,
355 "/usr/sbin/installboot -o %s /dev/r%s%c %s",
356 boot_options, pm->diskdev, 'a' + pm->rootpart,
357 bootxx_filename);
358 }
359 free(bootxx_filename);
360 } else
361 ret = -1;
362
363 if (ret != 0)
364 process_menu(MENU_ok,
365 deconst("Warning: disk is probably not bootable"));
366
367 return ret;
368 }
369
370 int
371 md_post_extract(void)
372 {
373 return 0;
374 }
375
376 void
377 md_cleanup_install(void)
378 {
379 #ifndef DEBUG
380 enable_rc_conf();
381 add_rc_conf("wscons=YES\n");
382
383 # if defined(__i386__) && defined(SET_KERNEL_TINY)
384 /*
385 * For GENERIC_TINY, do not enable any extra screens or wsmux.
386 * Otherwise, run getty on 4 VTs.
387 */
388 if (get_kernel_set() == SET_KERNEL_TINY)
389 run_program(RUN_CHROOT,
390 "sed -an -e '/^screen/s/^/#/;/^mux/s/^/#/;"
391 "H;$!d;g;w /etc/wscons.conf' /etc/wscons.conf");
392 else
393 # endif
394 run_program(RUN_CHROOT,
395 "sed -an -e '/^ttyE[1-9]/s/off/on/;"
396 "H;$!d;g;w /etc/ttys' /etc/ttys");
397
398 #endif
399 }
400
401 int
402 md_pre_update(void)
403 {
404 return 1;
405 }
406
407 /* Upgrade support */
408 int
409 md_update(void)
410 {
411 md_post_newfs();
412 md_upgrade_mbrtype();
413 return 1;
414 }
415
416 int
417 md_check_mbr(mbr_info_t *mbri)
418 {
419 return 2;
420 }
421
422 int
423 md_mbr_use_wholedisk(mbr_info_t *mbri)
424 {
425 return mbr_use_wholedisk(mbri);
426 }
427
428 static int
429 get_bios_info(char *dev)
430 {
431 static struct disklist *disklist = NULL;
432 static int mib[2] = {CTL_MACHDEP, CPU_DISKINFO};
433 int i;
434 size_t len;
435 struct biosdisk_info *bip;
436 struct nativedisk_info *nip = NULL, *nat;
437 int cyl, head;
438 daddr_t sec;
439
440 if (disklist == NULL) {
441 if (sysctl(mib, 2, NULL, &len, NULL, 0) < 0)
442 goto nogeom;
443 disklist = malloc(len);
444 if (disklist == NULL) {
445 fprintf(stderr, "Out of memory\n");
446 return -1;
447 }
448 sysctl(mib, 2, disklist, &len, NULL, 0);
449 }
450
451 for (i = 0; i < disklist->dl_nnativedisks; i++) {
452 nat = &disklist->dl_nativedisks[i];
453 if (!strcmp(dev, nat->ni_devname)) {
454 nip = nat;
455 break;
456 }
457 }
458 if (nip == NULL || nip->ni_nmatches == 0) {
459 nogeom:
460 if (nip != NULL)
461 msg_display(MSG_nobiosgeom, pm->dlcyl, pm->dlhead, pm->dlsec);
462 if (guess_biosgeom_from_mbr(&mbr, &cyl, &head, &sec) >= 0
463 && nip != NULL)
464 msg_display_add(MSG_biosguess, cyl, head, sec);
465 biosdisk = NULL;
466 } else {
467 guess_biosgeom_from_mbr(&mbr, &cyl, &head, &sec);
468 if (nip->ni_nmatches == 1) {
469 bip = &disklist->dl_biosdisks[nip->ni_biosmatches[0]];
470 msg_display(MSG_onebiosmatch);
471 msg_table_add(MSG_onebiosmatch_header);
472 msg_table_add(MSG_onebiosmatch_row, bip->bi_dev,
473 bip->bi_cyl, bip->bi_head, bip->bi_sec,
474 (unsigned)bip->bi_lbasecs,
475 (unsigned)(bip->bi_lbasecs / (1000000000 / 512)));
476 msg_display_add(MSG_biosgeom_advise);
477 biosdisk = bip;
478 process_menu(MENU_biosonematch, &biosdisk);
479 } else {
480 msg_display(MSG_biosmultmatch);
481 msg_table_add(MSG_biosmultmatch_header);
482 for (i = 0; i < nip->ni_nmatches; i++) {
483 bip = &disklist->dl_biosdisks[
484 nip->ni_biosmatches[i]];
485 msg_table_add(MSG_biosmultmatch_row, i,
486 bip->bi_dev, bip->bi_cyl, bip->bi_head,
487 bip->bi_sec, (unsigned)bip->bi_lbasecs,
488 (unsigned)bip->bi_lbasecs/(1000000000/512));
489 }
490 process_menu(MENU_biosmultmatch, &i);
491 if (i == -1)
492 biosdisk = NULL;
493 else
494 biosdisk = &disklist->dl_biosdisks[
495 nip->ni_biosmatches[i]];
496 }
497 }
498 if (biosdisk == NULL) {
499 if (nip != NULL) {
500 set_bios_geom(cyl, head, sec);
501 } else {
502 bcyl = cyl;
503 bhead = head;
504 bsec = sec;
505 }
506 } else {
507 bcyl = biosdisk->bi_cyl;
508 bhead = biosdisk->bi_head;
509 bsec = biosdisk->bi_sec;
510 }
511 return 0;
512 }
513
514 static int
515 mbr_root_above_chs(void)
516 {
517 return pm->ptstart + DEFROOTSIZE * (MEG / 512) >= bcyl * bhead * bsec;
518 }
519
520 static void
521 md_upgrade_mbrtype(void)
522 {
523 struct mbr_partition *mbrp;
524 int i, netbsdpart = -1, oldbsdpart = -1, oldbsdcount = 0;
525
526 if (pm->no_mbr)
527 return;
528
529 if (read_mbr(pm->diskdev, &mbr) < 0)
530 return;
531
532 mbrp = &mbr.mbr.mbr_parts[0];
533
534 for (i = 0; i < MBR_PART_COUNT; i++) {
535 if (mbrp[i].mbrp_type == MBR_PTYPE_386BSD) {
536 oldbsdpart = i;
537 oldbsdcount++;
538 } else if (mbrp[i].mbrp_type == MBR_PTYPE_NETBSD)
539 netbsdpart = i;
540 }
541
542 if (netbsdpart == -1 && oldbsdcount == 1) {
543 mbrp[oldbsdpart].mbrp_type = MBR_PTYPE_NETBSD;
544 write_mbr(pm->diskdev, &mbr, 0);
545 }
546 }
547
548 /*
549 * Read MBR code from a file.
550 * The existing partition table and bootselect configuration is kept.
551 */
552 static int
553 md_read_bootcode(const char *path, struct mbr_sector *mbrs)
554 {
555 int fd;
556 struct stat st;
557 size_t len;
558 struct mbr_sector new_mbr;
559 uint32_t dsn;
560
561 fd = open(path, O_RDONLY);
562 if (fd < 0)
563 return -1;
564
565 if (fstat(fd, &st) < 0 || st.st_size != sizeof *mbrs) {
566 close(fd);
567 return -1;
568 }
569
570 if (read(fd, &new_mbr, sizeof new_mbr) != sizeof new_mbr) {
571 close(fd);
572 return -1;
573 }
574 close(fd);
575
576 if (new_mbr.mbr_bootsel_magic != htole16(MBR_BS_MAGIC))
577 return -1;
578
579 if (mbrs->mbr_bootsel_magic == htole16(MBR_BS_MAGIC)) {
580 len = offsetof(struct mbr_sector, mbr_bootsel);
581 } else
582 len = offsetof(struct mbr_sector, mbr_parts);
583
584 /* Preserve the 'drive serial number' - especially for Vista */
585 dsn = mbrs->mbr_dsn;
586 memcpy(mbrs, &new_mbr, len);
587 mbrs->mbr_dsn = dsn;
588
589 /* Keep flags from object file - indicate the properties */
590 mbrs->mbr_bootsel.mbrbs_flags = new_mbr.mbr_bootsel.mbrbs_flags;
591 mbrs->mbr_magic = htole16(MBR_MAGIC);
592
593 return 0;
594 }
595
596 static unsigned int
597 get_bootmodel(void)
598 {
599 #if defined(__i386__)
600 struct utsname ut;
601 #ifdef DEBUG
602 char *envstr;
603
604 envstr = getenv("BOOTMODEL");
605 if (envstr != NULL)
606 return atoi(envstr);
607 #endif
608
609 if (uname(&ut) < 0)
610 ut.version[0] = 0;
611
612 #if defined(SET_KERNEL_TINY)
613 if (strstr(ut.version, "TINY") != NULL)
614 return SET_KERNEL_TINY;
615 #endif
616 #if defined(SET_KERNEL_PS2)
617 if (strstr(ut.version, "PS2") != NULL)
618 return SET_KERNEL_PS2;
619 #endif
620 #endif
621 return SET_KERNEL_GENERIC;
622 }
623
624
625 int
626 md_pre_mount()
627 {
628 return 0;
629 }
630