md.c revision 1.32 1 /* $NetBSD: md.c,v 1.32 2020/10/12 16:14:34 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 -- 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 <assert.h>
45 #include <stdio.h>
46 #include <stddef.h>
47 #include <util.h>
48 #include <dirent.h>
49 #include <termios.h>
50
51 #include "defs.h"
52 #include "md.h"
53 #include "endian.h"
54 #include "msg_defs.h"
55 #include "menu_defs.h"
56
57 #ifdef NO_LBA_READS /* for testing */
58 #undef BIFLAG_EXTINT13
59 #define BIFLAG_EXTINT13 0
60 #endif
61
62 static struct biosdisk_info *biosdisk = NULL;
63 static bool uefi_boot;
64
65 /* prototypes */
66
67 static bool get_bios_info(const char*, struct disk_partitions*, int*, int*, int*);
68 static int mbr_root_above_chs(daddr_t);
69 static int md_read_bootcode(const char *, struct mbr_sector *);
70 static unsigned int get_bootmodel(void);
71
72 static int conmib[] = { CTL_MACHDEP, CPU_CONSDEV };
73
74 #define BOOT_PART (128*(MEG/512))
75 #define BOOT_PART_TYPE PT_EFI_SYSTEM
76
77 static const char * uefi_bootloaders[] = {
78 "/usr/mdec/bootia32.efi",
79 "/usr/mdec/bootx64.efi",
80 };
81
82 void
83 md_init(void)
84 {
85 char boot_method[100];
86 size_t len;
87
88 len = sizeof(boot_method);
89 if (sysctlbyname("machdep.bootmethod", boot_method, &len, NULL, 0)
90 != -1) {
91 if (strcmp(boot_method, "BIOS") == 0)
92 uefi_boot = false;
93 else if (strcmp(boot_method, "UEFI") == 0)
94 uefi_boot = true;
95 }
96 }
97
98 void
99 md_init_set_status(int flags)
100 {
101 (void)flags;
102
103 /* Default to install same type of kernel as we are running */
104 set_kernel_set(get_bootmodel());
105 }
106
107 bool
108 md_get_info(struct install_partition_desc *install)
109 {
110 int bcyl = 0, bhead = 0, bsec = 0, res;
111
112 if (pm->no_mbr || pm->no_part)
113 return true;
114
115 again:
116 if (pm->parts == NULL) {
117
118 const struct disk_partitioning_scheme *ps =
119 select_part_scheme(pm, NULL, true, NULL);
120
121 if (!ps)
122 return false;
123
124 struct disk_partitions *parts =
125 (*ps->create_new_for_disk)(pm->diskdev,
126 0, pm->dlsize, true, NULL);
127 if (!parts)
128 return false;
129
130 pm->parts = parts;
131 if (ps->size_limit > 0 && pm->dlsize > ps->size_limit)
132 pm->dlsize = ps->size_limit;
133 }
134
135 if (get_bios_info(pm->diskdev, pm->parts, &bcyl, &bhead, &bsec)
136 && pm->parts->pscheme->change_disk_geom != NULL)
137 pm->parts->pscheme->change_disk_geom(pm->parts,
138 bcyl, bhead, bsec);
139 else
140 set_default_sizemult(pm->diskdev, MEG, pm->sectorsize);
141
142 /*
143 * If the selected scheme does not need two-stage partitioning
144 * (like GPT), do not bother to edit the outer partitions.
145 */
146 if (pm->parts->pscheme->secondary_partitions == NULL ||
147 pm->parts->pscheme->secondary_scheme == NULL)
148 return true;
149
150 if (pm->no_mbr || pm->no_part)
151 return true;
152
153 res = edit_outer_parts(pm->parts);
154 if (res == 0)
155 return false;
156 else if (res == 1)
157 return true;
158
159 pm->parts->pscheme->destroy_part_scheme(pm->parts);
160 pm->parts = NULL;
161 goto again;
162 }
163
164 /*
165 * md back-end code for menu-driven BSD disklabel editor.
166 */
167 int
168 md_make_bsd_partitions(struct install_partition_desc *install)
169 {
170 return make_bsd_partitions(install);
171 }
172
173 /*
174 * any additional partition validation
175 */
176 bool
177 md_check_partitions(struct install_partition_desc *install)
178 {
179 int rval;
180 char *bootxx;
181
182 /* if booting via UEFI no boot blocks are needed */
183 if (uefi_boot)
184 return true;
185
186 /* check we have boot code for the root partition type */
187 bootxx = bootxx_name(install);
188 rval = access(bootxx, R_OK);
189 free(bootxx);
190 if (rval == 0)
191 return true;
192 process_menu(MENU_ok, __UNCONST(MSG_No_Bootcode));
193 return false;
194 }
195
196 /*
197 * hook called before writing new disklabel.
198 */
199 bool
200 md_pre_disklabel(struct install_partition_desc *install,
201 struct disk_partitions *parts)
202 {
203
204 if (parts->parent == NULL)
205 return true; /* no outer partitions */
206
207 parts = parts->parent;
208
209 msg_display_subst(MSG_dofdisk, 3, parts->disk,
210 msg_string(parts->pscheme->name),
211 msg_string(parts->pscheme->short_name));
212
213 /* write edited "MBR" onto disk. */
214 if (!parts->pscheme->write_to_disk(parts)) {
215 msg_display(MSG_wmbrfail);
216 process_menu(MENU_ok, NULL);
217 return false;
218 }
219 return true;
220 }
221
222 /*
223 * hook called after writing disklabel to new target disk.
224 */
225 bool
226 md_post_disklabel(struct install_partition_desc *install,
227 struct disk_partitions *parts)
228 {
229 return true;
230 }
231
232 /*
233 * Do all legacy bootblock update/setup here
234 */
235 static int
236 md_post_newfs_bios(struct install_partition_desc *install)
237 {
238 int ret;
239 size_t len;
240 char boot_options[1024];
241 char *bootxx_filename;
242 /*
243 * XXX - this code retains a lot of cruft from when we went
244 * to great pains to exclude installboot from the ramdisk
245 * for size reasons and should be rewritten.
246 */
247 static const char *consoles[]={
248 "pc", /* CONSDEV_PC */
249 "com0", /* CONSDEV_COM0 */
250 "com1", /* CONSDEV_COM1 */
251 "com2", /* CONSDEV_COM2 */
252 "com3", /* CONSDEV_COM3 */
253 "com0kbd", /* CONSDEV_COM0KBD */
254 "com1kbd", /* CONSDEV_COM1KBD */
255 "com2kbd", /* CONSDEV_COM2KBD */
256 "com3kbd" /* CONSDEV_COM3KBD */ };
257 static struct x86_boot_params boottype =
258 {sizeof boottype, 0, 5, 0, 9600, { '\0' }, "", 0};
259 struct termios t;
260 dev_t condev;
261
262 if (pm == NULL || !pm->no_part) {
263 /*
264 * Get console device, should either be ttyE0 or tty0n.
265 * Too hard to double check, so just 'know' the device numbers.
266 */
267 len = sizeof condev;
268 if (sysctl(conmib, __arraycount(conmib), &condev, &len, NULL,
269 0) != -1 && (condev & ~3) == 0x800) {
270 /* Motherboard serial port */
271 boottype.bp_consdev = (condev & 3) + 1;
272 /* Defaulting the baud rate to that of stdin should
273 suffice */
274 if (tcgetattr(0, &t) != -1)
275 boottype.bp_conspeed = t.c_ispeed;
276 }
277
278 process_menu(MENU_getboottype, &boottype);
279 msg_fmt_display(MSG_dobootblks, "%s", pm->diskdev);
280 if (boottype.bp_consdev == ~0u)
281 /* Use existing bootblocks */
282 return 0;
283 }
284
285 ret = cp_to_target("/usr/mdec/boot", "/boot");
286 if (ret)
287 return ret;
288 if (pm && pm->no_part)
289 return 0;
290
291 bootxx_filename = bootxx_name(install);
292 if (bootxx_filename != NULL) {
293 char rdev[PATH_MAX];
294
295 install->infos[0].parts->pscheme->get_part_device(
296 install->infos[0].parts, install->infos[0].cur_part_id,
297 rdev, sizeof rdev, NULL, raw_dev_name, true, true);
298
299 snprintf(boot_options, sizeof boot_options,
300 "console=%s,speed=%u", consoles[boottype.bp_consdev],
301 boottype.bp_conspeed);
302 ret = run_program(RUN_DISPLAY,
303 "/usr/sbin/installboot -f -o %s %s %s",
304 boot_options, rdev, bootxx_filename);
305 free(bootxx_filename);
306 } else {
307 ret = -1;
308 }
309
310 if (ret != 0)
311 process_menu(MENU_ok,
312 __UNCONST("Warning: disk is probably not bootable"));
313
314 return ret;
315 }
316
317 /*
318 * Make sure our bootloader(s) are in the proper directory in the boot
319 * boot partition (or update them).
320 */
321 static int
322 copy_uefi_boot(const struct part_usage_info *boot)
323 {
324 char dev[MAXPATHLEN], path[MAXPATHLEN];
325 size_t i;
326 int err;
327
328 if (!boot->parts->pscheme->get_part_device(boot->parts,
329 boot->cur_part_id, dev, sizeof(dev), NULL, plain_name, true, true))
330 return -1;
331
332 /*
333 * We should have a valid file system on that partition.
334 * Try to mount it and check if there is a /EFI in there.
335 */
336 if (boot->mount[0])
337 strlcpy(path, boot->mount, sizeof(path));
338 else
339 strcpy(path, "/mnt");
340
341 if (!(boot->instflags & PUIINST_MOUNT)) {
342 make_target_dir(path);
343 err = target_mount("", dev, path);
344 if (err != 0)
345 return err;
346 }
347
348 strlcat(path, "/EFI/boot", sizeof(path));
349 make_target_dir(path);
350
351 for (i = 0; i < __arraycount(uefi_bootloaders); i++) {
352 if (access(uefi_bootloaders[i], R_OK) != 0)
353 continue;
354 err = cp_to_target(uefi_bootloaders[i], path);
355 if (err)
356 return err;
357 }
358
359 return 0;
360 }
361
362 /*
363 * Find (U)EFI boot partition and install/update bootloaders
364 */
365 static int
366 md_post_newfs_uefi(struct install_partition_desc *install)
367 {
368 size_t i;
369
370 for (i = 0; i < install->num; i++) {
371 if (!(install->infos[i].instflags & PUIINST_BOOT))
372 continue;
373
374 return copy_uefi_boot(&install->infos[i]);
375 }
376
377 return -1; /* no EFI boot partition found */
378 }
379
380 /*
381 * hook called after upgrade() or install() has finished setting
382 * up the target disk but immediately before the user is given the
383 * ``disks are now set up'' message.
384 */
385 int
386 md_post_newfs(struct install_partition_desc *install)
387 {
388
389 return uefi_boot ? md_post_newfs_uefi(install)
390 : md_post_newfs_bios(install);
391 }
392
393 int
394 md_post_extract(struct install_partition_desc *install)
395 {
396 #if defined(__amd64__)
397 if (get_kernel_set() == SET_KERNEL_2) {
398 int ret;
399
400 ret = cp_within_target("/usr/mdec/prekern", "/prekern", 0);
401 if (ret)
402 return ret;
403 }
404 #endif
405 return 0;
406 }
407
408 void
409 md_cleanup_install(struct install_partition_desc *install)
410 {
411 size_t len;
412 dev_t condev;
413
414 #ifndef DEBUG
415 enable_rc_conf();
416 add_rc_conf("wscons=YES\n");
417
418 # if defined(__i386__) && defined(SET_KERNEL_TINY)
419 /*
420 * For GENERIC_TINY, do not enable any extra screens or wsmux.
421 * Otherwise, run getty on 4 VTs.
422 */
423 if (get_kernel_set() == SET_KERNEL_TINY)
424 run_program(RUN_CHROOT,
425 "sed -an -e '/^screen/s/^/#/;/^mux/s/^/#/;"
426 "H;$!d;g;w /etc/wscons.conf' /etc/wscons.conf");
427 else
428 # endif
429 run_program(RUN_CHROOT,
430 "sed -an -e '/^ttyE[1-9]/s/off/on/;"
431 "H;$!d;g;w /etc/ttys' /etc/ttys");
432
433 #endif
434
435 /*
436 * Get console device, should either be ttyE0 or tty0n.
437 * Too hard to double check, so just 'know' the device numbers.
438 */
439 len = sizeof condev;
440 if (sysctl(conmib, __arraycount(conmib), &condev, &len, NULL, 0) != -1
441 && (condev & ~3) != 0x800) {
442
443 /*
444 * Current console is not com*, assume ttyE*.
445 * Modify /etc/ttys to use wsvt25 for all ports.
446 */
447
448 run_program(RUN_CHROOT,
449 "sed -an -e 's/vt100/wsvt25/g;"
450 "H;$!d;g;w /etc/ttys' /etc/ttys");
451 }
452 }
453
454 int
455 md_pre_update(struct install_partition_desc *install)
456 {
457 return 1;
458 }
459
460 /* Upgrade support */
461 int
462 md_update(struct install_partition_desc *install)
463 {
464 md_post_newfs(install);
465 return 1;
466 }
467
468 int
469 md_check_mbr(struct disk_partitions *parts, mbr_info_t *mbri, bool quiet)
470 {
471 mbr_info_t *ext;
472 struct mbr_partition *p;
473 const char *bootcode;
474 daddr_t inst_start, inst_size;
475 int i, names, fl, ofl;
476 #define ACTIVE_FOUND 0x0100
477 #define NETBSD_ACTIVE 0x0200
478 #define NETBSD_NAMED 0x0400
479 #define ACTIVE_NAMED 0x0800
480
481 root_limit = 0;
482 if (parts->pscheme->guess_install_target == NULL ||
483 !parts->pscheme->guess_install_target(parts, &inst_start,
484 &inst_size)) {
485 inst_start = parts->disk_start;
486 inst_size = parts->disk_size;
487 }
488
489 if (biosdisk != NULL && (biosdisk->bi_flags & BIFLAG_EXTINT13) == 0) {
490 if (mbr_root_above_chs(inst_start)) {
491 if (quiet)
492 return 0;
493 msg_display(MSG_partabovechs);
494 if (!ask_noyes(NULL))
495 return 1;
496 /* The user is shooting themselves in the foot here...*/
497 } else {
498 if (parts->pscheme->size_limit)
499 root_limit = min(parts->pscheme->size_limit,
500 parts->disk_size);
501 else
502 root_limit = parts->disk_size;
503 }
504 }
505
506 /*
507 * Ensure the install partition (at sector inst_start) and the active
508 * partition are bootable.
509 * Determine whether the bootselect code is needed.
510 * Note that MBR_BS_NEWMBR is always set, so we ignore it!
511 */
512 fl = 0;
513 names = 0;
514 for (ext = mbri; ext != NULL; ext = ext->extended) {
515 p = ext->mbr.mbr_parts;
516 for (i = 0; i < MBR_PART_COUNT; p++, i++) {
517 if (p->mbrp_flag == MBR_PFLAG_ACTIVE) {
518 fl |= ACTIVE_FOUND;
519 if (ext->sector + p->mbrp_start == inst_start)
520 fl |= NETBSD_ACTIVE;
521 }
522 if (ext->mbrb.mbrbs_nametab[i][0] == 0) {
523 /* No bootmenu label... */
524 if (ext->sector == 0)
525 continue;
526 if (ext->sector + p->mbrp_start == inst_start)
527 /*
528 * Have installed into an extended ptn
529 * force name & bootsel...
530 */
531 names++;
532 continue;
533 }
534 /* Partition has a bootmenu label... */
535 if (ext->sector != 0)
536 fl |= MBR_BS_EXTLBA;
537 if (ext->sector + p->mbrp_start == inst_start)
538 fl |= NETBSD_NAMED;
539 else if (p->mbrp_flag == MBR_PFLAG_ACTIVE)
540 fl |= ACTIVE_NAMED;
541 else
542 names++;
543 }
544 }
545 if (!(fl & ACTIVE_FOUND))
546 fl |= NETBSD_ACTIVE;
547 if (fl & NETBSD_NAMED && fl & NETBSD_ACTIVE)
548 fl |= ACTIVE_NAMED;
549
550 if ((names > 0 || !(fl & NETBSD_ACTIVE)) &&
551 (!(fl & NETBSD_NAMED) || !(fl & ACTIVE_NAMED))) {
552 /*
553 * There appear to be multiple bootable partitions, but they
554 * don't all have bootmenu texts.
555 */
556 if (quiet)
557 return 0;
558
559 msg_display(MSG_missing_bootmenu_text);
560 if (ask_yesno(NULL))
561 return 1;
562 }
563
564 if ((fl & MBR_BS_EXTLBA) &&
565 (biosdisk == NULL || !(biosdisk->bi_flags & BIFLAG_EXTINT13))) {
566 /* Need unsupported LBA reads to read boot sectors */
567 if (quiet)
568 return 0;
569
570 msg_display(MSG_no_extended_bootmenu);
571 if (!ask_noyes(NULL))
572 return 1;
573 }
574
575 /* Sort out the name of the mbr code we need */
576 if (names > 0 || fl & (NETBSD_NAMED | ACTIVE_NAMED)) {
577 /* Need bootselect code */
578 fl |= MBR_BS_ACTIVE;
579 bootcode = fl & MBR_BS_EXTLBA ? _PATH_BOOTEXT : _PATH_BOOTSEL;
580 } else
581 bootcode = _PATH_MBR;
582
583 fl &= MBR_BS_ACTIVE | MBR_BS_EXTLBA;
584
585 /* Look at what is installed */
586 ofl = mbri->mbrb.mbrbs_flags;
587 if (ofl == 0) {
588 /* Check there is some bootcode at all... */
589 if (mbri->mbr.mbr_magic != htole16(MBR_MAGIC) ||
590 mbri->mbr.mbr_jmpboot[0] == 0 ||
591 mbr_root_above_chs(inst_start))
592 /* Existing won't do, force update */
593 fl |= MBR_BS_NEWMBR;
594 }
595 ofl = mbri->oflags & (MBR_BS_ACTIVE | MBR_BS_EXTLBA);
596
597 if (!quiet) {
598 if (fl & ~ofl || (fl == 0 && ofl & MBR_BS_ACTIVE)) {
599 /* Existing boot code isn't the right one... */
600 if (fl & MBR_BS_ACTIVE)
601 msg_display(MSG_installbootsel);
602 else
603 msg_display(MSG_installmbr);
604 } else
605 /* Existing code would (probably) be ok */
606 msg_display(MSG_updatembr);
607
608 if (!ask_yesno(NULL))
609 /* User doesn't want to update mbr code */
610 return 2;
611 }
612
613 if (md_read_bootcode(bootcode, &mbri->mbr) == 0)
614 /* update suceeded - to memory copy */
615 return 2;
616
617 /* This shouldn't happen since the files are in the floppy fs... */
618 msg_fmt_display("Can't find %s", "%s", bootcode);
619 return ask_reedit(parts);
620 }
621
622 bool
623 md_parts_use_wholedisk(struct disk_partitions *parts)
624 {
625 struct disk_part_info boot_part = {
626 .size = BOOT_PART,
627 .fs_type = FS_MSDOS, .fs_sub_type = MBR_PTYPE_FAT32L,
628 };
629
630 if (!uefi_boot)
631 return parts_use_wholedisk(parts, 0, NULL);
632
633 boot_part.nat_type = parts->pscheme->get_generic_part_type(
634 PT_EFI_SYSTEM);
635
636 return parts_use_wholedisk(parts, 1, &boot_part);
637 }
638
639 static bool
640 get_bios_info(const char *dev, struct disk_partitions *parts, int *bcyl,
641 int *bhead, int *bsec)
642 {
643 static struct disklist *disklist = NULL;
644 static int mib[2] = {CTL_MACHDEP, CPU_DISKINFO};
645 int i;
646 size_t len;
647 struct biosdisk_info *bip;
648 struct nativedisk_info *nip = NULL, *nat;
649 int cyl, head, sec;
650
651 if (disklist == NULL) {
652 if (sysctl(mib, 2, NULL, &len, NULL, 0) < 0)
653 goto nogeom;
654 disklist = malloc(len);
655 if (disklist == NULL) {
656 fprintf(stderr, "Out of memory\n");
657 return false;
658 }
659 sysctl(mib, 2, disklist, &len, NULL, 0);
660 }
661
662 for (i = 0; i < disklist->dl_nnativedisks; i++) {
663 nat = &disklist->dl_nativedisks[i];
664 if (!strcmp(dev, nat->ni_devname)) {
665 nip = nat;
666 break;
667 }
668 }
669 if (nip == NULL || nip->ni_nmatches == 0) {
670 nogeom:
671 if (nip != NULL)
672 msg_fmt_display(MSG_nobiosgeom, "%d%d%d",
673 pm->dlcyl, pm->dlhead, pm->dlsec);
674 if (guess_biosgeom_from_parts(parts, &cyl, &head, &sec) >= 0
675 && nip != NULL)
676 {
677 msg_fmt_display_add(MSG_biosguess, "%d%d%d",
678 cyl, head, sec);
679 }
680 biosdisk = NULL;
681 } else {
682 guess_biosgeom_from_parts(parts, &cyl, &head, &sec);
683 if (nip->ni_nmatches == 1) {
684 bip = &disklist->dl_biosdisks[nip->ni_biosmatches[0]];
685 msg_display(MSG_onebiosmatch);
686 msg_table_add(MSG_onebiosmatch_header);
687 msg_fmt_table_add(MSG_onebiosmatch_row, "%d%d%d%d%u%u",
688 bip->bi_dev, bip->bi_cyl, bip->bi_head, bip->bi_sec,
689 (unsigned)bip->bi_lbasecs,
690 (unsigned)(bip->bi_lbasecs / (1000000000 / 512)));
691 msg_display_add(MSG_biosgeom_advise);
692 biosdisk = bip;
693 process_menu(MENU_biosonematch, &biosdisk);
694 } else {
695 msg_display(MSG_biosmultmatch);
696 msg_table_add(MSG_biosmultmatch_header);
697 for (i = 0; i < nip->ni_nmatches; i++) {
698 bip = &disklist->dl_biosdisks[
699 nip->ni_biosmatches[i]];
700 msg_fmt_table_add(MSG_biosmultmatch_row,
701 "%d%d%d%d%d%u%u", i,
702 bip->bi_dev, bip->bi_cyl, bip->bi_head,
703 bip->bi_sec, (unsigned)bip->bi_lbasecs,
704 (unsigned)bip->bi_lbasecs/(1000000000/512));
705 }
706 process_menu(MENU_biosmultmatch, &i);
707 if (i == -1)
708 biosdisk = NULL;
709 else
710 biosdisk = &disklist->dl_biosdisks[
711 nip->ni_biosmatches[i]];
712 }
713 }
714 if (biosdisk == NULL) {
715 *bcyl = cyl;
716 *bhead = head;
717 *bsec = sec;
718 if (nip != NULL)
719 set_bios_geom(parts, bcyl, bhead, bsec);
720 } else {
721 *bcyl = biosdisk->bi_cyl;
722 *bhead = biosdisk->bi_head;
723 *bsec = biosdisk->bi_sec;
724 }
725 return true;
726 }
727
728 static int
729 mbr_root_above_chs(daddr_t ptstart)
730 {
731 return ptstart + (daddr_t)DEFROOTSIZE * (daddr_t)(MEG / 512)
732 >= pm->max_chs;
733 }
734
735 /* returns false if no write-back of parts is required */
736 bool
737 md_mbr_update_check(struct disk_partitions *parts, mbr_info_t *mbri)
738 {
739 struct mbr_partition *mbrp;
740 int i, netbsdpart = -1, oldbsdpart = -1, oldbsdcount = 0;
741
742 if (pm->no_mbr || pm->no_part)
743 return false;
744
745 mbrp = &mbri->mbr.mbr_parts[0];
746
747 for (i = 0; i < MBR_PART_COUNT; i++) {
748 if (mbrp[i].mbrp_type == MBR_PTYPE_386BSD) {
749 oldbsdpart = i;
750 oldbsdcount++;
751 } else if (mbrp[i].mbrp_type == MBR_PTYPE_NETBSD)
752 netbsdpart = i;
753 }
754
755 if (netbsdpart == -1 && oldbsdcount == 1) {
756 mbrp[oldbsdpart].mbrp_type = MBR_PTYPE_NETBSD;
757 return true;
758 }
759
760 return false;
761 }
762
763 /*
764 * Read MBR code from a file.
765 * The existing partition table and bootselect configuration is kept.
766 */
767 static int
768 md_read_bootcode(const char *path, struct mbr_sector *mbrs)
769 {
770 int fd;
771 struct stat st;
772 size_t len;
773 struct mbr_sector new_mbr;
774 uint32_t dsn;
775
776 fd = open(path, O_RDONLY);
777 if (fd < 0)
778 return -1;
779
780 if (fstat(fd, &st) < 0 || st.st_size != sizeof *mbrs) {
781 close(fd);
782 return -1;
783 }
784
785 if (read(fd, &new_mbr, sizeof new_mbr) != sizeof new_mbr) {
786 close(fd);
787 return -1;
788 }
789 close(fd);
790
791 if (new_mbr.mbr_bootsel_magic != htole16(MBR_BS_MAGIC))
792 return -1;
793
794 if (mbrs->mbr_bootsel_magic == htole16(MBR_BS_MAGIC)) {
795 len = offsetof(struct mbr_sector, mbr_bootsel);
796 } else
797 len = offsetof(struct mbr_sector, mbr_parts);
798
799 /* Preserve the 'drive serial number' - especially for Vista */
800 dsn = mbrs->mbr_dsn;
801 memcpy(mbrs, &new_mbr, len);
802 mbrs->mbr_dsn = dsn;
803
804 /* Keep flags from object file - indicate the properties */
805 mbrs->mbr_bootsel.mbrbs_flags = new_mbr.mbr_bootsel.mbrbs_flags;
806 mbrs->mbr_magic = htole16(MBR_MAGIC);
807
808 return 0;
809 }
810
811 static unsigned int
812 get_bootmodel(void)
813 {
814 #if defined(__i386__)
815 struct utsname ut;
816 #ifdef DEBUG
817 char *envstr;
818
819 envstr = getenv("BOOTMODEL");
820 if (envstr != NULL)
821 return atoi(envstr);
822 #endif
823
824 if (uname(&ut) < 0)
825 ut.version[0] = 0;
826
827 #if defined(SET_KERNEL_TINY)
828 if (strstr(ut.version, "TINY") != NULL)
829 return SET_KERNEL_TINY;
830 #endif
831 #if defined(SET_KERNEL_PS2)
832 if (strstr(ut.version, "PS2") != NULL)
833 return SET_KERNEL_PS2;
834 #endif
835 #endif
836 return SET_KERNEL_GENERIC;
837 }
838
839
840 int
841 md_pre_mount(struct install_partition_desc *install, size_t ndx)
842 {
843 return 0;
844 }
845
846 #ifdef HAVE_GPT
847 /*
848 * New GPT partitions have been written, update bootloader or remember
849 * data untill needed in md_post_newfs
850 */
851 bool
852 md_gpt_post_write(struct disk_partitions *parts, part_id root_id,
853 bool root_is_new, part_id efi_id, bool efi_is_new)
854 {
855 struct disk_part_info info;
856
857 if (root_id != NO_PART) {
858 /* we always update the gpt boot record for now */
859 if (!parts->pscheme->get_part_info(parts, root_id, &info))
860 return false;
861 if (run_program(RUN_SILENT, "gpt biosboot -b %" PRIu64 " %s",
862 info.start, parts->disk) != 0)
863 return false;
864 }
865
866 return true;
867 }
868 #endif
869
870 /*
871 * When we do an UEFI install, we have completely different default
872 * partitions and need to adjust the description at runtime.
873 */
874 void
875 x86_md_part_defaults(struct pm_devs *cur_pm, struct part_usage_info **partsp,
876 size_t *num_usage_infos)
877 {
878 static const struct part_usage_info uefi_boot_part =
879 {
880 .size = BOOT_PART,
881 .type = BOOT_PART_TYPE,
882 .instflags = PUIINST_NEWFS|PUIINST_BOOT,
883 .fs_type = FS_MSDOS, .fs_version = MBR_PTYPE_FAT32L,
884 .flags = PUIFLAG_ADD_OUTER,
885 };
886
887 struct disk_partitions *parts;
888 struct part_usage_info *new_usage, *boot;
889 struct disk_part_info info;
890 size_t num;
891 part_id pno;
892
893 if (!uefi_boot)
894 return; /* legacy defaults apply */
895
896 /*
897 * Insert a UEFI boot partition at the beginning of the array
898 */
899
900 /* create space for new description */
901 num = *num_usage_infos + 1;
902 new_usage = realloc(*partsp, sizeof(*new_usage)*num);
903 if (new_usage == NULL)
904 return;
905 *partsp = new_usage;
906 *num_usage_infos = num;
907 boot = new_usage;
908 memmove(boot+1, boot, sizeof(*boot)*(num-1));
909 *boot = uefi_boot_part;
910
911 /*
912 * Check if the UEFI partition already exists
913 */
914 parts = pm->parts;
915 if (parts->parent != NULL)
916 parts = parts->parent;
917 for (pno = 0; pno < parts->num_part; pno++) {
918 if (!parts->pscheme->get_part_info(parts, pno, &info))
919 continue;
920 if (info.nat_type->generic_ptype != boot->type)
921 continue;
922 boot->flags &= ~PUIFLAG_ADD_OUTER;
923 boot->flags |= PUIFLG_IS_OUTER|PUIFLG_ADD_INNER;
924 boot->size = info.size;
925 boot->cur_start = info.start;
926 boot->cur_flags = info.flags;
927 break;
928 }
929 }
930
931 /* no need to install bootblock if installing for UEFI */
932 bool
933 x86_md_need_bootblock(struct install_partition_desc *install)
934 {
935
936 return !uefi_boot;
937 }
938